1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Symmetric key ciphers.
4 *
5 * Copyright (c) 2007 Herbert Xu <[email protected]>
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 struct {
60 void *addr;
61 } virt;
62 } src, dst;
63
64 struct scatter_walk in;
65 unsigned int nbytes;
66
67 struct scatter_walk out;
68 unsigned int total;
69
70 u8 *page;
71 u8 *buffer;
72 u8 *oiv;
73 void *iv;
74
75 unsigned int ivsize;
76
77 int flags;
78 unsigned int blocksize;
79 unsigned int stride;
80 unsigned int alignmask;
81 };
82
skcipher_crypto_instance(struct skcipher_instance * inst)83 static inline struct crypto_instance *skcipher_crypto_instance(
84 struct skcipher_instance *inst)
85 {
86 return &inst->s.base;
87 }
88
lskcipher_crypto_instance(struct lskcipher_instance * inst)89 static inline struct crypto_instance *lskcipher_crypto_instance(
90 struct lskcipher_instance *inst)
91 {
92 return &inst->s.base;
93 }
94
skcipher_alg_instance(struct crypto_skcipher * skcipher)95 static inline struct skcipher_instance *skcipher_alg_instance(
96 struct crypto_skcipher *skcipher)
97 {
98 return container_of(crypto_skcipher_alg(skcipher),
99 struct skcipher_instance, alg);
100 }
101
lskcipher_alg_instance(struct crypto_lskcipher * lskcipher)102 static inline struct lskcipher_instance *lskcipher_alg_instance(
103 struct crypto_lskcipher *lskcipher)
104 {
105 return container_of(crypto_lskcipher_alg(lskcipher),
106 struct lskcipher_instance, alg);
107 }
108
skcipher_instance_ctx(struct skcipher_instance * inst)109 static inline void *skcipher_instance_ctx(struct skcipher_instance *inst)
110 {
111 return crypto_instance_ctx(skcipher_crypto_instance(inst));
112 }
113
lskcipher_instance_ctx(struct lskcipher_instance * inst)114 static inline void *lskcipher_instance_ctx(struct lskcipher_instance *inst)
115 {
116 return crypto_instance_ctx(lskcipher_crypto_instance(inst));
117 }
118
skcipher_request_complete(struct skcipher_request * req,int err)119 static inline void skcipher_request_complete(struct skcipher_request *req, int err)
120 {
121 crypto_request_complete(&req->base, err);
122 }
123
124 int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn,
125 struct crypto_instance *inst,
126 const char *name, u32 type, u32 mask);
127
128 int crypto_grab_lskcipher(struct crypto_lskcipher_spawn *spawn,
129 struct crypto_instance *inst,
130 const char *name, u32 type, u32 mask);
131
crypto_drop_skcipher(struct crypto_skcipher_spawn * spawn)132 static inline void crypto_drop_skcipher(struct crypto_skcipher_spawn *spawn)
133 {
134 crypto_drop_spawn(&spawn->base);
135 }
136
crypto_drop_lskcipher(struct crypto_lskcipher_spawn * spawn)137 static inline void crypto_drop_lskcipher(struct crypto_lskcipher_spawn *spawn)
138 {
139 crypto_drop_spawn(&spawn->base);
140 }
141
crypto_lskcipher_spawn_alg(struct crypto_lskcipher_spawn * spawn)142 static inline struct lskcipher_alg *crypto_lskcipher_spawn_alg(
143 struct crypto_lskcipher_spawn *spawn)
144 {
145 return container_of(spawn->base.alg, struct lskcipher_alg, co.base);
146 }
147
crypto_spawn_skcipher_alg_common(struct crypto_skcipher_spawn * spawn)148 static inline struct skcipher_alg_common *crypto_spawn_skcipher_alg_common(
149 struct crypto_skcipher_spawn *spawn)
150 {
151 return container_of(spawn->base.alg, struct skcipher_alg_common, base);
152 }
153
crypto_spawn_lskcipher_alg(struct crypto_lskcipher_spawn * spawn)154 static inline struct lskcipher_alg *crypto_spawn_lskcipher_alg(
155 struct crypto_lskcipher_spawn *spawn)
156 {
157 return crypto_lskcipher_spawn_alg(spawn);
158 }
159
crypto_spawn_skcipher(struct crypto_skcipher_spawn * spawn)160 static inline struct crypto_skcipher *crypto_spawn_skcipher(
161 struct crypto_skcipher_spawn *spawn)
162 {
163 return crypto_spawn_tfm2(&spawn->base);
164 }
165
crypto_spawn_lskcipher(struct crypto_lskcipher_spawn * spawn)166 static inline struct crypto_lskcipher *crypto_spawn_lskcipher(
167 struct crypto_lskcipher_spawn *spawn)
168 {
169 return crypto_spawn_tfm2(&spawn->base);
170 }
171
crypto_skcipher_set_reqsize(struct crypto_skcipher * skcipher,unsigned int reqsize)172 static inline void crypto_skcipher_set_reqsize(
173 struct crypto_skcipher *skcipher, unsigned int reqsize)
174 {
175 skcipher->reqsize = reqsize;
176 }
177
crypto_skcipher_set_reqsize_dma(struct crypto_skcipher * skcipher,unsigned int reqsize)178 static inline void crypto_skcipher_set_reqsize_dma(
179 struct crypto_skcipher *skcipher, unsigned int reqsize)
180 {
181 reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
182 skcipher->reqsize = reqsize;
183 }
184
185 int crypto_register_skcipher(struct skcipher_alg *alg);
186 void crypto_unregister_skcipher(struct skcipher_alg *alg);
187 int crypto_register_skciphers(struct skcipher_alg *algs, int count);
188 void crypto_unregister_skciphers(struct skcipher_alg *algs, int count);
189 int skcipher_register_instance(struct crypto_template *tmpl,
190 struct skcipher_instance *inst);
191
192 int crypto_register_lskcipher(struct lskcipher_alg *alg);
193 void crypto_unregister_lskcipher(struct lskcipher_alg *alg);
194 int crypto_register_lskciphers(struct lskcipher_alg *algs, int count);
195 void crypto_unregister_lskciphers(struct lskcipher_alg *algs, int count);
196 int lskcipher_register_instance(struct crypto_template *tmpl,
197 struct lskcipher_instance *inst);
198
199 int skcipher_walk_done(struct skcipher_walk *walk, int res);
200 int skcipher_walk_virt(struct skcipher_walk *walk,
201 struct skcipher_request *req,
202 bool atomic);
203 int skcipher_walk_aead_encrypt(struct skcipher_walk *walk,
204 struct aead_request *req, bool atomic);
205 int skcipher_walk_aead_decrypt(struct skcipher_walk *walk,
206 struct aead_request *req, bool atomic);
207
skcipher_walk_abort(struct skcipher_walk * walk)208 static inline void skcipher_walk_abort(struct skcipher_walk *walk)
209 {
210 skcipher_walk_done(walk, -ECANCELED);
211 }
212
crypto_skcipher_ctx(struct crypto_skcipher * tfm)213 static inline void *crypto_skcipher_ctx(struct crypto_skcipher *tfm)
214 {
215 return crypto_tfm_ctx(&tfm->base);
216 }
217
crypto_lskcipher_ctx(struct crypto_lskcipher * tfm)218 static inline void *crypto_lskcipher_ctx(struct crypto_lskcipher *tfm)
219 {
220 return crypto_tfm_ctx(&tfm->base);
221 }
222
crypto_skcipher_ctx_dma(struct crypto_skcipher * tfm)223 static inline void *crypto_skcipher_ctx_dma(struct crypto_skcipher *tfm)
224 {
225 return crypto_tfm_ctx_dma(&tfm->base);
226 }
227
skcipher_request_ctx(struct skcipher_request * req)228 static inline void *skcipher_request_ctx(struct skcipher_request *req)
229 {
230 return req->__ctx;
231 }
232
skcipher_request_ctx_dma(struct skcipher_request * req)233 static inline void *skcipher_request_ctx_dma(struct skcipher_request *req)
234 {
235 unsigned int align = crypto_dma_align();
236
237 if (align <= crypto_tfm_ctx_alignment())
238 align = 1;
239
240 return PTR_ALIGN(skcipher_request_ctx(req), align);
241 }
242
skcipher_request_flags(struct skcipher_request * req)243 static inline u32 skcipher_request_flags(struct skcipher_request *req)
244 {
245 return req->base.flags;
246 }
247
248 /* Helpers for simple block cipher modes of operation */
249 struct skcipher_ctx_simple {
250 struct crypto_cipher *cipher; /* underlying block cipher */
251 };
252 static inline struct crypto_cipher *
skcipher_cipher_simple(struct crypto_skcipher * tfm)253 skcipher_cipher_simple(struct crypto_skcipher *tfm)
254 {
255 struct skcipher_ctx_simple *ctx = crypto_skcipher_ctx(tfm);
256
257 return ctx->cipher;
258 }
259
260 struct skcipher_instance *skcipher_alloc_instance_simple(
261 struct crypto_template *tmpl, struct rtattr **tb);
262
skcipher_ialg_simple(struct skcipher_instance * inst)263 static inline struct crypto_alg *skcipher_ialg_simple(
264 struct skcipher_instance *inst)
265 {
266 struct crypto_cipher_spawn *spawn = skcipher_instance_ctx(inst);
267
268 return crypto_spawn_cipher_alg(spawn);
269 }
270
lskcipher_cipher_simple(struct crypto_lskcipher * tfm)271 static inline struct crypto_lskcipher *lskcipher_cipher_simple(
272 struct crypto_lskcipher *tfm)
273 {
274 struct crypto_lskcipher **ctx = crypto_lskcipher_ctx(tfm);
275
276 return *ctx;
277 }
278
279 struct lskcipher_instance *lskcipher_alloc_instance_simple(
280 struct crypto_template *tmpl, struct rtattr **tb);
281
lskcipher_ialg_simple(struct lskcipher_instance * inst)282 static inline struct lskcipher_alg *lskcipher_ialg_simple(
283 struct lskcipher_instance *inst)
284 {
285 struct crypto_lskcipher_spawn *spawn = lskcipher_instance_ctx(inst);
286
287 return crypto_lskcipher_spawn_alg(spawn);
288 }
289
290 #endif /* _CRYPTO_INTERNAL_SKCIPHER_H */
291
292