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