12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
220036252SHerbert Xu /*
320036252SHerbert Xu * Hash algorithms.
420036252SHerbert Xu *
520036252SHerbert Xu * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
620036252SHerbert Xu */
720036252SHerbert Xu
820036252SHerbert Xu #ifndef _CRYPTO_INTERNAL_HASH_H
920036252SHerbert Xu #define _CRYPTO_INTERNAL_HASH_H
1020036252SHerbert Xu
1120036252SHerbert Xu #include <crypto/algapi.h>
1218e33e6dSHerbert Xu #include <crypto/hash.h>
1320036252SHerbert Xu
1420036252SHerbert Xu struct ahash_request;
1520036252SHerbert Xu struct scatterlist;
1620036252SHerbert Xu
1720036252SHerbert Xu struct crypto_hash_walk {
1820036252SHerbert Xu char *data;
1920036252SHerbert Xu
2020036252SHerbert Xu unsigned int offset;
2120036252SHerbert Xu unsigned int alignmask;
2220036252SHerbert Xu
2320036252SHerbert Xu struct page *pg;
2420036252SHerbert Xu unsigned int entrylen;
2520036252SHerbert Xu
2620036252SHerbert Xu unsigned int total;
2720036252SHerbert Xu struct scatterlist *sg;
2820036252SHerbert Xu
2920036252SHerbert Xu unsigned int flags;
3020036252SHerbert Xu };
3120036252SHerbert Xu
3201c2deceSHerbert Xu struct ahash_instance {
3348fb3e57SEric Biggers void (*free)(struct ahash_instance *inst);
3477f7e94dSEric Biggers union {
3577f7e94dSEric Biggers struct {
3677f7e94dSEric Biggers char head[offsetof(struct ahash_alg, halg.base)];
3777f7e94dSEric Biggers struct crypto_instance base;
3877f7e94dSEric Biggers } s;
3901c2deceSHerbert Xu struct ahash_alg alg;
4001c2deceSHerbert Xu };
4177f7e94dSEric Biggers };
4201c2deceSHerbert Xu
432e4fddd8SHerbert Xu struct shash_instance {
4448fb3e57SEric Biggers void (*free)(struct shash_instance *inst);
451b84e7d0SEric Biggers union {
461b84e7d0SEric Biggers struct {
471b84e7d0SEric Biggers char head[offsetof(struct shash_alg, base)];
481b84e7d0SEric Biggers struct crypto_instance base;
491b84e7d0SEric Biggers } s;
502e4fddd8SHerbert Xu struct shash_alg alg;
512e4fddd8SHerbert Xu };
521b84e7d0SEric Biggers };
532e4fddd8SHerbert Xu
5401c2deceSHerbert Xu struct crypto_ahash_spawn {
5501c2deceSHerbert Xu struct crypto_spawn base;
5601c2deceSHerbert Xu };
5701c2deceSHerbert Xu
5894296999SHerbert Xu struct crypto_shash_spawn {
5994296999SHerbert Xu struct crypto_spawn base;
6094296999SHerbert Xu };
6194296999SHerbert Xu
6220036252SHerbert Xu int crypto_hash_walk_done(struct crypto_hash_walk *walk, int err);
6320036252SHerbert Xu int crypto_hash_walk_first(struct ahash_request *req,
6420036252SHerbert Xu struct crypto_hash_walk *walk);
6575ecb231SHerbert Xu
crypto_hash_walk_last(struct crypto_hash_walk * walk)6666f6ce5eSHerbert Xu static inline int crypto_hash_walk_last(struct crypto_hash_walk *walk)
6766f6ce5eSHerbert Xu {
6866f6ce5eSHerbert Xu return !(walk->entrylen | walk->total);
6966f6ce5eSHerbert Xu }
7066f6ce5eSHerbert Xu
7188056ec3SHerbert Xu int crypto_register_ahash(struct ahash_alg *alg);
72c6d633a9SEric Biggers void crypto_unregister_ahash(struct ahash_alg *alg);
736f7473c5SRabin Vincent int crypto_register_ahashes(struct ahash_alg *algs, int count);
746f7473c5SRabin Vincent void crypto_unregister_ahashes(struct ahash_alg *algs, int count);
7501c2deceSHerbert Xu int ahash_register_instance(struct crypto_template *tmpl,
7601c2deceSHerbert Xu struct ahash_instance *inst);
7701c2deceSHerbert Xu
78c060e16dSEric Biggers int shash_no_setkey(struct crypto_shash *tfm, const u8 *key,
79c060e16dSEric Biggers unsigned int keylen);
80c060e16dSEric Biggers
crypto_shash_alg_has_setkey(struct shash_alg * alg)81c060e16dSEric Biggers static inline bool crypto_shash_alg_has_setkey(struct shash_alg *alg)
82c060e16dSEric Biggers {
83c060e16dSEric Biggers return alg->setkey != shash_no_setkey;
84c060e16dSEric Biggers }
85af3ff804SEric Biggers
crypto_shash_alg_needs_key(struct shash_alg * alg)86c2881789SEric Biggers static inline bool crypto_shash_alg_needs_key(struct shash_alg *alg)
87c2881789SEric Biggers {
88c2881789SEric Biggers return crypto_shash_alg_has_setkey(alg) &&
89c2881789SEric Biggers !(alg->base.cra_flags & CRYPTO_ALG_OPTIONAL_KEY);
90c2881789SEric Biggers }
91c2881789SEric Biggers
92cd6ed77aSEric Biggers bool crypto_hash_alg_has_setkey(struct hash_alg_common *halg);
93cd6ed77aSEric Biggers
9484a9c938SEric Biggers int crypto_grab_ahash(struct crypto_ahash_spawn *spawn,
9584a9c938SEric Biggers struct crypto_instance *inst,
9684a9c938SEric Biggers const char *name, u32 type, u32 mask);
9784a9c938SEric Biggers
crypto_drop_ahash(struct crypto_ahash_spawn * spawn)9852861c7cSHerbert Xu static inline void crypto_drop_ahash(struct crypto_ahash_spawn *spawn)
9952861c7cSHerbert Xu {
10052861c7cSHerbert Xu crypto_drop_spawn(&spawn->base);
10152861c7cSHerbert Xu }
10252861c7cSHerbert Xu
crypto_spawn_ahash_alg(struct crypto_ahash_spawn * spawn)10384a9c938SEric Biggers static inline struct hash_alg_common *crypto_spawn_ahash_alg(
10484a9c938SEric Biggers struct crypto_ahash_spawn *spawn)
10584a9c938SEric Biggers {
10684a9c938SEric Biggers return __crypto_hash_alg_common(spawn->base.alg);
10784a9c938SEric Biggers }
10884a9c938SEric Biggers
1097b5a080bSHerbert Xu int crypto_register_shash(struct shash_alg *alg);
110c6d633a9SEric Biggers void crypto_unregister_shash(struct shash_alg *alg);
11150fc3e8dSJussi Kivilinna int crypto_register_shashes(struct shash_alg *algs, int count);
112c6d633a9SEric Biggers void crypto_unregister_shashes(struct shash_alg *algs, int count);
113619a6ebdSHerbert Xu int shash_register_instance(struct crypto_template *tmpl,
114619a6ebdSHerbert Xu struct shash_instance *inst);
115a39c66ccSEric Biggers void shash_free_singlespawn_instance(struct shash_instance *inst);
1162e4fddd8SHerbert Xu
117fdfad1ffSEric Biggers int crypto_grab_shash(struct crypto_shash_spawn *spawn,
118fdfad1ffSEric Biggers struct crypto_instance *inst,
119fdfad1ffSEric Biggers const char *name, u32 type, u32 mask);
120fdfad1ffSEric Biggers
crypto_drop_shash(struct crypto_shash_spawn * spawn)12152861c7cSHerbert Xu static inline void crypto_drop_shash(struct crypto_shash_spawn *spawn)
12252861c7cSHerbert Xu {
12352861c7cSHerbert Xu crypto_drop_spawn(&spawn->base);
12452861c7cSHerbert Xu }
12552861c7cSHerbert Xu
crypto_spawn_shash_alg(struct crypto_shash_spawn * spawn)126fdfad1ffSEric Biggers static inline struct shash_alg *crypto_spawn_shash_alg(
127fdfad1ffSEric Biggers struct crypto_shash_spawn *spawn)
128fdfad1ffSEric Biggers {
129fdfad1ffSEric Biggers return __crypto_shash_alg(spawn->base.alg);
130fdfad1ffSEric Biggers }
131fdfad1ffSEric Biggers
1327eddf95eSHerbert Xu int shash_ahash_update(struct ahash_request *req, struct shash_desc *desc);
13366f6ce5eSHerbert Xu int shash_ahash_finup(struct ahash_request *req, struct shash_desc *desc);
1347eddf95eSHerbert Xu int shash_ahash_digest(struct ahash_request *req, struct shash_desc *desc);
1357eddf95eSHerbert Xu
crypto_ahash_ctx(struct crypto_ahash * tfm)13618e33e6dSHerbert Xu static inline void *crypto_ahash_ctx(struct crypto_ahash *tfm)
13718e33e6dSHerbert Xu {
13888056ec3SHerbert Xu return crypto_tfm_ctx(crypto_ahash_tfm(tfm));
13918e33e6dSHerbert Xu }
14018e33e6dSHerbert Xu
crypto_ahash_ctx_dma(struct crypto_ahash * tfm)141b5f755fbSHerbert Xu static inline void *crypto_ahash_ctx_dma(struct crypto_ahash *tfm)
142b5f755fbSHerbert Xu {
143b5f755fbSHerbert Xu return crypto_tfm_ctx_dma(crypto_ahash_tfm(tfm));
144b5f755fbSHerbert Xu }
145b5f755fbSHerbert Xu
__crypto_ahash_alg(struct crypto_alg * alg)1464dc10c01SHerbert Xu static inline struct ahash_alg *__crypto_ahash_alg(struct crypto_alg *alg)
1474dc10c01SHerbert Xu {
1484dc10c01SHerbert Xu return container_of(__crypto_hash_alg_common(alg), struct ahash_alg,
1494dc10c01SHerbert Xu halg);
1504dc10c01SHerbert Xu }
1514dc10c01SHerbert Xu
crypto_ahash_alg(struct crypto_ahash * hash)152fc00127fSHerbert Xu static inline struct ahash_alg *crypto_ahash_alg(struct crypto_ahash *hash)
153fc00127fSHerbert Xu {
154fc00127fSHerbert Xu return container_of(crypto_hash_alg_common(hash), struct ahash_alg,
15588056ec3SHerbert Xu halg);
156fc00127fSHerbert Xu }
157fc00127fSHerbert Xu
crypto_ahash_set_statesize(struct crypto_ahash * tfm,unsigned int size)158b5f755fbSHerbert Xu static inline void crypto_ahash_set_statesize(struct crypto_ahash *tfm,
159b5f755fbSHerbert Xu unsigned int size)
160b5f755fbSHerbert Xu {
161b5f755fbSHerbert Xu tfm->statesize = size;
162b5f755fbSHerbert Xu }
163b5f755fbSHerbert Xu
crypto_ahash_set_reqsize(struct crypto_ahash * tfm,unsigned int reqsize)164b5f755fbSHerbert Xu static inline void crypto_ahash_set_reqsize(struct crypto_ahash *tfm,
16501c2deceSHerbert Xu unsigned int reqsize)
16601c2deceSHerbert Xu {
16701c2deceSHerbert Xu tfm->reqsize = reqsize;
16877f7e94dSEric Biggers }
16901c2deceSHerbert Xu
crypto_ahash_set_reqsize_dma(struct crypto_ahash * ahash,unsigned int reqsize)17001c2deceSHerbert Xu static inline void crypto_ahash_set_reqsize_dma(struct crypto_ahash *ahash,
17101c2deceSHerbert Xu unsigned int reqsize)
17201c2deceSHerbert Xu {
17301c2deceSHerbert Xu reqsize += crypto_dma_align() & ~(crypto_tfm_ctx_alignment() - 1);
17477f7e94dSEric Biggers ahash->reqsize = reqsize;
17501c2deceSHerbert Xu }
17601c2deceSHerbert Xu
ahash_crypto_instance(struct ahash_instance * inst)177b00ba76aSHerbert Xu static inline struct crypto_instance *ahash_crypto_instance(
178b00ba76aSHerbert Xu struct ahash_instance *inst)
179b00ba76aSHerbert Xu {
180b00ba76aSHerbert Xu return &inst->s.base;
181b00ba76aSHerbert Xu }
182b00ba76aSHerbert Xu
ahash_instance(struct crypto_instance * inst)18301c2deceSHerbert Xu static inline struct ahash_instance *ahash_instance(
18401c2deceSHerbert Xu struct crypto_instance *inst)
18501c2deceSHerbert Xu {
18601c2deceSHerbert Xu return container_of(inst, struct ahash_instance, s.base);
18701c2deceSHerbert Xu }
188b5f755fbSHerbert Xu
ahash_alg_instance(struct crypto_ahash * ahash)189b5f755fbSHerbert Xu static inline struct ahash_instance *ahash_alg_instance(
190b5f755fbSHerbert Xu struct crypto_ahash *ahash)
191b5f755fbSHerbert Xu {
192b5f755fbSHerbert Xu return ahash_instance(crypto_tfm_alg_instance(&ahash->base));
193b5f755fbSHerbert Xu }
194b5f755fbSHerbert Xu
ahash_instance_ctx(struct ahash_instance * inst)195b5f755fbSHerbert Xu static inline void *ahash_instance_ctx(struct ahash_instance *inst)
196b5f755fbSHerbert Xu {
197b5f755fbSHerbert Xu return crypto_instance_ctx(ahash_crypto_instance(inst));
198ef0579b6SHerbert Xu }
199ef0579b6SHerbert Xu
ahash_request_ctx_dma(struct ahash_request * req)200*d9588045SHerbert Xu static inline void *ahash_request_ctx_dma(struct ahash_request *req)
201ef0579b6SHerbert Xu {
202ef0579b6SHerbert Xu unsigned int align = crypto_dma_align();
203ef0579b6SHerbert Xu
204ef0579b6SHerbert Xu if (align <= crypto_tfm_ctx_alignment())
205ef0579b6SHerbert Xu align = 1;
206ef0579b6SHerbert Xu
207ef0579b6SHerbert Xu return PTR_ALIGN(ahash_request_ctx(req), align);
20801c2deceSHerbert Xu }
20901c2deceSHerbert Xu
ahash_request_complete(struct ahash_request * req,int err)21001c2deceSHerbert Xu static inline void ahash_request_complete(struct ahash_request *req, int err)
21101c2deceSHerbert Xu {
21201c2deceSHerbert Xu crypto_request_complete(&req->base, err);
21301c2deceSHerbert Xu }
21418e33e6dSHerbert Xu
ahash_request_flags(struct ahash_request * req)21518e33e6dSHerbert Xu static inline u32 ahash_request_flags(struct ahash_request *req)
21618e33e6dSHerbert Xu {
21718e33e6dSHerbert Xu return req->base.flags;
21818e33e6dSHerbert Xu }
21918e33e6dSHerbert Xu
crypto_spawn_ahash(struct crypto_ahash_spawn * spawn)22018e33e6dSHerbert Xu static inline struct crypto_ahash *crypto_spawn_ahash(
22118e33e6dSHerbert Xu struct crypto_ahash_spawn *spawn)
22218e33e6dSHerbert Xu {
22318e33e6dSHerbert Xu return crypto_spawn_tfm2(&spawn->base);
22418e33e6dSHerbert Xu }
22518e33e6dSHerbert Xu
ahash_enqueue_request(struct crypto_queue * queue,struct ahash_request * request)2267b5a080bSHerbert Xu static inline int ahash_enqueue_request(struct crypto_queue *queue,
2277b5a080bSHerbert Xu struct ahash_request *request)
2287b5a080bSHerbert Xu {
2297b5a080bSHerbert Xu return crypto_enqueue_request(queue, &request->base);
2307b5a080bSHerbert Xu }
2312e4fddd8SHerbert Xu
ahash_dequeue_request(struct crypto_queue * queue)2322e4fddd8SHerbert Xu static inline struct ahash_request *ahash_dequeue_request(
2332e4fddd8SHerbert Xu struct crypto_queue *queue)
2341b84e7d0SEric Biggers {
2352e4fddd8SHerbert Xu return ahash_request_cast(crypto_dequeue_request(queue));
2362e4fddd8SHerbert Xu }
2372e4fddd8SHerbert Xu
crypto_shash_ctx(struct crypto_shash * tfm)2382e4fddd8SHerbert Xu static inline void *crypto_shash_ctx(struct crypto_shash *tfm)
2392e4fddd8SHerbert Xu {
2401b84e7d0SEric Biggers return crypto_tfm_ctx(&tfm->base);
2412e4fddd8SHerbert Xu }
2422e4fddd8SHerbert Xu
shash_crypto_instance(struct shash_instance * inst)243d9e1670bSHerbert Xu static inline struct crypto_instance *shash_crypto_instance(
244d9e1670bSHerbert Xu struct shash_instance *inst)
245d9e1670bSHerbert Xu {
246d9e1670bSHerbert Xu return &inst->s.base;
247d9e1670bSHerbert Xu }
248d9e1670bSHerbert Xu
shash_instance(struct crypto_instance * inst)249ef5d590cSHerbert Xu static inline struct shash_instance *shash_instance(
250ef5d590cSHerbert Xu struct crypto_instance *inst)
251ef5d590cSHerbert Xu {
252ef5d590cSHerbert Xu return container_of(inst, struct shash_instance, s.base);
253ef5d590cSHerbert Xu }
25494296999SHerbert Xu
shash_alg_instance(struct crypto_shash * shash)25594296999SHerbert Xu static inline struct shash_instance *shash_alg_instance(
25694296999SHerbert Xu struct crypto_shash *shash)
25794296999SHerbert Xu {
25894296999SHerbert Xu return shash_instance(crypto_tfm_alg_instance(&shash->base));
25994296999SHerbert Xu }
260cde6263fSHerbert Xu
shash_instance_ctx(struct shash_instance * inst)261cde6263fSHerbert Xu static inline void *shash_instance_ctx(struct shash_instance *inst)
262cde6263fSHerbert Xu {
263cde6263fSHerbert Xu return crypto_instance_ctx(shash_crypto_instance(inst));
264cde6263fSHerbert Xu }
2650390e6aeSHerbert Xu
crypto_spawn_shash(struct crypto_shash_spawn * spawn)2660390e6aeSHerbert Xu static inline struct crypto_shash *crypto_spawn_shash(
267113adefcSHerbert Xu struct crypto_shash_spawn *spawn)
2680390e6aeSHerbert Xu {
2690390e6aeSHerbert Xu return crypto_spawn_tfm2(&spawn->base);
27020036252SHerbert Xu }
27120036252SHerbert Xu
crypto_shash_ctx_aligned(struct crypto_shash * tfm)272 static inline void *crypto_shash_ctx_aligned(struct crypto_shash *tfm)
273 {
274 return crypto_tfm_ctx_aligned(&tfm->base);
275 }
276
__crypto_shash_cast(struct crypto_tfm * tfm)277 static inline struct crypto_shash *__crypto_shash_cast(struct crypto_tfm *tfm)
278 {
279 return container_of(tfm, struct crypto_shash, base);
280 }
281
282 #endif /* _CRYPTO_INTERNAL_HASH_H */
283
284