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)
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)
77 /* Handle any leading odd-sized chunks */ 77 /* Handle any leading odd-sized chunks */
78 78
79 if (t) { 79 if (t) {
80 unsigned char *p = (unsigned char *) ctx->in + t; 80 unsigned char *p = ctx->in.s + t;
81 81
82 t = 64 - t; 82 t = 64 - t;
83 if (len < t) { 83 if (len < t) {
@@ -85,24 +85,24 @@ void MD5Update(MD5_CTX *ctx, unsigned char const *buf, unsigned len)
85 return; 85 return;
86 } 86 }
87 memcpy(p, buf, t); 87 memcpy(p, buf, t);
88 byteReverse(ctx->in, 16); 88 byteReverse(ctx->in.s, 16);
89 MD5Transform(ctx->buf, (uint32_t *) ctx->in); 89 MD5Transform(ctx->buf, ctx->in.i);
90 buf += t; 90 buf += t;
91 len -= t; 91 len -= t;
92 } 92 }
93 /* Process data in 64-byte chunks */ 93 /* Process data in 64-byte chunks */
94 94
95 while (len >= 64) { 95 while (len >= 64) {
96 memcpy(ctx->in, buf, 64); 96 memcpy(ctx->in.s, buf, 64);
97 byteReverse(ctx->in, 16); 97 byteReverse(ctx->in.s, 16);
98 MD5Transform(ctx->buf, (uint32_t *) ctx->in); 98 MD5Transform(ctx->buf, ctx->in.i);
99 buf += 64; 99 buf += 64;
100 len -= 64; 100 len -= 64;
101 } 101 }
102 102
103 /* Handle any remaining bytes of data. */ 103 /* Handle any remaining bytes of data. */
104 104
105 memcpy(ctx->in, buf, len); 105 memcpy(ctx->in.s, buf, len);
106} 106}
107 107
108/* 108/*
@@ -119,7 +119,7 @@ void MD5Final(unsigned char digest[16], MD5_CTX *ctx)
119 119
120 /* Set the first char of padding to 0x80. This is safe since there is 120 /* Set the first char of padding to 0x80. This is safe since there is
121 always at least one byte free */ 121 always at least one byte free */
122 p = ctx->in + count; 122 p = ctx->in.s + count;
123 *p++ = 0x80; 123 *p++ = 0x80;
124 124
125 /* Bytes of padding needed to make 64 bytes */ 125 /* Bytes of padding needed to make 64 bytes */
@@ -129,22 +129,22 @@ void MD5Final(unsigned char digest[16], MD5_CTX *ctx)
129 if (count < 8) { 129 if (count < 8) {
130 /* Two lots of padding: Pad the first block to 64 bytes */ 130 /* Two lots of padding: Pad the first block to 64 bytes */
131 memset(p, 0, count); 131 memset(p, 0, count);
132 byteReverse(ctx->in, 16); 132 byteReverse(ctx->in.s, 16);
133 MD5Transform(ctx->buf, (uint32_t *) ctx->in); 133 MD5Transform(ctx->buf, ctx->in.i);
134 134
135 /* Now fill the next block with 56 bytes */ 135 /* Now fill the next block with 56 bytes */
136 memset(ctx->in, 0, 56); 136 memset(ctx->in.s, 0, 56);
137 } else { 137 } else {
138 /* Pad block to 56 bytes */ 138 /* Pad block to 56 bytes */
139 memset(p, 0, count - 8); 139 memset(p, 0, count - 8);
140 } 140 }
141 byteReverse(ctx->in, 14); 141 byteReverse(ctx->in.s, 14);
142 142
143 /* Append length in bits and transform */ 143 /* Append length in bits and transform */
144 ((uint32_t *) ctx->in)[14] = ctx->bits[0]; 144 ctx->in.i[14] = ctx->bits[0];
145 ((uint32_t *) ctx->in)[15] = ctx->bits[1]; 145 ctx->in.i[15] = ctx->bits[1];
146 146
147 MD5Transform(ctx->buf, (uint32_t *) ctx->in); 147 MD5Transform(ctx->buf, ctx->in.i);
148 byteReverse((unsigned char *) ctx->buf, 4); 148 byteReverse((unsigned char *) ctx->buf, 4);
149 memcpy(digest, ctx->buf, 16); 149 memcpy(digest, ctx->buf, 16);
150 memset((char *) ctx, 0, sizeof(ctx)); /* In case it's sensitive */ 150 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 @@
9typedef struct MD5Context { 9typedef struct MD5Context {
10 uint32_t buf[4]; 10 uint32_t buf[4];
11 uint32_t bits[2]; 11 uint32_t bits[2];
12 unsigned char in[64]; 12 union
13 {
14 unsigned char s[64];
15 uint32_t i[16];
16 } in;
13} MD5_CTX; 17} MD5_CTX;
14 18
15extern void MD5Init(MD5_CTX *context); 19extern void MD5Init(MD5_CTX *context);