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