1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Symmetric key ciphers.
4 *
5 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
6 */
7
8 #ifndef _CRYPTO_INTERNAL_SKCIPHER_H
9 #define _CRYPTO_INTERNAL_SKCIPHER_H
10
11 #include <crypto/algapi.h>
12 #include <crypto/internal/cipher.h>
13 #include <crypto/skcipher.h>
14 #include <linux/types.h>
15
16 /*
17 * Set this if your algorithm is sync but needs a reqsize larger
18 * than MAX_SYNC_SKCIPHER_REQSIZE.
19 *
20 * Reuse bit that is specific to hash algorithms.
21 */
22 #define CRYPTO_ALG_SKCIPHER_REQSIZE_LARGE CRYPTO_ALG_OPTIONAL_KEY
23
24 struct aead_request;
25 struct rtattr;
26
27 struct skcipher_instance {
28 void (*free)(struct skcipher_instance *inst);
29 union {
30 struct {
31 char head[offsetof(struct skcipher_alg, base)];
32 struct crypto_instance base;
33 } s;
34 struct skcipher_alg alg;
35 };
36 };
37
38 struct lskcipher_instance {
39 void (*free)(struct lskcipher_instance *inst);
40 union {
41 struct {
42 char head[offsetof(struct lskcipher_alg, co.base)];
43 struct crypto_instance base;
44 } s;
45 struct lskcipher_alg alg;
46 };
47 };
48
49 struct crypto_skcipher_spawn {
50 struct crypto_spawn base;
51 };
52
53 struct crypto_lskcipher_spawn {
54 struct crypto_spawn base;
55 };
56
57 struct skcipher_walk {
58 union {
59 /* Virtual address of the source. */
60 struct {
61 struct {
62 const void *const addr;
63 } virt;
64 } src;
65
66 /* Private field for the API, do not use. */
67 struct scatter_walk in;
68 };
69
70 union {
71 /* Virtual address of the destination. */
72 struct {
73 struct {
74 void *const addr;
75 } virt;
76 } dst;
77
78 /* Private field for the API, do not use. */
79 struct scatter_walk out;
80 };
81
82 unsigned int nbytes;
83 unsigned int total;
84
85 u8 *page;
86 u8 *buffer;
87 u8 *oiv;
88 void *iv;
89
90 unsigned int ivsize;
91
92 int flags;
93 unsigned int blocksize;
94 unsigned int stride;
95 unsigned int alignmask;
96 };
97
skcipher_crypto_instance(struct skcipher_instance * inst)98 static inline struct crypto_instance *skcipher_crypto_instance(
99 struct skcipher_instance *inst)
100 {
101 return &inst->s.base;
102 }
103
lskcipher_crypto_instance(struct lskcipher_instance * inst)104 static inline struct crypto_instance *lskcipher_crypto_instance(
105 struct lskcipher_instance *inst)
106 {
107 return &inst->s.base;
108 }
109
skcipher_alg_instance(struct crypto_skcipher * skcipher)110 static inline struct skcipher_instance *skcipher_alg_instance(
111 struct crypto_skcipher *skcipher)
112 {
113 return container_of(crypto_skcipher_alg(skcipher),
114 struct skcipher_instance, alg);
115 }
116
lskcipher_alg_instance(struct crypto_lskcipher * lskcipher)117 static inline struct lskcipher_instance *lskcipher_alg_instance(
118 struct crypto_lskcipher *lskcipher)
119 {
120 return container_of(crypto_lskcipher_alg(lskcipher),
121 struct lskcipher_instance, alg);
122 }
123
skcipher_instance_ctx(struct skcipher_instance * inst)124 static inline void *skcipher_instance_ctx(struct skcipher_instance *inst)
125 {
126 return crypto_instance_ctx(skcipher_crypto_instance(inst));
127 }
128
lskcipher_instance_ctx(struct lskcipher_instance * inst)129 static inline void *lskcipher_instance_ctx(struct lskcipher_instance *inst)
130 {
131 return crypto_instance_ctx(lskcipher_crypto_instance(inst));
132 }
133
skcipher_request_complete(struct skcipher_request * req,int err)134 static inline void skcipher_request_complete(struct skcipher_request *req, int err)
135 {
136 crypto_request_complete(&req->base, err);
137 }
138
139 int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
140 struct crypto_instance *inst,
141 const char *name, u32 type, u32 mask);
142
143 int crypto_grab_lskcipher(struct crypto_lskcipher_spawn *spawn,
144 struct crypto_instance *inst,
145 const char *name, u32 type, u32 mask);
146
crypto_drop_skcipher(struct crypto_skcipher_spawn * spawn)147 static inline void crypto_drop_skcipher(struct crypto_skcipher_spawn *spawn)
148 {
149 crypto_drop_spawn(&spawn->base);
150 }
151
crypto_drop_lskcipher(struct crypto_lskcipher_spawn * spawn)152 static inline void crypto_drop_lskcipher(struct crypto_lskcipher_spawn *spawn)
153 {
154 crypto_drop_spawn(&spawn->base);
155 }
156
crypto_lskcipher_spawn_alg(struct crypto_lskcipher_spawn * spawn)157 static inline struct lskcipher_alg *crypto_lskcipher_spawn_alg(
158 struct crypto_lskcipher_spawn *spawn)
159 {
160 return container_of(spawn->base.alg, struct lskcipher_alg, co.base);
161 }
162
crypto_spawn_skcipher_alg_common(struct crypto_skcipher_spawn * spawn)163 static inline struct skcipher_alg_common *crypto_spawn_skcipher_alg_common(
164 struct crypto_skcipher_spawn *spawn)
165 {
166 return container_of(spawn->base.alg, struct skcipher_alg_common, base);
167 }
168
crypto_spawn_lskcipher_alg(struct crypto_lskcipher_spawn * spawn)169 static inline struct lskcipher_alg *crypto_spawn_lskcipher_alg(
170 struct crypto_lskcipher_spawn *spawn)
171 {
172 return crypto_lskcipher_spawn_alg(spawn);
173 }
174
crypto_spawn_skcipher(struct crypto_skcipher_spawn * spawn)175 static inline struct crypto_skcipher *crypto_spawn_skcipher(
176 struct crypto_skcipher_spawn *spawn)
177 {
178 return crypto_spawn_tfm2(&spawn->base);
179 }
180
crypto_spawn_lskcipher(struct crypto_lskcipher_spawn * spawn)181 static inline struct crypto_lskcipher *crypto_spawn_lskcipher(
182 struct crypto_lskcipher_spawn *spawn)
183 {
184 return crypto_spawn_tfm2(&spawn->base);
185 }
186
crypto_skcipher_set_reqsize(struct crypto_skcipher * skcipher,unsigned int reqsize)187 static inline void crypto_skcipher_set_reqsize(
188 struct crypto_skcipher *skcipher, unsigned int reqsize)
189 {
190 skcipher->reqsize = reqsize;
191 }
192
crypto_skcipher_set_reqsize_dma(struct crypto_skcipher * skcipher,unsigned int reqsize)193 static inline void crypto_skcipher_set_reqsize_dma(
194 struct crypto_skcipher *skcipher, unsigned int reqsize)
195 {
196 reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
197 skcipher->reqsize = reqsize;
198 }
199
200 int crypto_register_skcipher(struct skcipher_alg *alg);
201 void crypto_unregister_skcipher(struct skcipher_alg *alg);
202 int crypto_register_skciphers(struct skcipher_alg *algs, int count);
203 void crypto_unregister_skciphers(struct skcipher_alg *algs, int count);
204 int skcipher_register_instance(struct crypto_template *tmpl,
205 struct skcipher_instance *inst);
206
207 int crypto_register_lskcipher(struct lskcipher_alg *alg);
208 void crypto_unregister_lskcipher(struct lskcipher_alg *alg);
209 int crypto_register_lskciphers(struct lskcipher_alg *algs, int count);
210 void crypto_unregister_lskciphers(struct lskcipher_alg *algs, int count);
211 int lskcipher_register_instance(struct crypto_template *tmpl,
212 struct lskcipher_instance *inst);
213
214 int skcipher_walk_done(struct skcipher_walk *walk, int res);
215 int skcipher_walk_virt(struct skcipher_walk *__restrict walk,
216 struct skcipher_request *__restrict req,
217 bool atomic);
218 int skcipher_walk_aead_encrypt(struct skcipher_walk *__restrict walk,
219 struct aead_request *__restrict req,
220 bool atomic);
221 int skcipher_walk_aead_decrypt(struct skcipher_walk *__restrict walk,
222 struct aead_request *__restrict req,
223 bool atomic);
224
skcipher_walk_abort(struct skcipher_walk * walk)225 static inline void skcipher_walk_abort(struct skcipher_walk *walk)
226 {
227 skcipher_walk_done(walk, -ECANCELED);
228 }
229
crypto_skcipher_ctx(struct crypto_skcipher * tfm)230 static inline void *crypto_skcipher_ctx(struct crypto_skcipher *tfm)
231 {
232 return crypto_tfm_ctx(&tfm->base);
233 }
234
crypto_lskcipher_ctx(struct crypto_lskcipher * tfm)235 static inline void *crypto_lskcipher_ctx(struct crypto_lskcipher *tfm)
236 {
237 return crypto_tfm_ctx(&tfm->base);
238 }
239
crypto_skcipher_ctx_dma(struct crypto_skcipher * tfm)240 static inline void *crypto_skcipher_ctx_dma(struct crypto_skcipher *tfm)
241 {
242 return crypto_tfm_ctx_dma(&tfm->base);
243 }
244
skcipher_request_ctx(struct skcipher_request * req)245 static inline void *skcipher_request_ctx(struct skcipher_request *req)
246 {
247 return req->__ctx;
248 }
249
skcipher_request_ctx_dma(struct skcipher_request * req)250 static inline void *skcipher_request_ctx_dma(struct skcipher_request *req)
251 {
252 unsigned int align = crypto_dma_align();
253
254 if (align <= crypto_tfm_ctx_alignment())
255 align = 1;
256
257 return PTR_ALIGN(skcipher_request_ctx(req), align);
258 }
259
skcipher_request_flags(struct skcipher_request * req)260 static inline u32 skcipher_request_flags(struct skcipher_request *req)
261 {
262 return req->base.flags;
263 }
264
265 /* Helpers for simple block cipher modes of operation */
266 struct skcipher_ctx_simple {
267 struct crypto_cipher *cipher; /* underlying block cipher */
268 };
269 static inline struct crypto_cipher *
skcipher_cipher_simple(struct crypto_skcipher * tfm)270 skcipher_cipher_simple(struct crypto_skcipher *tfm)
271 {
272 struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
273
274 return ctx->cipher;
275 }
276
277 struct skcipher_instance *skcipher_alloc_instance_simple(
278 struct crypto_template *tmpl, struct rtattr **tb);
279
skcipher_ialg_simple(struct skcipher_instance * inst)280 static inline struct crypto_alg *skcipher_ialg_simple(
281 struct skcipher_instance *inst)
282 {
283 struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst);
284
285 return crypto_spawn_cipher_alg(spawn);
286 }
287
lskcipher_cipher_simple(struct crypto_lskcipher * tfm)288 static inline struct crypto_lskcipher *lskcipher_cipher_simple(
289 struct crypto_lskcipher *tfm)
290 {
291 struct crypto_lskcipher **ctx = crypto_lskcipher_ctx(tfm);
292
293 return *ctx;
294 }
295
296 struct lskcipher_instance *lskcipher_alloc_instance_simple(
297 struct crypto_template *tmpl, struct rtattr **tb);
298
lskcipher_ialg_simple(struct lskcipher_instance * inst)299 static inline struct lskcipher_alg *lskcipher_ialg_simple(
300 struct lskcipher_instance *inst)
301 {
302 struct crypto_lskcipher_spawn *spawn = lskcipher_instance_ctx(inst);
303
304 return crypto_lskcipher_spawn_alg(spawn);
305 }
306
307 #endif /* _CRYPTO_INTERNAL_SKCIPHER_H */
308
309