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