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
79 * Transform masks and values (for crt_flags).
81 #define CRYPTO_TFM_REQ_MASK 0x000fff00
82 #define CRYPTO_TFM_RES_MASK 0xfff00000
84 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
85 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
86 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
87 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
88 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
89 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
90 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
91 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
94 * Miscellaneous stuff.
96 #define CRYPTO_MAX_ALG_NAME 64
99 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual
100 * declaration) is used to ensure that the crypto_tfm context structure is
101 * aligned correctly for the given architecture so that there are no alignment
102 * faults for C data types. In particular, this is required on platforms such
103 * as arm where pointers are 32-bit aligned but there are data types such as
104 * u64 which require 64-bit alignment.
106 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
108 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
111 struct crypto_ablkcipher
;
112 struct crypto_async_request
;
114 struct crypto_blkcipher
;
119 struct aead_givcrypt_request
;
120 struct skcipher_givcrypt_request
;
122 typedef void (*crypto_completion_t
)(struct crypto_async_request
*req
, int err
);
124 struct crypto_async_request
{
125 struct list_head list
;
126 crypto_completion_t complete
;
128 struct crypto_tfm
*tfm
;
133 struct ablkcipher_request
{
134 struct crypto_async_request base
;
140 struct scatterlist
*src
;
141 struct scatterlist
*dst
;
143 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
147 * struct aead_request - AEAD request
148 * @base: Common attributes for async crypto requests
149 * @assoclen: Length in bytes of associated data for authentication
150 * @cryptlen: Length of data to be encrypted or decrypted
151 * @iv: Initialisation vector
152 * @assoc: Associated data
154 * @dst: Destination data
155 * @__ctx: Start of private context data
157 struct aead_request
{
158 struct crypto_async_request base
;
160 unsigned int assoclen
;
161 unsigned int cryptlen
;
165 struct scatterlist
*assoc
;
166 struct scatterlist
*src
;
167 struct scatterlist
*dst
;
169 void *__ctx
[] CRYPTO_MINALIGN_ATTR
;
172 struct blkcipher_desc
{
173 struct crypto_blkcipher
*tfm
;
179 struct crypto_tfm
*tfm
;
180 void (*crfn
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
181 unsigned int (*prfn
)(const struct cipher_desc
*desc
, u8
*dst
,
182 const u8
*src
, unsigned int nbytes
);
187 struct crypto_hash
*tfm
;
192 * Algorithms: modular crypto algorithm implementations, managed
193 * via crypto_register_alg() and crypto_unregister_alg().
195 struct ablkcipher_alg
{
196 int (*setkey
)(struct crypto_ablkcipher
*tfm
, const u8
*key
,
197 unsigned int keylen
);
198 int (*encrypt
)(struct ablkcipher_request
*req
);
199 int (*decrypt
)(struct ablkcipher_request
*req
);
200 int (*givencrypt
)(struct skcipher_givcrypt_request
*req
);
201 int (*givdecrypt
)(struct skcipher_givcrypt_request
*req
);
205 unsigned int min_keysize
;
206 unsigned int max_keysize
;
211 int (*setkey
)(struct crypto_aead
*tfm
, const u8
*key
,
212 unsigned int keylen
);
213 int (*setauthsize
)(struct crypto_aead
*tfm
, unsigned int authsize
);
214 int (*encrypt
)(struct aead_request
*req
);
215 int (*decrypt
)(struct aead_request
*req
);
216 int (*givencrypt
)(struct aead_givcrypt_request
*req
);
217 int (*givdecrypt
)(struct aead_givcrypt_request
*req
);
222 unsigned int maxauthsize
;
225 struct blkcipher_alg
{
226 int (*setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
227 unsigned int keylen
);
228 int (*encrypt
)(struct blkcipher_desc
*desc
,
229 struct scatterlist
*dst
, struct scatterlist
*src
,
230 unsigned int nbytes
);
231 int (*decrypt
)(struct blkcipher_desc
*desc
,
232 struct scatterlist
*dst
, struct scatterlist
*src
,
233 unsigned int nbytes
);
237 unsigned int min_keysize
;
238 unsigned int max_keysize
;
243 unsigned int cia_min_keysize
;
244 unsigned int cia_max_keysize
;
245 int (*cia_setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
246 unsigned int keylen
);
247 void (*cia_encrypt
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
248 void (*cia_decrypt
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
251 struct compress_alg
{
252 int (*coa_compress
)(struct crypto_tfm
*tfm
, const u8
*src
,
253 unsigned int slen
, u8
*dst
, unsigned int *dlen
);
254 int (*coa_decompress
)(struct crypto_tfm
*tfm
, const u8
*src
,
255 unsigned int slen
, u8
*dst
, unsigned int *dlen
);
259 int (*rng_make_random
)(struct crypto_rng
*tfm
, u8
*rdata
,
261 int (*rng_reset
)(struct crypto_rng
*tfm
, u8
*seed
, unsigned int slen
);
263 unsigned int seedsize
;
267 #define cra_ablkcipher cra_u.ablkcipher
268 #define cra_aead cra_u.aead
269 #define cra_blkcipher cra_u.blkcipher
270 #define cra_cipher cra_u.cipher
271 #define cra_compress cra_u.compress
272 #define cra_rng cra_u.rng
275 struct list_head cra_list
;
276 struct list_head cra_users
;
279 unsigned int cra_blocksize
;
280 unsigned int cra_ctxsize
;
281 unsigned int cra_alignmask
;
286 char cra_name
[CRYPTO_MAX_ALG_NAME
];
287 char cra_driver_name
[CRYPTO_MAX_ALG_NAME
];
289 const struct crypto_type
*cra_type
;
292 struct ablkcipher_alg ablkcipher
;
293 struct aead_alg aead
;
294 struct blkcipher_alg blkcipher
;
295 struct cipher_alg cipher
;
296 struct compress_alg compress
;
300 int (*cra_init
)(struct crypto_tfm
*tfm
);
301 void (*cra_exit
)(struct crypto_tfm
*tfm
);
302 void (*cra_destroy
)(struct crypto_alg
*alg
);
304 struct module
*cra_module
;
308 * Algorithm registration interface.
310 int crypto_register_alg(struct crypto_alg
*alg
);
311 int crypto_unregister_alg(struct crypto_alg
*alg
);
314 * Algorithm query interface.
316 int crypto_has_alg(const char *name
, u32 type
, u32 mask
);
319 * Transforms: user-instantiated objects which encapsulate algorithms
320 * and core processing logic. Managed via crypto_alloc_*() and
321 * crypto_free_*(), as well as the various helpers below.
324 struct ablkcipher_tfm
{
325 int (*setkey
)(struct crypto_ablkcipher
*tfm
, const u8
*key
,
326 unsigned int keylen
);
327 int (*encrypt
)(struct ablkcipher_request
*req
);
328 int (*decrypt
)(struct ablkcipher_request
*req
);
329 int (*givencrypt
)(struct skcipher_givcrypt_request
*req
);
330 int (*givdecrypt
)(struct skcipher_givcrypt_request
*req
);
332 struct crypto_ablkcipher
*base
;
335 unsigned int reqsize
;
339 int (*setkey
)(struct crypto_aead
*tfm
, const u8
*key
,
340 unsigned int keylen
);
341 int (*encrypt
)(struct aead_request
*req
);
342 int (*decrypt
)(struct aead_request
*req
);
343 int (*givencrypt
)(struct aead_givcrypt_request
*req
);
344 int (*givdecrypt
)(struct aead_givcrypt_request
*req
);
346 struct crypto_aead
*base
;
349 unsigned int authsize
;
350 unsigned int reqsize
;
353 struct blkcipher_tfm
{
355 int (*setkey
)(struct crypto_tfm
*tfm
, const u8
*key
,
356 unsigned int keylen
);
357 int (*encrypt
)(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
358 struct scatterlist
*src
, unsigned int nbytes
);
359 int (*decrypt
)(struct blkcipher_desc
*desc
, struct scatterlist
*dst
,
360 struct scatterlist
*src
, unsigned int nbytes
);
364 int (*cit_setkey
)(struct crypto_tfm
*tfm
,
365 const u8
*key
, unsigned int keylen
);
366 void (*cit_encrypt_one
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
367 void (*cit_decrypt_one
)(struct crypto_tfm
*tfm
, u8
*dst
, const u8
*src
);
371 int (*init
)(struct hash_desc
*desc
);
372 int (*update
)(struct hash_desc
*desc
,
373 struct scatterlist
*sg
, unsigned int nsg
);
374 int (*final
)(struct hash_desc
*desc
, u8
*out
);
375 int (*digest
)(struct hash_desc
*desc
, struct scatterlist
*sg
,
376 unsigned int nsg
, u8
*out
);
377 int (*setkey
)(struct crypto_hash
*tfm
, const u8
*key
,
378 unsigned int keylen
);
379 unsigned int digestsize
;
382 struct compress_tfm
{
383 int (*cot_compress
)(struct crypto_tfm
*tfm
,
384 const u8
*src
, unsigned int slen
,
385 u8
*dst
, unsigned int *dlen
);
386 int (*cot_decompress
)(struct crypto_tfm
*tfm
,
387 const u8
*src
, unsigned int slen
,
388 u8
*dst
, unsigned int *dlen
);
392 int (*rng_gen_random
)(struct crypto_rng
*tfm
, u8
*rdata
,
394 int (*rng_reset
)(struct crypto_rng
*tfm
, u8
*seed
, unsigned int slen
);
397 #define crt_ablkcipher crt_u.ablkcipher
398 #define crt_aead crt_u.aead
399 #define crt_blkcipher crt_u.blkcipher
400 #define crt_cipher crt_u.cipher
401 #define crt_hash crt_u.hash
402 #define crt_compress crt_u.compress
403 #define crt_rng crt_u.rng
410 struct ablkcipher_tfm ablkcipher
;
411 struct aead_tfm aead
;
412 struct blkcipher_tfm blkcipher
;
413 struct cipher_tfm cipher
;
414 struct hash_tfm hash
;
415 struct compress_tfm compress
;
419 void (*exit
)(struct crypto_tfm
*tfm
);
421 struct crypto_alg
*__crt_alg
;
423 void *__crt_ctx
[] CRYPTO_MINALIGN_ATTR
;
426 struct crypto_ablkcipher
{
427 struct crypto_tfm base
;
431 struct crypto_tfm base
;
434 struct crypto_blkcipher
{
435 struct crypto_tfm base
;
438 struct crypto_cipher
{
439 struct crypto_tfm base
;
443 struct crypto_tfm base
;
447 struct crypto_tfm base
;
451 struct crypto_tfm base
;
462 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
464 /* Maximum number of (rtattr) parameters for each template. */
465 #define CRYPTO_MAX_ATTRS 32
467 struct crypto_attr_alg
{
468 char name
[CRYPTO_MAX_ALG_NAME
];
471 struct crypto_attr_type
{
476 struct crypto_attr_u32
{
481 * Transform user interface.
484 struct crypto_tfm
*crypto_alloc_base(const char *alg_name
, u32 type
, u32 mask
);
485 void crypto_destroy_tfm(void *mem
, struct crypto_tfm
*tfm
);
487 static inline void crypto_free_tfm(struct crypto_tfm
*tfm
)
489 return crypto_destroy_tfm(tfm
, tfm
);
492 int alg_test(const char *driver
, const char *alg
, u32 type
, u32 mask
);
495 * Transform helpers which query the underlying algorithm.
497 static inline const char *crypto_tfm_alg_name(struct crypto_tfm
*tfm
)
499 return tfm
->__crt_alg
->cra_name
;
502 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm
*tfm
)
504 return tfm
->__crt_alg
->cra_driver_name
;
507 static inline int crypto_tfm_alg_priority(struct crypto_tfm
*tfm
)
509 return tfm
->__crt_alg
->cra_priority
;
512 static inline u32
crypto_tfm_alg_type(struct crypto_tfm
*tfm
)
514 return tfm
->__crt_alg
->cra_flags
& CRYPTO_ALG_TYPE_MASK
;
517 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm
*tfm
)
519 return tfm
->__crt_alg
->cra_blocksize
;
522 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm
*tfm
)
524 return tfm
->__crt_alg
->cra_alignmask
;
527 static inline u32
crypto_tfm_get_flags(struct crypto_tfm
*tfm
)
529 return tfm
->crt_flags
;
532 static inline void crypto_tfm_set_flags(struct crypto_tfm
*tfm
, u32 flags
)
534 tfm
->crt_flags
|= flags
;
537 static inline void crypto_tfm_clear_flags(struct crypto_tfm
*tfm
, u32 flags
)
539 tfm
->crt_flags
&= ~flags
;
542 static inline void *crypto_tfm_ctx(struct crypto_tfm
*tfm
)
544 return tfm
->__crt_ctx
;
547 static inline unsigned int crypto_tfm_ctx_alignment(void)
549 struct crypto_tfm
*tfm
;
550 return __alignof__(tfm
->__crt_ctx
);
556 static inline struct crypto_ablkcipher
*__crypto_ablkcipher_cast(
557 struct crypto_tfm
*tfm
)
559 return (struct crypto_ablkcipher
*)tfm
;
562 static inline u32
crypto_skcipher_type(u32 type
)
564 type
&= ~(CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_GENIV
);
565 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
569 static inline u32
crypto_skcipher_mask(u32 mask
)
571 mask
&= ~(CRYPTO_ALG_TYPE_MASK
| CRYPTO_ALG_GENIV
);
572 mask
|= CRYPTO_ALG_TYPE_BLKCIPHER_MASK
;
576 struct crypto_ablkcipher
*crypto_alloc_ablkcipher(const char *alg_name
,
579 static inline struct crypto_tfm
*crypto_ablkcipher_tfm(
580 struct crypto_ablkcipher
*tfm
)
585 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher
*tfm
)
587 crypto_free_tfm(crypto_ablkcipher_tfm(tfm
));
590 static inline int crypto_has_ablkcipher(const char *alg_name
, u32 type
,
593 return crypto_has_alg(alg_name
, crypto_skcipher_type(type
),
594 crypto_skcipher_mask(mask
));
597 static inline struct ablkcipher_tfm
*crypto_ablkcipher_crt(
598 struct crypto_ablkcipher
*tfm
)
600 return &crypto_ablkcipher_tfm(tfm
)->crt_ablkcipher
;
603 static inline unsigned int crypto_ablkcipher_ivsize(
604 struct crypto_ablkcipher
*tfm
)
606 return crypto_ablkcipher_crt(tfm
)->ivsize
;
609 static inline unsigned int crypto_ablkcipher_blocksize(
610 struct crypto_ablkcipher
*tfm
)
612 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm
));
615 static inline unsigned int crypto_ablkcipher_alignmask(
616 struct crypto_ablkcipher
*tfm
)
618 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm
));
621 static inline u32
crypto_ablkcipher_get_flags(struct crypto_ablkcipher
*tfm
)
623 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm
));
626 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher
*tfm
,
629 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm
), flags
);
632 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher
*tfm
,
635 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm
), flags
);
638 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher
*tfm
,
639 const u8
*key
, unsigned int keylen
)
641 struct ablkcipher_tfm
*crt
= crypto_ablkcipher_crt(tfm
);
643 return crt
->setkey(crt
->base
, key
, keylen
);
646 static inline struct crypto_ablkcipher
*crypto_ablkcipher_reqtfm(
647 struct ablkcipher_request
*req
)
649 return __crypto_ablkcipher_cast(req
->base
.tfm
);
652 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request
*req
)
654 struct ablkcipher_tfm
*crt
=
655 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req
));
656 return crt
->encrypt(req
);
659 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request
*req
)
661 struct ablkcipher_tfm
*crt
=
662 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req
));
663 return crt
->decrypt(req
);
666 static inline unsigned int crypto_ablkcipher_reqsize(
667 struct crypto_ablkcipher
*tfm
)
669 return crypto_ablkcipher_crt(tfm
)->reqsize
;
672 static inline void ablkcipher_request_set_tfm(
673 struct ablkcipher_request
*req
, struct crypto_ablkcipher
*tfm
)
675 req
->base
.tfm
= crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm
)->base
);
678 static inline struct ablkcipher_request
*ablkcipher_request_cast(
679 struct crypto_async_request
*req
)
681 return container_of(req
, struct ablkcipher_request
, base
);
684 static inline struct ablkcipher_request
*ablkcipher_request_alloc(
685 struct crypto_ablkcipher
*tfm
, gfp_t gfp
)
687 struct ablkcipher_request
*req
;
689 req
= kmalloc(sizeof(struct ablkcipher_request
) +
690 crypto_ablkcipher_reqsize(tfm
), gfp
);
693 ablkcipher_request_set_tfm(req
, tfm
);
698 static inline void ablkcipher_request_free(struct ablkcipher_request
*req
)
703 static inline void ablkcipher_request_set_callback(
704 struct ablkcipher_request
*req
,
705 u32 flags
, crypto_completion_t complete
, void *data
)
707 req
->base
.complete
= complete
;
708 req
->base
.data
= data
;
709 req
->base
.flags
= flags
;
712 static inline void ablkcipher_request_set_crypt(
713 struct ablkcipher_request
*req
,
714 struct scatterlist
*src
, struct scatterlist
*dst
,
715 unsigned int nbytes
, void *iv
)
719 req
->nbytes
= nbytes
;
723 static inline struct crypto_aead
*__crypto_aead_cast(struct crypto_tfm
*tfm
)
725 return (struct crypto_aead
*)tfm
;
728 struct crypto_aead
*crypto_alloc_aead(const char *alg_name
, u32 type
, u32 mask
);
730 static inline struct crypto_tfm
*crypto_aead_tfm(struct crypto_aead
*tfm
)
735 static inline void crypto_free_aead(struct crypto_aead
*tfm
)
737 crypto_free_tfm(crypto_aead_tfm(tfm
));
740 static inline struct aead_tfm
*crypto_aead_crt(struct crypto_aead
*tfm
)
742 return &crypto_aead_tfm(tfm
)->crt_aead
;
745 static inline unsigned int crypto_aead_ivsize(struct crypto_aead
*tfm
)
747 return crypto_aead_crt(tfm
)->ivsize
;
750 static inline unsigned int crypto_aead_authsize(struct crypto_aead
*tfm
)
752 return crypto_aead_crt(tfm
)->authsize
;
755 static inline unsigned int crypto_aead_blocksize(struct crypto_aead
*tfm
)
757 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm
));
760 static inline unsigned int crypto_aead_alignmask(struct crypto_aead
*tfm
)
762 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm
));
765 static inline u32
crypto_aead_get_flags(struct crypto_aead
*tfm
)
767 return crypto_tfm_get_flags(crypto_aead_tfm(tfm
));
770 static inline void crypto_aead_set_flags(struct crypto_aead
*tfm
, u32 flags
)
772 crypto_tfm_set_flags(crypto_aead_tfm(tfm
), flags
);
775 static inline void crypto_aead_clear_flags(struct crypto_aead
*tfm
, u32 flags
)
777 crypto_tfm_clear_flags(crypto_aead_tfm(tfm
), flags
);
780 static inline int crypto_aead_setkey(struct crypto_aead
*tfm
, const u8
*key
,
783 struct aead_tfm
*crt
= crypto_aead_crt(tfm
);
785 return crt
->setkey(crt
->base
, key
, keylen
);
788 int crypto_aead_setauthsize(struct crypto_aead
*tfm
, unsigned int authsize
);
790 static inline struct crypto_aead
*crypto_aead_reqtfm(struct aead_request
*req
)
792 return __crypto_aead_cast(req
->base
.tfm
);
795 static inline int crypto_aead_encrypt(struct aead_request
*req
)
797 return crypto_aead_crt(crypto_aead_reqtfm(req
))->encrypt(req
);
800 static inline int crypto_aead_decrypt(struct aead_request
*req
)
802 return crypto_aead_crt(crypto_aead_reqtfm(req
))->decrypt(req
);
805 static inline unsigned int crypto_aead_reqsize(struct crypto_aead
*tfm
)
807 return crypto_aead_crt(tfm
)->reqsize
;
810 static inline void aead_request_set_tfm(struct aead_request
*req
,
811 struct crypto_aead
*tfm
)
813 req
->base
.tfm
= crypto_aead_tfm(crypto_aead_crt(tfm
)->base
);
816 static inline struct aead_request
*aead_request_alloc(struct crypto_aead
*tfm
,
819 struct aead_request
*req
;
821 req
= kmalloc(sizeof(*req
) + crypto_aead_reqsize(tfm
), gfp
);
824 aead_request_set_tfm(req
, tfm
);
829 static inline void aead_request_free(struct aead_request
*req
)
834 static inline void aead_request_set_callback(struct aead_request
*req
,
836 crypto_completion_t complete
,
839 req
->base
.complete
= complete
;
840 req
->base
.data
= data
;
841 req
->base
.flags
= flags
;
844 static inline void aead_request_set_crypt(struct aead_request
*req
,
845 struct scatterlist
*src
,
846 struct scatterlist
*dst
,
847 unsigned int cryptlen
, u8
*iv
)
851 req
->cryptlen
= cryptlen
;
855 static inline void aead_request_set_assoc(struct aead_request
*req
,
856 struct scatterlist
*assoc
,
857 unsigned int assoclen
)
860 req
->assoclen
= assoclen
;
863 static inline struct crypto_blkcipher
*__crypto_blkcipher_cast(
864 struct crypto_tfm
*tfm
)
866 return (struct crypto_blkcipher
*)tfm
;
869 static inline struct crypto_blkcipher
*crypto_blkcipher_cast(
870 struct crypto_tfm
*tfm
)
872 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_BLKCIPHER
);
873 return __crypto_blkcipher_cast(tfm
);
876 static inline struct crypto_blkcipher
*crypto_alloc_blkcipher(
877 const char *alg_name
, u32 type
, u32 mask
)
879 type
&= ~CRYPTO_ALG_TYPE_MASK
;
880 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
881 mask
|= CRYPTO_ALG_TYPE_MASK
;
883 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name
, type
, mask
));
886 static inline struct crypto_tfm
*crypto_blkcipher_tfm(
887 struct crypto_blkcipher
*tfm
)
892 static inline void crypto_free_blkcipher(struct crypto_blkcipher
*tfm
)
894 crypto_free_tfm(crypto_blkcipher_tfm(tfm
));
897 static inline int crypto_has_blkcipher(const char *alg_name
, u32 type
, u32 mask
)
899 type
&= ~CRYPTO_ALG_TYPE_MASK
;
900 type
|= CRYPTO_ALG_TYPE_BLKCIPHER
;
901 mask
|= CRYPTO_ALG_TYPE_MASK
;
903 return crypto_has_alg(alg_name
, type
, mask
);
906 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher
*tfm
)
908 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm
));
911 static inline struct blkcipher_tfm
*crypto_blkcipher_crt(
912 struct crypto_blkcipher
*tfm
)
914 return &crypto_blkcipher_tfm(tfm
)->crt_blkcipher
;
917 static inline struct blkcipher_alg
*crypto_blkcipher_alg(
918 struct crypto_blkcipher
*tfm
)
920 return &crypto_blkcipher_tfm(tfm
)->__crt_alg
->cra_blkcipher
;
923 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher
*tfm
)
925 return crypto_blkcipher_alg(tfm
)->ivsize
;
928 static inline unsigned int crypto_blkcipher_blocksize(
929 struct crypto_blkcipher
*tfm
)
931 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm
));
934 static inline unsigned int crypto_blkcipher_alignmask(
935 struct crypto_blkcipher
*tfm
)
937 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm
));
940 static inline u32
crypto_blkcipher_get_flags(struct crypto_blkcipher
*tfm
)
942 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm
));
945 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher
*tfm
,
948 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm
), flags
);
951 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher
*tfm
,
954 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm
), flags
);
957 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher
*tfm
,
958 const u8
*key
, unsigned int keylen
)
960 return crypto_blkcipher_crt(tfm
)->setkey(crypto_blkcipher_tfm(tfm
),
964 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc
*desc
,
965 struct scatterlist
*dst
,
966 struct scatterlist
*src
,
969 desc
->info
= crypto_blkcipher_crt(desc
->tfm
)->iv
;
970 return crypto_blkcipher_crt(desc
->tfm
)->encrypt(desc
, dst
, src
, nbytes
);
973 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc
*desc
,
974 struct scatterlist
*dst
,
975 struct scatterlist
*src
,
978 return crypto_blkcipher_crt(desc
->tfm
)->encrypt(desc
, dst
, src
, nbytes
);
981 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc
*desc
,
982 struct scatterlist
*dst
,
983 struct scatterlist
*src
,
986 desc
->info
= crypto_blkcipher_crt(desc
->tfm
)->iv
;
987 return crypto_blkcipher_crt(desc
->tfm
)->decrypt(desc
, dst
, src
, nbytes
);
990 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc
*desc
,
991 struct scatterlist
*dst
,
992 struct scatterlist
*src
,
995 return crypto_blkcipher_crt(desc
->tfm
)->decrypt(desc
, dst
, src
, nbytes
);
998 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher
*tfm
,
999 const u8
*src
, unsigned int len
)
1001 memcpy(crypto_blkcipher_crt(tfm
)->iv
, src
, len
);
1004 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher
*tfm
,
1005 u8
*dst
, unsigned int len
)
1007 memcpy(dst
, crypto_blkcipher_crt(tfm
)->iv
, len
);
1010 static inline struct crypto_cipher
*__crypto_cipher_cast(struct crypto_tfm
*tfm
)
1012 return (struct crypto_cipher
*)tfm
;
1015 static inline struct crypto_cipher
*crypto_cipher_cast(struct crypto_tfm
*tfm
)
1017 BUG_ON(crypto_tfm_alg_type(tfm
) != CRYPTO_ALG_TYPE_CIPHER
);
1018 return __crypto_cipher_cast(tfm
);
1021 static inline struct crypto_cipher
*crypto_alloc_cipher(const char *alg_name
,
1024 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1025 type
|= CRYPTO_ALG_TYPE_CIPHER
;
1026 mask
|= CRYPTO_ALG_TYPE_MASK
;
1028 return __crypto_cipher_cast(crypto_alloc_base(alg_name
, type
, mask
));
1031 static inline struct crypto_tfm
*crypto_cipher_tfm(struct crypto_cipher
*tfm
)
1036 static inline void crypto_free_cipher(struct crypto_cipher
*tfm
)
1038 crypto_free_tfm(crypto_cipher_tfm(tfm
));
1041 static inline int crypto_has_cipher(const char *alg_name
, u32 type
, u32 mask
)
1043 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1044 type
|= CRYPTO_ALG_TYPE_CIPHER
;
1045 mask
|= CRYPTO_ALG_TYPE_MASK
;
1047 return crypto_has_alg(alg_name
, type
, mask
);
1050 static inline struct cipher_tfm
*crypto_cipher_crt(struct crypto_cipher
*tfm
)
1052 return &crypto_cipher_tfm(tfm
)->crt_cipher
;
1055 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher
*tfm
)
1057 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm
));
1060 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher
*tfm
)
1062 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm
));
1065 static inline u32
crypto_cipher_get_flags(struct crypto_cipher
*tfm
)
1067 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm
));
1070 static inline void crypto_cipher_set_flags(struct crypto_cipher
*tfm
,
1073 crypto_tfm_set_flags(crypto_cipher_tfm(tfm
), flags
);
1076 static inline void crypto_cipher_clear_flags(struct crypto_cipher
*tfm
,
1079 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm
), flags
);
1082 static inline int crypto_cipher_setkey(struct crypto_cipher
*tfm
,
1083 const u8
*key
, unsigned int keylen
)
1085 return crypto_cipher_crt(tfm
)->cit_setkey(crypto_cipher_tfm(tfm
),
1089 static inline void crypto_cipher_encrypt_one(struct crypto_cipher
*tfm
,
1090 u8
*dst
, const u8
*src
)
1092 crypto_cipher_crt(tfm
)->cit_encrypt_one(crypto_cipher_tfm(tfm
),
1096 static inline void crypto_cipher_decrypt_one(struct crypto_cipher
*tfm
,
1097 u8
*dst
, const u8
*src
)
1099 crypto_cipher_crt(tfm
)->cit_decrypt_one(crypto_cipher_tfm(tfm
),
1103 static inline struct crypto_hash
*__crypto_hash_cast(struct crypto_tfm
*tfm
)
1105 return (struct crypto_hash
*)tfm
;
1108 static inline struct crypto_hash
*crypto_hash_cast(struct crypto_tfm
*tfm
)
1110 BUG_ON((crypto_tfm_alg_type(tfm
) ^ CRYPTO_ALG_TYPE_HASH
) &
1111 CRYPTO_ALG_TYPE_HASH_MASK
);
1112 return __crypto_hash_cast(tfm
);
1115 static inline struct crypto_hash
*crypto_alloc_hash(const char *alg_name
,
1118 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1119 mask
&= ~CRYPTO_ALG_TYPE_MASK
;
1120 type
|= CRYPTO_ALG_TYPE_HASH
;
1121 mask
|= CRYPTO_ALG_TYPE_HASH_MASK
;
1123 return __crypto_hash_cast(crypto_alloc_base(alg_name
, type
, mask
));
1126 static inline struct crypto_tfm
*crypto_hash_tfm(struct crypto_hash
*tfm
)
1131 static inline void crypto_free_hash(struct crypto_hash
*tfm
)
1133 crypto_free_tfm(crypto_hash_tfm(tfm
));
1136 static inline int crypto_has_hash(const char *alg_name
, u32 type
, u32 mask
)
1138 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1139 mask
&= ~CRYPTO_ALG_TYPE_MASK
;
1140 type
|= CRYPTO_ALG_TYPE_HASH
;
1141 mask
|= CRYPTO_ALG_TYPE_HASH_MASK
;
1143 return crypto_has_alg(alg_name
, type
, mask
);
1146 static inline struct hash_tfm
*crypto_hash_crt(struct crypto_hash
*tfm
)
1148 return &crypto_hash_tfm(tfm
)->crt_hash
;
1151 static inline unsigned int crypto_hash_blocksize(struct crypto_hash
*tfm
)
1153 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm
));
1156 static inline unsigned int crypto_hash_alignmask(struct crypto_hash
*tfm
)
1158 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm
));
1161 static inline unsigned int crypto_hash_digestsize(struct crypto_hash
*tfm
)
1163 return crypto_hash_crt(tfm
)->digestsize
;
1166 static inline u32
crypto_hash_get_flags(struct crypto_hash
*tfm
)
1168 return crypto_tfm_get_flags(crypto_hash_tfm(tfm
));
1171 static inline void crypto_hash_set_flags(struct crypto_hash
*tfm
, u32 flags
)
1173 crypto_tfm_set_flags(crypto_hash_tfm(tfm
), flags
);
1176 static inline void crypto_hash_clear_flags(struct crypto_hash
*tfm
, u32 flags
)
1178 crypto_tfm_clear_flags(crypto_hash_tfm(tfm
), flags
);
1181 static inline int crypto_hash_init(struct hash_desc
*desc
)
1183 return crypto_hash_crt(desc
->tfm
)->init(desc
);
1186 static inline int crypto_hash_update(struct hash_desc
*desc
,
1187 struct scatterlist
*sg
,
1188 unsigned int nbytes
)
1190 return crypto_hash_crt(desc
->tfm
)->update(desc
, sg
, nbytes
);
1193 static inline int crypto_hash_final(struct hash_desc
*desc
, u8
*out
)
1195 return crypto_hash_crt(desc
->tfm
)->final(desc
, out
);
1198 static inline int crypto_hash_digest(struct hash_desc
*desc
,
1199 struct scatterlist
*sg
,
1200 unsigned int nbytes
, u8
*out
)
1202 return crypto_hash_crt(desc
->tfm
)->digest(desc
, sg
, nbytes
, out
);
1205 static inline int crypto_hash_setkey(struct crypto_hash
*hash
,
1206 const u8
*key
, unsigned int keylen
)
1208 return crypto_hash_crt(hash
)->setkey(hash
, key
, keylen
);
1211 static inline struct crypto_comp
*__crypto_comp_cast(struct crypto_tfm
*tfm
)
1213 return (struct crypto_comp
*)tfm
;
1216 static inline struct crypto_comp
*crypto_comp_cast(struct crypto_tfm
*tfm
)
1218 BUG_ON((crypto_tfm_alg_type(tfm
) ^ CRYPTO_ALG_TYPE_COMPRESS
) &
1219 CRYPTO_ALG_TYPE_MASK
);
1220 return __crypto_comp_cast(tfm
);
1223 static inline struct crypto_comp
*crypto_alloc_comp(const char *alg_name
,
1226 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1227 type
|= CRYPTO_ALG_TYPE_COMPRESS
;
1228 mask
|= CRYPTO_ALG_TYPE_MASK
;
1230 return __crypto_comp_cast(crypto_alloc_base(alg_name
, type
, mask
));
1233 static inline struct crypto_tfm
*crypto_comp_tfm(struct crypto_comp
*tfm
)
1238 static inline void crypto_free_comp(struct crypto_comp
*tfm
)
1240 crypto_free_tfm(crypto_comp_tfm(tfm
));
1243 static inline int crypto_has_comp(const char *alg_name
, u32 type
, u32 mask
)
1245 type
&= ~CRYPTO_ALG_TYPE_MASK
;
1246 type
|= CRYPTO_ALG_TYPE_COMPRESS
;
1247 mask
|= CRYPTO_ALG_TYPE_MASK
;
1249 return crypto_has_alg(alg_name
, type
, mask
);
1252 static inline const char *crypto_comp_name(struct crypto_comp
*tfm
)
1254 return crypto_tfm_alg_name(crypto_comp_tfm(tfm
));
1257 static inline struct compress_tfm
*crypto_comp_crt(struct crypto_comp
*tfm
)
1259 return &crypto_comp_tfm(tfm
)->crt_compress
;
1262 static inline int crypto_comp_compress(struct crypto_comp
*tfm
,
1263 const u8
*src
, unsigned int slen
,
1264 u8
*dst
, unsigned int *dlen
)
1266 return crypto_comp_crt(tfm
)->cot_compress(crypto_comp_tfm(tfm
),
1267 src
, slen
, dst
, dlen
);
1270 static inline int crypto_comp_decompress(struct crypto_comp
*tfm
,
1271 const u8
*src
, unsigned int slen
,
1272 u8
*dst
, unsigned int *dlen
)
1274 return crypto_comp_crt(tfm
)->cot_decompress(crypto_comp_tfm(tfm
),
1275 src
, slen
, dst
, dlen
);
1278 #endif /* _LINUX_CRYPTO_H */