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 (*reinit)(struct shash_desc *desc);
28 int (*update)(struct shash_desc *desc, const u8 *data,
30 int (*final)(struct shash_desc *desc, u8 *out);
31 int (*finup)(struct shash_desc *desc, const u8 *data,
32 unsigned int len, u8 *out);
33 int (*digest)(struct shash_desc *desc, const u8 *data,
34 unsigned int len, u8 *out);
35 int (*setkey)(struct crypto_shash *tfm, const u8 *key,
38 unsigned int descsize;
39 unsigned int digestsize;
41 struct crypto_alg base;
45 struct crypto_tfm base;
49 struct crypto_tfm base;
52 static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
54 return (struct crypto_ahash *)tfm;
57 static inline struct crypto_ahash *crypto_alloc_ahash(const char *alg_name,
60 type &= ~CRYPTO_ALG_TYPE_MASK;
61 mask &= ~CRYPTO_ALG_TYPE_MASK;
62 type |= CRYPTO_ALG_TYPE_AHASH;
63 mask |= CRYPTO_ALG_TYPE_AHASH_MASK;
65 return __crypto_ahash_cast(crypto_alloc_base(alg_name, type, mask));
68 static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
73 static inline void crypto_free_ahash(struct crypto_ahash *tfm)
75 crypto_free_tfm(crypto_ahash_tfm(tfm));
78 static inline unsigned int crypto_ahash_alignmask(
79 struct crypto_ahash *tfm)
81 return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
84 static inline struct ahash_tfm *crypto_ahash_crt(struct crypto_ahash *tfm)
86 return &crypto_ahash_tfm(tfm)->crt_ahash;
89 static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
91 return crypto_ahash_crt(tfm)->digestsize;
94 static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
96 return crypto_tfm_get_flags(crypto_ahash_tfm(tfm));
99 static inline void crypto_ahash_set_flags(struct crypto_ahash *tfm, u32 flags)
101 crypto_tfm_set_flags(crypto_ahash_tfm(tfm), flags);
104 static inline void crypto_ahash_clear_flags(struct crypto_ahash *tfm, u32 flags)
106 crypto_tfm_clear_flags(crypto_ahash_tfm(tfm), flags);
109 static inline struct crypto_ahash *crypto_ahash_reqtfm(
110 struct ahash_request *req)
112 return __crypto_ahash_cast(req->base.tfm);
115 static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
117 return crypto_ahash_crt(tfm)->reqsize;
120 static inline void *ahash_request_ctx(struct ahash_request *req)
125 static inline int crypto_ahash_setkey(struct crypto_ahash *tfm,
126 const u8 *key, unsigned int keylen)
128 struct ahash_tfm *crt = crypto_ahash_crt(tfm);
130 return crt->setkey(tfm, key, keylen);
133 static inline int crypto_ahash_digest(struct ahash_request *req)
135 struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
136 return crt->digest(req);
139 static inline void crypto_ahash_export(struct ahash_request *req, u8 *out)
141 memcpy(out, ahash_request_ctx(req),
142 crypto_ahash_reqsize(crypto_ahash_reqtfm(req)));
145 int crypto_ahash_import(struct ahash_request *req, const u8 *in);
147 static inline int crypto_ahash_init(struct ahash_request *req)
149 struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
150 return crt->init(req);
153 static inline int crypto_ahash_update(struct ahash_request *req)
155 struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
156 return crt->update(req);
159 static inline int crypto_ahash_final(struct ahash_request *req)
161 struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
162 return crt->final(req);
165 static inline void ahash_request_set_tfm(struct ahash_request *req,
166 struct crypto_ahash *tfm)
168 req->base.tfm = crypto_ahash_tfm(tfm);
171 static inline struct ahash_request *ahash_request_alloc(
172 struct crypto_ahash *tfm, gfp_t gfp)
174 struct ahash_request *req;
176 req = kmalloc(sizeof(struct ahash_request) +
177 crypto_ahash_reqsize(tfm), gfp);
180 ahash_request_set_tfm(req, tfm);
185 static inline void ahash_request_free(struct ahash_request *req)
190 static inline struct ahash_request *ahash_request_cast(
191 struct crypto_async_request *req)
193 return container_of(req, struct ahash_request, base);
196 static inline void ahash_request_set_callback(struct ahash_request *req,
198 crypto_completion_t complete,
201 req->base.complete = complete;
202 req->base.data = data;
203 req->base.flags = flags;
206 static inline void ahash_request_set_crypt(struct ahash_request *req,
207 struct scatterlist *src, u8 *result,
211 req->nbytes = nbytes;
212 req->result = result;
215 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
218 static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
223 static inline void crypto_free_shash(struct crypto_shash *tfm)
225 crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
228 static inline unsigned int crypto_shash_alignmask(
229 struct crypto_shash *tfm)
231 return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm));
234 static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)
236 return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));
239 static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
241 return container_of(alg, struct shash_alg, base);
244 static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
246 return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);
249 static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
251 return crypto_shash_alg(tfm)->digestsize;
254 static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
256 return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
259 static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)
261 crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);
264 static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
266 crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);
269 static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
271 return crypto_shash_alg(tfm)->descsize;
274 static inline void *shash_desc_ctx(struct shash_desc *desc)
279 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
280 unsigned int keylen);
281 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
282 unsigned int len, u8 *out);
284 static inline void crypto_shash_export(struct shash_desc *desc, u8 *out)
286 memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(desc->tfm));
289 int crypto_shash_import(struct shash_desc *desc, const u8 *in);
291 static inline int crypto_shash_init(struct shash_desc *desc)
293 return crypto_shash_alg(desc->tfm)->init(desc);
296 int crypto_shash_update(struct shash_desc *desc, const u8 *data,
298 int crypto_shash_final(struct shash_desc *desc, u8 *out);
299 int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
300 unsigned int len, u8 *out);
302 #endif /* _CRYPTO_HASH_H */