Commit | Line | Data |
---|---|---|
18e33e6d HX |
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 | ||
7b5a080b HX |
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 (*setkey)(struct crypto_shash *tfm, const u8 *key, | |
35 | unsigned int keylen); | |
36 | ||
37 | unsigned int descsize; | |
38 | unsigned int digestsize; | |
39 | ||
40 | struct crypto_alg base; | |
41 | }; | |
42 | ||
18e33e6d HX |
43 | struct crypto_ahash { |
44 | struct crypto_tfm base; | |
45 | }; | |
46 | ||
7b5a080b HX |
47 | struct crypto_shash { |
48 | struct crypto_tfm base; | |
49 | }; | |
50 | ||
18e33e6d HX |
51 | static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm) |
52 | { | |
53 | return (struct crypto_ahash *)tfm; | |
54 | } | |
55 | ||
56 | static inline struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, | |
57 | u32 type, u32 mask) | |
58 | { | |
59 | type &= ~CRYPTO_ALG_TYPE_MASK; | |
60 | mask &= ~CRYPTO_ALG_TYPE_MASK; | |
61 | type |= CRYPTO_ALG_TYPE_AHASH; | |
62 | mask |= CRYPTO_ALG_TYPE_AHASH_MASK; | |
63 | ||
64 | return __crypto_ahash_cast(crypto_alloc_base(alg_name, type, mask)); | |
65 | } | |
66 | ||
67 | static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm) | |
68 | { | |
69 | return &tfm->base; | |
70 | } | |
71 | ||
72 | static inline void crypto_free_ahash(struct crypto_ahash *tfm) | |
73 | { | |
74 | crypto_free_tfm(crypto_ahash_tfm(tfm)); | |
75 | } | |
76 | ||
77 | static inline unsigned int crypto_ahash_alignmask( | |
78 | struct crypto_ahash *tfm) | |
79 | { | |
80 | return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm)); | |
81 | } | |
82 | ||
83 | static inline struct ahash_tfm *crypto_ahash_crt(struct crypto_ahash *tfm) | |
84 | { | |
85 | return &crypto_ahash_tfm(tfm)->crt_ahash; | |
86 | } | |
87 | ||
88 | static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm) | |
89 | { | |
90 | return crypto_ahash_crt(tfm)->digestsize; | |
91 | } | |
92 | ||
93 | static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm) | |
94 | { | |
95 | return crypto_tfm_get_flags(crypto_ahash_tfm(tfm)); | |
96 | } | |
97 | ||
98 | static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags) | |
99 | { | |
100 | crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags); | |
101 | } | |
102 | ||
103 | static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags) | |
104 | { | |
105 | crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags); | |
106 | } | |
107 | ||
108 | static inline struct crypto_ahash *crypto_ahash_reqtfm( | |
109 | struct ahash_request *req) | |
110 | { | |
111 | return __crypto_ahash_cast(req->base.tfm); | |
112 | } | |
113 | ||
114 | static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm) | |
115 | { | |
116 | return crypto_ahash_crt(tfm)->reqsize; | |
117 | } | |
118 | ||
119 | static inline int crypto_ahash_setkey(struct crypto_ahash *tfm, | |
120 | const u8 *key, unsigned int keylen) | |
121 | { | |
122 | struct ahash_tfm *crt = crypto_ahash_crt(tfm); | |
123 | ||
124 | return crt->setkey(tfm, key, keylen); | |
125 | } | |
126 | ||
127 | static inline int crypto_ahash_digest(struct ahash_request *req) | |
128 | { | |
129 | struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req)); | |
130 | return crt->digest(req); | |
131 | } | |
132 | ||
318e5313 HX |
133 | static inline int crypto_ahash_init(struct ahash_request *req) |
134 | { | |
135 | struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req)); | |
136 | return crt->init(req); | |
137 | } | |
138 | ||
139 | static inline int crypto_ahash_update(struct ahash_request *req) | |
140 | { | |
141 | struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req)); | |
142 | return crt->update(req); | |
143 | } | |
144 | ||
145 | static inline int crypto_ahash_final(struct ahash_request *req) | |
146 | { | |
147 | struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req)); | |
148 | return crt->final(req); | |
149 | } | |
150 | ||
18e33e6d HX |
151 | static inline void ahash_request_set_tfm(struct ahash_request *req, |
152 | struct crypto_ahash *tfm) | |
153 | { | |
154 | req->base.tfm = crypto_ahash_tfm(tfm); | |
155 | } | |
156 | ||
157 | static inline struct ahash_request *ahash_request_alloc( | |
158 | struct crypto_ahash *tfm, gfp_t gfp) | |
159 | { | |
160 | struct ahash_request *req; | |
161 | ||
162 | req = kmalloc(sizeof(struct ahash_request) + | |
163 | crypto_ahash_reqsize(tfm), gfp); | |
164 | ||
165 | if (likely(req)) | |
166 | ahash_request_set_tfm(req, tfm); | |
167 | ||
168 | return req; | |
169 | } | |
170 | ||
171 | static inline void ahash_request_free(struct ahash_request *req) | |
172 | { | |
173 | kfree(req); | |
174 | } | |
175 | ||
176 | static inline struct ahash_request *ahash_request_cast( | |
177 | struct crypto_async_request *req) | |
178 | { | |
179 | return container_of(req, struct ahash_request, base); | |
180 | } | |
181 | ||
182 | static inline void ahash_request_set_callback(struct ahash_request *req, | |
183 | u32 flags, | |
184 | crypto_completion_t complete, | |
185 | void *data) | |
186 | { | |
187 | req->base.complete = complete; | |
188 | req->base.data = data; | |
189 | req->base.flags = flags; | |
190 | } | |
191 | ||
192 | static inline void ahash_request_set_crypt(struct ahash_request *req, | |
193 | struct scatterlist *src, u8 *result, | |
194 | unsigned int nbytes) | |
195 | { | |
196 | req->src = src; | |
197 | req->nbytes = nbytes; | |
198 | req->result = result; | |
199 | } | |
200 | ||
7b5a080b HX |
201 | struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type, |
202 | u32 mask); | |
203 | ||
204 | static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm) | |
205 | { | |
206 | return &tfm->base; | |
207 | } | |
208 | ||
209 | static inline void crypto_free_shash(struct crypto_shash *tfm) | |
210 | { | |
211 | crypto_free_tfm(crypto_shash_tfm(tfm)); | |
212 | } | |
213 | ||
214 | static inline unsigned int crypto_shash_alignmask( | |
215 | struct crypto_shash *tfm) | |
216 | { | |
217 | return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm)); | |
218 | } | |
219 | ||
220 | static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg) | |
221 | { | |
222 | return container_of(alg, struct shash_alg, base); | |
223 | } | |
224 | ||
225 | static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm) | |
226 | { | |
227 | return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg); | |
228 | } | |
229 | ||
230 | static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm) | |
231 | { | |
232 | return crypto_shash_alg(tfm)->digestsize; | |
233 | } | |
234 | ||
235 | static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm) | |
236 | { | |
237 | return crypto_tfm_get_flags(crypto_shash_tfm(tfm)); | |
238 | } | |
239 | ||
240 | static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags) | |
241 | { | |
242 | crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags); | |
243 | } | |
244 | ||
245 | static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags) | |
246 | { | |
247 | crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags); | |
248 | } | |
249 | ||
250 | static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm) | |
251 | { | |
252 | return crypto_shash_alg(tfm)->descsize; | |
253 | } | |
254 | ||
255 | static inline void *shash_desc_ctx(struct shash_desc *desc) | |
256 | { | |
257 | return desc->__ctx; | |
258 | } | |
259 | ||
260 | int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key, | |
261 | unsigned int keylen); | |
262 | int crypto_shash_digest(struct shash_desc *desc, const u8 *data, | |
263 | unsigned int len, u8 *out); | |
264 | ||
265 | static inline int crypto_shash_init(struct shash_desc *desc) | |
266 | { | |
267 | return crypto_shash_alg(desc->tfm)->init(desc); | |
268 | } | |
269 | ||
270 | int crypto_shash_update(struct shash_desc *desc, const u8 *data, | |
271 | unsigned int len); | |
272 | int crypto_shash_final(struct shash_desc *desc, u8 *out); | |
273 | int crypto_shash_finup(struct shash_desc *desc, const u8 *data, | |
274 | unsigned int len, u8 *out); | |
275 | ||
18e33e6d | 276 | #endif /* _CRYPTO_HASH_H */ |