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>
17 #include <linux/string.h>
19 struct hash_alg_common {
20 unsigned int digestsize;
21 unsigned int statesize;
23 struct crypto_alg base;
27 struct crypto_shash *tfm;
30 void *__ctx[] CRYPTO_MINALIGN_ATTR;
33 #define SHASH_DESC_ON_STACK(shash, ctx) \
34 char __##shash##_desc[sizeof(struct shash_desc) + \
35 crypto_shash_descsize(ctx)] CRYPTO_MINALIGN_ATTR; \
36 struct shash_desc *shash = (struct shash_desc *)__##shash##_desc
39 int (*init)(struct shash_desc *desc);
40 int (*update)(struct shash_desc *desc, const u8 *data,
42 int (*final)(struct shash_desc *desc, u8 *out);
43 int (*finup)(struct shash_desc *desc, const u8 *data,
44 unsigned int len, u8 *out);
45 int (*digest)(struct shash_desc *desc, const u8 *data,
46 unsigned int len, u8 *out);
47 int (*export)(struct shash_desc *desc, void *out);
48 int (*import)(struct shash_desc *desc, const void *in);
49 int (*setkey)(struct crypto_shash *tfm, const u8 *key,
52 unsigned int descsize;
54 /* These fields must match hash_alg_common. */
55 unsigned int digestsize
56 __attribute__ ((aligned(__alignof__(struct hash_alg_common))));
57 unsigned int statesize;
59 struct crypto_alg base;
63 unsigned int descsize;
64 struct crypto_tfm base;
67 struct crypto_shash *crypto_alloc_shash(const char *alg_name, u32 type,
70 static inline struct crypto_tfm *crypto_shash_tfm(struct crypto_shash *tfm)
75 static inline void crypto_free_shash(struct crypto_shash *tfm)
77 crypto_destroy_tfm(tfm, crypto_shash_tfm(tfm));
80 static inline const char *crypto_shash_alg_name(struct crypto_shash *tfm)
82 return crypto_tfm_alg_name(crypto_shash_tfm(tfm));
85 static inline const char *crypto_shash_driver_name(struct crypto_shash *tfm)
87 return crypto_tfm_alg_driver_name(crypto_shash_tfm(tfm));
90 static inline unsigned int crypto_shash_alignmask(
91 struct crypto_shash *tfm)
93 return crypto_tfm_alg_alignmask(crypto_shash_tfm(tfm));
96 static inline unsigned int crypto_shash_blocksize(struct crypto_shash *tfm)
98 return crypto_tfm_alg_blocksize(crypto_shash_tfm(tfm));
101 static inline struct shash_alg *__crypto_shash_alg(struct crypto_alg *alg)
103 return container_of(alg, struct shash_alg, base);
106 static inline struct shash_alg *crypto_shash_alg(struct crypto_shash *tfm)
108 return __crypto_shash_alg(crypto_shash_tfm(tfm)->__crt_alg);
111 static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
113 return crypto_shash_alg(tfm)->digestsize;
116 static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)
118 return crypto_shash_alg(tfm)->statesize;
121 static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
123 return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
126 static inline void crypto_shash_set_flags(struct crypto_shash *tfm, u32 flags)
128 crypto_tfm_set_flags(crypto_shash_tfm(tfm), flags);
131 static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
133 crypto_tfm_clear_flags(crypto_shash_tfm(tfm), flags);
136 static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
138 return tfm->descsize;
141 static inline void *shash_desc_ctx(struct shash_desc *desc)
146 int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
147 unsigned int keylen);
149 int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
150 unsigned int len, u8 *out);
152 static inline int crypto_shash_export(struct shash_desc *desc, void *out)
154 return crypto_shash_alg(desc->tfm)->export(desc, out);
157 static inline int crypto_shash_import(struct shash_desc *desc, const void *in)
159 return crypto_shash_alg(desc->tfm)->import(desc, in);
162 static inline int crypto_shash_init(struct shash_desc *desc)
164 return crypto_shash_alg(desc->tfm)->init(desc);
167 int crypto_shash_update(struct shash_desc *desc, const u8 *data,
170 int crypto_shash_final(struct shash_desc *desc, u8 *out);
172 int crypto_shash_finup(struct shash_desc *desc, const u8 *data,
173 unsigned int len, u8 *out);
175 static inline void shash_desc_zero(struct shash_desc *desc)
177 memzero_explicit(desc,
178 sizeof(*desc) + crypto_shash_descsize(desc->tfm));
181 #endif /* _CRYPTO_HASH_H */