1 /* SPDX-License-Identifier: GPL-2.0-or-later */
5 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
8 #ifndef _CRYPTO_INTERNAL_HASH_H
9 #define _CRYPTO_INTERNAL_HASH_H
11 #include <crypto/algapi.h>
12 #include <crypto/hash.h>
14 /* Set this bit to handle partial blocks in the API. */
15 #define CRYPTO_AHASH_ALG_BLOCK_ONLY 0x01000000
17 /* Set this bit if final requires at least one byte. */
18 #define CRYPTO_AHASH_ALG_FINAL_NONZERO 0x02000000
20 /* Set this bit if finup can deal with multiple blocks. */
21 #define CRYPTO_AHASH_ALG_FINUP_MAX 0x04000000
23 /* This bit is set by the Crypto API if export_core is not supported. */
24 #define CRYPTO_AHASH_ALG_NO_EXPORT_CORE 0x08000000
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))
34 struct ahash_instance {
35 void (*free)(struct ahash_instance *inst);
38 char head[offsetof(struct ahash_alg, halg.base)];
39 struct crypto_instance base;
45 struct shash_instance {
46 void (*free)(struct shash_instance *inst);
49 char head[offsetof(struct shash_alg, base)];
50 struct crypto_instance base;
56 struct crypto_ahash_spawn {
57 struct crypto_spawn base;
60 struct crypto_shash_spawn {
61 struct crypto_spawn base;
64 int crypto_register_ahash(struct ahash_alg *alg);
65 void crypto_unregister_ahash(struct ahash_alg *alg);
66 int crypto_register_ahashes(struct ahash_alg *algs, int count);
67 void crypto_unregister_ahashes(struct ahash_alg *algs, int count);
68 int ahash_register_instance(struct crypto_template *tmpl,
69 struct ahash_instance *inst);
70 void ahash_free_singlespawn_instance(struct ahash_instance *inst);
72 int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
75 static inline bool crypto_shash_alg_has_setkey(struct shash_alg *alg)
77 return alg->setkey != shash_no_setkey;
80 bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg);
82 static inline bool crypto_shash_alg_needs_key(struct shash_alg *alg)
84 return crypto_shash_alg_has_setkey(alg) &&
85 !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY);
88 static inline bool crypto_hash_alg_needs_key(struct hash_alg_common *alg)
90 return crypto_hash_alg_has_setkey(alg) &&
91 !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY);
94 int crypto_grab_ahash(struct crypto_ahash_spawn *spawn,
95 struct crypto_instance *inst,
96 const char *name, u32 type, u32 mask);
98 static inline void crypto_drop_ahash(struct crypto_ahash_spawn *spawn)
100 crypto_drop_spawn(&spawn->base);
103 static inline struct hash_alg_common *crypto_spawn_ahash_alg(
104 struct crypto_ahash_spawn *spawn)
106 return __crypto_hash_alg_common(spawn->base.alg);
109 int crypto_register_shash(struct shash_alg *alg);
110 void crypto_unregister_shash(struct shash_alg *alg);
111 int crypto_register_shashes(struct shash_alg *algs, int count);
112 void crypto_unregister_shashes(struct shash_alg *algs, int count);
113 int shash_register_instance(struct crypto_template *tmpl,
114 struct shash_instance *inst);
115 void shash_free_singlespawn_instance(struct shash_instance *inst);
117 int crypto_grab_shash(struct crypto_shash_spawn *spawn,
118 struct crypto_instance *inst,
119 const char *name, u32 type, u32 mask);
121 static inline void crypto_drop_shash(struct crypto_shash_spawn *spawn)
123 crypto_drop_spawn(&spawn->base);
126 static inline struct shash_alg *crypto_spawn_shash_alg(
127 struct crypto_shash_spawn *spawn)
129 return __crypto_shash_alg(spawn->base.alg);
132 int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc);
133 int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc);
134 int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc);
136 static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm)
138 return crypto_tfm_ctx(crypto_ahash_tfm(tfm));
141 static inline void *crypto_ahash_ctx_dma(struct crypto_ahash *tfm)
143 return crypto_tfm_ctx_dma(crypto_ahash_tfm(tfm));
146 static inline struct ahash_alg *__crypto_ahash_alg(struct crypto_alg *alg)
148 return container_of(__crypto_hash_alg_common(alg), struct ahash_alg,
152 static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash)
154 return container_of(crypto_hash_alg_common(hash), struct ahash_alg,
158 static inline void crypto_ahash_set_statesize(struct crypto_ahash *tfm,
161 tfm->statesize = size;
164 static inline void crypto_ahash_set_reqsize(struct crypto_ahash *tfm,
165 unsigned int reqsize)
167 tfm->reqsize = reqsize;
170 static inline void crypto_ahash_set_reqsize_dma(struct crypto_ahash *ahash,
171 unsigned int reqsize)
173 reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
174 ahash->reqsize = reqsize;
177 static inline struct crypto_instance *ahash_crypto_instance(
178 struct ahash_instance *inst)
180 return &inst->s.base;
183 static inline struct ahash_instance *ahash_instance(
184 struct crypto_instance *inst)
186 return container_of(inst, struct ahash_instance, s.base);
189 static inline struct ahash_instance *ahash_alg_instance(
190 struct crypto_ahash *ahash)
192 return ahash_instance(crypto_tfm_alg_instance(&ahash->base));
195 static inline void *ahash_instance_ctx(struct ahash_instance *inst)
197 return crypto_instance_ctx(ahash_crypto_instance(inst));
200 static inline void *ahash_request_ctx_dma(struct ahash_request *req)
202 unsigned int align = crypto_dma_align();
204 if (align <= crypto_tfm_ctx_alignment())
207 return PTR_ALIGN(ahash_request_ctx(req), align);
210 static inline void ahash_request_complete(struct ahash_request *req, int err)
212 crypto_request_complete(&req->base, err);
215 static inline u32 ahash_request_flags(struct ahash_request *req)
217 return crypto_request_flags(&req->base) & ~CRYPTO_AHASH_REQ_PRIVATE;
220 static inline struct crypto_ahash *crypto_spawn_ahash(
221 struct crypto_ahash_spawn *spawn)
223 return crypto_spawn_tfm2(&spawn->base);
226 static inline int ahash_enqueue_request(struct crypto_queue *queue,
227 struct ahash_request *request)
229 return crypto_enqueue_request(queue, &request->base);
232 static inline struct ahash_request *ahash_dequeue_request(
233 struct crypto_queue *queue)
235 return ahash_request_cast(crypto_dequeue_request(queue));
238 static inline void *crypto_shash_ctx(struct crypto_shash *tfm)
240 return crypto_tfm_ctx(&tfm->base);
243 static inline struct crypto_instance *shash_crypto_instance(
244 struct shash_instance *inst)
246 return &inst->s.base;
249 static inline struct shash_instance *shash_instance(
250 struct crypto_instance *inst)
252 return container_of(inst, struct shash_instance, s.base);
255 static inline struct shash_instance *shash_alg_instance(
256 struct crypto_shash *shash)
258 return shash_instance(crypto_tfm_alg_instance(&shash->base));
261 static inline void *shash_instance_ctx(struct shash_instance *inst)
263 return crypto_instance_ctx(shash_crypto_instance(inst));
266 static inline struct crypto_shash *crypto_spawn_shash(
267 struct crypto_shash_spawn *spawn)
269 return crypto_spawn_tfm2(&spawn->base);
272 static inline struct crypto_shash *__crypto_shash_cast(struct crypto_tfm *tfm)
274 return container_of(tfm, struct crypto_shash, base);
277 static inline bool ahash_request_isvirt(struct ahash_request *req)
279 return req->base.flags & CRYPTO_AHASH_REQ_VIRT;
282 static inline bool crypto_ahash_req_virt(struct crypto_ahash *tfm)
284 return crypto_tfm_req_virt(&tfm->base);
287 static inline struct crypto_ahash *crypto_ahash_fb(struct crypto_ahash *tfm)
289 return __crypto_ahash_cast(crypto_ahash_tfm(tfm)->fb);
292 static inline struct ahash_request *ahash_fbreq_on_stack_init(
293 char *buf, struct ahash_request *old)
295 struct crypto_ahash *tfm = crypto_ahash_reqtfm(old);
296 struct ahash_request *req = (void *)buf;
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;
304 req->result = old->result;
305 req->nbytes = old->nbytes;
310 /* Return the state size without partial block for block-only algorithms. */
311 static inline unsigned int crypto_shash_coresize(struct crypto_shash *tfm)
313 return crypto_shash_statesize(tfm) - crypto_shash_blocksize(tfm) - 1;
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))
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
325 * Export the hash state without the partial block buffer.
327 * Context: Softirq or process context.
328 * Return: 0 if the export creation was successful; < 0 if an error occurred
330 int crypto_ahash_export_core(struct ahash_request *req, void *out);
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
337 * Import the hash state without the partial block buffer.
339 * Context: Softirq or process context.
340 * Return: 0 if the import was successful; < 0 if an error occurred
342 int crypto_ahash_import_core(struct ahash_request *req, const void *in);
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
349 * Export the hash state without the partial block buffer.
351 * Context: Softirq or process context.
352 * Return: 0 if the export creation was successful; < 0 if an error occurred
354 int crypto_shash_export_core(struct shash_desc *desc, void *out);
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
361 * Import the hash state without the partial block buffer.
363 * Context: Softirq or process context.
364 * Return: 0 if the import was successful; < 0 if an error occurred
366 int crypto_shash_import_core(struct shash_desc *desc, const void *in);
368 #endif /* _CRYPTO_INTERNAL_HASH_H */