diff options
author | Xavier Roche <xroche@users.noreply.github.com> | 2014-05-28 16:36:24 +0000 |
---|---|---|
committer | Xavier Roche <xroche@users.noreply.github.com> | 2014-05-28 16:36:24 +0000 |
commit | 1bebff26972a3fd97c969072a6e44cfc1306fab6 (patch) | |
tree | e82805d3dd90c2f530d2ac72cdc8e67cd6a43443 | |
parent | c7fac4dbca8a3266c973bd06469fd2e6534ab7ab (diff) |
Fixed dirty uint32_t cast leading to aliasing issues.
-rw-r--r-- | src/md5.c | 29 | ||||
-rw-r--r-- | src/md5.h | 5 |
2 files changed, 19 insertions, 15 deletions
@@ -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); @@ -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; |