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