Commit 344b7b67 authored by Olivier Crête's avatar Olivier Crête

stun: Make the MD5 code strict-aliasing correct

parent 4b588f11
...@@ -92,7 +92,7 @@ void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len) ...@@ -92,7 +92,7 @@ void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
/* Handle any leading odd-sized chunks */ /* Handle any leading odd-sized chunks */
if (t) { if (t) {
unsigned char *p = (unsigned char *) ctx->in + t; unsigned char *p = (unsigned char *) ctx->in.u8 + t;
t = 64 - t; t = 64 - t;
if (len < t) { if (len < t) {
...@@ -100,24 +100,24 @@ void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len) ...@@ -100,24 +100,24 @@ void MD5Update(struct MD5Context *ctx, unsigned char const *buf, unsigned len)
return; return;
} }
memcpy(p, buf, t); memcpy(p, buf, t);
byteReverse(ctx->in, 16); byteReverse(ctx->in.u8, 16);
MD5Transform(ctx->buf, (uint32_t *) ctx->in); MD5Transform(ctx->buf, ctx->in.u32);
buf += t; buf += t;
len -= t; len -= t;
} }
/* Process data in 64-byte chunks */ /* Process data in 64-byte chunks */
while (len >= 64) { while (len >= 64) {
memcpy(ctx->in, buf, 64); memcpy(ctx->in.u8, buf, 64);
byteReverse(ctx->in, 16); byteReverse(ctx->in.u8, 16);
MD5Transform(ctx->buf, (uint32_t *) ctx->in); MD5Transform(ctx->buf, ctx->in.u32);
buf += 64; buf += 64;
len -= 64; len -= 64;
} }
/* Handle any remaining bytes of data. */ /* Handle any remaining bytes of data. */
memcpy(ctx->in, buf, len); memcpy(ctx->in.u8, buf, len);
} }
/* /*
...@@ -134,7 +134,7 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx) ...@@ -134,7 +134,7 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
/* Set the first char of padding to 0x80. This is safe since there is /* Set the first char of padding to 0x80. This is safe since there is
always at least one byte free */ always at least one byte free */
p = ctx->in + count; p = ctx->in.u8 + count;
*p++ = 0x80; *p++ = 0x80;
/* Bytes of padding needed to make 64 bytes */ /* Bytes of padding needed to make 64 bytes */
...@@ -144,22 +144,22 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx) ...@@ -144,22 +144,22 @@ void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
if (count < 8) { if (count < 8) {
/* Two lots of padding: Pad the first block to 64 bytes */ /* Two lots of padding: Pad the first block to 64 bytes */
memset(p, 0, count); memset(p, 0, count);
byteReverse(ctx->in, 16); byteReverse(ctx->in.u8, 16);
MD5Transform(ctx->buf, (uint32_t *) ctx->in); MD5Transform(ctx->buf, ctx->in.u32);
/* Now fill the next block with 56 bytes */ /* Now fill the next block with 56 bytes */
memset(ctx->in, 0, 56); memset(ctx->in.u8, 0, 56);
} else { } else {
/* Pad block to 56 bytes */ /* Pad block to 56 bytes */
memset(p, 0, count - 8); memset(p, 0, count - 8);
} }
byteReverse(ctx->in, 14); byteReverse(ctx->in.u8, 14);
/* Append length in bits and transform */ /* Append length in bits and transform */
((uint32_t *) ctx->in)[14] = ctx->bits[0]; ctx->in.u32[14] = ctx->bits[0];
((uint32_t *) ctx->in)[15] = ctx->bits[1]; ctx->in.u32[15] = ctx->bits[1];
MD5Transform(ctx->buf, (uint32_t *) ctx->in); MD5Transform(ctx->buf, ctx->in.u32);
byteReverse((unsigned char *) ctx->buf, 4); byteReverse((unsigned char *) ctx->buf, 4);
memcpy(digest, ctx->buf, 16); memcpy(digest, ctx->buf, 16);
memset(ctx, 0, sizeof(struct MD5Context)); /* In case it's sensitive */ memset(ctx, 0, sizeof(struct MD5Context)); /* In case it's sensitive */
......
...@@ -27,7 +27,10 @@ ...@@ -27,7 +27,10 @@
struct MD5Context { struct MD5Context {
uint32_t buf[4]; uint32_t buf[4];
uint32_t bits[2]; uint32_t bits[2];
uint8_t in[64]; union {
uint8_t u8[64];
uint32_t u32[16];
} in;
}; };
typedef struct MD5Context MD5_CTX; typedef struct MD5Context MD5_CTX;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment