2 * Hash: Hash algorithms under the crypto API
4 * Copyright (c) 2008 Herbert Xu <herbert@gondor.apana.org.au>
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)
13 #ifndef _CRYPTO_HASH_H
14 #define _CRYPTO_HASH_H
16 #include <linux/crypto.h>
20 struct hash_alg_common {
21 unsigned int digestsize;
22 unsigned int statesize;
24 struct crypto_alg base;
27 struct ahash_request {
28 struct crypto_async_request base;
31 struct scatterlist *src;
34 /* This field may only be used by the ahash API code. */
37 void *__ctx[] CRYPTO_MINALIGN_ATTR;
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,
51 struct hash_alg_common halg;
55 struct crypto_shash *tfm;
58 void *__ctx[] CRYPTO_MINALIGN_ATTR;
62 int (*init)(struct shash_desc *desc);
63 int (*update)(struct shash_desc *desc, const u8 *data,
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,
75 unsigned int descsize;
77 /* These fields must match hash_alg_common. */
78 unsigned int digestsize;
79 unsigned int statesize;
81 struct crypto_alg base;
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,
96 struct crypto_tfm base;
100 unsigned int descsize;
101 struct crypto_tfm base;
104 static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
106 return container_of(tfm, struct crypto_ahash, base);
109 struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
112 static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
117 static inline void crypto_free_ahash(struct crypto_ahash *tfm)
119 crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm));
122 static inline unsigned int crypto_ahash_alignmask(
123 struct crypto_ahash *tfm)
125 return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
128 static inline struct hash_alg_common *__crypto_hash_alg_common(
129 struct crypto_alg *alg)
131 return container_of(alg, struct hash_alg_common, base);
134 static inline struct hash_alg_common *crypto_hash_alg_common(
135 struct crypto_ahash *tfm)
137 return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg);
140 static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
142 return crypto_hash_alg_common(tfm)->digestsize;
145 static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm)
147 return crypto_hash_alg_common(tfm)->statesize;
150 static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
152 return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
155 static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
157 crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
160 static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
162 crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
165 static inline struct crypto_ahash *crypto_ahash_reqtfm(
166 struct ahash_request *req)
168 return __crypto_ahash_cast(req->base.tfm);
171 static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
176 static inline void *ahash_request_ctx(struct ahash_request *req)
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);
187 static inline int crypto_ahash_export(struct ahash_request *req, void *out)
189 return crypto_ahash_reqtfm(req)->export(req, out);
192 static inline int crypto_ahash_import(struct ahash_request *req, const void *in)
194 return crypto_ahash_reqtfm(req)->import(req, in);
197 static inline int crypto_ahash_init(struct ahash_request *req)
199 return crypto_ahash_reqtfm(req)->init(req);
202 static inline int crypto_ahash_update(struct ahash_request *req)
204 return crypto_ahash_reqtfm(req)->update(req);
207 static inline void ahash_request_set_tfm(struct ahash_request *req,
208 struct crypto_ahash *tfm)
210 req->base.tfm = crypto_ahash_tfm(tfm);
213 static inline struct ahash_request *ahash_request_alloc(
214 struct crypto_ahash *tfm, gfp_t gfp)
216 struct ahash_request *req;
218 req = kmalloc(sizeof(struct ahash_request) +
219 crypto_ahash_reqsize(tfm), gfp);
222 ahash_request_set_tfm(req, tfm);
227 static inline void ahash_request_free(struct ahash_request *req)
232 static inline struct ahash_request *ahash_request_cast(
233 struct crypto_async_request *req)
235 return container_of(req, struct ahash_request, base);
238 static inline void ahash_request_set_callback(struct ahash_request *req,
240 crypto_completion_t complete,
243 req->base.complete = complete;
244 req->base.data = data;
245 req->base.flags = flags;
248 static inline void ahash_request_set_crypt(struct ahash_request *req,
249 struct scatterlist *src, u8 *result,
253 req->nbytes = nbytes;
254 req->result = result;
257 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
260 static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
265 static inline void crypto_free_shash(struct crypto_shash *tfm)
267 crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
270 static inline unsigned int crypto_shash_alignmask(
271 struct crypto_shash *tfm)
273 return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm));
276 static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)
278 return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));
281 static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
283 return container_of(alg, struct shash_alg, base);
286 static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
288 return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);
291 static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
293 return crypto_shash_alg(tfm)->digestsize;
296 static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)
298 return crypto_shash_alg(tfm)->statesize;
301 static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
303 return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
306 static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)
308 crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);
311 static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
313 crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);
316 static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
318 return tfm->descsize;
321 static inline void *shash_desc_ctx(struct shash_desc *desc)
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);
331 static inline int crypto_shash_export(struct shash_desc *desc, void *out)
333 return crypto_shash_alg(desc->tfm)->export(desc, out);
336 static inline int crypto_shash_import(struct shash_desc *desc, const void *in)
338 return crypto_shash_alg(desc->tfm)->import(desc, in);
341 static inline int crypto_shash_init(struct shash_desc *desc)
343 return crypto_shash_alg(desc->tfm)->init(desc);
346 int crypto_shash_update(struct shash_desc *desc, const u8 *data,
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);
352 #endif /* _CRYPTO_HASH_H */