aboutsummaryrefslogtreecommitdiffstats
path: root/include/crypto/hash.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/crypto/hash.h')
-rw-r--r--include/crypto/hash.h147
1 files changed, 99 insertions, 48 deletions
diff --git a/include/crypto/hash.h b/include/crypto/hash.h
index d56bb71617c3..26cb1eb16f4c 100644
--- a/include/crypto/hash.h
+++ b/include/crypto/hash.h
@@ -15,6 +15,42 @@
15 15
16#include <linux/crypto.h> 16#include <linux/crypto.h>
17 17
18struct crypto_ahash;
19
20struct hash_alg_common {
21 unsigned int digestsize;
22 unsigned int statesize;
23
24 struct crypto_alg base;
25};
26
27struct ahash_request {
28 struct crypto_async_request base;
29
30 unsigned int nbytes;
31 struct scatterlist *src;
32 u8 *result;
33
34 /* This field may only be used by the ahash API code. */
35 void *priv;
36
37 void *__ctx[] CRYPTO_MINALIGN_ATTR;
38};
39
40struct ahash_alg {
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,
49 unsigned int keylen);
50
51 struct hash_alg_common halg;
52};
53
18struct shash_desc { 54struct shash_desc {
19 struct crypto_shash *tfm; 55 struct crypto_shash *tfm;
20 u32 flags; 56 u32 flags;
@@ -24,7 +60,6 @@ struct shash_desc {
24 60
25struct shash_alg { 61struct shash_alg {
26 int (*init)(struct shash_desc *desc); 62 int (*init)(struct shash_desc *desc);
27 int (*reinit)(struct shash_desc *desc);
28 int (*update)(struct shash_desc *desc, const u8 *data, 63 int (*update)(struct shash_desc *desc, const u8 *data,
29 unsigned int len); 64 unsigned int len);
30 int (*final)(struct shash_desc *desc, u8 *out); 65 int (*final)(struct shash_desc *desc, u8 *out);
@@ -32,38 +67,48 @@ struct shash_alg {
32 unsigned int len, u8 *out); 67 unsigned int len, u8 *out);
33 int (*digest)(struct shash_desc *desc, const u8 *data, 68 int (*digest)(struct shash_desc *desc, const u8 *data,
34 unsigned int len, u8 *out); 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);
35 int (*setkey)(struct crypto_shash *tfm, const u8 *key, 72 int (*setkey)(struct crypto_shash *tfm, const u8 *key,
36 unsigned int keylen); 73 unsigned int keylen);
37 74
38 unsigned int descsize; 75 unsigned int descsize;
39 unsigned int digestsize; 76
77 /* These fields must match hash_alg_common. */
78 unsigned int digestsize
79 __attribute__ ((aligned(__alignof__(struct hash_alg_common))));
80 unsigned int statesize;
40 81
41 struct crypto_alg base; 82 struct crypto_alg base;
42}; 83};
43 84
44struct crypto_ahash { 85struct crypto_ahash {
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,
94 unsigned int keylen);
95
96 unsigned int reqsize;
45 struct crypto_tfm base; 97 struct crypto_tfm base;
46}; 98};
47 99
48struct crypto_shash { 100struct crypto_shash {
101 unsigned int descsize;
49 struct crypto_tfm base; 102 struct crypto_tfm base;
50}; 103};
51 104
52static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm) 105static inline struct crypto_ahash *__crypto_ahash_cast(struct crypto_tfm *tfm)
53{ 106{
54 return (struct crypto_ahash *)tfm; 107 return container_of(tfm, struct crypto_ahash, base);
55} 108}
56 109
57static inline struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, 110struct crypto_ahash *crypto_alloc_ahash(const char *alg_name, u32 type,
58 u32 type, u32 mask) 111 u32 mask);
59{
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;
64
65 return __crypto_ahash_cast(crypto_alloc_base(alg_name, type, mask));
66}
67 112
68static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm) 113static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
69{ 114{
@@ -72,7 +117,7 @@ static inline struct crypto_tfm *crypto_ahash_tfm(struct crypto_ahash *tfm)
72 117
73static inline void crypto_free_ahash(struct crypto_ahash *tfm) 118static inline void crypto_free_ahash(struct crypto_ahash *tfm)
74{ 119{
75 crypto_free_tfm(crypto_ahash_tfm(tfm)); 120 crypto_destroy_tfm(tfm, crypto_ahash_tfm(tfm));
76} 121}
77 122
78static inline unsigned int crypto_ahash_alignmask( 123static inline unsigned int crypto_ahash_alignmask(
@@ -81,14 +126,26 @@ static inline unsigned int crypto_ahash_alignmask(
81 return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm)); 126 return crypto_tfm_alg_alignmask(crypto_ahash_tfm(tfm));
82} 127}
83 128
84static inline struct ahash_tfm *crypto_ahash_crt(struct crypto_ahash *tfm) 129static inline struct hash_alg_common *__crypto_hash_alg_common(
130 struct crypto_alg *alg)
131{
132 return container_of(alg, struct hash_alg_common, base);
133}
134
135static inline struct hash_alg_common *crypto_hash_alg_common(
136 struct crypto_ahash *tfm)
85{ 137{
86 return &crypto_ahash_tfm(tfm)->crt_ahash; 138 return __crypto_hash_alg_common(crypto_ahash_tfm(tfm)->__crt_alg);
87} 139}
88 140
89static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm) 141static inline unsigned int crypto_ahash_digestsize(struct crypto_ahash *tfm)
90{ 142{
91 return crypto_ahash_crt(tfm)->digestsize; 143 return crypto_hash_alg_common(tfm)->digestsize;
144}
145
146static inline unsigned int crypto_ahash_statesize(struct crypto_ahash *tfm)
147{
148 return crypto_hash_alg_common(tfm)->statesize;
92} 149}
93 150
94static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm) 151static inline u32 crypto_ahash_get_flags(struct crypto_ahash *tfm)
@@ -114,7 +171,7 @@ static inline struct crypto_ahash *crypto_ahash_reqtfm(
114 171
115static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm) 172static inline unsigned int crypto_ahash_reqsize(struct crypto_ahash *tfm)
116{ 173{
117 return crypto_ahash_crt(tfm)->reqsize; 174 return tfm->reqsize;
118} 175}
119 176
120static inline void *ahash_request_ctx(struct ahash_request *req) 177static inline void *ahash_request_ctx(struct ahash_request *req)
@@ -122,44 +179,30 @@ static inline void *ahash_request_ctx(struct ahash_request *req)
122 return req->__ctx; 179 return req->__ctx;
123} 180}
124 181
125static inline int crypto_ahash_setkey(struct crypto_ahash *tfm, 182int crypto_ahash_setkey(struct crypto_ahash *tfm, const u8 *key,
126 const u8 *key, unsigned int keylen) 183 unsigned int keylen);
127{ 184int crypto_ahash_finup(struct ahash_request *req);
128 struct ahash_tfm *crt = crypto_ahash_crt(tfm); 185int crypto_ahash_final(struct ahash_request *req);
129 186int crypto_ahash_digest(struct ahash_request *req);
130 return crt->setkey(tfm, key, keylen);
131}
132 187
133static inline int crypto_ahash_digest(struct ahash_request *req) 188static inline int crypto_ahash_export(struct ahash_request *req, void *out)
134{ 189{
135 struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req)); 190 return crypto_ahash_reqtfm(req)->export(req, out);
136 return crt->digest(req);
137} 191}
138 192
139static inline void crypto_ahash_export(struct ahash_request *req, u8 *out) 193static inline int crypto_ahash_import(struct ahash_request *req, const void *in)
140{ 194{
141 memcpy(out, ahash_request_ctx(req), 195 return crypto_ahash_reqtfm(req)->import(req, in);
142 crypto_ahash_reqsize(crypto_ahash_reqtfm(req)));
143} 196}
144 197
145int crypto_ahash_import(struct ahash_request *req, const u8 *in);
146
147static inline int crypto_ahash_init(struct ahash_request *req) 198static inline int crypto_ahash_init(struct ahash_request *req)
148{ 199{
149 struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req)); 200 return crypto_ahash_reqtfm(req)->init(req);
150 return crt->init(req);
151} 201}
152 202
153static inline int crypto_ahash_update(struct ahash_request *req) 203static inline int crypto_ahash_update(struct ahash_request *req)
154{ 204{
155 struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req)); 205 return crypto_ahash_reqtfm(req)->update(req);
156 return crt->update(req);
157}
158
159static inline int crypto_ahash_final(struct ahash_request *req)
160{
161 struct ahash_tfm *crt = crypto_ahash_crt(crypto_ahash_reqtfm(req));
162 return crt->final(req);
163} 206}
164 207
165static inline void ahash_request_set_tfm(struct ahash_request *req, 208static inline void ahash_request_set_tfm(struct ahash_request *req,
@@ -184,7 +227,7 @@ static inline struct ahash_request *ahash_request_alloc(
184 227
185static inline void ahash_request_free(struct ahash_request *req) 228static inline void ahash_request_free(struct ahash_request *req)
186{ 229{
187 kfree(req); 230 kzfree(req);
188} 231}
189 232
190static inline struct ahash_request *ahash_request_cast( 233static inline struct ahash_request *ahash_request_cast(
@@ -251,6 +294,11 @@ static inline unsigned int crypto_shash_digestsize(struct crypto_shash *tfm)
251 return crypto_shash_alg(tfm)->digestsize; 294 return crypto_shash_alg(tfm)->digestsize;
252} 295}
253 296
297static inline unsigned int crypto_shash_statesize(struct crypto_shash *tfm)
298{
299 return crypto_shash_alg(tfm)->statesize;
300}
301
254static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm) 302static inline u32 crypto_shash_get_flags(struct crypto_shash *tfm)
255{ 303{
256 return crypto_tfm_get_flags(crypto_shash_tfm(tfm)); 304 return crypto_tfm_get_flags(crypto_shash_tfm(tfm));
@@ -268,7 +316,7 @@ static inline void crypto_shash_clear_flags(struct crypto_shash *tfm, u32 flags)
268 316
269static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm) 317static inline unsigned int crypto_shash_descsize(struct crypto_shash *tfm)
270{ 318{
271 return crypto_shash_alg(tfm)->descsize; 319 return tfm->descsize;
272} 320}
273 321
274static inline void *shash_desc_ctx(struct shash_desc *desc) 322static inline void *shash_desc_ctx(struct shash_desc *desc)
@@ -281,12 +329,15 @@ int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
281int crypto_shash_digest(struct shash_desc *desc, const u8 *data, 329int crypto_shash_digest(struct shash_desc *desc, const u8 *data,
282 unsigned int len, u8 *out); 330 unsigned int len, u8 *out);
283 331
284static inline void crypto_shash_export(struct shash_desc *desc, u8 *out) 332static inline int crypto_shash_export(struct shash_desc *desc, void *out)
285{ 333{
286 memcpy(out, shash_desc_ctx(desc), crypto_shash_descsize(desc->tfm)); 334 return crypto_shash_alg(desc->tfm)->export(desc, out);
287} 335}
288 336
289int crypto_shash_import(struct shash_desc *desc, const u8 *in); 337static inline int crypto_shash_import(struct shash_desc *desc, const void *in)
338{
339 return crypto_shash_alg(desc->tfm)->import(desc, in);
340}
290 341
291static inline int crypto_shash_init(struct shash_desc *desc) 342static inline int crypto_shash_init(struct shash_desc *desc)
292{ 343{