Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.git] / include / crypto / internal / hash.h
... / ...
CommitLineData
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Hash algorithms.
4 *
5 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
6 */
7
8#ifndef _CRYPTO_INTERNAL_HASH_H
9#define _CRYPTO_INTERNAL_HASH_H
10
11#include <crypto/algapi.h>
12#include <crypto/hash.h>
13
14/* Set this bit to handle partial blocks in the API. */
15#define CRYPTO_AHASH_ALG_BLOCK_ONLY 0x01000000
16
17/* Set this bit if final requires at least one byte. */
18#define CRYPTO_AHASH_ALG_FINAL_NONZERO 0x02000000
19
20/* Set this bit if finup can deal with multiple blocks. */
21#define CRYPTO_AHASH_ALG_FINUP_MAX 0x04000000
22
23/* This bit is set by the Crypto API if export_core is not supported. */
24#define CRYPTO_AHASH_ALG_NO_EXPORT_CORE 0x08000000
25
26#define HASH_FBREQ_ON_STACK(name, req) \
27 char __##name##_req[sizeof(struct ahash_request) + \
28 MAX_SYNC_HASH_REQSIZE] CRYPTO_MINALIGN_ATTR; \
29 struct ahash_request *name = ahash_fbreq_on_stack_init( \
30 __##name##_req, (req))
31
32struct ahash_request;
33
34struct ahash_instance {
35 void (*free)(struct ahash_instance *inst);
36 union {
37 struct {
38 char head[offsetof(struct ahash_alg, halg.base)];
39 struct crypto_instance base;
40 } s;
41 struct ahash_alg alg;
42 };
43};
44
45struct shash_instance {
46 void (*free)(struct shash_instance *inst);
47 union {
48 struct {
49 char head[offsetof(struct shash_alg, base)];
50 struct crypto_instance base;
51 } s;
52 struct shash_alg alg;
53 };
54};
55
56struct crypto_ahash_spawn {
57 struct crypto_spawn base;
58};
59
60struct crypto_shash_spawn {
61 struct crypto_spawn base;
62};
63
64int crypto_register_ahash(struct ahash_alg *alg);
65void crypto_unregister_ahash(struct ahash_alg *alg);
66int crypto_register_ahashes(struct ahash_alg *algs, int count);
67void crypto_unregister_ahashes(struct ahash_alg *algs, int count);
68int ahash_register_instance(struct crypto_template *tmpl,
69 struct ahash_instance *inst);
70void ahash_free_singlespawn_instance(struct ahash_instance *inst);
71
72int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
73 unsigned int keylen);
74
75static inline bool crypto_shash_alg_has_setkey(struct shash_alg *alg)
76{
77 return alg->setkey != shash_no_setkey;
78}
79
80bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg);
81
82static inline bool crypto_shash_alg_needs_key(struct shash_alg *alg)
83{
84 return crypto_shash_alg_has_setkey(alg) &&
85 !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY);
86}
87
88static inline bool crypto_hash_alg_needs_key(struct hash_alg_common *alg)
89{
90 return crypto_hash_alg_has_setkey(alg) &&
91 !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY);
92}
93
94int crypto_grab_ahash(struct crypto_ahash_spawn *spawn,
95 struct crypto_instance *inst,
96 const char *name, u32 type, u32 mask);
97
98static inline void crypto_drop_ahash(struct crypto_ahash_spawn *spawn)
99{
100 crypto_drop_spawn(&spawn->base);
101}
102
103static inline struct hash_alg_common *crypto_spawn_ahash_alg(
104 struct crypto_ahash_spawn *spawn)
105{
106 return __crypto_hash_alg_common(spawn->base.alg);
107}
108
109int crypto_register_shash(struct shash_alg *alg);
110void crypto_unregister_shash(struct shash_alg *alg);
111int crypto_register_shashes(struct shash_alg *algs, int count);
112void crypto_unregister_shashes(struct shash_alg *algs, int count);
113int shash_register_instance(struct crypto_template *tmpl,
114 struct shash_instance *inst);
115void shash_free_singlespawn_instance(struct shash_instance *inst);
116
117int crypto_grab_shash(struct crypto_shash_spawn *spawn,
118 struct crypto_instance *inst,
119 const char *name, u32 type, u32 mask);
120
121static inline void crypto_drop_shash(struct crypto_shash_spawn *spawn)
122{
123 crypto_drop_spawn(&spawn->base);
124}
125
126static inline struct shash_alg *crypto_spawn_shash_alg(
127 struct crypto_shash_spawn *spawn)
128{
129 return __crypto_shash_alg(spawn->base.alg);
130}
131
132int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc);
133int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc);
134int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc);
135
136static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm)
137{
138 return crypto_tfm_ctx(crypto_ahash_tfm(tfm));
139}
140
141static inline void *crypto_ahash_ctx_dma(struct crypto_ahash *tfm)
142{
143 return crypto_tfm_ctx_dma(crypto_ahash_tfm(tfm));
144}
145
146static inline struct ahash_alg *__crypto_ahash_alg(struct crypto_alg *alg)
147{
148 return container_of(__crypto_hash_alg_common(alg), struct ahash_alg,
149 halg);
150}
151
152static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash)
153{
154 return container_of(crypto_hash_alg_common(hash), struct ahash_alg,
155 halg);
156}
157
158static inline void crypto_ahash_set_statesize(struct crypto_ahash *tfm,
159 unsigned int size)
160{
161 tfm->statesize = size;
162}
163
164static inline void crypto_ahash_set_reqsize(struct crypto_ahash *tfm,
165 unsigned int reqsize)
166{
167 tfm->reqsize = reqsize;
168}
169
170static inline void crypto_ahash_set_reqsize_dma(struct crypto_ahash *ahash,
171 unsigned int reqsize)
172{
173 reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
174 ahash->reqsize = reqsize;
175}
176
177static inline struct crypto_instance *ahash_crypto_instance(
178 struct ahash_instance *inst)
179{
180 return &inst->s.base;
181}
182
183static inline struct ahash_instance *ahash_instance(
184 struct crypto_instance *inst)
185{
186 return container_of(inst, struct ahash_instance, s.base);
187}
188
189static inline struct ahash_instance *ahash_alg_instance(
190 struct crypto_ahash *ahash)
191{
192 return ahash_instance(crypto_tfm_alg_instance(&ahash->base));
193}
194
195static inline void *ahash_instance_ctx(struct ahash_instance *inst)
196{
197 return crypto_instance_ctx(ahash_crypto_instance(inst));
198}
199
200static inline void *ahash_request_ctx_dma(struct ahash_request *req)
201{
202 unsigned int align = crypto_dma_align();
203
204 if (align <= crypto_tfm_ctx_alignment())
205 align = 1;
206
207 return PTR_ALIGN(ahash_request_ctx(req), align);
208}
209
210static inline void ahash_request_complete(struct ahash_request *req, int err)
211{
212 crypto_request_complete(&req->base, err);
213}
214
215static inline u32 ahash_request_flags(struct ahash_request *req)
216{
217 return crypto_request_flags(&req->base) & ~CRYPTO_AHASH_REQ_PRIVATE;
218}
219
220static inline struct crypto_ahash *crypto_spawn_ahash(
221 struct crypto_ahash_spawn *spawn)
222{
223 return crypto_spawn_tfm2(&spawn->base);
224}
225
226static inline int ahash_enqueue_request(struct crypto_queue *queue,
227 struct ahash_request *request)
228{
229 return crypto_enqueue_request(queue, &request->base);
230}
231
232static inline struct ahash_request *ahash_dequeue_request(
233 struct crypto_queue *queue)
234{
235 return ahash_request_cast(crypto_dequeue_request(queue));
236}
237
238static inline void *crypto_shash_ctx(struct crypto_shash *tfm)
239{
240 return crypto_tfm_ctx(&tfm->base);
241}
242
243static inline struct crypto_instance *shash_crypto_instance(
244 struct shash_instance *inst)
245{
246 return &inst->s.base;
247}
248
249static inline struct shash_instance *shash_instance(
250 struct crypto_instance *inst)
251{
252 return container_of(inst, struct shash_instance, s.base);
253}
254
255static inline struct shash_instance *shash_alg_instance(
256 struct crypto_shash *shash)
257{
258 return shash_instance(crypto_tfm_alg_instance(&shash->base));
259}
260
261static inline void *shash_instance_ctx(struct shash_instance *inst)
262{
263 return crypto_instance_ctx(shash_crypto_instance(inst));
264}
265
266static inline struct crypto_shash *crypto_spawn_shash(
267 struct crypto_shash_spawn *spawn)
268{
269 return crypto_spawn_tfm2(&spawn->base);
270}
271
272static inline struct crypto_shash *__crypto_shash_cast(struct crypto_tfm *tfm)
273{
274 return container_of(tfm, struct crypto_shash, base);
275}
276
277static inline bool ahash_request_isvirt(struct ahash_request *req)
278{
279 return req->base.flags & CRYPTO_AHASH_REQ_VIRT;
280}
281
282static inline bool crypto_ahash_req_virt(struct crypto_ahash *tfm)
283{
284 return crypto_tfm_req_virt(&tfm->base);
285}
286
287static inline struct crypto_ahash *crypto_ahash_fb(struct crypto_ahash *tfm)
288{
289 return __crypto_ahash_cast(crypto_ahash_tfm(tfm)->fb);
290}
291
292static inline struct ahash_request *ahash_fbreq_on_stack_init(
293 char *buf, struct ahash_request *old)
294{
295 struct crypto_ahash *tfm = crypto_ahash_reqtfm(old);
296 struct ahash_request *req = (void *)buf;
297
298 crypto_stack_request_init(&req->base,
299 crypto_ahash_tfm(crypto_ahash_fb(tfm)));
300 ahash_request_set_callback(req, ahash_request_flags(old), NULL, NULL);
301 req->base.flags &= ~CRYPTO_AHASH_REQ_PRIVATE;
302 req->base.flags |= old->base.flags & CRYPTO_AHASH_REQ_PRIVATE;
303 req->src = old->src;
304 req->result = old->result;
305 req->nbytes = old->nbytes;
306
307 return req;
308}
309
310/* Return the state size without partial block for block-only algorithms. */
311static inline unsigned int crypto_shash_coresize(struct crypto_shash *tfm)
312{
313 return crypto_shash_statesize(tfm) - crypto_shash_blocksize(tfm) - 1;
314}
315
316/* This can only be used if the request was never cloned. */
317#define HASH_REQUEST_ZERO(name) \
318 memzero_explicit(__##name##_req, sizeof(__##name##_req))
319
320/**
321 * crypto_ahash_export_core() - extract core state for message digest
322 * @req: reference to the ahash_request handle whose state is exported
323 * @out: output buffer of sufficient size that can hold the hash state
324 *
325 * Export the hash state without the partial block buffer.
326 *
327 * Context: Softirq or process context.
328 * Return: 0 if the export creation was successful; < 0 if an error occurred
329 */
330int crypto_ahash_export_core(struct ahash_request *req, void *out);
331
332/**
333 * crypto_ahash_import_core() - import core state
334 * @req: reference to ahash_request handle the state is imported into
335 * @in: buffer holding the state
336 *
337 * Import the hash state without the partial block buffer.
338 *
339 * Context: Softirq or process context.
340 * Return: 0 if the import was successful; < 0 if an error occurred
341 */
342int crypto_ahash_import_core(struct ahash_request *req, const void *in);
343
344/**
345 * crypto_shash_export_core() - extract core state for message digest
346 * @desc: reference to the operational state handle whose state is exported
347 * @out: output buffer of sufficient size that can hold the hash state
348 *
349 * Export the hash state without the partial block buffer.
350 *
351 * Context: Softirq or process context.
352 * Return: 0 if the export creation was successful; < 0 if an error occurred
353 */
354int crypto_shash_export_core(struct shash_desc *desc, void *out);
355
356/**
357 * crypto_shash_import_core() - import core state
358 * @desc: reference to the operational state handle the state imported into
359 * @in: buffer holding the state
360 *
361 * Import the hash state without the partial block buffer.
362 *
363 * Context: Softirq or process context.
364 * Return: 0 if the import was successful; < 0 if an error occurred
365 */
366int crypto_shash_import_core(struct shash_desc *desc, const void *in);
367
368#endif /* _CRYPTO_INTERNAL_HASH_H */
369