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 <asm/atomic.h>
21 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
26 #include <linux/uaccess.h>
29 * Algorithm masks and types.
31 #define CRYPTO_ALG_TYPE_MASK 0x0000000f
32 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
33 #define CRYPTO_ALG_TYPE_DIGEST 0x00000002
34 #define CRYPTO_ALG_TYPE_HASH 0x00000003
35 #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004
36 #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005
37 #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006
38 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000008
39 #define CRYPTO_ALG_TYPE_AEAD 0x00000009
41 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
42 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
44 #define CRYPTO_ALG_LARVAL 0x00000010
45 #define CRYPTO_ALG_DEAD 0x00000020
46 #define CRYPTO_ALG_DYING 0x00000040
47 #define CRYPTO_ALG_ASYNC 0x00000080
50 * Set this bit if and only if the algorithm requires another algorithm of
51 * the same type to handle corner cases.
53 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
56 * Transform masks and values (for crt_flags).
58 #define CRYPTO_TFM_REQ_MASK 0x000fff00
59 #define CRYPTO_TFM_RES_MASK 0xfff00000
61 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
62 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
63 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
64 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
65 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
66 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
67 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
68 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
71 * Miscellaneous stuff.
73 #define CRYPTO_MAX_ALG_NAME 64
76 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
77 * declaration) is used to ensure that the crypto_tfm context structure is
78 * aligned correctly for the given architecture so that there are no alignment
79 * faults for C data types. In particular, this is required on platforms such
80 * as arm where pointers are 32-bit aligned but there are data types such as
81 * u64 which require 64-bit alignment.
83 #if defined(ARCH_KMALLOC_MINALIGN)
84 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
85 #elif defined(ARCH_SLAB_MINALIGN)
86 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
89 #ifdef CRYPTO_MINALIGN
90 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
92 #define CRYPTO_MINALIGN_ATTR
96 struct crypto_ablkcipher
;
97 struct crypto_async_request
;
99 struct crypto_blkcipher
;
103 struct skcipher_givcrypt_request
;
105 typedef void (*crypto_completion_t
)(struct crypto_async_request
*req
, int err
);
107 struct crypto_async_request
{
108 struct list_head list
;
109 crypto_completion_t complete
;
111 struct crypto_tfm
*tfm
;
116 struct ablkcipher_request
{
117 struct crypto_async_request base
;
123 struct scatterlist
*src
;
124 struct scatterlist
*dst
;
126 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
130 * struct aead_request - AEAD request
131 * @base: Common attributes for async crypto requests
132 * @assoclen: Length in bytes of associated data for authentication
133 * @cryptlen: Length of data to be encrypted or decrypted
134 * @iv: Initialisation vector
135 * @assoc: Associated data
137 * @dst: Destination data
138 * @__ctx: Start of private context data
140 struct aead_request
{
141 struct crypto_async_request base
;
143 unsigned int assoclen
;
144 unsigned int cryptlen
;
148 struct scatterlist
*assoc
;
149 struct scatterlist
*src
;
150 struct scatterlist
*dst
;
152 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
155 struct blkcipher_desc
{
156 struct crypto_blkcipher
*tfm
;
162 struct crypto_tfm
*tfm
;
163 void (*crfn
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
164 unsigned int (*prfn
)(const struct cipher_desc
*desc
, u8
*dst
,
165 const u8
*src
, unsigned int nbytes
);
170 struct crypto_hash
*tfm
;
175 * Algorithms: modular crypto algorithm implementations, managed
176 * via crypto_register_alg() and crypto_unregister_alg().
178 struct ablkcipher_alg
{
179 int (*setkey
)(struct crypto_ablkcipher
*tfm
, const u8
*key
,
180 unsigned int keylen
);
181 int (*encrypt
)(struct ablkcipher_request
*req
);
182 int (*decrypt
)(struct ablkcipher_request
*req
);
183 int (*givencrypt
)(struct skcipher_givcrypt_request
*req
);
184 int (*givdecrypt
)(struct skcipher_givcrypt_request
*req
);
186 unsigned int min_keysize
;
187 unsigned int max_keysize
;
192 int (*setkey
)(struct crypto_aead
*tfm
, const u8
*key
,
193 unsigned int keylen
);
194 int (*setauthsize
)(struct crypto_aead
*tfm
, unsigned int authsize
);
195 int (*encrypt
)(struct aead_request
*req
);
196 int (*decrypt
)(struct aead_request
*req
);
199 unsigned int maxauthsize
;
202 struct blkcipher_alg
{
203 int (*setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
204 unsigned int keylen
);
205 int (*encrypt
)(struct blkcipher_desc
*desc
,
206 struct scatterlist
*dst
, struct scatterlist
*src
,
207 unsigned int nbytes
);
208 int (*decrypt
)(struct blkcipher_desc
*desc
,
209 struct scatterlist
*dst
, struct scatterlist
*src
,
210 unsigned int nbytes
);
212 unsigned int min_keysize
;
213 unsigned int max_keysize
;
218 unsigned int cia_min_keysize
;
219 unsigned int cia_max_keysize
;
220 int (*cia_setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
221 unsigned int keylen
);
222 void (*cia_encrypt
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
223 void (*cia_decrypt
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
227 unsigned int dia_digestsize
;
228 void (*dia_init
)(struct crypto_tfm
*tfm
);
229 void (*dia_update
)(struct crypto_tfm
*tfm
, const u8
*data
,
231 void (*dia_final
)(struct crypto_tfm
*tfm
, u8
*out
);
232 int (*dia_setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
233 unsigned int keylen
);
237 int (*init
)(struct hash_desc
*desc
);
238 int (*update
)(struct hash_desc
*desc
, struct scatterlist
*sg
,
239 unsigned int nbytes
);
240 int (*final
)(struct hash_desc
*desc
, u8
*out
);
241 int (*digest
)(struct hash_desc
*desc
, struct scatterlist
*sg
,
242 unsigned int nbytes
, u8
*out
);
243 int (*setkey
)(struct crypto_hash
*tfm
, const u8
*key
,
244 unsigned int keylen
);
246 unsigned int digestsize
;
249 struct compress_alg
{
250 int (*coa_compress
)(struct crypto_tfm
*tfm
, const u8
*src
,
251 unsigned int slen
, u8
*dst
, unsigned int *dlen
);
252 int (*coa_decompress
)(struct crypto_tfm
*tfm
, const u8
*src
,
253 unsigned int slen
, u8
*dst
, unsigned int *dlen
);
256 #define cra_ablkcipher cra_u.ablkcipher
257 #define cra_aead cra_u.aead
258 #define cra_blkcipher cra_u.blkcipher
259 #define cra_cipher cra_u.cipher
260 #define cra_digest cra_u.digest
261 #define cra_hash cra_u.hash
262 #define cra_compress cra_u.compress
265 struct list_head cra_list
;
266 struct list_head cra_users
;
269 unsigned int cra_blocksize
;
270 unsigned int cra_ctxsize
;
271 unsigned int cra_alignmask
;
276 char cra_name
[CRYPTO_MAX_ALG_NAME
];
277 char cra_driver_name
[CRYPTO_MAX_ALG_NAME
];
279 const struct crypto_type
*cra_type
;
282 struct ablkcipher_alg ablkcipher
;
283 struct aead_alg aead
;
284 struct blkcipher_alg blkcipher
;
285 struct cipher_alg cipher
;
286 struct digest_alg digest
;
287 struct hash_alg hash
;
288 struct compress_alg compress
;
291 int (*cra_init
)(struct crypto_tfm
*tfm
);
292 void (*cra_exit
)(struct crypto_tfm
*tfm
);
293 void (*cra_destroy
)(struct crypto_alg
*alg
);
295 struct module
*cra_module
;
299 * Algorithm registration interface.
301 int crypto_register_alg(struct crypto_alg
*alg
);
302 int crypto_unregister_alg(struct crypto_alg
*alg
);
305 * Algorithm query interface.
308 int crypto_has_alg(const char *name
, u32 type
, u32 mask
);
310 static inline int crypto_has_alg(const char *name
, u32 type
, u32 mask
)
317 * Transforms: user-instantiated objects which encapsulate algorithms
318 * and core processing logic. Managed via crypto_alloc_*() and
319 * crypto_free_*(), as well as the various helpers below.
322 struct ablkcipher_tfm
{
323 int (*setkey
)(struct crypto_ablkcipher
*tfm
, const u8
*key
,
324 unsigned int keylen
);
325 int (*encrypt
)(struct ablkcipher_request
*req
);
326 int (*decrypt
)(struct ablkcipher_request
*req
);
327 int (*givencrypt
)(struct skcipher_givcrypt_request
*req
);
328 int (*givdecrypt
)(struct skcipher_givcrypt_request
*req
);
331 unsigned int reqsize
;
335 int (*setkey
)(struct crypto_aead
*tfm
, const u8
*key
,
336 unsigned int keylen
);
337 int (*encrypt
)(struct aead_request
*req
);
338 int (*decrypt
)(struct aead_request
*req
);
340 unsigned int authsize
;
341 unsigned int reqsize
;
344 struct blkcipher_tfm
{
346 int (*setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
347 unsigned int keylen
);
348 int (*encrypt
)(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
349 struct scatterlist
*src
, unsigned int nbytes
);
350 int (*decrypt
)(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
351 struct scatterlist
*src
, unsigned int nbytes
);
355 int (*cit_setkey
)(struct crypto_tfm
*tfm
,
356 const u8
*key
, unsigned int keylen
);
357 void (*cit_encrypt_one
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
358 void (*cit_decrypt_one
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
362 int (*init
)(struct hash_desc
*desc
);
363 int (*update
)(struct hash_desc
*desc
,
364 struct scatterlist
*sg
, unsigned int nsg
);
365 int (*final
)(struct hash_desc
*desc
, u8
*out
);
366 int (*digest
)(struct hash_desc
*desc
, struct scatterlist
*sg
,
367 unsigned int nsg
, u8
*out
);
368 int (*setkey
)(struct crypto_hash
*tfm
, const u8
*key
,
369 unsigned int keylen
);
370 unsigned int digestsize
;
373 struct compress_tfm
{
374 int (*cot_compress
)(struct crypto_tfm
*tfm
,
375 const u8
*src
, unsigned int slen
,
376 u8
*dst
, unsigned int *dlen
);
377 int (*cot_decompress
)(struct crypto_tfm
*tfm
,
378 const u8
*src
, unsigned int slen
,
379 u8
*dst
, unsigned int *dlen
);
382 #define crt_ablkcipher crt_u.ablkcipher
383 #define crt_aead crt_u.aead
384 #define crt_blkcipher crt_u.blkcipher
385 #define crt_cipher crt_u.cipher
386 #define crt_hash crt_u.hash
387 #define crt_compress crt_u.compress
394 struct ablkcipher_tfm ablkcipher
;
395 struct aead_tfm aead
;
396 struct blkcipher_tfm blkcipher
;
397 struct cipher_tfm cipher
;
398 struct hash_tfm hash
;
399 struct compress_tfm compress
;
402 struct crypto_alg
*__crt_alg
;
404 void *__crt_ctx
[] CRYPTO_MINALIGN_ATTR
;
407 struct crypto_ablkcipher
{
408 struct crypto_tfm base
;
412 struct crypto_tfm base
;
415 struct crypto_blkcipher
{
416 struct crypto_tfm base
;
419 struct crypto_cipher
{
420 struct crypto_tfm base
;
424 struct crypto_tfm base
;
428 struct crypto_tfm base
;
439 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
441 /* Maximum number of (rtattr) parameters for each template. */
442 #define CRYPTO_MAX_ATTRS 32
444 struct crypto_attr_alg
{
445 char name
[CRYPTO_MAX_ALG_NAME
];
448 struct crypto_attr_type
{
453 struct crypto_attr_u32
{
458 * Transform user interface.
461 struct crypto_tfm
*crypto_alloc_tfm(const char *alg_name
, u32 tfm_flags
);
462 struct crypto_tfm
*crypto_alloc_base(const char *alg_name
, u32 type
, u32 mask
);
463 void crypto_free_tfm(struct crypto_tfm
*tfm
);
466 * Transform helpers which query the underlying algorithm.
468 static inline const char *crypto_tfm_alg_name(struct crypto_tfm
*tfm
)
470 return tfm
->__crt_alg
->cra_name
;
473 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm
*tfm
)
475 return tfm
->__crt_alg
->cra_driver_name
;
478 static inline int crypto_tfm_alg_priority(struct crypto_tfm
*tfm
)
480 return tfm
->__crt_alg
->cra_priority
;
483 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm
*tfm
)
485 return module_name(tfm
->__crt_alg
->cra_module
);
488 static inline u32
crypto_tfm_alg_type(struct crypto_tfm
*tfm
)
490 return tfm
->__crt_alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
;
493 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm
*tfm
)
495 return tfm
->__crt_alg
->cra_blocksize
;
498 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm
*tfm
)
500 return tfm
->__crt_alg
->cra_alignmask
;
503 static inline u32
crypto_tfm_get_flags(struct crypto_tfm
*tfm
)
505 return tfm
->crt_flags
;
508 static inline void crypto_tfm_set_flags(struct crypto_tfm
*tfm
, u32 flags
)
510 tfm
->crt_flags
|= flags
;
513 static inline void crypto_tfm_clear_flags(struct crypto_tfm
*tfm
, u32 flags
)
515 tfm
->crt_flags
&= ~flags
;
518 static inline void *crypto_tfm_ctx(struct crypto_tfm
*tfm
)
520 return tfm
->__crt_ctx
;
523 static inline unsigned int crypto_tfm_ctx_alignment(void)
525 struct crypto_tfm
*tfm
;
526 return __alignof__(tfm
->__crt_ctx
);
532 static inline struct crypto_ablkcipher
*__crypto_ablkcipher_cast(
533 struct crypto_tfm
*tfm
)
535 return (struct crypto_ablkcipher
*)tfm
;
538 static inline u32
crypto_skcipher_type(u32 type
)
540 type
&= ~CRYPTO_ALG_TYPE_MASK
;
541 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
545 static inline u32
crypto_skcipher_mask(u32 mask
)
547 mask
&= ~CRYPTO_ALG_TYPE_MASK
;
548 mask
|= CRYPTO_ALG_TYPE_BLKCIPHER_MASK
;
552 static inline struct crypto_ablkcipher
*crypto_alloc_ablkcipher(
553 const char *alg_name
, u32 type
, u32 mask
)
555 return __crypto_ablkcipher_cast(
556 crypto_alloc_base(alg_name
, crypto_skcipher_type(type
),
557 crypto_skcipher_mask(mask
)));
560 static inline struct crypto_tfm
*crypto_ablkcipher_tfm(
561 struct crypto_ablkcipher
*tfm
)
566 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher
*tfm
)
568 crypto_free_tfm(crypto_ablkcipher_tfm(tfm
));
571 static inline int crypto_has_ablkcipher(const char *alg_name
, u32 type
,
574 return crypto_has_alg(alg_name
, crypto_skcipher_type(type
),
575 crypto_skcipher_mask(mask
));
578 static inline struct ablkcipher_tfm
*crypto_ablkcipher_crt(
579 struct crypto_ablkcipher
*tfm
)
581 return &crypto_ablkcipher_tfm(tfm
)->crt_ablkcipher
;
584 static inline unsigned int crypto_ablkcipher_ivsize(
585 struct crypto_ablkcipher
*tfm
)
587 return crypto_ablkcipher_crt(tfm
)->ivsize
;
590 static inline unsigned int crypto_ablkcipher_blocksize(
591 struct crypto_ablkcipher
*tfm
)
593 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm
));
596 static inline unsigned int crypto_ablkcipher_alignmask(
597 struct crypto_ablkcipher
*tfm
)
599 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm
));
602 static inline u32
crypto_ablkcipher_get_flags(struct crypto_ablkcipher
*tfm
)
604 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm
));
607 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher
*tfm
,
610 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm
), flags
);
613 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher
*tfm
,
616 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm
), flags
);
619 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher
*tfm
,
620 const u8
*key
, unsigned int keylen
)
622 return crypto_ablkcipher_crt(tfm
)->setkey(tfm
, key
, keylen
);
625 static inline struct crypto_ablkcipher
*crypto_ablkcipher_reqtfm(
626 struct ablkcipher_request
*req
)
628 return __crypto_ablkcipher_cast(req
->base
.tfm
);
631 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request
*req
)
633 struct ablkcipher_tfm
*crt
=
634 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req
));
635 return crt
->encrypt(req
);
638 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request
*req
)
640 struct ablkcipher_tfm
*crt
=
641 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req
));
642 return crt
->decrypt(req
);
645 static inline unsigned int crypto_ablkcipher_reqsize(
646 struct crypto_ablkcipher
*tfm
)
648 return crypto_ablkcipher_crt(tfm
)->reqsize
;
651 static inline void ablkcipher_request_set_tfm(
652 struct ablkcipher_request
*req
, struct crypto_ablkcipher
*tfm
)
654 req
->base
.tfm
= crypto_ablkcipher_tfm(tfm
);
657 static inline struct ablkcipher_request
*ablkcipher_request_cast(
658 struct crypto_async_request
*req
)
660 return container_of(req
, struct ablkcipher_request
, base
);
663 static inline struct ablkcipher_request
*ablkcipher_request_alloc(
664 struct crypto_ablkcipher
*tfm
, gfp_t gfp
)
666 struct ablkcipher_request
*req
;
668 req
= kmalloc(sizeof(struct ablkcipher_request
) +
669 crypto_ablkcipher_reqsize(tfm
), gfp
);
672 ablkcipher_request_set_tfm(req
, tfm
);
677 static inline void ablkcipher_request_free(struct ablkcipher_request
*req
)
682 static inline void ablkcipher_request_set_callback(
683 struct ablkcipher_request
*req
,
684 u32 flags
, crypto_completion_t complete
, void *data
)
686 req
->base
.complete
= complete
;
687 req
->base
.data
= data
;
688 req
->base
.flags
= flags
;
691 static inline void ablkcipher_request_set_crypt(
692 struct ablkcipher_request
*req
,
693 struct scatterlist
*src
, struct scatterlist
*dst
,
694 unsigned int nbytes
, void *iv
)
698 req
->nbytes
= nbytes
;
702 static inline struct crypto_aead
*__crypto_aead_cast(struct crypto_tfm
*tfm
)
704 return (struct crypto_aead
*)tfm
;
707 static inline struct crypto_aead
*crypto_alloc_aead(const char *alg_name
,
710 type
&= ~CRYPTO_ALG_TYPE_MASK
;
711 type
|= CRYPTO_ALG_TYPE_AEAD
;
712 mask
|= CRYPTO_ALG_TYPE_MASK
;
714 return __crypto_aead_cast(crypto_alloc_base(alg_name
, type
, mask
));
717 static inline struct crypto_tfm
*crypto_aead_tfm(struct crypto_aead
*tfm
)
722 static inline void crypto_free_aead(struct crypto_aead
*tfm
)
724 crypto_free_tfm(crypto_aead_tfm(tfm
));
727 static inline struct aead_tfm
*crypto_aead_crt(struct crypto_aead
*tfm
)
729 return &crypto_aead_tfm(tfm
)->crt_aead
;
732 static inline unsigned int crypto_aead_ivsize(struct crypto_aead
*tfm
)
734 return crypto_aead_crt(tfm
)->ivsize
;
737 static inline unsigned int crypto_aead_authsize(struct crypto_aead
*tfm
)
739 return crypto_aead_crt(tfm
)->authsize
;
742 static inline unsigned int crypto_aead_blocksize(struct crypto_aead
*tfm
)
744 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm
));
747 static inline unsigned int crypto_aead_alignmask(struct crypto_aead
*tfm
)
749 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm
));
752 static inline u32
crypto_aead_get_flags(struct crypto_aead
*tfm
)
754 return crypto_tfm_get_flags(crypto_aead_tfm(tfm
));
757 static inline void crypto_aead_set_flags(struct crypto_aead
*tfm
, u32 flags
)
759 crypto_tfm_set_flags(crypto_aead_tfm(tfm
), flags
);
762 static inline void crypto_aead_clear_flags(struct crypto_aead
*tfm
, u32 flags
)
764 crypto_tfm_clear_flags(crypto_aead_tfm(tfm
), flags
);
767 static inline int crypto_aead_setkey(struct crypto_aead
*tfm
, const u8
*key
,
770 return crypto_aead_crt(tfm
)->setkey(tfm
, key
, keylen
);
773 int crypto_aead_setauthsize(struct crypto_aead
*tfm
, unsigned int authsize
);
775 static inline struct crypto_aead
*crypto_aead_reqtfm(struct aead_request
*req
)
777 return __crypto_aead_cast(req
->base
.tfm
);
780 static inline int crypto_aead_encrypt(struct aead_request
*req
)
782 return crypto_aead_crt(crypto_aead_reqtfm(req
))->encrypt(req
);
785 static inline int crypto_aead_decrypt(struct aead_request
*req
)
787 return crypto_aead_crt(crypto_aead_reqtfm(req
))->decrypt(req
);
790 static inline unsigned int crypto_aead_reqsize(struct crypto_aead
*tfm
)
792 return crypto_aead_crt(tfm
)->reqsize
;
795 static inline void aead_request_set_tfm(struct aead_request
*req
,
796 struct crypto_aead
*tfm
)
798 req
->base
.tfm
= crypto_aead_tfm(tfm
);
801 static inline struct aead_request
*aead_request_alloc(struct crypto_aead
*tfm
,
804 struct aead_request
*req
;
806 req
= kmalloc(sizeof(*req
) + crypto_aead_reqsize(tfm
), gfp
);
809 aead_request_set_tfm(req
, tfm
);
814 static inline void aead_request_free(struct aead_request
*req
)
819 static inline void aead_request_set_callback(struct aead_request
*req
,
821 crypto_completion_t complete
,
824 req
->base
.complete
= complete
;
825 req
->base
.data
= data
;
826 req
->base
.flags
= flags
;
829 static inline void aead_request_set_crypt(struct aead_request
*req
,
830 struct scatterlist
*src
,
831 struct scatterlist
*dst
,
832 unsigned int cryptlen
, u8
*iv
)
836 req
->cryptlen
= cryptlen
;
840 static inline void aead_request_set_assoc(struct aead_request
*req
,
841 struct scatterlist
*assoc
,
842 unsigned int assoclen
)
845 req
->assoclen
= assoclen
;
848 static inline struct crypto_blkcipher
*__crypto_blkcipher_cast(
849 struct crypto_tfm
*tfm
)
851 return (struct crypto_blkcipher
*)tfm
;
854 static inline struct crypto_blkcipher
*crypto_blkcipher_cast(
855 struct crypto_tfm
*tfm
)
857 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_BLKCIPHER
);
858 return __crypto_blkcipher_cast(tfm
);
861 static inline struct crypto_blkcipher
*crypto_alloc_blkcipher(
862 const char *alg_name
, u32 type
, u32 mask
)
864 type
&= ~CRYPTO_ALG_TYPE_MASK
;
865 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
866 mask
|= CRYPTO_ALG_TYPE_MASK
;
868 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name
, type
, mask
));
871 static inline struct crypto_tfm
*crypto_blkcipher_tfm(
872 struct crypto_blkcipher
*tfm
)
877 static inline void crypto_free_blkcipher(struct crypto_blkcipher
*tfm
)
879 crypto_free_tfm(crypto_blkcipher_tfm(tfm
));
882 static inline int crypto_has_blkcipher(const char *alg_name
, u32 type
, u32 mask
)
884 type
&= ~CRYPTO_ALG_TYPE_MASK
;
885 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
886 mask
|= CRYPTO_ALG_TYPE_MASK
;
888 return crypto_has_alg(alg_name
, type
, mask
);
891 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher
*tfm
)
893 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm
));
896 static inline struct blkcipher_tfm
*crypto_blkcipher_crt(
897 struct crypto_blkcipher
*tfm
)
899 return &crypto_blkcipher_tfm(tfm
)->crt_blkcipher
;
902 static inline struct blkcipher_alg
*crypto_blkcipher_alg(
903 struct crypto_blkcipher
*tfm
)
905 return &crypto_blkcipher_tfm(tfm
)->__crt_alg
->cra_blkcipher
;
908 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher
*tfm
)
910 return crypto_blkcipher_alg(tfm
)->ivsize
;
913 static inline unsigned int crypto_blkcipher_blocksize(
914 struct crypto_blkcipher
*tfm
)
916 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm
));
919 static inline unsigned int crypto_blkcipher_alignmask(
920 struct crypto_blkcipher
*tfm
)
922 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm
));
925 static inline u32
crypto_blkcipher_get_flags(struct crypto_blkcipher
*tfm
)
927 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm
));
930 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher
*tfm
,
933 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm
), flags
);
936 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher
*tfm
,
939 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm
), flags
);
942 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher
*tfm
,
943 const u8
*key
, unsigned int keylen
)
945 return crypto_blkcipher_crt(tfm
)->setkey(crypto_blkcipher_tfm(tfm
),
949 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc
*desc
,
950 struct scatterlist
*dst
,
951 struct scatterlist
*src
,
954 desc
->info
= crypto_blkcipher_crt(desc
->tfm
)->iv
;
955 return crypto_blkcipher_crt(desc
->tfm
)->encrypt(desc
, dst
, src
, nbytes
);
958 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc
*desc
,
959 struct scatterlist
*dst
,
960 struct scatterlist
*src
,
963 return crypto_blkcipher_crt(desc
->tfm
)->encrypt(desc
, dst
, src
, nbytes
);
966 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc
*desc
,
967 struct scatterlist
*dst
,
968 struct scatterlist
*src
,
971 desc
->info
= crypto_blkcipher_crt(desc
->tfm
)->iv
;
972 return crypto_blkcipher_crt(desc
->tfm
)->decrypt(desc
, dst
, src
, nbytes
);
975 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc
*desc
,
976 struct scatterlist
*dst
,
977 struct scatterlist
*src
,
980 return crypto_blkcipher_crt(desc
->tfm
)->decrypt(desc
, dst
, src
, nbytes
);
983 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher
*tfm
,
984 const u8
*src
, unsigned int len
)
986 memcpy(crypto_blkcipher_crt(tfm
)->iv
, src
, len
);
989 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher
*tfm
,
990 u8
*dst
, unsigned int len
)
992 memcpy(dst
, crypto_blkcipher_crt(tfm
)->iv
, len
);
995 static inline struct crypto_cipher
*__crypto_cipher_cast(struct crypto_tfm
*tfm
)
997 return (struct crypto_cipher
*)tfm
;
1000 static inline struct crypto_cipher
*crypto_cipher_cast(struct crypto_tfm
*tfm
)
1002 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
1003 return __crypto_cipher_cast(tfm
);
1006 static inline struct crypto_cipher
*crypto_alloc_cipher(const char *alg_name
,
1009 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1010 type
|= CRYPTO_ALG_TYPE_CIPHER
;
1011 mask
|= CRYPTO_ALG_TYPE_MASK
;
1013 return __crypto_cipher_cast(crypto_alloc_base(alg_name
, type
, mask
));
1016 static inline struct crypto_tfm
*crypto_cipher_tfm(struct crypto_cipher
*tfm
)
1021 static inline void crypto_free_cipher(struct crypto_cipher
*tfm
)
1023 crypto_free_tfm(crypto_cipher_tfm(tfm
));
1026 static inline int crypto_has_cipher(const char *alg_name
, u32 type
, u32 mask
)
1028 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1029 type
|= CRYPTO_ALG_TYPE_CIPHER
;
1030 mask
|= CRYPTO_ALG_TYPE_MASK
;
1032 return crypto_has_alg(alg_name
, type
, mask
);
1035 static inline struct cipher_tfm
*crypto_cipher_crt(struct crypto_cipher
*tfm
)
1037 return &crypto_cipher_tfm(tfm
)->crt_cipher
;
1040 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher
*tfm
)
1042 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm
));
1045 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher
*tfm
)
1047 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm
));
1050 static inline u32
crypto_cipher_get_flags(struct crypto_cipher
*tfm
)
1052 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm
));
1055 static inline void crypto_cipher_set_flags(struct crypto_cipher
*tfm
,
1058 crypto_tfm_set_flags(crypto_cipher_tfm(tfm
), flags
);
1061 static inline void crypto_cipher_clear_flags(struct crypto_cipher
*tfm
,
1064 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm
), flags
);
1067 static inline int crypto_cipher_setkey(struct crypto_cipher
*tfm
,
1068 const u8
*key
, unsigned int keylen
)
1070 return crypto_cipher_crt(tfm
)->cit_setkey(crypto_cipher_tfm(tfm
),
1074 static inline void crypto_cipher_encrypt_one(struct crypto_cipher
*tfm
,
1075 u8
*dst
, const u8
*src
)
1077 crypto_cipher_crt(tfm
)->cit_encrypt_one(crypto_cipher_tfm(tfm
),
1081 static inline void crypto_cipher_decrypt_one(struct crypto_cipher
*tfm
,
1082 u8
*dst
, const u8
*src
)
1084 crypto_cipher_crt(tfm
)->cit_decrypt_one(crypto_cipher_tfm(tfm
),
1088 static inline struct crypto_hash
*__crypto_hash_cast(struct crypto_tfm
*tfm
)
1090 return (struct crypto_hash
*)tfm
;
1093 static inline struct crypto_hash
*crypto_hash_cast(struct crypto_tfm
*tfm
)
1095 BUG_ON((crypto_tfm_alg_type(tfm
) ^ CRYPTO_ALG_TYPE_HASH
) &
1096 CRYPTO_ALG_TYPE_HASH_MASK
);
1097 return __crypto_hash_cast(tfm
);
1100 static inline struct crypto_hash
*crypto_alloc_hash(const char *alg_name
,
1103 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1104 mask
&= ~CRYPTO_ALG_TYPE_MASK
;
1105 type
|= CRYPTO_ALG_TYPE_HASH
;
1106 mask
|= CRYPTO_ALG_TYPE_HASH_MASK
;
1108 return __crypto_hash_cast(crypto_alloc_base(alg_name
, type
, mask
));
1111 static inline struct crypto_tfm
*crypto_hash_tfm(struct crypto_hash
*tfm
)
1116 static inline void crypto_free_hash(struct crypto_hash
*tfm
)
1118 crypto_free_tfm(crypto_hash_tfm(tfm
));
1121 static inline int crypto_has_hash(const char *alg_name
, u32 type
, u32 mask
)
1123 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1124 mask
&= ~CRYPTO_ALG_TYPE_MASK
;
1125 type
|= CRYPTO_ALG_TYPE_HASH
;
1126 mask
|= CRYPTO_ALG_TYPE_HASH_MASK
;
1128 return crypto_has_alg(alg_name
, type
, mask
);
1131 static inline struct hash_tfm
*crypto_hash_crt(struct crypto_hash
*tfm
)
1133 return &crypto_hash_tfm(tfm
)->crt_hash
;
1136 static inline unsigned int crypto_hash_blocksize(struct crypto_hash
*tfm
)
1138 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm
));
1141 static inline unsigned int crypto_hash_alignmask(struct crypto_hash
*tfm
)
1143 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm
));
1146 static inline unsigned int crypto_hash_digestsize(struct crypto_hash
*tfm
)
1148 return crypto_hash_crt(tfm
)->digestsize
;
1151 static inline u32
crypto_hash_get_flags(struct crypto_hash
*tfm
)
1153 return crypto_tfm_get_flags(crypto_hash_tfm(tfm
));
1156 static inline void crypto_hash_set_flags(struct crypto_hash
*tfm
, u32 flags
)
1158 crypto_tfm_set_flags(crypto_hash_tfm(tfm
), flags
);
1161 static inline void crypto_hash_clear_flags(struct crypto_hash
*tfm
, u32 flags
)
1163 crypto_tfm_clear_flags(crypto_hash_tfm(tfm
), flags
);
1166 static inline int crypto_hash_init(struct hash_desc
*desc
)
1168 return crypto_hash_crt(desc
->tfm
)->init(desc
);
1171 static inline int crypto_hash_update(struct hash_desc
*desc
,
1172 struct scatterlist
*sg
,
1173 unsigned int nbytes
)
1175 return crypto_hash_crt(desc
->tfm
)->update(desc
, sg
, nbytes
);
1178 static inline int crypto_hash_final(struct hash_desc
*desc
, u8
*out
)
1180 return crypto_hash_crt(desc
->tfm
)->final(desc
, out
);
1183 static inline int crypto_hash_digest(struct hash_desc
*desc
,
1184 struct scatterlist
*sg
,
1185 unsigned int nbytes
, u8
*out
)
1187 return crypto_hash_crt(desc
->tfm
)->digest(desc
, sg
, nbytes
, out
);
1190 static inline int crypto_hash_setkey(struct crypto_hash
*hash
,
1191 const u8
*key
, unsigned int keylen
)
1193 return crypto_hash_crt(hash
)->setkey(hash
, key
, keylen
);
1196 static inline struct crypto_comp
*__crypto_comp_cast(struct crypto_tfm
*tfm
)
1198 return (struct crypto_comp
*)tfm
;
1201 static inline struct crypto_comp
*crypto_comp_cast(struct crypto_tfm
*tfm
)
1203 BUG_ON((crypto_tfm_alg_type(tfm
) ^ CRYPTO_ALG_TYPE_COMPRESS
) &
1204 CRYPTO_ALG_TYPE_MASK
);
1205 return __crypto_comp_cast(tfm
);
1208 static inline struct crypto_comp
*crypto_alloc_comp(const char *alg_name
,
1211 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1212 type
|= CRYPTO_ALG_TYPE_COMPRESS
;
1213 mask
|= CRYPTO_ALG_TYPE_MASK
;
1215 return __crypto_comp_cast(crypto_alloc_base(alg_name
, type
, mask
));
1218 static inline struct crypto_tfm
*crypto_comp_tfm(struct crypto_comp
*tfm
)
1223 static inline void crypto_free_comp(struct crypto_comp
*tfm
)
1225 crypto_free_tfm(crypto_comp_tfm(tfm
));
1228 static inline int crypto_has_comp(const char *alg_name
, u32 type
, u32 mask
)
1230 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1231 type
|= CRYPTO_ALG_TYPE_COMPRESS
;
1232 mask
|= CRYPTO_ALG_TYPE_MASK
;
1234 return crypto_has_alg(alg_name
, type
, mask
);
1237 static inline const char *crypto_comp_name(struct crypto_comp
*tfm
)
1239 return crypto_tfm_alg_name(crypto_comp_tfm(tfm
));
1242 static inline struct compress_tfm
*crypto_comp_crt(struct crypto_comp
*tfm
)
1244 return &crypto_comp_tfm(tfm
)->crt_compress
;
1247 static inline int crypto_comp_compress(struct crypto_comp
*tfm
,
1248 const u8
*src
, unsigned int slen
,
1249 u8
*dst
, unsigned int *dlen
)
1251 return crypto_comp_crt(tfm
)->cot_compress(crypto_comp_tfm(tfm
),
1252 src
, slen
, dst
, dlen
);
1255 static inline int crypto_comp_decompress(struct crypto_comp
*tfm
,
1256 const u8
*src
, unsigned int slen
,
1257 u8
*dst
, unsigned int *dlen
)
1259 return crypto_comp_crt(tfm
)->cot_decompress(crypto_comp_tfm(tfm
),
1260 src
, slen
, dst
, dlen
);
1263 #endif /* _LINUX_CRYPTO_H */