aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Tatham <anakin@pobox.com>2017-05-18 18:49:35 +0100
committerSimon Tatham <anakin@pobox.com>2017-05-18 18:49:35 +0100
commit2cab25bf140eff04aef44b87e5a2ac791cc74c30 (patch)
treee26d93c44163314db8d96b78926e2f65b7faa144
parent4b3314ff48cb373d9933a5b731b5e476849f5579 (diff)
downloadwix-on-linux-2cab25bf140eff04aef44b87e5a2ac791cc74c30.tar.gz
wix-on-linux-2cab25bf140eff04aef44b87e5a2ac791cc74c30.tar.bz2
wix-on-linux-2cab25bf140eff04aef44b87e5a2ac791cc74c30.zip
General MD5 cleanups.
I did these in passing while looking for the bug in the previous commit, and they seem like general improvements, so I'll keep them.
-rw-r--r--md5.c58
1 files changed, 24 insertions, 34 deletions
diff --git a/md5.c b/md5.c
index 2fbbeec..648620c 100644
--- a/md5.c
+++ b/md5.c
@@ -153,7 +153,6 @@ static void MD5Init(struct MD5Context *s)
153static void MD5Update(struct MD5Context *s, unsigned char const *p, 153static void MD5Update(struct MD5Context *s, unsigned char const *p,
154 unsigned len) 154 unsigned len)
155{ 155{
156 unsigned char *q = (unsigned char *) p;
157 uint32_t wordblock[16]; 156 uint32_t wordblock[16];
158 int i; 157 int i;
159 158
@@ -162,33 +161,31 @@ static void MD5Update(struct MD5Context *s, unsigned char const *p,
162 */ 161 */
163 s->len += len; 162 s->len += len;
164 163
165 if (s->blkused + len < BLKSIZE) { 164 while (len >= BLKSIZE - s->blkused) {
165 /*
166 * Complete and process a hash block.
167 */
168 memcpy(s->block + s->blkused, p, BLKSIZE - s->blkused);
169 p += BLKSIZE - s->blkused;
170 len -= BLKSIZE - s->blkused;
171 /* Now process the block. Gather bytes little-endian into words */
172 for (i = 0; i < 16; i++) {
173 wordblock[i] =
174 (((uint32_t) s->block[i * 4 + 3]) << 24) |
175 (((uint32_t) s->block[i * 4 + 2]) << 16) |
176 (((uint32_t) s->block[i * 4 + 1]) << 8) |
177 (((uint32_t) s->block[i * 4 + 0]) << 0);
178 }
179 MD5_Block(&s->core, wordblock);
180 s->blkused = 0;
181 }
182
183 if (len > 0) {
166 /* 184 /*
167 * Trivial case: just add to the block. 185 * Append to the block.
168 */ 186 */
169 memcpy(s->block + s->blkused, q, len); 187 memcpy(s->block + s->blkused, p, len);
170 s->blkused += len; 188 s->blkused += len;
171 } else {
172 /*
173 * We must complete and process at least one block.
174 */
175 while (s->blkused + len >= BLKSIZE) {
176 memcpy(s->block + s->blkused, q, BLKSIZE - s->blkused);
177 q += BLKSIZE - s->blkused;
178 len -= BLKSIZE - s->blkused;
179 /* Now process the block. Gather bytes little-endian into words */
180 for (i = 0; i < 16; i++) {
181 wordblock[i] =
182 (((uint32_t) s->block[i * 4 + 3]) << 24) |
183 (((uint32_t) s->block[i * 4 + 2]) << 16) |
184 (((uint32_t) s->block[i * 4 + 1]) << 8) |
185 (((uint32_t) s->block[i * 4 + 0]) << 0);
186 }
187 MD5_Block(&s->core, wordblock);
188 s->blkused = 0;
189 }
190 memcpy(s->block, q, len);
191 s->blkused = len;
192 } 189 }
193} 190}
194 191
@@ -210,15 +207,8 @@ static void MD5Final(uint32_t output[4], struct MD5Context *s)
210 c[0] = 0x80; 207 c[0] = 0x80;
211 MD5Update(s, c, pad); 208 MD5Update(s, c, pad);
212 209
213 c[7] = (len >> (8*7)) & 0xFF; 210 for (i = 0; i < 8; i++)
214 c[6] = (len >> (8*6)) & 0xFF; 211 c[i] = (len >> (8*i)) & 0xFF;
215 c[5] = (len >> (8*5)) & 0xFF;
216 c[4] = (len >> (8*4)) & 0xFF;
217 c[3] = (len >> (8*3)) & 0xFF;
218 c[2] = (len >> (8*2)) & 0xFF;
219 c[1] = (len >> (8*1)) & 0xFF;
220 c[0] = (len >> (8*0)) & 0xFF;
221
222 MD5Update(s, c, 8); 212 MD5Update(s, c, 8);
223 213
224 for (i = 0; i < 4; i++) 214 for (i = 0; i < 4; i++)