1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Hash: Hash algorithms under the crypto API 4 * 5 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au> 6 */ 7 8 #ifndef _CRYPTO_HASH_H 9 #define _CRYPTO_HASH_H 10 11 #include <linux/atomic.h> 12 #include <linux/crypto.h> 13 #include <linux/string.h> 14 15 /* Set this bit for virtual address instead of SG list. */ 16 #define CRYPTO_AHASH_REQ_VIRT 0x00000001 17 18 struct crypto_ahash; 19 20 /** 21 * DOC: Message Digest Algorithm Definitions 22 * 23 * These data structures define modular message digest algorithm 24 * implementations, managed via crypto_register_ahash(), 25 * crypto_register_shash(), crypto_unregister_ahash() and 26 * crypto_unregister_shash(). 27 */ 28 29 /* 30 * struct hash_alg_common - define properties of message digest 31 * @digestsize: Size of the result of the transformation. A buffer of this size 32 * must be available to the @final and @finup calls, so they can 33 * store the resulting hash into it. For various predefined sizes, 34 * search include/crypto/ using 35 * git grep _DIGEST_SIZE include/crypto. 36 * @statesize: Size of the block for partial state of the transformation. A 37 * buffer of this size must be passed to the @export function as it 38 * will save the partial state of the transformation into it. On the 39 * other side, the @import function will load the state from a 40 * buffer of this size as well. 41 * @base: Start of data structure of cipher algorithm. The common data 42 * structure of crypto_alg contains information common to all ciphers. 43 * The hash_alg_common data structure now adds the hash-specific 44 * information. 45 */ 46 #define HASH_ALG_COMMON { \ 47 unsigned int digestsize; \ 48 unsigned int statesize; \ 49 \ 50 struct crypto_alg base; \ 51 } 52 struct hash_alg_common HASH_ALG_COMMON; 53 54 struct ahash_request { 55 struct crypto_async_request base; 56 57 unsigned int nbytes; 58 union { 59 struct scatterlist *src; 60 const u8 *svirt; 61 }; 62 u8 *result; 63 64 void *__ctx[] CRYPTO_MINALIGN_ATTR; 65 }; 66 67 /** 68 * struct ahash_alg - asynchronous message digest definition 69 * @init: **[mandatory]** Initialize the transformation context. Intended only to initialize the 70 * state of the HASH transformation at the beginning. This shall fill in 71 * the internal structures used during the entire duration of the whole 72 * transformation. No data processing happens at this point. Driver code 73 * implementation must not use req->result. 74 * @update: **[mandatory]** Push a chunk of data into the driver for transformation. This 75 * function actually pushes blocks of data from upper layers into the 76 * driver, which then passes those to the hardware as seen fit. This 77 * function must not finalize the HASH transformation by calculating the 78 * final message digest as this only adds more data into the 79 * transformation. This function shall not modify the transformation 80 * context, as this function may be called in parallel with the same 81 * transformation object. Data processing can happen synchronously 82 * [SHASH] or asynchronously [AHASH] at this point. Driver must not use 83 * req->result. 84 * @final: **[mandatory]** Retrieve result from the driver. This function finalizes the 85 * transformation and retrieves the resulting hash from the driver and 86 * pushes it back to upper layers. No data processing happens at this 87 * point unless hardware requires it to finish the transformation 88 * (then the data buffered by the device driver is processed). 89 * @finup: **[optional]** Combination of @update and @final. This function is effectively a 90 * combination of @update and @final calls issued in sequence. As some 91 * hardware cannot do @update and @final separately, this callback was 92 * added to allow such hardware to be used at least by IPsec. Data 93 * processing can happen synchronously [SHASH] or asynchronously [AHASH] 94 * at this point. 95 * @digest: Combination of @init and @update and @final. This function 96 * effectively behaves as the entire chain of operations, @init, 97 * @update and @final issued in sequence. Just like @finup, this was 98 * added for hardware which cannot do even the @finup, but can only do 99 * the whole transformation in one run. Data processing can happen 100 * synchronously [SHASH] or asynchronously [AHASH] at this point. 101 * @setkey: Set optional key used by the hashing algorithm. Intended to push 102 * optional key used by the hashing algorithm from upper layers into 103 * the driver. This function can store the key in the transformation 104 * context or can outright program it into the hardware. In the former 105 * case, one must be careful to program the key into the hardware at 106 * appropriate time and one must be careful that .setkey() can be 107 * called multiple times during the existence of the transformation 108 * object. Not all hashing algorithms do implement this function as it 109 * is only needed for keyed message digests. SHAx/MDx/CRCx do NOT 110 * implement this function. HMAC(MDx)/HMAC(SHAx)/CMAC(AES) do implement 111 * this function. This function must be called before any other of the 112 * @init, @update, @final, @finup, @digest is called. No data 113 * processing happens at this point. 114 * @export: Export partial state of the transformation. This function dumps the 115 * entire state of the ongoing transformation into a provided block of 116 * data so it can be @import 'ed back later on. This is useful in case 117 * you want to save partial result of the transformation after 118 * processing certain amount of data and reload this partial result 119 * multiple times later on for multiple re-use. No data processing 120 * happens at this point. Driver must not use req->result. 121 * @import: Import partial state of the transformation. This function loads the 122 * entire state of the ongoing transformation from a provided block of 123 * data so the transformation can continue from this point onward. No 124 * data processing happens at this point. Driver must not use 125 * req->result. 126 * @init_tfm: Initialize the cryptographic transformation object. 127 * This function is called only once at the instantiation 128 * time, right after the transformation context was 129 * allocated. In case the cryptographic hardware has 130 * some special requirements which need to be handled 131 * by software, this function shall check for the precise 132 * requirement of the transformation and put any software 133 * fallbacks in place. 134 * @exit_tfm: Deinitialize the cryptographic transformation object. 135 * This is a counterpart to @init_tfm, used to remove 136 * various changes set in @init_tfm. 137 * @clone_tfm: Copy transform into new object, may allocate memory. 138 * @reqsize: Size of the request context. 139 * @halg: see struct hash_alg_common 140 */ 141 struct ahash_alg { 142 int (*init)(struct ahash_request *req); 143 int (*update)(struct ahash_request *req); 144 int (*final)(struct ahash_request *req); 145 int (*finup)(struct ahash_request *req); 146 int (*digest)(struct ahash_request *req); 147 int (*export)(struct ahash_request *req, void *out); 148 int (*import)(struct ahash_request *req, const void *in); 149 int (*setkey)(struct crypto_ahash *tfm, const u8 *key, 150 unsigned int keylen); 151 int (*init_tfm)(struct crypto_ahash *tfm); 152 void (*exit_tfm)(struct crypto_ahash *tfm); 153 int (*clone_tfm)(struct crypto_ahash *dst, struct crypto_ahash *src); 154 155 unsigned int reqsize; 156 157 struct hash_alg_common halg; 158 }; 159 160 struct shash_desc { 161 struct crypto_shash *tfm; 162 void *__ctx[] __aligned(ARCH_SLAB_MINALIGN); 163 }; 164 165 #define HASH_MAX_DIGESTSIZE 64 166 167 /* 168 * Worst case is hmac(sha3-224-generic). Its context is a nested 'shash_desc' 169 * containing a 'struct sha3_state'. 170 */ 171 #define HASH_MAX_DESCSIZE (sizeof(struct shash_desc) + 360) 172 173 #define SHASH_DESC_ON_STACK(shash, ctx) \ 174 char __##shash##_desc[sizeof(struct shash_desc) + HASH_MAX_DESCSIZE] \ 175 __aligned(__alignof__(struct shash_desc)); \ 176 struct shash_desc *shash = (struct shash_desc *)__##shash##_desc 177 178 /** 179 * struct shash_alg - synchronous message digest definition 180 * @init: see struct ahash_alg 181 * @update: see struct ahash_alg 182 * @final: see struct ahash_alg 183 * @finup: see struct ahash_alg 184 * @digest: see struct ahash_alg 185 * @export: see struct ahash_alg 186 * @import: see struct ahash_alg 187 * @setkey: see struct ahash_alg 188 * @init_tfm: Initialize the cryptographic transformation object. 189 * This function is called only once at the instantiation 190 * time, right after the transformation context was 191 * allocated. In case the cryptographic hardware has 192 * some special requirements which need to be handled 193 * by software, this function shall check for the precise 194 * requirement of the transformation and put any software 195 * fallbacks in place. 196 * @exit_tfm: Deinitialize the cryptographic transformation object. 197 * This is a counterpart to @init_tfm, used to remove 198 * various changes set in @init_tfm. 199 * @clone_tfm: Copy transform into new object, may allocate memory. 200 * @descsize: Size of the operational state for the message digest. This state 201 * size is the memory size that needs to be allocated for 202 * shash_desc.__ctx 203 * @halg: see struct hash_alg_common 204 * @HASH_ALG_COMMON: see struct hash_alg_common 205 */ 206 struct shash_alg { 207 int (*init)(struct shash_desc *desc); 208 int (*update)(struct shash_desc *desc, const u8 *data, 209 unsigned int len); 210 int (*final)(struct shash_desc *desc, u8 *out); 211 int (*finup)(struct shash_desc *desc, const u8 *data, 212 unsigned int len, u8 *out); 213 int (*digest)(struct shash_desc *desc, const u8 *data, 214 unsigned int len, u8 *out); 215 int (*export)(struct shash_desc *desc, void *out); 216 int (*import)(struct shash_desc *desc, const void *in); 217 int (*setkey)(struct crypto_shash *tfm, const u8 *key, 218 unsigned int keylen); 219 int (*init_tfm)(struct crypto_shash *tfm); 220 void (*exit_tfm)(struct crypto_shash *tfm); 221 int (*clone_tfm)(struct crypto_shash *dst, struct crypto_shash *src); 222 223 unsigned int descsize; 224 225 union { 226 struct HASH_ALG_COMMON; 227 struct hash_alg_common halg; 228 }; 229 }; 230 #undef HASH_ALG_COMMON 231 232 struct crypto_ahash { 233 bool using_shash; /* Underlying algorithm is shash, not ahash */ 234 unsigned int statesize; 235 unsigned int reqsize; 236 struct crypto_tfm base; 237 }; 238 239 struct crypto_shash { 240 unsigned int descsize; 241 struct crypto_tfm base; 242 }; 243 244 /** 245 * DOC: Asynchronous Message Digest API 246 * 247 * The asynchronous message digest API is used with the ciphers of type 248 * CRYPTO_ALG_TYPE_AHASH (listed as type "ahash" in /proc/crypto) 249 * 250 * The asynchronous cipher operation discussion provided for the 251 * CRYPTO_ALG_TYPE_SKCIPHER API applies here as well. 252 */ 253 254 static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm) 255 { 256 return container_of(tfm, struct crypto_ahash, base); 257 } 258 259 /** 260 * crypto_alloc_ahash() - allocate ahash cipher handle 261 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 262 * ahash cipher 263 * @type: specifies the type of the cipher 264 * @mask: specifies the mask for the cipher 265 * 266 * Allocate a cipher handle for an ahash. The returned struct 267 * crypto_ahash is the cipher handle that is required for any subsequent 268 * API invocation for that ahash. 269 * 270 * Return: allocated cipher handle in case of success; IS_ERR() is true in case 271 * of an error, PTR_ERR() returns the error code. 272 */ 273 struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type, 274 u32 mask); 275 276 struct crypto_ahash *crypto_clone_ahash(struct crypto_ahash *tfm); 277 278 static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm) 279 { 280 return &tfm->base; 281 } 282 283 /** 284 * crypto_free_ahash() - zeroize and free the ahash handle 285 * @tfm: cipher handle to be freed 286 * 287 * If @tfm is a NULL or error pointer, this function does nothing. 288 */ 289 static inline void crypto_free_ahash(struct crypto_ahash *tfm) 290 { 291 crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm)); 292 } 293 294 /** 295 * crypto_has_ahash() - Search for the availability of an ahash. 296 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 297 * ahash 298 * @type: specifies the type of the ahash 299 * @mask: specifies the mask for the ahash 300 * 301 * Return: true when the ahash is known to the kernel crypto API; false 302 * otherwise 303 */ 304 int crypto_has_ahash(const char *alg_name, u32 type, u32 mask); 305 306 static inline const char *crypto_ahash_alg_name(struct crypto_ahash *tfm) 307 { 308 return crypto_tfm_alg_name(crypto_ahash_tfm(tfm)); 309 } 310 311 static inline const char *crypto_ahash_driver_name(struct crypto_ahash *tfm) 312 { 313 return crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm)); 314 } 315 316 /** 317 * crypto_ahash_blocksize() - obtain block size for cipher 318 * @tfm: cipher handle 319 * 320 * The block size for the message digest cipher referenced with the cipher 321 * handle is returned. 322 * 323 * Return: block size of cipher 324 */ 325 static inline unsigned int crypto_ahash_blocksize(struct crypto_ahash *tfm) 326 { 327 return crypto_tfm_alg_blocksize(crypto_ahash_tfm(tfm)); 328 } 329 330 static inline struct hash_alg_common *__crypto_hash_alg_common( 331 struct crypto_alg *alg) 332 { 333 return container_of(alg, struct hash_alg_common, base); 334 } 335 336 static inline struct hash_alg_common *crypto_hash_alg_common( 337 struct crypto_ahash *tfm) 338 { 339 return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg); 340 } 341 342 /** 343 * crypto_ahash_digestsize() - obtain message digest size 344 * @tfm: cipher handle 345 * 346 * The size for the message digest created by the message digest cipher 347 * referenced with the cipher handle is returned. 348 * 349 * 350 * Return: message digest size of cipher 351 */ 352 static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm) 353 { 354 return crypto_hash_alg_common(tfm)->digestsize; 355 } 356 357 /** 358 * crypto_ahash_statesize() - obtain size of the ahash state 359 * @tfm: cipher handle 360 * 361 * Return the size of the ahash state. With the crypto_ahash_export() 362 * function, the caller can export the state into a buffer whose size is 363 * defined with this function. 364 * 365 * Return: size of the ahash state 366 */ 367 static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm) 368 { 369 return tfm->statesize; 370 } 371 372 static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm) 373 { 374 return crypto_tfm_get_flags(crypto_ahash_tfm(tfm)); 375 } 376 377 static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags) 378 { 379 crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags); 380 } 381 382 static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags) 383 { 384 crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags); 385 } 386 387 /** 388 * crypto_ahash_reqtfm() - obtain cipher handle from request 389 * @req: asynchronous request handle that contains the reference to the ahash 390 * cipher handle 391 * 392 * Return the ahash cipher handle that is registered with the asynchronous 393 * request handle ahash_request. 394 * 395 * Return: ahash cipher handle 396 */ 397 static inline struct crypto_ahash *crypto_ahash_reqtfm( 398 struct ahash_request *req) 399 { 400 return __crypto_ahash_cast(req->base.tfm); 401 } 402 403 /** 404 * crypto_ahash_reqsize() - obtain size of the request data structure 405 * @tfm: cipher handle 406 * 407 * Return: size of the request data 408 */ 409 static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm) 410 { 411 return tfm->reqsize; 412 } 413 414 static inline void *ahash_request_ctx(struct ahash_request *req) 415 { 416 return req->__ctx; 417 } 418 419 /** 420 * crypto_ahash_setkey - set key for cipher handle 421 * @tfm: cipher handle 422 * @key: buffer holding the key 423 * @keylen: length of the key in bytes 424 * 425 * The caller provided key is set for the ahash cipher. The cipher 426 * handle must point to a keyed hash in order for this function to succeed. 427 * 428 * Return: 0 if the setting of the key was successful; < 0 if an error occurred 429 */ 430 int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key, 431 unsigned int keylen); 432 433 /** 434 * crypto_ahash_finup() - update and finalize message digest 435 * @req: reference to the ahash_request handle that holds all information 436 * needed to perform the cipher operation 437 * 438 * This function is a "short-hand" for the function calls of 439 * crypto_ahash_update and crypto_ahash_final. The parameters have the same 440 * meaning as discussed for those separate functions. 441 * 442 * Return: see crypto_ahash_final() 443 */ 444 int crypto_ahash_finup(struct ahash_request *req); 445 446 /** 447 * crypto_ahash_final() - calculate message digest 448 * @req: reference to the ahash_request handle that holds all information 449 * needed to perform the cipher operation 450 * 451 * Finalize the message digest operation and create the message digest 452 * based on all data added to the cipher handle. The message digest is placed 453 * into the output buffer registered with the ahash_request handle. 454 * 455 * Return: 456 * 0 if the message digest was successfully calculated; 457 * -EINPROGRESS if data is fed into hardware (DMA) or queued for later; 458 * -EBUSY if queue is full and request should be resubmitted later; 459 * other < 0 if an error occurred 460 */ 461 int crypto_ahash_final(struct ahash_request *req); 462 463 /** 464 * crypto_ahash_digest() - calculate message digest for a buffer 465 * @req: reference to the ahash_request handle that holds all information 466 * needed to perform the cipher operation 467 * 468 * This function is a "short-hand" for the function calls of crypto_ahash_init, 469 * crypto_ahash_update and crypto_ahash_final. The parameters have the same 470 * meaning as discussed for those separate three functions. 471 * 472 * Return: see crypto_ahash_final() 473 */ 474 int crypto_ahash_digest(struct ahash_request *req); 475 476 /** 477 * crypto_ahash_export() - extract current message digest state 478 * @req: reference to the ahash_request handle whose state is exported 479 * @out: output buffer of sufficient size that can hold the hash state 480 * 481 * This function exports the hash state of the ahash_request handle into the 482 * caller-allocated output buffer out which must have sufficient size (e.g. by 483 * calling crypto_ahash_statesize()). 484 * 485 * Return: 0 if the export was successful; < 0 if an error occurred 486 */ 487 int crypto_ahash_export(struct ahash_request *req, void *out); 488 489 /** 490 * crypto_ahash_import() - import message digest state 491 * @req: reference to ahash_request handle the state is imported into 492 * @in: buffer holding the state 493 * 494 * This function imports the hash state into the ahash_request handle from the 495 * input buffer. That buffer should have been generated with the 496 * crypto_ahash_export function. 497 * 498 * Return: 0 if the import was successful; < 0 if an error occurred 499 */ 500 int crypto_ahash_import(struct ahash_request *req, const void *in); 501 502 /** 503 * crypto_ahash_init() - (re)initialize message digest handle 504 * @req: ahash_request handle that already is initialized with all necessary 505 * data using the ahash_request_* API functions 506 * 507 * The call (re-)initializes the message digest referenced by the ahash_request 508 * handle. Any potentially existing state created by previous operations is 509 * discarded. 510 * 511 * Return: see crypto_ahash_final() 512 */ 513 int crypto_ahash_init(struct ahash_request *req); 514 515 /** 516 * crypto_ahash_update() - add data to message digest for processing 517 * @req: ahash_request handle that was previously initialized with the 518 * crypto_ahash_init call. 519 * 520 * Updates the message digest state of the &ahash_request handle. The input data 521 * is pointed to by the scatter/gather list registered in the &ahash_request 522 * handle 523 * 524 * Return: see crypto_ahash_final() 525 */ 526 int crypto_ahash_update(struct ahash_request *req); 527 528 /** 529 * DOC: Asynchronous Hash Request Handle 530 * 531 * The &ahash_request data structure contains all pointers to data 532 * required for the asynchronous cipher operation. This includes the cipher 533 * handle (which can be used by multiple &ahash_request instances), pointer 534 * to plaintext and the message digest output buffer, asynchronous callback 535 * function, etc. It acts as a handle to the ahash_request_* API calls in a 536 * similar way as ahash handle to the crypto_ahash_* API calls. 537 */ 538 539 /** 540 * ahash_request_set_tfm() - update cipher handle reference in request 541 * @req: request handle to be modified 542 * @tfm: cipher handle that shall be added to the request handle 543 * 544 * Allow the caller to replace the existing ahash handle in the request 545 * data structure with a different one. 546 */ 547 static inline void ahash_request_set_tfm(struct ahash_request *req, 548 struct crypto_ahash *tfm) 549 { 550 req->base.tfm = crypto_ahash_tfm(tfm); 551 } 552 553 /** 554 * ahash_request_alloc() - allocate request data structure 555 * @tfm: cipher handle to be registered with the request 556 * @gfp: memory allocation flag that is handed to kmalloc by the API call. 557 * 558 * Allocate the request data structure that must be used with the ahash 559 * message digest API calls. During 560 * the allocation, the provided ahash handle 561 * is registered in the request data structure. 562 * 563 * Return: allocated request handle in case of success, or NULL if out of memory 564 */ 565 static inline struct ahash_request *ahash_request_alloc_noprof( 566 struct crypto_ahash *tfm, gfp_t gfp) 567 { 568 struct ahash_request *req; 569 570 req = kmalloc_noprof(sizeof(struct ahash_request) + 571 crypto_ahash_reqsize(tfm), gfp); 572 573 if (likely(req)) 574 ahash_request_set_tfm(req, tfm); 575 576 return req; 577 } 578 #define ahash_request_alloc(...) alloc_hooks(ahash_request_alloc_noprof(__VA_ARGS__)) 579 580 /** 581 * ahash_request_free() - zeroize and free the request data structure 582 * @req: request data structure cipher handle to be freed 583 */ 584 void ahash_request_free(struct ahash_request *req); 585 586 static inline struct ahash_request *ahash_request_cast( 587 struct crypto_async_request *req) 588 { 589 return container_of(req, struct ahash_request, base); 590 } 591 592 /** 593 * ahash_request_set_callback() - set asynchronous callback function 594 * @req: request handle 595 * @flags: specify zero or an ORing of the flags 596 * CRYPTO_TFM_REQ_MAY_BACKLOG the request queue may back log and 597 * increase the wait queue beyond the initial maximum size; 598 * CRYPTO_TFM_REQ_MAY_SLEEP the request processing may sleep 599 * @compl: callback function pointer to be registered with the request handle 600 * @data: The data pointer refers to memory that is not used by the kernel 601 * crypto API, but provided to the callback function for it to use. Here, 602 * the caller can provide a reference to memory the callback function can 603 * operate on. As the callback function is invoked asynchronously to the 604 * related functionality, it may need to access data structures of the 605 * related functionality which can be referenced using this pointer. The 606 * callback function can access the memory via the "data" field in the 607 * &crypto_async_request data structure provided to the callback function. 608 * 609 * This function allows setting the callback function that is triggered once 610 * the cipher operation completes. 611 * 612 * The callback function is registered with the &ahash_request handle and 613 * must comply with the following template:: 614 * 615 * void callback_function(struct crypto_async_request *req, int error) 616 */ 617 static inline void ahash_request_set_callback(struct ahash_request *req, 618 u32 flags, 619 crypto_completion_t compl, 620 void *data) 621 { 622 u32 keep = CRYPTO_AHASH_REQ_VIRT; 623 624 req->base.complete = compl; 625 req->base.data = data; 626 flags &= ~keep; 627 req->base.flags &= keep; 628 req->base.flags |= flags; 629 crypto_reqchain_init(&req->base); 630 } 631 632 /** 633 * ahash_request_set_crypt() - set data buffers 634 * @req: ahash_request handle to be updated 635 * @src: source scatter/gather list 636 * @result: buffer that is filled with the message digest -- the caller must 637 * ensure that the buffer has sufficient space by, for example, calling 638 * crypto_ahash_digestsize() 639 * @nbytes: number of bytes to process from the source scatter/gather list 640 * 641 * By using this call, the caller references the source scatter/gather list. 642 * The source scatter/gather list points to the data the message digest is to 643 * be calculated for. 644 */ 645 static inline void ahash_request_set_crypt(struct ahash_request *req, 646 struct scatterlist *src, u8 *result, 647 unsigned int nbytes) 648 { 649 req->src = src; 650 req->nbytes = nbytes; 651 req->result = result; 652 req->base.flags &= ~CRYPTO_AHASH_REQ_VIRT; 653 } 654 655 /** 656 * ahash_request_set_virt() - set virtual address data buffers 657 * @req: ahash_request handle to be updated 658 * @src: source virtual address 659 * @result: buffer that is filled with the message digest -- the caller must 660 * ensure that the buffer has sufficient space by, for example, calling 661 * crypto_ahash_digestsize() 662 * @nbytes: number of bytes to process from the source virtual address 663 * 664 * By using this call, the caller references the source virtual address. 665 * The source virtual address points to the data the message digest is to 666 * be calculated for. 667 */ 668 static inline void ahash_request_set_virt(struct ahash_request *req, 669 const u8 *src, u8 *result, 670 unsigned int nbytes) 671 { 672 req->svirt = src; 673 req->nbytes = nbytes; 674 req->result = result; 675 req->base.flags |= CRYPTO_AHASH_REQ_VIRT; 676 } 677 678 static inline void ahash_request_chain(struct ahash_request *req, 679 struct ahash_request *head) 680 { 681 crypto_request_chain(&req->base, &head->base); 682 } 683 684 /** 685 * DOC: Synchronous Message Digest API 686 * 687 * The synchronous message digest API is used with the ciphers of type 688 * CRYPTO_ALG_TYPE_SHASH (listed as type "shash" in /proc/crypto) 689 * 690 * The message digest API is able to maintain state information for the 691 * caller. 692 * 693 * The synchronous message digest API can store user-related context in its 694 * shash_desc request data structure. 695 */ 696 697 /** 698 * crypto_alloc_shash() - allocate message digest handle 699 * @alg_name: is the cra_name / name or cra_driver_name / driver name of the 700 * message digest cipher 701 * @type: specifies the type of the cipher 702 * @mask: specifies the mask for the cipher 703 * 704 * Allocate a cipher handle for a message digest. The returned &struct 705 * crypto_shash is the cipher handle that is required for any subsequent 706 * API invocation for that message digest. 707 * 708 * Return: allocated cipher handle in case of success; IS_ERR() is true in case 709 * of an error, PTR_ERR() returns the error code. 710 */ 711 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type, 712 u32 mask); 713 714 struct crypto_shash *crypto_clone_shash(struct crypto_shash *tfm); 715 716 int crypto_has_shash(const char *alg_name, u32 type, u32 mask); 717 718 static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm) 719 { 720 return &tfm->base; 721 } 722 723 /** 724 * crypto_free_shash() - zeroize and free the message digest handle 725 * @tfm: cipher handle to be freed 726 * 727 * If @tfm is a NULL or error pointer, this function does nothing. 728 */ 729 static inline void crypto_free_shash(struct crypto_shash *tfm) 730 { 731 crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm)); 732 } 733 734 static inline const char *crypto_shash_alg_name(struct crypto_shash *tfm) 735 { 736 return crypto_tfm_alg_name(crypto_shash_tfm(tfm)); 737 } 738 739 static inline const char *crypto_shash_driver_name(struct crypto_shash *tfm) 740 { 741 return crypto_tfm_alg_driver_name(crypto_shash_tfm(tfm)); 742 } 743 744 /** 745 * crypto_shash_blocksize() - obtain block size for cipher 746 * @tfm: cipher handle 747 * 748 * The block size for the message digest cipher referenced with the cipher 749 * handle is returned. 750 * 751 * Return: block size of cipher 752 */ 753 static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm) 754 { 755 return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm)); 756 } 757 758 static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg) 759 { 760 return container_of(alg, struct shash_alg, base); 761 } 762 763 static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm) 764 { 765 return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg); 766 } 767 768 /** 769 * crypto_shash_digestsize() - obtain message digest size 770 * @tfm: cipher handle 771 * 772 * The size for the message digest created by the message digest cipher 773 * referenced with the cipher handle is returned. 774 * 775 * Return: digest size of cipher 776 */ 777 static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm) 778 { 779 return crypto_shash_alg(tfm)->digestsize; 780 } 781 782 static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm) 783 { 784 return crypto_shash_alg(tfm)->statesize; 785 } 786 787 static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm) 788 { 789 return crypto_tfm_get_flags(crypto_shash_tfm(tfm)); 790 } 791 792 static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags) 793 { 794 crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags); 795 } 796 797 static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags) 798 { 799 crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags); 800 } 801 802 /** 803 * crypto_shash_descsize() - obtain the operational state size 804 * @tfm: cipher handle 805 * 806 * The size of the operational state the cipher needs during operation is 807 * returned for the hash referenced with the cipher handle. This size is 808 * required to calculate the memory requirements to allow the caller allocating 809 * sufficient memory for operational state. 810 * 811 * The operational state is defined with struct shash_desc where the size of 812 * that data structure is to be calculated as 813 * sizeof(struct shash_desc) + crypto_shash_descsize(alg) 814 * 815 * Return: size of the operational state 816 */ 817 static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm) 818 { 819 return tfm->descsize; 820 } 821 822 static inline void *shash_desc_ctx(struct shash_desc *desc) 823 { 824 return desc->__ctx; 825 } 826 827 /** 828 * crypto_shash_setkey() - set key for message digest 829 * @tfm: cipher handle 830 * @key: buffer holding the key 831 * @keylen: length of the key in bytes 832 * 833 * The caller provided key is set for the keyed message digest cipher. The 834 * cipher handle must point to a keyed message digest cipher in order for this 835 * function to succeed. 836 * 837 * Context: Any context. 838 * Return: 0 if the setting of the key was successful; < 0 if an error occurred 839 */ 840 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key, 841 unsigned int keylen); 842 843 /** 844 * crypto_shash_digest() - calculate message digest for buffer 845 * @desc: see crypto_shash_final() 846 * @data: see crypto_shash_update() 847 * @len: see crypto_shash_update() 848 * @out: see crypto_shash_final() 849 * 850 * This function is a "short-hand" for the function calls of crypto_shash_init, 851 * crypto_shash_update and crypto_shash_final. The parameters have the same 852 * meaning as discussed for those separate three functions. 853 * 854 * Context: Any context. 855 * Return: 0 if the message digest creation was successful; < 0 if an error 856 * occurred 857 */ 858 int crypto_shash_digest(struct shash_desc *desc, const u8 *data, 859 unsigned int len, u8 *out); 860 861 /** 862 * crypto_shash_tfm_digest() - calculate message digest for buffer 863 * @tfm: hash transformation object 864 * @data: see crypto_shash_update() 865 * @len: see crypto_shash_update() 866 * @out: see crypto_shash_final() 867 * 868 * This is a simplified version of crypto_shash_digest() for users who don't 869 * want to allocate their own hash descriptor (shash_desc). Instead, 870 * crypto_shash_tfm_digest() takes a hash transformation object (crypto_shash) 871 * directly, and it allocates a hash descriptor on the stack internally. 872 * Note that this stack allocation may be fairly large. 873 * 874 * Context: Any context. 875 * Return: 0 on success; < 0 if an error occurred. 876 */ 877 int crypto_shash_tfm_digest(struct crypto_shash *tfm, const u8 *data, 878 unsigned int len, u8 *out); 879 880 /** 881 * crypto_shash_export() - extract operational state for message digest 882 * @desc: reference to the operational state handle whose state is exported 883 * @out: output buffer of sufficient size that can hold the hash state 884 * 885 * This function exports the hash state of the operational state handle into the 886 * caller-allocated output buffer out which must have sufficient size (e.g. by 887 * calling crypto_shash_descsize). 888 * 889 * Context: Any context. 890 * Return: 0 if the export creation was successful; < 0 if an error occurred 891 */ 892 int crypto_shash_export(struct shash_desc *desc, void *out); 893 894 /** 895 * crypto_shash_import() - import operational state 896 * @desc: reference to the operational state handle the state imported into 897 * @in: buffer holding the state 898 * 899 * This function imports the hash state into the operational state handle from 900 * the input buffer. That buffer should have been generated with the 901 * crypto_ahash_export function. 902 * 903 * Context: Any context. 904 * Return: 0 if the import was successful; < 0 if an error occurred 905 */ 906 int crypto_shash_import(struct shash_desc *desc, const void *in); 907 908 /** 909 * crypto_shash_init() - (re)initialize message digest 910 * @desc: operational state handle that is already filled 911 * 912 * The call (re-)initializes the message digest referenced by the 913 * operational state handle. Any potentially existing state created by 914 * previous operations is discarded. 915 * 916 * Context: Any context. 917 * Return: 0 if the message digest initialization was successful; < 0 if an 918 * error occurred 919 */ 920 static inline int crypto_shash_init(struct shash_desc *desc) 921 { 922 struct crypto_shash *tfm = desc->tfm; 923 924 if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY) 925 return -ENOKEY; 926 927 return crypto_shash_alg(tfm)->init(desc); 928 } 929 930 /** 931 * crypto_shash_update() - add data to message digest for processing 932 * @desc: operational state handle that is already initialized 933 * @data: input data to be added to the message digest 934 * @len: length of the input data 935 * 936 * Updates the message digest state of the operational state handle. 937 * 938 * Context: Any context. 939 * Return: 0 if the message digest update was successful; < 0 if an error 940 * occurred 941 */ 942 int crypto_shash_update(struct shash_desc *desc, const u8 *data, 943 unsigned int len); 944 945 /** 946 * crypto_shash_final() - calculate message digest 947 * @desc: operational state handle that is already filled with data 948 * @out: output buffer filled with the message digest 949 * 950 * Finalize the message digest operation and create the message digest 951 * based on all data added to the cipher handle. The message digest is placed 952 * into the output buffer. The caller must ensure that the output buffer is 953 * large enough by using crypto_shash_digestsize. 954 * 955 * Context: Any context. 956 * Return: 0 if the message digest creation was successful; < 0 if an error 957 * occurred 958 */ 959 int crypto_shash_final(struct shash_desc *desc, u8 *out); 960 961 /** 962 * crypto_shash_finup() - calculate message digest of buffer 963 * @desc: see crypto_shash_final() 964 * @data: see crypto_shash_update() 965 * @len: see crypto_shash_update() 966 * @out: see crypto_shash_final() 967 * 968 * This function is a "short-hand" for the function calls of 969 * crypto_shash_update and crypto_shash_final. The parameters have the same 970 * meaning as discussed for those separate functions. 971 * 972 * Context: Any context. 973 * Return: 0 if the message digest creation was successful; < 0 if an error 974 * occurred 975 */ 976 int crypto_shash_finup(struct shash_desc *desc, const u8 *data, 977 unsigned int len, u8 *out); 978 979 static inline void shash_desc_zero(struct shash_desc *desc) 980 { 981 memzero_explicit(desc, 982 sizeof(*desc) + crypto_shash_descsize(desc->tfm)); 983 } 984 985 static inline int ahash_request_err(struct ahash_request *req) 986 { 987 return req->base.err; 988 } 989 990 static inline bool ahash_is_async(struct crypto_ahash *tfm) 991 { 992 return crypto_tfm_is_async(&tfm->base); 993 } 994 995 #endif /* _CRYPTO_HASH_H */ 996