1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Symmetric key ciphers.
5 * Copyright (c) 2007 Herbert Xu <herbert@gondor.apana.org.au>
8 #ifndef _CRYPTO_INTERNAL_SKCIPHER_H
9 #define _CRYPTO_INTERNAL_SKCIPHER_H
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>
18 * Set this if your algorithm is sync but needs a reqsize larger
19 * than MAX_SYNC_SKCIPHER_REQSIZE.
21 * Reuse bit that is specific to hash algorithms.
23 #define CRYPTO_ALG_SKCIPHER_REQSIZE_LARGE CRYPTO_ALG_OPTIONAL_KEY
28 struct skcipher_instance
{
29 void (*free
)(struct skcipher_instance
*inst
);
32 char head
[offsetof(struct skcipher_alg
, base
)];
33 struct crypto_instance base
;
35 struct skcipher_alg alg
;
39 struct lskcipher_instance
{
40 void (*free
)(struct lskcipher_instance
*inst
);
43 char head
[offsetof(struct lskcipher_alg
, co
.base
)];
44 struct crypto_instance base
;
46 struct lskcipher_alg alg
;
50 struct crypto_skcipher_spawn
{
51 struct crypto_spawn base
;
54 struct crypto_lskcipher_spawn
{
55 struct crypto_spawn base
;
58 struct skcipher_walk
{
71 struct scatter_walk in
;
74 struct scatter_walk out
;
77 struct list_head buffers
;
87 unsigned int blocksize
;
89 unsigned int alignmask
;
92 static inline struct crypto_instance
*skcipher_crypto_instance(
93 struct skcipher_instance
*inst
)
98 static inline struct crypto_instance
*lskcipher_crypto_instance(
99 struct lskcipher_instance
*inst
)
101 return &inst
->s
.base
;
104 static inline struct skcipher_instance
*skcipher_alg_instance(
105 struct crypto_skcipher
*skcipher
)
107 return container_of(crypto_skcipher_alg(skcipher
),
108 struct skcipher_instance
, alg
);
111 static inline struct lskcipher_instance
*lskcipher_alg_instance(
112 struct crypto_lskcipher
*lskcipher
)
114 return container_of(crypto_lskcipher_alg(lskcipher
),
115 struct lskcipher_instance
, alg
);
118 static inline void *skcipher_instance_ctx(struct skcipher_instance
*inst
)
120 return crypto_instance_ctx(skcipher_crypto_instance(inst
));
123 static inline void *lskcipher_instance_ctx(struct lskcipher_instance
*inst
)
125 return crypto_instance_ctx(lskcipher_crypto_instance(inst
));
128 static inline void skcipher_request_complete(struct skcipher_request
*req
, int err
)
130 crypto_request_complete(&req
->base
, err
);
133 int crypto_grab_skcipher(struct crypto_skcipher_spawn
*spawn
,
134 struct crypto_instance
*inst
,
135 const char *name
, u32 type
, u32 mask
);
137 int crypto_grab_lskcipher(struct crypto_lskcipher_spawn
*spawn
,
138 struct crypto_instance
*inst
,
139 const char *name
, u32 type
, u32 mask
);
141 static inline void crypto_drop_skcipher(struct crypto_skcipher_spawn
*spawn
)
143 crypto_drop_spawn(&spawn
->base
);
146 static inline void crypto_drop_lskcipher(struct crypto_lskcipher_spawn
*spawn
)
148 crypto_drop_spawn(&spawn
->base
);
151 static inline struct lskcipher_alg
*crypto_lskcipher_spawn_alg(
152 struct crypto_lskcipher_spawn
*spawn
)
154 return container_of(spawn
->base
.alg
, struct lskcipher_alg
, co
.base
);
157 static inline struct skcipher_alg_common
*crypto_spawn_skcipher_alg_common(
158 struct crypto_skcipher_spawn
*spawn
)
160 return container_of(spawn
->base
.alg
, struct skcipher_alg_common
, base
);
163 static inline struct lskcipher_alg
*crypto_spawn_lskcipher_alg(
164 struct crypto_lskcipher_spawn
*spawn
)
166 return crypto_lskcipher_spawn_alg(spawn
);
169 static inline struct crypto_skcipher
*crypto_spawn_skcipher(
170 struct crypto_skcipher_spawn
*spawn
)
172 return crypto_spawn_tfm2(&spawn
->base
);
175 static inline struct crypto_lskcipher
*crypto_spawn_lskcipher(
176 struct crypto_lskcipher_spawn
*spawn
)
178 return crypto_spawn_tfm2(&spawn
->base
);
181 static inline void crypto_skcipher_set_reqsize(
182 struct crypto_skcipher
*skcipher
, unsigned int reqsize
)
184 skcipher
->reqsize
= reqsize
;
187 static inline void crypto_skcipher_set_reqsize_dma(
188 struct crypto_skcipher
*skcipher
, unsigned int reqsize
)
190 reqsize
+= crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
191 skcipher
->reqsize
= reqsize
;
194 int crypto_register_skcipher(struct skcipher_alg
*alg
);
195 void crypto_unregister_skcipher(struct skcipher_alg
*alg
);
196 int crypto_register_skciphers(struct skcipher_alg
*algs
, int count
);
197 void crypto_unregister_skciphers(struct skcipher_alg
*algs
, int count
);
198 int skcipher_register_instance(struct crypto_template
*tmpl
,
199 struct skcipher_instance
*inst
);
201 int crypto_register_lskcipher(struct lskcipher_alg
*alg
);
202 void crypto_unregister_lskcipher(struct lskcipher_alg
*alg
);
203 int crypto_register_lskciphers(struct lskcipher_alg
*algs
, int count
);
204 void crypto_unregister_lskciphers(struct lskcipher_alg
*algs
, int count
);
205 int lskcipher_register_instance(struct crypto_template
*tmpl
,
206 struct lskcipher_instance
*inst
);
208 int skcipher_walk_done(struct skcipher_walk
*walk
, int err
);
209 int skcipher_walk_virt(struct skcipher_walk
*walk
,
210 struct skcipher_request
*req
,
212 int skcipher_walk_async(struct skcipher_walk
*walk
,
213 struct skcipher_request
*req
);
214 int skcipher_walk_aead_encrypt(struct skcipher_walk
*walk
,
215 struct aead_request
*req
, bool atomic
);
216 int skcipher_walk_aead_decrypt(struct skcipher_walk
*walk
,
217 struct aead_request
*req
, bool atomic
);
218 void skcipher_walk_complete(struct skcipher_walk
*walk
, int err
);
220 static inline void skcipher_walk_abort(struct skcipher_walk
*walk
)
222 skcipher_walk_done(walk
, -ECANCELED
);
225 static inline void *crypto_skcipher_ctx(struct crypto_skcipher
*tfm
)
227 return crypto_tfm_ctx(&tfm
->base
);
230 static inline void *crypto_lskcipher_ctx(struct crypto_lskcipher
*tfm
)
232 return crypto_tfm_ctx(&tfm
->base
);
235 static inline void *crypto_skcipher_ctx_dma(struct crypto_skcipher
*tfm
)
237 return crypto_tfm_ctx_dma(&tfm
->base
);
240 static inline void *skcipher_request_ctx(struct skcipher_request
*req
)
245 static inline void *skcipher_request_ctx_dma(struct skcipher_request
*req
)
247 unsigned int align
= crypto_dma_align();
249 if (align
<= crypto_tfm_ctx_alignment())
252 return PTR_ALIGN(skcipher_request_ctx(req
), align
);
255 static inline u32
skcipher_request_flags(struct skcipher_request
*req
)
257 return req
->base
.flags
;
260 /* Helpers for simple block cipher modes of operation */
261 struct skcipher_ctx_simple
{
262 struct crypto_cipher
*cipher
; /* underlying block cipher */
264 static inline struct crypto_cipher
*
265 skcipher_cipher_simple(struct crypto_skcipher
*tfm
)
267 struct skcipher_ctx_simple
*ctx
= crypto_skcipher_ctx(tfm
);
272 struct skcipher_instance
*skcipher_alloc_instance_simple(
273 struct crypto_template
*tmpl
, struct rtattr
**tb
);
275 static inline struct crypto_alg
*skcipher_ialg_simple(
276 struct skcipher_instance
*inst
)
278 struct crypto_cipher_spawn
*spawn
= skcipher_instance_ctx(inst
);
280 return crypto_spawn_cipher_alg(spawn
);
283 static inline struct crypto_lskcipher
*lskcipher_cipher_simple(
284 struct crypto_lskcipher
*tfm
)
286 struct crypto_lskcipher
**ctx
= crypto_lskcipher_ctx(tfm
);
291 struct lskcipher_instance
*lskcipher_alloc_instance_simple(
292 struct crypto_template
*tmpl
, struct rtattr
**tb
);
294 static inline struct lskcipher_alg
*lskcipher_ialg_simple(
295 struct lskcipher_instance
*inst
)
297 struct crypto_lskcipher_spawn
*spawn
= lskcipher_instance_ctx(inst
);
299 return crypto_lskcipher_spawn_alg(spawn
);
302 #endif /* _CRYPTO_INTERNAL_SKCIPHER_H */