]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - include/crypto/hash.h
crypto: shash - Make descsize a run-time attribute
[mirror_ubuntu-bionic-kernel.git] / include / crypto / hash.h
1 /*
2 * Hash: Hash algorithms under the crypto API
3 *
4 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 */
12
13 #ifndef _CRYPTO_HASH_H
14 #define _CRYPTO_HASH_H
15
16 #include <linux/crypto.h>
17
18 struct shash_desc {
19 struct crypto_shash *tfm;
20 u32 flags;
21
22 void *__ctx[] CRYPTO_MINALIGN_ATTR;
23 };
24
25 struct shash_alg {
26 int (*init)(struct shash_desc *desc);
27 int (*update)(struct shash_desc *desc, const u8 *data,
28 unsigned int len);
29 int (*final)(struct shash_desc *desc, u8 *out);
30 int (*finup)(struct shash_desc *desc, const u8 *data,
31 unsigned int len, u8 *out);
32 int (*digest)(struct shash_desc *desc, const u8 *data,
33 unsigned int len, u8 *out);
34 int (*export)(struct shash_desc *desc, void *out);
35 int (*import)(struct shash_desc *desc, const void *in);
36 int (*setkey)(struct crypto_shash *tfm, const u8 *key,
37 unsigned int keylen);
38
39 unsigned int descsize;
40 unsigned int digestsize;
41 unsigned int statesize;
42
43 struct crypto_alg base;
44 };
45
46 struct crypto_ahash {
47 struct crypto_tfm base;
48 };
49
50 struct crypto_shash {
51 unsigned int descsize;
52 struct crypto_tfm base;
53 };
54
55 static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
56 {
57 return (struct crypto_ahash *)tfm;
58 }
59
60 static inline struct crypto_ahash *crypto_alloc_ahash(const char *alg_name,
61 u32 type, u32 mask)
62 {
63 type &= ~CRYPTO_ALG_TYPE_MASK;
64 mask &= ~CRYPTO_ALG_TYPE_MASK;
65 type |= CRYPTO_ALG_TYPE_AHASH;
66 mask |= CRYPTO_ALG_TYPE_AHASH_MASK;
67
68 return __crypto_ahash_cast(crypto_alloc_base(alg_name, type, mask));
69 }
70
71 static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
72 {
73 return &tfm->base;
74 }
75
76 static inline void crypto_free_ahash(struct crypto_ahash *tfm)
77 {
78 crypto_free_tfm(crypto_ahash_tfm(tfm));
79 }
80
81 static inline unsigned int crypto_ahash_alignmask(
82 struct crypto_ahash *tfm)
83 {
84 return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
85 }
86
87 static inline struct ahash_tfm *crypto_ahash_crt(struct crypto_ahash *tfm)
88 {
89 return &crypto_ahash_tfm(tfm)->crt_ahash;
90 }
91
92 static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
93 {
94 return crypto_ahash_crt(tfm)->digestsize;
95 }
96
97 static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
98 {
99 return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
100 }
101
102 static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
103 {
104 crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
105 }
106
107 static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
108 {
109 crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
110 }
111
112 static inline struct crypto_ahash *crypto_ahash_reqtfm(
113 struct ahash_request *req)
114 {
115 return __crypto_ahash_cast(req->base.tfm);
116 }
117
118 static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
119 {
120 return crypto_ahash_crt(tfm)->reqsize;
121 }
122
123 static inline void *ahash_request_ctx(struct ahash_request *req)
124 {
125 return req->__ctx;
126 }
127
128 static inline int crypto_ahash_setkey(struct crypto_ahash *tfm,
129 const u8 *key, unsigned int keylen)
130 {
131 struct ahash_tfm *crt = crypto_ahash_crt(tfm);
132
133 return crt->setkey(tfm, key, keylen);
134 }
135
136 static inline int crypto_ahash_digest(struct ahash_request *req)
137 {
138 struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
139 return crt->digest(req);
140 }
141
142 static inline void crypto_ahash_export(struct ahash_request *req, u8 *out)
143 {
144 memcpy(out, ahash_request_ctx(req),
145 crypto_ahash_reqsize(crypto_ahash_reqtfm(req)));
146 }
147
148 int crypto_ahash_import(struct ahash_request *req, const u8 *in);
149
150 static inline int crypto_ahash_init(struct ahash_request *req)
151 {
152 struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
153 return crt->init(req);
154 }
155
156 static inline int crypto_ahash_update(struct ahash_request *req)
157 {
158 struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
159 return crt->update(req);
160 }
161
162 static inline int crypto_ahash_final(struct ahash_request *req)
163 {
164 struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
165 return crt->final(req);
166 }
167
168 static inline void ahash_request_set_tfm(struct ahash_request *req,
169 struct crypto_ahash *tfm)
170 {
171 req->base.tfm = crypto_ahash_tfm(tfm);
172 }
173
174 static inline struct ahash_request *ahash_request_alloc(
175 struct crypto_ahash *tfm, gfp_t gfp)
176 {
177 struct ahash_request *req;
178
179 req = kmalloc(sizeof(struct ahash_request) +
180 crypto_ahash_reqsize(tfm), gfp);
181
182 if (likely(req))
183 ahash_request_set_tfm(req, tfm);
184
185 return req;
186 }
187
188 static inline void ahash_request_free(struct ahash_request *req)
189 {
190 kzfree(req);
191 }
192
193 static inline struct ahash_request *ahash_request_cast(
194 struct crypto_async_request *req)
195 {
196 return container_of(req, struct ahash_request, base);
197 }
198
199 static inline void ahash_request_set_callback(struct ahash_request *req,
200 u32 flags,
201 crypto_completion_t complete,
202 void *data)
203 {
204 req->base.complete = complete;
205 req->base.data = data;
206 req->base.flags = flags;
207 }
208
209 static inline void ahash_request_set_crypt(struct ahash_request *req,
210 struct scatterlist *src, u8 *result,
211 unsigned int nbytes)
212 {
213 req->src = src;
214 req->nbytes = nbytes;
215 req->result = result;
216 }
217
218 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
219 u32 mask);
220
221 static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
222 {
223 return &tfm->base;
224 }
225
226 static inline void crypto_free_shash(struct crypto_shash *tfm)
227 {
228 crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
229 }
230
231 static inline unsigned int crypto_shash_alignmask(
232 struct crypto_shash *tfm)
233 {
234 return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm));
235 }
236
237 static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)
238 {
239 return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));
240 }
241
242 static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
243 {
244 return container_of(alg, struct shash_alg, base);
245 }
246
247 static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
248 {
249 return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);
250 }
251
252 static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
253 {
254 return crypto_shash_alg(tfm)->digestsize;
255 }
256
257 static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)
258 {
259 return crypto_shash_alg(tfm)->statesize;
260 }
261
262 static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
263 {
264 return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
265 }
266
267 static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)
268 {
269 crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);
270 }
271
272 static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
273 {
274 crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);
275 }
276
277 static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
278 {
279 return tfm->descsize;
280 }
281
282 static inline void *shash_desc_ctx(struct shash_desc *desc)
283 {
284 return desc->__ctx;
285 }
286
287 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
288 unsigned int keylen);
289 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
290 unsigned int len, u8 *out);
291
292 static inline int crypto_shash_export(struct shash_desc *desc, void *out)
293 {
294 return crypto_shash_alg(desc->tfm)->export(desc, out);
295 }
296
297 static inline int crypto_shash_import(struct shash_desc *desc, const void *in)
298 {
299 return crypto_shash_alg(desc->tfm)->import(desc, in);
300 }
301
302 static inline int crypto_shash_init(struct shash_desc *desc)
303 {
304 return crypto_shash_alg(desc->tfm)->init(desc);
305 }
306
307 int crypto_shash_update(struct shash_desc *desc, const u8 *data,
308 unsigned int len);
309 int crypto_shash_final(struct shash_desc *desc, u8 *out);
310 int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
311 unsigned int len, u8 *out);
312
313 #endif /* _CRYPTO_HASH_H */