Browse Source

Made it temporarily possible to call MD5 routines directly from external

code, rather than going through the digest layer.

Removed a spurious line of constants.
tags/v0.9.3
Michael Brown 18 years ago
parent
commit
57d539aab0
2 changed files with 37 additions and 21 deletions
  1. 12
    21
      src/crypto/md5.c
  2. 25
    0
      src/include/gpxe/md5.h

+ 12
- 21
src/crypto/md5.c View File

24
 #include <string.h>
24
 #include <string.h>
25
 #include <byteswap.h>
25
 #include <byteswap.h>
26
 #include <gpxe/crypto.h>
26
 #include <gpxe/crypto.h>
27
-
28
-#define MD5_DIGEST_SIZE		16
29
-#define MD5_BLOCK_WORDS		16
30
-#define MD5_HASH_WORDS		4
31
-
32
-struct md5_ctx {
33
-	u32 hash[MD5_HASH_WORDS];
34
-	u32 block[MD5_BLOCK_WORDS];
35
-	u64 byte_count;
36
-};
27
+#include <gpxe/md5.h>
37
 
28
 
38
 #define __md5step __attribute__ (( regparm ( 3 ) ))
29
 #define __md5step __attribute__ (( regparm ( 3 ) ))
39
 
30
 
93
 	6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21
84
 	6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21
94
 };
85
 };
95
 
86
 
96
-static const u32 k[] = {
87
+static const u32 k[64] = {
97
 	0xd76aa478UL, 0xe8c7b756UL, 0x242070dbUL, 0xc1bdceeeUL,
88
 	0xd76aa478UL, 0xe8c7b756UL, 0x242070dbUL, 0xc1bdceeeUL,
98
 	0xf57c0fafUL, 0x4787c62aUL, 0xa8304613UL, 0xfd469501UL,
89
 	0xf57c0fafUL, 0x4787c62aUL, 0xa8304613UL, 0xfd469501UL,
99
 	0x698098d8UL, 0x8b44f7afUL, 0xffff5bb1UL, 0x895cd7beUL,
90
 	0x698098d8UL, 0x8b44f7afUL, 0xffff5bb1UL, 0x895cd7beUL,
110
 	0x655b59c3UL, 0x8f0ccc92UL, 0xffeff47dUL, 0x85845dd1UL,
101
 	0x655b59c3UL, 0x8f0ccc92UL, 0xffeff47dUL, 0x85845dd1UL,
111
 	0x6fa87e4fUL, 0xfe2ce6e0UL, 0xa3014314UL, 0x4e0811a1UL,
102
 	0x6fa87e4fUL, 0xfe2ce6e0UL, 0xa3014314UL, 0x4e0811a1UL,
112
 	0xf7537e82UL, 0xbd3af235UL, 0x2ad7d2bbUL, 0xeb86d391UL,
103
 	0xf7537e82UL, 0xbd3af235UL, 0x2ad7d2bbUL, 0xeb86d391UL,
113
-	0xe1f27f3aUL, 0xf5710fb0UL, 0xada0e5c4UL, 0x98e4c919UL
114
 };
104
 };
115
 
105
 
116
 static void md5_transform(u32 *hash, const u32 *in)
106
 static void md5_transform(u32 *hash, const u32 *in)
160
 	}
150
 	}
161
 }
151
 }
162
 
152
 
163
-static inline void md5_transform_helper(struct md5_ctx *ctx)
153
+static inline void md5_transform_helper(struct md5_context *ctx)
164
 {
154
 {
165
 	le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
155
 	le32_to_cpu_array(ctx->block, sizeof(ctx->block) / sizeof(u32));
166
 	md5_transform(ctx->hash, ctx->block);
156
 	md5_transform(ctx->hash, ctx->block);
167
 }
157
 }
168
 
158
 
169
-static void md5_init(void *context)
159
+void md5_init ( struct md5_context *context )
170
 {
160
 {
171
-	struct md5_ctx *mctx = context;
172
-
161
+	struct md5_context *mctx = context;
173
 	mctx->hash[0] = 0x67452301;
162
 	mctx->hash[0] = 0x67452301;
174
 	mctx->hash[1] = 0xefcdab89;
163
 	mctx->hash[1] = 0xefcdab89;
175
 	mctx->hash[2] = 0x98badcfe;
164
 	mctx->hash[2] = 0x98badcfe;
177
 	mctx->byte_count = 0;
166
 	mctx->byte_count = 0;
178
 }
167
 }
179
 
168
 
180
-static void md5_update(void *context, const void *data, size_t len)
169
+void md5_update ( struct md5_context *context, const void *data, size_t len )
181
 {
170
 {
182
-	struct md5_ctx *mctx = context;
171
+	struct md5_context *mctx = context;
183
 	const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
172
 	const u32 avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
184
 
173
 
185
 	mctx->byte_count += len;
174
 	mctx->byte_count += len;
207
 	memcpy(mctx->block, data, len);
196
 	memcpy(mctx->block, data, len);
208
 }
197
 }
209
 
198
 
210
-static void md5_finish(void *context, void *out)
199
+void md5_finish ( struct md5_context *context, struct md5_hash *out )
211
 {
200
 {
212
-	struct md5_ctx *mctx = context;
201
+	struct md5_context *mctx = context;
213
 	const unsigned int offset = mctx->byte_count & 0x3f;
202
 	const unsigned int offset = mctx->byte_count & 0x3f;
214
 	char *p = (char *)mctx->block + offset;
203
 	char *p = (char *)mctx->block + offset;
215
 	int padding = 56 - (offset + 1);
204
 	int padding = 56 - (offset + 1);
233
 	memset(mctx, 0, sizeof(*mctx));
222
 	memset(mctx, 0, sizeof(*mctx));
234
 }
223
 }
235
 
224
 
225
+/*
236
 struct digest_algorithm md5_algorithm = {
226
 struct digest_algorithm md5_algorithm = {
237
-	.context_len	= sizeof ( struct md5_ctx ),
227
+	.context_len	= sizeof ( struct md5_context ),
238
 	.digest_len	= MD5_DIGEST_SIZE,
228
 	.digest_len	= MD5_DIGEST_SIZE,
239
 	.init		= md5_init,
229
 	.init		= md5_init,
240
 	.update		= md5_update,
230
 	.update		= md5_update,
241
 	.finish		= md5_finish,
231
 	.finish		= md5_finish,
242
 };
232
 };
233
+*/

+ 25
- 0
src/include/gpxe/md5.h View File

1
+#ifndef _GPXE_MD5_H
2
+#define _GPXE_MD5_H
3
+
4
+#include <stdint.h>
5
+
6
+#define MD5_DIGEST_SIZE		16
7
+#define MD5_BLOCK_WORDS		16
8
+#define MD5_HASH_WORDS		4
9
+
10
+struct md5_context {
11
+	u32 hash[MD5_HASH_WORDS];
12
+	u32 block[MD5_BLOCK_WORDS];
13
+	u64 byte_count;
14
+};
15
+
16
+struct md5_hash {
17
+	u8 hash[MD5_DIGEST_SIZE];
18
+};
19
+
20
+extern void md5_init ( struct md5_context *context );
21
+extern void md5_update ( struct md5_context *context, const void *data,
22
+			 size_t len );
23
+extern void md5_finish ( struct md5_context *context, struct md5_hash *hash );
24
+
25
+#endif /* _GPXE_MD5_H */

Loading…
Cancel
Save