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>
19 struct crypto_shash *tfm;
22 void *__ctx[] CRYPTO_MINALIGN_ATTR;
26 int (*init)(struct shash_desc *desc);
27 int (*update)(struct shash_desc *desc, const u8 *data,
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 (*export)(struct shash_desc *desc, void *out);
35 int (*import)(struct shash_desc *desc, const void *in);
36 int (*setkey)(struct crypto_shash *tfm, const u8 *key,
39 unsigned int descsize;
40 unsigned int digestsize;
41 unsigned int statesize;
43 struct crypto_alg base;
47 struct crypto_tfm base;
51 struct crypto_tfm base;
54 static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
56 return (struct crypto_ahash *)tfm;
59 static inline struct crypto_ahash *crypto_alloc_ahash(const char *alg_name,
62 type &= ~CRYPTO_ALG_TYPE_MASK;
63 mask &= ~CRYPTO_ALG_TYPE_MASK;
64 type |= CRYPTO_ALG_TYPE_AHASH;
65 mask |= CRYPTO_ALG_TYPE_AHASH_MASK;
67 return __crypto_ahash_cast(crypto_alloc_base(alg_name, type, mask));
70 static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
75 static inline void crypto_free_ahash(struct crypto_ahash *tfm)
77 crypto_free_tfm(crypto_ahash_tfm(tfm));
80 static inline unsigned int crypto_ahash_alignmask(
81 struct crypto_ahash *tfm)
83 return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
86 static inline struct ahash_tfm *crypto_ahash_crt(struct crypto_ahash *tfm)
88 return &crypto_ahash_tfm(tfm)->crt_ahash;
91 static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
93 return crypto_ahash_crt(tfm)->digestsize;
96 static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
98 return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
101 static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
103 crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
106 static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
108 crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
111 static inline struct crypto_ahash *crypto_ahash_reqtfm(
112 struct ahash_request *req)
114 return __crypto_ahash_cast(req->base.tfm);
117 static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
119 return crypto_ahash_crt(tfm)->reqsize;
122 static inline void *ahash_request_ctx(struct ahash_request *req)
127 static inline int crypto_ahash_setkey(struct crypto_ahash *tfm,
128 const u8 *key, unsigned int keylen)
130 struct ahash_tfm *crt = crypto_ahash_crt(tfm);
132 return crt->setkey(tfm, key, keylen);
135 static inline int crypto_ahash_digest(struct ahash_request *req)
137 struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
138 return crt->digest(req);
141 static inline void crypto_ahash_export(struct ahash_request *req, u8 *out)
143 memcpy(out, ahash_request_ctx(req),
144 crypto_ahash_reqsize(crypto_ahash_reqtfm(req)));
147 int crypto_ahash_import(struct ahash_request *req, const u8 *in);
149 static inline int crypto_ahash_init(struct ahash_request *req)
151 struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
152 return crt->init(req);
155 static inline int crypto_ahash_update(struct ahash_request *req)
157 struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
158 return crt->update(req);
161 static inline int crypto_ahash_final(struct ahash_request *req)
163 struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
164 return crt->final(req);
167 static inline void ahash_request_set_tfm(struct ahash_request *req,
168 struct crypto_ahash *tfm)
170 req->base.tfm = crypto_ahash_tfm(tfm);
173 static inline struct ahash_request *ahash_request_alloc(
174 struct crypto_ahash *tfm, gfp_t gfp)
176 struct ahash_request *req;
178 req = kmalloc(sizeof(struct ahash_request) +
179 crypto_ahash_reqsize(tfm), gfp);
182 ahash_request_set_tfm(req, tfm);
187 static inline void ahash_request_free(struct ahash_request *req)
192 static inline struct ahash_request *ahash_request_cast(
193 struct crypto_async_request *req)
195 return container_of(req, struct ahash_request, base);
198 static inline void ahash_request_set_callback(struct ahash_request *req,
200 crypto_completion_t complete,
203 req->base.complete = complete;
204 req->base.data = data;
205 req->base.flags = flags;
208 static inline void ahash_request_set_crypt(struct ahash_request *req,
209 struct scatterlist *src, u8 *result,
213 req->nbytes = nbytes;
214 req->result = result;
217 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
220 static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
225 static inline void crypto_free_shash(struct crypto_shash *tfm)
227 crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
230 static inline unsigned int crypto_shash_alignmask(
231 struct crypto_shash *tfm)
233 return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm));
236 static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)
238 return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));
241 static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
243 return container_of(alg, struct shash_alg, base);
246 static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
248 return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);
251 static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
253 return crypto_shash_alg(tfm)->digestsize;
256 static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)
258 return crypto_shash_alg(tfm)->statesize;
261 static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
263 return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
266 static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)
268 crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);
271 static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
273 crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);
276 static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
278 return crypto_shash_alg(tfm)->descsize;
281 static inline void *shash_desc_ctx(struct shash_desc *desc)
286 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
287 unsigned int keylen);
288 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
289 unsigned int len, u8 *out);
291 static inline int crypto_shash_export(struct shash_desc *desc, void *out)
293 return crypto_shash_alg(desc->tfm)->export(desc, out);
296 static inline int crypto_shash_import(struct shash_desc *desc, const void *in)
298 return crypto_shash_alg(desc->tfm)->import(desc, in);
301 static inline int crypto_shash_init(struct shash_desc *desc)
303 return crypto_shash_alg(desc->tfm)->init(desc);
306 int crypto_shash_update(struct shash_desc *desc, const u8 *data,
308 int crypto_shash_final(struct shash_desc *desc, u8 *out);
309 int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
310 unsigned int len, u8 *out);
312 #endif /* _CRYPTO_HASH_H */