diff options
Diffstat (limited to 'include/linux/crypto.h')
-rw-r--r-- | include/linux/crypto.h | 236 |
1 files changed, 232 insertions, 4 deletions
diff --git a/include/linux/crypto.h b/include/linux/crypto.h index 779aa78ee643..0de7e2ace822 100644 --- a/include/linux/crypto.h +++ b/include/linux/crypto.h | |||
@@ -56,6 +56,7 @@ | |||
56 | 56 | ||
57 | #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100 | 57 | #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100 |
58 | #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200 | 58 | #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200 |
59 | #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400 | ||
59 | #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000 | 60 | #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000 |
60 | #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000 | 61 | #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000 |
61 | #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000 | 62 | #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000 |
@@ -88,11 +89,38 @@ | |||
88 | #endif | 89 | #endif |
89 | 90 | ||
90 | struct scatterlist; | 91 | struct scatterlist; |
92 | struct crypto_ablkcipher; | ||
93 | struct crypto_async_request; | ||
91 | struct crypto_blkcipher; | 94 | struct crypto_blkcipher; |
92 | struct crypto_hash; | 95 | struct crypto_hash; |
96 | struct crypto_queue; | ||
93 | struct crypto_tfm; | 97 | struct crypto_tfm; |
94 | struct crypto_type; | 98 | struct crypto_type; |
95 | 99 | ||
100 | typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err); | ||
101 | |||
102 | struct crypto_async_request { | ||
103 | struct list_head list; | ||
104 | crypto_completion_t complete; | ||
105 | void *data; | ||
106 | struct crypto_tfm *tfm; | ||
107 | |||
108 | u32 flags; | ||
109 | }; | ||
110 | |||
111 | struct ablkcipher_request { | ||
112 | struct crypto_async_request base; | ||
113 | |||
114 | unsigned int nbytes; | ||
115 | |||
116 | void *info; | ||
117 | |||
118 | struct scatterlist *src; | ||
119 | struct scatterlist *dst; | ||
120 | |||
121 | void *__ctx[] CRYPTO_MINALIGN_ATTR; | ||
122 | }; | ||
123 | |||
96 | struct blkcipher_desc { | 124 | struct blkcipher_desc { |
97 | struct crypto_blkcipher *tfm; | 125 | struct crypto_blkcipher *tfm; |
98 | void *info; | 126 | void *info; |
@@ -116,6 +144,19 @@ struct hash_desc { | |||
116 | * Algorithms: modular crypto algorithm implementations, managed | 144 | * Algorithms: modular crypto algorithm implementations, managed |
117 | * via crypto_register_alg() and crypto_unregister_alg(). | 145 | * via crypto_register_alg() and crypto_unregister_alg(). |
118 | */ | 146 | */ |
147 | struct ablkcipher_alg { | ||
148 | int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key, | ||
149 | unsigned int keylen); | ||
150 | int (*encrypt)(struct ablkcipher_request *req); | ||
151 | int (*decrypt)(struct ablkcipher_request *req); | ||
152 | |||
153 | struct crypto_queue *queue; | ||
154 | |||
155 | unsigned int min_keysize; | ||
156 | unsigned int max_keysize; | ||
157 | unsigned int ivsize; | ||
158 | }; | ||
159 | |||
119 | struct blkcipher_alg { | 160 | struct blkcipher_alg { |
120 | int (*setkey)(struct crypto_tfm *tfm, const u8 *key, | 161 | int (*setkey)(struct crypto_tfm *tfm, const u8 *key, |
121 | unsigned int keylen); | 162 | unsigned int keylen); |
@@ -170,6 +211,7 @@ struct compress_alg { | |||
170 | unsigned int slen, u8 *dst, unsigned int *dlen); | 211 | unsigned int slen, u8 *dst, unsigned int *dlen); |
171 | }; | 212 | }; |
172 | 213 | ||
214 | #define cra_ablkcipher cra_u.ablkcipher | ||
173 | #define cra_blkcipher cra_u.blkcipher | 215 | #define cra_blkcipher cra_u.blkcipher |
174 | #define cra_cipher cra_u.cipher | 216 | #define cra_cipher cra_u.cipher |
175 | #define cra_digest cra_u.digest | 217 | #define cra_digest cra_u.digest |
@@ -194,6 +236,7 @@ struct crypto_alg { | |||
194 | const struct crypto_type *cra_type; | 236 | const struct crypto_type *cra_type; |
195 | 237 | ||
196 | union { | 238 | union { |
239 | struct ablkcipher_alg ablkcipher; | ||
197 | struct blkcipher_alg blkcipher; | 240 | struct blkcipher_alg blkcipher; |
198 | struct cipher_alg cipher; | 241 | struct cipher_alg cipher; |
199 | struct digest_alg digest; | 242 | struct digest_alg digest; |
@@ -232,6 +275,15 @@ static inline int crypto_has_alg(const char *name, u32 type, u32 mask) | |||
232 | * crypto_free_*(), as well as the various helpers below. | 275 | * crypto_free_*(), as well as the various helpers below. |
233 | */ | 276 | */ |
234 | 277 | ||
278 | struct ablkcipher_tfm { | ||
279 | int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key, | ||
280 | unsigned int keylen); | ||
281 | int (*encrypt)(struct ablkcipher_request *req); | ||
282 | int (*decrypt)(struct ablkcipher_request *req); | ||
283 | unsigned int ivsize; | ||
284 | unsigned int reqsize; | ||
285 | }; | ||
286 | |||
235 | struct blkcipher_tfm { | 287 | struct blkcipher_tfm { |
236 | void *iv; | 288 | void *iv; |
237 | int (*setkey)(struct crypto_tfm *tfm, const u8 *key, | 289 | int (*setkey)(struct crypto_tfm *tfm, const u8 *key, |
@@ -290,6 +342,7 @@ struct compress_tfm { | |||
290 | u8 *dst, unsigned int *dlen); | 342 | u8 *dst, unsigned int *dlen); |
291 | }; | 343 | }; |
292 | 344 | ||
345 | #define crt_ablkcipher crt_u.ablkcipher | ||
293 | #define crt_blkcipher crt_u.blkcipher | 346 | #define crt_blkcipher crt_u.blkcipher |
294 | #define crt_cipher crt_u.cipher | 347 | #define crt_cipher crt_u.cipher |
295 | #define crt_hash crt_u.hash | 348 | #define crt_hash crt_u.hash |
@@ -300,6 +353,7 @@ struct crypto_tfm { | |||
300 | u32 crt_flags; | 353 | u32 crt_flags; |
301 | 354 | ||
302 | union { | 355 | union { |
356 | struct ablkcipher_tfm ablkcipher; | ||
303 | struct blkcipher_tfm blkcipher; | 357 | struct blkcipher_tfm blkcipher; |
304 | struct cipher_tfm cipher; | 358 | struct cipher_tfm cipher; |
305 | struct hash_tfm hash; | 359 | struct hash_tfm hash; |
@@ -311,6 +365,10 @@ struct crypto_tfm { | |||
311 | void *__crt_ctx[] CRYPTO_MINALIGN_ATTR; | 365 | void *__crt_ctx[] CRYPTO_MINALIGN_ATTR; |
312 | }; | 366 | }; |
313 | 367 | ||
368 | struct crypto_ablkcipher { | ||
369 | struct crypto_tfm base; | ||
370 | }; | ||
371 | |||
314 | struct crypto_blkcipher { | 372 | struct crypto_blkcipher { |
315 | struct crypto_tfm base; | 373 | struct crypto_tfm base; |
316 | }; | 374 | }; |
@@ -330,12 +388,21 @@ struct crypto_hash { | |||
330 | enum { | 388 | enum { |
331 | CRYPTOA_UNSPEC, | 389 | CRYPTOA_UNSPEC, |
332 | CRYPTOA_ALG, | 390 | CRYPTOA_ALG, |
391 | CRYPTOA_TYPE, | ||
392 | __CRYPTOA_MAX, | ||
333 | }; | 393 | }; |
334 | 394 | ||
395 | #define CRYPTOA_MAX (__CRYPTOA_MAX - 1) | ||
396 | |||
335 | struct crypto_attr_alg { | 397 | struct crypto_attr_alg { |
336 | char name[CRYPTO_MAX_ALG_NAME]; | 398 | char name[CRYPTO_MAX_ALG_NAME]; |
337 | }; | 399 | }; |
338 | 400 | ||
401 | struct crypto_attr_type { | ||
402 | u32 type; | ||
403 | u32 mask; | ||
404 | }; | ||
405 | |||
339 | /* | 406 | /* |
340 | * Transform user interface. | 407 | * Transform user interface. |
341 | */ | 408 | */ |
@@ -411,6 +478,167 @@ static inline unsigned int crypto_tfm_ctx_alignment(void) | |||
411 | /* | 478 | /* |
412 | * API wrappers. | 479 | * API wrappers. |
413 | */ | 480 | */ |
481 | static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast( | ||
482 | struct crypto_tfm *tfm) | ||
483 | { | ||
484 | return (struct crypto_ablkcipher *)tfm; | ||
485 | } | ||
486 | |||
487 | static inline struct crypto_ablkcipher *crypto_alloc_ablkcipher( | ||
488 | const char *alg_name, u32 type, u32 mask) | ||
489 | { | ||
490 | type &= ~CRYPTO_ALG_TYPE_MASK; | ||
491 | type |= CRYPTO_ALG_TYPE_BLKCIPHER; | ||
492 | mask |= CRYPTO_ALG_TYPE_MASK; | ||
493 | |||
494 | return __crypto_ablkcipher_cast( | ||
495 | crypto_alloc_base(alg_name, type, mask)); | ||
496 | } | ||
497 | |||
498 | static inline struct crypto_tfm *crypto_ablkcipher_tfm( | ||
499 | struct crypto_ablkcipher *tfm) | ||
500 | { | ||
501 | return &tfm->base; | ||
502 | } | ||
503 | |||
504 | static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm) | ||
505 | { | ||
506 | crypto_free_tfm(crypto_ablkcipher_tfm(tfm)); | ||
507 | } | ||
508 | |||
509 | static inline int crypto_has_ablkcipher(const char *alg_name, u32 type, | ||
510 | u32 mask) | ||
511 | { | ||
512 | type &= ~CRYPTO_ALG_TYPE_MASK; | ||
513 | type |= CRYPTO_ALG_TYPE_BLKCIPHER; | ||
514 | mask |= CRYPTO_ALG_TYPE_MASK; | ||
515 | |||
516 | return crypto_has_alg(alg_name, type, mask); | ||
517 | } | ||
518 | |||
519 | static inline struct ablkcipher_tfm *crypto_ablkcipher_crt( | ||
520 | struct crypto_ablkcipher *tfm) | ||
521 | { | ||
522 | return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher; | ||
523 | } | ||
524 | |||
525 | static inline unsigned int crypto_ablkcipher_ivsize( | ||
526 | struct crypto_ablkcipher *tfm) | ||
527 | { | ||
528 | return crypto_ablkcipher_crt(tfm)->ivsize; | ||
529 | } | ||
530 | |||
531 | static inline unsigned int crypto_ablkcipher_blocksize( | ||
532 | struct crypto_ablkcipher *tfm) | ||
533 | { | ||
534 | return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm)); | ||
535 | } | ||
536 | |||
537 | static inline unsigned int crypto_ablkcipher_alignmask( | ||
538 | struct crypto_ablkcipher *tfm) | ||
539 | { | ||
540 | return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm)); | ||
541 | } | ||
542 | |||
543 | static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm) | ||
544 | { | ||
545 | return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm)); | ||
546 | } | ||
547 | |||
548 | static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm, | ||
549 | u32 flags) | ||
550 | { | ||
551 | crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags); | ||
552 | } | ||
553 | |||
554 | static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm, | ||
555 | u32 flags) | ||
556 | { | ||
557 | crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags); | ||
558 | } | ||
559 | |||
560 | static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm, | ||
561 | const u8 *key, unsigned int keylen) | ||
562 | { | ||
563 | return crypto_ablkcipher_crt(tfm)->setkey(tfm, key, keylen); | ||
564 | } | ||
565 | |||
566 | static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm( | ||
567 | struct ablkcipher_request *req) | ||
568 | { | ||
569 | return __crypto_ablkcipher_cast(req->base.tfm); | ||
570 | } | ||
571 | |||
572 | static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req) | ||
573 | { | ||
574 | struct ablkcipher_tfm *crt = | ||
575 | crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req)); | ||
576 | return crt->encrypt(req); | ||
577 | } | ||
578 | |||
579 | static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req) | ||
580 | { | ||
581 | struct ablkcipher_tfm *crt = | ||
582 | crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req)); | ||
583 | return crt->decrypt(req); | ||
584 | } | ||
585 | |||
586 | static inline int crypto_ablkcipher_reqsize(struct crypto_ablkcipher *tfm) | ||
587 | { | ||
588 | return crypto_ablkcipher_crt(tfm)->reqsize; | ||
589 | } | ||
590 | |||
591 | static inline void ablkcipher_request_set_tfm( | ||
592 | struct ablkcipher_request *req, struct crypto_ablkcipher *tfm) | ||
593 | { | ||
594 | req->base.tfm = crypto_ablkcipher_tfm(tfm); | ||
595 | } | ||
596 | |||
597 | static inline struct ablkcipher_request *ablkcipher_request_cast( | ||
598 | struct crypto_async_request *req) | ||
599 | { | ||
600 | return container_of(req, struct ablkcipher_request, base); | ||
601 | } | ||
602 | |||
603 | static inline struct ablkcipher_request *ablkcipher_request_alloc( | ||
604 | struct crypto_ablkcipher *tfm, gfp_t gfp) | ||
605 | { | ||
606 | struct ablkcipher_request *req; | ||
607 | |||
608 | req = kmalloc(sizeof(struct ablkcipher_request) + | ||
609 | crypto_ablkcipher_reqsize(tfm), gfp); | ||
610 | |||
611 | if (likely(req)) | ||
612 | ablkcipher_request_set_tfm(req, tfm); | ||
613 | |||
614 | return req; | ||
615 | } | ||
616 | |||
617 | static inline void ablkcipher_request_free(struct ablkcipher_request *req) | ||
618 | { | ||
619 | kfree(req); | ||
620 | } | ||
621 | |||
622 | static inline void ablkcipher_request_set_callback( | ||
623 | struct ablkcipher_request *req, | ||
624 | u32 flags, crypto_completion_t complete, void *data) | ||
625 | { | ||
626 | req->base.complete = complete; | ||
627 | req->base.data = data; | ||
628 | req->base.flags = flags; | ||
629 | } | ||
630 | |||
631 | static inline void ablkcipher_request_set_crypt( | ||
632 | struct ablkcipher_request *req, | ||
633 | struct scatterlist *src, struct scatterlist *dst, | ||
634 | unsigned int nbytes, void *iv) | ||
635 | { | ||
636 | req->src = src; | ||
637 | req->dst = dst; | ||
638 | req->nbytes = nbytes; | ||
639 | req->info = iv; | ||
640 | } | ||
641 | |||
414 | static inline struct crypto_blkcipher *__crypto_blkcipher_cast( | 642 | static inline struct crypto_blkcipher *__crypto_blkcipher_cast( |
415 | struct crypto_tfm *tfm) | 643 | struct crypto_tfm *tfm) |
416 | { | 644 | { |
@@ -427,9 +655,9 @@ static inline struct crypto_blkcipher *crypto_blkcipher_cast( | |||
427 | static inline struct crypto_blkcipher *crypto_alloc_blkcipher( | 655 | static inline struct crypto_blkcipher *crypto_alloc_blkcipher( |
428 | const char *alg_name, u32 type, u32 mask) | 656 | const char *alg_name, u32 type, u32 mask) |
429 | { | 657 | { |
430 | type &= ~CRYPTO_ALG_TYPE_MASK; | 658 | type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC); |
431 | type |= CRYPTO_ALG_TYPE_BLKCIPHER; | 659 | type |= CRYPTO_ALG_TYPE_BLKCIPHER; |
432 | mask |= CRYPTO_ALG_TYPE_MASK; | 660 | mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC; |
433 | 661 | ||
434 | return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask)); | 662 | return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask)); |
435 | } | 663 | } |
@@ -447,9 +675,9 @@ static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm) | |||
447 | 675 | ||
448 | static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask) | 676 | static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask) |
449 | { | 677 | { |
450 | type &= ~CRYPTO_ALG_TYPE_MASK; | 678 | type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC); |
451 | type |= CRYPTO_ALG_TYPE_BLKCIPHER; | 679 | type |= CRYPTO_ALG_TYPE_BLKCIPHER; |
452 | mask |= CRYPTO_ALG_TYPE_MASK; | 680 | mask |= CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC; |
453 | 681 | ||
454 | return crypto_has_alg(alg_name, type, mask); | 682 | return crypto_has_alg(alg_name, type, mask); |
455 | } | 683 | } |