From 1bebff26972a3fd97c969072a6e44cfc1306fab6 Mon Sep 17 00:00:00 2001 From: Xavier Roche Date: Wed, 28 May 2014 16:36:24 +0000 Subject: Fixed dirty uint32_t cast leading to aliasing issues. --- src/md5.c | 29 +++++++++++++++-------------- src/md5.h | 5 ++++- 2 files changed, 19 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/md5.c b/src/md5.c index d5b8945..7c9d9de 100644 --- a/src/md5.c +++ b/src/md5.c @@ -88,7 +88,7 @@ void MD5Update(struct MD5Context *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.ui8 + t; t = 64 - t; if (len < t) { @@ -97,25 +97,25 @@ void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len) { } memcpy(p, buf, t); if (ctx->doByteReverse) - byteReverse(ctx->in, 16); - MD5Transform(ctx->buf, (uint32 *) ctx->in); + byteReverse(ctx->in.ui8, 16); + MD5Transform(ctx->buf, ctx->in.ui32); buf += t; len -= t; } /* Process data in 64-byte chunks */ while(len >= 64) { - memcpy(ctx->in, buf, 64); + memcpy(ctx->in.ui8, buf, 64); if (ctx->doByteReverse) - byteReverse(ctx->in, 16); - MD5Transform(ctx->buf, (uint32 *) ctx->in); + byteReverse(ctx->in.ui8, 16); + MD5Transform(ctx->buf, ctx->in.ui32); buf += 64; len -= 64; } /* Handle any remaining bytes of data. */ - memcpy(ctx->in, buf, len); + memcpy(ctx->in.ui8, buf, len); } /* @@ -131,7 +131,7 @@ void MD5Final(unsigned char digest[16], struct MD5Context *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.ui8 + count; *p++ = 0x80; /* Bytes of padding needed to make 64 bytes */ @@ -142,26 +142,27 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx) { /* Two lots of padding: Pad the first block to 64 bytes */ memset(p, 0, count); if (ctx->doByteReverse) - byteReverse(ctx->in, 16); - MD5Transform(ctx->buf, (uint32 *) ctx->in); + byteReverse(ctx->in.ui8, 16); + MD5Transform(ctx->buf, ctx->in.ui32); /* Now fill the next block with 56 bytes */ - memset(ctx->in, 0, 56); + memset(ctx->in.ui8, 0, 56); } else { /* Pad block to 56 bytes */ memset(p, 0, count - 8); } if (ctx->doByteReverse) - byteReverse(ctx->in, 14); + byteReverse(ctx->in.ui8, 14); /* Append length in bits and transform */ /* Note: see patch for PAM from Tomas Mraz */ - memcpy((uint32 *) ctx->in + 14, ctx->bits, 2 * sizeof(uint32)); + ctx->in.ui32[14] = ctx->bits[0]; + ctx->in.ui32[15] = ctx->bits[1]; /*((uint32 *) ctx->in)[14] = ctx->bits[0]; ((uint32 *) ctx->in)[15] = ctx->bits[1]; */ - MD5Transform(ctx->buf, (uint32 *) ctx->in); + MD5Transform(ctx->buf, ctx->in.ui32); if (ctx->doByteReverse) byteReverse((unsigned char *) ctx->buf, 4); memcpy(digest, ctx->buf, 16); diff --git a/src/md5.h b/src/md5.h index a7b9f17..9dc72c7 100644 --- a/src/md5.h +++ b/src/md5.h @@ -18,7 +18,10 @@ typedef unsigned long uint32; #endif struct MD5Context { - unsigned char in[64]; + union { + unsigned char ui8[64]; + uint32 ui32[16]; + } in; uint32 buf[4]; uint32 bits[2]; int doByteReverse; -- cgit v1.2.3