0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef _LINUX_CRYPTO_H
0013 #define _LINUX_CRYPTO_H
0014
0015 #include <linux/atomic.h>
0016 #include <linux/kernel.h>
0017 #include <linux/list.h>
0018 #include <linux/bug.h>
0019 #include <linux/refcount.h>
0020 #include <linux/slab.h>
0021 #include <linux/completion.h>
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032 #define MODULE_ALIAS_CRYPTO(name) \
0033 __MODULE_INFO(alias, alias_userspace, name); \
0034 __MODULE_INFO(alias, alias_crypto, "crypto-" name)
0035
0036
0037
0038
0039 #define CRYPTO_ALG_TYPE_MASK 0x0000000f
0040 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001
0041 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002
0042 #define CRYPTO_ALG_TYPE_AEAD 0x00000003
0043 #define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005
0044 #define CRYPTO_ALG_TYPE_KPP 0x00000008
0045 #define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000000a
0046 #define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000000b
0047 #define CRYPTO_ALG_TYPE_RNG 0x0000000c
0048 #define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d
0049 #define CRYPTO_ALG_TYPE_HASH 0x0000000e
0050 #define CRYPTO_ALG_TYPE_SHASH 0x0000000e
0051 #define CRYPTO_ALG_TYPE_AHASH 0x0000000f
0052
0053 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e
0054 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e
0055 #define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000000e
0056
0057 #define CRYPTO_ALG_LARVAL 0x00000010
0058 #define CRYPTO_ALG_DEAD 0x00000020
0059 #define CRYPTO_ALG_DYING 0x00000040
0060 #define CRYPTO_ALG_ASYNC 0x00000080
0061
0062
0063
0064
0065
0066 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100
0067
0068
0069
0070
0071
0072
0073
0074 #define CRYPTO_ALG_TESTED 0x00000400
0075
0076
0077
0078
0079 #define CRYPTO_ALG_INSTANCE 0x00000800
0080
0081
0082
0083
0084 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000
0085
0086
0087
0088
0089
0090 #define CRYPTO_ALG_INTERNAL 0x00002000
0091
0092
0093
0094
0095
0096 #define CRYPTO_ALG_OPTIONAL_KEY 0x00004000
0097
0098
0099
0100
0101 #define CRYPTO_NOLOAD 0x00008000
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133 #define CRYPTO_ALG_ALLOCATES_MEMORY 0x00010000
0134
0135
0136
0137
0138
0139
0140
0141
0142 #define CRYPTO_ALG_FIPS_INTERNAL 0x00020000
0143
0144
0145
0146
0147 #define CRYPTO_TFM_NEED_KEY 0x00000001
0148
0149 #define CRYPTO_TFM_REQ_MASK 0x000fff00
0150 #define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS 0x00000100
0151 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200
0152 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400
0153
0154
0155
0156
0157 #define CRYPTO_MAX_ALG_NAME 128
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN
0171
0172 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN)))
0173
0174 struct scatterlist;
0175 struct crypto_async_request;
0176 struct crypto_tfm;
0177 struct crypto_type;
0178
0179 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err);
0180
0181
0182
0183
0184
0185
0186
0187
0188 struct crypto_async_request {
0189 struct list_head list;
0190 crypto_completion_t complete;
0191 void *data;
0192 struct crypto_tfm *tfm;
0193
0194 u32 flags;
0195 };
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251 struct cipher_alg {
0252 unsigned int cia_min_keysize;
0253 unsigned int cia_max_keysize;
0254 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key,
0255 unsigned int keylen);
0256 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
0257 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src);
0258 };
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271 struct compress_alg {
0272 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src,
0273 unsigned int slen, u8 *dst, unsigned int *dlen);
0274 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src,
0275 unsigned int slen, u8 *dst, unsigned int *dlen);
0276 };
0277
0278 #ifdef CONFIG_CRYPTO_STATS
0279
0280
0281
0282
0283
0284
0285
0286
0287 struct crypto_istat_aead {
0288 atomic64_t encrypt_cnt;
0289 atomic64_t encrypt_tlen;
0290 atomic64_t decrypt_cnt;
0291 atomic64_t decrypt_tlen;
0292 atomic64_t err_cnt;
0293 };
0294
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305 struct crypto_istat_akcipher {
0306 atomic64_t encrypt_cnt;
0307 atomic64_t encrypt_tlen;
0308 atomic64_t decrypt_cnt;
0309 atomic64_t decrypt_tlen;
0310 atomic64_t verify_cnt;
0311 atomic64_t sign_cnt;
0312 atomic64_t err_cnt;
0313 };
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323 struct crypto_istat_cipher {
0324 atomic64_t encrypt_cnt;
0325 atomic64_t encrypt_tlen;
0326 atomic64_t decrypt_cnt;
0327 atomic64_t decrypt_tlen;
0328 atomic64_t err_cnt;
0329 };
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339 struct crypto_istat_compress {
0340 atomic64_t compress_cnt;
0341 atomic64_t compress_tlen;
0342 atomic64_t decompress_cnt;
0343 atomic64_t decompress_tlen;
0344 atomic64_t err_cnt;
0345 };
0346
0347
0348
0349
0350
0351
0352
0353 struct crypto_istat_hash {
0354 atomic64_t hash_cnt;
0355 atomic64_t hash_tlen;
0356 atomic64_t err_cnt;
0357 };
0358
0359
0360
0361
0362
0363
0364
0365
0366 struct crypto_istat_kpp {
0367 atomic64_t setsecret_cnt;
0368 atomic64_t generate_public_key_cnt;
0369 atomic64_t compute_shared_secret_cnt;
0370 atomic64_t err_cnt;
0371 };
0372
0373
0374
0375
0376
0377
0378
0379
0380 struct crypto_istat_rng {
0381 atomic64_t generate_cnt;
0382 atomic64_t generate_tlen;
0383 atomic64_t seed_cnt;
0384 atomic64_t err_cnt;
0385 };
0386 #endif
0387
0388 #define cra_cipher cra_u.cipher
0389 #define cra_compress cra_u.compress
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478 struct crypto_alg {
0479 struct list_head cra_list;
0480 struct list_head cra_users;
0481
0482 u32 cra_flags;
0483 unsigned int cra_blocksize;
0484 unsigned int cra_ctxsize;
0485 unsigned int cra_alignmask;
0486
0487 int cra_priority;
0488 refcount_t cra_refcnt;
0489
0490 char cra_name[CRYPTO_MAX_ALG_NAME];
0491 char cra_driver_name[CRYPTO_MAX_ALG_NAME];
0492
0493 const struct crypto_type *cra_type;
0494
0495 union {
0496 struct cipher_alg cipher;
0497 struct compress_alg compress;
0498 } cra_u;
0499
0500 int (*cra_init)(struct crypto_tfm *tfm);
0501 void (*cra_exit)(struct crypto_tfm *tfm);
0502 void (*cra_destroy)(struct crypto_alg *alg);
0503
0504 struct module *cra_module;
0505
0506 #ifdef CONFIG_CRYPTO_STATS
0507 union {
0508 struct crypto_istat_aead aead;
0509 struct crypto_istat_akcipher akcipher;
0510 struct crypto_istat_cipher cipher;
0511 struct crypto_istat_compress compress;
0512 struct crypto_istat_hash hash;
0513 struct crypto_istat_rng rng;
0514 struct crypto_istat_kpp kpp;
0515 } stats;
0516 #endif
0517
0518 } CRYPTO_MINALIGN_ATTR;
0519
0520 #ifdef CONFIG_CRYPTO_STATS
0521 void crypto_stats_init(struct crypto_alg *alg);
0522 void crypto_stats_get(struct crypto_alg *alg);
0523 void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret);
0524 void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret);
0525 void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg);
0526 void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg);
0527 void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg);
0528 void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg);
0529 void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg);
0530 void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg);
0531 void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg);
0532 void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg);
0533 void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret);
0534 void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret);
0535 void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret);
0536 void crypto_stats_rng_seed(struct crypto_alg *alg, int ret);
0537 void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret);
0538 void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg);
0539 void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg);
0540 #else
0541 static inline void crypto_stats_init(struct crypto_alg *alg)
0542 {}
0543 static inline void crypto_stats_get(struct crypto_alg *alg)
0544 {}
0545 static inline void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret)
0546 {}
0547 static inline void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret)
0548 {}
0549 static inline void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg)
0550 {}
0551 static inline void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg)
0552 {}
0553 static inline void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg)
0554 {}
0555 static inline void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg)
0556 {}
0557 static inline void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg)
0558 {}
0559 static inline void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg)
0560 {}
0561 static inline void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg)
0562 {}
0563 static inline void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg)
0564 {}
0565 static inline void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret)
0566 {}
0567 static inline void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret)
0568 {}
0569 static inline void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret)
0570 {}
0571 static inline void crypto_stats_rng_seed(struct crypto_alg *alg, int ret)
0572 {}
0573 static inline void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret)
0574 {}
0575 static inline void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg)
0576 {}
0577 static inline void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg)
0578 {}
0579 #endif
0580
0581
0582
0583 struct crypto_wait {
0584 struct completion completion;
0585 int err;
0586 };
0587
0588
0589
0590
0591 #define DECLARE_CRYPTO_WAIT(_wait) \
0592 struct crypto_wait _wait = { \
0593 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 }
0594
0595
0596
0597
0598 void crypto_req_done(struct crypto_async_request *req, int err);
0599
0600 static inline int crypto_wait_req(int err, struct crypto_wait *wait)
0601 {
0602 switch (err) {
0603 case -EINPROGRESS:
0604 case -EBUSY:
0605 wait_for_completion(&wait->completion);
0606 reinit_completion(&wait->completion);
0607 err = wait->err;
0608 break;
0609 }
0610
0611 return err;
0612 }
0613
0614 static inline void crypto_init_wait(struct crypto_wait *wait)
0615 {
0616 init_completion(&wait->completion);
0617 }
0618
0619
0620
0621
0622 int crypto_register_alg(struct crypto_alg *alg);
0623 void crypto_unregister_alg(struct crypto_alg *alg);
0624 int crypto_register_algs(struct crypto_alg *algs, int count);
0625 void crypto_unregister_algs(struct crypto_alg *algs, int count);
0626
0627
0628
0629
0630 int crypto_has_alg(const char *name, u32 type, u32 mask);
0631
0632
0633
0634
0635
0636
0637
0638 struct crypto_tfm {
0639
0640 u32 crt_flags;
0641
0642 int node;
0643
0644 void (*exit)(struct crypto_tfm *tfm);
0645
0646 struct crypto_alg *__crt_alg;
0647
0648 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR;
0649 };
0650
0651 struct crypto_comp {
0652 struct crypto_tfm base;
0653 };
0654
0655
0656
0657
0658
0659 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask);
0660 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm);
0661
0662 static inline void crypto_free_tfm(struct crypto_tfm *tfm)
0663 {
0664 return crypto_destroy_tfm(tfm, tfm);
0665 }
0666
0667 int alg_test(const char *driver, const char *alg, u32 type, u32 mask);
0668
0669
0670
0671
0672 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm)
0673 {
0674 return tfm->__crt_alg->cra_name;
0675 }
0676
0677 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm)
0678 {
0679 return tfm->__crt_alg->cra_driver_name;
0680 }
0681
0682 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm)
0683 {
0684 return tfm->__crt_alg->cra_priority;
0685 }
0686
0687 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm)
0688 {
0689 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK;
0690 }
0691
0692 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm)
0693 {
0694 return tfm->__crt_alg->cra_blocksize;
0695 }
0696
0697 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm)
0698 {
0699 return tfm->__crt_alg->cra_alignmask;
0700 }
0701
0702 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm)
0703 {
0704 return tfm->crt_flags;
0705 }
0706
0707 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags)
0708 {
0709 tfm->crt_flags |= flags;
0710 }
0711
0712 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags)
0713 {
0714 tfm->crt_flags &= ~flags;
0715 }
0716
0717 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm)
0718 {
0719 return tfm->__crt_ctx;
0720 }
0721
0722 static inline unsigned int crypto_tfm_ctx_alignment(void)
0723 {
0724 struct crypto_tfm *tfm;
0725 return __alignof__(tfm->__crt_ctx);
0726 }
0727
0728 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm)
0729 {
0730 return (struct crypto_comp *)tfm;
0731 }
0732
0733 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name,
0734 u32 type, u32 mask)
0735 {
0736 type &= ~CRYPTO_ALG_TYPE_MASK;
0737 type |= CRYPTO_ALG_TYPE_COMPRESS;
0738 mask |= CRYPTO_ALG_TYPE_MASK;
0739
0740 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask));
0741 }
0742
0743 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm)
0744 {
0745 return &tfm->base;
0746 }
0747
0748 static inline void crypto_free_comp(struct crypto_comp *tfm)
0749 {
0750 crypto_free_tfm(crypto_comp_tfm(tfm));
0751 }
0752
0753 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask)
0754 {
0755 type &= ~CRYPTO_ALG_TYPE_MASK;
0756 type |= CRYPTO_ALG_TYPE_COMPRESS;
0757 mask |= CRYPTO_ALG_TYPE_MASK;
0758
0759 return crypto_has_alg(alg_name, type, mask);
0760 }
0761
0762 static inline const char *crypto_comp_name(struct crypto_comp *tfm)
0763 {
0764 return crypto_tfm_alg_name(crypto_comp_tfm(tfm));
0765 }
0766
0767 int crypto_comp_compress(struct crypto_comp *tfm,
0768 const u8 *src, unsigned int slen,
0769 u8 *dst, unsigned int *dlen);
0770
0771 int crypto_comp_decompress(struct crypto_comp *tfm,
0772 const u8 *src, unsigned int slen,
0773 u8 *dst, unsigned int *dlen);
0774
0775 #endif
0776