1 /* 2 * Scatterlist Cryptographic API. 3 * 4 * Copyright (c) 2002 James Morris <[email protected]> 5 * Copyright (c) 2002 David S. Miller ([email protected]) 6 * Copyright (c) 2005 Herbert Xu <[email protected]> 7 * 8 * Portions derived from Cryptoapi, by Alexander Kjeldaas <[email protected]> 9 * and Nettle, by Niels Möller. 10 * 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) 14 * any later version. 15 * 16 */ 17 #ifndef _LINUX_CRYPTO_H 18 #define _LINUX_CRYPTO_H 19 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> 26 27 /* 28 * Algorithm masks and types. 29 */ 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 43 44 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e 45 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c 46 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c 47 48 #define CRYPTO_ALG_LARVAL 0x00000010 49 #define CRYPTO_ALG_DEAD 0x00000020 50 #define CRYPTO_ALG_DYING 0x00000040 51 #define CRYPTO_ALG_ASYNC 0x00000080 52 53 /* 54 * Set this bit if and only if the algorithm requires another algorithm of 55 * the same type to handle corner cases. 56 */ 57 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100 58 59 /* 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. 62 */ 63 #define CRYPTO_ALG_GENIV 0x00000200 64 65 /* 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 68 * to have passed. 69 */ 70 71 #define CRYPTO_ALG_TESTED 0x00000400 72 73 /* 74 * Set if the algorithm is an instance that is build from templates. 75 */ 76 #define CRYPTO_ALG_INSTANCE 0x00000800 77 78 /* 79 * Transform masks and values (for crt_flags). 80 */ 81 #define CRYPTO_TFM_REQ_MASK 0x000fff00 82 #define CRYPTO_TFM_RES_MASK 0xfff00000 83 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 92 93 /* 94 * Miscellaneous stuff. 95 */ 96 #define CRYPTO_MAX_ALG_NAME 64 97 98 /* 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. 105 */ 106 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN 107 108 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN))) 109 110 struct scatterlist; 111 struct crypto_ablkcipher; 112 struct crypto_async_request; 113 struct crypto_aead; 114 struct crypto_blkcipher; 115 struct crypto_hash; 116 struct crypto_rng; 117 struct crypto_tfm; 118 struct crypto_type; 119 struct aead_givcrypt_request; 120 struct skcipher_givcrypt_request; 121 122 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err); 123 124 struct crypto_async_request { 125 struct list_head list; 126 crypto_completion_t complete; 127 void *data; 128 struct crypto_tfm *tfm; 129 130 u32 flags; 131 }; 132 133 struct ablkcipher_request { 134 struct crypto_async_request base; 135 136 unsigned int nbytes; 137 138 void *info; 139 140 struct scatterlist *src; 141 struct scatterlist *dst; 142 143 void *__ctx[] CRYPTO_MINALIGN_ATTR; 144 }; 145 146 /** 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 153 * @src: Source data 154 * @dst: Destination data 155 * @__ctx: Start of private context data 156 */ 157 struct aead_request { 158 struct crypto_async_request base; 159 160 unsigned int assoclen; 161 unsigned int cryptlen; 162 163 u8 *iv; 164 165 struct scatterlist *assoc; 166 struct scatterlist *src; 167 struct scatterlist *dst; 168 169 void *__ctx[] CRYPTO_MINALIGN_ATTR; 170 }; 171 172 struct blkcipher_desc { 173 struct crypto_blkcipher *tfm; 174 void *info; 175 u32 flags; 176 }; 177 178 struct cipher_desc { 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); 183 void *info; 184 }; 185 186 struct hash_desc { 187 struct crypto_hash *tfm; 188 u32 flags; 189 }; 190 191 /* 192 * Algorithms: modular crypto algorithm implementations, managed 193 * via crypto_register_alg() and crypto_unregister_alg(). 194 */ 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); 202 203 const char *geniv; 204 205 unsigned int min_keysize; 206 unsigned int max_keysize; 207 unsigned int ivsize; 208 }; 209 210 struct aead_alg { 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); 218 219 const char *geniv; 220 221 unsigned int ivsize; 222 unsigned int maxauthsize; 223 }; 224 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); 234 235 const char *geniv; 236 237 unsigned int min_keysize; 238 unsigned int max_keysize; 239 unsigned int ivsize; 240 }; 241 242 struct cipher_alg { 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); 249 }; 250 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); 256 }; 257 258 struct rng_alg { 259 int (*rng_make_random)(struct crypto_rng *tfm, u8 *rdata, 260 unsigned int dlen); 261 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen); 262 263 unsigned int seedsize; 264 }; 265 266 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 273 274 struct crypto_alg { 275 struct list_head cra_list; 276 struct list_head cra_users; 277 278 u32 cra_flags; 279 unsigned int cra_blocksize; 280 unsigned int cra_ctxsize; 281 unsigned int cra_alignmask; 282 283 int cra_priority; 284 atomic_t cra_refcnt; 285 286 char cra_name[CRYPTO_MAX_ALG_NAME]; 287 char cra_driver_name[CRYPTO_MAX_ALG_NAME]; 288 289 const struct crypto_type *cra_type; 290 291 union { 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; 297 struct rng_alg rng; 298 } cra_u; 299 300 int (*cra_init)(struct crypto_tfm *tfm); 301 void (*cra_exit)(struct crypto_tfm *tfm); 302 void (*cra_destroy)(struct crypto_alg *alg); 303 304 struct module *cra_module; 305 }; 306 307 /* 308 * Algorithm registration interface. 309 */ 310 int crypto_register_alg(struct crypto_alg *alg); 311 int crypto_unregister_alg(struct crypto_alg *alg); 312 313 /* 314 * Algorithm query interface. 315 */ 316 int crypto_has_alg(const char *name, u32 type, u32 mask); 317 318 /* 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. 322 */ 323 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); 331 332 struct crypto_ablkcipher *base; 333 334 unsigned int ivsize; 335 unsigned int reqsize; 336 }; 337 338 struct aead_tfm { 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); 345 346 struct crypto_aead *base; 347 348 unsigned int ivsize; 349 unsigned int authsize; 350 unsigned int reqsize; 351 }; 352 353 struct blkcipher_tfm { 354 void *iv; 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); 361 }; 362 363 struct cipher_tfm { 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); 368 }; 369 370 struct hash_tfm { 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; 380 }; 381 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); 389 }; 390 391 struct rng_tfm { 392 int (*rng_gen_random)(struct crypto_rng *tfm, u8 *rdata, 393 unsigned int dlen); 394 int (*rng_reset)(struct crypto_rng *tfm, u8 *seed, unsigned int slen); 395 }; 396 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 404 405 struct crypto_tfm { 406 407 u32 crt_flags; 408 409 union { 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; 416 struct rng_tfm rng; 417 } crt_u; 418 419 void (*exit)(struct crypto_tfm *tfm); 420 421 struct crypto_alg *__crt_alg; 422 423 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR; 424 }; 425 426 struct crypto_ablkcipher { 427 struct crypto_tfm base; 428 }; 429 430 struct crypto_aead { 431 struct crypto_tfm base; 432 }; 433 434 struct crypto_blkcipher { 435 struct crypto_tfm base; 436 }; 437 438 struct crypto_cipher { 439 struct crypto_tfm base; 440 }; 441 442 struct crypto_comp { 443 struct crypto_tfm base; 444 }; 445 446 struct crypto_hash { 447 struct crypto_tfm base; 448 }; 449 450 struct crypto_rng { 451 struct crypto_tfm base; 452 }; 453 454 enum { 455 CRYPTOA_UNSPEC, 456 CRYPTOA_ALG, 457 CRYPTOA_TYPE, 458 CRYPTOA_U32, 459 __CRYPTOA_MAX, 460 }; 461 462 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1) 463 464 /* Maximum number of (rtattr) parameters for each template. */ 465 #define CRYPTO_MAX_ATTRS 32 466 467 struct crypto_attr_alg { 468 char name[CRYPTO_MAX_ALG_NAME]; 469 }; 470 471 struct crypto_attr_type { 472 u32 type; 473 u32 mask; 474 }; 475 476 struct crypto_attr_u32 { 477 u32 num; 478 }; 479 480 /* 481 * Transform user interface. 482 */ 483 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); 486 487 static inline void crypto_free_tfm(struct crypto_tfm *tfm) 488 { 489 return crypto_destroy_tfm(tfm, tfm); 490 } 491 492 int alg_test(const char *driver, const char *alg, u32 type, u32 mask); 493 494 /* 495 * Transform helpers which query the underlying algorithm. 496 */ 497 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm) 498 { 499 return tfm->__crt_alg->cra_name; 500 } 501 502 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm) 503 { 504 return tfm->__crt_alg->cra_driver_name; 505 } 506 507 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm) 508 { 509 return tfm->__crt_alg->cra_priority; 510 } 511 512 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm) 513 { 514 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK; 515 } 516 517 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm) 518 { 519 return tfm->__crt_alg->cra_blocksize; 520 } 521 522 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm) 523 { 524 return tfm->__crt_alg->cra_alignmask; 525 } 526 527 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm) 528 { 529 return tfm->crt_flags; 530 } 531 532 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags) 533 { 534 tfm->crt_flags |= flags; 535 } 536 537 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags) 538 { 539 tfm->crt_flags &= ~flags; 540 } 541 542 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm) 543 { 544 return tfm->__crt_ctx; 545 } 546 547 static inline unsigned int crypto_tfm_ctx_alignment(void) 548 { 549 struct crypto_tfm *tfm; 550 return __alignof__(tfm->__crt_ctx); 551 } 552 553 /* 554 * API wrappers. 555 */ 556 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast( 557 struct crypto_tfm *tfm) 558 { 559 return (struct crypto_ablkcipher *)tfm; 560 } 561 562 static inline u32 crypto_skcipher_type(u32 type) 563 { 564 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV); 565 type |= CRYPTO_ALG_TYPE_BLKCIPHER; 566 return type; 567 } 568 569 static inline u32 crypto_skcipher_mask(u32 mask) 570 { 571 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV); 572 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK; 573 return mask; 574 } 575 576 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name, 577 u32 type, u32 mask); 578 579 static inline struct crypto_tfm *crypto_ablkcipher_tfm( 580 struct crypto_ablkcipher *tfm) 581 { 582 return &tfm->base; 583 } 584 585 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm) 586 { 587 crypto_free_tfm(crypto_ablkcipher_tfm(tfm)); 588 } 589 590 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type, 591 u32 mask) 592 { 593 return crypto_has_alg(alg_name, crypto_skcipher_type(type), 594 crypto_skcipher_mask(mask)); 595 } 596 597 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt( 598 struct crypto_ablkcipher *tfm) 599 { 600 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher; 601 } 602 603 static inline unsigned int crypto_ablkcipher_ivsize( 604 struct crypto_ablkcipher *tfm) 605 { 606 return crypto_ablkcipher_crt(tfm)->ivsize; 607 } 608 609 static inline unsigned int crypto_ablkcipher_blocksize( 610 struct crypto_ablkcipher *tfm) 611 { 612 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm)); 613 } 614 615 static inline unsigned int crypto_ablkcipher_alignmask( 616 struct crypto_ablkcipher *tfm) 617 { 618 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm)); 619 } 620 621 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm) 622 { 623 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm)); 624 } 625 626 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm, 627 u32 flags) 628 { 629 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags); 630 } 631 632 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm, 633 u32 flags) 634 { 635 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags); 636 } 637 638 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm, 639 const u8 *key, unsigned int keylen) 640 { 641 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm); 642 643 return crt->setkey(crt->base, key, keylen); 644 } 645 646 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm( 647 struct ablkcipher_request *req) 648 { 649 return __crypto_ablkcipher_cast(req->base.tfm); 650 } 651 652 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req) 653 { 654 struct ablkcipher_tfm *crt = 655 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req)); 656 return crt->encrypt(req); 657 } 658 659 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req) 660 { 661 struct ablkcipher_tfm *crt = 662 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req)); 663 return crt->decrypt(req); 664 } 665 666 static inline unsigned int crypto_ablkcipher_reqsize( 667 struct crypto_ablkcipher *tfm) 668 { 669 return crypto_ablkcipher_crt(tfm)->reqsize; 670 } 671 672 static inline void ablkcipher_request_set_tfm( 673 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm) 674 { 675 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base); 676 } 677 678 static inline struct ablkcipher_request *ablkcipher_request_cast( 679 struct crypto_async_request *req) 680 { 681 return container_of(req, struct ablkcipher_request, base); 682 } 683 684 static inline struct ablkcipher_request *ablkcipher_request_alloc( 685 struct crypto_ablkcipher *tfm, gfp_t gfp) 686 { 687 struct ablkcipher_request *req; 688 689 req = kmalloc(sizeof(struct ablkcipher_request) + 690 crypto_ablkcipher_reqsize(tfm), gfp); 691 692 if (likely(req)) 693 ablkcipher_request_set_tfm(req, tfm); 694 695 return req; 696 } 697 698 static inline void ablkcipher_request_free(struct ablkcipher_request *req) 699 { 700 kzfree(req); 701 } 702 703 static inline void ablkcipher_request_set_callback( 704 struct ablkcipher_request *req, 705 u32 flags, crypto_completion_t complete, void *data) 706 { 707 req->base.complete = complete; 708 req->base.data = data; 709 req->base.flags = flags; 710 } 711 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) 716 { 717 req->src = src; 718 req->dst = dst; 719 req->nbytes = nbytes; 720 req->info = iv; 721 } 722 723 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm) 724 { 725 return (struct crypto_aead *)tfm; 726 } 727 728 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask); 729 730 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm) 731 { 732 return &tfm->base; 733 } 734 735 static inline void crypto_free_aead(struct crypto_aead *tfm) 736 { 737 crypto_free_tfm(crypto_aead_tfm(tfm)); 738 } 739 740 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm) 741 { 742 return &crypto_aead_tfm(tfm)->crt_aead; 743 } 744 745 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm) 746 { 747 return crypto_aead_crt(tfm)->ivsize; 748 } 749 750 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm) 751 { 752 return crypto_aead_crt(tfm)->authsize; 753 } 754 755 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm) 756 { 757 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm)); 758 } 759 760 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm) 761 { 762 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm)); 763 } 764 765 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm) 766 { 767 return crypto_tfm_get_flags(crypto_aead_tfm(tfm)); 768 } 769 770 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags) 771 { 772 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags); 773 } 774 775 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags) 776 { 777 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags); 778 } 779 780 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key, 781 unsigned int keylen) 782 { 783 struct aead_tfm *crt = crypto_aead_crt(tfm); 784 785 return crt->setkey(crt->base, key, keylen); 786 } 787 788 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize); 789 790 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req) 791 { 792 return __crypto_aead_cast(req->base.tfm); 793 } 794 795 static inline int crypto_aead_encrypt(struct aead_request *req) 796 { 797 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req); 798 } 799 800 static inline int crypto_aead_decrypt(struct aead_request *req) 801 { 802 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req); 803 } 804 805 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm) 806 { 807 return crypto_aead_crt(tfm)->reqsize; 808 } 809 810 static inline void aead_request_set_tfm(struct aead_request *req, 811 struct crypto_aead *tfm) 812 { 813 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base); 814 } 815 816 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm, 817 gfp_t gfp) 818 { 819 struct aead_request *req; 820 821 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp); 822 823 if (likely(req)) 824 aead_request_set_tfm(req, tfm); 825 826 return req; 827 } 828 829 static inline void aead_request_free(struct aead_request *req) 830 { 831 kzfree(req); 832 } 833 834 static inline void aead_request_set_callback(struct aead_request *req, 835 u32 flags, 836 crypto_completion_t complete, 837 void *data) 838 { 839 req->base.complete = complete; 840 req->base.data = data; 841 req->base.flags = flags; 842 } 843 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) 848 { 849 req->src = src; 850 req->dst = dst; 851 req->cryptlen = cryptlen; 852 req->iv = iv; 853 } 854 855 static inline void aead_request_set_assoc(struct aead_request *req, 856 struct scatterlist *assoc, 857 unsigned int assoclen) 858 { 859 req->assoc = assoc; 860 req->assoclen = assoclen; 861 } 862 863 static inline struct crypto_blkcipher *__crypto_blkcipher_cast( 864 struct crypto_tfm *tfm) 865 { 866 return (struct crypto_blkcipher *)tfm; 867 } 868 869 static inline struct crypto_blkcipher *crypto_blkcipher_cast( 870 struct crypto_tfm *tfm) 871 { 872 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER); 873 return __crypto_blkcipher_cast(tfm); 874 } 875 876 static inline struct crypto_blkcipher *crypto_alloc_blkcipher( 877 const char *alg_name, u32 type, u32 mask) 878 { 879 type &= ~CRYPTO_ALG_TYPE_MASK; 880 type |= CRYPTO_ALG_TYPE_BLKCIPHER; 881 mask |= CRYPTO_ALG_TYPE_MASK; 882 883 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask)); 884 } 885 886 static inline struct crypto_tfm *crypto_blkcipher_tfm( 887 struct crypto_blkcipher *tfm) 888 { 889 return &tfm->base; 890 } 891 892 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm) 893 { 894 crypto_free_tfm(crypto_blkcipher_tfm(tfm)); 895 } 896 897 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask) 898 { 899 type &= ~CRYPTO_ALG_TYPE_MASK; 900 type |= CRYPTO_ALG_TYPE_BLKCIPHER; 901 mask |= CRYPTO_ALG_TYPE_MASK; 902 903 return crypto_has_alg(alg_name, type, mask); 904 } 905 906 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm) 907 { 908 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm)); 909 } 910 911 static inline struct blkcipher_tfm *crypto_blkcipher_crt( 912 struct crypto_blkcipher *tfm) 913 { 914 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher; 915 } 916 917 static inline struct blkcipher_alg *crypto_blkcipher_alg( 918 struct crypto_blkcipher *tfm) 919 { 920 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher; 921 } 922 923 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm) 924 { 925 return crypto_blkcipher_alg(tfm)->ivsize; 926 } 927 928 static inline unsigned int crypto_blkcipher_blocksize( 929 struct crypto_blkcipher *tfm) 930 { 931 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm)); 932 } 933 934 static inline unsigned int crypto_blkcipher_alignmask( 935 struct crypto_blkcipher *tfm) 936 { 937 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm)); 938 } 939 940 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm) 941 { 942 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm)); 943 } 944 945 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm, 946 u32 flags) 947 { 948 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags); 949 } 950 951 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm, 952 u32 flags) 953 { 954 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags); 955 } 956 957 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm, 958 const u8 *key, unsigned int keylen) 959 { 960 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm), 961 key, keylen); 962 } 963 964 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc, 965 struct scatterlist *dst, 966 struct scatterlist *src, 967 unsigned int nbytes) 968 { 969 desc->info = crypto_blkcipher_crt(desc->tfm)->iv; 970 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes); 971 } 972 973 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc, 974 struct scatterlist *dst, 975 struct scatterlist *src, 976 unsigned int nbytes) 977 { 978 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes); 979 } 980 981 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc, 982 struct scatterlist *dst, 983 struct scatterlist *src, 984 unsigned int nbytes) 985 { 986 desc->info = crypto_blkcipher_crt(desc->tfm)->iv; 987 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes); 988 } 989 990 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc, 991 struct scatterlist *dst, 992 struct scatterlist *src, 993 unsigned int nbytes) 994 { 995 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes); 996 } 997 998 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm, 999 const u8 *src, unsigned int len) 1000 { 1001 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len); 1002 } 1003 1004 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm, 1005 u8 *dst, unsigned int len) 1006 { 1007 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len); 1008 } 1009 1010 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm) 1011 { 1012 return (struct crypto_cipher *)tfm; 1013 } 1014 1015 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm) 1016 { 1017 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); 1018 return __crypto_cipher_cast(tfm); 1019 } 1020 1021 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name, 1022 u32 type, u32 mask) 1023 { 1024 type &= ~CRYPTO_ALG_TYPE_MASK; 1025 type |= CRYPTO_ALG_TYPE_CIPHER; 1026 mask |= CRYPTO_ALG_TYPE_MASK; 1027 1028 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask)); 1029 } 1030 1031 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm) 1032 { 1033 return &tfm->base; 1034 } 1035 1036 static inline void crypto_free_cipher(struct crypto_cipher *tfm) 1037 { 1038 crypto_free_tfm(crypto_cipher_tfm(tfm)); 1039 } 1040 1041 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask) 1042 { 1043 type &= ~CRYPTO_ALG_TYPE_MASK; 1044 type |= CRYPTO_ALG_TYPE_CIPHER; 1045 mask |= CRYPTO_ALG_TYPE_MASK; 1046 1047 return crypto_has_alg(alg_name, type, mask); 1048 } 1049 1050 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm) 1051 { 1052 return &crypto_cipher_tfm(tfm)->crt_cipher; 1053 } 1054 1055 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm) 1056 { 1057 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm)); 1058 } 1059 1060 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm) 1061 { 1062 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm)); 1063 } 1064 1065 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm) 1066 { 1067 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm)); 1068 } 1069 1070 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm, 1071 u32 flags) 1072 { 1073 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags); 1074 } 1075 1076 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm, 1077 u32 flags) 1078 { 1079 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags); 1080 } 1081 1082 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm, 1083 const u8 *key, unsigned int keylen) 1084 { 1085 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm), 1086 key, keylen); 1087 } 1088 1089 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm, 1090 u8 *dst, const u8 *src) 1091 { 1092 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm), 1093 dst, src); 1094 } 1095 1096 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm, 1097 u8 *dst, const u8 *src) 1098 { 1099 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm), 1100 dst, src); 1101 } 1102 1103 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm) 1104 { 1105 return (struct crypto_hash *)tfm; 1106 } 1107 1108 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm) 1109 { 1110 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) & 1111 CRYPTO_ALG_TYPE_HASH_MASK); 1112 return __crypto_hash_cast(tfm); 1113 } 1114 1115 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name, 1116 u32 type, u32 mask) 1117 { 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; 1122 1123 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask)); 1124 } 1125 1126 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm) 1127 { 1128 return &tfm->base; 1129 } 1130 1131 static inline void crypto_free_hash(struct crypto_hash *tfm) 1132 { 1133 crypto_free_tfm(crypto_hash_tfm(tfm)); 1134 } 1135 1136 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask) 1137 { 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; 1142 1143 return crypto_has_alg(alg_name, type, mask); 1144 } 1145 1146 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm) 1147 { 1148 return &crypto_hash_tfm(tfm)->crt_hash; 1149 } 1150 1151 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm) 1152 { 1153 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm)); 1154 } 1155 1156 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm) 1157 { 1158 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm)); 1159 } 1160 1161 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm) 1162 { 1163 return crypto_hash_crt(tfm)->digestsize; 1164 } 1165 1166 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm) 1167 { 1168 return crypto_tfm_get_flags(crypto_hash_tfm(tfm)); 1169 } 1170 1171 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags) 1172 { 1173 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags); 1174 } 1175 1176 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags) 1177 { 1178 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags); 1179 } 1180 1181 static inline int crypto_hash_init(struct hash_desc *desc) 1182 { 1183 return crypto_hash_crt(desc->tfm)->init(desc); 1184 } 1185 1186 static inline int crypto_hash_update(struct hash_desc *desc, 1187 struct scatterlist *sg, 1188 unsigned int nbytes) 1189 { 1190 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes); 1191 } 1192 1193 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out) 1194 { 1195 return crypto_hash_crt(desc->tfm)->final(desc, out); 1196 } 1197 1198 static inline int crypto_hash_digest(struct hash_desc *desc, 1199 struct scatterlist *sg, 1200 unsigned int nbytes, u8 *out) 1201 { 1202 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out); 1203 } 1204 1205 static inline int crypto_hash_setkey(struct crypto_hash *hash, 1206 const u8 *key, unsigned int keylen) 1207 { 1208 return crypto_hash_crt(hash)->setkey(hash, key, keylen); 1209 } 1210 1211 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm) 1212 { 1213 return (struct crypto_comp *)tfm; 1214 } 1215 1216 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm) 1217 { 1218 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) & 1219 CRYPTO_ALG_TYPE_MASK); 1220 return __crypto_comp_cast(tfm); 1221 } 1222 1223 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name, 1224 u32 type, u32 mask) 1225 { 1226 type &= ~CRYPTO_ALG_TYPE_MASK; 1227 type |= CRYPTO_ALG_TYPE_COMPRESS; 1228 mask |= CRYPTO_ALG_TYPE_MASK; 1229 1230 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask)); 1231 } 1232 1233 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm) 1234 { 1235 return &tfm->base; 1236 } 1237 1238 static inline void crypto_free_comp(struct crypto_comp *tfm) 1239 { 1240 crypto_free_tfm(crypto_comp_tfm(tfm)); 1241 } 1242 1243 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask) 1244 { 1245 type &= ~CRYPTO_ALG_TYPE_MASK; 1246 type |= CRYPTO_ALG_TYPE_COMPRESS; 1247 mask |= CRYPTO_ALG_TYPE_MASK; 1248 1249 return crypto_has_alg(alg_name, type, mask); 1250 } 1251 1252 static inline const char *crypto_comp_name(struct crypto_comp *tfm) 1253 { 1254 return crypto_tfm_alg_name(crypto_comp_tfm(tfm)); 1255 } 1256 1257 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm) 1258 { 1259 return &crypto_comp_tfm(tfm)->crt_compress; 1260 } 1261 1262 static inline int crypto_comp_compress(struct crypto_comp *tfm, 1263 const u8 *src, unsigned int slen, 1264 u8 *dst, unsigned int *dlen) 1265 { 1266 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm), 1267 src, slen, dst, dlen); 1268 } 1269 1270 static inline int crypto_comp_decompress(struct crypto_comp *tfm, 1271 const u8 *src, unsigned int slen, 1272 u8 *dst, unsigned int *dlen) 1273 { 1274 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm), 1275 src, slen, dst, dlen); 1276 } 1277 1278 #endif /* _LINUX_CRYPTO_H */ 1279 1280