Commit 7d6f75eb21b84cdc5dfb09789974f02b42a89058
Committed by
Herbert Xu
1 parent
6c3f975a4c
Exists in
master
and in
4 other branches
crypto: md5 - Add export support
This patch adds export/import support to md5. The exported type is defined by struct md5_state. This is modeled after the equivalent change to sha1_generic. Signed-off-by: Max Vozeler <max@hinterhof.net> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Showing 2 changed files with 41 additions and 16 deletions Side-by-side Diff
crypto/md5.c
... | ... | @@ -16,17 +16,13 @@ |
16 | 16 | * |
17 | 17 | */ |
18 | 18 | #include <crypto/internal/hash.h> |
19 | +#include <crypto/md5.h> | |
19 | 20 | #include <linux/init.h> |
20 | 21 | #include <linux/module.h> |
21 | 22 | #include <linux/string.h> |
22 | 23 | #include <linux/types.h> |
23 | 24 | #include <asm/byteorder.h> |
24 | 25 | |
25 | -#define MD5_DIGEST_SIZE 16 | |
26 | -#define MD5_HMAC_BLOCK_SIZE 64 | |
27 | -#define MD5_BLOCK_WORDS 16 | |
28 | -#define MD5_HASH_WORDS 4 | |
29 | - | |
30 | 26 | #define F1(x, y, z) (z ^ (x & (y ^ z))) |
31 | 27 | #define F2(x, y, z) F1(z, x, y) |
32 | 28 | #define F3(x, y, z) (x ^ y ^ z) |
... | ... | @@ -35,12 +31,6 @@ |
35 | 31 | #define MD5STEP(f, w, x, y, z, in, s) \ |
36 | 32 | (w += f(x, y, z) + in, w = (w<<s | w>>(32-s)) + x) |
37 | 33 | |
38 | -struct md5_ctx { | |
39 | - u32 hash[MD5_HASH_WORDS]; | |
40 | - u32 block[MD5_BLOCK_WORDS]; | |
41 | - u64 byte_count; | |
42 | -}; | |
43 | - | |
44 | 34 | static void md5_transform(u32 *hash, u32 const *in) |
45 | 35 | { |
46 | 36 | u32 a, b, c, d; |
... | ... | @@ -141,7 +131,7 @@ |
141 | 131 | } |
142 | 132 | } |
143 | 133 | |
144 | -static inline void md5_transform_helper(struct md5_ctx *ctx) | |
134 | +static inline void md5_transform_helper(struct md5_state *ctx) | |
145 | 135 | { |
146 | 136 | le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32)); |
147 | 137 | md5_transform(ctx->hash, ctx->block); |
... | ... | @@ -149,7 +139,7 @@ |
149 | 139 | |
150 | 140 | static int md5_init(struct shash_desc *desc) |
151 | 141 | { |
152 | - struct md5_ctx *mctx = shash_desc_ctx(desc); | |
142 | + struct md5_state *mctx = shash_desc_ctx(desc); | |
153 | 143 | |
154 | 144 | mctx->hash[0] = 0x67452301; |
155 | 145 | mctx->hash[1] = 0xefcdab89; |
... | ... | @@ -162,7 +152,7 @@ |
162 | 152 | |
163 | 153 | static int md5_update(struct shash_desc *desc, const u8 *data, unsigned int len) |
164 | 154 | { |
165 | - struct md5_ctx *mctx = shash_desc_ctx(desc); | |
155 | + struct md5_state *mctx = shash_desc_ctx(desc); | |
166 | 156 | const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f); |
167 | 157 | |
168 | 158 | mctx->byte_count += len; |
... | ... | @@ -194,7 +184,7 @@ |
194 | 184 | |
195 | 185 | static int md5_final(struct shash_desc *desc, u8 *out) |
196 | 186 | { |
197 | - struct md5_ctx *mctx = shash_desc_ctx(desc); | |
187 | + struct md5_state *mctx = shash_desc_ctx(desc); | |
198 | 188 | const unsigned int offset = mctx->byte_count & 0x3f; |
199 | 189 | char *p = (char *)mctx->block + offset; |
200 | 190 | int padding = 56 - (offset + 1); |
201 | 191 | |
... | ... | @@ -220,12 +210,30 @@ |
220 | 210 | return 0; |
221 | 211 | } |
222 | 212 | |
213 | +static int md5_export(struct shash_desc *desc, void *out) | |
214 | +{ | |
215 | + struct md5_state *ctx = shash_desc_ctx(desc); | |
216 | + | |
217 | + memcpy(out, ctx, sizeof(*ctx)); | |
218 | + return 0; | |
219 | +} | |
220 | + | |
221 | +static int md5_import(struct shash_desc *desc, const void *in) | |
222 | +{ | |
223 | + struct md5_state *ctx = shash_desc_ctx(desc); | |
224 | + | |
225 | + memcpy(ctx, in, sizeof(*ctx)); | |
226 | + return 0; | |
227 | +} | |
228 | + | |
223 | 229 | static struct shash_alg alg = { |
224 | 230 | .digestsize = MD5_DIGEST_SIZE, |
225 | 231 | .init = md5_init, |
226 | 232 | .update = md5_update, |
227 | 233 | .final = md5_final, |
228 | - .descsize = sizeof(struct md5_ctx), | |
234 | + .export = md5_export, | |
235 | + .import = md5_import, | |
236 | + .descsize = sizeof(struct md5_state), | |
229 | 237 | .base = { |
230 | 238 | .cra_name = "md5", |
231 | 239 | .cra_flags = CRYPTO_ALG_TYPE_SHASH, |
include/crypto/md5.h
1 | +#ifndef _CRYPTO_MD5_H | |
2 | +#define _CRYPTO_MD5_H | |
3 | + | |
4 | +#include <linux/types.h> | |
5 | + | |
6 | +#define MD5_DIGEST_SIZE 16 | |
7 | +#define MD5_HMAC_BLOCK_SIZE 64 | |
8 | +#define MD5_BLOCK_WORDS 16 | |
9 | +#define MD5_HASH_WORDS 4 | |
10 | + | |
11 | +struct md5_state { | |
12 | + u32 hash[MD5_HASH_WORDS]; | |
13 | + u32 block[MD5_BLOCK_WORDS]; | |
14 | + u64 byte_count; | |
15 | +}; | |
16 | + | |
17 | +#endif |