aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/md5.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/microhttpd/md5.c')
-rw-r--r--src/microhttpd/md5.c130
1 files changed, 76 insertions, 54 deletions
diff --git a/src/microhttpd/md5.c b/src/microhttpd/md5.c
index d92a42ee..6a8819c9 100644
--- a/src/microhttpd/md5.c
+++ b/src/microhttpd/md5.c
@@ -42,16 +42,20 @@ static uint8_t PADDING[MD5_BLOCK_SIZE] = {
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
43}; 43};
44 44
45/* 45
46/**
46 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 47 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
47 * initialization constants. 48 * initialization constants.
49 *
50 * @param ctx must be a `struct MD5Context *`
48 */ 51 */
49void 52void
50MD5Init(struct MD5Context *ctx) 53MD5Init (void *ctx_)
51{ 54{
55 struct MD5Context *ctx = ctx_;
56
52 if (!ctx) 57 if (!ctx)
53 return; 58 return;
54
55 ctx->count = 0; 59 ctx->count = 0;
56 ctx->state[0] = 0x67452301; 60 ctx->state[0] = 0x67452301;
57 ctx->state[1] = 0xefcdab89; 61 ctx->state[1] = 0xefcdab89;
@@ -59,56 +63,13 @@ MD5Init(struct MD5Context *ctx)
59 ctx->state[3] = 0x10325476; 63 ctx->state[3] = 0x10325476;
60} 64}
61 65
62/*
63 * Update context to reflect the concatenation of another buffer full
64 * of bytes.
65 */
66void
67MD5Update(struct MD5Context *ctx, const unsigned char *input, size_t len)
68{
69 size_t have, need;
70
71 if (!ctx || !input)
72 return;
73
74 /* Check how many bytes we already have and how many more we need. */
75 have = (size_t)((ctx->count >> 3) & (MD5_BLOCK_SIZE - 1));
76 need = MD5_BLOCK_SIZE - have;
77
78 /* Update bitcount */
79 ctx->count += (uint64_t)len << 3;
80
81 if (len >= need)
82 {
83 if (have != 0)
84 {
85 memcpy(ctx->buffer + have, input, need);
86 MD5Transform(ctx->state, ctx->buffer);
87 input += need;
88 len -= need;
89 have = 0;
90 }
91 66
92 /* Process data in MD5_BLOCK_SIZE-byte chunks. */ 67/**
93 while (len >= MD5_BLOCK_SIZE)
94 {
95 MD5Transform(ctx->state, input);
96 input += MD5_BLOCK_SIZE;
97 len -= MD5_BLOCK_SIZE;
98 }
99 }
100
101 /* Handle any remaining bytes of data. */
102 if (len != 0)
103 memcpy(ctx->buffer + have, input, len);
104}
105
106/*
107 * Pad pad to 64-byte boundary with the bit pattern 68 * Pad pad to 64-byte boundary with the bit pattern
108 * 1 0* (64-bit count of bits processed, MSB-first) 69 * 1 0* (64-bit count of bits processed, MSB-first)
109 */ 70 */
110void 71static void
111MD5Pad(struct MD5Context *ctx) 72MD5Pad (struct MD5Context *ctx)
112{ 73{
113 uint8_t count[8]; 74 uint8_t count[8];
114 size_t padlen; 75 size_t padlen;
@@ -128,12 +89,17 @@ MD5Pad(struct MD5Context *ctx)
128 MD5Update(ctx, count, 8); 89 MD5Update(ctx, count, 8);
129} 90}
130 91
131/* 92
93/**
132 * Final wrapup--call MD5Pad, fill in digest and zero out ctx. 94 * Final wrapup--call MD5Pad, fill in digest and zero out ctx.
95 *
96 * @param ctx must be a `struct MD5Context *`
133 */ 97 */
134void 98void
135MD5Final(unsigned char digest[MD5_DIGEST_SIZE], struct MD5Context *ctx) 99MD5Final (void *ctx_,
100 unsigned char digest[MD5_DIGEST_SIZE])
136{ 101{
102 struct MD5Context *ctx = ctx_;
137 int i; 103 int i;
138 104
139 if (!ctx || !digest) 105 if (!ctx || !digest)
@@ -159,13 +125,14 @@ MD5Final(unsigned char digest[MD5_DIGEST_SIZE], struct MD5Context *ctx)
159#define MD5STEP(f, w, x, y, z, data, s) \ 125#define MD5STEP(f, w, x, y, z, data, s) \
160 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x ) 126 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
161 127
162/* 128/**
163 * The core of the MD5 algorithm, this alters an existing MD5 hash to 129 * The core of the MD5 algorithm, this alters an existing MD5 hash to
164 * reflect the addition of 16 longwords of new data. MD5Update blocks 130 * reflect the addition of 16 longwords of new data. MD5Update blocks
165 * the data and converts bytes into longwords for this routine. 131 * the data and converts bytes into longwords for this routine.
166 */ 132 */
167void 133static void
168MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_SIZE]) 134MD5Transform (uint32_t state[4],
135 const uint8_t block[MD5_BLOCK_SIZE])
169{ 136{
170 uint32_t a, b, c, d, in[MD5_BLOCK_SIZE / 4]; 137 uint32_t a, b, c, d, in[MD5_BLOCK_SIZE / 4];
171 138
@@ -261,4 +228,59 @@ MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_SIZE])
261 state[3] += d; 228 state[3] += d;
262} 229}
263 230
231
232/**
233 * Update context to reflect the concatenation of another buffer full
234 * of bytes.
235 */
236void
237MD5Update (void *ctx_,
238 const uint8_t *input,
239 size_t len)
240{
241 struct MD5Context *ctx = ctx_;
242 size_t have, need;
243
244 if (!ctx || !input)
245 return;
246
247 /* Check how many bytes we already have and how many more we need. */
248 have = (size_t)((ctx->count >> 3) & (MD5_BLOCK_SIZE - 1));
249 need = MD5_BLOCK_SIZE - have;
250
251 /* Update bitcount */
252 ctx->count += (uint64_t)len << 3;
253
254 if (len >= need)
255 {
256 if (have != 0)
257 {
258 memcpy (ctx->buffer + have,
259 input,
260 need);
261 MD5Transform(ctx->state, ctx->buffer);
262 input += need;
263 len -= need;
264 have = 0;
265 }
266
267 /* Process data in MD5_BLOCK_SIZE-byte chunks. */
268 while (len >= MD5_BLOCK_SIZE)
269 {
270 MD5Transform (ctx->state,
271 (const unsigned char *) input);
272 input += MD5_BLOCK_SIZE;
273 len -= MD5_BLOCK_SIZE;
274 }
275 }
276
277 /* Handle any remaining bytes of data. */
278 if (0 != len)
279 memcpy (ctx->buffer + have,
280 input,
281 len);
282}
283
284
285
264/* end of md5.c */ 286/* end of md5.c */