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
;
61 #define SHASH_DESC_ON_STACK(shash, ctx) \
62 char __##shash##_desc[sizeof(struct shash_desc) + \
63 crypto_shash_descsize(ctx)] CRYPTO_MINALIGN_ATTR; \
64 struct shash_desc *shash = (struct shash_desc *)__##shash##_desc
67 int (*init
)(struct shash_desc
*desc
);
68 int (*update
)(struct shash_desc
*desc
, const u8
*data
,
70 int (*final
)(struct shash_desc
*desc
, u8
*out
);
71 int (*finup
)(struct shash_desc
*desc
, const u8
*data
,
72 unsigned int len
, u8
*out
);
73 int (*digest
)(struct shash_desc
*desc
, const u8
*data
,
74 unsigned int len
, u8
*out
);
75 int (*export
)(struct shash_desc
*desc
, void *out
);
76 int (*import
)(struct shash_desc
*desc
, const void *in
);
77 int (*setkey
)(struct crypto_shash
*tfm
, const u8
*key
,
80 unsigned int descsize
;
82 /* These fields must match hash_alg_common. */
83 unsigned int digestsize
84 __attribute__ ((aligned(__alignof__(struct hash_alg_common
))));
85 unsigned int statesize
;
87 struct crypto_alg base
;
91 int (*init
)(struct ahash_request
*req
);
92 int (*update
)(struct ahash_request
*req
);
93 int (*final
)(struct ahash_request
*req
);
94 int (*finup
)(struct ahash_request
*req
);
95 int (*digest
)(struct ahash_request
*req
);
96 int (*export
)(struct ahash_request
*req
, void *out
);
97 int (*import
)(struct ahash_request
*req
, const void *in
);
98 int (*setkey
)(struct crypto_ahash
*tfm
, const u8
*key
,
101 unsigned int reqsize
;
102 struct crypto_tfm base
;
105 struct crypto_shash
{
106 unsigned int descsize
;
107 struct crypto_tfm base
;
110 static inline struct crypto_ahash
*__crypto_ahash_cast(struct crypto_tfm
*tfm
)
112 return container_of(tfm
, struct crypto_ahash
, base
);
115 struct crypto_ahash
*crypto_alloc_ahash(const char *alg_name
, u32 type
,
118 static inline struct crypto_tfm
*crypto_ahash_tfm(struct crypto_ahash
*tfm
)
123 static inline void crypto_free_ahash(struct crypto_ahash
*tfm
)
125 crypto_destroy_tfm(tfm
, crypto_ahash_tfm(tfm
));
128 static inline unsigned int crypto_ahash_alignmask(
129 struct crypto_ahash
*tfm
)
131 return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm
));
134 static inline struct hash_alg_common
*__crypto_hash_alg_common(
135 struct crypto_alg
*alg
)
137 return container_of(alg
, struct hash_alg_common
, base
);
140 static inline struct hash_alg_common
*crypto_hash_alg_common(
141 struct crypto_ahash
*tfm
)
143 return __crypto_hash_alg_common(crypto_ahash_tfm(tfm
)->__crt_alg
);
146 static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash
*tfm
)
148 return crypto_hash_alg_common(tfm
)->digestsize
;
151 static inline unsigned int crypto_ahash_statesize(struct crypto_ahash
*tfm
)
153 return crypto_hash_alg_common(tfm
)->statesize
;
156 static inline u32
crypto_ahash_get_flags(struct crypto_ahash
*tfm
)
158 return crypto_tfm_get_flags(crypto_ahash_tfm(tfm
));
161 static inline void crypto_ahash_set_flags(struct crypto_ahash
*tfm
, u32 flags
)
163 crypto_tfm_set_flags(crypto_ahash_tfm(tfm
), flags
);
166 static inline void crypto_ahash_clear_flags(struct crypto_ahash
*tfm
, u32 flags
)
168 crypto_tfm_clear_flags(crypto_ahash_tfm(tfm
), flags
);
171 static inline struct crypto_ahash
*crypto_ahash_reqtfm(
172 struct ahash_request
*req
)
174 return __crypto_ahash_cast(req
->base
.tfm
);
177 static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash
*tfm
)
182 static inline void *ahash_request_ctx(struct ahash_request
*req
)
187 int crypto_ahash_setkey(struct crypto_ahash
*tfm
, const u8
*key
,
188 unsigned int keylen
);
189 int crypto_ahash_finup(struct ahash_request
*req
);
190 int crypto_ahash_final(struct ahash_request
*req
);
191 int crypto_ahash_digest(struct ahash_request
*req
);
193 static inline int crypto_ahash_export(struct ahash_request
*req
, void *out
)
195 return crypto_ahash_reqtfm(req
)->export(req
, out
);
198 static inline int crypto_ahash_import(struct ahash_request
*req
, const void *in
)
200 return crypto_ahash_reqtfm(req
)->import(req
, in
);
203 static inline int crypto_ahash_init(struct ahash_request
*req
)
205 return crypto_ahash_reqtfm(req
)->init(req
);
208 static inline int crypto_ahash_update(struct ahash_request
*req
)
210 return crypto_ahash_reqtfm(req
)->update(req
);
213 static inline void ahash_request_set_tfm(struct ahash_request
*req
,
214 struct crypto_ahash
*tfm
)
216 req
->base
.tfm
= crypto_ahash_tfm(tfm
);
219 static inline struct ahash_request
*ahash_request_alloc(
220 struct crypto_ahash
*tfm
, gfp_t gfp
)
222 struct ahash_request
*req
;
224 req
= kmalloc(sizeof(struct ahash_request
) +
225 crypto_ahash_reqsize(tfm
), gfp
);
228 ahash_request_set_tfm(req
, tfm
);
233 static inline void ahash_request_free(struct ahash_request
*req
)
238 static inline struct ahash_request
*ahash_request_cast(
239 struct crypto_async_request
*req
)
241 return container_of(req
, struct ahash_request
, base
);
244 static inline void ahash_request_set_callback(struct ahash_request
*req
,
246 crypto_completion_t
compl,
249 req
->base
.complete
= compl;
250 req
->base
.data
= data
;
251 req
->base
.flags
= flags
;
254 static inline void ahash_request_set_crypt(struct ahash_request
*req
,
255 struct scatterlist
*src
, u8
*result
,
259 req
->nbytes
= nbytes
;
260 req
->result
= result
;
263 struct crypto_shash
*crypto_alloc_shash(const char *alg_name
, u32 type
,
266 static inline struct crypto_tfm
*crypto_shash_tfm(struct crypto_shash
*tfm
)
271 static inline void crypto_free_shash(struct crypto_shash
*tfm
)
273 crypto_destroy_tfm(tfm
, crypto_shash_tfm(tfm
));
276 static inline unsigned int crypto_shash_alignmask(
277 struct crypto_shash
*tfm
)
279 return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm
));
282 static inline unsigned int crypto_shash_blocksize(struct crypto_shash
*tfm
)
284 return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm
));
287 static inline struct shash_alg
*__crypto_shash_alg(struct crypto_alg
*alg
)
289 return container_of(alg
, struct shash_alg
, base
);
292 static inline struct shash_alg
*crypto_shash_alg(struct crypto_shash
*tfm
)
294 return __crypto_shash_alg(crypto_shash_tfm(tfm
)->__crt_alg
);
297 static inline unsigned int crypto_shash_digestsize(struct crypto_shash
*tfm
)
299 return crypto_shash_alg(tfm
)->digestsize
;
302 static inline unsigned int crypto_shash_statesize(struct crypto_shash
*tfm
)
304 return crypto_shash_alg(tfm
)->statesize
;
307 static inline u32
crypto_shash_get_flags(struct crypto_shash
*tfm
)
309 return crypto_tfm_get_flags(crypto_shash_tfm(tfm
));
312 static inline void crypto_shash_set_flags(struct crypto_shash
*tfm
, u32 flags
)
314 crypto_tfm_set_flags(crypto_shash_tfm(tfm
), flags
);
317 static inline void crypto_shash_clear_flags(struct crypto_shash
*tfm
, u32 flags
)
319 crypto_tfm_clear_flags(crypto_shash_tfm(tfm
), flags
);
322 static inline unsigned int crypto_shash_descsize(struct crypto_shash
*tfm
)
324 return tfm
->descsize
;
327 static inline void *shash_desc_ctx(struct shash_desc
*desc
)
332 int crypto_shash_setkey(struct crypto_shash
*tfm
, const u8
*key
,
333 unsigned int keylen
);
334 int crypto_shash_digest(struct shash_desc
*desc
, const u8
*data
,
335 unsigned int len
, u8
*out
);
337 static inline int crypto_shash_export(struct shash_desc
*desc
, void *out
)
339 return crypto_shash_alg(desc
->tfm
)->export(desc
, out
);
342 static inline int crypto_shash_import(struct shash_desc
*desc
, const void *in
)
344 return crypto_shash_alg(desc
->tfm
)->import(desc
, in
);
347 static inline int crypto_shash_init(struct shash_desc
*desc
)
349 return crypto_shash_alg(desc
->tfm
)->init(desc
);
352 int crypto_shash_update(struct shash_desc
*desc
, const u8
*data
,
354 int crypto_shash_final(struct shash_desc
*desc
, u8
*out
);
355 int crypto_shash_finup(struct shash_desc
*desc
, const u8
*data
,
356 unsigned int len
, u8
*out
);
358 #endif /* _CRYPTO_HASH_H */