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.c79
1 files changed, 59 insertions, 20 deletions
diff --git a/src/microhttpd/md5.c b/src/microhttpd/md5.c
index 05a2c9e6..785873b5 100644
--- a/src/microhttpd/md5.c
+++ b/src/microhttpd/md5.c
@@ -26,6 +26,12 @@
26#include "mhd_bithelpers.h" 26#include "mhd_bithelpers.h"
27#include "mhd_assert.h" 27#include "mhd_assert.h"
28 28
29/**
30 * Number of bytes in single MD5 word
31 * used to process data
32 */
33#define MD5_BYTES_IN_WORD (32 / 8)
34
29 35
30/** 36/**
31 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious 37 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
@@ -87,26 +93,37 @@ MHD_MD5Final (void *ctx_,
87 93
88 /* Put number of bits */ 94 /* Put number of bits */
89 count_bits = ctx->count << 3; 95 count_bits = ctx->count << 3;
90 _MHD_PUT_64BIT_LE (ctx->buffer + 56, count_bits); 96 _MHD_PUT_64BIT_LE_SAFE (ctx->buffer + 56, count_bits);
91 MD5Transform (ctx->state, ctx->buffer); 97 MD5Transform (ctx->state, ctx->buffer);
92 98
93 /* Put digest in LE mode */ 99 /* Put digest in LE mode */
94 _MHD_PUT_32BIT_LE (digest, ctx->state[0]); 100#ifndef _MHD_PUT_32BIT_LE_UNALIGNED
95 _MHD_PUT_32BIT_LE (digest + 4, ctx->state[1]); 101 if (0 != ((uintptr_t) digest) % _MHD_UINT32_ALIGN)
96 _MHD_PUT_32BIT_LE (digest + 8, ctx->state[2]); 102 {
97 _MHD_PUT_32BIT_LE (digest + 12, ctx->state[3]); 103 uint32_t alig_dgst[MD5_DIGEST_SIZE / MD5_BYTES_IN_WORD];
104 _MHD_PUT_32BIT_LE (alig_dgst + 0, ctx->state[0]);
105 _MHD_PUT_32BIT_LE (alig_dgst + 1, ctx->state[1]);
106 _MHD_PUT_32BIT_LE (alig_dgst + 2, ctx->state[2]);
107 _MHD_PUT_32BIT_LE (alig_dgst + 3, ctx->state[3]);
108 /* Copy result to unaligned destination address */
109 memcpy (digest, alig_dgst, MD5_DIGEST_SIZE);
110 }
111 else
112#else /* _MHD_PUT_32BIT_LE_UNALIGNED */
113 if (1)
114#endif /* _MHD_PUT_32BIT_LE_UNALIGNED */
115 {
116 _MHD_PUT_32BIT_LE (digest, ctx->state[0]);
117 _MHD_PUT_32BIT_LE (digest + 4, ctx->state[1]);
118 _MHD_PUT_32BIT_LE (digest + 8, ctx->state[2]);
119 _MHD_PUT_32BIT_LE (digest + 12, ctx->state[3]);
120 }
98 121
99 /* Erase buffer */ 122 /* Erase buffer */
100 memset (ctx, 0, sizeof(*ctx)); 123 memset (ctx, 0, sizeof(*ctx));
101} 124}
102 125
103 126
104/**
105 * Number of bytes in single SHA-256 word
106 * used to process data
107 */
108#define MD5_BYTES_IN_WORD (32 / 8)
109
110/* The four core functions - F1 is optimized somewhat */ 127/* The four core functions - F1 is optimized somewhat */
111 128
112/* #define F1(x, y, z) (x & y | ~x & z) */ 129/* #define F1(x, y, z) (x & y | ~x & z) */
@@ -129,18 +146,40 @@ MD5Transform (uint32_t state[4],
129 const uint8_t block[MD5_BLOCK_SIZE]) 146 const uint8_t block[MD5_BLOCK_SIZE])
130{ 147{
131 uint32_t a, b, c, d; 148 uint32_t a, b, c, d;
149 uint32_t data_buf[MD5_BLOCK_SIZE / MD5_BYTES_IN_WORD];
150 const uint32_t *in;
132 151
133#if _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN 152#if (_MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN) || \
134 const uint32_t *in = (const uint32_t *) block; 153 ! defined (_MHD_GET_32BIT_LE_UNALIGNED)
135#else 154 if (0 != (((uintptr_t) block) % _MHD_UINT32_ALIGN))
136 uint32_t in[MD5_BLOCK_SIZE / MD5_BYTES_IN_WORD];
137 int i;
138
139 for (i = 0; i < MD5_BLOCK_SIZE / MD5_BYTES_IN_WORD; i++)
140 { 155 {
141 in[i] = _MHD_GET_32BIT_LE (block + i * MD5_BYTES_IN_WORD); 156 /* Copy data to the aligned buffer */
157 memcpy (data_buf, block, MD5_BLOCK_SIZE);
158 in = data_buf;
142 } 159 }
143#endif 160 else
161 in = (const uint32_t *) block;
162#endif /* _MHD_BYTE_ORDER == _MHD_LITTLE_ENDIAN) || \
163 ! _MHD_GET_32BIT_LE_UNALIGNED */
164#if _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN
165 data_buf[0] = _MHD_GET_32BIT_LE (in + 0);
166 data_buf[1] = _MHD_GET_32BIT_LE (in + 1);
167 data_buf[2] = _MHD_GET_32BIT_LE (in + 2);
168 data_buf[3] = _MHD_GET_32BIT_LE (in + 3);
169 data_buf[4] = _MHD_GET_32BIT_LE (in + 4);
170 data_buf[5] = _MHD_GET_32BIT_LE (in + 5);
171 data_buf[6] = _MHD_GET_32BIT_LE (in + 6);
172 data_buf[7] = _MHD_GET_32BIT_LE (in + 7);
173 data_buf[8] = _MHD_GET_32BIT_LE (in + 8);
174 data_buf[9] = _MHD_GET_32BIT_LE (in + 9);
175 data_buf[10] = _MHD_GET_32BIT_LE (in + 10);
176 data_buf[11] = _MHD_GET_32BIT_LE (in + 11);
177 data_buf[12] = _MHD_GET_32BIT_LE (in + 12);
178 data_buf[13] = _MHD_GET_32BIT_LE (in + 13);
179 data_buf[14] = _MHD_GET_32BIT_LE (in + 14);
180 data_buf[15] = _MHD_GET_32BIT_LE (in + 15);
181 in = data_buf;
182#endif /* _MHD_BYTE_ORDER != _MHD_LITTLE_ENDIAN */
144 183
145 a = state[0]; 184 a = state[0];
146 b = state[1]; 185 b = state[1];