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