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