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_COMPRESS 0x00000002
34 #define CRYPTO_ALG_TYPE_AEAD 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_DIGEST 0x00000008
39 #define CRYPTO_ALG_TYPE_HASH 0x00000009
40 #define CRYPTO_ALG_TYPE_AHASH 0x0000000a
42 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
43 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
44 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
46 #define CRYPTO_ALG_LARVAL 0x00000010
47 #define CRYPTO_ALG_DEAD 0x00000020
48 #define CRYPTO_ALG_DYING 0x00000040
49 #define CRYPTO_ALG_ASYNC 0x00000080
52 * Set this bit if and only if the algorithm requires another algorithm of
53 * the same type to handle corner cases.
55 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
58 * This bit is set for symmetric key ciphers that have already been wrapped
59 * with a generic IV generator to prevent them from being wrapped again.
61 #define CRYPTO_ALG_GENIV 0x00000200
64 * Transform masks and values (for crt_flags).
66 #define CRYPTO_TFM_REQ_MASK 0x000fff00
67 #define CRYPTO_TFM_RES_MASK 0xfff00000
69 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
70 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
71 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
72 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
73 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
74 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
75 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
76 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
79 * Miscellaneous stuff.
81 #define CRYPTO_MAX_ALG_NAME 64
84 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
85 * declaration) is used to ensure that the crypto_tfm context structure is
86 * aligned correctly for the given architecture so that there are no alignment
87 * faults for C data types. In particular, this is required on platforms such
88 * as arm where pointers are 32-bit aligned but there are data types such as
89 * u64 which require 64-bit alignment.
91 #if defined(ARCH_KMALLOC_MINALIGN)
92 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
93 #elif defined(ARCH_SLAB_MINALIGN)
94 #define CRYPTO_MINALIGN ARCH_SLAB_MINALIGN
96 #define CRYPTO_MINALIGN __alignof__(unsigned long long)
99 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
102 struct crypto_ablkcipher
;
103 struct crypto_async_request
;
105 struct crypto_blkcipher
;
110 struct aead_givcrypt_request
;
111 struct skcipher_givcrypt_request
;
113 typedef void (*crypto_completion_t
)(struct crypto_async_request
*req
, int err
);
115 struct crypto_async_request
{
116 struct list_head list
;
117 crypto_completion_t complete
;
119 struct crypto_tfm
*tfm
;
124 struct ablkcipher_request
{
125 struct crypto_async_request base
;
131 struct scatterlist
*src
;
132 struct scatterlist
*dst
;
134 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
137 struct ahash_request
{
138 struct crypto_async_request base
;
141 struct scatterlist
*src
;
144 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
148 * struct aead_request - AEAD request
149 * @base: Common attributes for async crypto requests
150 * @assoclen: Length in bytes of associated data for authentication
151 * @cryptlen: Length of data to be encrypted or decrypted
152 * @iv: Initialisation vector
153 * @assoc: Associated data
155 * @dst: Destination data
156 * @__ctx: Start of private context data
158 struct aead_request
{
159 struct crypto_async_request base
;
161 unsigned int assoclen
;
162 unsigned int cryptlen
;
166 struct scatterlist
*assoc
;
167 struct scatterlist
*src
;
168 struct scatterlist
*dst
;
170 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
173 struct blkcipher_desc
{
174 struct crypto_blkcipher
*tfm
;
180 struct crypto_tfm
*tfm
;
181 void (*crfn
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
182 unsigned int (*prfn
)(const struct cipher_desc
*desc
, u8
*dst
,
183 const u8
*src
, unsigned int nbytes
);
188 struct crypto_hash
*tfm
;
193 * Algorithms: modular crypto algorithm implementations, managed
194 * via crypto_register_alg() and crypto_unregister_alg().
196 struct ablkcipher_alg
{
197 int (*setkey
)(struct crypto_ablkcipher
*tfm
, const u8
*key
,
198 unsigned int keylen
);
199 int (*encrypt
)(struct ablkcipher_request
*req
);
200 int (*decrypt
)(struct ablkcipher_request
*req
);
201 int (*givencrypt
)(struct skcipher_givcrypt_request
*req
);
202 int (*givdecrypt
)(struct skcipher_givcrypt_request
*req
);
206 unsigned int min_keysize
;
207 unsigned int max_keysize
;
212 int (*init
)(struct ahash_request
*req
);
213 int (*update
)(struct ahash_request
*req
);
214 int (*final
)(struct ahash_request
*req
);
215 int (*digest
)(struct ahash_request
*req
);
216 int (*setkey
)(struct crypto_ahash
*tfm
, const u8
*key
,
217 unsigned int keylen
);
219 unsigned int digestsize
;
223 int (*setkey
)(struct crypto_aead
*tfm
, const u8
*key
,
224 unsigned int keylen
);
225 int (*setauthsize
)(struct crypto_aead
*tfm
, unsigned int authsize
);
226 int (*encrypt
)(struct aead_request
*req
);
227 int (*decrypt
)(struct aead_request
*req
);
228 int (*givencrypt
)(struct aead_givcrypt_request
*req
);
229 int (*givdecrypt
)(struct aead_givcrypt_request
*req
);
234 unsigned int maxauthsize
;
237 struct blkcipher_alg
{
238 int (*setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
239 unsigned int keylen
);
240 int (*encrypt
)(struct blkcipher_desc
*desc
,
241 struct scatterlist
*dst
, struct scatterlist
*src
,
242 unsigned int nbytes
);
243 int (*decrypt
)(struct blkcipher_desc
*desc
,
244 struct scatterlist
*dst
, struct scatterlist
*src
,
245 unsigned int nbytes
);
249 unsigned int min_keysize
;
250 unsigned int max_keysize
;
255 unsigned int cia_min_keysize
;
256 unsigned int cia_max_keysize
;
257 int (*cia_setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
258 unsigned int keylen
);
259 void (*cia_encrypt
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
260 void (*cia_decrypt
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
264 unsigned int dia_digestsize
;
265 void (*dia_init
)(struct crypto_tfm
*tfm
);
266 void (*dia_update
)(struct crypto_tfm
*tfm
, const u8
*data
,
268 void (*dia_final
)(struct crypto_tfm
*tfm
, u8
*out
);
269 int (*dia_setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
270 unsigned int keylen
);
274 int (*init
)(struct hash_desc
*desc
);
275 int (*update
)(struct hash_desc
*desc
, struct scatterlist
*sg
,
276 unsigned int nbytes
);
277 int (*final
)(struct hash_desc
*desc
, u8
*out
);
278 int (*digest
)(struct hash_desc
*desc
, struct scatterlist
*sg
,
279 unsigned int nbytes
, u8
*out
);
280 int (*setkey
)(struct crypto_hash
*tfm
, const u8
*key
,
281 unsigned int keylen
);
283 unsigned int digestsize
;
286 struct compress_alg
{
287 int (*coa_compress
)(struct crypto_tfm
*tfm
, const u8
*src
,
288 unsigned int slen
, u8
*dst
, unsigned int *dlen
);
289 int (*coa_decompress
)(struct crypto_tfm
*tfm
, const u8
*src
,
290 unsigned int slen
, u8
*dst
, unsigned int *dlen
);
293 #define cra_ablkcipher cra_u.ablkcipher
294 #define cra_aead cra_u.aead
295 #define cra_blkcipher cra_u.blkcipher
296 #define cra_cipher cra_u.cipher
297 #define cra_digest cra_u.digest
298 #define cra_hash cra_u.hash
299 #define cra_ahash cra_u.ahash
300 #define cra_compress cra_u.compress
303 struct list_head cra_list
;
304 struct list_head cra_users
;
307 unsigned int cra_blocksize
;
308 unsigned int cra_ctxsize
;
309 unsigned int cra_alignmask
;
314 char cra_name
[CRYPTO_MAX_ALG_NAME
];
315 char cra_driver_name
[CRYPTO_MAX_ALG_NAME
];
317 const struct crypto_type
*cra_type
;
320 struct ablkcipher_alg ablkcipher
;
321 struct aead_alg aead
;
322 struct blkcipher_alg blkcipher
;
323 struct cipher_alg cipher
;
324 struct digest_alg digest
;
325 struct hash_alg hash
;
326 struct ahash_alg ahash
;
327 struct compress_alg compress
;
330 int (*cra_init
)(struct crypto_tfm
*tfm
);
331 void (*cra_exit
)(struct crypto_tfm
*tfm
);
332 void (*cra_destroy
)(struct crypto_alg
*alg
);
334 struct module
*cra_module
;
338 * Algorithm registration interface.
340 int crypto_register_alg(struct crypto_alg
*alg
);
341 int crypto_unregister_alg(struct crypto_alg
*alg
);
344 * Algorithm query interface.
346 int crypto_has_alg(const char *name
, u32 type
, u32 mask
);
349 * Transforms: user-instantiated objects which encapsulate algorithms
350 * and core processing logic. Managed via crypto_alloc_*() and
351 * crypto_free_*(), as well as the various helpers below.
354 struct ablkcipher_tfm
{
355 int (*setkey
)(struct crypto_ablkcipher
*tfm
, const u8
*key
,
356 unsigned int keylen
);
357 int (*encrypt
)(struct ablkcipher_request
*req
);
358 int (*decrypt
)(struct ablkcipher_request
*req
);
359 int (*givencrypt
)(struct skcipher_givcrypt_request
*req
);
360 int (*givdecrypt
)(struct skcipher_givcrypt_request
*req
);
362 struct crypto_ablkcipher
*base
;
365 unsigned int reqsize
;
369 int (*setkey
)(struct crypto_aead
*tfm
, const u8
*key
,
370 unsigned int keylen
);
371 int (*encrypt
)(struct aead_request
*req
);
372 int (*decrypt
)(struct aead_request
*req
);
373 int (*givencrypt
)(struct aead_givcrypt_request
*req
);
374 int (*givdecrypt
)(struct aead_givcrypt_request
*req
);
376 struct crypto_aead
*base
;
379 unsigned int authsize
;
380 unsigned int reqsize
;
383 struct blkcipher_tfm
{
385 int (*setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
386 unsigned int keylen
);
387 int (*encrypt
)(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
388 struct scatterlist
*src
, unsigned int nbytes
);
389 int (*decrypt
)(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
390 struct scatterlist
*src
, unsigned int nbytes
);
394 int (*cit_setkey
)(struct crypto_tfm
*tfm
,
395 const u8
*key
, unsigned int keylen
);
396 void (*cit_encrypt_one
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
397 void (*cit_decrypt_one
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
401 int (*init
)(struct hash_desc
*desc
);
402 int (*update
)(struct hash_desc
*desc
,
403 struct scatterlist
*sg
, unsigned int nsg
);
404 int (*final
)(struct hash_desc
*desc
, u8
*out
);
405 int (*digest
)(struct hash_desc
*desc
, struct scatterlist
*sg
,
406 unsigned int nsg
, u8
*out
);
407 int (*setkey
)(struct crypto_hash
*tfm
, const u8
*key
,
408 unsigned int keylen
);
409 unsigned int digestsize
;
413 int (*init
)(struct ahash_request
*req
);
414 int (*update
)(struct ahash_request
*req
);
415 int (*final
)(struct ahash_request
*req
);
416 int (*digest
)(struct ahash_request
*req
);
417 int (*setkey
)(struct crypto_ahash
*tfm
, const u8
*key
,
418 unsigned int keylen
);
420 unsigned int digestsize
;
421 unsigned int reqsize
;
424 struct compress_tfm
{
425 int (*cot_compress
)(struct crypto_tfm
*tfm
,
426 const u8
*src
, unsigned int slen
,
427 u8
*dst
, unsigned int *dlen
);
428 int (*cot_decompress
)(struct crypto_tfm
*tfm
,
429 const u8
*src
, unsigned int slen
,
430 u8
*dst
, unsigned int *dlen
);
433 #define crt_ablkcipher crt_u.ablkcipher
434 #define crt_aead crt_u.aead
435 #define crt_blkcipher crt_u.blkcipher
436 #define crt_cipher crt_u.cipher
437 #define crt_hash crt_u.hash
438 #define crt_ahash crt_u.ahash
439 #define crt_compress crt_u.compress
446 struct ablkcipher_tfm ablkcipher
;
447 struct aead_tfm aead
;
448 struct blkcipher_tfm blkcipher
;
449 struct cipher_tfm cipher
;
450 struct hash_tfm hash
;
451 struct ahash_tfm ahash
;
452 struct compress_tfm compress
;
455 struct crypto_alg
*__crt_alg
;
457 void *__crt_ctx
[] CRYPTO_MINALIGN_ATTR
;
460 struct crypto_ablkcipher
{
461 struct crypto_tfm base
;
465 struct crypto_tfm base
;
468 struct crypto_blkcipher
{
469 struct crypto_tfm base
;
472 struct crypto_cipher
{
473 struct crypto_tfm base
;
477 struct crypto_tfm base
;
481 struct crypto_tfm base
;
492 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
494 /* Maximum number of (rtattr) parameters for each template. */
495 #define CRYPTO_MAX_ATTRS 32
497 struct crypto_attr_alg
{
498 char name
[CRYPTO_MAX_ALG_NAME
];
501 struct crypto_attr_type
{
506 struct crypto_attr_u32
{
511 * Transform user interface.
514 struct crypto_tfm
*crypto_alloc_tfm(const char *alg_name
, u32 tfm_flags
);
515 struct crypto_tfm
*crypto_alloc_base(const char *alg_name
, u32 type
, u32 mask
);
516 void crypto_free_tfm(struct crypto_tfm
*tfm
);
518 int alg_test(const char *driver
, const char *alg
, u32 type
, u32 mask
);
521 * Transform helpers which query the underlying algorithm.
523 static inline const char *crypto_tfm_alg_name(struct crypto_tfm
*tfm
)
525 return tfm
->__crt_alg
->cra_name
;
528 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm
*tfm
)
530 return tfm
->__crt_alg
->cra_driver_name
;
533 static inline int crypto_tfm_alg_priority(struct crypto_tfm
*tfm
)
535 return tfm
->__crt_alg
->cra_priority
;
538 static inline const char *crypto_tfm_alg_modname(struct crypto_tfm
*tfm
)
540 return module_name(tfm
->__crt_alg
->cra_module
);
543 static inline u32
crypto_tfm_alg_type(struct crypto_tfm
*tfm
)
545 return tfm
->__crt_alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
;
548 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm
*tfm
)
550 return tfm
->__crt_alg
->cra_blocksize
;
553 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm
*tfm
)
555 return tfm
->__crt_alg
->cra_alignmask
;
558 static inline u32
crypto_tfm_get_flags(struct crypto_tfm
*tfm
)
560 return tfm
->crt_flags
;
563 static inline void crypto_tfm_set_flags(struct crypto_tfm
*tfm
, u32 flags
)
565 tfm
->crt_flags
|= flags
;
568 static inline void crypto_tfm_clear_flags(struct crypto_tfm
*tfm
, u32 flags
)
570 tfm
->crt_flags
&= ~flags
;
573 static inline void *crypto_tfm_ctx(struct crypto_tfm
*tfm
)
575 return tfm
->__crt_ctx
;
578 static inline unsigned int crypto_tfm_ctx_alignment(void)
580 struct crypto_tfm
*tfm
;
581 return __alignof__(tfm
->__crt_ctx
);
587 static inline struct crypto_ablkcipher
*__crypto_ablkcipher_cast(
588 struct crypto_tfm
*tfm
)
590 return (struct crypto_ablkcipher
*)tfm
;
593 static inline u32
crypto_skcipher_type(u32 type
)
595 type
&= ~(CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_GENIV
);
596 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
600 static inline u32
crypto_skcipher_mask(u32 mask
)
602 mask
&= ~(CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_GENIV
);
603 mask
|= CRYPTO_ALG_TYPE_BLKCIPHER_MASK
;
607 struct crypto_ablkcipher
*crypto_alloc_ablkcipher(const char *alg_name
,
610 static inline struct crypto_tfm
*crypto_ablkcipher_tfm(
611 struct crypto_ablkcipher
*tfm
)
616 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher
*tfm
)
618 crypto_free_tfm(crypto_ablkcipher_tfm(tfm
));
621 static inline int crypto_has_ablkcipher(const char *alg_name
, u32 type
,
624 return crypto_has_alg(alg_name
, crypto_skcipher_type(type
),
625 crypto_skcipher_mask(mask
));
628 static inline struct ablkcipher_tfm
*crypto_ablkcipher_crt(
629 struct crypto_ablkcipher
*tfm
)
631 return &crypto_ablkcipher_tfm(tfm
)->crt_ablkcipher
;
634 static inline unsigned int crypto_ablkcipher_ivsize(
635 struct crypto_ablkcipher
*tfm
)
637 return crypto_ablkcipher_crt(tfm
)->ivsize
;
640 static inline unsigned int crypto_ablkcipher_blocksize(
641 struct crypto_ablkcipher
*tfm
)
643 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm
));
646 static inline unsigned int crypto_ablkcipher_alignmask(
647 struct crypto_ablkcipher
*tfm
)
649 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm
));
652 static inline u32
crypto_ablkcipher_get_flags(struct crypto_ablkcipher
*tfm
)
654 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm
));
657 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher
*tfm
,
660 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm
), flags
);
663 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher
*tfm
,
666 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm
), flags
);
669 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher
*tfm
,
670 const u8
*key
, unsigned int keylen
)
672 struct ablkcipher_tfm
*crt
= crypto_ablkcipher_crt(tfm
);
674 return crt
->setkey(crt
->base
, key
, keylen
);
677 static inline struct crypto_ablkcipher
*crypto_ablkcipher_reqtfm(
678 struct ablkcipher_request
*req
)
680 return __crypto_ablkcipher_cast(req
->base
.tfm
);
683 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request
*req
)
685 struct ablkcipher_tfm
*crt
=
686 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req
));
687 return crt
->encrypt(req
);
690 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request
*req
)
692 struct ablkcipher_tfm
*crt
=
693 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req
));
694 return crt
->decrypt(req
);
697 static inline unsigned int crypto_ablkcipher_reqsize(
698 struct crypto_ablkcipher
*tfm
)
700 return crypto_ablkcipher_crt(tfm
)->reqsize
;
703 static inline void ablkcipher_request_set_tfm(
704 struct ablkcipher_request
*req
, struct crypto_ablkcipher
*tfm
)
706 req
->base
.tfm
= crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm
)->base
);
709 static inline struct ablkcipher_request
*ablkcipher_request_cast(
710 struct crypto_async_request
*req
)
712 return container_of(req
, struct ablkcipher_request
, base
);
715 static inline struct ablkcipher_request
*ablkcipher_request_alloc(
716 struct crypto_ablkcipher
*tfm
, gfp_t gfp
)
718 struct ablkcipher_request
*req
;
720 req
= kmalloc(sizeof(struct ablkcipher_request
) +
721 crypto_ablkcipher_reqsize(tfm
), gfp
);
724 ablkcipher_request_set_tfm(req
, tfm
);
729 static inline void ablkcipher_request_free(struct ablkcipher_request
*req
)
734 static inline void ablkcipher_request_set_callback(
735 struct ablkcipher_request
*req
,
736 u32 flags
, crypto_completion_t complete
, void *data
)
738 req
->base
.complete
= complete
;
739 req
->base
.data
= data
;
740 req
->base
.flags
= flags
;
743 static inline void ablkcipher_request_set_crypt(
744 struct ablkcipher_request
*req
,
745 struct scatterlist
*src
, struct scatterlist
*dst
,
746 unsigned int nbytes
, void *iv
)
750 req
->nbytes
= nbytes
;
754 static inline struct crypto_aead
*__crypto_aead_cast(struct crypto_tfm
*tfm
)
756 return (struct crypto_aead
*)tfm
;
759 struct crypto_aead
*crypto_alloc_aead(const char *alg_name
, u32 type
, u32 mask
);
761 static inline struct crypto_tfm
*crypto_aead_tfm(struct crypto_aead
*tfm
)
766 static inline void crypto_free_aead(struct crypto_aead
*tfm
)
768 crypto_free_tfm(crypto_aead_tfm(tfm
));
771 static inline struct aead_tfm
*crypto_aead_crt(struct crypto_aead
*tfm
)
773 return &crypto_aead_tfm(tfm
)->crt_aead
;
776 static inline unsigned int crypto_aead_ivsize(struct crypto_aead
*tfm
)
778 return crypto_aead_crt(tfm
)->ivsize
;
781 static inline unsigned int crypto_aead_authsize(struct crypto_aead
*tfm
)
783 return crypto_aead_crt(tfm
)->authsize
;
786 static inline unsigned int crypto_aead_blocksize(struct crypto_aead
*tfm
)
788 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm
));
791 static inline unsigned int crypto_aead_alignmask(struct crypto_aead
*tfm
)
793 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm
));
796 static inline u32
crypto_aead_get_flags(struct crypto_aead
*tfm
)
798 return crypto_tfm_get_flags(crypto_aead_tfm(tfm
));
801 static inline void crypto_aead_set_flags(struct crypto_aead
*tfm
, u32 flags
)
803 crypto_tfm_set_flags(crypto_aead_tfm(tfm
), flags
);
806 static inline void crypto_aead_clear_flags(struct crypto_aead
*tfm
, u32 flags
)
808 crypto_tfm_clear_flags(crypto_aead_tfm(tfm
), flags
);
811 static inline int crypto_aead_setkey(struct crypto_aead
*tfm
, const u8
*key
,
814 struct aead_tfm
*crt
= crypto_aead_crt(tfm
);
816 return crt
->setkey(crt
->base
, key
, keylen
);
819 int crypto_aead_setauthsize(struct crypto_aead
*tfm
, unsigned int authsize
);
821 static inline struct crypto_aead
*crypto_aead_reqtfm(struct aead_request
*req
)
823 return __crypto_aead_cast(req
->base
.tfm
);
826 static inline int crypto_aead_encrypt(struct aead_request
*req
)
828 return crypto_aead_crt(crypto_aead_reqtfm(req
))->encrypt(req
);
831 static inline int crypto_aead_decrypt(struct aead_request
*req
)
833 return crypto_aead_crt(crypto_aead_reqtfm(req
))->decrypt(req
);
836 static inline unsigned int crypto_aead_reqsize(struct crypto_aead
*tfm
)
838 return crypto_aead_crt(tfm
)->reqsize
;
841 static inline void aead_request_set_tfm(struct aead_request
*req
,
842 struct crypto_aead
*tfm
)
844 req
->base
.tfm
= crypto_aead_tfm(crypto_aead_crt(tfm
)->base
);
847 static inline struct aead_request
*aead_request_alloc(struct crypto_aead
*tfm
,
850 struct aead_request
*req
;
852 req
= kmalloc(sizeof(*req
) + crypto_aead_reqsize(tfm
), gfp
);
855 aead_request_set_tfm(req
, tfm
);
860 static inline void aead_request_free(struct aead_request
*req
)
865 static inline void aead_request_set_callback(struct aead_request
*req
,
867 crypto_completion_t complete
,
870 req
->base
.complete
= complete
;
871 req
->base
.data
= data
;
872 req
->base
.flags
= flags
;
875 static inline void aead_request_set_crypt(struct aead_request
*req
,
876 struct scatterlist
*src
,
877 struct scatterlist
*dst
,
878 unsigned int cryptlen
, u8
*iv
)
882 req
->cryptlen
= cryptlen
;
886 static inline void aead_request_set_assoc(struct aead_request
*req
,
887 struct scatterlist
*assoc
,
888 unsigned int assoclen
)
891 req
->assoclen
= assoclen
;
894 static inline struct crypto_blkcipher
*__crypto_blkcipher_cast(
895 struct crypto_tfm
*tfm
)
897 return (struct crypto_blkcipher
*)tfm
;
900 static inline struct crypto_blkcipher
*crypto_blkcipher_cast(
901 struct crypto_tfm
*tfm
)
903 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_BLKCIPHER
);
904 return __crypto_blkcipher_cast(tfm
);
907 static inline struct crypto_blkcipher
*crypto_alloc_blkcipher(
908 const char *alg_name
, u32 type
, u32 mask
)
910 type
&= ~CRYPTO_ALG_TYPE_MASK
;
911 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
912 mask
|= CRYPTO_ALG_TYPE_MASK
;
914 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name
, type
, mask
));
917 static inline struct crypto_tfm
*crypto_blkcipher_tfm(
918 struct crypto_blkcipher
*tfm
)
923 static inline void crypto_free_blkcipher(struct crypto_blkcipher
*tfm
)
925 crypto_free_tfm(crypto_blkcipher_tfm(tfm
));
928 static inline int crypto_has_blkcipher(const char *alg_name
, u32 type
, u32 mask
)
930 type
&= ~CRYPTO_ALG_TYPE_MASK
;
931 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
932 mask
|= CRYPTO_ALG_TYPE_MASK
;
934 return crypto_has_alg(alg_name
, type
, mask
);
937 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher
*tfm
)
939 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm
));
942 static inline struct blkcipher_tfm
*crypto_blkcipher_crt(
943 struct crypto_blkcipher
*tfm
)
945 return &crypto_blkcipher_tfm(tfm
)->crt_blkcipher
;
948 static inline struct blkcipher_alg
*crypto_blkcipher_alg(
949 struct crypto_blkcipher
*tfm
)
951 return &crypto_blkcipher_tfm(tfm
)->__crt_alg
->cra_blkcipher
;
954 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher
*tfm
)
956 return crypto_blkcipher_alg(tfm
)->ivsize
;
959 static inline unsigned int crypto_blkcipher_blocksize(
960 struct crypto_blkcipher
*tfm
)
962 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm
));
965 static inline unsigned int crypto_blkcipher_alignmask(
966 struct crypto_blkcipher
*tfm
)
968 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm
));
971 static inline u32
crypto_blkcipher_get_flags(struct crypto_blkcipher
*tfm
)
973 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm
));
976 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher
*tfm
,
979 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm
), flags
);
982 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher
*tfm
,
985 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm
), flags
);
988 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher
*tfm
,
989 const u8
*key
, unsigned int keylen
)
991 return crypto_blkcipher_crt(tfm
)->setkey(crypto_blkcipher_tfm(tfm
),
995 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc
*desc
,
996 struct scatterlist
*dst
,
997 struct scatterlist
*src
,
1000 desc
->info
= crypto_blkcipher_crt(desc
->tfm
)->iv
;
1001 return crypto_blkcipher_crt(desc
->tfm
)->encrypt(desc
, dst
, src
, nbytes
);
1004 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc
*desc
,
1005 struct scatterlist
*dst
,
1006 struct scatterlist
*src
,
1007 unsigned int nbytes
)
1009 return crypto_blkcipher_crt(desc
->tfm
)->encrypt(desc
, dst
, src
, nbytes
);
1012 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc
*desc
,
1013 struct scatterlist
*dst
,
1014 struct scatterlist
*src
,
1015 unsigned int nbytes
)
1017 desc
->info
= crypto_blkcipher_crt(desc
->tfm
)->iv
;
1018 return crypto_blkcipher_crt(desc
->tfm
)->decrypt(desc
, dst
, src
, nbytes
);
1021 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc
*desc
,
1022 struct scatterlist
*dst
,
1023 struct scatterlist
*src
,
1024 unsigned int nbytes
)
1026 return crypto_blkcipher_crt(desc
->tfm
)->decrypt(desc
, dst
, src
, nbytes
);
1029 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher
*tfm
,
1030 const u8
*src
, unsigned int len
)
1032 memcpy(crypto_blkcipher_crt(tfm
)->iv
, src
, len
);
1035 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher
*tfm
,
1036 u8
*dst
, unsigned int len
)
1038 memcpy(dst
, crypto_blkcipher_crt(tfm
)->iv
, len
);
1041 static inline struct crypto_cipher
*__crypto_cipher_cast(struct crypto_tfm
*tfm
)
1043 return (struct crypto_cipher
*)tfm
;
1046 static inline struct crypto_cipher
*crypto_cipher_cast(struct crypto_tfm
*tfm
)
1048 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
1049 return __crypto_cipher_cast(tfm
);
1052 static inline struct crypto_cipher
*crypto_alloc_cipher(const char *alg_name
,
1055 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1056 type
|= CRYPTO_ALG_TYPE_CIPHER
;
1057 mask
|= CRYPTO_ALG_TYPE_MASK
;
1059 return __crypto_cipher_cast(crypto_alloc_base(alg_name
, type
, mask
));
1062 static inline struct crypto_tfm
*crypto_cipher_tfm(struct crypto_cipher
*tfm
)
1067 static inline void crypto_free_cipher(struct crypto_cipher
*tfm
)
1069 crypto_free_tfm(crypto_cipher_tfm(tfm
));
1072 static inline int crypto_has_cipher(const char *alg_name
, u32 type
, u32 mask
)
1074 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1075 type
|= CRYPTO_ALG_TYPE_CIPHER
;
1076 mask
|= CRYPTO_ALG_TYPE_MASK
;
1078 return crypto_has_alg(alg_name
, type
, mask
);
1081 static inline struct cipher_tfm
*crypto_cipher_crt(struct crypto_cipher
*tfm
)
1083 return &crypto_cipher_tfm(tfm
)->crt_cipher
;
1086 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher
*tfm
)
1088 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm
));
1091 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher
*tfm
)
1093 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm
));
1096 static inline u32
crypto_cipher_get_flags(struct crypto_cipher
*tfm
)
1098 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm
));
1101 static inline void crypto_cipher_set_flags(struct crypto_cipher
*tfm
,
1104 crypto_tfm_set_flags(crypto_cipher_tfm(tfm
), flags
);
1107 static inline void crypto_cipher_clear_flags(struct crypto_cipher
*tfm
,
1110 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm
), flags
);
1113 static inline int crypto_cipher_setkey(struct crypto_cipher
*tfm
,
1114 const u8
*key
, unsigned int keylen
)
1116 return crypto_cipher_crt(tfm
)->cit_setkey(crypto_cipher_tfm(tfm
),
1120 static inline void crypto_cipher_encrypt_one(struct crypto_cipher
*tfm
,
1121 u8
*dst
, const u8
*src
)
1123 crypto_cipher_crt(tfm
)->cit_encrypt_one(crypto_cipher_tfm(tfm
),
1127 static inline void crypto_cipher_decrypt_one(struct crypto_cipher
*tfm
,
1128 u8
*dst
, const u8
*src
)
1130 crypto_cipher_crt(tfm
)->cit_decrypt_one(crypto_cipher_tfm(tfm
),
1134 static inline struct crypto_hash
*__crypto_hash_cast(struct crypto_tfm
*tfm
)
1136 return (struct crypto_hash
*)tfm
;
1139 static inline struct crypto_hash
*crypto_hash_cast(struct crypto_tfm
*tfm
)
1141 BUG_ON((crypto_tfm_alg_type(tfm
) ^ CRYPTO_ALG_TYPE_HASH
) &
1142 CRYPTO_ALG_TYPE_HASH_MASK
);
1143 return __crypto_hash_cast(tfm
);
1146 static inline struct crypto_hash
*crypto_alloc_hash(const char *alg_name
,
1149 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1150 mask
&= ~CRYPTO_ALG_TYPE_MASK
;
1151 type
|= CRYPTO_ALG_TYPE_HASH
;
1152 mask
|= CRYPTO_ALG_TYPE_HASH_MASK
;
1154 return __crypto_hash_cast(crypto_alloc_base(alg_name
, type
, mask
));
1157 static inline struct crypto_tfm
*crypto_hash_tfm(struct crypto_hash
*tfm
)
1162 static inline void crypto_free_hash(struct crypto_hash
*tfm
)
1164 crypto_free_tfm(crypto_hash_tfm(tfm
));
1167 static inline int crypto_has_hash(const char *alg_name
, u32 type
, u32 mask
)
1169 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1170 mask
&= ~CRYPTO_ALG_TYPE_MASK
;
1171 type
|= CRYPTO_ALG_TYPE_HASH
;
1172 mask
|= CRYPTO_ALG_TYPE_HASH_MASK
;
1174 return crypto_has_alg(alg_name
, type
, mask
);
1177 static inline struct hash_tfm
*crypto_hash_crt(struct crypto_hash
*tfm
)
1179 return &crypto_hash_tfm(tfm
)->crt_hash
;
1182 static inline unsigned int crypto_hash_blocksize(struct crypto_hash
*tfm
)
1184 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm
));
1187 static inline unsigned int crypto_hash_alignmask(struct crypto_hash
*tfm
)
1189 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm
));
1192 static inline unsigned int crypto_hash_digestsize(struct crypto_hash
*tfm
)
1194 return crypto_hash_crt(tfm
)->digestsize
;
1197 static inline u32
crypto_hash_get_flags(struct crypto_hash
*tfm
)
1199 return crypto_tfm_get_flags(crypto_hash_tfm(tfm
));
1202 static inline void crypto_hash_set_flags(struct crypto_hash
*tfm
, u32 flags
)
1204 crypto_tfm_set_flags(crypto_hash_tfm(tfm
), flags
);
1207 static inline void crypto_hash_clear_flags(struct crypto_hash
*tfm
, u32 flags
)
1209 crypto_tfm_clear_flags(crypto_hash_tfm(tfm
), flags
);
1212 static inline int crypto_hash_init(struct hash_desc
*desc
)
1214 return crypto_hash_crt(desc
->tfm
)->init(desc
);
1217 static inline int crypto_hash_update(struct hash_desc
*desc
,
1218 struct scatterlist
*sg
,
1219 unsigned int nbytes
)
1221 return crypto_hash_crt(desc
->tfm
)->update(desc
, sg
, nbytes
);
1224 static inline int crypto_hash_final(struct hash_desc
*desc
, u8
*out
)
1226 return crypto_hash_crt(desc
->tfm
)->final(desc
, out
);
1229 static inline int crypto_hash_digest(struct hash_desc
*desc
,
1230 struct scatterlist
*sg
,
1231 unsigned int nbytes
, u8
*out
)
1233 return crypto_hash_crt(desc
->tfm
)->digest(desc
, sg
, nbytes
, out
);
1236 static inline int crypto_hash_setkey(struct crypto_hash
*hash
,
1237 const u8
*key
, unsigned int keylen
)
1239 return crypto_hash_crt(hash
)->setkey(hash
, key
, keylen
);
1242 static inline struct crypto_comp
*__crypto_comp_cast(struct crypto_tfm
*tfm
)
1244 return (struct crypto_comp
*)tfm
;
1247 static inline struct crypto_comp
*crypto_comp_cast(struct crypto_tfm
*tfm
)
1249 BUG_ON((crypto_tfm_alg_type(tfm
) ^ CRYPTO_ALG_TYPE_COMPRESS
) &
1250 CRYPTO_ALG_TYPE_MASK
);
1251 return __crypto_comp_cast(tfm
);
1254 static inline struct crypto_comp
*crypto_alloc_comp(const char *alg_name
,
1257 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1258 type
|= CRYPTO_ALG_TYPE_COMPRESS
;
1259 mask
|= CRYPTO_ALG_TYPE_MASK
;
1261 return __crypto_comp_cast(crypto_alloc_base(alg_name
, type
, mask
));
1264 static inline struct crypto_tfm
*crypto_comp_tfm(struct crypto_comp
*tfm
)
1269 static inline void crypto_free_comp(struct crypto_comp
*tfm
)
1271 crypto_free_tfm(crypto_comp_tfm(tfm
));
1274 static inline int crypto_has_comp(const char *alg_name
, u32 type
, u32 mask
)
1276 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1277 type
|= CRYPTO_ALG_TYPE_COMPRESS
;
1278 mask
|= CRYPTO_ALG_TYPE_MASK
;
1280 return crypto_has_alg(alg_name
, type
, mask
);
1283 static inline const char *crypto_comp_name(struct crypto_comp
*tfm
)
1285 return crypto_tfm_alg_name(crypto_comp_tfm(tfm
));
1288 static inline struct compress_tfm
*crypto_comp_crt(struct crypto_comp
*tfm
)
1290 return &crypto_comp_tfm(tfm
)->crt_compress
;
1293 static inline int crypto_comp_compress(struct crypto_comp
*tfm
,
1294 const u8
*src
, unsigned int slen
,
1295 u8
*dst
, unsigned int *dlen
)
1297 return crypto_comp_crt(tfm
)->cot_compress(crypto_comp_tfm(tfm
),
1298 src
, slen
, dst
, dlen
);
1301 static inline int crypto_comp_decompress(struct crypto_comp
*tfm
,
1302 const u8
*src
, unsigned int slen
,
1303 u8
*dst
, unsigned int *dlen
)
1305 return crypto_comp_crt(tfm
)->cot_decompress(crypto_comp_tfm(tfm
),
1306 src
, slen
, dst
, dlen
);
1309 #endif /* _LINUX_CRYPTO_H */