]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - include/crypto/hash.h
3e89ce16b59ca94c7e453ba490fd463152ce063b
[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 crypto_ahash;
19
20 struct hash_alg_common {
21 unsigned int digestsize;
22 unsigned int statesize;
23
24 struct crypto_alg base;
25 };
26
27 struct ahash_request {
28 struct crypto_async_request base;
29
30 unsigned int nbytes;
31 struct scatterlist *src;
32 u8 *result;
33
34 /* This field may only be used by the ahash API code. */
35 void *priv;
36
37 void *__ctx[] CRYPTO_MINALIGN_ATTR;
38 };
39
40 struct ahash_alg {
41 int (*init)(struct ahash_request *req);
42 int (*update)(struct ahash_request *req);
43 int (*final)(struct ahash_request *req);
44 int (*finup)(struct ahash_request *req);
45 int (*digest)(struct ahash_request *req);
46 int (*export)(struct ahash_request *req, void *out);
47 int (*import)(struct ahash_request *req, const void *in);
48 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
49 unsigned int keylen);
50
51 struct hash_alg_common halg;
52 };
53
54 struct shash_desc {
55 struct crypto_shash *tfm;
56 u32 flags;
57
58 void *__ctx[] CRYPTO_MINALIGN_ATTR;
59 };
60
61 struct shash_alg {
62 int (*init)(struct shash_desc *desc);
63 int (*update)(struct shash_desc *desc, const u8 *data,
64 unsigned int len);
65 int (*final)(struct shash_desc *desc, u8 *out);
66 int (*finup)(struct shash_desc *desc, const u8 *data,
67 unsigned int len, u8 *out);
68 int (*digest)(struct shash_desc *desc, const u8 *data,
69 unsigned int len, u8 *out);
70 int (*export)(struct shash_desc *desc, void *out);
71 int (*import)(struct shash_desc *desc, const void *in);
72 int (*setkey)(struct crypto_shash *tfm, const u8 *key,
73 unsigned int keylen);
74
75 unsigned int descsize;
76
77 /* These fields must match hash_alg_common. */
78 unsigned int digestsize;
79 unsigned int statesize;
80
81 struct crypto_alg base;
82 };
83
84 struct crypto_ahash {
85 int (*init)(struct ahash_request *req);
86 int (*update)(struct ahash_request *req);
87 int (*final)(struct ahash_request *req);
88 int (*finup)(struct ahash_request *req);
89 int (*digest)(struct ahash_request *req);
90 int (*export)(struct ahash_request *req, void *out);
91 int (*import)(struct ahash_request *req, const void *in);
92 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
93 unsigned int keylen);
94
95 unsigned int reqsize;
96 struct crypto_tfm base;
97 };
98
99 struct crypto_shash {
100 unsigned int descsize;
101 struct crypto_tfm base;
102 };
103
104 static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
105 {
106 return container_of(tfm, struct crypto_ahash, base);
107 }
108
109 struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
110 u32 mask);
111
112 static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
113 {
114 return &tfm->base;
115 }
116
117 static inline void crypto_free_ahash(struct crypto_ahash *tfm)
118 {
119 crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm));
120 }
121
122 static inline unsigned int crypto_ahash_alignmask(
123 struct crypto_ahash *tfm)
124 {
125 return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
126 }
127
128 static inline struct hash_alg_common *__crypto_hash_alg_common(
129 struct crypto_alg *alg)
130 {
131 return container_of(alg, struct hash_alg_common, base);
132 }
133
134 static inline struct hash_alg_common *crypto_hash_alg_common(
135 struct crypto_ahash *tfm)
136 {
137 return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg);
138 }
139
140 static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
141 {
142 return crypto_hash_alg_common(tfm)->digestsize;
143 }
144
145 static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm)
146 {
147 return crypto_hash_alg_common(tfm)->statesize;
148 }
149
150 static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
151 {
152 return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
153 }
154
155 static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
156 {
157 crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
158 }
159
160 static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
161 {
162 crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
163 }
164
165 static inline struct crypto_ahash *crypto_ahash_reqtfm(
166 struct ahash_request *req)
167 {
168 return __crypto_ahash_cast(req->base.tfm);
169 }
170
171 static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
172 {
173 return tfm->reqsize;
174 }
175
176 static inline void *ahash_request_ctx(struct ahash_request *req)
177 {
178 return req->__ctx;
179 }
180
181 int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
182 unsigned int keylen);
183 int crypto_ahash_finup(struct ahash_request *req);
184 int crypto_ahash_final(struct ahash_request *req);
185 int crypto_ahash_digest(struct ahash_request *req);
186
187 static inline int crypto_ahash_export(struct ahash_request *req, void *out)
188 {
189 return crypto_ahash_reqtfm(req)->export(req, out);
190 }
191
192 static inline int crypto_ahash_import(struct ahash_request *req, const void *in)
193 {
194 return crypto_ahash_reqtfm(req)->import(req, in);
195 }
196
197 static inline int crypto_ahash_init(struct ahash_request *req)
198 {
199 return crypto_ahash_reqtfm(req)->init(req);
200 }
201
202 static inline int crypto_ahash_update(struct ahash_request *req)
203 {
204 return crypto_ahash_reqtfm(req)->update(req);
205 }
206
207 static inline void ahash_request_set_tfm(struct ahash_request *req,
208 struct crypto_ahash *tfm)
209 {
210 req->base.tfm = crypto_ahash_tfm(tfm);
211 }
212
213 static inline struct ahash_request *ahash_request_alloc(
214 struct crypto_ahash *tfm, gfp_t gfp)
215 {
216 struct ahash_request *req;
217
218 req = kmalloc(sizeof(struct ahash_request) +
219 crypto_ahash_reqsize(tfm), gfp);
220
221 if (likely(req))
222 ahash_request_set_tfm(req, tfm);
223
224 return req;
225 }
226
227 static inline void ahash_request_free(struct ahash_request *req)
228 {
229 kzfree(req);
230 }
231
232 static inline struct ahash_request *ahash_request_cast(
233 struct crypto_async_request *req)
234 {
235 return container_of(req, struct ahash_request, base);
236 }
237
238 static inline void ahash_request_set_callback(struct ahash_request *req,
239 u32 flags,
240 crypto_completion_t complete,
241 void *data)
242 {
243 req->base.complete = complete;
244 req->base.data = data;
245 req->base.flags = flags;
246 }
247
248 static inline void ahash_request_set_crypt(struct ahash_request *req,
249 struct scatterlist *src, u8 *result,
250 unsigned int nbytes)
251 {
252 req->src = src;
253 req->nbytes = nbytes;
254 req->result = result;
255 }
256
257 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
258 u32 mask);
259
260 static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
261 {
262 return &tfm->base;
263 }
264
265 static inline void crypto_free_shash(struct crypto_shash *tfm)
266 {
267 crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
268 }
269
270 static inline unsigned int crypto_shash_alignmask(
271 struct crypto_shash *tfm)
272 {
273 return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm));
274 }
275
276 static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)
277 {
278 return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));
279 }
280
281 static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
282 {
283 return container_of(alg, struct shash_alg, base);
284 }
285
286 static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
287 {
288 return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);
289 }
290
291 static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
292 {
293 return crypto_shash_alg(tfm)->digestsize;
294 }
295
296 static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)
297 {
298 return crypto_shash_alg(tfm)->statesize;
299 }
300
301 static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
302 {
303 return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
304 }
305
306 static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)
307 {
308 crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);
309 }
310
311 static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
312 {
313 crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);
314 }
315
316 static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
317 {
318 return tfm->descsize;
319 }
320
321 static inline void *shash_desc_ctx(struct shash_desc *desc)
322 {
323 return desc->__ctx;
324 }
325
326 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
327 unsigned int keylen);
328 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
329 unsigned int len, u8 *out);
330
331 static inline int crypto_shash_export(struct shash_desc *desc, void *out)
332 {
333 return crypto_shash_alg(desc->tfm)->export(desc, out);
334 }
335
336 static inline int crypto_shash_import(struct shash_desc *desc, const void *in)
337 {
338 return crypto_shash_alg(desc->tfm)->import(desc, in);
339 }
340
341 static inline int crypto_shash_init(struct shash_desc *desc)
342 {
343 return crypto_shash_alg(desc->tfm)->init(desc);
344 }
345
346 int crypto_shash_update(struct shash_desc *desc, const u8 *data,
347 unsigned int len);
348 int crypto_shash_final(struct shash_desc *desc, u8 *out);
349 int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
350 unsigned int len, u8 *out);
351
352 #endif /* _CRYPTO_HASH_H */