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/bug.h> 24 #include <linux/slab.h> 25 #include <linux/string.h> 26 #include <linux/uaccess.h> 27 28 /* 29 * Autoloaded crypto modules should only use a prefixed name to avoid allowing 30 * arbitrary modules to be loaded. Loading from userspace may still need the 31 * unprefixed names, so retains those aliases as well. 32 * This uses __MODULE_INFO directly instead of MODULE_ALIAS because pre-4.3 33 * gcc (e.g. avr32 toolchain) uses __LINE__ for uniqueness, and this macro 34 * expands twice on the same line. Instead, use a separate base name for the 35 * alias. 36 */ 37 #define MODULE_ALIAS_CRYPTO(name) \ 38 __MODULE_INFO(alias, alias_userspace, name); \ 39 __MODULE_INFO(alias, alias_crypto, "crypto-" name) 40 41 /* 42 * Algorithm masks and types. 43 */ 44 #define CRYPTO_ALG_TYPE_MASK 0x0000000f 45 #define CRYPTO_ALG_TYPE_CIPHER 0x00000001 46 #define CRYPTO_ALG_TYPE_COMPRESS 0x00000002 47 #define CRYPTO_ALG_TYPE_AEAD 0x00000003 48 #define CRYPTO_ALG_TYPE_BLKCIPHER 0x00000004 49 #define CRYPTO_ALG_TYPE_ABLKCIPHER 0x00000005 50 #define CRYPTO_ALG_TYPE_GIVCIPHER 0x00000006 51 #define CRYPTO_ALG_TYPE_DIGEST 0x00000008 52 #define CRYPTO_ALG_TYPE_HASH 0x00000008 53 #define CRYPTO_ALG_TYPE_SHASH 0x00000009 54 #define CRYPTO_ALG_TYPE_AHASH 0x0000000a 55 #define CRYPTO_ALG_TYPE_RNG 0x0000000c 56 #define CRYPTO_ALG_TYPE_PCOMPRESS 0x0000000f 57 58 #define CRYPTO_ALG_TYPE_HASH_MASK 0x0000000e 59 #define CRYPTO_ALG_TYPE_AHASH_MASK 0x0000000c 60 #define CRYPTO_ALG_TYPE_BLKCIPHER_MASK 0x0000000c 61 62 #define CRYPTO_ALG_LARVAL 0x00000010 63 #define CRYPTO_ALG_DEAD 0x00000020 64 #define CRYPTO_ALG_DYING 0x00000040 65 #define CRYPTO_ALG_ASYNC 0x00000080 66 67 /* 68 * Set this bit if and only if the algorithm requires another algorithm of 69 * the same type to handle corner cases. 70 */ 71 #define CRYPTO_ALG_NEED_FALLBACK 0x00000100 72 73 /* 74 * This bit is set for symmetric key ciphers that have already been wrapped 75 * with a generic IV generator to prevent them from being wrapped again. 76 */ 77 #define CRYPTO_ALG_GENIV 0x00000200 78 79 /* 80 * Set if the algorithm has passed automated run-time testing. Note that 81 * if there is no run-time testing for a given algorithm it is considered 82 * to have passed. 83 */ 84 85 #define CRYPTO_ALG_TESTED 0x00000400 86 87 /* 88 * Set if the algorithm is an instance that is build from templates. 89 */ 90 #define CRYPTO_ALG_INSTANCE 0x00000800 91 92 /* Set this bit if the algorithm provided is hardware accelerated but 93 * not available to userspace via instruction set or so. 94 */ 95 #define CRYPTO_ALG_KERN_DRIVER_ONLY 0x00001000 96 97 /* 98 * Mark a cipher as a service implementation only usable by another 99 * cipher and never by a normal user of the kernel crypto API 100 */ 101 #define CRYPTO_ALG_INTERNAL 0x00002000 102 103 /* 104 * Transform masks and values (for crt_flags). 105 */ 106 #define CRYPTO_TFM_REQ_MASK 0x000fff00 107 #define CRYPTO_TFM_RES_MASK 0xfff00000 108 109 #define CRYPTO_TFM_REQ_WEAK_KEY 0x00000100 110 #define CRYPTO_TFM_REQ_MAY_SLEEP 0x00000200 111 #define CRYPTO_TFM_REQ_MAY_BACKLOG 0x00000400 112 #define CRYPTO_TFM_RES_WEAK_KEY 0x00100000 113 #define CRYPTO_TFM_RES_BAD_KEY_LEN 0x00200000 114 #define CRYPTO_TFM_RES_BAD_KEY_SCHED 0x00400000 115 #define CRYPTO_TFM_RES_BAD_BLOCK_LEN 0x00800000 116 #define CRYPTO_TFM_RES_BAD_FLAGS 0x01000000 117 118 /* 119 * Miscellaneous stuff. 120 */ 121 #define CRYPTO_MAX_ALG_NAME 64 122 123 /* 124 * The macro CRYPTO_MINALIGN_ATTR (along with the void * type in the actual 125 * declaration) is used to ensure that the crypto_tfm context structure is 126 * aligned correctly for the given architecture so that there are no alignment 127 * faults for C data types. In particular, this is required on platforms such 128 * as arm where pointers are 32-bit aligned but there are data types such as 129 * u64 which require 64-bit alignment. 130 */ 131 #define CRYPTO_MINALIGN ARCH_KMALLOC_MINALIGN 132 133 #define CRYPTO_MINALIGN_ATTR __attribute__ ((__aligned__(CRYPTO_MINALIGN))) 134 135 struct scatterlist; 136 struct crypto_ablkcipher; 137 struct crypto_async_request; 138 struct crypto_aead; 139 struct crypto_blkcipher; 140 struct crypto_hash; 141 struct crypto_tfm; 142 struct crypto_type; 143 struct aead_givcrypt_request; 144 struct skcipher_givcrypt_request; 145 146 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err); 147 148 /** 149 * DOC: Block Cipher Context Data Structures 150 * 151 * These data structures define the operating context for each block cipher 152 * type. 153 */ 154 155 struct crypto_async_request { 156 struct list_head list; 157 crypto_completion_t complete; 158 void *data; 159 struct crypto_tfm *tfm; 160 161 u32 flags; 162 }; 163 164 struct ablkcipher_request { 165 struct crypto_async_request base; 166 167 unsigned int nbytes; 168 169 void *info; 170 171 struct scatterlist *src; 172 struct scatterlist *dst; 173 174 void *__ctx[] CRYPTO_MINALIGN_ATTR; 175 }; 176 177 /** 178 * struct aead_request - AEAD request 179 * @base: Common attributes for async crypto requests 180 * @assoclen: Length in bytes of associated data for authentication 181 * @cryptlen: Length of data to be encrypted or decrypted 182 * @iv: Initialisation vector 183 * @assoc: Associated data 184 * @src: Source data 185 * @dst: Destination data 186 * @__ctx: Start of private context data 187 */ 188 struct aead_request { 189 struct crypto_async_request base; 190 191 unsigned int assoclen; 192 unsigned int cryptlen; 193 194 u8 *iv; 195 196 struct scatterlist *assoc; 197 struct scatterlist *src; 198 struct scatterlist *dst; 199 200 void *__ctx[] CRYPTO_MINALIGN_ATTR; 201 }; 202 203 struct blkcipher_desc { 204 struct crypto_blkcipher *tfm; 205 void *info; 206 u32 flags; 207 }; 208 209 struct cipher_desc { 210 struct crypto_tfm *tfm; 211 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 212 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst, 213 const u8 *src, unsigned int nbytes); 214 void *info; 215 }; 216 217 struct hash_desc { 218 struct crypto_hash *tfm; 219 u32 flags; 220 }; 221 222 /** 223 * DOC: Block Cipher Algorithm Definitions 224 * 225 * These data structures define modular crypto algorithm implementations, 226 * managed via crypto_register_alg() and crypto_unregister_alg(). 227 */ 228 229 /** 230 * struct ablkcipher_alg - asynchronous block cipher definition 231 * @min_keysize: Minimum key size supported by the transformation. This is the 232 * smallest key length supported by this transformation algorithm. 233 * This must be set to one of the pre-defined values as this is 234 * not hardware specific. Possible values for this field can be 235 * found via git grep "_MIN_KEY_SIZE" include/crypto/ 236 * @max_keysize: Maximum key size supported by the transformation. This is the 237 * largest key length supported by this transformation algorithm. 238 * This must be set to one of the pre-defined values as this is 239 * not hardware specific. Possible values for this field can be 240 * found via git grep "_MAX_KEY_SIZE" include/crypto/ 241 * @setkey: Set key for the transformation. This function is used to either 242 * program a supplied key into the hardware or store the key in the 243 * transformation context for programming it later. Note that this 244 * function does modify the transformation context. This function can 245 * be called multiple times during the existence of the transformation 246 * object, so one must make sure the key is properly reprogrammed into 247 * the hardware. This function is also responsible for checking the key 248 * length for validity. In case a software fallback was put in place in 249 * the @cra_init call, this function might need to use the fallback if 250 * the algorithm doesn't support all of the key sizes. 251 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt 252 * the supplied scatterlist containing the blocks of data. The crypto 253 * API consumer is responsible for aligning the entries of the 254 * scatterlist properly and making sure the chunks are correctly 255 * sized. In case a software fallback was put in place in the 256 * @cra_init call, this function might need to use the fallback if 257 * the algorithm doesn't support all of the key sizes. In case the 258 * key was stored in transformation context, the key might need to be 259 * re-programmed into the hardware in this function. This function 260 * shall not modify the transformation context, as this function may 261 * be called in parallel with the same transformation object. 262 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt 263 * and the conditions are exactly the same. 264 * @givencrypt: Update the IV for encryption. With this function, a cipher 265 * implementation may provide the function on how to update the IV 266 * for encryption. 267 * @givdecrypt: Update the IV for decryption. This is the reverse of 268 * @givencrypt . 269 * @geniv: The transformation implementation may use an "IV generator" provided 270 * by the kernel crypto API. Several use cases have a predefined 271 * approach how IVs are to be updated. For such use cases, the kernel 272 * crypto API provides ready-to-use implementations that can be 273 * referenced with this variable. 274 * @ivsize: IV size applicable for transformation. The consumer must provide an 275 * IV of exactly that size to perform the encrypt or decrypt operation. 276 * 277 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are 278 * mandatory and must be filled. 279 */ 280 struct ablkcipher_alg { 281 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key, 282 unsigned int keylen); 283 int (*encrypt)(struct ablkcipher_request *req); 284 int (*decrypt)(struct ablkcipher_request *req); 285 int (*givencrypt)(struct skcipher_givcrypt_request *req); 286 int (*givdecrypt)(struct skcipher_givcrypt_request *req); 287 288 const char *geniv; 289 290 unsigned int min_keysize; 291 unsigned int max_keysize; 292 unsigned int ivsize; 293 }; 294 295 /** 296 * struct aead_alg - AEAD cipher definition 297 * @maxauthsize: Set the maximum authentication tag size supported by the 298 * transformation. A transformation may support smaller tag sizes. 299 * As the authentication tag is a message digest to ensure the 300 * integrity of the encrypted data, a consumer typically wants the 301 * largest authentication tag possible as defined by this 302 * variable. 303 * @setauthsize: Set authentication size for the AEAD transformation. This 304 * function is used to specify the consumer requested size of the 305 * authentication tag to be either generated by the transformation 306 * during encryption or the size of the authentication tag to be 307 * supplied during the decryption operation. This function is also 308 * responsible for checking the authentication tag size for 309 * validity. 310 * @setkey: see struct ablkcipher_alg 311 * @encrypt: see struct ablkcipher_alg 312 * @decrypt: see struct ablkcipher_alg 313 * @givencrypt: see struct ablkcipher_alg 314 * @givdecrypt: see struct ablkcipher_alg 315 * @geniv: see struct ablkcipher_alg 316 * @ivsize: see struct ablkcipher_alg 317 * 318 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are 319 * mandatory and must be filled. 320 */ 321 struct aead_alg { 322 int (*setkey)(struct crypto_aead *tfm, const u8 *key, 323 unsigned int keylen); 324 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize); 325 int (*encrypt)(struct aead_request *req); 326 int (*decrypt)(struct aead_request *req); 327 int (*givencrypt)(struct aead_givcrypt_request *req); 328 int (*givdecrypt)(struct aead_givcrypt_request *req); 329 330 const char *geniv; 331 332 unsigned int ivsize; 333 unsigned int maxauthsize; 334 }; 335 336 /** 337 * struct blkcipher_alg - synchronous block cipher definition 338 * @min_keysize: see struct ablkcipher_alg 339 * @max_keysize: see struct ablkcipher_alg 340 * @setkey: see struct ablkcipher_alg 341 * @encrypt: see struct ablkcipher_alg 342 * @decrypt: see struct ablkcipher_alg 343 * @geniv: see struct ablkcipher_alg 344 * @ivsize: see struct ablkcipher_alg 345 * 346 * All fields except @geniv and @ivsize are mandatory and must be filled. 347 */ 348 struct blkcipher_alg { 349 int (*setkey)(struct crypto_tfm *tfm, const u8 *key, 350 unsigned int keylen); 351 int (*encrypt)(struct blkcipher_desc *desc, 352 struct scatterlist *dst, struct scatterlist *src, 353 unsigned int nbytes); 354 int (*decrypt)(struct blkcipher_desc *desc, 355 struct scatterlist *dst, struct scatterlist *src, 356 unsigned int nbytes); 357 358 const char *geniv; 359 360 unsigned int min_keysize; 361 unsigned int max_keysize; 362 unsigned int ivsize; 363 }; 364 365 /** 366 * struct cipher_alg - single-block symmetric ciphers definition 367 * @cia_min_keysize: Minimum key size supported by the transformation. This is 368 * the smallest key length supported by this transformation 369 * algorithm. This must be set to one of the pre-defined 370 * values as this is not hardware specific. Possible values 371 * for this field can be found via git grep "_MIN_KEY_SIZE" 372 * include/crypto/ 373 * @cia_max_keysize: Maximum key size supported by the transformation. This is 374 * the largest key length supported by this transformation 375 * algorithm. This must be set to one of the pre-defined values 376 * as this is not hardware specific. Possible values for this 377 * field can be found via git grep "_MAX_KEY_SIZE" 378 * include/crypto/ 379 * @cia_setkey: Set key for the transformation. This function is used to either 380 * program a supplied key into the hardware or store the key in the 381 * transformation context for programming it later. Note that this 382 * function does modify the transformation context. This function 383 * can be called multiple times during the existence of the 384 * transformation object, so one must make sure the key is properly 385 * reprogrammed into the hardware. This function is also 386 * responsible for checking the key length for validity. 387 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a 388 * single block of data, which must be @cra_blocksize big. This 389 * always operates on a full @cra_blocksize and it is not possible 390 * to encrypt a block of smaller size. The supplied buffers must 391 * therefore also be at least of @cra_blocksize size. Both the 392 * input and output buffers are always aligned to @cra_alignmask. 393 * In case either of the input or output buffer supplied by user 394 * of the crypto API is not aligned to @cra_alignmask, the crypto 395 * API will re-align the buffers. The re-alignment means that a 396 * new buffer will be allocated, the data will be copied into the 397 * new buffer, then the processing will happen on the new buffer, 398 * then the data will be copied back into the original buffer and 399 * finally the new buffer will be freed. In case a software 400 * fallback was put in place in the @cra_init call, this function 401 * might need to use the fallback if the algorithm doesn't support 402 * all of the key sizes. In case the key was stored in 403 * transformation context, the key might need to be re-programmed 404 * into the hardware in this function. This function shall not 405 * modify the transformation context, as this function may be 406 * called in parallel with the same transformation object. 407 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to 408 * @cia_encrypt, and the conditions are exactly the same. 409 * 410 * All fields are mandatory and must be filled. 411 */ 412 struct cipher_alg { 413 unsigned int cia_min_keysize; 414 unsigned int cia_max_keysize; 415 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key, 416 unsigned int keylen); 417 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 418 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 419 }; 420 421 struct compress_alg { 422 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src, 423 unsigned int slen, u8 *dst, unsigned int *dlen); 424 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src, 425 unsigned int slen, u8 *dst, unsigned int *dlen); 426 }; 427 428 429 #define cra_ablkcipher cra_u.ablkcipher 430 #define cra_aead cra_u.aead 431 #define cra_blkcipher cra_u.blkcipher 432 #define cra_cipher cra_u.cipher 433 #define cra_compress cra_u.compress 434 435 /** 436 * struct crypto_alg - definition of a cryptograpic cipher algorithm 437 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h 438 * CRYPTO_ALG_* flags for the flags which go in here. Those are 439 * used for fine-tuning the description of the transformation 440 * algorithm. 441 * @cra_blocksize: Minimum block size of this transformation. The size in bytes 442 * of the smallest possible unit which can be transformed with 443 * this algorithm. The users must respect this value. 444 * In case of HASH transformation, it is possible for a smaller 445 * block than @cra_blocksize to be passed to the crypto API for 446 * transformation, in case of any other transformation type, an 447 * error will be returned upon any attempt to transform smaller 448 * than @cra_blocksize chunks. 449 * @cra_ctxsize: Size of the operational context of the transformation. This 450 * value informs the kernel crypto API about the memory size 451 * needed to be allocated for the transformation context. 452 * @cra_alignmask: Alignment mask for the input and output data buffer. The data 453 * buffer containing the input data for the algorithm must be 454 * aligned to this alignment mask. The data buffer for the 455 * output data must be aligned to this alignment mask. Note that 456 * the Crypto API will do the re-alignment in software, but 457 * only under special conditions and there is a performance hit. 458 * The re-alignment happens at these occasions for different 459 * @cra_u types: cipher -- For both input data and output data 460 * buffer; ahash -- For output hash destination buf; shash -- 461 * For output hash destination buf. 462 * This is needed on hardware which is flawed by design and 463 * cannot pick data from arbitrary addresses. 464 * @cra_priority: Priority of this transformation implementation. In case 465 * multiple transformations with same @cra_name are available to 466 * the Crypto API, the kernel will use the one with highest 467 * @cra_priority. 468 * @cra_name: Generic name (usable by multiple implementations) of the 469 * transformation algorithm. This is the name of the transformation 470 * itself. This field is used by the kernel when looking up the 471 * providers of particular transformation. 472 * @cra_driver_name: Unique name of the transformation provider. This is the 473 * name of the provider of the transformation. This can be any 474 * arbitrary value, but in the usual case, this contains the 475 * name of the chip or provider and the name of the 476 * transformation algorithm. 477 * @cra_type: Type of the cryptographic transformation. This is a pointer to 478 * struct crypto_type, which implements callbacks common for all 479 * trasnformation types. There are multiple options: 480 * &crypto_blkcipher_type, &crypto_ablkcipher_type, 481 * &crypto_ahash_type, &crypto_aead_type, &crypto_rng_type. 482 * This field might be empty. In that case, there are no common 483 * callbacks. This is the case for: cipher, compress, shash. 484 * @cra_u: Callbacks implementing the transformation. This is a union of 485 * multiple structures. Depending on the type of transformation selected 486 * by @cra_type and @cra_flags above, the associated structure must be 487 * filled with callbacks. This field might be empty. This is the case 488 * for ahash, shash. 489 * @cra_init: Initialize the cryptographic transformation object. This function 490 * is used to initialize the cryptographic transformation object. 491 * This function is called only once at the instantiation time, right 492 * after the transformation context was allocated. In case the 493 * cryptographic hardware has some special requirements which need to 494 * be handled by software, this function shall check for the precise 495 * requirement of the transformation and put any software fallbacks 496 * in place. 497 * @cra_exit: Deinitialize the cryptographic transformation object. This is a 498 * counterpart to @cra_init, used to remove various changes set in 499 * @cra_init. 500 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE 501 * @cra_list: internally used 502 * @cra_users: internally used 503 * @cra_refcnt: internally used 504 * @cra_destroy: internally used 505 * 506 * The struct crypto_alg describes a generic Crypto API algorithm and is common 507 * for all of the transformations. Any variable not documented here shall not 508 * be used by a cipher implementation as it is internal to the Crypto API. 509 */ 510 struct crypto_alg { 511 struct list_head cra_list; 512 struct list_head cra_users; 513 514 u32 cra_flags; 515 unsigned int cra_blocksize; 516 unsigned int cra_ctxsize; 517 unsigned int cra_alignmask; 518 519 int cra_priority; 520 atomic_t cra_refcnt; 521 522 char cra_name[CRYPTO_MAX_ALG_NAME]; 523 char cra_driver_name[CRYPTO_MAX_ALG_NAME]; 524 525 const struct crypto_type *cra_type; 526 527 union { 528 struct ablkcipher_alg ablkcipher; 529 struct aead_alg aead; 530 struct blkcipher_alg blkcipher; 531 struct cipher_alg cipher; 532 struct compress_alg compress; 533 } cra_u; 534 535 int (*cra_init)(struct crypto_tfm *tfm); 536 void (*cra_exit)(struct crypto_tfm *tfm); 537 void (*cra_destroy)(struct crypto_alg *alg); 538 539 struct module *cra_module; 540 }; 541 542 /* 543 * Algorithm registration interface. 544 */ 545 int crypto_register_alg(struct crypto_alg *alg); 546 int crypto_unregister_alg(struct crypto_alg *alg); 547 int crypto_register_algs(struct crypto_alg *algs, int count); 548 int crypto_unregister_algs(struct crypto_alg *algs, int count); 549 550 /* 551 * Algorithm query interface. 552 */ 553 int crypto_has_alg(const char *name, u32 type, u32 mask); 554 555 /* 556 * Transforms: user-instantiated objects which encapsulate algorithms 557 * and core processing logic. Managed via crypto_alloc_*() and 558 * crypto_free_*(), as well as the various helpers below. 559 */ 560 561 struct ablkcipher_tfm { 562 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key, 563 unsigned int keylen); 564 int (*encrypt)(struct ablkcipher_request *req); 565 int (*decrypt)(struct ablkcipher_request *req); 566 int (*givencrypt)(struct skcipher_givcrypt_request *req); 567 int (*givdecrypt)(struct skcipher_givcrypt_request *req); 568 569 struct crypto_ablkcipher *base; 570 571 unsigned int ivsize; 572 unsigned int reqsize; 573 }; 574 575 struct aead_tfm { 576 int (*setkey)(struct crypto_aead *tfm, const u8 *key, 577 unsigned int keylen); 578 int (*encrypt)(struct aead_request *req); 579 int (*decrypt)(struct aead_request *req); 580 int (*givencrypt)(struct aead_givcrypt_request *req); 581 int (*givdecrypt)(struct aead_givcrypt_request *req); 582 583 struct crypto_aead *base; 584 585 unsigned int ivsize; 586 unsigned int authsize; 587 unsigned int reqsize; 588 }; 589 590 struct blkcipher_tfm { 591 void *iv; 592 int (*setkey)(struct crypto_tfm *tfm, const u8 *key, 593 unsigned int keylen); 594 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst, 595 struct scatterlist *src, unsigned int nbytes); 596 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst, 597 struct scatterlist *src, unsigned int nbytes); 598 }; 599 600 struct cipher_tfm { 601 int (*cit_setkey)(struct crypto_tfm *tfm, 602 const u8 *key, unsigned int keylen); 603 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 604 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 605 }; 606 607 struct hash_tfm { 608 int (*init)(struct hash_desc *desc); 609 int (*update)(struct hash_desc *desc, 610 struct scatterlist *sg, unsigned int nsg); 611 int (*final)(struct hash_desc *desc, u8 *out); 612 int (*digest)(struct hash_desc *desc, struct scatterlist *sg, 613 unsigned int nsg, u8 *out); 614 int (*setkey)(struct crypto_hash *tfm, const u8 *key, 615 unsigned int keylen); 616 unsigned int digestsize; 617 }; 618 619 struct compress_tfm { 620 int (*cot_compress)(struct crypto_tfm *tfm, 621 const u8 *src, unsigned int slen, 622 u8 *dst, unsigned int *dlen); 623 int (*cot_decompress)(struct crypto_tfm *tfm, 624 const u8 *src, unsigned int slen, 625 u8 *dst, unsigned int *dlen); 626 }; 627 628 #define crt_ablkcipher crt_u.ablkcipher 629 #define crt_aead crt_u.aead 630 #define crt_blkcipher crt_u.blkcipher 631 #define crt_cipher crt_u.cipher 632 #define crt_hash crt_u.hash 633 #define crt_compress crt_u.compress 634 635 struct crypto_tfm { 636 637 u32 crt_flags; 638 639 union { 640 struct ablkcipher_tfm ablkcipher; 641 struct aead_tfm aead; 642 struct blkcipher_tfm blkcipher; 643 struct cipher_tfm cipher; 644 struct hash_tfm hash; 645 struct compress_tfm compress; 646 } crt_u; 647 648 void (*exit)(struct crypto_tfm *tfm); 649 650 struct crypto_alg *__crt_alg; 651 652 void *__crt_ctx[] CRYPTO_MINALIGN_ATTR; 653 }; 654 655 struct crypto_ablkcipher { 656 struct crypto_tfm base; 657 }; 658 659 struct crypto_aead { 660 struct crypto_tfm base; 661 }; 662 663 struct crypto_blkcipher { 664 struct crypto_tfm base; 665 }; 666 667 struct crypto_cipher { 668 struct crypto_tfm base; 669 }; 670 671 struct crypto_comp { 672 struct crypto_tfm base; 673 }; 674 675 struct crypto_hash { 676 struct crypto_tfm base; 677 }; 678 679 enum { 680 CRYPTOA_UNSPEC, 681 CRYPTOA_ALG, 682 CRYPTOA_TYPE, 683 CRYPTOA_U32, 684 __CRYPTOA_MAX, 685 }; 686 687 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1) 688 689 /* Maximum number of (rtattr) parameters for each template. */ 690 #define CRYPTO_MAX_ATTRS 32 691 692 struct crypto_attr_alg { 693 char name[CRYPTO_MAX_ALG_NAME]; 694 }; 695 696 struct crypto_attr_type { 697 u32 type; 698 u32 mask; 699 }; 700 701 struct crypto_attr_u32 { 702 u32 num; 703 }; 704 705 /* 706 * Transform user interface. 707 */ 708 709 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask); 710 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm); 711 712 static inline void crypto_free_tfm(struct crypto_tfm *tfm) 713 { 714 return crypto_destroy_tfm(tfm, tfm); 715 } 716 717 int alg_test(const char *driver, const char *alg, u32 type, u32 mask); 718 719 /* 720 * Transform helpers which query the underlying algorithm. 721 */ 722 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm) 723 { 724 return tfm->__crt_alg->cra_name; 725 } 726 727 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm) 728 { 729 return tfm->__crt_alg->cra_driver_name; 730 } 731 732 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm) 733 { 734 return tfm->__crt_alg->cra_priority; 735 } 736 737 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm) 738 { 739 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK; 740 } 741 742 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm) 743 { 744 return tfm->__crt_alg->cra_blocksize; 745 } 746 747 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm) 748 { 749 return tfm->__crt_alg->cra_alignmask; 750 } 751 752 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm) 753 { 754 return tfm->crt_flags; 755 } 756 757 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags) 758 { 759 tfm->crt_flags |= flags; 760 } 761 762 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags) 763 { 764 tfm->crt_flags &= ~flags; 765 } 766 767 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm) 768 { 769 return tfm->__crt_ctx; 770 } 771 772 static inline unsigned int crypto_tfm_ctx_alignment(void) 773 { 774 struct crypto_tfm *tfm; 775 return __alignof__(tfm->__crt_ctx); 776 } 777 778 /* 779 * API wrappers. 780 */ 781 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast( 782 struct crypto_tfm *tfm) 783 { 784 return (struct crypto_ablkcipher *)tfm; 785 } 786 787 static inline u32 crypto_skcipher_type(u32 type) 788 { 789 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV); 790 type |= CRYPTO_ALG_TYPE_BLKCIPHER; 791 return type; 792 } 793 794 static inline u32 crypto_skcipher_mask(u32 mask) 795 { 796 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV); 797 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK; 798 return mask; 799 } 800 801 /** 802 * DOC: Asynchronous Block Cipher API 803 * 804 * Asynchronous block cipher API is used with the ciphers of type 805 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto). 806 * 807 * Asynchronous cipher operations imply that the function invocation for a 808 * cipher request returns immediately before the completion of the operation. 809 * The cipher request is scheduled as a separate kernel thread and therefore 810 * load-balanced on the different CPUs via the process scheduler. To allow 811 * the kernel crypto API to inform the caller about the completion of a cipher 812 * request, the caller must provide a callback function. That function is 813 * invoked with the cipher handle when the request completes. 814 * 815 * To support the asynchronous operation, additional information than just the 816 * cipher handle must be supplied to the kernel crypto API. That additional 817 * information is given by filling in the ablkcipher_request data structure. 818 * 819 * For the asynchronous block cipher API, the state is maintained with the tfm 820 * cipher handle. A single tfm can be used across multiple calls and in 821 * parallel. For asynchronous block cipher calls, context data supplied and 822 * only used by the caller can be referenced the request data structure in 823 * addition to the IV used for the cipher request. The maintenance of such 824 * state information would be important for a crypto driver implementer to 825 * have, because when calling the callback function upon completion of the 826 * cipher operation, that callback function may need some information about 827 * which operation just finished if it invoked multiple in parallel. This 828 * state information is unused by the kernel crypto API. 829 */ 830 831 /** 832 * crypto_alloc_ablkcipher() - allocate asynchronous block cipher handle 833 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 834 * ablkcipher cipher 835 * @type: specifies the type of the cipher 836 * @mask: specifies the mask for the cipher 837 * 838 * Allocate a cipher handle for an ablkcipher. The returned struct 839 * crypto_ablkcipher is the cipher handle that is required for any subsequent 840 * API invocation for that ablkcipher. 841 * 842 * Return: allocated cipher handle in case of success; IS_ERR() is true in case 843 * of an error, PTR_ERR() returns the error code. 844 */ 845 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name, 846 u32 type, u32 mask); 847 848 static inline struct crypto_tfm *crypto_ablkcipher_tfm( 849 struct crypto_ablkcipher *tfm) 850 { 851 return &tfm->base; 852 } 853 854 /** 855 * crypto_free_ablkcipher() - zeroize and free cipher handle 856 * @tfm: cipher handle to be freed 857 */ 858 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm) 859 { 860 crypto_free_tfm(crypto_ablkcipher_tfm(tfm)); 861 } 862 863 /** 864 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher. 865 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 866 * ablkcipher 867 * @type: specifies the type of the cipher 868 * @mask: specifies the mask for the cipher 869 * 870 * Return: true when the ablkcipher is known to the kernel crypto API; false 871 * otherwise 872 */ 873 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type, 874 u32 mask) 875 { 876 return crypto_has_alg(alg_name, crypto_skcipher_type(type), 877 crypto_skcipher_mask(mask)); 878 } 879 880 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt( 881 struct crypto_ablkcipher *tfm) 882 { 883 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher; 884 } 885 886 /** 887 * crypto_ablkcipher_ivsize() - obtain IV size 888 * @tfm: cipher handle 889 * 890 * The size of the IV for the ablkcipher referenced by the cipher handle is 891 * returned. This IV size may be zero if the cipher does not need an IV. 892 * 893 * Return: IV size in bytes 894 */ 895 static inline unsigned int crypto_ablkcipher_ivsize( 896 struct crypto_ablkcipher *tfm) 897 { 898 return crypto_ablkcipher_crt(tfm)->ivsize; 899 } 900 901 /** 902 * crypto_ablkcipher_blocksize() - obtain block size of cipher 903 * @tfm: cipher handle 904 * 905 * The block size for the ablkcipher referenced with the cipher handle is 906 * returned. The caller may use that information to allocate appropriate 907 * memory for the data returned by the encryption or decryption operation 908 * 909 * Return: block size of cipher 910 */ 911 static inline unsigned int crypto_ablkcipher_blocksize( 912 struct crypto_ablkcipher *tfm) 913 { 914 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm)); 915 } 916 917 static inline unsigned int crypto_ablkcipher_alignmask( 918 struct crypto_ablkcipher *tfm) 919 { 920 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm)); 921 } 922 923 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm) 924 { 925 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm)); 926 } 927 928 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm, 929 u32 flags) 930 { 931 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags); 932 } 933 934 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm, 935 u32 flags) 936 { 937 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags); 938 } 939 940 /** 941 * crypto_ablkcipher_setkey() - set key for cipher 942 * @tfm: cipher handle 943 * @key: buffer holding the key 944 * @keylen: length of the key in bytes 945 * 946 * The caller provided key is set for the ablkcipher referenced by the cipher 947 * handle. 948 * 949 * Note, the key length determines the cipher type. Many block ciphers implement 950 * different cipher modes depending on the key size, such as AES-128 vs AES-192 951 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 952 * is performed. 953 * 954 * Return: 0 if the setting of the key was successful; < 0 if an error occurred 955 */ 956 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm, 957 const u8 *key, unsigned int keylen) 958 { 959 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm); 960 961 return crt->setkey(crt->base, key, keylen); 962 } 963 964 /** 965 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request 966 * @req: ablkcipher_request out of which the cipher handle is to be obtained 967 * 968 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request 969 * data structure. 970 * 971 * Return: crypto_ablkcipher handle 972 */ 973 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm( 974 struct ablkcipher_request *req) 975 { 976 return __crypto_ablkcipher_cast(req->base.tfm); 977 } 978 979 /** 980 * crypto_ablkcipher_encrypt() - encrypt plaintext 981 * @req: reference to the ablkcipher_request handle that holds all information 982 * needed to perform the cipher operation 983 * 984 * Encrypt plaintext data using the ablkcipher_request handle. That data 985 * structure and how it is filled with data is discussed with the 986 * ablkcipher_request_* functions. 987 * 988 * Return: 0 if the cipher operation was successful; < 0 if an error occurred 989 */ 990 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req) 991 { 992 struct ablkcipher_tfm *crt = 993 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req)); 994 return crt->encrypt(req); 995 } 996 997 /** 998 * crypto_ablkcipher_decrypt() - decrypt ciphertext 999 * @req: reference to the ablkcipher_request handle that holds all information 1000 * needed to perform the cipher operation 1001 * 1002 * Decrypt ciphertext data using the ablkcipher_request handle. That data 1003 * structure and how it is filled with data is discussed with the 1004 * ablkcipher_request_* functions. 1005 * 1006 * Return: 0 if the cipher operation was successful; < 0 if an error occurred 1007 */ 1008 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req) 1009 { 1010 struct ablkcipher_tfm *crt = 1011 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req)); 1012 return crt->decrypt(req); 1013 } 1014 1015 /** 1016 * DOC: Asynchronous Cipher Request Handle 1017 * 1018 * The ablkcipher_request data structure contains all pointers to data 1019 * required for the asynchronous cipher operation. This includes the cipher 1020 * handle (which can be used by multiple ablkcipher_request instances), pointer 1021 * to plaintext and ciphertext, asynchronous callback function, etc. It acts 1022 * as a handle to the ablkcipher_request_* API calls in a similar way as 1023 * ablkcipher handle to the crypto_ablkcipher_* API calls. 1024 */ 1025 1026 /** 1027 * crypto_ablkcipher_reqsize() - obtain size of the request data structure 1028 * @tfm: cipher handle 1029 * 1030 * Return: number of bytes 1031 */ 1032 static inline unsigned int crypto_ablkcipher_reqsize( 1033 struct crypto_ablkcipher *tfm) 1034 { 1035 return crypto_ablkcipher_crt(tfm)->reqsize; 1036 } 1037 1038 /** 1039 * ablkcipher_request_set_tfm() - update cipher handle reference in request 1040 * @req: request handle to be modified 1041 * @tfm: cipher handle that shall be added to the request handle 1042 * 1043 * Allow the caller to replace the existing ablkcipher handle in the request 1044 * data structure with a different one. 1045 */ 1046 static inline void ablkcipher_request_set_tfm( 1047 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm) 1048 { 1049 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base); 1050 } 1051 1052 static inline struct ablkcipher_request *ablkcipher_request_cast( 1053 struct crypto_async_request *req) 1054 { 1055 return container_of(req, struct ablkcipher_request, base); 1056 } 1057 1058 /** 1059 * ablkcipher_request_alloc() - allocate request data structure 1060 * @tfm: cipher handle to be registered with the request 1061 * @gfp: memory allocation flag that is handed to kmalloc by the API call. 1062 * 1063 * Allocate the request data structure that must be used with the ablkcipher 1064 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher 1065 * handle is registered in the request data structure. 1066 * 1067 * Return: allocated request handle in case of success; IS_ERR() is true in case 1068 * of an error, PTR_ERR() returns the error code. 1069 */ 1070 static inline struct ablkcipher_request *ablkcipher_request_alloc( 1071 struct crypto_ablkcipher *tfm, gfp_t gfp) 1072 { 1073 struct ablkcipher_request *req; 1074 1075 req = kmalloc(sizeof(struct ablkcipher_request) + 1076 crypto_ablkcipher_reqsize(tfm), gfp); 1077 1078 if (likely(req)) 1079 ablkcipher_request_set_tfm(req, tfm); 1080 1081 return req; 1082 } 1083 1084 /** 1085 * ablkcipher_request_free() - zeroize and free request data structure 1086 * @req: request data structure cipher handle to be freed 1087 */ 1088 static inline void ablkcipher_request_free(struct ablkcipher_request *req) 1089 { 1090 kzfree(req); 1091 } 1092 1093 /** 1094 * ablkcipher_request_set_callback() - set asynchronous callback function 1095 * @req: request handle 1096 * @flags: specify zero or an ORing of the flags 1097 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and 1098 * increase the wait queue beyond the initial maximum size; 1099 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep 1100 * @compl: callback function pointer to be registered with the request handle 1101 * @data: The data pointer refers to memory that is not used by the kernel 1102 * crypto API, but provided to the callback function for it to use. Here, 1103 * the caller can provide a reference to memory the callback function can 1104 * operate on. As the callback function is invoked asynchronously to the 1105 * related functionality, it may need to access data structures of the 1106 * related functionality which can be referenced using this pointer. The 1107 * callback function can access the memory via the "data" field in the 1108 * crypto_async_request data structure provided to the callback function. 1109 * 1110 * This function allows setting the callback function that is triggered once the 1111 * cipher operation completes. 1112 * 1113 * The callback function is registered with the ablkcipher_request handle and 1114 * must comply with the following template 1115 * 1116 * void callback_function(struct crypto_async_request *req, int error) 1117 */ 1118 static inline void ablkcipher_request_set_callback( 1119 struct ablkcipher_request *req, 1120 u32 flags, crypto_completion_t compl, void *data) 1121 { 1122 req->base.complete = compl; 1123 req->base.data = data; 1124 req->base.flags = flags; 1125 } 1126 1127 /** 1128 * ablkcipher_request_set_crypt() - set data buffers 1129 * @req: request handle 1130 * @src: source scatter / gather list 1131 * @dst: destination scatter / gather list 1132 * @nbytes: number of bytes to process from @src 1133 * @iv: IV for the cipher operation which must comply with the IV size defined 1134 * by crypto_ablkcipher_ivsize 1135 * 1136 * This function allows setting of the source data and destination data 1137 * scatter / gather lists. 1138 * 1139 * For encryption, the source is treated as the plaintext and the 1140 * destination is the ciphertext. For a decryption operation, the use is 1141 * reversed - the source is the ciphertext and the destination is the plaintext. 1142 */ 1143 static inline void ablkcipher_request_set_crypt( 1144 struct ablkcipher_request *req, 1145 struct scatterlist *src, struct scatterlist *dst, 1146 unsigned int nbytes, void *iv) 1147 { 1148 req->src = src; 1149 req->dst = dst; 1150 req->nbytes = nbytes; 1151 req->info = iv; 1152 } 1153 1154 /** 1155 * DOC: Authenticated Encryption With Associated Data (AEAD) Cipher API 1156 * 1157 * The AEAD cipher API is used with the ciphers of type CRYPTO_ALG_TYPE_AEAD 1158 * (listed as type "aead" in /proc/crypto) 1159 * 1160 * The most prominent examples for this type of encryption is GCM and CCM. 1161 * However, the kernel supports other types of AEAD ciphers which are defined 1162 * with the following cipher string: 1163 * 1164 * authenc(keyed message digest, block cipher) 1165 * 1166 * For example: authenc(hmac(sha256), cbc(aes)) 1167 * 1168 * The example code provided for the asynchronous block cipher operation 1169 * applies here as well. Naturally all *ablkcipher* symbols must be exchanged 1170 * the *aead* pendants discussed in the following. In addtion, for the AEAD 1171 * operation, the aead_request_set_assoc function must be used to set the 1172 * pointer to the associated data memory location before performing the 1173 * encryption or decryption operation. In case of an encryption, the associated 1174 * data memory is filled during the encryption operation. For decryption, the 1175 * associated data memory must contain data that is used to verify the integrity 1176 * of the decrypted data. Another deviation from the asynchronous block cipher 1177 * operation is that the caller should explicitly check for -EBADMSG of the 1178 * crypto_aead_decrypt. That error indicates an authentication error, i.e. 1179 * a breach in the integrity of the message. In essence, that -EBADMSG error 1180 * code is the key bonus an AEAD cipher has over "standard" block chaining 1181 * modes. 1182 */ 1183 1184 static inline struct crypto_aead *__crypto_aead_cast(struct crypto_tfm *tfm) 1185 { 1186 return (struct crypto_aead *)tfm; 1187 } 1188 1189 /** 1190 * crypto_alloc_aead() - allocate AEAD cipher handle 1191 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 1192 * AEAD cipher 1193 * @type: specifies the type of the cipher 1194 * @mask: specifies the mask for the cipher 1195 * 1196 * Allocate a cipher handle for an AEAD. The returned struct 1197 * crypto_aead is the cipher handle that is required for any subsequent 1198 * API invocation for that AEAD. 1199 * 1200 * Return: allocated cipher handle in case of success; IS_ERR() is true in case 1201 * of an error, PTR_ERR() returns the error code. 1202 */ 1203 struct crypto_aead *crypto_alloc_aead(const char *alg_name, u32 type, u32 mask); 1204 1205 static inline struct crypto_tfm *crypto_aead_tfm(struct crypto_aead *tfm) 1206 { 1207 return &tfm->base; 1208 } 1209 1210 /** 1211 * crypto_free_aead() - zeroize and free aead handle 1212 * @tfm: cipher handle to be freed 1213 */ 1214 static inline void crypto_free_aead(struct crypto_aead *tfm) 1215 { 1216 crypto_free_tfm(crypto_aead_tfm(tfm)); 1217 } 1218 1219 static inline struct aead_tfm *crypto_aead_crt(struct crypto_aead *tfm) 1220 { 1221 return &crypto_aead_tfm(tfm)->crt_aead; 1222 } 1223 1224 /** 1225 * crypto_aead_ivsize() - obtain IV size 1226 * @tfm: cipher handle 1227 * 1228 * The size of the IV for the aead referenced by the cipher handle is 1229 * returned. This IV size may be zero if the cipher does not need an IV. 1230 * 1231 * Return: IV size in bytes 1232 */ 1233 static inline unsigned int crypto_aead_ivsize(struct crypto_aead *tfm) 1234 { 1235 return crypto_aead_crt(tfm)->ivsize; 1236 } 1237 1238 /** 1239 * crypto_aead_authsize() - obtain maximum authentication data size 1240 * @tfm: cipher handle 1241 * 1242 * The maximum size of the authentication data for the AEAD cipher referenced 1243 * by the AEAD cipher handle is returned. The authentication data size may be 1244 * zero if the cipher implements a hard-coded maximum. 1245 * 1246 * The authentication data may also be known as "tag value". 1247 * 1248 * Return: authentication data size / tag size in bytes 1249 */ 1250 static inline unsigned int crypto_aead_authsize(struct crypto_aead *tfm) 1251 { 1252 return crypto_aead_crt(tfm)->authsize; 1253 } 1254 1255 /** 1256 * crypto_aead_blocksize() - obtain block size of cipher 1257 * @tfm: cipher handle 1258 * 1259 * The block size for the AEAD referenced with the cipher handle is returned. 1260 * The caller may use that information to allocate appropriate memory for the 1261 * data returned by the encryption or decryption operation 1262 * 1263 * Return: block size of cipher 1264 */ 1265 static inline unsigned int crypto_aead_blocksize(struct crypto_aead *tfm) 1266 { 1267 return crypto_tfm_alg_blocksize(crypto_aead_tfm(tfm)); 1268 } 1269 1270 static inline unsigned int crypto_aead_alignmask(struct crypto_aead *tfm) 1271 { 1272 return crypto_tfm_alg_alignmask(crypto_aead_tfm(tfm)); 1273 } 1274 1275 static inline u32 crypto_aead_get_flags(struct crypto_aead *tfm) 1276 { 1277 return crypto_tfm_get_flags(crypto_aead_tfm(tfm)); 1278 } 1279 1280 static inline void crypto_aead_set_flags(struct crypto_aead *tfm, u32 flags) 1281 { 1282 crypto_tfm_set_flags(crypto_aead_tfm(tfm), flags); 1283 } 1284 1285 static inline void crypto_aead_clear_flags(struct crypto_aead *tfm, u32 flags) 1286 { 1287 crypto_tfm_clear_flags(crypto_aead_tfm(tfm), flags); 1288 } 1289 1290 /** 1291 * crypto_aead_setkey() - set key for cipher 1292 * @tfm: cipher handle 1293 * @key: buffer holding the key 1294 * @keylen: length of the key in bytes 1295 * 1296 * The caller provided key is set for the AEAD referenced by the cipher 1297 * handle. 1298 * 1299 * Note, the key length determines the cipher type. Many block ciphers implement 1300 * different cipher modes depending on the key size, such as AES-128 vs AES-192 1301 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 1302 * is performed. 1303 * 1304 * Return: 0 if the setting of the key was successful; < 0 if an error occurred 1305 */ 1306 static inline int crypto_aead_setkey(struct crypto_aead *tfm, const u8 *key, 1307 unsigned int keylen) 1308 { 1309 struct aead_tfm *crt = crypto_aead_crt(tfm); 1310 1311 return crt->setkey(crt->base, key, keylen); 1312 } 1313 1314 /** 1315 * crypto_aead_setauthsize() - set authentication data size 1316 * @tfm: cipher handle 1317 * @authsize: size of the authentication data / tag in bytes 1318 * 1319 * Set the authentication data size / tag size. AEAD requires an authentication 1320 * tag (or MAC) in addition to the associated data. 1321 * 1322 * Return: 0 if the setting of the key was successful; < 0 if an error occurred 1323 */ 1324 int crypto_aead_setauthsize(struct crypto_aead *tfm, unsigned int authsize); 1325 1326 static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req) 1327 { 1328 return __crypto_aead_cast(req->base.tfm); 1329 } 1330 1331 /** 1332 * crypto_aead_encrypt() - encrypt plaintext 1333 * @req: reference to the aead_request handle that holds all information 1334 * needed to perform the cipher operation 1335 * 1336 * Encrypt plaintext data using the aead_request handle. That data structure 1337 * and how it is filled with data is discussed with the aead_request_* 1338 * functions. 1339 * 1340 * IMPORTANT NOTE The encryption operation creates the authentication data / 1341 * tag. That data is concatenated with the created ciphertext. 1342 * The ciphertext memory size is therefore the given number of 1343 * block cipher blocks + the size defined by the 1344 * crypto_aead_setauthsize invocation. The caller must ensure 1345 * that sufficient memory is available for the ciphertext and 1346 * the authentication tag. 1347 * 1348 * Return: 0 if the cipher operation was successful; < 0 if an error occurred 1349 */ 1350 static inline int crypto_aead_encrypt(struct aead_request *req) 1351 { 1352 return crypto_aead_crt(crypto_aead_reqtfm(req))->encrypt(req); 1353 } 1354 1355 /** 1356 * crypto_aead_decrypt() - decrypt ciphertext 1357 * @req: reference to the ablkcipher_request handle that holds all information 1358 * needed to perform the cipher operation 1359 * 1360 * Decrypt ciphertext data using the aead_request handle. That data structure 1361 * and how it is filled with data is discussed with the aead_request_* 1362 * functions. 1363 * 1364 * IMPORTANT NOTE The caller must concatenate the ciphertext followed by the 1365 * authentication data / tag. That authentication data / tag 1366 * must have the size defined by the crypto_aead_setauthsize 1367 * invocation. 1368 * 1369 * 1370 * Return: 0 if the cipher operation was successful; -EBADMSG: The AEAD 1371 * cipher operation performs the authentication of the data during the 1372 * decryption operation. Therefore, the function returns this error if 1373 * the authentication of the ciphertext was unsuccessful (i.e. the 1374 * integrity of the ciphertext or the associated data was violated); 1375 * < 0 if an error occurred. 1376 */ 1377 static inline int crypto_aead_decrypt(struct aead_request *req) 1378 { 1379 if (req->cryptlen < crypto_aead_authsize(crypto_aead_reqtfm(req))) 1380 return -EINVAL; 1381 1382 return crypto_aead_crt(crypto_aead_reqtfm(req))->decrypt(req); 1383 } 1384 1385 /** 1386 * DOC: Asynchronous AEAD Request Handle 1387 * 1388 * The aead_request data structure contains all pointers to data required for 1389 * the AEAD cipher operation. This includes the cipher handle (which can be 1390 * used by multiple aead_request instances), pointer to plaintext and 1391 * ciphertext, asynchronous callback function, etc. It acts as a handle to the 1392 * aead_request_* API calls in a similar way as AEAD handle to the 1393 * crypto_aead_* API calls. 1394 */ 1395 1396 /** 1397 * crypto_aead_reqsize() - obtain size of the request data structure 1398 * @tfm: cipher handle 1399 * 1400 * Return: number of bytes 1401 */ 1402 static inline unsigned int crypto_aead_reqsize(struct crypto_aead *tfm) 1403 { 1404 return crypto_aead_crt(tfm)->reqsize; 1405 } 1406 1407 /** 1408 * aead_request_set_tfm() - update cipher handle reference in request 1409 * @req: request handle to be modified 1410 * @tfm: cipher handle that shall be added to the request handle 1411 * 1412 * Allow the caller to replace the existing aead handle in the request 1413 * data structure with a different one. 1414 */ 1415 static inline void aead_request_set_tfm(struct aead_request *req, 1416 struct crypto_aead *tfm) 1417 { 1418 req->base.tfm = crypto_aead_tfm(crypto_aead_crt(tfm)->base); 1419 } 1420 1421 /** 1422 * aead_request_alloc() - allocate request data structure 1423 * @tfm: cipher handle to be registered with the request 1424 * @gfp: memory allocation flag that is handed to kmalloc by the API call. 1425 * 1426 * Allocate the request data structure that must be used with the AEAD 1427 * encrypt and decrypt API calls. During the allocation, the provided aead 1428 * handle is registered in the request data structure. 1429 * 1430 * Return: allocated request handle in case of success; IS_ERR() is true in case 1431 * of an error, PTR_ERR() returns the error code. 1432 */ 1433 static inline struct aead_request *aead_request_alloc(struct crypto_aead *tfm, 1434 gfp_t gfp) 1435 { 1436 struct aead_request *req; 1437 1438 req = kmalloc(sizeof(*req) + crypto_aead_reqsize(tfm), gfp); 1439 1440 if (likely(req)) 1441 aead_request_set_tfm(req, tfm); 1442 1443 return req; 1444 } 1445 1446 /** 1447 * aead_request_free() - zeroize and free request data structure 1448 * @req: request data structure cipher handle to be freed 1449 */ 1450 static inline void aead_request_free(struct aead_request *req) 1451 { 1452 kzfree(req); 1453 } 1454 1455 /** 1456 * aead_request_set_callback() - set asynchronous callback function 1457 * @req: request handle 1458 * @flags: specify zero or an ORing of the flags 1459 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and 1460 * increase the wait queue beyond the initial maximum size; 1461 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep 1462 * @compl: callback function pointer to be registered with the request handle 1463 * @data: The data pointer refers to memory that is not used by the kernel 1464 * crypto API, but provided to the callback function for it to use. Here, 1465 * the caller can provide a reference to memory the callback function can 1466 * operate on. As the callback function is invoked asynchronously to the 1467 * related functionality, it may need to access data structures of the 1468 * related functionality which can be referenced using this pointer. The 1469 * callback function can access the memory via the "data" field in the 1470 * crypto_async_request data structure provided to the callback function. 1471 * 1472 * Setting the callback function that is triggered once the cipher operation 1473 * completes 1474 * 1475 * The callback function is registered with the aead_request handle and 1476 * must comply with the following template 1477 * 1478 * void callback_function(struct crypto_async_request *req, int error) 1479 */ 1480 static inline void aead_request_set_callback(struct aead_request *req, 1481 u32 flags, 1482 crypto_completion_t compl, 1483 void *data) 1484 { 1485 req->base.complete = compl; 1486 req->base.data = data; 1487 req->base.flags = flags; 1488 } 1489 1490 /** 1491 * aead_request_set_crypt - set data buffers 1492 * @req: request handle 1493 * @src: source scatter / gather list 1494 * @dst: destination scatter / gather list 1495 * @cryptlen: number of bytes to process from @src 1496 * @iv: IV for the cipher operation which must comply with the IV size defined 1497 * by crypto_aead_ivsize() 1498 * 1499 * Setting the source data and destination data scatter / gather lists. 1500 * 1501 * For encryption, the source is treated as the plaintext and the 1502 * destination is the ciphertext. For a decryption operation, the use is 1503 * reversed - the source is the ciphertext and the destination is the plaintext. 1504 * 1505 * IMPORTANT NOTE AEAD requires an authentication tag (MAC). For decryption, 1506 * the caller must concatenate the ciphertext followed by the 1507 * authentication tag and provide the entire data stream to the 1508 * decryption operation (i.e. the data length used for the 1509 * initialization of the scatterlist and the data length for the 1510 * decryption operation is identical). For encryption, however, 1511 * the authentication tag is created while encrypting the data. 1512 * The destination buffer must hold sufficient space for the 1513 * ciphertext and the authentication tag while the encryption 1514 * invocation must only point to the plaintext data size. The 1515 * following code snippet illustrates the memory usage 1516 * buffer = kmalloc(ptbuflen + (enc ? authsize : 0)); 1517 * sg_init_one(&sg, buffer, ptbuflen + (enc ? authsize : 0)); 1518 * aead_request_set_crypt(req, &sg, &sg, ptbuflen, iv); 1519 */ 1520 static inline void aead_request_set_crypt(struct aead_request *req, 1521 struct scatterlist *src, 1522 struct scatterlist *dst, 1523 unsigned int cryptlen, u8 *iv) 1524 { 1525 req->src = src; 1526 req->dst = dst; 1527 req->cryptlen = cryptlen; 1528 req->iv = iv; 1529 } 1530 1531 /** 1532 * aead_request_set_assoc() - set the associated data scatter / gather list 1533 * @req: request handle 1534 * @assoc: associated data scatter / gather list 1535 * @assoclen: number of bytes to process from @assoc 1536 * 1537 * For encryption, the memory is filled with the associated data. For 1538 * decryption, the memory must point to the associated data. 1539 */ 1540 static inline void aead_request_set_assoc(struct aead_request *req, 1541 struct scatterlist *assoc, 1542 unsigned int assoclen) 1543 { 1544 req->assoc = assoc; 1545 req->assoclen = assoclen; 1546 } 1547 1548 /** 1549 * DOC: Synchronous Block Cipher API 1550 * 1551 * The synchronous block cipher API is used with the ciphers of type 1552 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto) 1553 * 1554 * Synchronous calls, have a context in the tfm. But since a single tfm can be 1555 * used in multiple calls and in parallel, this info should not be changeable 1556 * (unless a lock is used). This applies, for example, to the symmetric key. 1557 * However, the IV is changeable, so there is an iv field in blkcipher_tfm 1558 * structure for synchronous blkcipher api. So, its the only state info that can 1559 * be kept for synchronous calls without using a big lock across a tfm. 1560 * 1561 * The block cipher API allows the use of a complete cipher, i.e. a cipher 1562 * consisting of a template (a block chaining mode) and a single block cipher 1563 * primitive (e.g. AES). 1564 * 1565 * The plaintext data buffer and the ciphertext data buffer are pointed to 1566 * by using scatter/gather lists. The cipher operation is performed 1567 * on all segments of the provided scatter/gather lists. 1568 * 1569 * The kernel crypto API supports a cipher operation "in-place" which means that 1570 * the caller may provide the same scatter/gather list for the plaintext and 1571 * cipher text. After the completion of the cipher operation, the plaintext 1572 * data is replaced with the ciphertext data in case of an encryption and vice 1573 * versa for a decryption. The caller must ensure that the scatter/gather lists 1574 * for the output data point to sufficiently large buffers, i.e. multiples of 1575 * the block size of the cipher. 1576 */ 1577 1578 static inline struct crypto_blkcipher *__crypto_blkcipher_cast( 1579 struct crypto_tfm *tfm) 1580 { 1581 return (struct crypto_blkcipher *)tfm; 1582 } 1583 1584 static inline struct crypto_blkcipher *crypto_blkcipher_cast( 1585 struct crypto_tfm *tfm) 1586 { 1587 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER); 1588 return __crypto_blkcipher_cast(tfm); 1589 } 1590 1591 /** 1592 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle 1593 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 1594 * blkcipher cipher 1595 * @type: specifies the type of the cipher 1596 * @mask: specifies the mask for the cipher 1597 * 1598 * Allocate a cipher handle for a block cipher. The returned struct 1599 * crypto_blkcipher is the cipher handle that is required for any subsequent 1600 * API invocation for that block cipher. 1601 * 1602 * Return: allocated cipher handle in case of success; IS_ERR() is true in case 1603 * of an error, PTR_ERR() returns the error code. 1604 */ 1605 static inline struct crypto_blkcipher *crypto_alloc_blkcipher( 1606 const char *alg_name, u32 type, u32 mask) 1607 { 1608 type &= ~CRYPTO_ALG_TYPE_MASK; 1609 type |= CRYPTO_ALG_TYPE_BLKCIPHER; 1610 mask |= CRYPTO_ALG_TYPE_MASK; 1611 1612 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask)); 1613 } 1614 1615 static inline struct crypto_tfm *crypto_blkcipher_tfm( 1616 struct crypto_blkcipher *tfm) 1617 { 1618 return &tfm->base; 1619 } 1620 1621 /** 1622 * crypto_free_blkcipher() - zeroize and free the block cipher handle 1623 * @tfm: cipher handle to be freed 1624 */ 1625 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm) 1626 { 1627 crypto_free_tfm(crypto_blkcipher_tfm(tfm)); 1628 } 1629 1630 /** 1631 * crypto_has_blkcipher() - Search for the availability of a block cipher 1632 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 1633 * block cipher 1634 * @type: specifies the type of the cipher 1635 * @mask: specifies the mask for the cipher 1636 * 1637 * Return: true when the block cipher is known to the kernel crypto API; false 1638 * otherwise 1639 */ 1640 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask) 1641 { 1642 type &= ~CRYPTO_ALG_TYPE_MASK; 1643 type |= CRYPTO_ALG_TYPE_BLKCIPHER; 1644 mask |= CRYPTO_ALG_TYPE_MASK; 1645 1646 return crypto_has_alg(alg_name, type, mask); 1647 } 1648 1649 /** 1650 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle 1651 * @tfm: cipher handle 1652 * 1653 * Return: The character string holding the name of the cipher 1654 */ 1655 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm) 1656 { 1657 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm)); 1658 } 1659 1660 static inline struct blkcipher_tfm *crypto_blkcipher_crt( 1661 struct crypto_blkcipher *tfm) 1662 { 1663 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher; 1664 } 1665 1666 static inline struct blkcipher_alg *crypto_blkcipher_alg( 1667 struct crypto_blkcipher *tfm) 1668 { 1669 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher; 1670 } 1671 1672 /** 1673 * crypto_blkcipher_ivsize() - obtain IV size 1674 * @tfm: cipher handle 1675 * 1676 * The size of the IV for the block cipher referenced by the cipher handle is 1677 * returned. This IV size may be zero if the cipher does not need an IV. 1678 * 1679 * Return: IV size in bytes 1680 */ 1681 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm) 1682 { 1683 return crypto_blkcipher_alg(tfm)->ivsize; 1684 } 1685 1686 /** 1687 * crypto_blkcipher_blocksize() - obtain block size of cipher 1688 * @tfm: cipher handle 1689 * 1690 * The block size for the block cipher referenced with the cipher handle is 1691 * returned. The caller may use that information to allocate appropriate 1692 * memory for the data returned by the encryption or decryption operation. 1693 * 1694 * Return: block size of cipher 1695 */ 1696 static inline unsigned int crypto_blkcipher_blocksize( 1697 struct crypto_blkcipher *tfm) 1698 { 1699 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm)); 1700 } 1701 1702 static inline unsigned int crypto_blkcipher_alignmask( 1703 struct crypto_blkcipher *tfm) 1704 { 1705 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm)); 1706 } 1707 1708 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm) 1709 { 1710 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm)); 1711 } 1712 1713 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm, 1714 u32 flags) 1715 { 1716 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags); 1717 } 1718 1719 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm, 1720 u32 flags) 1721 { 1722 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags); 1723 } 1724 1725 /** 1726 * crypto_blkcipher_setkey() - set key for cipher 1727 * @tfm: cipher handle 1728 * @key: buffer holding the key 1729 * @keylen: length of the key in bytes 1730 * 1731 * The caller provided key is set for the block cipher referenced by the cipher 1732 * handle. 1733 * 1734 * Note, the key length determines the cipher type. Many block ciphers implement 1735 * different cipher modes depending on the key size, such as AES-128 vs AES-192 1736 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 1737 * is performed. 1738 * 1739 * Return: 0 if the setting of the key was successful; < 0 if an error occurred 1740 */ 1741 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm, 1742 const u8 *key, unsigned int keylen) 1743 { 1744 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm), 1745 key, keylen); 1746 } 1747 1748 /** 1749 * crypto_blkcipher_encrypt() - encrypt plaintext 1750 * @desc: reference to the block cipher handle with meta data 1751 * @dst: scatter/gather list that is filled by the cipher operation with the 1752 * ciphertext 1753 * @src: scatter/gather list that holds the plaintext 1754 * @nbytes: number of bytes of the plaintext to encrypt. 1755 * 1756 * Encrypt plaintext data using the IV set by the caller with a preceding 1757 * call of crypto_blkcipher_set_iv. 1758 * 1759 * The blkcipher_desc data structure must be filled by the caller and can 1760 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled 1761 * with the block cipher handle; desc.flags is filled with either 1762 * CRYPTO_TFM_REQ_MAY_SLEEP or 0. 1763 * 1764 * Return: 0 if the cipher operation was successful; < 0 if an error occurred 1765 */ 1766 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc, 1767 struct scatterlist *dst, 1768 struct scatterlist *src, 1769 unsigned int nbytes) 1770 { 1771 desc->info = crypto_blkcipher_crt(desc->tfm)->iv; 1772 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes); 1773 } 1774 1775 /** 1776 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV 1777 * @desc: reference to the block cipher handle with meta data 1778 * @dst: scatter/gather list that is filled by the cipher operation with the 1779 * ciphertext 1780 * @src: scatter/gather list that holds the plaintext 1781 * @nbytes: number of bytes of the plaintext to encrypt. 1782 * 1783 * Encrypt plaintext data with the use of an IV that is solely used for this 1784 * cipher operation. Any previously set IV is not used. 1785 * 1786 * The blkcipher_desc data structure must be filled by the caller and can 1787 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled 1788 * with the block cipher handle; desc.info is filled with the IV to be used for 1789 * the current operation; desc.flags is filled with either 1790 * CRYPTO_TFM_REQ_MAY_SLEEP or 0. 1791 * 1792 * Return: 0 if the cipher operation was successful; < 0 if an error occurred 1793 */ 1794 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc, 1795 struct scatterlist *dst, 1796 struct scatterlist *src, 1797 unsigned int nbytes) 1798 { 1799 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes); 1800 } 1801 1802 /** 1803 * crypto_blkcipher_decrypt() - decrypt ciphertext 1804 * @desc: reference to the block cipher handle with meta data 1805 * @dst: scatter/gather list that is filled by the cipher operation with the 1806 * plaintext 1807 * @src: scatter/gather list that holds the ciphertext 1808 * @nbytes: number of bytes of the ciphertext to decrypt. 1809 * 1810 * Decrypt ciphertext data using the IV set by the caller with a preceding 1811 * call of crypto_blkcipher_set_iv. 1812 * 1813 * The blkcipher_desc data structure must be filled by the caller as documented 1814 * for the crypto_blkcipher_encrypt call above. 1815 * 1816 * Return: 0 if the cipher operation was successful; < 0 if an error occurred 1817 * 1818 */ 1819 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc, 1820 struct scatterlist *dst, 1821 struct scatterlist *src, 1822 unsigned int nbytes) 1823 { 1824 desc->info = crypto_blkcipher_crt(desc->tfm)->iv; 1825 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes); 1826 } 1827 1828 /** 1829 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV 1830 * @desc: reference to the block cipher handle with meta data 1831 * @dst: scatter/gather list that is filled by the cipher operation with the 1832 * plaintext 1833 * @src: scatter/gather list that holds the ciphertext 1834 * @nbytes: number of bytes of the ciphertext to decrypt. 1835 * 1836 * Decrypt ciphertext data with the use of an IV that is solely used for this 1837 * cipher operation. Any previously set IV is not used. 1838 * 1839 * The blkcipher_desc data structure must be filled by the caller as documented 1840 * for the crypto_blkcipher_encrypt_iv call above. 1841 * 1842 * Return: 0 if the cipher operation was successful; < 0 if an error occurred 1843 */ 1844 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc, 1845 struct scatterlist *dst, 1846 struct scatterlist *src, 1847 unsigned int nbytes) 1848 { 1849 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes); 1850 } 1851 1852 /** 1853 * crypto_blkcipher_set_iv() - set IV for cipher 1854 * @tfm: cipher handle 1855 * @src: buffer holding the IV 1856 * @len: length of the IV in bytes 1857 * 1858 * The caller provided IV is set for the block cipher referenced by the cipher 1859 * handle. 1860 */ 1861 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm, 1862 const u8 *src, unsigned int len) 1863 { 1864 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len); 1865 } 1866 1867 /** 1868 * crypto_blkcipher_get_iv() - obtain IV from cipher 1869 * @tfm: cipher handle 1870 * @dst: buffer filled with the IV 1871 * @len: length of the buffer dst 1872 * 1873 * The caller can obtain the IV set for the block cipher referenced by the 1874 * cipher handle and store it into the user-provided buffer. If the buffer 1875 * has an insufficient space, the IV is truncated to fit the buffer. 1876 */ 1877 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm, 1878 u8 *dst, unsigned int len) 1879 { 1880 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len); 1881 } 1882 1883 /** 1884 * DOC: Single Block Cipher API 1885 * 1886 * The single block cipher API is used with the ciphers of type 1887 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto). 1888 * 1889 * Using the single block cipher API calls, operations with the basic cipher 1890 * primitive can be implemented. These cipher primitives exclude any block 1891 * chaining operations including IV handling. 1892 * 1893 * The purpose of this single block cipher API is to support the implementation 1894 * of templates or other concepts that only need to perform the cipher operation 1895 * on one block at a time. Templates invoke the underlying cipher primitive 1896 * block-wise and process either the input or the output data of these cipher 1897 * operations. 1898 */ 1899 1900 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm) 1901 { 1902 return (struct crypto_cipher *)tfm; 1903 } 1904 1905 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm) 1906 { 1907 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); 1908 return __crypto_cipher_cast(tfm); 1909 } 1910 1911 /** 1912 * crypto_alloc_cipher() - allocate single block cipher handle 1913 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 1914 * single block cipher 1915 * @type: specifies the type of the cipher 1916 * @mask: specifies the mask for the cipher 1917 * 1918 * Allocate a cipher handle for a single block cipher. The returned struct 1919 * crypto_cipher is the cipher handle that is required for any subsequent API 1920 * invocation for that single block cipher. 1921 * 1922 * Return: allocated cipher handle in case of success; IS_ERR() is true in case 1923 * of an error, PTR_ERR() returns the error code. 1924 */ 1925 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name, 1926 u32 type, u32 mask) 1927 { 1928 type &= ~CRYPTO_ALG_TYPE_MASK; 1929 type |= CRYPTO_ALG_TYPE_CIPHER; 1930 mask |= CRYPTO_ALG_TYPE_MASK; 1931 1932 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask)); 1933 } 1934 1935 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm) 1936 { 1937 return &tfm->base; 1938 } 1939 1940 /** 1941 * crypto_free_cipher() - zeroize and free the single block cipher handle 1942 * @tfm: cipher handle to be freed 1943 */ 1944 static inline void crypto_free_cipher(struct crypto_cipher *tfm) 1945 { 1946 crypto_free_tfm(crypto_cipher_tfm(tfm)); 1947 } 1948 1949 /** 1950 * crypto_has_cipher() - Search for the availability of a single block cipher 1951 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 1952 * single block cipher 1953 * @type: specifies the type of the cipher 1954 * @mask: specifies the mask for the cipher 1955 * 1956 * Return: true when the single block cipher is known to the kernel crypto API; 1957 * false otherwise 1958 */ 1959 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask) 1960 { 1961 type &= ~CRYPTO_ALG_TYPE_MASK; 1962 type |= CRYPTO_ALG_TYPE_CIPHER; 1963 mask |= CRYPTO_ALG_TYPE_MASK; 1964 1965 return crypto_has_alg(alg_name, type, mask); 1966 } 1967 1968 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm) 1969 { 1970 return &crypto_cipher_tfm(tfm)->crt_cipher; 1971 } 1972 1973 /** 1974 * crypto_cipher_blocksize() - obtain block size for cipher 1975 * @tfm: cipher handle 1976 * 1977 * The block size for the single block cipher referenced with the cipher handle 1978 * tfm is returned. The caller may use that information to allocate appropriate 1979 * memory for the data returned by the encryption or decryption operation 1980 * 1981 * Return: block size of cipher 1982 */ 1983 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm) 1984 { 1985 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm)); 1986 } 1987 1988 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm) 1989 { 1990 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm)); 1991 } 1992 1993 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm) 1994 { 1995 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm)); 1996 } 1997 1998 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm, 1999 u32 flags) 2000 { 2001 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags); 2002 } 2003 2004 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm, 2005 u32 flags) 2006 { 2007 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags); 2008 } 2009 2010 /** 2011 * crypto_cipher_setkey() - set key for cipher 2012 * @tfm: cipher handle 2013 * @key: buffer holding the key 2014 * @keylen: length of the key in bytes 2015 * 2016 * The caller provided key is set for the single block cipher referenced by the 2017 * cipher handle. 2018 * 2019 * Note, the key length determines the cipher type. Many block ciphers implement 2020 * different cipher modes depending on the key size, such as AES-128 vs AES-192 2021 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 2022 * is performed. 2023 * 2024 * Return: 0 if the setting of the key was successful; < 0 if an error occurred 2025 */ 2026 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm, 2027 const u8 *key, unsigned int keylen) 2028 { 2029 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm), 2030 key, keylen); 2031 } 2032 2033 /** 2034 * crypto_cipher_encrypt_one() - encrypt one block of plaintext 2035 * @tfm: cipher handle 2036 * @dst: points to the buffer that will be filled with the ciphertext 2037 * @src: buffer holding the plaintext to be encrypted 2038 * 2039 * Invoke the encryption operation of one block. The caller must ensure that 2040 * the plaintext and ciphertext buffers are at least one block in size. 2041 */ 2042 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm, 2043 u8 *dst, const u8 *src) 2044 { 2045 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm), 2046 dst, src); 2047 } 2048 2049 /** 2050 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext 2051 * @tfm: cipher handle 2052 * @dst: points to the buffer that will be filled with the plaintext 2053 * @src: buffer holding the ciphertext to be decrypted 2054 * 2055 * Invoke the decryption operation of one block. The caller must ensure that 2056 * the plaintext and ciphertext buffers are at least one block in size. 2057 */ 2058 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm, 2059 u8 *dst, const u8 *src) 2060 { 2061 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm), 2062 dst, src); 2063 } 2064 2065 /** 2066 * DOC: Synchronous Message Digest API 2067 * 2068 * The synchronous message digest API is used with the ciphers of type 2069 * CRYPTO_ALG_TYPE_HASH (listed as type "hash" in /proc/crypto) 2070 */ 2071 2072 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm) 2073 { 2074 return (struct crypto_hash *)tfm; 2075 } 2076 2077 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm) 2078 { 2079 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) & 2080 CRYPTO_ALG_TYPE_HASH_MASK); 2081 return __crypto_hash_cast(tfm); 2082 } 2083 2084 /** 2085 * crypto_alloc_hash() - allocate synchronous message digest handle 2086 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 2087 * message digest cipher 2088 * @type: specifies the type of the cipher 2089 * @mask: specifies the mask for the cipher 2090 * 2091 * Allocate a cipher handle for a message digest. The returned struct 2092 * crypto_hash is the cipher handle that is required for any subsequent 2093 * API invocation for that message digest. 2094 * 2095 * Return: allocated cipher handle in case of success; IS_ERR() is true in case 2096 * of an error, PTR_ERR() returns the error code. 2097 */ 2098 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name, 2099 u32 type, u32 mask) 2100 { 2101 type &= ~CRYPTO_ALG_TYPE_MASK; 2102 mask &= ~CRYPTO_ALG_TYPE_MASK; 2103 type |= CRYPTO_ALG_TYPE_HASH; 2104 mask |= CRYPTO_ALG_TYPE_HASH_MASK; 2105 2106 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask)); 2107 } 2108 2109 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm) 2110 { 2111 return &tfm->base; 2112 } 2113 2114 /** 2115 * crypto_free_hash() - zeroize and free message digest handle 2116 * @tfm: cipher handle to be freed 2117 */ 2118 static inline void crypto_free_hash(struct crypto_hash *tfm) 2119 { 2120 crypto_free_tfm(crypto_hash_tfm(tfm)); 2121 } 2122 2123 /** 2124 * crypto_has_hash() - Search for the availability of a message digest 2125 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 2126 * message digest cipher 2127 * @type: specifies the type of the cipher 2128 * @mask: specifies the mask for the cipher 2129 * 2130 * Return: true when the message digest cipher is known to the kernel crypto 2131 * API; false otherwise 2132 */ 2133 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask) 2134 { 2135 type &= ~CRYPTO_ALG_TYPE_MASK; 2136 mask &= ~CRYPTO_ALG_TYPE_MASK; 2137 type |= CRYPTO_ALG_TYPE_HASH; 2138 mask |= CRYPTO_ALG_TYPE_HASH_MASK; 2139 2140 return crypto_has_alg(alg_name, type, mask); 2141 } 2142 2143 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm) 2144 { 2145 return &crypto_hash_tfm(tfm)->crt_hash; 2146 } 2147 2148 /** 2149 * crypto_hash_blocksize() - obtain block size for message digest 2150 * @tfm: cipher handle 2151 * 2152 * The block size for the message digest cipher referenced with the cipher 2153 * handle is returned. 2154 * 2155 * Return: block size of cipher 2156 */ 2157 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm) 2158 { 2159 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm)); 2160 } 2161 2162 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm) 2163 { 2164 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm)); 2165 } 2166 2167 /** 2168 * crypto_hash_digestsize() - obtain message digest size 2169 * @tfm: cipher handle 2170 * 2171 * The size for the message digest created by the message digest cipher 2172 * referenced with the cipher handle is returned. 2173 * 2174 * Return: message digest size 2175 */ 2176 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm) 2177 { 2178 return crypto_hash_crt(tfm)->digestsize; 2179 } 2180 2181 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm) 2182 { 2183 return crypto_tfm_get_flags(crypto_hash_tfm(tfm)); 2184 } 2185 2186 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags) 2187 { 2188 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags); 2189 } 2190 2191 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags) 2192 { 2193 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags); 2194 } 2195 2196 /** 2197 * crypto_hash_init() - (re)initialize message digest handle 2198 * @desc: cipher request handle that to be filled by caller -- 2199 * desc.tfm is filled with the hash cipher handle; 2200 * desc.flags is filled with either CRYPTO_TFM_REQ_MAY_SLEEP or 0. 2201 * 2202 * The call (re-)initializes the message digest referenced by the hash cipher 2203 * request handle. Any potentially existing state created by previous 2204 * operations is discarded. 2205 * 2206 * Return: 0 if the message digest initialization was successful; < 0 if an 2207 * error occurred 2208 */ 2209 static inline int crypto_hash_init(struct hash_desc *desc) 2210 { 2211 return crypto_hash_crt(desc->tfm)->init(desc); 2212 } 2213 2214 /** 2215 * crypto_hash_update() - add data to message digest for processing 2216 * @desc: cipher request handle 2217 * @sg: scatter / gather list pointing to the data to be added to the message 2218 * digest 2219 * @nbytes: number of bytes to be processed from @sg 2220 * 2221 * Updates the message digest state of the cipher handle pointed to by the 2222 * hash cipher request handle with the input data pointed to by the 2223 * scatter/gather list. 2224 * 2225 * Return: 0 if the message digest update was successful; < 0 if an error 2226 * occurred 2227 */ 2228 static inline int crypto_hash_update(struct hash_desc *desc, 2229 struct scatterlist *sg, 2230 unsigned int nbytes) 2231 { 2232 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes); 2233 } 2234 2235 /** 2236 * crypto_hash_final() - calculate message digest 2237 * @desc: cipher request handle 2238 * @out: message digest output buffer -- The caller must ensure that the out 2239 * buffer has a sufficient size (e.g. by using the crypto_hash_digestsize 2240 * function). 2241 * 2242 * Finalize the message digest operation and create the message digest 2243 * based on all data added to the cipher handle. The message digest is placed 2244 * into the output buffer. 2245 * 2246 * Return: 0 if the message digest creation was successful; < 0 if an error 2247 * occurred 2248 */ 2249 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out) 2250 { 2251 return crypto_hash_crt(desc->tfm)->final(desc, out); 2252 } 2253 2254 /** 2255 * crypto_hash_digest() - calculate message digest for a buffer 2256 * @desc: see crypto_hash_final() 2257 * @sg: see crypto_hash_update() 2258 * @nbytes: see crypto_hash_update() 2259 * @out: see crypto_hash_final() 2260 * 2261 * This function is a "short-hand" for the function calls of crypto_hash_init, 2262 * crypto_hash_update and crypto_hash_final. The parameters have the same 2263 * meaning as discussed for those separate three functions. 2264 * 2265 * Return: 0 if the message digest creation was successful; < 0 if an error 2266 * occurred 2267 */ 2268 static inline int crypto_hash_digest(struct hash_desc *desc, 2269 struct scatterlist *sg, 2270 unsigned int nbytes, u8 *out) 2271 { 2272 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out); 2273 } 2274 2275 /** 2276 * crypto_hash_setkey() - set key for message digest 2277 * @hash: cipher handle 2278 * @key: buffer holding the key 2279 * @keylen: length of the key in bytes 2280 * 2281 * The caller provided key is set for the message digest cipher. The cipher 2282 * handle must point to a keyed hash in order for this function to succeed. 2283 * 2284 * Return: 0 if the setting of the key was successful; < 0 if an error occurred 2285 */ 2286 static inline int crypto_hash_setkey(struct crypto_hash *hash, 2287 const u8 *key, unsigned int keylen) 2288 { 2289 return crypto_hash_crt(hash)->setkey(hash, key, keylen); 2290 } 2291 2292 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm) 2293 { 2294 return (struct crypto_comp *)tfm; 2295 } 2296 2297 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm) 2298 { 2299 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) & 2300 CRYPTO_ALG_TYPE_MASK); 2301 return __crypto_comp_cast(tfm); 2302 } 2303 2304 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name, 2305 u32 type, u32 mask) 2306 { 2307 type &= ~CRYPTO_ALG_TYPE_MASK; 2308 type |= CRYPTO_ALG_TYPE_COMPRESS; 2309 mask |= CRYPTO_ALG_TYPE_MASK; 2310 2311 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask)); 2312 } 2313 2314 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm) 2315 { 2316 return &tfm->base; 2317 } 2318 2319 static inline void crypto_free_comp(struct crypto_comp *tfm) 2320 { 2321 crypto_free_tfm(crypto_comp_tfm(tfm)); 2322 } 2323 2324 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask) 2325 { 2326 type &= ~CRYPTO_ALG_TYPE_MASK; 2327 type |= CRYPTO_ALG_TYPE_COMPRESS; 2328 mask |= CRYPTO_ALG_TYPE_MASK; 2329 2330 return crypto_has_alg(alg_name, type, mask); 2331 } 2332 2333 static inline const char *crypto_comp_name(struct crypto_comp *tfm) 2334 { 2335 return crypto_tfm_alg_name(crypto_comp_tfm(tfm)); 2336 } 2337 2338 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm) 2339 { 2340 return &crypto_comp_tfm(tfm)->crt_compress; 2341 } 2342 2343 static inline int crypto_comp_compress(struct crypto_comp *tfm, 2344 const u8 *src, unsigned int slen, 2345 u8 *dst, unsigned int *dlen) 2346 { 2347 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm), 2348 src, slen, dst, dlen); 2349 } 2350 2351 static inline int crypto_comp_decompress(struct crypto_comp *tfm, 2352 const u8 *src, unsigned int slen, 2353 u8 *dst, unsigned int *dlen) 2354 { 2355 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm), 2356 src, slen, dst, dlen); 2357 } 2358 2359 #endif /* _LINUX_CRYPTO_H */ 2360 2361