1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Scatterlist Cryptographic API. 4 * 5 * Copyright (c) 2002 James Morris <[email protected]> 6 * Copyright (c) 2002 David S. Miller ([email protected]) 7 * Copyright (c) 2005 Herbert Xu <[email protected]> 8 * 9 * Portions derived from Cryptoapi, by Alexander Kjeldaas <[email protected]> 10 * and Nettle, by Niels Möller. 11 */ 12 #ifndef _LINUX_CRYPTO_H 13 #define _LINUX_CRYPTO_H 14 15 #include <linux/atomic.h> 16 #include <linux/kernel.h> 17 #include <linux/list.h> 18 #include <linux/bug.h> 19 #include <linux/refcount.h> 20 #include <linux/slab.h> 21 #include <linux/completion.h> 22 23 /* 24 * Autoloaded crypto modules should only use a prefixed name to avoid allowing 25 * arbitrary modules to be loaded. Loading from userspace may still need the 26 * unprefixed names, so retains those aliases as well. 27 * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3 28 * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro 29 * expands twice on the same line. Instead, use a separate base name for the 30 * alias. 31 */ 32 #define MODULE_ALIAS_CRYPTO(name) \ 33 __MODULE_INFO(alias, alias_userspace, name); \ 34 __MODULE_INFO(alias, alias_crypto, "crypto-" name) 35 36 /* 37 * Algorithm masks and types. 38 */ 39 #define CRYPTO_ALG_TYPE_MASK 0x0000000f 40 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001 41 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002 42 #define CRYPTO_ALG_TYPE_AEAD 0x00000003 43 #define CRYPTO_ALG_TYPE_SKCIPHER 0x00000005 44 #define CRYPTO_ALG_TYPE_KPP 0x00000008 45 #define CRYPTO_ALG_TYPE_ACOMPRESS 0x0000000a 46 #define CRYPTO_ALG_TYPE_SCOMPRESS 0x0000000b 47 #define CRYPTO_ALG_TYPE_RNG 0x0000000c 48 #define CRYPTO_ALG_TYPE_AKCIPHER 0x0000000d 49 #define CRYPTO_ALG_TYPE_HASH 0x0000000e 50 #define CRYPTO_ALG_TYPE_SHASH 0x0000000e 51 #define CRYPTO_ALG_TYPE_AHASH 0x0000000f 52 53 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e 54 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000e 55 #define CRYPTO_ALG_TYPE_ACOMPRESS_MASK 0x0000000e 56 57 #define CRYPTO_ALG_LARVAL 0x00000010 58 #define CRYPTO_ALG_DEAD 0x00000020 59 #define CRYPTO_ALG_DYING 0x00000040 60 #define CRYPTO_ALG_ASYNC 0x00000080 61 62 /* 63 * Set this bit if and only if the algorithm requires another algorithm of 64 * the same type to handle corner cases. 65 */ 66 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100 67 68 /* 69 * Set if the algorithm has passed automated run-time testing. Note that 70 * if there is no run-time testing for a given algorithm it is considered 71 * to have passed. 72 */ 73 74 #define CRYPTO_ALG_TESTED 0x00000400 75 76 /* 77 * Set if the algorithm is an instance that is built from templates. 78 */ 79 #define CRYPTO_ALG_INSTANCE 0x00000800 80 81 /* Set this bit if the algorithm provided is hardware accelerated but 82 * not available to userspace via instruction set or so. 83 */ 84 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000 85 86 /* 87 * Mark a cipher as a service implementation only usable by another 88 * cipher and never by a normal user of the kernel crypto API 89 */ 90 #define CRYPTO_ALG_INTERNAL 0x00002000 91 92 /* 93 * Set if the algorithm has a ->setkey() method but can be used without 94 * calling it first, i.e. there is a default key. 95 */ 96 #define CRYPTO_ALG_OPTIONAL_KEY 0x00004000 97 98 /* 99 * Don't trigger module loading 100 */ 101 #define CRYPTO_NOLOAD 0x00008000 102 103 /* 104 * Transform masks and values (for crt_flags). 105 */ 106 #define CRYPTO_TFM_NEED_KEY 0x00000001 107 108 #define CRYPTO_TFM_REQ_MASK 0x000fff00 109 #define CRYPTO_TFM_REQ_FORBID_WEAK_KEYS 0x00000100 110 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200 111 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400 112 113 /* 114 * Miscellaneous stuff. 115 */ 116 #define CRYPTO_MAX_ALG_NAME 128 117 118 /* 119 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual 120 * declaration) is used to ensure that the crypto_tfm context structure is 121 * aligned correctly for the given architecture so that there are no alignment 122 * faults for C data types. In particular, this is required on platforms such 123 * as arm where pointers are 32-bit aligned but there are data types such as 124 * u64 which require 64-bit alignment. 125 */ 126 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN 127 128 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN))) 129 130 struct scatterlist; 131 struct crypto_async_request; 132 struct crypto_tfm; 133 struct crypto_type; 134 135 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err); 136 137 /** 138 * DOC: Block Cipher Context Data Structures 139 * 140 * These data structures define the operating context for each block cipher 141 * type. 142 */ 143 144 struct crypto_async_request { 145 struct list_head list; 146 crypto_completion_t complete; 147 void *data; 148 struct crypto_tfm *tfm; 149 150 u32 flags; 151 }; 152 153 /** 154 * DOC: Block Cipher Algorithm Definitions 155 * 156 * These data structures define modular crypto algorithm implementations, 157 * managed via crypto_register_alg() and crypto_unregister_alg(). 158 */ 159 160 /** 161 * struct cipher_alg - single-block symmetric ciphers definition 162 * @cia_min_keysize: Minimum key size supported by the transformation. This is 163 * the smallest key length supported by this transformation 164 * algorithm. This must be set to one of the pre-defined 165 * values as this is not hardware specific. Possible values 166 * for this field can be found via git grep "_MIN_KEY_SIZE" 167 * include/crypto/ 168 * @cia_max_keysize: Maximum key size supported by the transformation. This is 169 * the largest key length supported by this transformation 170 * algorithm. This must be set to one of the pre-defined values 171 * as this is not hardware specific. Possible values for this 172 * field can be found via git grep "_MAX_KEY_SIZE" 173 * include/crypto/ 174 * @cia_setkey: Set key for the transformation. This function is used to either 175 * program a supplied key into the hardware or store the key in the 176 * transformation context for programming it later. Note that this 177 * function does modify the transformation context. This function 178 * can be called multiple times during the existence of the 179 * transformation object, so one must make sure the key is properly 180 * reprogrammed into the hardware. This function is also 181 * responsible for checking the key length for validity. 182 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a 183 * single block of data, which must be @cra_blocksize big. This 184 * always operates on a full @cra_blocksize and it is not possible 185 * to encrypt a block of smaller size. The supplied buffers must 186 * therefore also be at least of @cra_blocksize size. Both the 187 * input and output buffers are always aligned to @cra_alignmask. 188 * In case either of the input or output buffer supplied by user 189 * of the crypto API is not aligned to @cra_alignmask, the crypto 190 * API will re-align the buffers. The re-alignment means that a 191 * new buffer will be allocated, the data will be copied into the 192 * new buffer, then the processing will happen on the new buffer, 193 * then the data will be copied back into the original buffer and 194 * finally the new buffer will be freed. In case a software 195 * fallback was put in place in the @cra_init call, this function 196 * might need to use the fallback if the algorithm doesn't support 197 * all of the key sizes. In case the key was stored in 198 * transformation context, the key might need to be re-programmed 199 * into the hardware in this function. This function shall not 200 * modify the transformation context, as this function may be 201 * called in parallel with the same transformation object. 202 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to 203 * @cia_encrypt, and the conditions are exactly the same. 204 * 205 * All fields are mandatory and must be filled. 206 */ 207 struct cipher_alg { 208 unsigned int cia_min_keysize; 209 unsigned int cia_max_keysize; 210 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key, 211 unsigned int keylen); 212 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 213 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 214 }; 215 216 /** 217 * struct compress_alg - compression/decompression algorithm 218 * @coa_compress: Compress a buffer of specified length, storing the resulting 219 * data in the specified buffer. Return the length of the 220 * compressed data in dlen. 221 * @coa_decompress: Decompress the source buffer, storing the uncompressed 222 * data in the specified buffer. The length of the data is 223 * returned in dlen. 224 * 225 * All fields are mandatory. 226 */ 227 struct compress_alg { 228 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src, 229 unsigned int slen, u8 *dst, unsigned int *dlen); 230 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src, 231 unsigned int slen, u8 *dst, unsigned int *dlen); 232 }; 233 234 #ifdef CONFIG_CRYPTO_STATS 235 /* 236 * struct crypto_istat_aead - statistics for AEAD algorithm 237 * @encrypt_cnt: number of encrypt requests 238 * @encrypt_tlen: total data size handled by encrypt requests 239 * @decrypt_cnt: number of decrypt requests 240 * @decrypt_tlen: total data size handled by decrypt requests 241 * @err_cnt: number of error for AEAD requests 242 */ 243 struct crypto_istat_aead { 244 atomic64_t encrypt_cnt; 245 atomic64_t encrypt_tlen; 246 atomic64_t decrypt_cnt; 247 atomic64_t decrypt_tlen; 248 atomic64_t err_cnt; 249 }; 250 251 /* 252 * struct crypto_istat_akcipher - statistics for akcipher algorithm 253 * @encrypt_cnt: number of encrypt requests 254 * @encrypt_tlen: total data size handled by encrypt requests 255 * @decrypt_cnt: number of decrypt requests 256 * @decrypt_tlen: total data size handled by decrypt requests 257 * @verify_cnt: number of verify operation 258 * @sign_cnt: number of sign requests 259 * @err_cnt: number of error for akcipher requests 260 */ 261 struct crypto_istat_akcipher { 262 atomic64_t encrypt_cnt; 263 atomic64_t encrypt_tlen; 264 atomic64_t decrypt_cnt; 265 atomic64_t decrypt_tlen; 266 atomic64_t verify_cnt; 267 atomic64_t sign_cnt; 268 atomic64_t err_cnt; 269 }; 270 271 /* 272 * struct crypto_istat_cipher - statistics for cipher algorithm 273 * @encrypt_cnt: number of encrypt requests 274 * @encrypt_tlen: total data size handled by encrypt requests 275 * @decrypt_cnt: number of decrypt requests 276 * @decrypt_tlen: total data size handled by decrypt requests 277 * @err_cnt: number of error for cipher requests 278 */ 279 struct crypto_istat_cipher { 280 atomic64_t encrypt_cnt; 281 atomic64_t encrypt_tlen; 282 atomic64_t decrypt_cnt; 283 atomic64_t decrypt_tlen; 284 atomic64_t err_cnt; 285 }; 286 287 /* 288 * struct crypto_istat_compress - statistics for compress algorithm 289 * @compress_cnt: number of compress requests 290 * @compress_tlen: total data size handled by compress requests 291 * @decompress_cnt: number of decompress requests 292 * @decompress_tlen: total data size handled by decompress requests 293 * @err_cnt: number of error for compress requests 294 */ 295 struct crypto_istat_compress { 296 atomic64_t compress_cnt; 297 atomic64_t compress_tlen; 298 atomic64_t decompress_cnt; 299 atomic64_t decompress_tlen; 300 atomic64_t err_cnt; 301 }; 302 303 /* 304 * struct crypto_istat_hash - statistics for has algorithm 305 * @hash_cnt: number of hash requests 306 * @hash_tlen: total data size hashed 307 * @err_cnt: number of error for hash requests 308 */ 309 struct crypto_istat_hash { 310 atomic64_t hash_cnt; 311 atomic64_t hash_tlen; 312 atomic64_t err_cnt; 313 }; 314 315 /* 316 * struct crypto_istat_kpp - statistics for KPP algorithm 317 * @setsecret_cnt: number of setsecrey operation 318 * @generate_public_key_cnt: number of generate_public_key operation 319 * @compute_shared_secret_cnt: number of compute_shared_secret operation 320 * @err_cnt: number of error for KPP requests 321 */ 322 struct crypto_istat_kpp { 323 atomic64_t setsecret_cnt; 324 atomic64_t generate_public_key_cnt; 325 atomic64_t compute_shared_secret_cnt; 326 atomic64_t err_cnt; 327 }; 328 329 /* 330 * struct crypto_istat_rng: statistics for RNG algorithm 331 * @generate_cnt: number of RNG generate requests 332 * @generate_tlen: total data size of generated data by the RNG 333 * @seed_cnt: number of times the RNG was seeded 334 * @err_cnt: number of error for RNG requests 335 */ 336 struct crypto_istat_rng { 337 atomic64_t generate_cnt; 338 atomic64_t generate_tlen; 339 atomic64_t seed_cnt; 340 atomic64_t err_cnt; 341 }; 342 #endif /* CONFIG_CRYPTO_STATS */ 343 344 #define cra_cipher cra_u.cipher 345 #define cra_compress cra_u.compress 346 347 /** 348 * struct crypto_alg - definition of a cryptograpic cipher algorithm 349 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h 350 * CRYPTO_ALG_* flags for the flags which go in here. Those are 351 * used for fine-tuning the description of the transformation 352 * algorithm. 353 * @cra_blocksize: Minimum block size of this transformation. The size in bytes 354 * of the smallest possible unit which can be transformed with 355 * this algorithm. The users must respect this value. 356 * In case of HASH transformation, it is possible for a smaller 357 * block than @cra_blocksize to be passed to the crypto API for 358 * transformation, in case of any other transformation type, an 359 * error will be returned upon any attempt to transform smaller 360 * than @cra_blocksize chunks. 361 * @cra_ctxsize: Size of the operational context of the transformation. This 362 * value informs the kernel crypto API about the memory size 363 * needed to be allocated for the transformation context. 364 * @cra_alignmask: Alignment mask for the input and output data buffer. The data 365 * buffer containing the input data for the algorithm must be 366 * aligned to this alignment mask. The data buffer for the 367 * output data must be aligned to this alignment mask. Note that 368 * the Crypto API will do the re-alignment in software, but 369 * only under special conditions and there is a performance hit. 370 * The re-alignment happens at these occasions for different 371 * @cra_u types: cipher -- For both input data and output data 372 * buffer; ahash -- For output hash destination buf; shash -- 373 * For output hash destination buf. 374 * This is needed on hardware which is flawed by design and 375 * cannot pick data from arbitrary addresses. 376 * @cra_priority: Priority of this transformation implementation. In case 377 * multiple transformations with same @cra_name are available to 378 * the Crypto API, the kernel will use the one with highest 379 * @cra_priority. 380 * @cra_name: Generic name (usable by multiple implementations) of the 381 * transformation algorithm. This is the name of the transformation 382 * itself. This field is used by the kernel when looking up the 383 * providers of particular transformation. 384 * @cra_driver_name: Unique name of the transformation provider. This is the 385 * name of the provider of the transformation. This can be any 386 * arbitrary value, but in the usual case, this contains the 387 * name of the chip or provider and the name of the 388 * transformation algorithm. 389 * @cra_type: Type of the cryptographic transformation. This is a pointer to 390 * struct crypto_type, which implements callbacks common for all 391 * transformation types. There are multiple options, such as 392 * &crypto_skcipher_type, &crypto_ahash_type, &crypto_rng_type. 393 * This field might be empty. In that case, there are no common 394 * callbacks. This is the case for: cipher, compress, shash. 395 * @cra_u: Callbacks implementing the transformation. This is a union of 396 * multiple structures. Depending on the type of transformation selected 397 * by @cra_type and @cra_flags above, the associated structure must be 398 * filled with callbacks. This field might be empty. This is the case 399 * for ahash, shash. 400 * @cra_init: Initialize the cryptographic transformation object. This function 401 * is used to initialize the cryptographic transformation object. 402 * This function is called only once at the instantiation time, right 403 * after the transformation context was allocated. In case the 404 * cryptographic hardware has some special requirements which need to 405 * be handled by software, this function shall check for the precise 406 * requirement of the transformation and put any software fallbacks 407 * in place. 408 * @cra_exit: Deinitialize the cryptographic transformation object. This is a 409 * counterpart to @cra_init, used to remove various changes set in 410 * @cra_init. 411 * @cra_u.cipher: Union member which contains a single-block symmetric cipher 412 * definition. See @struct @cipher_alg. 413 * @cra_u.compress: Union member which contains a (de)compression algorithm. 414 * See @struct @compress_alg. 415 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE 416 * @cra_list: internally used 417 * @cra_users: internally used 418 * @cra_refcnt: internally used 419 * @cra_destroy: internally used 420 * 421 * @stats: union of all possible crypto_istat_xxx structures 422 * @stats.aead: statistics for AEAD algorithm 423 * @stats.akcipher: statistics for akcipher algorithm 424 * @stats.cipher: statistics for cipher algorithm 425 * @stats.compress: statistics for compress algorithm 426 * @stats.hash: statistics for hash algorithm 427 * @stats.rng: statistics for rng algorithm 428 * @stats.kpp: statistics for KPP algorithm 429 * 430 * The struct crypto_alg describes a generic Crypto API algorithm and is common 431 * for all of the transformations. Any variable not documented here shall not 432 * be used by a cipher implementation as it is internal to the Crypto API. 433 */ 434 struct crypto_alg { 435 struct list_head cra_list; 436 struct list_head cra_users; 437 438 u32 cra_flags; 439 unsigned int cra_blocksize; 440 unsigned int cra_ctxsize; 441 unsigned int cra_alignmask; 442 443 int cra_priority; 444 refcount_t cra_refcnt; 445 446 char cra_name[CRYPTO_MAX_ALG_NAME]; 447 char cra_driver_name[CRYPTO_MAX_ALG_NAME]; 448 449 const struct crypto_type *cra_type; 450 451 union { 452 struct cipher_alg cipher; 453 struct compress_alg compress; 454 } cra_u; 455 456 int (*cra_init)(struct crypto_tfm *tfm); 457 void (*cra_exit)(struct crypto_tfm *tfm); 458 void (*cra_destroy)(struct crypto_alg *alg); 459 460 struct module *cra_module; 461 462 #ifdef CONFIG_CRYPTO_STATS 463 union { 464 struct crypto_istat_aead aead; 465 struct crypto_istat_akcipher akcipher; 466 struct crypto_istat_cipher cipher; 467 struct crypto_istat_compress compress; 468 struct crypto_istat_hash hash; 469 struct crypto_istat_rng rng; 470 struct crypto_istat_kpp kpp; 471 } stats; 472 #endif /* CONFIG_CRYPTO_STATS */ 473 474 } CRYPTO_MINALIGN_ATTR; 475 476 #ifdef CONFIG_CRYPTO_STATS 477 void crypto_stats_init(struct crypto_alg *alg); 478 void crypto_stats_get(struct crypto_alg *alg); 479 void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret); 480 void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret); 481 void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg); 482 void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg); 483 void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg); 484 void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg); 485 void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg); 486 void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg); 487 void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg); 488 void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg); 489 void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret); 490 void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret); 491 void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret); 492 void crypto_stats_rng_seed(struct crypto_alg *alg, int ret); 493 void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret); 494 void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg); 495 void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg); 496 #else 497 static inline void crypto_stats_init(struct crypto_alg *alg) 498 {} 499 static inline void crypto_stats_get(struct crypto_alg *alg) 500 {} 501 static inline void crypto_stats_aead_encrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret) 502 {} 503 static inline void crypto_stats_aead_decrypt(unsigned int cryptlen, struct crypto_alg *alg, int ret) 504 {} 505 static inline void crypto_stats_ahash_update(unsigned int nbytes, int ret, struct crypto_alg *alg) 506 {} 507 static inline void crypto_stats_ahash_final(unsigned int nbytes, int ret, struct crypto_alg *alg) 508 {} 509 static inline void crypto_stats_akcipher_encrypt(unsigned int src_len, int ret, struct crypto_alg *alg) 510 {} 511 static inline void crypto_stats_akcipher_decrypt(unsigned int src_len, int ret, struct crypto_alg *alg) 512 {} 513 static inline void crypto_stats_akcipher_sign(int ret, struct crypto_alg *alg) 514 {} 515 static inline void crypto_stats_akcipher_verify(int ret, struct crypto_alg *alg) 516 {} 517 static inline void crypto_stats_compress(unsigned int slen, int ret, struct crypto_alg *alg) 518 {} 519 static inline void crypto_stats_decompress(unsigned int slen, int ret, struct crypto_alg *alg) 520 {} 521 static inline void crypto_stats_kpp_set_secret(struct crypto_alg *alg, int ret) 522 {} 523 static inline void crypto_stats_kpp_generate_public_key(struct crypto_alg *alg, int ret) 524 {} 525 static inline void crypto_stats_kpp_compute_shared_secret(struct crypto_alg *alg, int ret) 526 {} 527 static inline void crypto_stats_rng_seed(struct crypto_alg *alg, int ret) 528 {} 529 static inline void crypto_stats_rng_generate(struct crypto_alg *alg, unsigned int dlen, int ret) 530 {} 531 static inline void crypto_stats_skcipher_encrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg) 532 {} 533 static inline void crypto_stats_skcipher_decrypt(unsigned int cryptlen, int ret, struct crypto_alg *alg) 534 {} 535 #endif 536 /* 537 * A helper struct for waiting for completion of async crypto ops 538 */ 539 struct crypto_wait { 540 struct completion completion; 541 int err; 542 }; 543 544 /* 545 * Macro for declaring a crypto op async wait object on stack 546 */ 547 #define DECLARE_CRYPTO_WAIT(_wait) \ 548 struct crypto_wait _wait = { \ 549 COMPLETION_INITIALIZER_ONSTACK((_wait).completion), 0 } 550 551 /* 552 * Async ops completion helper functioons 553 */ 554 void crypto_req_done(struct crypto_async_request *req, int err); 555 556 static inline int crypto_wait_req(int err, struct crypto_wait *wait) 557 { 558 switch (err) { 559 case -EINPROGRESS: 560 case -EBUSY: 561 wait_for_completion(&wait->completion); 562 reinit_completion(&wait->completion); 563 err = wait->err; 564 break; 565 } 566 567 return err; 568 } 569 570 static inline void crypto_init_wait(struct crypto_wait *wait) 571 { 572 init_completion(&wait->completion); 573 } 574 575 /* 576 * Algorithm registration interface. 577 */ 578 int crypto_register_alg(struct crypto_alg *alg); 579 void crypto_unregister_alg(struct crypto_alg *alg); 580 int crypto_register_algs(struct crypto_alg *algs, int count); 581 void crypto_unregister_algs(struct crypto_alg *algs, int count); 582 583 /* 584 * Algorithm query interface. 585 */ 586 int crypto_has_alg(const char *name, u32 type, u32 mask); 587 588 /* 589 * Transforms: user-instantiated objects which encapsulate algorithms 590 * and core processing logic. Managed via crypto_alloc_*() and 591 * crypto_free_*(), as well as the various helpers below. 592 */ 593 594 struct crypto_tfm { 595 596 u32 crt_flags; 597 598 int node; 599 600 void (*exit)(struct crypto_tfm *tfm); 601 602 struct crypto_alg *__crt_alg; 603 604 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR; 605 }; 606 607 struct crypto_cipher { 608 struct crypto_tfm base; 609 }; 610 611 struct crypto_comp { 612 struct crypto_tfm base; 613 }; 614 615 enum { 616 CRYPTOA_UNSPEC, 617 CRYPTOA_ALG, 618 CRYPTOA_TYPE, 619 CRYPTOA_U32, 620 __CRYPTOA_MAX, 621 }; 622 623 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1) 624 625 /* Maximum number of (rtattr) parameters for each template. */ 626 #define CRYPTO_MAX_ATTRS 32 627 628 struct crypto_attr_alg { 629 char name[CRYPTO_MAX_ALG_NAME]; 630 }; 631 632 struct crypto_attr_type { 633 u32 type; 634 u32 mask; 635 }; 636 637 struct crypto_attr_u32 { 638 u32 num; 639 }; 640 641 /* 642 * Transform user interface. 643 */ 644 645 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask); 646 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm); 647 648 static inline void crypto_free_tfm(struct crypto_tfm *tfm) 649 { 650 return crypto_destroy_tfm(tfm, tfm); 651 } 652 653 int alg_test(const char *driver, const char *alg, u32 type, u32 mask); 654 655 /* 656 * Transform helpers which query the underlying algorithm. 657 */ 658 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm) 659 { 660 return tfm->__crt_alg->cra_name; 661 } 662 663 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm) 664 { 665 return tfm->__crt_alg->cra_driver_name; 666 } 667 668 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm) 669 { 670 return tfm->__crt_alg->cra_priority; 671 } 672 673 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm) 674 { 675 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK; 676 } 677 678 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm) 679 { 680 return tfm->__crt_alg->cra_blocksize; 681 } 682 683 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm) 684 { 685 return tfm->__crt_alg->cra_alignmask; 686 } 687 688 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm) 689 { 690 return tfm->crt_flags; 691 } 692 693 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags) 694 { 695 tfm->crt_flags |= flags; 696 } 697 698 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags) 699 { 700 tfm->crt_flags &= ~flags; 701 } 702 703 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm) 704 { 705 return tfm->__crt_ctx; 706 } 707 708 static inline unsigned int crypto_tfm_ctx_alignment(void) 709 { 710 struct crypto_tfm *tfm; 711 return __alignof__(tfm->__crt_ctx); 712 } 713 714 /** 715 * DOC: Single Block Cipher API 716 * 717 * The single block cipher API is used with the ciphers of type 718 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto). 719 * 720 * Using the single block cipher API calls, operations with the basic cipher 721 * primitive can be implemented. These cipher primitives exclude any block 722 * chaining operations including IV handling. 723 * 724 * The purpose of this single block cipher API is to support the implementation 725 * of templates or other concepts that only need to perform the cipher operation 726 * on one block at a time. Templates invoke the underlying cipher primitive 727 * block-wise and process either the input or the output data of these cipher 728 * operations. 729 */ 730 731 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm) 732 { 733 return (struct crypto_cipher *)tfm; 734 } 735 736 /** 737 * crypto_alloc_cipher() - allocate single block cipher handle 738 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 739 * single block cipher 740 * @type: specifies the type of the cipher 741 * @mask: specifies the mask for the cipher 742 * 743 * Allocate a cipher handle for a single block cipher. The returned struct 744 * crypto_cipher is the cipher handle that is required for any subsequent API 745 * invocation for that single block cipher. 746 * 747 * Return: allocated cipher handle in case of success; IS_ERR() is true in case 748 * of an error, PTR_ERR() returns the error code. 749 */ 750 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name, 751 u32 type, u32 mask) 752 { 753 type &= ~CRYPTO_ALG_TYPE_MASK; 754 type |= CRYPTO_ALG_TYPE_CIPHER; 755 mask |= CRYPTO_ALG_TYPE_MASK; 756 757 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask)); 758 } 759 760 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm) 761 { 762 return &tfm->base; 763 } 764 765 /** 766 * crypto_free_cipher() - zeroize and free the single block cipher handle 767 * @tfm: cipher handle to be freed 768 */ 769 static inline void crypto_free_cipher(struct crypto_cipher *tfm) 770 { 771 crypto_free_tfm(crypto_cipher_tfm(tfm)); 772 } 773 774 /** 775 * crypto_has_cipher() - Search for the availability of a single block cipher 776 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 777 * single block cipher 778 * @type: specifies the type of the cipher 779 * @mask: specifies the mask for the cipher 780 * 781 * Return: true when the single block cipher is known to the kernel crypto API; 782 * false otherwise 783 */ 784 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask) 785 { 786 type &= ~CRYPTO_ALG_TYPE_MASK; 787 type |= CRYPTO_ALG_TYPE_CIPHER; 788 mask |= CRYPTO_ALG_TYPE_MASK; 789 790 return crypto_has_alg(alg_name, type, mask); 791 } 792 793 /** 794 * crypto_cipher_blocksize() - obtain block size for cipher 795 * @tfm: cipher handle 796 * 797 * The block size for the single block cipher referenced with the cipher handle 798 * tfm is returned. The caller may use that information to allocate appropriate 799 * memory for the data returned by the encryption or decryption operation 800 * 801 * Return: block size of cipher 802 */ 803 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm) 804 { 805 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm)); 806 } 807 808 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm) 809 { 810 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm)); 811 } 812 813 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm) 814 { 815 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm)); 816 } 817 818 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm, 819 u32 flags) 820 { 821 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags); 822 } 823 824 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm, 825 u32 flags) 826 { 827 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags); 828 } 829 830 /** 831 * crypto_cipher_setkey() - set key for cipher 832 * @tfm: cipher handle 833 * @key: buffer holding the key 834 * @keylen: length of the key in bytes 835 * 836 * The caller provided key is set for the single block cipher referenced by the 837 * cipher handle. 838 * 839 * Note, the key length determines the cipher type. Many block ciphers implement 840 * different cipher modes depending on the key size, such as AES-128 vs AES-192 841 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 842 * is performed. 843 * 844 * Return: 0 if the setting of the key was successful; < 0 if an error occurred 845 */ 846 int crypto_cipher_setkey(struct crypto_cipher *tfm, 847 const u8 *key, unsigned int keylen); 848 849 /** 850 * crypto_cipher_encrypt_one() - encrypt one block of plaintext 851 * @tfm: cipher handle 852 * @dst: points to the buffer that will be filled with the ciphertext 853 * @src: buffer holding the plaintext to be encrypted 854 * 855 * Invoke the encryption operation of one block. The caller must ensure that 856 * the plaintext and ciphertext buffers are at least one block in size. 857 */ 858 void crypto_cipher_encrypt_one(struct crypto_cipher *tfm, 859 u8 *dst, const u8 *src); 860 861 /** 862 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext 863 * @tfm: cipher handle 864 * @dst: points to the buffer that will be filled with the plaintext 865 * @src: buffer holding the ciphertext to be decrypted 866 * 867 * Invoke the decryption operation of one block. The caller must ensure that 868 * the plaintext and ciphertext buffers are at least one block in size. 869 */ 870 void crypto_cipher_decrypt_one(struct crypto_cipher *tfm, 871 u8 *dst, const u8 *src); 872 873 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm) 874 { 875 return (struct crypto_comp *)tfm; 876 } 877 878 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name, 879 u32 type, u32 mask) 880 { 881 type &= ~CRYPTO_ALG_TYPE_MASK; 882 type |= CRYPTO_ALG_TYPE_COMPRESS; 883 mask |= CRYPTO_ALG_TYPE_MASK; 884 885 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask)); 886 } 887 888 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm) 889 { 890 return &tfm->base; 891 } 892 893 static inline void crypto_free_comp(struct crypto_comp *tfm) 894 { 895 crypto_free_tfm(crypto_comp_tfm(tfm)); 896 } 897 898 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask) 899 { 900 type &= ~CRYPTO_ALG_TYPE_MASK; 901 type |= CRYPTO_ALG_TYPE_COMPRESS; 902 mask |= CRYPTO_ALG_TYPE_MASK; 903 904 return crypto_has_alg(alg_name, type, mask); 905 } 906 907 static inline const char *crypto_comp_name(struct crypto_comp *tfm) 908 { 909 return crypto_tfm_alg_name(crypto_comp_tfm(tfm)); 910 } 911 912 int crypto_comp_compress(struct crypto_comp *tfm, 913 const u8 *src, unsigned int slen, 914 u8 *dst, unsigned int *dlen); 915 916 int crypto_comp_decompress(struct crypto_comp *tfm, 917 const u8 *src, unsigned int slen, 918 u8 *dst, unsigned int *dlen); 919 920 #endif /* _LINUX_CRYPTO_H */ 921 922