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 __attribute__ ((aligned(__alignof__(struct hash_alg_common))));
80 unsigned int statesize;
82 struct crypto_alg base;
86 int (*init)(struct ahash_request *req);
87 int (*update)(struct ahash_request *req);
88 int (*final)(struct ahash_request *req);
89 int (*finup)(struct ahash_request *req);
90 int (*digest)(struct ahash_request *req);
91 int (*export)(struct ahash_request *req, void *out);
92 int (*import)(struct ahash_request *req, const void *in);
93 int (*setkey)(struct crypto_ahash *tfm, const u8 *key,
97 struct crypto_tfm base;
100 struct crypto_shash {
101 unsigned int descsize;
102 struct crypto_tfm base;
105 static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
107 return container_of(tfm, struct crypto_ahash, base);
110 struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
113 static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
118 static inline void crypto_free_ahash(struct crypto_ahash *tfm)
120 crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm));
123 static inline unsigned int crypto_ahash_alignmask(
124 struct crypto_ahash *tfm)
126 return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
129 static inline struct hash_alg_common *__crypto_hash_alg_common(
130 struct crypto_alg *alg)
132 return container_of(alg, struct hash_alg_common, base);
135 static inline struct hash_alg_common *crypto_hash_alg_common(
136 struct crypto_ahash *tfm)
138 return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg);
141 static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
143 return crypto_hash_alg_common(tfm)->digestsize;
146 static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm)
148 return crypto_hash_alg_common(tfm)->statesize;
151 static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
153 return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
156 static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
158 crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
161 static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
163 crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
166 static inline struct crypto_ahash *crypto_ahash_reqtfm(
167 struct ahash_request *req)
169 return __crypto_ahash_cast(req->base.tfm);
172 static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
177 static inline void *ahash_request_ctx(struct ahash_request *req)
182 int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
183 unsigned int keylen);
184 int crypto_ahash_finup(struct ahash_request *req);
185 int crypto_ahash_final(struct ahash_request *req);
186 int crypto_ahash_digest(struct ahash_request *req);
188 static inline int crypto_ahash_export(struct ahash_request *req, void *out)
190 return crypto_ahash_reqtfm(req)->export(req, out);
193 static inline int crypto_ahash_import(struct ahash_request *req, const void *in)
195 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
197 if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
200 return tfm->import(req, in);
203 static inline int crypto_ahash_init(struct ahash_request *req)
205 struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
207 if (crypto_ahash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
210 return tfm->init(req);
213 static inline int crypto_ahash_update(struct ahash_request *req)
215 return crypto_ahash_reqtfm(req)->update(req);
218 static inline void ahash_request_set_tfm(struct ahash_request *req,
219 struct crypto_ahash *tfm)
221 req->base.tfm = crypto_ahash_tfm(tfm);
224 static inline struct ahash_request *ahash_request_alloc(
225 struct crypto_ahash *tfm, gfp_t gfp)
227 struct ahash_request *req;
229 req = kmalloc(sizeof(struct ahash_request) +
230 crypto_ahash_reqsize(tfm), gfp);
233 ahash_request_set_tfm(req, tfm);
238 static inline void ahash_request_free(struct ahash_request *req)
243 static inline struct ahash_request *ahash_request_cast(
244 struct crypto_async_request *req)
246 return container_of(req, struct ahash_request, base);
249 static inline void ahash_request_set_callback(struct ahash_request *req,
251 crypto_completion_t complete,
254 req->base.complete = complete;
255 req->base.data = data;
256 req->base.flags = flags;
259 static inline void ahash_request_set_crypt(struct ahash_request *req,
260 struct scatterlist *src, u8 *result,
264 req->nbytes = nbytes;
265 req->result = result;
268 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
271 static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
276 static inline void crypto_free_shash(struct crypto_shash *tfm)
278 crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
281 static inline unsigned int crypto_shash_alignmask(
282 struct crypto_shash *tfm)
284 return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm));
287 static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)
289 return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));
292 static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
294 return container_of(alg, struct shash_alg, base);
297 static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
299 return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);
302 static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
304 return crypto_shash_alg(tfm)->digestsize;
307 static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)
309 return crypto_shash_alg(tfm)->statesize;
312 static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
314 return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
317 static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)
319 crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);
322 static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
324 crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);
327 static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
329 return tfm->descsize;
332 static inline void *shash_desc_ctx(struct shash_desc *desc)
337 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
338 unsigned int keylen);
339 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
340 unsigned int len, u8 *out);
342 static inline int crypto_shash_export(struct shash_desc *desc, void *out)
344 return crypto_shash_alg(desc->tfm)->export(desc, out);
347 static inline int crypto_shash_import(struct shash_desc *desc, const void *in)
349 struct crypto_shash *tfm = desc->tfm;
351 if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
354 return crypto_shash_alg(tfm)->import(desc, in);
357 static inline int crypto_shash_init(struct shash_desc *desc)
359 struct crypto_shash *tfm = desc->tfm;
361 if (crypto_shash_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
364 return crypto_shash_alg(tfm)->init(desc);
367 int crypto_shash_update(struct shash_desc *desc, const u8 *data,
369 int crypto_shash_final(struct shash_desc *desc, u8 *out);
370 int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
371 unsigned int len, u8 *out);
373 #endif /* _CRYPTO_HASH_H */