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