2 * Scatterlist Cryptographic API.
4 * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5 * Copyright (c) 2002 David S. Miller (davem@redhat.com)
6 * Copyright (c) 2005 Herbert Xu <herbert@gondor.apana.org.au>
8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <astor@fast.no>
9 * and Nettle, by Niels Möller.
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
17 #ifndef _LINUX_CRYPTO_H
18 #define _LINUX_CRYPTO_H
20 #include <linux/atomic.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/bug.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/uaccess.h>
29 * Autoloaded crypto modules should only use a prefixed name to avoid allowing
30 * arbitrary modules to be loaded. Loading from userspace may still need the
31 * unprefixed names, so retains those aliases as well.
32 * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3
33 * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro
34 * expands twice on the same line. Instead, use a separate base name for the
37 #define MODULE_ALIAS_CRYPTO(name) \
38 __MODULE_INFO(alias, alias_userspace, name); \
39 __MODULE_INFO(alias, alias_crypto, "crypto-" name)
42 * Algorithm masks and types.
44 #define CRYPTO_ALG_TYPE_MASK 0x0000000f
45 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
46 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
47 #define CRYPTO_ALG_TYPE_AEAD 0x00000003
48 #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
49 #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
50 #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
51 #define CRYPTO_ALG_TYPE_DIGEST 0x00000008
52 #define CRYPTO_ALG_TYPE_HASH 0x00000008
53 #define CRYPTO_ALG_TYPE_SHASH 0x00000009
54 #define CRYPTO_ALG_TYPE_AHASH 0x0000000a
55 #define CRYPTO_ALG_TYPE_RNG 0x0000000c
56 #define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f
58 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
59 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
60 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
62 #define CRYPTO_ALG_LARVAL 0x00000010
63 #define CRYPTO_ALG_DEAD 0x00000020
64 #define CRYPTO_ALG_DYING 0x00000040
65 #define CRYPTO_ALG_ASYNC 0x00000080
68 * Set this bit if and only if the algorithm requires another algorithm of
69 * the same type to handle corner cases.
71 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
74 * This bit is set for symmetric key ciphers that have already been wrapped
75 * with a generic IV generator to prevent them from being wrapped again.
77 #define CRYPTO_ALG_GENIV 0x00000200
80 * Set if the algorithm has passed automated run-time testing. Note that
81 * if there is no run-time testing for a given algorithm it is considered
85 #define CRYPTO_ALG_TESTED 0x00000400
88 * Set if the algorithm is an instance that is build from templates.
90 #define CRYPTO_ALG_INSTANCE 0x00000800
92 /* Set this bit if the algorithm provided is hardware accelerated but
93 * not available to userspace via instruction set or so.
95 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
98 * Transform masks and values (for crt_flags).
100 #define CRYPTO_TFM_REQ_MASK 0x000fff00
101 #define CRYPTO_TFM_RES_MASK 0xfff00000
103 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
104 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
105 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
106 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
107 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
108 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
109 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
110 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
113 * Miscellaneous stuff.
115 #define CRYPTO_MAX_ALG_NAME 64
118 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
119 * declaration) is used to ensure that the crypto_tfm context structure is
120 * aligned correctly for the given architecture so that there are no alignment
121 * faults for C data types. In particular, this is required on platforms such
122 * as arm where pointers are 32-bit aligned but there are data types such as
123 * u64 which require 64-bit alignment.
125 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
127 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
130 struct crypto_ablkcipher
;
131 struct crypto_async_request
;
133 struct crypto_blkcipher
;
138 struct aead_givcrypt_request
;
139 struct skcipher_givcrypt_request
;
141 typedef void (*crypto_completion_t
)(struct crypto_async_request
*req
, int err
);
143 struct crypto_async_request
{
144 struct list_head list
;
145 crypto_completion_t complete
;
147 struct crypto_tfm
*tfm
;
152 struct ablkcipher_request
{
153 struct crypto_async_request base
;
159 struct scatterlist
*src
;
160 struct scatterlist
*dst
;
162 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
166 * struct aead_request - AEAD request
167 * @base: Common attributes for async crypto requests
168 * @assoclen: Length in bytes of associated data for authentication
169 * @cryptlen: Length of data to be encrypted or decrypted
170 * @iv: Initialisation vector
171 * @assoc: Associated data
173 * @dst: Destination data
174 * @__ctx: Start of private context data
176 struct aead_request
{
177 struct crypto_async_request base
;
179 unsigned int assoclen
;
180 unsigned int cryptlen
;
184 struct scatterlist
*assoc
;
185 struct scatterlist
*src
;
186 struct scatterlist
*dst
;
188 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
191 struct blkcipher_desc
{
192 struct crypto_blkcipher
*tfm
;
198 struct crypto_tfm
*tfm
;
199 void (*crfn
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
200 unsigned int (*prfn
)(const struct cipher_desc
*desc
, u8
*dst
,
201 const u8
*src
, unsigned int nbytes
);
206 struct crypto_hash
*tfm
;
211 * Algorithms: modular crypto algorithm implementations, managed
212 * via crypto_register_alg() and crypto_unregister_alg().
214 struct ablkcipher_alg
{
215 int (*setkey
)(struct crypto_ablkcipher
*tfm
, const u8
*key
,
216 unsigned int keylen
);
217 int (*encrypt
)(struct ablkcipher_request
*req
);
218 int (*decrypt
)(struct ablkcipher_request
*req
);
219 int (*givencrypt
)(struct skcipher_givcrypt_request
*req
);
220 int (*givdecrypt
)(struct skcipher_givcrypt_request
*req
);
224 unsigned int min_keysize
;
225 unsigned int max_keysize
;
230 int (*setkey
)(struct crypto_aead
*tfm
, const u8
*key
,
231 unsigned int keylen
);
232 int (*setauthsize
)(struct crypto_aead
*tfm
, unsigned int authsize
);
233 int (*encrypt
)(struct aead_request
*req
);
234 int (*decrypt
)(struct aead_request
*req
);
235 int (*givencrypt
)(struct aead_givcrypt_request
*req
);
236 int (*givdecrypt
)(struct aead_givcrypt_request
*req
);
241 unsigned int maxauthsize
;
244 struct blkcipher_alg
{
245 int (*setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
246 unsigned int keylen
);
247 int (*encrypt
)(struct blkcipher_desc
*desc
,
248 struct scatterlist
*dst
, struct scatterlist
*src
,
249 unsigned int nbytes
);
250 int (*decrypt
)(struct blkcipher_desc
*desc
,
251 struct scatterlist
*dst
, struct scatterlist
*src
,
252 unsigned int nbytes
);
256 unsigned int min_keysize
;
257 unsigned int max_keysize
;
262 unsigned int cia_min_keysize
;
263 unsigned int cia_max_keysize
;
264 int (*cia_setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
265 unsigned int keylen
);
266 void (*cia_encrypt
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
267 void (*cia_decrypt
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
270 struct compress_alg
{
271 int (*coa_compress
)(struct crypto_tfm
*tfm
, const u8
*src
,
272 unsigned int slen
, u8
*dst
, unsigned int *dlen
);
273 int (*coa_decompress
)(struct crypto_tfm
*tfm
, const u8
*src
,
274 unsigned int slen
, u8
*dst
, unsigned int *dlen
);
278 int (*rng_make_random
)(struct crypto_rng
*tfm
, u8
*rdata
,
280 int (*rng_reset
)(struct crypto_rng
*tfm
, u8
*seed
, unsigned int slen
);
282 unsigned int seedsize
;
286 #define cra_ablkcipher cra_u.ablkcipher
287 #define cra_aead cra_u.aead
288 #define cra_blkcipher cra_u.blkcipher
289 #define cra_cipher cra_u.cipher
290 #define cra_compress cra_u.compress
291 #define cra_rng cra_u.rng
294 struct list_head cra_list
;
295 struct list_head cra_users
;
298 unsigned int cra_blocksize
;
299 unsigned int cra_ctxsize
;
300 unsigned int cra_alignmask
;
305 char cra_name
[CRYPTO_MAX_ALG_NAME
];
306 char cra_driver_name
[CRYPTO_MAX_ALG_NAME
];
308 const struct crypto_type
*cra_type
;
311 struct ablkcipher_alg ablkcipher
;
312 struct aead_alg aead
;
313 struct blkcipher_alg blkcipher
;
314 struct cipher_alg cipher
;
315 struct compress_alg compress
;
319 int (*cra_init
)(struct crypto_tfm
*tfm
);
320 void (*cra_exit
)(struct crypto_tfm
*tfm
);
321 void (*cra_destroy
)(struct crypto_alg
*alg
);
323 struct module
*cra_module
;
327 * Algorithm registration interface.
329 int crypto_register_alg(struct crypto_alg
*alg
);
330 int crypto_unregister_alg(struct crypto_alg
*alg
);
331 int crypto_register_algs(struct crypto_alg
*algs
, int count
);
332 int crypto_unregister_algs(struct crypto_alg
*algs
, int count
);
335 * Algorithm query interface.
337 int crypto_has_alg(const char *name
, u32 type
, u32 mask
);
340 * Transforms: user-instantiated objects which encapsulate algorithms
341 * and core processing logic. Managed via crypto_alloc_*() and
342 * crypto_free_*(), as well as the various helpers below.
345 struct ablkcipher_tfm
{
346 int (*setkey
)(struct crypto_ablkcipher
*tfm
, const u8
*key
,
347 unsigned int keylen
);
348 int (*encrypt
)(struct ablkcipher_request
*req
);
349 int (*decrypt
)(struct ablkcipher_request
*req
);
350 int (*givencrypt
)(struct skcipher_givcrypt_request
*req
);
351 int (*givdecrypt
)(struct skcipher_givcrypt_request
*req
);
353 struct crypto_ablkcipher
*base
;
356 unsigned int reqsize
;
360 int (*setkey
)(struct crypto_aead
*tfm
, const u8
*key
,
361 unsigned int keylen
);
362 int (*encrypt
)(struct aead_request
*req
);
363 int (*decrypt
)(struct aead_request
*req
);
364 int (*givencrypt
)(struct aead_givcrypt_request
*req
);
365 int (*givdecrypt
)(struct aead_givcrypt_request
*req
);
367 struct crypto_aead
*base
;
370 unsigned int authsize
;
371 unsigned int reqsize
;
374 struct blkcipher_tfm
{
376 int (*setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
377 unsigned int keylen
);
378 int (*encrypt
)(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
379 struct scatterlist
*src
, unsigned int nbytes
);
380 int (*decrypt
)(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
381 struct scatterlist
*src
, unsigned int nbytes
);
385 int (*cit_setkey
)(struct crypto_tfm
*tfm
,
386 const u8
*key
, unsigned int keylen
);
387 void (*cit_encrypt_one
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
388 void (*cit_decrypt_one
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
392 int (*init
)(struct hash_desc
*desc
);
393 int (*update
)(struct hash_desc
*desc
,
394 struct scatterlist
*sg
, unsigned int nsg
);
395 int (*final
)(struct hash_desc
*desc
, u8
*out
);
396 int (*digest
)(struct hash_desc
*desc
, struct scatterlist
*sg
,
397 unsigned int nsg
, u8
*out
);
398 int (*setkey
)(struct crypto_hash
*tfm
, const u8
*key
,
399 unsigned int keylen
);
400 unsigned int digestsize
;
403 struct compress_tfm
{
404 int (*cot_compress
)(struct crypto_tfm
*tfm
,
405 const u8
*src
, unsigned int slen
,
406 u8
*dst
, unsigned int *dlen
);
407 int (*cot_decompress
)(struct crypto_tfm
*tfm
,
408 const u8
*src
, unsigned int slen
,
409 u8
*dst
, unsigned int *dlen
);
413 int (*rng_gen_random
)(struct crypto_rng
*tfm
, u8
*rdata
,
415 int (*rng_reset
)(struct crypto_rng
*tfm
, u8
*seed
, unsigned int slen
);
418 #define crt_ablkcipher crt_u.ablkcipher
419 #define crt_aead crt_u.aead
420 #define crt_blkcipher crt_u.blkcipher
421 #define crt_cipher crt_u.cipher
422 #define crt_hash crt_u.hash
423 #define crt_compress crt_u.compress
424 #define crt_rng crt_u.rng
431 struct ablkcipher_tfm ablkcipher
;
432 struct aead_tfm aead
;
433 struct blkcipher_tfm blkcipher
;
434 struct cipher_tfm cipher
;
435 struct hash_tfm hash
;
436 struct compress_tfm compress
;
440 void (*exit
)(struct crypto_tfm
*tfm
);
442 struct crypto_alg
*__crt_alg
;
444 void *__crt_ctx
[] CRYPTO_MINALIGN_ATTR
;
447 struct crypto_ablkcipher
{
448 struct crypto_tfm base
;
452 struct crypto_tfm base
;
455 struct crypto_blkcipher
{
456 struct crypto_tfm base
;
459 struct crypto_cipher
{
460 struct crypto_tfm base
;
464 struct crypto_tfm base
;
468 struct crypto_tfm base
;
472 struct crypto_tfm base
;
483 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
485 /* Maximum number of (rtattr) parameters for each template. */
486 #define CRYPTO_MAX_ATTRS 32
488 struct crypto_attr_alg
{
489 char name
[CRYPTO_MAX_ALG_NAME
];
492 struct crypto_attr_type
{
497 struct crypto_attr_u32
{
502 * Transform user interface.
505 struct crypto_tfm
*crypto_alloc_base(const char *alg_name
, u32 type
, u32 mask
);
506 void crypto_destroy_tfm(void *mem
, struct crypto_tfm
*tfm
);
508 static inline void crypto_free_tfm(struct crypto_tfm
*tfm
)
510 return crypto_destroy_tfm(tfm
, tfm
);
513 int alg_test(const char *driver
, const char *alg
, u32 type
, u32 mask
);
516 * Transform helpers which query the underlying algorithm.
518 static inline const char *crypto_tfm_alg_name(struct crypto_tfm
*tfm
)
520 return tfm
->__crt_alg
->cra_name
;
523 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm
*tfm
)
525 return tfm
->__crt_alg
->cra_driver_name
;
528 static inline int crypto_tfm_alg_priority(struct crypto_tfm
*tfm
)
530 return tfm
->__crt_alg
->cra_priority
;
533 static inline u32
crypto_tfm_alg_type(struct crypto_tfm
*tfm
)
535 return tfm
->__crt_alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
;
538 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm
*tfm
)
540 return tfm
->__crt_alg
->cra_blocksize
;
543 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm
*tfm
)
545 return tfm
->__crt_alg
->cra_alignmask
;
548 static inline u32
crypto_tfm_get_flags(struct crypto_tfm
*tfm
)
550 return tfm
->crt_flags
;
553 static inline void crypto_tfm_set_flags(struct crypto_tfm
*tfm
, u32 flags
)
555 tfm
->crt_flags
|= flags
;
558 static inline void crypto_tfm_clear_flags(struct crypto_tfm
*tfm
, u32 flags
)
560 tfm
->crt_flags
&= ~flags
;
563 static inline void *crypto_tfm_ctx(struct crypto_tfm
*tfm
)
565 return tfm
->__crt_ctx
;
568 static inline unsigned int crypto_tfm_ctx_alignment(void)
570 struct crypto_tfm
*tfm
;
571 return __alignof__(tfm
->__crt_ctx
);
577 static inline struct crypto_ablkcipher
*__crypto_ablkcipher_cast(
578 struct crypto_tfm
*tfm
)
580 return (struct crypto_ablkcipher
*)tfm
;
583 static inline u32
crypto_skcipher_type(u32 type
)
585 type
&= ~(CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_GENIV
);
586 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
590 static inline u32
crypto_skcipher_mask(u32 mask
)
592 mask
&= ~(CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_GENIV
);
593 mask
|= CRYPTO_ALG_TYPE_BLKCIPHER_MASK
;
597 struct crypto_ablkcipher
*crypto_alloc_ablkcipher(const char *alg_name
,
600 static inline struct crypto_tfm
*crypto_ablkcipher_tfm(
601 struct crypto_ablkcipher
*tfm
)
606 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher
*tfm
)
608 crypto_free_tfm(crypto_ablkcipher_tfm(tfm
));
611 static inline int crypto_has_ablkcipher(const char *alg_name
, u32 type
,
614 return crypto_has_alg(alg_name
, crypto_skcipher_type(type
),
615 crypto_skcipher_mask(mask
));
618 static inline struct ablkcipher_tfm
*crypto_ablkcipher_crt(
619 struct crypto_ablkcipher
*tfm
)
621 return &crypto_ablkcipher_tfm(tfm
)->crt_ablkcipher
;
624 static inline unsigned int crypto_ablkcipher_ivsize(
625 struct crypto_ablkcipher
*tfm
)
627 return crypto_ablkcipher_crt(tfm
)->ivsize
;
630 static inline unsigned int crypto_ablkcipher_blocksize(
631 struct crypto_ablkcipher
*tfm
)
633 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm
));
636 static inline unsigned int crypto_ablkcipher_alignmask(
637 struct crypto_ablkcipher
*tfm
)
639 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm
));
642 static inline u32
crypto_ablkcipher_get_flags(struct crypto_ablkcipher
*tfm
)
644 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm
));
647 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher
*tfm
,
650 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm
), flags
);
653 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher
*tfm
,
656 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm
), flags
);
659 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher
*tfm
,
660 const u8
*key
, unsigned int keylen
)
662 struct ablkcipher_tfm
*crt
= crypto_ablkcipher_crt(tfm
);
664 return crt
->setkey(crt
->base
, key
, keylen
);
667 static inline struct crypto_ablkcipher
*crypto_ablkcipher_reqtfm(
668 struct ablkcipher_request
*req
)
670 return __crypto_ablkcipher_cast(req
->base
.tfm
);
673 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request
*req
)
675 struct ablkcipher_tfm
*crt
=
676 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req
));
677 return crt
->encrypt(req
);
680 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request
*req
)
682 struct ablkcipher_tfm
*crt
=
683 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req
));
684 return crt
->decrypt(req
);
687 static inline unsigned int crypto_ablkcipher_reqsize(
688 struct crypto_ablkcipher
*tfm
)
690 return crypto_ablkcipher_crt(tfm
)->reqsize
;
693 static inline void ablkcipher_request_set_tfm(
694 struct ablkcipher_request
*req
, struct crypto_ablkcipher
*tfm
)
696 req
->base
.tfm
= crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm
)->base
);
699 static inline struct ablkcipher_request
*ablkcipher_request_cast(
700 struct crypto_async_request
*req
)
702 return container_of(req
, struct ablkcipher_request
, base
);
705 static inline struct ablkcipher_request
*ablkcipher_request_alloc(
706 struct crypto_ablkcipher
*tfm
, gfp_t gfp
)
708 struct ablkcipher_request
*req
;
710 req
= kmalloc(sizeof(struct ablkcipher_request
) +
711 crypto_ablkcipher_reqsize(tfm
), gfp
);
714 ablkcipher_request_set_tfm(req
, tfm
);
719 static inline void ablkcipher_request_free(struct ablkcipher_request
*req
)
724 static inline void ablkcipher_request_set_callback(
725 struct ablkcipher_request
*req
,
726 u32 flags
, crypto_completion_t complete
, void *data
)
728 req
->base
.complete
= complete
;
729 req
->base
.data
= data
;
730 req
->base
.flags
= flags
;
733 static inline void ablkcipher_request_set_crypt(
734 struct ablkcipher_request
*req
,
735 struct scatterlist
*src
, struct scatterlist
*dst
,
736 unsigned int nbytes
, void *iv
)
740 req
->nbytes
= nbytes
;
744 static inline struct crypto_aead
*__crypto_aead_cast(struct crypto_tfm
*tfm
)
746 return (struct crypto_aead
*)tfm
;
749 struct crypto_aead
*crypto_alloc_aead(const char *alg_name
, u32 type
, u32 mask
);
751 static inline struct crypto_tfm
*crypto_aead_tfm(struct crypto_aead
*tfm
)
756 static inline void crypto_free_aead(struct crypto_aead
*tfm
)
758 crypto_free_tfm(crypto_aead_tfm(tfm
));
761 static inline struct aead_tfm
*crypto_aead_crt(struct crypto_aead
*tfm
)
763 return &crypto_aead_tfm(tfm
)->crt_aead
;
766 static inline unsigned int crypto_aead_ivsize(struct crypto_aead
*tfm
)
768 return crypto_aead_crt(tfm
)->ivsize
;
771 static inline unsigned int crypto_aead_authsize(struct crypto_aead
*tfm
)
773 return crypto_aead_crt(tfm
)->authsize
;
776 static inline unsigned int crypto_aead_blocksize(struct crypto_aead
*tfm
)
778 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm
));
781 static inline unsigned int crypto_aead_alignmask(struct crypto_aead
*tfm
)
783 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm
));
786 static inline u32
crypto_aead_get_flags(struct crypto_aead
*tfm
)
788 return crypto_tfm_get_flags(crypto_aead_tfm(tfm
));
791 static inline void crypto_aead_set_flags(struct crypto_aead
*tfm
, u32 flags
)
793 crypto_tfm_set_flags(crypto_aead_tfm(tfm
), flags
);
796 static inline void crypto_aead_clear_flags(struct crypto_aead
*tfm
, u32 flags
)
798 crypto_tfm_clear_flags(crypto_aead_tfm(tfm
), flags
);
801 static inline int crypto_aead_setkey(struct crypto_aead
*tfm
, const u8
*key
,
804 struct aead_tfm
*crt
= crypto_aead_crt(tfm
);
806 return crt
->setkey(crt
->base
, key
, keylen
);
809 int crypto_aead_setauthsize(struct crypto_aead
*tfm
, unsigned int authsize
);
811 static inline struct crypto_aead
*crypto_aead_reqtfm(struct aead_request
*req
)
813 return __crypto_aead_cast(req
->base
.tfm
);
816 static inline int crypto_aead_encrypt(struct aead_request
*req
)
818 return crypto_aead_crt(crypto_aead_reqtfm(req
))->encrypt(req
);
821 static inline int crypto_aead_decrypt(struct aead_request
*req
)
823 return crypto_aead_crt(crypto_aead_reqtfm(req
))->decrypt(req
);
826 static inline unsigned int crypto_aead_reqsize(struct crypto_aead
*tfm
)
828 return crypto_aead_crt(tfm
)->reqsize
;
831 static inline void aead_request_set_tfm(struct aead_request
*req
,
832 struct crypto_aead
*tfm
)
834 req
->base
.tfm
= crypto_aead_tfm(crypto_aead_crt(tfm
)->base
);
837 static inline struct aead_request
*aead_request_alloc(struct crypto_aead
*tfm
,
840 struct aead_request
*req
;
842 req
= kmalloc(sizeof(*req
) + crypto_aead_reqsize(tfm
), gfp
);
845 aead_request_set_tfm(req
, tfm
);
850 static inline void aead_request_free(struct aead_request
*req
)
855 static inline void aead_request_set_callback(struct aead_request
*req
,
857 crypto_completion_t complete
,
860 req
->base
.complete
= complete
;
861 req
->base
.data
= data
;
862 req
->base
.flags
= flags
;
865 static inline void aead_request_set_crypt(struct aead_request
*req
,
866 struct scatterlist
*src
,
867 struct scatterlist
*dst
,
868 unsigned int cryptlen
, u8
*iv
)
872 req
->cryptlen
= cryptlen
;
876 static inline void aead_request_set_assoc(struct aead_request
*req
,
877 struct scatterlist
*assoc
,
878 unsigned int assoclen
)
881 req
->assoclen
= assoclen
;
884 static inline struct crypto_blkcipher
*__crypto_blkcipher_cast(
885 struct crypto_tfm
*tfm
)
887 return (struct crypto_blkcipher
*)tfm
;
890 static inline struct crypto_blkcipher
*crypto_blkcipher_cast(
891 struct crypto_tfm
*tfm
)
893 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_BLKCIPHER
);
894 return __crypto_blkcipher_cast(tfm
);
897 static inline struct crypto_blkcipher
*crypto_alloc_blkcipher(
898 const char *alg_name
, u32 type
, u32 mask
)
900 type
&= ~CRYPTO_ALG_TYPE_MASK
;
901 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
902 mask
|= CRYPTO_ALG_TYPE_MASK
;
904 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name
, type
, mask
));
907 static inline struct crypto_tfm
*crypto_blkcipher_tfm(
908 struct crypto_blkcipher
*tfm
)
913 static inline void crypto_free_blkcipher(struct crypto_blkcipher
*tfm
)
915 crypto_free_tfm(crypto_blkcipher_tfm(tfm
));
918 static inline int crypto_has_blkcipher(const char *alg_name
, u32 type
, u32 mask
)
920 type
&= ~CRYPTO_ALG_TYPE_MASK
;
921 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
922 mask
|= CRYPTO_ALG_TYPE_MASK
;
924 return crypto_has_alg(alg_name
, type
, mask
);
927 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher
*tfm
)
929 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm
));
932 static inline struct blkcipher_tfm
*crypto_blkcipher_crt(
933 struct crypto_blkcipher
*tfm
)
935 return &crypto_blkcipher_tfm(tfm
)->crt_blkcipher
;
938 static inline struct blkcipher_alg
*crypto_blkcipher_alg(
939 struct crypto_blkcipher
*tfm
)
941 return &crypto_blkcipher_tfm(tfm
)->__crt_alg
->cra_blkcipher
;
944 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher
*tfm
)
946 return crypto_blkcipher_alg(tfm
)->ivsize
;
949 static inline unsigned int crypto_blkcipher_blocksize(
950 struct crypto_blkcipher
*tfm
)
952 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm
));
955 static inline unsigned int crypto_blkcipher_alignmask(
956 struct crypto_blkcipher
*tfm
)
958 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm
));
961 static inline u32
crypto_blkcipher_get_flags(struct crypto_blkcipher
*tfm
)
963 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm
));
966 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher
*tfm
,
969 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm
), flags
);
972 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher
*tfm
,
975 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm
), flags
);
978 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher
*tfm
,
979 const u8
*key
, unsigned int keylen
)
981 return crypto_blkcipher_crt(tfm
)->setkey(crypto_blkcipher_tfm(tfm
),
985 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc
*desc
,
986 struct scatterlist
*dst
,
987 struct scatterlist
*src
,
990 desc
->info
= crypto_blkcipher_crt(desc
->tfm
)->iv
;
991 return crypto_blkcipher_crt(desc
->tfm
)->encrypt(desc
, dst
, src
, nbytes
);
994 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc
*desc
,
995 struct scatterlist
*dst
,
996 struct scatterlist
*src
,
999 return crypto_blkcipher_crt(desc
->tfm
)->encrypt(desc
, dst
, src
, nbytes
);
1002 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc
*desc
,
1003 struct scatterlist
*dst
,
1004 struct scatterlist
*src
,
1005 unsigned int nbytes
)
1007 desc
->info
= crypto_blkcipher_crt(desc
->tfm
)->iv
;
1008 return crypto_blkcipher_crt(desc
->tfm
)->decrypt(desc
, dst
, src
, nbytes
);
1011 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc
*desc
,
1012 struct scatterlist
*dst
,
1013 struct scatterlist
*src
,
1014 unsigned int nbytes
)
1016 return crypto_blkcipher_crt(desc
->tfm
)->decrypt(desc
, dst
, src
, nbytes
);
1019 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher
*tfm
,
1020 const u8
*src
, unsigned int len
)
1022 memcpy(crypto_blkcipher_crt(tfm
)->iv
, src
, len
);
1025 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher
*tfm
,
1026 u8
*dst
, unsigned int len
)
1028 memcpy(dst
, crypto_blkcipher_crt(tfm
)->iv
, len
);
1031 static inline struct crypto_cipher
*__crypto_cipher_cast(struct crypto_tfm
*tfm
)
1033 return (struct crypto_cipher
*)tfm
;
1036 static inline struct crypto_cipher
*crypto_cipher_cast(struct crypto_tfm
*tfm
)
1038 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
1039 return __crypto_cipher_cast(tfm
);
1042 static inline struct crypto_cipher
*crypto_alloc_cipher(const char *alg_name
,
1045 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1046 type
|= CRYPTO_ALG_TYPE_CIPHER
;
1047 mask
|= CRYPTO_ALG_TYPE_MASK
;
1049 return __crypto_cipher_cast(crypto_alloc_base(alg_name
, type
, mask
));
1052 static inline struct crypto_tfm
*crypto_cipher_tfm(struct crypto_cipher
*tfm
)
1057 static inline void crypto_free_cipher(struct crypto_cipher
*tfm
)
1059 crypto_free_tfm(crypto_cipher_tfm(tfm
));
1062 static inline int crypto_has_cipher(const char *alg_name
, u32 type
, u32 mask
)
1064 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1065 type
|= CRYPTO_ALG_TYPE_CIPHER
;
1066 mask
|= CRYPTO_ALG_TYPE_MASK
;
1068 return crypto_has_alg(alg_name
, type
, mask
);
1071 static inline struct cipher_tfm
*crypto_cipher_crt(struct crypto_cipher
*tfm
)
1073 return &crypto_cipher_tfm(tfm
)->crt_cipher
;
1076 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher
*tfm
)
1078 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm
));
1081 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher
*tfm
)
1083 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm
));
1086 static inline u32
crypto_cipher_get_flags(struct crypto_cipher
*tfm
)
1088 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm
));
1091 static inline void crypto_cipher_set_flags(struct crypto_cipher
*tfm
,
1094 crypto_tfm_set_flags(crypto_cipher_tfm(tfm
), flags
);
1097 static inline void crypto_cipher_clear_flags(struct crypto_cipher
*tfm
,
1100 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm
), flags
);
1103 static inline int crypto_cipher_setkey(struct crypto_cipher
*tfm
,
1104 const u8
*key
, unsigned int keylen
)
1106 return crypto_cipher_crt(tfm
)->cit_setkey(crypto_cipher_tfm(tfm
),
1110 static inline void crypto_cipher_encrypt_one(struct crypto_cipher
*tfm
,
1111 u8
*dst
, const u8
*src
)
1113 crypto_cipher_crt(tfm
)->cit_encrypt_one(crypto_cipher_tfm(tfm
),
1117 static inline void crypto_cipher_decrypt_one(struct crypto_cipher
*tfm
,
1118 u8
*dst
, const u8
*src
)
1120 crypto_cipher_crt(tfm
)->cit_decrypt_one(crypto_cipher_tfm(tfm
),
1124 static inline struct crypto_hash
*__crypto_hash_cast(struct crypto_tfm
*tfm
)
1126 return (struct crypto_hash
*)tfm
;
1129 static inline struct crypto_hash
*crypto_hash_cast(struct crypto_tfm
*tfm
)
1131 BUG_ON((crypto_tfm_alg_type(tfm
) ^ CRYPTO_ALG_TYPE_HASH
) &
1132 CRYPTO_ALG_TYPE_HASH_MASK
);
1133 return __crypto_hash_cast(tfm
);
1136 static inline struct crypto_hash
*crypto_alloc_hash(const char *alg_name
,
1139 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1140 mask
&= ~CRYPTO_ALG_TYPE_MASK
;
1141 type
|= CRYPTO_ALG_TYPE_HASH
;
1142 mask
|= CRYPTO_ALG_TYPE_HASH_MASK
;
1144 return __crypto_hash_cast(crypto_alloc_base(alg_name
, type
, mask
));
1147 static inline struct crypto_tfm
*crypto_hash_tfm(struct crypto_hash
*tfm
)
1152 static inline void crypto_free_hash(struct crypto_hash
*tfm
)
1154 crypto_free_tfm(crypto_hash_tfm(tfm
));
1157 static inline int crypto_has_hash(const char *alg_name
, u32 type
, u32 mask
)
1159 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1160 mask
&= ~CRYPTO_ALG_TYPE_MASK
;
1161 type
|= CRYPTO_ALG_TYPE_HASH
;
1162 mask
|= CRYPTO_ALG_TYPE_HASH_MASK
;
1164 return crypto_has_alg(alg_name
, type
, mask
);
1167 static inline struct hash_tfm
*crypto_hash_crt(struct crypto_hash
*tfm
)
1169 return &crypto_hash_tfm(tfm
)->crt_hash
;
1172 static inline unsigned int crypto_hash_blocksize(struct crypto_hash
*tfm
)
1174 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm
));
1177 static inline unsigned int crypto_hash_alignmask(struct crypto_hash
*tfm
)
1179 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm
));
1182 static inline unsigned int crypto_hash_digestsize(struct crypto_hash
*tfm
)
1184 return crypto_hash_crt(tfm
)->digestsize
;
1187 static inline u32
crypto_hash_get_flags(struct crypto_hash
*tfm
)
1189 return crypto_tfm_get_flags(crypto_hash_tfm(tfm
));
1192 static inline void crypto_hash_set_flags(struct crypto_hash
*tfm
, u32 flags
)
1194 crypto_tfm_set_flags(crypto_hash_tfm(tfm
), flags
);
1197 static inline void crypto_hash_clear_flags(struct crypto_hash
*tfm
, u32 flags
)
1199 crypto_tfm_clear_flags(crypto_hash_tfm(tfm
), flags
);
1202 static inline int crypto_hash_init(struct hash_desc
*desc
)
1204 return crypto_hash_crt(desc
->tfm
)->init(desc
);
1207 static inline int crypto_hash_update(struct hash_desc
*desc
,
1208 struct scatterlist
*sg
,
1209 unsigned int nbytes
)
1211 return crypto_hash_crt(desc
->tfm
)->update(desc
, sg
, nbytes
);
1214 static inline int crypto_hash_final(struct hash_desc
*desc
, u8
*out
)
1216 return crypto_hash_crt(desc
->tfm
)->final(desc
, out
);
1219 static inline int crypto_hash_digest(struct hash_desc
*desc
,
1220 struct scatterlist
*sg
,
1221 unsigned int nbytes
, u8
*out
)
1223 return crypto_hash_crt(desc
->tfm
)->digest(desc
, sg
, nbytes
, out
);
1226 static inline int crypto_hash_setkey(struct crypto_hash
*hash
,
1227 const u8
*key
, unsigned int keylen
)
1229 return crypto_hash_crt(hash
)->setkey(hash
, key
, keylen
);
1232 static inline struct crypto_comp
*__crypto_comp_cast(struct crypto_tfm
*tfm
)
1234 return (struct crypto_comp
*)tfm
;
1237 static inline struct crypto_comp
*crypto_comp_cast(struct crypto_tfm
*tfm
)
1239 BUG_ON((crypto_tfm_alg_type(tfm
) ^ CRYPTO_ALG_TYPE_COMPRESS
) &
1240 CRYPTO_ALG_TYPE_MASK
);
1241 return __crypto_comp_cast(tfm
);
1244 static inline struct crypto_comp
*crypto_alloc_comp(const char *alg_name
,
1247 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1248 type
|= CRYPTO_ALG_TYPE_COMPRESS
;
1249 mask
|= CRYPTO_ALG_TYPE_MASK
;
1251 return __crypto_comp_cast(crypto_alloc_base(alg_name
, type
, mask
));
1254 static inline struct crypto_tfm
*crypto_comp_tfm(struct crypto_comp
*tfm
)
1259 static inline void crypto_free_comp(struct crypto_comp
*tfm
)
1261 crypto_free_tfm(crypto_comp_tfm(tfm
));
1264 static inline int crypto_has_comp(const char *alg_name
, u32 type
, u32 mask
)
1266 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1267 type
|= CRYPTO_ALG_TYPE_COMPRESS
;
1268 mask
|= CRYPTO_ALG_TYPE_MASK
;
1270 return crypto_has_alg(alg_name
, type
, mask
);
1273 static inline const char *crypto_comp_name(struct crypto_comp
*tfm
)
1275 return crypto_tfm_alg_name(crypto_comp_tfm(tfm
));
1278 static inline struct compress_tfm
*crypto_comp_crt(struct crypto_comp
*tfm
)
1280 return &crypto_comp_tfm(tfm
)->crt_compress
;
1283 static inline int crypto_comp_compress(struct crypto_comp
*tfm
,
1284 const u8
*src
, unsigned int slen
,
1285 u8
*dst
, unsigned int *dlen
)
1287 return crypto_comp_crt(tfm
)->cot_compress(crypto_comp_tfm(tfm
),
1288 src
, slen
, dst
, dlen
);
1291 static inline int crypto_comp_decompress(struct crypto_comp
*tfm
,
1292 const u8
*src
, unsigned int slen
,
1293 u8
*dst
, unsigned int *dlen
)
1295 return crypto_comp_crt(tfm
)->cot_decompress(crypto_comp_tfm(tfm
),
1296 src
, slen
, dst
, dlen
);
1299 #endif /* _LINUX_CRYPTO_H */