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/slab.h>
24 #include <linux/string.h>
25 #include <linux/uaccess.h>
28 * Algorithm masks and types.
30 #define CRYPTO_ALG_TYPE_MASK 0x0000000f
31 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
32 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
33 #define CRYPTO_ALG_TYPE_AEAD 0x00000003
34 #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
35 #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
36 #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
37 #define CRYPTO_ALG_TYPE_DIGEST 0x00000008
38 #define CRYPTO_ALG_TYPE_HASH 0x00000008
39 #define CRYPTO_ALG_TYPE_SHASH 0x00000009
40 #define CRYPTO_ALG_TYPE_AHASH 0x0000000a
41 #define CRYPTO_ALG_TYPE_RNG 0x0000000c
42 #define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f
44 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
45 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
46 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
48 #define CRYPTO_ALG_LARVAL 0x00000010
49 #define CRYPTO_ALG_DEAD 0x00000020
50 #define CRYPTO_ALG_DYING 0x00000040
51 #define CRYPTO_ALG_ASYNC 0x00000080
54 * Set this bit if and only if the algorithm requires another algorithm of
55 * the same type to handle corner cases.
57 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
60 * This bit is set for symmetric key ciphers that have already been wrapped
61 * with a generic IV generator to prevent them from being wrapped again.
63 #define CRYPTO_ALG_GENIV 0x00000200
66 * Set if the algorithm has passed automated run-time testing. Note that
67 * if there is no run-time testing for a given algorithm it is considered
71 #define CRYPTO_ALG_TESTED 0x00000400
74 * Set if the algorithm is an instance that is build from templates.
76 #define CRYPTO_ALG_INSTANCE 0x00000800
78 /* Set this bit if the algorithm provided is hardware accelerated but
79 * not available to userspace via instruction set or so.
81 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
84 * Transform masks and values (for crt_flags).
86 #define CRYPTO_TFM_REQ_MASK 0x000fff00
87 #define CRYPTO_TFM_RES_MASK 0xfff00000
89 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
90 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
91 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
92 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
93 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
94 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
95 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
96 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
99 * Miscellaneous stuff.
101 #define CRYPTO_MAX_ALG_NAME 64
104 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
105 * declaration) is used to ensure that the crypto_tfm context structure is
106 * aligned correctly for the given architecture so that there are no alignment
107 * faults for C data types. In particular, this is required on platforms such
108 * as arm where pointers are 32-bit aligned but there are data types such as
109 * u64 which require 64-bit alignment.
111 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
113 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
116 struct crypto_ablkcipher
;
117 struct crypto_async_request
;
119 struct crypto_blkcipher
;
124 struct aead_givcrypt_request
;
125 struct skcipher_givcrypt_request
;
127 typedef void (*crypto_completion_t
)(struct crypto_async_request
*req
, int err
);
129 struct crypto_async_request
{
130 struct list_head list
;
131 crypto_completion_t complete
;
133 struct crypto_tfm
*tfm
;
138 struct ablkcipher_request
{
139 struct crypto_async_request base
;
145 struct scatterlist
*src
;
146 struct scatterlist
*dst
;
148 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
152 * struct aead_request - AEAD request
153 * @base: Common attributes for async crypto requests
154 * @assoclen: Length in bytes of associated data for authentication
155 * @cryptlen: Length of data to be encrypted or decrypted
156 * @iv: Initialisation vector
157 * @assoc: Associated data
159 * @dst: Destination data
160 * @__ctx: Start of private context data
162 struct aead_request
{
163 struct crypto_async_request base
;
165 unsigned int assoclen
;
166 unsigned int cryptlen
;
170 struct scatterlist
*assoc
;
171 struct scatterlist
*src
;
172 struct scatterlist
*dst
;
174 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
177 struct blkcipher_desc
{
178 struct crypto_blkcipher
*tfm
;
184 struct crypto_tfm
*tfm
;
185 void (*crfn
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
186 unsigned int (*prfn
)(const struct cipher_desc
*desc
, u8
*dst
,
187 const u8
*src
, unsigned int nbytes
);
192 struct crypto_hash
*tfm
;
197 * Algorithms: modular crypto algorithm implementations, managed
198 * via crypto_register_alg() and crypto_unregister_alg().
200 struct ablkcipher_alg
{
201 int (*setkey
)(struct crypto_ablkcipher
*tfm
, const u8
*key
,
202 unsigned int keylen
);
203 int (*encrypt
)(struct ablkcipher_request
*req
);
204 int (*decrypt
)(struct ablkcipher_request
*req
);
205 int (*givencrypt
)(struct skcipher_givcrypt_request
*req
);
206 int (*givdecrypt
)(struct skcipher_givcrypt_request
*req
);
210 unsigned int min_keysize
;
211 unsigned int max_keysize
;
216 int (*setkey
)(struct crypto_aead
*tfm
, const u8
*key
,
217 unsigned int keylen
);
218 int (*setauthsize
)(struct crypto_aead
*tfm
, unsigned int authsize
);
219 int (*encrypt
)(struct aead_request
*req
);
220 int (*decrypt
)(struct aead_request
*req
);
221 int (*givencrypt
)(struct aead_givcrypt_request
*req
);
222 int (*givdecrypt
)(struct aead_givcrypt_request
*req
);
227 unsigned int maxauthsize
;
230 struct blkcipher_alg
{
231 int (*setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
232 unsigned int keylen
);
233 int (*encrypt
)(struct blkcipher_desc
*desc
,
234 struct scatterlist
*dst
, struct scatterlist
*src
,
235 unsigned int nbytes
);
236 int (*decrypt
)(struct blkcipher_desc
*desc
,
237 struct scatterlist
*dst
, struct scatterlist
*src
,
238 unsigned int nbytes
);
242 unsigned int min_keysize
;
243 unsigned int max_keysize
;
248 unsigned int cia_min_keysize
;
249 unsigned int cia_max_keysize
;
250 int (*cia_setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
251 unsigned int keylen
);
252 void (*cia_encrypt
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
253 void (*cia_decrypt
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
256 struct compress_alg
{
257 int (*coa_compress
)(struct crypto_tfm
*tfm
, const u8
*src
,
258 unsigned int slen
, u8
*dst
, unsigned int *dlen
);
259 int (*coa_decompress
)(struct crypto_tfm
*tfm
, const u8
*src
,
260 unsigned int slen
, u8
*dst
, unsigned int *dlen
);
264 int (*rng_make_random
)(struct crypto_rng
*tfm
, u8
*rdata
,
266 int (*rng_reset
)(struct crypto_rng
*tfm
, u8
*seed
, unsigned int slen
);
268 unsigned int seedsize
;
272 #define cra_ablkcipher cra_u.ablkcipher
273 #define cra_aead cra_u.aead
274 #define cra_blkcipher cra_u.blkcipher
275 #define cra_cipher cra_u.cipher
276 #define cra_compress cra_u.compress
277 #define cra_rng cra_u.rng
280 struct list_head cra_list
;
281 struct list_head cra_users
;
284 unsigned int cra_blocksize
;
285 unsigned int cra_ctxsize
;
286 unsigned int cra_alignmask
;
291 char cra_name
[CRYPTO_MAX_ALG_NAME
];
292 char cra_driver_name
[CRYPTO_MAX_ALG_NAME
];
294 const struct crypto_type
*cra_type
;
297 struct ablkcipher_alg ablkcipher
;
298 struct aead_alg aead
;
299 struct blkcipher_alg blkcipher
;
300 struct cipher_alg cipher
;
301 struct compress_alg compress
;
305 int (*cra_init
)(struct crypto_tfm
*tfm
);
306 void (*cra_exit
)(struct crypto_tfm
*tfm
);
307 void (*cra_destroy
)(struct crypto_alg
*alg
);
309 struct module
*cra_module
;
313 * Algorithm registration interface.
315 int crypto_register_alg(struct crypto_alg
*alg
);
316 int crypto_unregister_alg(struct crypto_alg
*alg
);
317 int crypto_register_algs(struct crypto_alg
*algs
, int count
);
318 int crypto_unregister_algs(struct crypto_alg
*algs
, int count
);
321 * Algorithm query interface.
323 int crypto_has_alg(const char *name
, u32 type
, u32 mask
);
326 * Transforms: user-instantiated objects which encapsulate algorithms
327 * and core processing logic. Managed via crypto_alloc_*() and
328 * crypto_free_*(), as well as the various helpers below.
331 struct ablkcipher_tfm
{
332 int (*setkey
)(struct crypto_ablkcipher
*tfm
, const u8
*key
,
333 unsigned int keylen
);
334 int (*encrypt
)(struct ablkcipher_request
*req
);
335 int (*decrypt
)(struct ablkcipher_request
*req
);
336 int (*givencrypt
)(struct skcipher_givcrypt_request
*req
);
337 int (*givdecrypt
)(struct skcipher_givcrypt_request
*req
);
339 struct crypto_ablkcipher
*base
;
342 unsigned int reqsize
;
346 int (*setkey
)(struct crypto_aead
*tfm
, const u8
*key
,
347 unsigned int keylen
);
348 int (*encrypt
)(struct aead_request
*req
);
349 int (*decrypt
)(struct aead_request
*req
);
350 int (*givencrypt
)(struct aead_givcrypt_request
*req
);
351 int (*givdecrypt
)(struct aead_givcrypt_request
*req
);
353 struct crypto_aead
*base
;
356 unsigned int authsize
;
357 unsigned int reqsize
;
360 struct blkcipher_tfm
{
362 int (*setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
363 unsigned int keylen
);
364 int (*encrypt
)(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
365 struct scatterlist
*src
, unsigned int nbytes
);
366 int (*decrypt
)(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
367 struct scatterlist
*src
, unsigned int nbytes
);
371 int (*cit_setkey
)(struct crypto_tfm
*tfm
,
372 const u8
*key
, unsigned int keylen
);
373 void (*cit_encrypt_one
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
374 void (*cit_decrypt_one
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
378 int (*init
)(struct hash_desc
*desc
);
379 int (*update
)(struct hash_desc
*desc
,
380 struct scatterlist
*sg
, unsigned int nsg
);
381 int (*final
)(struct hash_desc
*desc
, u8
*out
);
382 int (*digest
)(struct hash_desc
*desc
, struct scatterlist
*sg
,
383 unsigned int nsg
, u8
*out
);
384 int (*setkey
)(struct crypto_hash
*tfm
, const u8
*key
,
385 unsigned int keylen
);
386 unsigned int digestsize
;
389 struct compress_tfm
{
390 int (*cot_compress
)(struct crypto_tfm
*tfm
,
391 const u8
*src
, unsigned int slen
,
392 u8
*dst
, unsigned int *dlen
);
393 int (*cot_decompress
)(struct crypto_tfm
*tfm
,
394 const u8
*src
, unsigned int slen
,
395 u8
*dst
, unsigned int *dlen
);
399 int (*rng_gen_random
)(struct crypto_rng
*tfm
, u8
*rdata
,
401 int (*rng_reset
)(struct crypto_rng
*tfm
, u8
*seed
, unsigned int slen
);
404 #define crt_ablkcipher crt_u.ablkcipher
405 #define crt_aead crt_u.aead
406 #define crt_blkcipher crt_u.blkcipher
407 #define crt_cipher crt_u.cipher
408 #define crt_hash crt_u.hash
409 #define crt_compress crt_u.compress
410 #define crt_rng crt_u.rng
417 struct ablkcipher_tfm ablkcipher
;
418 struct aead_tfm aead
;
419 struct blkcipher_tfm blkcipher
;
420 struct cipher_tfm cipher
;
421 struct hash_tfm hash
;
422 struct compress_tfm compress
;
426 void (*exit
)(struct crypto_tfm
*tfm
);
428 struct crypto_alg
*__crt_alg
;
430 void *__crt_ctx
[] CRYPTO_MINALIGN_ATTR
;
433 struct crypto_ablkcipher
{
434 struct crypto_tfm base
;
438 struct crypto_tfm base
;
441 struct crypto_blkcipher
{
442 struct crypto_tfm base
;
445 struct crypto_cipher
{
446 struct crypto_tfm base
;
450 struct crypto_tfm base
;
454 struct crypto_tfm base
;
458 struct crypto_tfm base
;
469 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
471 /* Maximum number of (rtattr) parameters for each template. */
472 #define CRYPTO_MAX_ATTRS 32
474 struct crypto_attr_alg
{
475 char name
[CRYPTO_MAX_ALG_NAME
];
478 struct crypto_attr_type
{
483 struct crypto_attr_u32
{
488 * Transform user interface.
491 struct crypto_tfm
*crypto_alloc_base(const char *alg_name
, u32 type
, u32 mask
);
492 void crypto_destroy_tfm(void *mem
, struct crypto_tfm
*tfm
);
494 static inline void crypto_free_tfm(struct crypto_tfm
*tfm
)
496 return crypto_destroy_tfm(tfm
, tfm
);
499 int alg_test(const char *driver
, const char *alg
, u32 type
, u32 mask
);
502 * Transform helpers which query the underlying algorithm.
504 static inline const char *crypto_tfm_alg_name(struct crypto_tfm
*tfm
)
506 return tfm
->__crt_alg
->cra_name
;
509 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm
*tfm
)
511 return tfm
->__crt_alg
->cra_driver_name
;
514 static inline int crypto_tfm_alg_priority(struct crypto_tfm
*tfm
)
516 return tfm
->__crt_alg
->cra_priority
;
519 static inline u32
crypto_tfm_alg_type(struct crypto_tfm
*tfm
)
521 return tfm
->__crt_alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
;
524 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm
*tfm
)
526 return tfm
->__crt_alg
->cra_blocksize
;
529 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm
*tfm
)
531 return tfm
->__crt_alg
->cra_alignmask
;
534 static inline u32
crypto_tfm_get_flags(struct crypto_tfm
*tfm
)
536 return tfm
->crt_flags
;
539 static inline void crypto_tfm_set_flags(struct crypto_tfm
*tfm
, u32 flags
)
541 tfm
->crt_flags
|= flags
;
544 static inline void crypto_tfm_clear_flags(struct crypto_tfm
*tfm
, u32 flags
)
546 tfm
->crt_flags
&= ~flags
;
549 static inline void *crypto_tfm_ctx(struct crypto_tfm
*tfm
)
551 return tfm
->__crt_ctx
;
554 static inline unsigned int crypto_tfm_ctx_alignment(void)
556 struct crypto_tfm
*tfm
;
557 return __alignof__(tfm
->__crt_ctx
);
563 static inline struct crypto_ablkcipher
*__crypto_ablkcipher_cast(
564 struct crypto_tfm
*tfm
)
566 return (struct crypto_ablkcipher
*)tfm
;
569 static inline u32
crypto_skcipher_type(u32 type
)
571 type
&= ~(CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_GENIV
);
572 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
576 static inline u32
crypto_skcipher_mask(u32 mask
)
578 mask
&= ~(CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_GENIV
);
579 mask
|= CRYPTO_ALG_TYPE_BLKCIPHER_MASK
;
583 struct crypto_ablkcipher
*crypto_alloc_ablkcipher(const char *alg_name
,
586 static inline struct crypto_tfm
*crypto_ablkcipher_tfm(
587 struct crypto_ablkcipher
*tfm
)
592 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher
*tfm
)
594 crypto_free_tfm(crypto_ablkcipher_tfm(tfm
));
597 static inline int crypto_has_ablkcipher(const char *alg_name
, u32 type
,
600 return crypto_has_alg(alg_name
, crypto_skcipher_type(type
),
601 crypto_skcipher_mask(mask
));
604 static inline struct ablkcipher_tfm
*crypto_ablkcipher_crt(
605 struct crypto_ablkcipher
*tfm
)
607 return &crypto_ablkcipher_tfm(tfm
)->crt_ablkcipher
;
610 static inline unsigned int crypto_ablkcipher_ivsize(
611 struct crypto_ablkcipher
*tfm
)
613 return crypto_ablkcipher_crt(tfm
)->ivsize
;
616 static inline unsigned int crypto_ablkcipher_blocksize(
617 struct crypto_ablkcipher
*tfm
)
619 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm
));
622 static inline unsigned int crypto_ablkcipher_alignmask(
623 struct crypto_ablkcipher
*tfm
)
625 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm
));
628 static inline u32
crypto_ablkcipher_get_flags(struct crypto_ablkcipher
*tfm
)
630 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm
));
633 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher
*tfm
,
636 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm
), flags
);
639 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher
*tfm
,
642 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm
), flags
);
645 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher
*tfm
,
646 const u8
*key
, unsigned int keylen
)
648 struct ablkcipher_tfm
*crt
= crypto_ablkcipher_crt(tfm
);
650 return crt
->setkey(crt
->base
, key
, keylen
);
653 static inline struct crypto_ablkcipher
*crypto_ablkcipher_reqtfm(
654 struct ablkcipher_request
*req
)
656 return __crypto_ablkcipher_cast(req
->base
.tfm
);
659 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request
*req
)
661 struct ablkcipher_tfm
*crt
=
662 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req
));
663 return crt
->encrypt(req
);
666 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request
*req
)
668 struct ablkcipher_tfm
*crt
=
669 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req
));
670 return crt
->decrypt(req
);
673 static inline unsigned int crypto_ablkcipher_reqsize(
674 struct crypto_ablkcipher
*tfm
)
676 return crypto_ablkcipher_crt(tfm
)->reqsize
;
679 static inline void ablkcipher_request_set_tfm(
680 struct ablkcipher_request
*req
, struct crypto_ablkcipher
*tfm
)
682 req
->base
.tfm
= crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm
)->base
);
685 static inline struct ablkcipher_request
*ablkcipher_request_cast(
686 struct crypto_async_request
*req
)
688 return container_of(req
, struct ablkcipher_request
, base
);
691 static inline struct ablkcipher_request
*ablkcipher_request_alloc(
692 struct crypto_ablkcipher
*tfm
, gfp_t gfp
)
694 struct ablkcipher_request
*req
;
696 req
= kmalloc(sizeof(struct ablkcipher_request
) +
697 crypto_ablkcipher_reqsize(tfm
), gfp
);
700 ablkcipher_request_set_tfm(req
, tfm
);
705 static inline void ablkcipher_request_free(struct ablkcipher_request
*req
)
710 static inline void ablkcipher_request_set_callback(
711 struct ablkcipher_request
*req
,
712 u32 flags
, crypto_completion_t complete
, void *data
)
714 req
->base
.complete
= complete
;
715 req
->base
.data
= data
;
716 req
->base
.flags
= flags
;
719 static inline void ablkcipher_request_set_crypt(
720 struct ablkcipher_request
*req
,
721 struct scatterlist
*src
, struct scatterlist
*dst
,
722 unsigned int nbytes
, void *iv
)
726 req
->nbytes
= nbytes
;
730 static inline struct crypto_aead
*__crypto_aead_cast(struct crypto_tfm
*tfm
)
732 return (struct crypto_aead
*)tfm
;
735 struct crypto_aead
*crypto_alloc_aead(const char *alg_name
, u32 type
, u32 mask
);
737 static inline struct crypto_tfm
*crypto_aead_tfm(struct crypto_aead
*tfm
)
742 static inline void crypto_free_aead(struct crypto_aead
*tfm
)
744 crypto_free_tfm(crypto_aead_tfm(tfm
));
747 static inline struct aead_tfm
*crypto_aead_crt(struct crypto_aead
*tfm
)
749 return &crypto_aead_tfm(tfm
)->crt_aead
;
752 static inline unsigned int crypto_aead_ivsize(struct crypto_aead
*tfm
)
754 return crypto_aead_crt(tfm
)->ivsize
;
757 static inline unsigned int crypto_aead_authsize(struct crypto_aead
*tfm
)
759 return crypto_aead_crt(tfm
)->authsize
;
762 static inline unsigned int crypto_aead_blocksize(struct crypto_aead
*tfm
)
764 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm
));
767 static inline unsigned int crypto_aead_alignmask(struct crypto_aead
*tfm
)
769 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm
));
772 static inline u32
crypto_aead_get_flags(struct crypto_aead
*tfm
)
774 return crypto_tfm_get_flags(crypto_aead_tfm(tfm
));
777 static inline void crypto_aead_set_flags(struct crypto_aead
*tfm
, u32 flags
)
779 crypto_tfm_set_flags(crypto_aead_tfm(tfm
), flags
);
782 static inline void crypto_aead_clear_flags(struct crypto_aead
*tfm
, u32 flags
)
784 crypto_tfm_clear_flags(crypto_aead_tfm(tfm
), flags
);
787 static inline int crypto_aead_setkey(struct crypto_aead
*tfm
, const u8
*key
,
790 struct aead_tfm
*crt
= crypto_aead_crt(tfm
);
792 return crt
->setkey(crt
->base
, key
, keylen
);
795 int crypto_aead_setauthsize(struct crypto_aead
*tfm
, unsigned int authsize
);
797 static inline struct crypto_aead
*crypto_aead_reqtfm(struct aead_request
*req
)
799 return __crypto_aead_cast(req
->base
.tfm
);
802 static inline int crypto_aead_encrypt(struct aead_request
*req
)
804 return crypto_aead_crt(crypto_aead_reqtfm(req
))->encrypt(req
);
807 static inline int crypto_aead_decrypt(struct aead_request
*req
)
809 return crypto_aead_crt(crypto_aead_reqtfm(req
))->decrypt(req
);
812 static inline unsigned int crypto_aead_reqsize(struct crypto_aead
*tfm
)
814 return crypto_aead_crt(tfm
)->reqsize
;
817 static inline void aead_request_set_tfm(struct aead_request
*req
,
818 struct crypto_aead
*tfm
)
820 req
->base
.tfm
= crypto_aead_tfm(crypto_aead_crt(tfm
)->base
);
823 static inline struct aead_request
*aead_request_alloc(struct crypto_aead
*tfm
,
826 struct aead_request
*req
;
828 req
= kmalloc(sizeof(*req
) + crypto_aead_reqsize(tfm
), gfp
);
831 aead_request_set_tfm(req
, tfm
);
836 static inline void aead_request_free(struct aead_request
*req
)
841 static inline void aead_request_set_callback(struct aead_request
*req
,
843 crypto_completion_t complete
,
846 req
->base
.complete
= complete
;
847 req
->base
.data
= data
;
848 req
->base
.flags
= flags
;
851 static inline void aead_request_set_crypt(struct aead_request
*req
,
852 struct scatterlist
*src
,
853 struct scatterlist
*dst
,
854 unsigned int cryptlen
, u8
*iv
)
858 req
->cryptlen
= cryptlen
;
862 static inline void aead_request_set_assoc(struct aead_request
*req
,
863 struct scatterlist
*assoc
,
864 unsigned int assoclen
)
867 req
->assoclen
= assoclen
;
870 static inline struct crypto_blkcipher
*__crypto_blkcipher_cast(
871 struct crypto_tfm
*tfm
)
873 return (struct crypto_blkcipher
*)tfm
;
876 static inline struct crypto_blkcipher
*crypto_blkcipher_cast(
877 struct crypto_tfm
*tfm
)
879 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_BLKCIPHER
);
880 return __crypto_blkcipher_cast(tfm
);
883 static inline struct crypto_blkcipher
*crypto_alloc_blkcipher(
884 const char *alg_name
, u32 type
, u32 mask
)
886 type
&= ~CRYPTO_ALG_TYPE_MASK
;
887 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
888 mask
|= CRYPTO_ALG_TYPE_MASK
;
890 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name
, type
, mask
));
893 static inline struct crypto_tfm
*crypto_blkcipher_tfm(
894 struct crypto_blkcipher
*tfm
)
899 static inline void crypto_free_blkcipher(struct crypto_blkcipher
*tfm
)
901 crypto_free_tfm(crypto_blkcipher_tfm(tfm
));
904 static inline int crypto_has_blkcipher(const char *alg_name
, u32 type
, u32 mask
)
906 type
&= ~CRYPTO_ALG_TYPE_MASK
;
907 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
908 mask
|= CRYPTO_ALG_TYPE_MASK
;
910 return crypto_has_alg(alg_name
, type
, mask
);
913 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher
*tfm
)
915 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm
));
918 static inline struct blkcipher_tfm
*crypto_blkcipher_crt(
919 struct crypto_blkcipher
*tfm
)
921 return &crypto_blkcipher_tfm(tfm
)->crt_blkcipher
;
924 static inline struct blkcipher_alg
*crypto_blkcipher_alg(
925 struct crypto_blkcipher
*tfm
)
927 return &crypto_blkcipher_tfm(tfm
)->__crt_alg
->cra_blkcipher
;
930 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher
*tfm
)
932 return crypto_blkcipher_alg(tfm
)->ivsize
;
935 static inline unsigned int crypto_blkcipher_blocksize(
936 struct crypto_blkcipher
*tfm
)
938 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm
));
941 static inline unsigned int crypto_blkcipher_alignmask(
942 struct crypto_blkcipher
*tfm
)
944 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm
));
947 static inline u32
crypto_blkcipher_get_flags(struct crypto_blkcipher
*tfm
)
949 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm
));
952 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher
*tfm
,
955 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm
), flags
);
958 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher
*tfm
,
961 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm
), flags
);
964 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher
*tfm
,
965 const u8
*key
, unsigned int keylen
)
967 return crypto_blkcipher_crt(tfm
)->setkey(crypto_blkcipher_tfm(tfm
),
971 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc
*desc
,
972 struct scatterlist
*dst
,
973 struct scatterlist
*src
,
976 desc
->info
= crypto_blkcipher_crt(desc
->tfm
)->iv
;
977 return crypto_blkcipher_crt(desc
->tfm
)->encrypt(desc
, dst
, src
, nbytes
);
980 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc
*desc
,
981 struct scatterlist
*dst
,
982 struct scatterlist
*src
,
985 return crypto_blkcipher_crt(desc
->tfm
)->encrypt(desc
, dst
, src
, nbytes
);
988 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc
*desc
,
989 struct scatterlist
*dst
,
990 struct scatterlist
*src
,
993 desc
->info
= crypto_blkcipher_crt(desc
->tfm
)->iv
;
994 return crypto_blkcipher_crt(desc
->tfm
)->decrypt(desc
, dst
, src
, nbytes
);
997 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc
*desc
,
998 struct scatterlist
*dst
,
999 struct scatterlist
*src
,
1000 unsigned int nbytes
)
1002 return crypto_blkcipher_crt(desc
->tfm
)->decrypt(desc
, dst
, src
, nbytes
);
1005 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher
*tfm
,
1006 const u8
*src
, unsigned int len
)
1008 memcpy(crypto_blkcipher_crt(tfm
)->iv
, src
, len
);
1011 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher
*tfm
,
1012 u8
*dst
, unsigned int len
)
1014 memcpy(dst
, crypto_blkcipher_crt(tfm
)->iv
, len
);
1017 static inline struct crypto_cipher
*__crypto_cipher_cast(struct crypto_tfm
*tfm
)
1019 return (struct crypto_cipher
*)tfm
;
1022 static inline struct crypto_cipher
*crypto_cipher_cast(struct crypto_tfm
*tfm
)
1024 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
1025 return __crypto_cipher_cast(tfm
);
1028 static inline struct crypto_cipher
*crypto_alloc_cipher(const char *alg_name
,
1031 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1032 type
|= CRYPTO_ALG_TYPE_CIPHER
;
1033 mask
|= CRYPTO_ALG_TYPE_MASK
;
1035 return __crypto_cipher_cast(crypto_alloc_base(alg_name
, type
, mask
));
1038 static inline struct crypto_tfm
*crypto_cipher_tfm(struct crypto_cipher
*tfm
)
1043 static inline void crypto_free_cipher(struct crypto_cipher
*tfm
)
1045 crypto_free_tfm(crypto_cipher_tfm(tfm
));
1048 static inline int crypto_has_cipher(const char *alg_name
, u32 type
, u32 mask
)
1050 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1051 type
|= CRYPTO_ALG_TYPE_CIPHER
;
1052 mask
|= CRYPTO_ALG_TYPE_MASK
;
1054 return crypto_has_alg(alg_name
, type
, mask
);
1057 static inline struct cipher_tfm
*crypto_cipher_crt(struct crypto_cipher
*tfm
)
1059 return &crypto_cipher_tfm(tfm
)->crt_cipher
;
1062 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher
*tfm
)
1064 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm
));
1067 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher
*tfm
)
1069 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm
));
1072 static inline u32
crypto_cipher_get_flags(struct crypto_cipher
*tfm
)
1074 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm
));
1077 static inline void crypto_cipher_set_flags(struct crypto_cipher
*tfm
,
1080 crypto_tfm_set_flags(crypto_cipher_tfm(tfm
), flags
);
1083 static inline void crypto_cipher_clear_flags(struct crypto_cipher
*tfm
,
1086 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm
), flags
);
1089 static inline int crypto_cipher_setkey(struct crypto_cipher
*tfm
,
1090 const u8
*key
, unsigned int keylen
)
1092 return crypto_cipher_crt(tfm
)->cit_setkey(crypto_cipher_tfm(tfm
),
1096 static inline void crypto_cipher_encrypt_one(struct crypto_cipher
*tfm
,
1097 u8
*dst
, const u8
*src
)
1099 crypto_cipher_crt(tfm
)->cit_encrypt_one(crypto_cipher_tfm(tfm
),
1103 static inline void crypto_cipher_decrypt_one(struct crypto_cipher
*tfm
,
1104 u8
*dst
, const u8
*src
)
1106 crypto_cipher_crt(tfm
)->cit_decrypt_one(crypto_cipher_tfm(tfm
),
1110 static inline struct crypto_hash
*__crypto_hash_cast(struct crypto_tfm
*tfm
)
1112 return (struct crypto_hash
*)tfm
;
1115 static inline struct crypto_hash
*crypto_hash_cast(struct crypto_tfm
*tfm
)
1117 BUG_ON((crypto_tfm_alg_type(tfm
) ^ CRYPTO_ALG_TYPE_HASH
) &
1118 CRYPTO_ALG_TYPE_HASH_MASK
);
1119 return __crypto_hash_cast(tfm
);
1122 static inline struct crypto_hash
*crypto_alloc_hash(const char *alg_name
,
1125 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1126 mask
&= ~CRYPTO_ALG_TYPE_MASK
;
1127 type
|= CRYPTO_ALG_TYPE_HASH
;
1128 mask
|= CRYPTO_ALG_TYPE_HASH_MASK
;
1130 return __crypto_hash_cast(crypto_alloc_base(alg_name
, type
, mask
));
1133 static inline struct crypto_tfm
*crypto_hash_tfm(struct crypto_hash
*tfm
)
1138 static inline void crypto_free_hash(struct crypto_hash
*tfm
)
1140 crypto_free_tfm(crypto_hash_tfm(tfm
));
1143 static inline int crypto_has_hash(const char *alg_name
, u32 type
, u32 mask
)
1145 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1146 mask
&= ~CRYPTO_ALG_TYPE_MASK
;
1147 type
|= CRYPTO_ALG_TYPE_HASH
;
1148 mask
|= CRYPTO_ALG_TYPE_HASH_MASK
;
1150 return crypto_has_alg(alg_name
, type
, mask
);
1153 static inline struct hash_tfm
*crypto_hash_crt(struct crypto_hash
*tfm
)
1155 return &crypto_hash_tfm(tfm
)->crt_hash
;
1158 static inline unsigned int crypto_hash_blocksize(struct crypto_hash
*tfm
)
1160 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm
));
1163 static inline unsigned int crypto_hash_alignmask(struct crypto_hash
*tfm
)
1165 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm
));
1168 static inline unsigned int crypto_hash_digestsize(struct crypto_hash
*tfm
)
1170 return crypto_hash_crt(tfm
)->digestsize
;
1173 static inline u32
crypto_hash_get_flags(struct crypto_hash
*tfm
)
1175 return crypto_tfm_get_flags(crypto_hash_tfm(tfm
));
1178 static inline void crypto_hash_set_flags(struct crypto_hash
*tfm
, u32 flags
)
1180 crypto_tfm_set_flags(crypto_hash_tfm(tfm
), flags
);
1183 static inline void crypto_hash_clear_flags(struct crypto_hash
*tfm
, u32 flags
)
1185 crypto_tfm_clear_flags(crypto_hash_tfm(tfm
), flags
);
1188 static inline int crypto_hash_init(struct hash_desc
*desc
)
1190 return crypto_hash_crt(desc
->tfm
)->init(desc
);
1193 static inline int crypto_hash_update(struct hash_desc
*desc
,
1194 struct scatterlist
*sg
,
1195 unsigned int nbytes
)
1197 return crypto_hash_crt(desc
->tfm
)->update(desc
, sg
, nbytes
);
1200 static inline int crypto_hash_final(struct hash_desc
*desc
, u8
*out
)
1202 return crypto_hash_crt(desc
->tfm
)->final(desc
, out
);
1205 static inline int crypto_hash_digest(struct hash_desc
*desc
,
1206 struct scatterlist
*sg
,
1207 unsigned int nbytes
, u8
*out
)
1209 return crypto_hash_crt(desc
->tfm
)->digest(desc
, sg
, nbytes
, out
);
1212 static inline int crypto_hash_setkey(struct crypto_hash
*hash
,
1213 const u8
*key
, unsigned int keylen
)
1215 return crypto_hash_crt(hash
)->setkey(hash
, key
, keylen
);
1218 static inline struct crypto_comp
*__crypto_comp_cast(struct crypto_tfm
*tfm
)
1220 return (struct crypto_comp
*)tfm
;
1223 static inline struct crypto_comp
*crypto_comp_cast(struct crypto_tfm
*tfm
)
1225 BUG_ON((crypto_tfm_alg_type(tfm
) ^ CRYPTO_ALG_TYPE_COMPRESS
) &
1226 CRYPTO_ALG_TYPE_MASK
);
1227 return __crypto_comp_cast(tfm
);
1230 static inline struct crypto_comp
*crypto_alloc_comp(const char *alg_name
,
1233 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1234 type
|= CRYPTO_ALG_TYPE_COMPRESS
;
1235 mask
|= CRYPTO_ALG_TYPE_MASK
;
1237 return __crypto_comp_cast(crypto_alloc_base(alg_name
, type
, mask
));
1240 static inline struct crypto_tfm
*crypto_comp_tfm(struct crypto_comp
*tfm
)
1245 static inline void crypto_free_comp(struct crypto_comp
*tfm
)
1247 crypto_free_tfm(crypto_comp_tfm(tfm
));
1250 static inline int crypto_has_comp(const char *alg_name
, u32 type
, u32 mask
)
1252 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1253 type
|= CRYPTO_ALG_TYPE_COMPRESS
;
1254 mask
|= CRYPTO_ALG_TYPE_MASK
;
1256 return crypto_has_alg(alg_name
, type
, mask
);
1259 static inline const char *crypto_comp_name(struct crypto_comp
*tfm
)
1261 return crypto_tfm_alg_name(crypto_comp_tfm(tfm
));
1264 static inline struct compress_tfm
*crypto_comp_crt(struct crypto_comp
*tfm
)
1266 return &crypto_comp_tfm(tfm
)->crt_compress
;
1269 static inline int crypto_comp_compress(struct crypto_comp
*tfm
,
1270 const u8
*src
, unsigned int slen
,
1271 u8
*dst
, unsigned int *dlen
)
1273 return crypto_comp_crt(tfm
)->cot_compress(crypto_comp_tfm(tfm
),
1274 src
, slen
, dst
, dlen
);
1277 static inline int crypto_comp_decompress(struct crypto_comp
*tfm
,
1278 const u8
*src
, unsigned int slen
,
1279 u8
*dst
, unsigned int *dlen
)
1281 return crypto_comp_crt(tfm
)->cot_decompress(crypto_comp_tfm(tfm
),
1282 src
, slen
, dst
, dlen
);
1285 #endif /* _LINUX_CRYPTO_H */