Commit | Line | Data |
---|---|---|
2874c5fd | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
20036252 HX |
2 | /* |
3 | * Hash algorithms. | |
4 | * | |
5 | * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au> | |
20036252 HX |
6 | */ |
7 | ||
8 | #ifndef _CRYPTO_INTERNAL_HASH_H | |
9 | #define _CRYPTO_INTERNAL_HASH_H | |
10 | ||
11 | #include <crypto/algapi.h> | |
18e33e6d | 12 | #include <crypto/hash.h> |
20036252 | 13 | |
7650f826 HX |
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 | ||
c6a12f39 HX |
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 | ||
04bfa4c7 HX |
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 | ||
20036252 | 32 | struct ahash_request; |
20036252 | 33 | |
01c2dece | 34 | struct ahash_instance { |
48fb3e57 | 35 | void (*free)(struct ahash_instance *inst); |
77f7e94d EB |
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 | }; | |
01c2dece HX |
43 | }; |
44 | ||
2e4fddd8 | 45 | struct shash_instance { |
48fb3e57 | 46 | void (*free)(struct shash_instance *inst); |
1b84e7d0 EB |
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 | }; | |
2e4fddd8 HX |
54 | }; |
55 | ||
01c2dece HX |
56 | struct crypto_ahash_spawn { |
57 | struct crypto_spawn base; | |
58 | }; | |
59 | ||
94296999 HX |
60 | struct crypto_shash_spawn { |
61 | struct crypto_spawn base; | |
62 | }; | |
63 | ||
88056ec3 | 64 | int crypto_register_ahash(struct ahash_alg *alg); |
c6d633a9 | 65 | void crypto_unregister_ahash(struct ahash_alg *alg); |
6f7473c5 RV |
66 | int crypto_register_ahashes(struct ahash_alg *algs, int count); |
67 | void crypto_unregister_ahashes(struct ahash_alg *algs, int count); | |
01c2dece HX |
68 | int ahash_register_instance(struct crypto_template *tmpl, |
69 | struct ahash_instance *inst); | |
c3103416 | 70 | void ahash_free_singlespawn_instance(struct ahash_instance *inst); |
01c2dece | 71 | |
c060e16d EB |
72 | int shash_no_setkey(struct crypto_shash *tfm, const u8 *key, |
73 | unsigned int keylen); | |
74 | ||
75 | static inline bool crypto_shash_alg_has_setkey(struct shash_alg *alg) | |
76 | { | |
77 | return alg->setkey != shash_no_setkey; | |
78 | } | |
af3ff804 | 79 | |
c3103416 HX |
80 | bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg); |
81 | ||
c2881789 EB |
82 | static 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 | ||
c3103416 HX |
88 | static 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 | ||
84a9c938 EB |
94 | int crypto_grab_ahash(struct crypto_ahash_spawn *spawn, |
95 | struct crypto_instance *inst, | |
96 | const char *name, u32 type, u32 mask); | |
97 | ||
52861c7c HX |
98 | static inline void crypto_drop_ahash(struct crypto_ahash_spawn *spawn) |
99 | { | |
100 | crypto_drop_spawn(&spawn->base); | |
101 | } | |
102 | ||
84a9c938 EB |
103 | static 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 | ||
7b5a080b | 109 | int crypto_register_shash(struct shash_alg *alg); |
c6d633a9 | 110 | void crypto_unregister_shash(struct shash_alg *alg); |
50fc3e8d | 111 | int crypto_register_shashes(struct shash_alg *algs, int count); |
c6d633a9 | 112 | void crypto_unregister_shashes(struct shash_alg *algs, int count); |
619a6ebd HX |
113 | int shash_register_instance(struct crypto_template *tmpl, |
114 | struct shash_instance *inst); | |
a39c66cc | 115 | void shash_free_singlespawn_instance(struct shash_instance *inst); |
2e4fddd8 | 116 | |
fdfad1ff EB |
117 | int crypto_grab_shash(struct crypto_shash_spawn *spawn, |
118 | struct crypto_instance *inst, | |
119 | const char *name, u32 type, u32 mask); | |
120 | ||
52861c7c HX |
121 | static inline void crypto_drop_shash(struct crypto_shash_spawn *spawn) |
122 | { | |
123 | crypto_drop_spawn(&spawn->base); | |
124 | } | |
125 | ||
fdfad1ff EB |
126 | static 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 | ||
7eddf95e | 132 | int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc); |
66f6ce5e | 133 | int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc); |
7eddf95e | 134 | int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc); |
1e65b81a | 135 | |
18e33e6d HX |
136 | static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm) |
137 | { | |
88056ec3 | 138 | return crypto_tfm_ctx(crypto_ahash_tfm(tfm)); |
18e33e6d HX |
139 | } |
140 | ||
b5f755fb HX |
141 | static inline void *crypto_ahash_ctx_dma(struct crypto_ahash *tfm) |
142 | { | |
143 | return crypto_tfm_ctx_dma(crypto_ahash_tfm(tfm)); | |
144 | } | |
145 | ||
4dc10c01 HX |
146 | static 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 | ||
3908edf8 HX |
152 | static 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 | ||
c7535fb2 HX |
158 | static inline void crypto_ahash_set_statesize(struct crypto_ahash *tfm, |
159 | unsigned int size) | |
160 | { | |
161 | tfm->statesize = size; | |
162 | } | |
163 | ||
fc00127f HX |
164 | static inline void crypto_ahash_set_reqsize(struct crypto_ahash *tfm, |
165 | unsigned int reqsize) | |
166 | { | |
88056ec3 | 167 | tfm->reqsize = reqsize; |
fc00127f HX |
168 | } |
169 | ||
b5f755fb HX |
170 | static 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 | ||
01c2dece HX |
177 | static inline struct crypto_instance *ahash_crypto_instance( |
178 | struct ahash_instance *inst) | |
179 | { | |
77f7e94d | 180 | return &inst->s.base; |
01c2dece HX |
181 | } |
182 | ||
183 | static inline struct ahash_instance *ahash_instance( | |
184 | struct crypto_instance *inst) | |
185 | { | |
77f7e94d | 186 | return container_of(inst, struct ahash_instance, s.base); |
01c2dece HX |
187 | } |
188 | ||
b00ba76a HX |
189 | static 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 | ||
01c2dece HX |
195 | static inline void *ahash_instance_ctx(struct ahash_instance *inst) |
196 | { | |
197 | return crypto_instance_ctx(ahash_crypto_instance(inst)); | |
198 | } | |
199 | ||
b5f755fb HX |
200 | static 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 | ||
ef0579b6 HX |
210 | static inline void ahash_request_complete(struct ahash_request *req, int err) |
211 | { | |
d9588045 | 212 | crypto_request_complete(&req->base, err); |
ef0579b6 HX |
213 | } |
214 | ||
215 | static inline u32 ahash_request_flags(struct ahash_request *req) | |
216 | { | |
04bfa4c7 | 217 | return crypto_request_flags(&req->base) & ~CRYPTO_AHASH_REQ_PRIVATE; |
ef0579b6 HX |
218 | } |
219 | ||
01c2dece HX |
220 | static inline struct crypto_ahash *crypto_spawn_ahash( |
221 | struct crypto_ahash_spawn *spawn) | |
222 | { | |
223 | return crypto_spawn_tfm2(&spawn->base); | |
224 | } | |
225 | ||
18e33e6d HX |
226 | static 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 | ||
232 | static inline struct ahash_request *ahash_dequeue_request( | |
233 | struct crypto_queue *queue) | |
234 | { | |
235 | return ahash_request_cast(crypto_dequeue_request(queue)); | |
236 | } | |
237 | ||
7b5a080b HX |
238 | static inline void *crypto_shash_ctx(struct crypto_shash *tfm) |
239 | { | |
240 | return crypto_tfm_ctx(&tfm->base); | |
241 | } | |
242 | ||
2e4fddd8 HX |
243 | static inline struct crypto_instance *shash_crypto_instance( |
244 | struct shash_instance *inst) | |
245 | { | |
1b84e7d0 | 246 | return &inst->s.base; |
2e4fddd8 HX |
247 | } |
248 | ||
249 | static inline struct shash_instance *shash_instance( | |
250 | struct crypto_instance *inst) | |
251 | { | |
1b84e7d0 | 252 | return container_of(inst, struct shash_instance, s.base); |
2e4fddd8 HX |
253 | } |
254 | ||
d9e1670b HX |
255 | static 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 | ||
ef5d590c HX |
261 | static inline void *shash_instance_ctx(struct shash_instance *inst) |
262 | { | |
263 | return crypto_instance_ctx(shash_crypto_instance(inst)); | |
264 | } | |
265 | ||
94296999 HX |
266 | static inline struct crypto_shash *crypto_spawn_shash( |
267 | struct crypto_shash_spawn *spawn) | |
268 | { | |
269 | return crypto_spawn_tfm2(&spawn->base); | |
270 | } | |
271 | ||
0390e6ae HX |
272 | static inline struct crypto_shash *__crypto_shash_cast(struct crypto_tfm *tfm) |
273 | { | |
113adefc | 274 | return container_of(tfm, struct crypto_shash, base); |
0390e6ae HX |
275 | } |
276 | ||
439963cd HX |
277 | static inline bool ahash_request_isvirt(struct ahash_request *req) |
278 | { | |
279 | return req->base.flags & CRYPTO_AHASH_REQ_VIRT; | |
280 | } | |
281 | ||
8fd17374 | 282 | static inline bool crypto_ahash_req_virt(struct crypto_ahash *tfm) |
f2ffe5a9 | 283 | { |
8fd17374 | 284 | return crypto_tfm_req_virt(&tfm->base); |
f2ffe5a9 HX |
285 | } |
286 | ||
19da081a HX |
287 | static inline struct crypto_ahash *crypto_ahash_fb(struct crypto_ahash *tfm) |
288 | { | |
289 | return __crypto_ahash_cast(crypto_ahash_tfm(tfm)->fb); | |
290 | } | |
291 | ||
04bfa4c7 HX |
292 | static 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 | ||
b75fa20c HX |
298 | crypto_stack_request_init(&req->base, |
299 | crypto_ahash_tfm(crypto_ahash_fb(tfm))); | |
04bfa4c7 HX |
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 | ||
7650f826 HX |
310 | /* Return the state size without partial block for block-only algorithms. */ |
311 | static inline unsigned int crypto_shash_coresize(struct crypto_shash *tfm) | |
312 | { | |
313 | return crypto_shash_statesize(tfm) - crypto_shash_blocksize(tfm) - 1; | |
314 | } | |
315 | ||
88bca957 HX |
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 | ||
cd5a4d53 HX |
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 | */ | |
330 | int 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 | */ | |
342 | int 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 | */ | |
354 | int 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 | */ | |
366 | int crypto_shash_import_core(struct shash_desc *desc, const void *in); | |
367 | ||
20036252 HX |
368 | #endif /* _CRYPTO_INTERNAL_HASH_H */ |
369 |