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