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_request; 144 struct aead_givcrypt_request; 145 struct skcipher_givcrypt_request; 146 147 typedef void (*crypto_completion_t)(struct crypto_async_request *req, int err); 148 149 /** 150 * DOC: Block Cipher Context Data Structures 151 * 152 * These data structures define the operating context for each block cipher 153 * type. 154 */ 155 156 struct crypto_async_request { 157 struct list_head list; 158 crypto_completion_t complete; 159 void *data; 160 struct crypto_tfm *tfm; 161 162 u32 flags; 163 }; 164 165 struct ablkcipher_request { 166 struct crypto_async_request base; 167 168 unsigned int nbytes; 169 170 void *info; 171 172 struct scatterlist *src; 173 struct scatterlist *dst; 174 175 void *__ctx[] CRYPTO_MINALIGN_ATTR; 176 }; 177 178 struct blkcipher_desc { 179 struct crypto_blkcipher *tfm; 180 void *info; 181 u32 flags; 182 }; 183 184 struct cipher_desc { 185 struct crypto_tfm *tfm; 186 void (*crfn)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 187 unsigned int (*prfn)(const struct cipher_desc *desc, u8 *dst, 188 const u8 *src, unsigned int nbytes); 189 void *info; 190 }; 191 192 struct hash_desc { 193 struct crypto_hash *tfm; 194 u32 flags; 195 }; 196 197 /** 198 * DOC: Block Cipher Algorithm Definitions 199 * 200 * These data structures define modular crypto algorithm implementations, 201 * managed via crypto_register_alg() and crypto_unregister_alg(). 202 */ 203 204 /** 205 * struct ablkcipher_alg - asynchronous block cipher definition 206 * @min_keysize: Minimum key size supported by the transformation. This is the 207 * smallest key length supported by this transformation algorithm. 208 * This must be set to one of the pre-defined values as this is 209 * not hardware specific. Possible values for this field can be 210 * found via git grep "_MIN_KEY_SIZE" include/crypto/ 211 * @max_keysize: Maximum key size supported by the transformation. This is the 212 * largest key length supported by this transformation algorithm. 213 * This must be set to one of the pre-defined values as this is 214 * not hardware specific. Possible values for this field can be 215 * found via git grep "_MAX_KEY_SIZE" include/crypto/ 216 * @setkey: Set key for the transformation. This function is used to either 217 * program a supplied key into the hardware or store the key in the 218 * transformation context for programming it later. Note that this 219 * function does modify the transformation context. This function can 220 * be called multiple times during the existence of the transformation 221 * object, so one must make sure the key is properly reprogrammed into 222 * the hardware. This function is also responsible for checking the key 223 * length for validity. In case a software fallback was put in place in 224 * the @cra_init call, this function might need to use the fallback if 225 * the algorithm doesn't support all of the key sizes. 226 * @encrypt: Encrypt a scatterlist of blocks. This function is used to encrypt 227 * the supplied scatterlist containing the blocks of data. The crypto 228 * API consumer is responsible for aligning the entries of the 229 * scatterlist properly and making sure the chunks are correctly 230 * sized. In case a software fallback was put in place in the 231 * @cra_init call, this function might need to use the fallback if 232 * the algorithm doesn't support all of the key sizes. In case the 233 * key was stored in transformation context, the key might need to be 234 * re-programmed into the hardware in this function. This function 235 * shall not modify the transformation context, as this function may 236 * be called in parallel with the same transformation object. 237 * @decrypt: Decrypt a single block. This is a reverse counterpart to @encrypt 238 * and the conditions are exactly the same. 239 * @givencrypt: Update the IV for encryption. With this function, a cipher 240 * implementation may provide the function on how to update the IV 241 * for encryption. 242 * @givdecrypt: Update the IV for decryption. This is the reverse of 243 * @givencrypt . 244 * @geniv: The transformation implementation may use an "IV generator" provided 245 * by the kernel crypto API. Several use cases have a predefined 246 * approach how IVs are to be updated. For such use cases, the kernel 247 * crypto API provides ready-to-use implementations that can be 248 * referenced with this variable. 249 * @ivsize: IV size applicable for transformation. The consumer must provide an 250 * IV of exactly that size to perform the encrypt or decrypt operation. 251 * 252 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are 253 * mandatory and must be filled. 254 */ 255 struct ablkcipher_alg { 256 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key, 257 unsigned int keylen); 258 int (*encrypt)(struct ablkcipher_request *req); 259 int (*decrypt)(struct ablkcipher_request *req); 260 int (*givencrypt)(struct skcipher_givcrypt_request *req); 261 int (*givdecrypt)(struct skcipher_givcrypt_request *req); 262 263 const char *geniv; 264 265 unsigned int min_keysize; 266 unsigned int max_keysize; 267 unsigned int ivsize; 268 }; 269 270 /** 271 * struct aead_alg - AEAD cipher definition 272 * @maxauthsize: Set the maximum authentication tag size supported by the 273 * transformation. A transformation may support smaller tag sizes. 274 * As the authentication tag is a message digest to ensure the 275 * integrity of the encrypted data, a consumer typically wants the 276 * largest authentication tag possible as defined by this 277 * variable. 278 * @setauthsize: Set authentication size for the AEAD transformation. This 279 * function is used to specify the consumer requested size of the 280 * authentication tag to be either generated by the transformation 281 * during encryption or the size of the authentication tag to be 282 * supplied during the decryption operation. This function is also 283 * responsible for checking the authentication tag size for 284 * validity. 285 * @setkey: see struct ablkcipher_alg 286 * @encrypt: see struct ablkcipher_alg 287 * @decrypt: see struct ablkcipher_alg 288 * @givencrypt: see struct ablkcipher_alg 289 * @givdecrypt: see struct ablkcipher_alg 290 * @geniv: see struct ablkcipher_alg 291 * @ivsize: see struct ablkcipher_alg 292 * 293 * All fields except @givencrypt , @givdecrypt , @geniv and @ivsize are 294 * mandatory and must be filled. 295 */ 296 struct aead_alg { 297 int (*setkey)(struct crypto_aead *tfm, const u8 *key, 298 unsigned int keylen); 299 int (*setauthsize)(struct crypto_aead *tfm, unsigned int authsize); 300 int (*encrypt)(struct aead_request *req); 301 int (*decrypt)(struct aead_request *req); 302 int (*givencrypt)(struct aead_givcrypt_request *req); 303 int (*givdecrypt)(struct aead_givcrypt_request *req); 304 305 const char *geniv; 306 307 unsigned int ivsize; 308 unsigned int maxauthsize; 309 }; 310 311 /** 312 * struct blkcipher_alg - synchronous block cipher definition 313 * @min_keysize: see struct ablkcipher_alg 314 * @max_keysize: see struct ablkcipher_alg 315 * @setkey: see struct ablkcipher_alg 316 * @encrypt: see struct ablkcipher_alg 317 * @decrypt: see struct ablkcipher_alg 318 * @geniv: see struct ablkcipher_alg 319 * @ivsize: see struct ablkcipher_alg 320 * 321 * All fields except @geniv and @ivsize are mandatory and must be filled. 322 */ 323 struct blkcipher_alg { 324 int (*setkey)(struct crypto_tfm *tfm, const u8 *key, 325 unsigned int keylen); 326 int (*encrypt)(struct blkcipher_desc *desc, 327 struct scatterlist *dst, struct scatterlist *src, 328 unsigned int nbytes); 329 int (*decrypt)(struct blkcipher_desc *desc, 330 struct scatterlist *dst, struct scatterlist *src, 331 unsigned int nbytes); 332 333 const char *geniv; 334 335 unsigned int min_keysize; 336 unsigned int max_keysize; 337 unsigned int ivsize; 338 }; 339 340 /** 341 * struct cipher_alg - single-block symmetric ciphers definition 342 * @cia_min_keysize: Minimum key size supported by the transformation. This is 343 * the smallest key length supported by this transformation 344 * algorithm. This must be set to one of the pre-defined 345 * values as this is not hardware specific. Possible values 346 * for this field can be found via git grep "_MIN_KEY_SIZE" 347 * include/crypto/ 348 * @cia_max_keysize: Maximum key size supported by the transformation. This is 349 * the largest key length supported by this transformation 350 * algorithm. This must be set to one of the pre-defined values 351 * as this is not hardware specific. Possible values for this 352 * field can be found via git grep "_MAX_KEY_SIZE" 353 * include/crypto/ 354 * @cia_setkey: Set key for the transformation. This function is used to either 355 * program a supplied key into the hardware or store the key in the 356 * transformation context for programming it later. Note that this 357 * function does modify the transformation context. This function 358 * can be called multiple times during the existence of the 359 * transformation object, so one must make sure the key is properly 360 * reprogrammed into the hardware. This function is also 361 * responsible for checking the key length for validity. 362 * @cia_encrypt: Encrypt a single block. This function is used to encrypt a 363 * single block of data, which must be @cra_blocksize big. This 364 * always operates on a full @cra_blocksize and it is not possible 365 * to encrypt a block of smaller size. The supplied buffers must 366 * therefore also be at least of @cra_blocksize size. Both the 367 * input and output buffers are always aligned to @cra_alignmask. 368 * In case either of the input or output buffer supplied by user 369 * of the crypto API is not aligned to @cra_alignmask, the crypto 370 * API will re-align the buffers. The re-alignment means that a 371 * new buffer will be allocated, the data will be copied into the 372 * new buffer, then the processing will happen on the new buffer, 373 * then the data will be copied back into the original buffer and 374 * finally the new buffer will be freed. In case a software 375 * fallback was put in place in the @cra_init call, this function 376 * might need to use the fallback if the algorithm doesn't support 377 * all of the key sizes. In case the key was stored in 378 * transformation context, the key might need to be re-programmed 379 * into the hardware in this function. This function shall not 380 * modify the transformation context, as this function may be 381 * called in parallel with the same transformation object. 382 * @cia_decrypt: Decrypt a single block. This is a reverse counterpart to 383 * @cia_encrypt, and the conditions are exactly the same. 384 * 385 * All fields are mandatory and must be filled. 386 */ 387 struct cipher_alg { 388 unsigned int cia_min_keysize; 389 unsigned int cia_max_keysize; 390 int (*cia_setkey)(struct crypto_tfm *tfm, const u8 *key, 391 unsigned int keylen); 392 void (*cia_encrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 393 void (*cia_decrypt)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 394 }; 395 396 struct compress_alg { 397 int (*coa_compress)(struct crypto_tfm *tfm, const u8 *src, 398 unsigned int slen, u8 *dst, unsigned int *dlen); 399 int (*coa_decompress)(struct crypto_tfm *tfm, const u8 *src, 400 unsigned int slen, u8 *dst, unsigned int *dlen); 401 }; 402 403 404 #define cra_ablkcipher cra_u.ablkcipher 405 #define cra_aead cra_u.aead 406 #define cra_blkcipher cra_u.blkcipher 407 #define cra_cipher cra_u.cipher 408 #define cra_compress cra_u.compress 409 410 /** 411 * struct crypto_alg - definition of a cryptograpic cipher algorithm 412 * @cra_flags: Flags describing this transformation. See include/linux/crypto.h 413 * CRYPTO_ALG_* flags for the flags which go in here. Those are 414 * used for fine-tuning the description of the transformation 415 * algorithm. 416 * @cra_blocksize: Minimum block size of this transformation. The size in bytes 417 * of the smallest possible unit which can be transformed with 418 * this algorithm. The users must respect this value. 419 * In case of HASH transformation, it is possible for a smaller 420 * block than @cra_blocksize to be passed to the crypto API for 421 * transformation, in case of any other transformation type, an 422 * error will be returned upon any attempt to transform smaller 423 * than @cra_blocksize chunks. 424 * @cra_ctxsize: Size of the operational context of the transformation. This 425 * value informs the kernel crypto API about the memory size 426 * needed to be allocated for the transformation context. 427 * @cra_alignmask: Alignment mask for the input and output data buffer. The data 428 * buffer containing the input data for the algorithm must be 429 * aligned to this alignment mask. The data buffer for the 430 * output data must be aligned to this alignment mask. Note that 431 * the Crypto API will do the re-alignment in software, but 432 * only under special conditions and there is a performance hit. 433 * The re-alignment happens at these occasions for different 434 * @cra_u types: cipher -- For both input data and output data 435 * buffer; ahash -- For output hash destination buf; shash -- 436 * For output hash destination buf. 437 * This is needed on hardware which is flawed by design and 438 * cannot pick data from arbitrary addresses. 439 * @cra_priority: Priority of this transformation implementation. In case 440 * multiple transformations with same @cra_name are available to 441 * the Crypto API, the kernel will use the one with highest 442 * @cra_priority. 443 * @cra_name: Generic name (usable by multiple implementations) of the 444 * transformation algorithm. This is the name of the transformation 445 * itself. This field is used by the kernel when looking up the 446 * providers of particular transformation. 447 * @cra_driver_name: Unique name of the transformation provider. This is the 448 * name of the provider of the transformation. This can be any 449 * arbitrary value, but in the usual case, this contains the 450 * name of the chip or provider and the name of the 451 * transformation algorithm. 452 * @cra_type: Type of the cryptographic transformation. This is a pointer to 453 * struct crypto_type, which implements callbacks common for all 454 * trasnformation types. There are multiple options: 455 * &crypto_blkcipher_type, &crypto_ablkcipher_type, 456 * &crypto_ahash_type, &crypto_aead_type, &crypto_rng_type. 457 * This field might be empty. In that case, there are no common 458 * callbacks. This is the case for: cipher, compress, shash. 459 * @cra_u: Callbacks implementing the transformation. This is a union of 460 * multiple structures. Depending on the type of transformation selected 461 * by @cra_type and @cra_flags above, the associated structure must be 462 * filled with callbacks. This field might be empty. This is the case 463 * for ahash, shash. 464 * @cra_init: Initialize the cryptographic transformation object. This function 465 * is used to initialize the cryptographic transformation object. 466 * This function is called only once at the instantiation time, right 467 * after the transformation context was allocated. In case the 468 * cryptographic hardware has some special requirements which need to 469 * be handled by software, this function shall check for the precise 470 * requirement of the transformation and put any software fallbacks 471 * in place. 472 * @cra_exit: Deinitialize the cryptographic transformation object. This is a 473 * counterpart to @cra_init, used to remove various changes set in 474 * @cra_init. 475 * @cra_module: Owner of this transformation implementation. Set to THIS_MODULE 476 * @cra_list: internally used 477 * @cra_users: internally used 478 * @cra_refcnt: internally used 479 * @cra_destroy: internally used 480 * 481 * The struct crypto_alg describes a generic Crypto API algorithm and is common 482 * for all of the transformations. Any variable not documented here shall not 483 * be used by a cipher implementation as it is internal to the Crypto API. 484 */ 485 struct crypto_alg { 486 struct list_head cra_list; 487 struct list_head cra_users; 488 489 u32 cra_flags; 490 unsigned int cra_blocksize; 491 unsigned int cra_ctxsize; 492 unsigned int cra_alignmask; 493 494 int cra_priority; 495 atomic_t cra_refcnt; 496 497 char cra_name[CRYPTO_MAX_ALG_NAME]; 498 char cra_driver_name[CRYPTO_MAX_ALG_NAME]; 499 500 const struct crypto_type *cra_type; 501 502 union { 503 struct ablkcipher_alg ablkcipher; 504 struct aead_alg aead; 505 struct blkcipher_alg blkcipher; 506 struct cipher_alg cipher; 507 struct compress_alg compress; 508 } cra_u; 509 510 int (*cra_init)(struct crypto_tfm *tfm); 511 void (*cra_exit)(struct crypto_tfm *tfm); 512 void (*cra_destroy)(struct crypto_alg *alg); 513 514 struct module *cra_module; 515 }; 516 517 /* 518 * Algorithm registration interface. 519 */ 520 int crypto_register_alg(struct crypto_alg *alg); 521 int crypto_unregister_alg(struct crypto_alg *alg); 522 int crypto_register_algs(struct crypto_alg *algs, int count); 523 int crypto_unregister_algs(struct crypto_alg *algs, int count); 524 525 /* 526 * Algorithm query interface. 527 */ 528 int crypto_has_alg(const char *name, u32 type, u32 mask); 529 530 /* 531 * Transforms: user-instantiated objects which encapsulate algorithms 532 * and core processing logic. Managed via crypto_alloc_*() and 533 * crypto_free_*(), as well as the various helpers below. 534 */ 535 536 struct ablkcipher_tfm { 537 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key, 538 unsigned int keylen); 539 int (*encrypt)(struct ablkcipher_request *req); 540 int (*decrypt)(struct ablkcipher_request *req); 541 int (*givencrypt)(struct skcipher_givcrypt_request *req); 542 int (*givdecrypt)(struct skcipher_givcrypt_request *req); 543 544 struct crypto_ablkcipher *base; 545 546 unsigned int ivsize; 547 unsigned int reqsize; 548 }; 549 550 struct blkcipher_tfm { 551 void *iv; 552 int (*setkey)(struct crypto_tfm *tfm, const u8 *key, 553 unsigned int keylen); 554 int (*encrypt)(struct blkcipher_desc *desc, struct scatterlist *dst, 555 struct scatterlist *src, unsigned int nbytes); 556 int (*decrypt)(struct blkcipher_desc *desc, struct scatterlist *dst, 557 struct scatterlist *src, unsigned int nbytes); 558 }; 559 560 struct cipher_tfm { 561 int (*cit_setkey)(struct crypto_tfm *tfm, 562 const u8 *key, unsigned int keylen); 563 void (*cit_encrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 564 void (*cit_decrypt_one)(struct crypto_tfm *tfm, u8 *dst, const u8 *src); 565 }; 566 567 struct hash_tfm { 568 int (*init)(struct hash_desc *desc); 569 int (*update)(struct hash_desc *desc, 570 struct scatterlist *sg, unsigned int nsg); 571 int (*final)(struct hash_desc *desc, u8 *out); 572 int (*digest)(struct hash_desc *desc, struct scatterlist *sg, 573 unsigned int nsg, u8 *out); 574 int (*setkey)(struct crypto_hash *tfm, const u8 *key, 575 unsigned int keylen); 576 unsigned int digestsize; 577 }; 578 579 struct compress_tfm { 580 int (*cot_compress)(struct crypto_tfm *tfm, 581 const u8 *src, unsigned int slen, 582 u8 *dst, unsigned int *dlen); 583 int (*cot_decompress)(struct crypto_tfm *tfm, 584 const u8 *src, unsigned int slen, 585 u8 *dst, unsigned int *dlen); 586 }; 587 588 #define crt_ablkcipher crt_u.ablkcipher 589 #define crt_blkcipher crt_u.blkcipher 590 #define crt_cipher crt_u.cipher 591 #define crt_hash crt_u.hash 592 #define crt_compress crt_u.compress 593 594 struct crypto_tfm { 595 596 u32 crt_flags; 597 598 union { 599 struct ablkcipher_tfm ablkcipher; 600 struct blkcipher_tfm blkcipher; 601 struct cipher_tfm cipher; 602 struct hash_tfm hash; 603 struct compress_tfm compress; 604 } crt_u; 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_ablkcipher { 614 struct crypto_tfm base; 615 }; 616 617 struct crypto_blkcipher { 618 struct crypto_tfm base; 619 }; 620 621 struct crypto_cipher { 622 struct crypto_tfm base; 623 }; 624 625 struct crypto_comp { 626 struct crypto_tfm base; 627 }; 628 629 struct crypto_hash { 630 struct crypto_tfm base; 631 }; 632 633 enum { 634 CRYPTOA_UNSPEC, 635 CRYPTOA_ALG, 636 CRYPTOA_TYPE, 637 CRYPTOA_U32, 638 __CRYPTOA_MAX, 639 }; 640 641 #define CRYPTOA_MAX (__CRYPTOA_MAX - 1) 642 643 /* Maximum number of (rtattr) parameters for each template. */ 644 #define CRYPTO_MAX_ATTRS 32 645 646 struct crypto_attr_alg { 647 char name[CRYPTO_MAX_ALG_NAME]; 648 }; 649 650 struct crypto_attr_type { 651 u32 type; 652 u32 mask; 653 }; 654 655 struct crypto_attr_u32 { 656 u32 num; 657 }; 658 659 /* 660 * Transform user interface. 661 */ 662 663 struct crypto_tfm *crypto_alloc_base(const char *alg_name, u32 type, u32 mask); 664 void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm); 665 666 static inline void crypto_free_tfm(struct crypto_tfm *tfm) 667 { 668 return crypto_destroy_tfm(tfm, tfm); 669 } 670 671 int alg_test(const char *driver, const char *alg, u32 type, u32 mask); 672 673 /* 674 * Transform helpers which query the underlying algorithm. 675 */ 676 static inline const char *crypto_tfm_alg_name(struct crypto_tfm *tfm) 677 { 678 return tfm->__crt_alg->cra_name; 679 } 680 681 static inline const char *crypto_tfm_alg_driver_name(struct crypto_tfm *tfm) 682 { 683 return tfm->__crt_alg->cra_driver_name; 684 } 685 686 static inline int crypto_tfm_alg_priority(struct crypto_tfm *tfm) 687 { 688 return tfm->__crt_alg->cra_priority; 689 } 690 691 static inline u32 crypto_tfm_alg_type(struct crypto_tfm *tfm) 692 { 693 return tfm->__crt_alg->cra_flags & CRYPTO_ALG_TYPE_MASK; 694 } 695 696 static inline unsigned int crypto_tfm_alg_blocksize(struct crypto_tfm *tfm) 697 { 698 return tfm->__crt_alg->cra_blocksize; 699 } 700 701 static inline unsigned int crypto_tfm_alg_alignmask(struct crypto_tfm *tfm) 702 { 703 return tfm->__crt_alg->cra_alignmask; 704 } 705 706 static inline u32 crypto_tfm_get_flags(struct crypto_tfm *tfm) 707 { 708 return tfm->crt_flags; 709 } 710 711 static inline void crypto_tfm_set_flags(struct crypto_tfm *tfm, u32 flags) 712 { 713 tfm->crt_flags |= flags; 714 } 715 716 static inline void crypto_tfm_clear_flags(struct crypto_tfm *tfm, u32 flags) 717 { 718 tfm->crt_flags &= ~flags; 719 } 720 721 static inline void *crypto_tfm_ctx(struct crypto_tfm *tfm) 722 { 723 return tfm->__crt_ctx; 724 } 725 726 static inline unsigned int crypto_tfm_ctx_alignment(void) 727 { 728 struct crypto_tfm *tfm; 729 return __alignof__(tfm->__crt_ctx); 730 } 731 732 /* 733 * API wrappers. 734 */ 735 static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast( 736 struct crypto_tfm *tfm) 737 { 738 return (struct crypto_ablkcipher *)tfm; 739 } 740 741 static inline u32 crypto_skcipher_type(u32 type) 742 { 743 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV); 744 type |= CRYPTO_ALG_TYPE_BLKCIPHER; 745 return type; 746 } 747 748 static inline u32 crypto_skcipher_mask(u32 mask) 749 { 750 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV); 751 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK; 752 return mask; 753 } 754 755 /** 756 * DOC: Asynchronous Block Cipher API 757 * 758 * Asynchronous block cipher API is used with the ciphers of type 759 * CRYPTO_ALG_TYPE_ABLKCIPHER (listed as type "ablkcipher" in /proc/crypto). 760 * 761 * Asynchronous cipher operations imply that the function invocation for a 762 * cipher request returns immediately before the completion of the operation. 763 * The cipher request is scheduled as a separate kernel thread and therefore 764 * load-balanced on the different CPUs via the process scheduler. To allow 765 * the kernel crypto API to inform the caller about the completion of a cipher 766 * request, the caller must provide a callback function. That function is 767 * invoked with the cipher handle when the request completes. 768 * 769 * To support the asynchronous operation, additional information than just the 770 * cipher handle must be supplied to the kernel crypto API. That additional 771 * information is given by filling in the ablkcipher_request data structure. 772 * 773 * For the asynchronous block cipher API, the state is maintained with the tfm 774 * cipher handle. A single tfm can be used across multiple calls and in 775 * parallel. For asynchronous block cipher calls, context data supplied and 776 * only used by the caller can be referenced the request data structure in 777 * addition to the IV used for the cipher request. The maintenance of such 778 * state information would be important for a crypto driver implementer to 779 * have, because when calling the callback function upon completion of the 780 * cipher operation, that callback function may need some information about 781 * which operation just finished if it invoked multiple in parallel. This 782 * state information is unused by the kernel crypto API. 783 */ 784 785 /** 786 * crypto_alloc_ablkcipher() - allocate asynchronous block cipher handle 787 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 788 * ablkcipher cipher 789 * @type: specifies the type of the cipher 790 * @mask: specifies the mask for the cipher 791 * 792 * Allocate a cipher handle for an ablkcipher. The returned struct 793 * crypto_ablkcipher is the cipher handle that is required for any subsequent 794 * API invocation for that ablkcipher. 795 * 796 * Return: allocated cipher handle in case of success; IS_ERR() is true in case 797 * of an error, PTR_ERR() returns the error code. 798 */ 799 struct crypto_ablkcipher *crypto_alloc_ablkcipher(const char *alg_name, 800 u32 type, u32 mask); 801 802 static inline struct crypto_tfm *crypto_ablkcipher_tfm( 803 struct crypto_ablkcipher *tfm) 804 { 805 return &tfm->base; 806 } 807 808 /** 809 * crypto_free_ablkcipher() - zeroize and free cipher handle 810 * @tfm: cipher handle to be freed 811 */ 812 static inline void crypto_free_ablkcipher(struct crypto_ablkcipher *tfm) 813 { 814 crypto_free_tfm(crypto_ablkcipher_tfm(tfm)); 815 } 816 817 /** 818 * crypto_has_ablkcipher() - Search for the availability of an ablkcipher. 819 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 820 * ablkcipher 821 * @type: specifies the type of the cipher 822 * @mask: specifies the mask for the cipher 823 * 824 * Return: true when the ablkcipher is known to the kernel crypto API; false 825 * otherwise 826 */ 827 static inline int crypto_has_ablkcipher(const char *alg_name, u32 type, 828 u32 mask) 829 { 830 return crypto_has_alg(alg_name, crypto_skcipher_type(type), 831 crypto_skcipher_mask(mask)); 832 } 833 834 static inline struct ablkcipher_tfm *crypto_ablkcipher_crt( 835 struct crypto_ablkcipher *tfm) 836 { 837 return &crypto_ablkcipher_tfm(tfm)->crt_ablkcipher; 838 } 839 840 /** 841 * crypto_ablkcipher_ivsize() - obtain IV size 842 * @tfm: cipher handle 843 * 844 * The size of the IV for the ablkcipher referenced by the cipher handle is 845 * returned. This IV size may be zero if the cipher does not need an IV. 846 * 847 * Return: IV size in bytes 848 */ 849 static inline unsigned int crypto_ablkcipher_ivsize( 850 struct crypto_ablkcipher *tfm) 851 { 852 return crypto_ablkcipher_crt(tfm)->ivsize; 853 } 854 855 /** 856 * crypto_ablkcipher_blocksize() - obtain block size of cipher 857 * @tfm: cipher handle 858 * 859 * The block size for the ablkcipher referenced with the cipher handle is 860 * returned. The caller may use that information to allocate appropriate 861 * memory for the data returned by the encryption or decryption operation 862 * 863 * Return: block size of cipher 864 */ 865 static inline unsigned int crypto_ablkcipher_blocksize( 866 struct crypto_ablkcipher *tfm) 867 { 868 return crypto_tfm_alg_blocksize(crypto_ablkcipher_tfm(tfm)); 869 } 870 871 static inline unsigned int crypto_ablkcipher_alignmask( 872 struct crypto_ablkcipher *tfm) 873 { 874 return crypto_tfm_alg_alignmask(crypto_ablkcipher_tfm(tfm)); 875 } 876 877 static inline u32 crypto_ablkcipher_get_flags(struct crypto_ablkcipher *tfm) 878 { 879 return crypto_tfm_get_flags(crypto_ablkcipher_tfm(tfm)); 880 } 881 882 static inline void crypto_ablkcipher_set_flags(struct crypto_ablkcipher *tfm, 883 u32 flags) 884 { 885 crypto_tfm_set_flags(crypto_ablkcipher_tfm(tfm), flags); 886 } 887 888 static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm, 889 u32 flags) 890 { 891 crypto_tfm_clear_flags(crypto_ablkcipher_tfm(tfm), flags); 892 } 893 894 /** 895 * crypto_ablkcipher_setkey() - set key for cipher 896 * @tfm: cipher handle 897 * @key: buffer holding the key 898 * @keylen: length of the key in bytes 899 * 900 * The caller provided key is set for the ablkcipher referenced by the cipher 901 * handle. 902 * 903 * Note, the key length determines the cipher type. Many block ciphers implement 904 * different cipher modes depending on the key size, such as AES-128 vs AES-192 905 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 906 * is performed. 907 * 908 * Return: 0 if the setting of the key was successful; < 0 if an error occurred 909 */ 910 static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm, 911 const u8 *key, unsigned int keylen) 912 { 913 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm); 914 915 return crt->setkey(crt->base, key, keylen); 916 } 917 918 /** 919 * crypto_ablkcipher_reqtfm() - obtain cipher handle from request 920 * @req: ablkcipher_request out of which the cipher handle is to be obtained 921 * 922 * Return the crypto_ablkcipher handle when furnishing an ablkcipher_request 923 * data structure. 924 * 925 * Return: crypto_ablkcipher handle 926 */ 927 static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm( 928 struct ablkcipher_request *req) 929 { 930 return __crypto_ablkcipher_cast(req->base.tfm); 931 } 932 933 /** 934 * crypto_ablkcipher_encrypt() - encrypt plaintext 935 * @req: reference to the ablkcipher_request handle that holds all information 936 * needed to perform the cipher operation 937 * 938 * Encrypt plaintext data using the ablkcipher_request handle. That data 939 * structure and how it is filled with data is discussed with the 940 * ablkcipher_request_* functions. 941 * 942 * Return: 0 if the cipher operation was successful; < 0 if an error occurred 943 */ 944 static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req) 945 { 946 struct ablkcipher_tfm *crt = 947 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req)); 948 return crt->encrypt(req); 949 } 950 951 /** 952 * crypto_ablkcipher_decrypt() - decrypt ciphertext 953 * @req: reference to the ablkcipher_request handle that holds all information 954 * needed to perform the cipher operation 955 * 956 * Decrypt ciphertext data using the ablkcipher_request handle. That data 957 * structure and how it is filled with data is discussed with the 958 * ablkcipher_request_* functions. 959 * 960 * Return: 0 if the cipher operation was successful; < 0 if an error occurred 961 */ 962 static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req) 963 { 964 struct ablkcipher_tfm *crt = 965 crypto_ablkcipher_crt(crypto_ablkcipher_reqtfm(req)); 966 return crt->decrypt(req); 967 } 968 969 /** 970 * DOC: Asynchronous Cipher Request Handle 971 * 972 * The ablkcipher_request data structure contains all pointers to data 973 * required for the asynchronous cipher operation. This includes the cipher 974 * handle (which can be used by multiple ablkcipher_request instances), pointer 975 * to plaintext and ciphertext, asynchronous callback function, etc. It acts 976 * as a handle to the ablkcipher_request_* API calls in a similar way as 977 * ablkcipher handle to the crypto_ablkcipher_* API calls. 978 */ 979 980 /** 981 * crypto_ablkcipher_reqsize() - obtain size of the request data structure 982 * @tfm: cipher handle 983 * 984 * Return: number of bytes 985 */ 986 static inline unsigned int crypto_ablkcipher_reqsize( 987 struct crypto_ablkcipher *tfm) 988 { 989 return crypto_ablkcipher_crt(tfm)->reqsize; 990 } 991 992 /** 993 * ablkcipher_request_set_tfm() - update cipher handle reference in request 994 * @req: request handle to be modified 995 * @tfm: cipher handle that shall be added to the request handle 996 * 997 * Allow the caller to replace the existing ablkcipher handle in the request 998 * data structure with a different one. 999 */ 1000 static inline void ablkcipher_request_set_tfm( 1001 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm) 1002 { 1003 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base); 1004 } 1005 1006 static inline struct ablkcipher_request *ablkcipher_request_cast( 1007 struct crypto_async_request *req) 1008 { 1009 return container_of(req, struct ablkcipher_request, base); 1010 } 1011 1012 /** 1013 * ablkcipher_request_alloc() - allocate request data structure 1014 * @tfm: cipher handle to be registered with the request 1015 * @gfp: memory allocation flag that is handed to kmalloc by the API call. 1016 * 1017 * Allocate the request data structure that must be used with the ablkcipher 1018 * encrypt and decrypt API calls. During the allocation, the provided ablkcipher 1019 * handle is registered in the request data structure. 1020 * 1021 * Return: allocated request handle in case of success; IS_ERR() is true in case 1022 * of an error, PTR_ERR() returns the error code. 1023 */ 1024 static inline struct ablkcipher_request *ablkcipher_request_alloc( 1025 struct crypto_ablkcipher *tfm, gfp_t gfp) 1026 { 1027 struct ablkcipher_request *req; 1028 1029 req = kmalloc(sizeof(struct ablkcipher_request) + 1030 crypto_ablkcipher_reqsize(tfm), gfp); 1031 1032 if (likely(req)) 1033 ablkcipher_request_set_tfm(req, tfm); 1034 1035 return req; 1036 } 1037 1038 /** 1039 * ablkcipher_request_free() - zeroize and free request data structure 1040 * @req: request data structure cipher handle to be freed 1041 */ 1042 static inline void ablkcipher_request_free(struct ablkcipher_request *req) 1043 { 1044 kzfree(req); 1045 } 1046 1047 /** 1048 * ablkcipher_request_set_callback() - set asynchronous callback function 1049 * @req: request handle 1050 * @flags: specify zero or an ORing of the flags 1051 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and 1052 * increase the wait queue beyond the initial maximum size; 1053 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep 1054 * @compl: callback function pointer to be registered with the request handle 1055 * @data: The data pointer refers to memory that is not used by the kernel 1056 * crypto API, but provided to the callback function for it to use. Here, 1057 * the caller can provide a reference to memory the callback function can 1058 * operate on. As the callback function is invoked asynchronously to the 1059 * related functionality, it may need to access data structures of the 1060 * related functionality which can be referenced using this pointer. The 1061 * callback function can access the memory via the "data" field in the 1062 * crypto_async_request data structure provided to the callback function. 1063 * 1064 * This function allows setting the callback function that is triggered once the 1065 * cipher operation completes. 1066 * 1067 * The callback function is registered with the ablkcipher_request handle and 1068 * must comply with the following template 1069 * 1070 * void callback_function(struct crypto_async_request *req, int error) 1071 */ 1072 static inline void ablkcipher_request_set_callback( 1073 struct ablkcipher_request *req, 1074 u32 flags, crypto_completion_t compl, void *data) 1075 { 1076 req->base.complete = compl; 1077 req->base.data = data; 1078 req->base.flags = flags; 1079 } 1080 1081 /** 1082 * ablkcipher_request_set_crypt() - set data buffers 1083 * @req: request handle 1084 * @src: source scatter / gather list 1085 * @dst: destination scatter / gather list 1086 * @nbytes: number of bytes to process from @src 1087 * @iv: IV for the cipher operation which must comply with the IV size defined 1088 * by crypto_ablkcipher_ivsize 1089 * 1090 * This function allows setting of the source data and destination data 1091 * scatter / gather lists. 1092 * 1093 * For encryption, the source is treated as the plaintext and the 1094 * destination is the ciphertext. For a decryption operation, the use is 1095 * reversed - the source is the ciphertext and the destination is the plaintext. 1096 */ 1097 static inline void ablkcipher_request_set_crypt( 1098 struct ablkcipher_request *req, 1099 struct scatterlist *src, struct scatterlist *dst, 1100 unsigned int nbytes, void *iv) 1101 { 1102 req->src = src; 1103 req->dst = dst; 1104 req->nbytes = nbytes; 1105 req->info = iv; 1106 } 1107 1108 /** 1109 * DOC: Synchronous Block Cipher API 1110 * 1111 * The synchronous block cipher API is used with the ciphers of type 1112 * CRYPTO_ALG_TYPE_BLKCIPHER (listed as type "blkcipher" in /proc/crypto) 1113 * 1114 * Synchronous calls, have a context in the tfm. But since a single tfm can be 1115 * used in multiple calls and in parallel, this info should not be changeable 1116 * (unless a lock is used). This applies, for example, to the symmetric key. 1117 * However, the IV is changeable, so there is an iv field in blkcipher_tfm 1118 * structure for synchronous blkcipher api. So, its the only state info that can 1119 * be kept for synchronous calls without using a big lock across a tfm. 1120 * 1121 * The block cipher API allows the use of a complete cipher, i.e. a cipher 1122 * consisting of a template (a block chaining mode) and a single block cipher 1123 * primitive (e.g. AES). 1124 * 1125 * The plaintext data buffer and the ciphertext data buffer are pointed to 1126 * by using scatter/gather lists. The cipher operation is performed 1127 * on all segments of the provided scatter/gather lists. 1128 * 1129 * The kernel crypto API supports a cipher operation "in-place" which means that 1130 * the caller may provide the same scatter/gather list for the plaintext and 1131 * cipher text. After the completion of the cipher operation, the plaintext 1132 * data is replaced with the ciphertext data in case of an encryption and vice 1133 * versa for a decryption. The caller must ensure that the scatter/gather lists 1134 * for the output data point to sufficiently large buffers, i.e. multiples of 1135 * the block size of the cipher. 1136 */ 1137 1138 static inline struct crypto_blkcipher *__crypto_blkcipher_cast( 1139 struct crypto_tfm *tfm) 1140 { 1141 return (struct crypto_blkcipher *)tfm; 1142 } 1143 1144 static inline struct crypto_blkcipher *crypto_blkcipher_cast( 1145 struct crypto_tfm *tfm) 1146 { 1147 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_BLKCIPHER); 1148 return __crypto_blkcipher_cast(tfm); 1149 } 1150 1151 /** 1152 * crypto_alloc_blkcipher() - allocate synchronous block cipher handle 1153 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 1154 * blkcipher cipher 1155 * @type: specifies the type of the cipher 1156 * @mask: specifies the mask for the cipher 1157 * 1158 * Allocate a cipher handle for a block cipher. The returned struct 1159 * crypto_blkcipher is the cipher handle that is required for any subsequent 1160 * API invocation for that block cipher. 1161 * 1162 * Return: allocated cipher handle in case of success; IS_ERR() is true in case 1163 * of an error, PTR_ERR() returns the error code. 1164 */ 1165 static inline struct crypto_blkcipher *crypto_alloc_blkcipher( 1166 const char *alg_name, u32 type, u32 mask) 1167 { 1168 type &= ~CRYPTO_ALG_TYPE_MASK; 1169 type |= CRYPTO_ALG_TYPE_BLKCIPHER; 1170 mask |= CRYPTO_ALG_TYPE_MASK; 1171 1172 return __crypto_blkcipher_cast(crypto_alloc_base(alg_name, type, mask)); 1173 } 1174 1175 static inline struct crypto_tfm *crypto_blkcipher_tfm( 1176 struct crypto_blkcipher *tfm) 1177 { 1178 return &tfm->base; 1179 } 1180 1181 /** 1182 * crypto_free_blkcipher() - zeroize and free the block cipher handle 1183 * @tfm: cipher handle to be freed 1184 */ 1185 static inline void crypto_free_blkcipher(struct crypto_blkcipher *tfm) 1186 { 1187 crypto_free_tfm(crypto_blkcipher_tfm(tfm)); 1188 } 1189 1190 /** 1191 * crypto_has_blkcipher() - Search for the availability of a block cipher 1192 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 1193 * block cipher 1194 * @type: specifies the type of the cipher 1195 * @mask: specifies the mask for the cipher 1196 * 1197 * Return: true when the block cipher is known to the kernel crypto API; false 1198 * otherwise 1199 */ 1200 static inline int crypto_has_blkcipher(const char *alg_name, u32 type, u32 mask) 1201 { 1202 type &= ~CRYPTO_ALG_TYPE_MASK; 1203 type |= CRYPTO_ALG_TYPE_BLKCIPHER; 1204 mask |= CRYPTO_ALG_TYPE_MASK; 1205 1206 return crypto_has_alg(alg_name, type, mask); 1207 } 1208 1209 /** 1210 * crypto_blkcipher_name() - return the name / cra_name from the cipher handle 1211 * @tfm: cipher handle 1212 * 1213 * Return: The character string holding the name of the cipher 1214 */ 1215 static inline const char *crypto_blkcipher_name(struct crypto_blkcipher *tfm) 1216 { 1217 return crypto_tfm_alg_name(crypto_blkcipher_tfm(tfm)); 1218 } 1219 1220 static inline struct blkcipher_tfm *crypto_blkcipher_crt( 1221 struct crypto_blkcipher *tfm) 1222 { 1223 return &crypto_blkcipher_tfm(tfm)->crt_blkcipher; 1224 } 1225 1226 static inline struct blkcipher_alg *crypto_blkcipher_alg( 1227 struct crypto_blkcipher *tfm) 1228 { 1229 return &crypto_blkcipher_tfm(tfm)->__crt_alg->cra_blkcipher; 1230 } 1231 1232 /** 1233 * crypto_blkcipher_ivsize() - obtain IV size 1234 * @tfm: cipher handle 1235 * 1236 * The size of the IV for the block cipher referenced by the cipher handle is 1237 * returned. This IV size may be zero if the cipher does not need an IV. 1238 * 1239 * Return: IV size in bytes 1240 */ 1241 static inline unsigned int crypto_blkcipher_ivsize(struct crypto_blkcipher *tfm) 1242 { 1243 return crypto_blkcipher_alg(tfm)->ivsize; 1244 } 1245 1246 /** 1247 * crypto_blkcipher_blocksize() - obtain block size of cipher 1248 * @tfm: cipher handle 1249 * 1250 * The block size for the block cipher referenced with the cipher handle is 1251 * returned. The caller may use that information to allocate appropriate 1252 * memory for the data returned by the encryption or decryption operation. 1253 * 1254 * Return: block size of cipher 1255 */ 1256 static inline unsigned int crypto_blkcipher_blocksize( 1257 struct crypto_blkcipher *tfm) 1258 { 1259 return crypto_tfm_alg_blocksize(crypto_blkcipher_tfm(tfm)); 1260 } 1261 1262 static inline unsigned int crypto_blkcipher_alignmask( 1263 struct crypto_blkcipher *tfm) 1264 { 1265 return crypto_tfm_alg_alignmask(crypto_blkcipher_tfm(tfm)); 1266 } 1267 1268 static inline u32 crypto_blkcipher_get_flags(struct crypto_blkcipher *tfm) 1269 { 1270 return crypto_tfm_get_flags(crypto_blkcipher_tfm(tfm)); 1271 } 1272 1273 static inline void crypto_blkcipher_set_flags(struct crypto_blkcipher *tfm, 1274 u32 flags) 1275 { 1276 crypto_tfm_set_flags(crypto_blkcipher_tfm(tfm), flags); 1277 } 1278 1279 static inline void crypto_blkcipher_clear_flags(struct crypto_blkcipher *tfm, 1280 u32 flags) 1281 { 1282 crypto_tfm_clear_flags(crypto_blkcipher_tfm(tfm), flags); 1283 } 1284 1285 /** 1286 * crypto_blkcipher_setkey() - set key for cipher 1287 * @tfm: cipher handle 1288 * @key: buffer holding the key 1289 * @keylen: length of the key in bytes 1290 * 1291 * The caller provided key is set for the block cipher referenced by the cipher 1292 * handle. 1293 * 1294 * Note, the key length determines the cipher type. Many block ciphers implement 1295 * different cipher modes depending on the key size, such as AES-128 vs AES-192 1296 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 1297 * is performed. 1298 * 1299 * Return: 0 if the setting of the key was successful; < 0 if an error occurred 1300 */ 1301 static inline int crypto_blkcipher_setkey(struct crypto_blkcipher *tfm, 1302 const u8 *key, unsigned int keylen) 1303 { 1304 return crypto_blkcipher_crt(tfm)->setkey(crypto_blkcipher_tfm(tfm), 1305 key, keylen); 1306 } 1307 1308 /** 1309 * crypto_blkcipher_encrypt() - encrypt plaintext 1310 * @desc: reference to the block cipher handle with meta data 1311 * @dst: scatter/gather list that is filled by the cipher operation with the 1312 * ciphertext 1313 * @src: scatter/gather list that holds the plaintext 1314 * @nbytes: number of bytes of the plaintext to encrypt. 1315 * 1316 * Encrypt plaintext data using the IV set by the caller with a preceding 1317 * call of crypto_blkcipher_set_iv. 1318 * 1319 * The blkcipher_desc data structure must be filled by the caller and can 1320 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled 1321 * with the block cipher handle; desc.flags is filled with either 1322 * CRYPTO_TFM_REQ_MAY_SLEEP or 0. 1323 * 1324 * Return: 0 if the cipher operation was successful; < 0 if an error occurred 1325 */ 1326 static inline int crypto_blkcipher_encrypt(struct blkcipher_desc *desc, 1327 struct scatterlist *dst, 1328 struct scatterlist *src, 1329 unsigned int nbytes) 1330 { 1331 desc->info = crypto_blkcipher_crt(desc->tfm)->iv; 1332 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes); 1333 } 1334 1335 /** 1336 * crypto_blkcipher_encrypt_iv() - encrypt plaintext with dedicated IV 1337 * @desc: reference to the block cipher handle with meta data 1338 * @dst: scatter/gather list that is filled by the cipher operation with the 1339 * ciphertext 1340 * @src: scatter/gather list that holds the plaintext 1341 * @nbytes: number of bytes of the plaintext to encrypt. 1342 * 1343 * Encrypt plaintext data with the use of an IV that is solely used for this 1344 * cipher operation. Any previously set IV is not used. 1345 * 1346 * The blkcipher_desc data structure must be filled by the caller and can 1347 * reside on the stack. The caller must fill desc as follows: desc.tfm is filled 1348 * with the block cipher handle; desc.info is filled with the IV to be used for 1349 * the current operation; desc.flags is filled with either 1350 * CRYPTO_TFM_REQ_MAY_SLEEP or 0. 1351 * 1352 * Return: 0 if the cipher operation was successful; < 0 if an error occurred 1353 */ 1354 static inline int crypto_blkcipher_encrypt_iv(struct blkcipher_desc *desc, 1355 struct scatterlist *dst, 1356 struct scatterlist *src, 1357 unsigned int nbytes) 1358 { 1359 return crypto_blkcipher_crt(desc->tfm)->encrypt(desc, dst, src, nbytes); 1360 } 1361 1362 /** 1363 * crypto_blkcipher_decrypt() - decrypt ciphertext 1364 * @desc: reference to the block cipher handle with meta data 1365 * @dst: scatter/gather list that is filled by the cipher operation with the 1366 * plaintext 1367 * @src: scatter/gather list that holds the ciphertext 1368 * @nbytes: number of bytes of the ciphertext to decrypt. 1369 * 1370 * Decrypt ciphertext data using the IV set by the caller with a preceding 1371 * call of crypto_blkcipher_set_iv. 1372 * 1373 * The blkcipher_desc data structure must be filled by the caller as documented 1374 * for the crypto_blkcipher_encrypt call above. 1375 * 1376 * Return: 0 if the cipher operation was successful; < 0 if an error occurred 1377 * 1378 */ 1379 static inline int crypto_blkcipher_decrypt(struct blkcipher_desc *desc, 1380 struct scatterlist *dst, 1381 struct scatterlist *src, 1382 unsigned int nbytes) 1383 { 1384 desc->info = crypto_blkcipher_crt(desc->tfm)->iv; 1385 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes); 1386 } 1387 1388 /** 1389 * crypto_blkcipher_decrypt_iv() - decrypt ciphertext with dedicated IV 1390 * @desc: reference to the block cipher handle with meta data 1391 * @dst: scatter/gather list that is filled by the cipher operation with the 1392 * plaintext 1393 * @src: scatter/gather list that holds the ciphertext 1394 * @nbytes: number of bytes of the ciphertext to decrypt. 1395 * 1396 * Decrypt ciphertext data with the use of an IV that is solely used for this 1397 * cipher operation. Any previously set IV is not used. 1398 * 1399 * The blkcipher_desc data structure must be filled by the caller as documented 1400 * for the crypto_blkcipher_encrypt_iv call above. 1401 * 1402 * Return: 0 if the cipher operation was successful; < 0 if an error occurred 1403 */ 1404 static inline int crypto_blkcipher_decrypt_iv(struct blkcipher_desc *desc, 1405 struct scatterlist *dst, 1406 struct scatterlist *src, 1407 unsigned int nbytes) 1408 { 1409 return crypto_blkcipher_crt(desc->tfm)->decrypt(desc, dst, src, nbytes); 1410 } 1411 1412 /** 1413 * crypto_blkcipher_set_iv() - set IV for cipher 1414 * @tfm: cipher handle 1415 * @src: buffer holding the IV 1416 * @len: length of the IV in bytes 1417 * 1418 * The caller provided IV is set for the block cipher referenced by the cipher 1419 * handle. 1420 */ 1421 static inline void crypto_blkcipher_set_iv(struct crypto_blkcipher *tfm, 1422 const u8 *src, unsigned int len) 1423 { 1424 memcpy(crypto_blkcipher_crt(tfm)->iv, src, len); 1425 } 1426 1427 /** 1428 * crypto_blkcipher_get_iv() - obtain IV from cipher 1429 * @tfm: cipher handle 1430 * @dst: buffer filled with the IV 1431 * @len: length of the buffer dst 1432 * 1433 * The caller can obtain the IV set for the block cipher referenced by the 1434 * cipher handle and store it into the user-provided buffer. If the buffer 1435 * has an insufficient space, the IV is truncated to fit the buffer. 1436 */ 1437 static inline void crypto_blkcipher_get_iv(struct crypto_blkcipher *tfm, 1438 u8 *dst, unsigned int len) 1439 { 1440 memcpy(dst, crypto_blkcipher_crt(tfm)->iv, len); 1441 } 1442 1443 /** 1444 * DOC: Single Block Cipher API 1445 * 1446 * The single block cipher API is used with the ciphers of type 1447 * CRYPTO_ALG_TYPE_CIPHER (listed as type "cipher" in /proc/crypto). 1448 * 1449 * Using the single block cipher API calls, operations with the basic cipher 1450 * primitive can be implemented. These cipher primitives exclude any block 1451 * chaining operations including IV handling. 1452 * 1453 * The purpose of this single block cipher API is to support the implementation 1454 * of templates or other concepts that only need to perform the cipher operation 1455 * on one block at a time. Templates invoke the underlying cipher primitive 1456 * block-wise and process either the input or the output data of these cipher 1457 * operations. 1458 */ 1459 1460 static inline struct crypto_cipher *__crypto_cipher_cast(struct crypto_tfm *tfm) 1461 { 1462 return (struct crypto_cipher *)tfm; 1463 } 1464 1465 static inline struct crypto_cipher *crypto_cipher_cast(struct crypto_tfm *tfm) 1466 { 1467 BUG_ON(crypto_tfm_alg_type(tfm) != CRYPTO_ALG_TYPE_CIPHER); 1468 return __crypto_cipher_cast(tfm); 1469 } 1470 1471 /** 1472 * crypto_alloc_cipher() - allocate single block cipher handle 1473 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 1474 * single block cipher 1475 * @type: specifies the type of the cipher 1476 * @mask: specifies the mask for the cipher 1477 * 1478 * Allocate a cipher handle for a single block cipher. The returned struct 1479 * crypto_cipher is the cipher handle that is required for any subsequent API 1480 * invocation for that single block cipher. 1481 * 1482 * Return: allocated cipher handle in case of success; IS_ERR() is true in case 1483 * of an error, PTR_ERR() returns the error code. 1484 */ 1485 static inline struct crypto_cipher *crypto_alloc_cipher(const char *alg_name, 1486 u32 type, u32 mask) 1487 { 1488 type &= ~CRYPTO_ALG_TYPE_MASK; 1489 type |= CRYPTO_ALG_TYPE_CIPHER; 1490 mask |= CRYPTO_ALG_TYPE_MASK; 1491 1492 return __crypto_cipher_cast(crypto_alloc_base(alg_name, type, mask)); 1493 } 1494 1495 static inline struct crypto_tfm *crypto_cipher_tfm(struct crypto_cipher *tfm) 1496 { 1497 return &tfm->base; 1498 } 1499 1500 /** 1501 * crypto_free_cipher() - zeroize and free the single block cipher handle 1502 * @tfm: cipher handle to be freed 1503 */ 1504 static inline void crypto_free_cipher(struct crypto_cipher *tfm) 1505 { 1506 crypto_free_tfm(crypto_cipher_tfm(tfm)); 1507 } 1508 1509 /** 1510 * crypto_has_cipher() - Search for the availability of a single block cipher 1511 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 1512 * single block cipher 1513 * @type: specifies the type of the cipher 1514 * @mask: specifies the mask for the cipher 1515 * 1516 * Return: true when the single block cipher is known to the kernel crypto API; 1517 * false otherwise 1518 */ 1519 static inline int crypto_has_cipher(const char *alg_name, u32 type, u32 mask) 1520 { 1521 type &= ~CRYPTO_ALG_TYPE_MASK; 1522 type |= CRYPTO_ALG_TYPE_CIPHER; 1523 mask |= CRYPTO_ALG_TYPE_MASK; 1524 1525 return crypto_has_alg(alg_name, type, mask); 1526 } 1527 1528 static inline struct cipher_tfm *crypto_cipher_crt(struct crypto_cipher *tfm) 1529 { 1530 return &crypto_cipher_tfm(tfm)->crt_cipher; 1531 } 1532 1533 /** 1534 * crypto_cipher_blocksize() - obtain block size for cipher 1535 * @tfm: cipher handle 1536 * 1537 * The block size for the single block cipher referenced with the cipher handle 1538 * tfm is returned. The caller may use that information to allocate appropriate 1539 * memory for the data returned by the encryption or decryption operation 1540 * 1541 * Return: block size of cipher 1542 */ 1543 static inline unsigned int crypto_cipher_blocksize(struct crypto_cipher *tfm) 1544 { 1545 return crypto_tfm_alg_blocksize(crypto_cipher_tfm(tfm)); 1546 } 1547 1548 static inline unsigned int crypto_cipher_alignmask(struct crypto_cipher *tfm) 1549 { 1550 return crypto_tfm_alg_alignmask(crypto_cipher_tfm(tfm)); 1551 } 1552 1553 static inline u32 crypto_cipher_get_flags(struct crypto_cipher *tfm) 1554 { 1555 return crypto_tfm_get_flags(crypto_cipher_tfm(tfm)); 1556 } 1557 1558 static inline void crypto_cipher_set_flags(struct crypto_cipher *tfm, 1559 u32 flags) 1560 { 1561 crypto_tfm_set_flags(crypto_cipher_tfm(tfm), flags); 1562 } 1563 1564 static inline void crypto_cipher_clear_flags(struct crypto_cipher *tfm, 1565 u32 flags) 1566 { 1567 crypto_tfm_clear_flags(crypto_cipher_tfm(tfm), flags); 1568 } 1569 1570 /** 1571 * crypto_cipher_setkey() - set key for cipher 1572 * @tfm: cipher handle 1573 * @key: buffer holding the key 1574 * @keylen: length of the key in bytes 1575 * 1576 * The caller provided key is set for the single block cipher referenced by the 1577 * cipher handle. 1578 * 1579 * Note, the key length determines the cipher type. Many block ciphers implement 1580 * different cipher modes depending on the key size, such as AES-128 vs AES-192 1581 * vs. AES-256. When providing a 16 byte key for an AES cipher handle, AES-128 1582 * is performed. 1583 * 1584 * Return: 0 if the setting of the key was successful; < 0 if an error occurred 1585 */ 1586 static inline int crypto_cipher_setkey(struct crypto_cipher *tfm, 1587 const u8 *key, unsigned int keylen) 1588 { 1589 return crypto_cipher_crt(tfm)->cit_setkey(crypto_cipher_tfm(tfm), 1590 key, keylen); 1591 } 1592 1593 /** 1594 * crypto_cipher_encrypt_one() - encrypt one block of plaintext 1595 * @tfm: cipher handle 1596 * @dst: points to the buffer that will be filled with the ciphertext 1597 * @src: buffer holding the plaintext to be encrypted 1598 * 1599 * Invoke the encryption operation of one block. The caller must ensure that 1600 * the plaintext and ciphertext buffers are at least one block in size. 1601 */ 1602 static inline void crypto_cipher_encrypt_one(struct crypto_cipher *tfm, 1603 u8 *dst, const u8 *src) 1604 { 1605 crypto_cipher_crt(tfm)->cit_encrypt_one(crypto_cipher_tfm(tfm), 1606 dst, src); 1607 } 1608 1609 /** 1610 * crypto_cipher_decrypt_one() - decrypt one block of ciphertext 1611 * @tfm: cipher handle 1612 * @dst: points to the buffer that will be filled with the plaintext 1613 * @src: buffer holding the ciphertext to be decrypted 1614 * 1615 * Invoke the decryption operation of one block. The caller must ensure that 1616 * the plaintext and ciphertext buffers are at least one block in size. 1617 */ 1618 static inline void crypto_cipher_decrypt_one(struct crypto_cipher *tfm, 1619 u8 *dst, const u8 *src) 1620 { 1621 crypto_cipher_crt(tfm)->cit_decrypt_one(crypto_cipher_tfm(tfm), 1622 dst, src); 1623 } 1624 1625 /** 1626 * DOC: Synchronous Message Digest API 1627 * 1628 * The synchronous message digest API is used with the ciphers of type 1629 * CRYPTO_ALG_TYPE_HASH (listed as type "hash" in /proc/crypto) 1630 */ 1631 1632 static inline struct crypto_hash *__crypto_hash_cast(struct crypto_tfm *tfm) 1633 { 1634 return (struct crypto_hash *)tfm; 1635 } 1636 1637 static inline struct crypto_hash *crypto_hash_cast(struct crypto_tfm *tfm) 1638 { 1639 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_HASH) & 1640 CRYPTO_ALG_TYPE_HASH_MASK); 1641 return __crypto_hash_cast(tfm); 1642 } 1643 1644 /** 1645 * crypto_alloc_hash() - allocate synchronous message digest handle 1646 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 1647 * message digest cipher 1648 * @type: specifies the type of the cipher 1649 * @mask: specifies the mask for the cipher 1650 * 1651 * Allocate a cipher handle for a message digest. The returned struct 1652 * crypto_hash is the cipher handle that is required for any subsequent 1653 * API invocation for that message digest. 1654 * 1655 * Return: allocated cipher handle in case of success; IS_ERR() is true in case 1656 * of an error, PTR_ERR() returns the error code. 1657 */ 1658 static inline struct crypto_hash *crypto_alloc_hash(const char *alg_name, 1659 u32 type, u32 mask) 1660 { 1661 type &= ~CRYPTO_ALG_TYPE_MASK; 1662 mask &= ~CRYPTO_ALG_TYPE_MASK; 1663 type |= CRYPTO_ALG_TYPE_HASH; 1664 mask |= CRYPTO_ALG_TYPE_HASH_MASK; 1665 1666 return __crypto_hash_cast(crypto_alloc_base(alg_name, type, mask)); 1667 } 1668 1669 static inline struct crypto_tfm *crypto_hash_tfm(struct crypto_hash *tfm) 1670 { 1671 return &tfm->base; 1672 } 1673 1674 /** 1675 * crypto_free_hash() - zeroize and free message digest handle 1676 * @tfm: cipher handle to be freed 1677 */ 1678 static inline void crypto_free_hash(struct crypto_hash *tfm) 1679 { 1680 crypto_free_tfm(crypto_hash_tfm(tfm)); 1681 } 1682 1683 /** 1684 * crypto_has_hash() - Search for the availability of a message digest 1685 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 1686 * message digest cipher 1687 * @type: specifies the type of the cipher 1688 * @mask: specifies the mask for the cipher 1689 * 1690 * Return: true when the message digest cipher is known to the kernel crypto 1691 * API; false otherwise 1692 */ 1693 static inline int crypto_has_hash(const char *alg_name, u32 type, u32 mask) 1694 { 1695 type &= ~CRYPTO_ALG_TYPE_MASK; 1696 mask &= ~CRYPTO_ALG_TYPE_MASK; 1697 type |= CRYPTO_ALG_TYPE_HASH; 1698 mask |= CRYPTO_ALG_TYPE_HASH_MASK; 1699 1700 return crypto_has_alg(alg_name, type, mask); 1701 } 1702 1703 static inline struct hash_tfm *crypto_hash_crt(struct crypto_hash *tfm) 1704 { 1705 return &crypto_hash_tfm(tfm)->crt_hash; 1706 } 1707 1708 /** 1709 * crypto_hash_blocksize() - obtain block size for message digest 1710 * @tfm: cipher handle 1711 * 1712 * The block size for the message digest cipher referenced with the cipher 1713 * handle is returned. 1714 * 1715 * Return: block size of cipher 1716 */ 1717 static inline unsigned int crypto_hash_blocksize(struct crypto_hash *tfm) 1718 { 1719 return crypto_tfm_alg_blocksize(crypto_hash_tfm(tfm)); 1720 } 1721 1722 static inline unsigned int crypto_hash_alignmask(struct crypto_hash *tfm) 1723 { 1724 return crypto_tfm_alg_alignmask(crypto_hash_tfm(tfm)); 1725 } 1726 1727 /** 1728 * crypto_hash_digestsize() - obtain message digest size 1729 * @tfm: cipher handle 1730 * 1731 * The size for the message digest created by the message digest cipher 1732 * referenced with the cipher handle is returned. 1733 * 1734 * Return: message digest size 1735 */ 1736 static inline unsigned int crypto_hash_digestsize(struct crypto_hash *tfm) 1737 { 1738 return crypto_hash_crt(tfm)->digestsize; 1739 } 1740 1741 static inline u32 crypto_hash_get_flags(struct crypto_hash *tfm) 1742 { 1743 return crypto_tfm_get_flags(crypto_hash_tfm(tfm)); 1744 } 1745 1746 static inline void crypto_hash_set_flags(struct crypto_hash *tfm, u32 flags) 1747 { 1748 crypto_tfm_set_flags(crypto_hash_tfm(tfm), flags); 1749 } 1750 1751 static inline void crypto_hash_clear_flags(struct crypto_hash *tfm, u32 flags) 1752 { 1753 crypto_tfm_clear_flags(crypto_hash_tfm(tfm), flags); 1754 } 1755 1756 /** 1757 * crypto_hash_init() - (re)initialize message digest handle 1758 * @desc: cipher request handle that to be filled by caller -- 1759 * desc.tfm is filled with the hash cipher handle; 1760 * desc.flags is filled with either CRYPTO_TFM_REQ_MAY_SLEEP or 0. 1761 * 1762 * The call (re-)initializes the message digest referenced by the hash cipher 1763 * request handle. Any potentially existing state created by previous 1764 * operations is discarded. 1765 * 1766 * Return: 0 if the message digest initialization was successful; < 0 if an 1767 * error occurred 1768 */ 1769 static inline int crypto_hash_init(struct hash_desc *desc) 1770 { 1771 return crypto_hash_crt(desc->tfm)->init(desc); 1772 } 1773 1774 /** 1775 * crypto_hash_update() - add data to message digest for processing 1776 * @desc: cipher request handle 1777 * @sg: scatter / gather list pointing to the data to be added to the message 1778 * digest 1779 * @nbytes: number of bytes to be processed from @sg 1780 * 1781 * Updates the message digest state of the cipher handle pointed to by the 1782 * hash cipher request handle with the input data pointed to by the 1783 * scatter/gather list. 1784 * 1785 * Return: 0 if the message digest update was successful; < 0 if an error 1786 * occurred 1787 */ 1788 static inline int crypto_hash_update(struct hash_desc *desc, 1789 struct scatterlist *sg, 1790 unsigned int nbytes) 1791 { 1792 return crypto_hash_crt(desc->tfm)->update(desc, sg, nbytes); 1793 } 1794 1795 /** 1796 * crypto_hash_final() - calculate message digest 1797 * @desc: cipher request handle 1798 * @out: message digest output buffer -- The caller must ensure that the out 1799 * buffer has a sufficient size (e.g. by using the crypto_hash_digestsize 1800 * function). 1801 * 1802 * Finalize the message digest operation and create the message digest 1803 * based on all data added to the cipher handle. The message digest is placed 1804 * into the output buffer. 1805 * 1806 * Return: 0 if the message digest creation was successful; < 0 if an error 1807 * occurred 1808 */ 1809 static inline int crypto_hash_final(struct hash_desc *desc, u8 *out) 1810 { 1811 return crypto_hash_crt(desc->tfm)->final(desc, out); 1812 } 1813 1814 /** 1815 * crypto_hash_digest() - calculate message digest for a buffer 1816 * @desc: see crypto_hash_final() 1817 * @sg: see crypto_hash_update() 1818 * @nbytes: see crypto_hash_update() 1819 * @out: see crypto_hash_final() 1820 * 1821 * This function is a "short-hand" for the function calls of crypto_hash_init, 1822 * crypto_hash_update and crypto_hash_final. The parameters have the same 1823 * meaning as discussed for those separate three functions. 1824 * 1825 * Return: 0 if the message digest creation was successful; < 0 if an error 1826 * occurred 1827 */ 1828 static inline int crypto_hash_digest(struct hash_desc *desc, 1829 struct scatterlist *sg, 1830 unsigned int nbytes, u8 *out) 1831 { 1832 return crypto_hash_crt(desc->tfm)->digest(desc, sg, nbytes, out); 1833 } 1834 1835 /** 1836 * crypto_hash_setkey() - set key for message digest 1837 * @hash: cipher handle 1838 * @key: buffer holding the key 1839 * @keylen: length of the key in bytes 1840 * 1841 * The caller provided key is set for the message digest cipher. The cipher 1842 * handle must point to a keyed hash in order for this function to succeed. 1843 * 1844 * Return: 0 if the setting of the key was successful; < 0 if an error occurred 1845 */ 1846 static inline int crypto_hash_setkey(struct crypto_hash *hash, 1847 const u8 *key, unsigned int keylen) 1848 { 1849 return crypto_hash_crt(hash)->setkey(hash, key, keylen); 1850 } 1851 1852 static inline struct crypto_comp *__crypto_comp_cast(struct crypto_tfm *tfm) 1853 { 1854 return (struct crypto_comp *)tfm; 1855 } 1856 1857 static inline struct crypto_comp *crypto_comp_cast(struct crypto_tfm *tfm) 1858 { 1859 BUG_ON((crypto_tfm_alg_type(tfm) ^ CRYPTO_ALG_TYPE_COMPRESS) & 1860 CRYPTO_ALG_TYPE_MASK); 1861 return __crypto_comp_cast(tfm); 1862 } 1863 1864 static inline struct crypto_comp *crypto_alloc_comp(const char *alg_name, 1865 u32 type, u32 mask) 1866 { 1867 type &= ~CRYPTO_ALG_TYPE_MASK; 1868 type |= CRYPTO_ALG_TYPE_COMPRESS; 1869 mask |= CRYPTO_ALG_TYPE_MASK; 1870 1871 return __crypto_comp_cast(crypto_alloc_base(alg_name, type, mask)); 1872 } 1873 1874 static inline struct crypto_tfm *crypto_comp_tfm(struct crypto_comp *tfm) 1875 { 1876 return &tfm->base; 1877 } 1878 1879 static inline void crypto_free_comp(struct crypto_comp *tfm) 1880 { 1881 crypto_free_tfm(crypto_comp_tfm(tfm)); 1882 } 1883 1884 static inline int crypto_has_comp(const char *alg_name, u32 type, u32 mask) 1885 { 1886 type &= ~CRYPTO_ALG_TYPE_MASK; 1887 type |= CRYPTO_ALG_TYPE_COMPRESS; 1888 mask |= CRYPTO_ALG_TYPE_MASK; 1889 1890 return crypto_has_alg(alg_name, type, mask); 1891 } 1892 1893 static inline const char *crypto_comp_name(struct crypto_comp *tfm) 1894 { 1895 return crypto_tfm_alg_name(crypto_comp_tfm(tfm)); 1896 } 1897 1898 static inline struct compress_tfm *crypto_comp_crt(struct crypto_comp *tfm) 1899 { 1900 return &crypto_comp_tfm(tfm)->crt_compress; 1901 } 1902 1903 static inline int crypto_comp_compress(struct crypto_comp *tfm, 1904 const u8 *src, unsigned int slen, 1905 u8 *dst, unsigned int *dlen) 1906 { 1907 return crypto_comp_crt(tfm)->cot_compress(crypto_comp_tfm(tfm), 1908 src, slen, dst, dlen); 1909 } 1910 1911 static inline int crypto_comp_decompress(struct crypto_comp *tfm, 1912 const u8 *src, unsigned int slen, 1913 u8 *dst, unsigned int *dlen) 1914 { 1915 return crypto_comp_crt(tfm)->cot_decompress(crypto_comp_tfm(tfm), 1916 src, slen, dst, dlen); 1917 } 1918 1919 #endif /* _LINUX_CRYPTO_H */ 1920 1921