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