17 #ifndef _LINUX_CRYPTO_H
18 #define _LINUX_CRYPTO_H
21 #include <linux/kernel.h>
22 #include <linux/list.h>
24 #include <linux/slab.h>
25 #include <linux/string.h>
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 0x00000008
40 #define CRYPTO_ALG_TYPE_SHASH 0x00000009
41 #define CRYPTO_ALG_TYPE_AHASH 0x0000000a
42 #define CRYPTO_ALG_TYPE_RNG 0x0000000c
43 #define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f
45 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
46 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c
47 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c
49 #define CRYPTO_ALG_LARVAL 0x00000010
50 #define CRYPTO_ALG_DEAD 0x00000020
51 #define CRYPTO_ALG_DYING 0x00000040
52 #define CRYPTO_ALG_ASYNC 0x00000080
58 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
64 #define CRYPTO_ALG_GENIV 0x00000200
72 #define CRYPTO_ALG_TESTED 0x00000400
77 #define CRYPTO_ALG_INSTANCE 0x00000800
82 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
87 #define CRYPTO_TFM_REQ_MASK 0x000fff00
88 #define CRYPTO_TFM_RES_MASK 0xfff00000
90 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100
91 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
92 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
93 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000
94 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000
95 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000
96 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000
97 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000
102 #define CRYPTO_MAX_ALG_NAME 64
112 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
114 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
203 unsigned int keylen);
218 unsigned int keylen);
233 unsigned int keylen);
252 unsigned int keylen);
259 unsigned int slen,
u8 *
dst,
unsigned int *
dlen);
261 unsigned int slen,
u8 *
dst,
unsigned int *
dlen);
273 #define cra_ablkcipher cra_u.ablkcipher
274 #define cra_aead cra_u.aead
275 #define cra_blkcipher cra_u.blkcipher
276 #define cra_cipher cra_u.cipher
277 #define cra_compress cra_u.compress
278 #define cra_rng cra_u.rng
334 unsigned int keylen);
348 unsigned int keylen);
364 unsigned int keylen);
373 const u8 *
key,
unsigned int keylen);
384 unsigned int nsg,
u8 *
out);
386 unsigned int keylen);
392 const u8 *
src,
unsigned int slen,
395 const u8 *
src,
unsigned int slen,
405 #define crt_ablkcipher crt_u.ablkcipher
406 #define crt_aead crt_u.aead
407 #define crt_blkcipher crt_u.blkcipher
408 #define crt_cipher crt_u.cipher
409 #define crt_hash crt_u.hash
410 #define crt_compress crt_u.compress
411 #define crt_rng crt_u.rng
470 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1)
473 #define CRYPTO_MAX_ATTRS 32
505 static inline const char *crypto_tfm_alg_name(
struct crypto_tfm *tfm)
510 static inline const char *crypto_tfm_alg_driver_name(
struct crypto_tfm *tfm)
515 static inline int crypto_tfm_alg_priority(
struct crypto_tfm *tfm)
520 static inline u32 crypto_tfm_alg_type(
struct crypto_tfm *tfm)
525 static inline unsigned int crypto_tfm_alg_blocksize(
struct crypto_tfm *tfm)
530 static inline unsigned int crypto_tfm_alg_alignmask(
struct crypto_tfm *tfm)
535 static inline u32 crypto_tfm_get_flags(
struct crypto_tfm *tfm)
550 static inline void *crypto_tfm_ctx(
struct crypto_tfm *tfm)
552 return tfm->__crt_ctx;
555 static inline unsigned int crypto_tfm_ctx_alignment(
void)
558 return __alignof__(tfm->__crt_ctx);
570 static inline u32 crypto_skcipher_type(
u32 type)
577 static inline u32 crypto_skcipher_mask(
u32 mask)
587 static inline struct crypto_tfm *crypto_ablkcipher_tfm(
598 static inline int crypto_has_ablkcipher(
const char *alg_name,
u32 type,
602 crypto_skcipher_mask(mask));
608 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher;
611 static inline unsigned int crypto_ablkcipher_ivsize(
614 return crypto_ablkcipher_crt(tfm)->ivsize;
617 static inline unsigned int crypto_ablkcipher_blocksize(
620 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm));
623 static inline unsigned int crypto_ablkcipher_alignmask(
626 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm));
631 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm));
637 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags);
643 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags);
647 const u8 *
key,
unsigned int keylen)
657 return __crypto_ablkcipher_cast(req->
base.tfm);
663 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
670 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req));
674 static inline unsigned int crypto_ablkcipher_reqsize(
677 return crypto_ablkcipher_crt(tfm)->reqsize;
680 static inline void ablkcipher_request_set_tfm(
683 req->
base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->
base);
698 crypto_ablkcipher_reqsize(tfm), gfp);
701 ablkcipher_request_set_tfm(req, tfm);
711 static inline void ablkcipher_request_set_callback(
720 static inline void ablkcipher_request_set_crypt(
743 static inline void crypto_free_aead(
struct crypto_aead *tfm)
750 return &crypto_aead_tfm(tfm)->crt_aead;
753 static inline unsigned int crypto_aead_ivsize(
struct crypto_aead *tfm)
755 return crypto_aead_crt(tfm)->ivsize;
758 static inline unsigned int crypto_aead_authsize(
struct crypto_aead *tfm)
760 return crypto_aead_crt(tfm)->authsize;
763 static inline unsigned int crypto_aead_blocksize(
struct crypto_aead *tfm)
765 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm));
768 static inline unsigned int crypto_aead_alignmask(
struct crypto_aead *tfm)
770 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm));
775 return crypto_tfm_get_flags(crypto_aead_tfm(tfm));
778 static inline void crypto_aead_set_flags(
struct crypto_aead *tfm,
u32 flags)
780 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags);
783 static inline void crypto_aead_clear_flags(
struct crypto_aead *tfm,
u32 flags)
785 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags);
788 static inline int crypto_aead_setkey(
struct crypto_aead *tfm,
const u8 *key,
791 struct aead_tfm *crt = crypto_aead_crt(tfm);
800 return __crypto_aead_cast(req->
base.tfm);
803 static inline int crypto_aead_encrypt(
struct aead_request *req)
805 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req);
808 static inline int crypto_aead_decrypt(
struct aead_request *req)
810 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req);
813 static inline unsigned int crypto_aead_reqsize(
struct crypto_aead *tfm)
815 return crypto_aead_crt(tfm)->reqsize;
818 static inline void aead_request_set_tfm(
struct aead_request *req,
821 req->
base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->
base);
829 req =
kmalloc(
sizeof(*req) + crypto_aead_reqsize(tfm), gfp);
832 aead_request_set_tfm(req, tfm);
837 static inline void aead_request_free(
struct aead_request *req)
842 static inline void aead_request_set_callback(
struct aead_request *req,
852 static inline void aead_request_set_crypt(
struct aead_request *req,
863 static inline void aead_request_set_assoc(
struct aead_request *req,
881 return __crypto_blkcipher_cast(tfm);
885 const char *alg_name,
u32 type,
u32 mask)
894 static inline struct crypto_tfm *crypto_blkcipher_tfm(
905 static inline int crypto_has_blkcipher(
const char *alg_name,
u32 type,
u32 mask)
914 static inline const char *crypto_blkcipher_name(
struct crypto_blkcipher *tfm)
916 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm));
922 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher;
928 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher;
931 static inline unsigned int crypto_blkcipher_ivsize(
struct crypto_blkcipher *tfm)
933 return crypto_blkcipher_alg(tfm)->ivsize;
936 static inline unsigned int crypto_blkcipher_blocksize(
939 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm));
942 static inline unsigned int crypto_blkcipher_alignmask(
945 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm));
950 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm));
956 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags);
959 static inline void crypto_blkcipher_clear_flags(
struct crypto_blkcipher *tfm,
962 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags);
966 const u8 *key,
unsigned int keylen)
968 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm),
977 desc->
info = crypto_blkcipher_crt(desc->
tfm)->iv;
978 return crypto_blkcipher_crt(desc->
tfm)->encrypt(desc, dst, src, nbytes);
981 static inline int crypto_blkcipher_encrypt_iv(
struct blkcipher_desc *desc,
986 return crypto_blkcipher_crt(desc->
tfm)->encrypt(desc, dst, src, nbytes);
989 static inline int crypto_blkcipher_decrypt(
struct blkcipher_desc *desc,
994 desc->
info = crypto_blkcipher_crt(desc->
tfm)->iv;
995 return crypto_blkcipher_crt(desc->
tfm)->decrypt(desc, dst, src, nbytes);
998 static inline int crypto_blkcipher_decrypt_iv(
struct blkcipher_desc *desc,
1001 unsigned int nbytes)
1003 return crypto_blkcipher_crt(desc->
tfm)->decrypt(desc, dst, src, nbytes);
1007 const u8 *src,
unsigned int len)
1009 memcpy(crypto_blkcipher_crt(tfm)->
iv, src, len);
1013 u8 *dst,
unsigned int len)
1015 memcpy(dst, crypto_blkcipher_crt(tfm)->
iv, len);
1026 return __crypto_cipher_cast(tfm);
1029 static inline struct crypto_cipher *crypto_alloc_cipher(
const char *alg_name,
1044 static inline void crypto_free_cipher(
struct crypto_cipher *tfm)
1049 static inline int crypto_has_cipher(
const char *alg_name,
u32 type,
u32 mask)
1060 return &crypto_cipher_tfm(tfm)->crt_cipher;
1063 static inline unsigned int crypto_cipher_blocksize(
struct crypto_cipher *tfm)
1065 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm));
1068 static inline unsigned int crypto_cipher_alignmask(
struct crypto_cipher *tfm)
1070 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm));
1075 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm));
1078 static inline void crypto_cipher_set_flags(
struct crypto_cipher *tfm,
1081 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags);
1084 static inline void crypto_cipher_clear_flags(
struct crypto_cipher *tfm,
1087 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags);
1090 static inline int crypto_cipher_setkey(
struct crypto_cipher *tfm,
1091 const u8 *key,
unsigned int keylen)
1093 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm),
1097 static inline void crypto_cipher_encrypt_one(
struct crypto_cipher *tfm,
1098 u8 *dst,
const u8 *src)
1100 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm),
1104 static inline void crypto_cipher_decrypt_one(
struct crypto_cipher *tfm,
1105 u8 *dst,
const u8 *src)
1107 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm),
1120 return __crypto_hash_cast(tfm);
1123 static inline struct crypto_hash *crypto_alloc_hash(
const char *alg_name,
1139 static inline void crypto_free_hash(
struct crypto_hash *tfm)
1144 static inline int crypto_has_hash(
const char *alg_name,
u32 type,
u32 mask)
1156 return &crypto_hash_tfm(tfm)->crt_hash;
1159 static inline unsigned int crypto_hash_blocksize(
struct crypto_hash *tfm)
1161 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm));
1164 static inline unsigned int crypto_hash_alignmask(
struct crypto_hash *tfm)
1166 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm));
1169 static inline unsigned int crypto_hash_digestsize(
struct crypto_hash *tfm)
1171 return crypto_hash_crt(tfm)->digestsize;
1176 return crypto_tfm_get_flags(crypto_hash_tfm(tfm));
1179 static inline void crypto_hash_set_flags(
struct crypto_hash *tfm,
u32 flags)
1181 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags);
1184 static inline void crypto_hash_clear_flags(
struct crypto_hash *tfm,
u32 flags)
1186 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags);
1189 static inline int crypto_hash_init(
struct hash_desc *desc)
1191 return crypto_hash_crt(desc->
tfm)->init(desc);
1194 static inline int crypto_hash_update(
struct hash_desc *desc,
1196 unsigned int nbytes)
1198 return crypto_hash_crt(desc->
tfm)->update(desc, sg, nbytes);
1201 static inline int crypto_hash_final(
struct hash_desc *desc,
u8 *
out)
1203 return crypto_hash_crt(desc->
tfm)->final(desc, out);
1206 static inline int crypto_hash_digest(
struct hash_desc *desc,
1208 unsigned int nbytes,
u8 *out)
1210 return crypto_hash_crt(desc->
tfm)->digest(desc, sg, nbytes, out);
1214 const u8 *key,
unsigned int keylen)
1216 return crypto_hash_crt(hash)->setkey(hash, key, keylen);
1228 return __crypto_comp_cast(tfm);
1231 static inline struct crypto_comp *crypto_alloc_comp(
const char *alg_name,
1246 static inline void crypto_free_comp(
struct crypto_comp *tfm)
1251 static inline int crypto_has_comp(
const char *alg_name,
u32 type,
u32 mask)
1260 static inline const char *crypto_comp_name(
struct crypto_comp *tfm)
1262 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
1267 return &crypto_comp_tfm(tfm)->crt_compress;
1270 static inline int crypto_comp_compress(
struct crypto_comp *tfm,
1271 const u8 *src,
unsigned int slen,
1272 u8 *dst,
unsigned int *
dlen)
1274 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm),
1275 src, slen, dst, dlen);
1278 static inline int crypto_comp_decompress(
struct crypto_comp *tfm,
1279 const u8 *src,
unsigned int slen,
1280 u8 *dst,
unsigned int *dlen)
1282 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm),
1283 src, slen, dst, dlen);