summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHenrique Dante de Almeida <hdante@profusion.mobi>2013-01-25 14:46:05 -0200
committerLucas De Marchi <lucas.demarchi@profusion.mobi>2013-02-26 10:17:28 -0300
commit95623dee10f47328b7b52ed9faf228d3fa209f8b (patch)
tree1f3e0b88bb7595e14328d2538e8ce59395693074
parent2656d7097e30c479d3457eb3785d6c70b38ea107 (diff)
downloadefl-95623dee10f47328b7b52ed9faf228d3fa209f8b.tar.gz
ethumb: Support strict aliasing rules in MD5 code
-rw-r--r--src/lib/ethumb/md5.c30
-rw-r--r--src/lib/ethumb/md5.h6
2 files changed, 20 insertions, 16 deletions
diff --git a/src/lib/ethumb/md5.c b/src/lib/ethumb/md5.c
index b62a9ffe7d..9cfc8b5791 100644
--- a/src/lib/ethumb/md5.c
+++ b/src/lib/ethumb/md5.c
@@ -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 */
diff --git a/src/lib/ethumb/md5.h b/src/lib/ethumb/md5.h
index 348fcd654b..cca8e8c84c 100644
--- a/src/lib/ethumb/md5.h
+++ b/src/lib/ethumb/md5.h
@@ -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);