ethumb: Support strict aliasing rules in MD5 code

This commit is contained in:
Henrique Dante de Almeida 2013-01-25 14:46:05 -02:00 committed by Lucas De Marchi
parent 2656d7097e
commit 95623dee10
2 changed files with 20 additions and 16 deletions

View File

@ -77,7 +77,7 @@ void MD5Update(MD5_CTX *ctx, unsigned char const *buf, unsigned len)
/* Handle any leading odd-sized chunks */
if (t) {
unsigned char *p = (unsigned char *) ctx->in + t;
unsigned char *p = ctx->in.s + t;
t = 64 - t;
if (len < t) {
@ -85,24 +85,24 @@ void MD5Update(MD5_CTX *ctx, unsigned char const *buf, unsigned len)
return;
}
memcpy(p, buf, t);
byteReverse(ctx->in, 16);
MD5Transform(ctx->buf, (uint32_t *) ctx->in);
byteReverse(ctx->in.s, 16);
MD5Transform(ctx->buf, ctx->in.i);
buf += t;
len -= t;
}
/* Process data in 64-byte chunks */
while (len >= 64) {
memcpy(ctx->in, buf, 64);
byteReverse(ctx->in, 16);
MD5Transform(ctx->buf, (uint32_t *) ctx->in);
memcpy(ctx->in.s, buf, 64);
byteReverse(ctx->in.s, 16);
MD5Transform(ctx->buf, ctx->in.i);
buf += 64;
len -= 64;
}
/* Handle any remaining bytes of data. */
memcpy(ctx->in, buf, len);
memcpy(ctx->in.s, buf, len);
}
/*
@ -119,7 +119,7 @@ void MD5Final(unsigned char digest[16], MD5_CTX *ctx)
/* Set the first char of padding to 0x80. This is safe since there is
always at least one byte free */
p = ctx->in + count;
p = ctx->in.s + count;
*p++ = 0x80;
/* Bytes of padding needed to make 64 bytes */
@ -129,22 +129,22 @@ void MD5Final(unsigned char digest[16], MD5_CTX *ctx)
if (count < 8) {
/* Two lots of padding: Pad the first block to 64 bytes */
memset(p, 0, count);
byteReverse(ctx->in, 16);
MD5Transform(ctx->buf, (uint32_t *) ctx->in);
byteReverse(ctx->in.s, 16);
MD5Transform(ctx->buf, ctx->in.i);
/* Now fill the next block with 56 bytes */
memset(ctx->in, 0, 56);
memset(ctx->in.s, 0, 56);
} else {
/* Pad block to 56 bytes */
memset(p, 0, count - 8);
}
byteReverse(ctx->in, 14);
byteReverse(ctx->in.s, 14);
/* Append length in bits and transform */
((uint32_t *) ctx->in)[14] = ctx->bits[0];
((uint32_t *) ctx->in)[15] = ctx->bits[1];
ctx->in.i[14] = ctx->bits[0];
ctx->in.i[15] = ctx->bits[1];
MD5Transform(ctx->buf, (uint32_t *) ctx->in);
MD5Transform(ctx->buf, ctx->in.i);
byteReverse((unsigned char *) ctx->buf, 4);
memcpy(digest, ctx->buf, 16);
memset((char *) ctx, 0, sizeof(ctx)); /* In case it's sensitive */

View File

@ -9,7 +9,11 @@
typedef struct MD5Context {
uint32_t buf[4];
uint32_t bits[2];
unsigned char in[64];
union
{
unsigned char s[64];
uint32_t i[16];
} in;
} MD5_CTX;
extern void MD5Init(MD5_CTX *context);