ARMEBS4  revision-26.06.2015
hash_md5.c
Go to the documentation of this file.
1 /**
2  * \file heivs/hash_md5.c
3  * \see heivs/hash.h
4  *
5  * Reference code from RFC1321
6  */
7 
8 #include "heivs/hash_md5.h"
9 #include <stdint.h>
10 #include <string.h>
11 
12 struct MD5_CTX
13 {
14  uint32_t state[4];
15  uint32_t count[2];
16  uint8_t buffer[64];
17 };
18 
19 #define S11 7
20 #define S12 12
21 #define S13 17
22 #define S14 22
23 #define S21 5
24 #define S22 9
25 #define S23 14
26 #define S24 20
27 #define S31 4
28 #define S32 11
29 #define S33 16
30 #define S34 23
31 #define S41 6
32 #define S42 10
33 #define S43 15
34 #define S44 21
35 
36 static void MD5Transform(uint32_t [4], uint8_t [64]);
37 static void Encode(uint8_t *, uint32_t *, uint32_t);
38 static void Decode(uint32_t *, uint8_t *, uint32_t);
39 
40 static const uint8_t PADDING[64] = {
41  0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
42  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
44 };
45 
46 /* F, G, H and I are basic MD5 functions.
47  */
48 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
49 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
50 #define H(x, y, z) ((x) ^ (y) ^ (z))
51 #define I(x, y, z) ((y) ^ ((x) | (~z)))
52 
53 /* ROTATE_LEFT rotates x left n bits.
54  */
55 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
56 
57 /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
58 Rotation is separate from addition to prevent recomputation.
59  */
60 #define FF(a, b, c, d, x, s, ac) { \
61  (a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \
62  (a) = ROTATE_LEFT ((a), (s)); \
63  (a) += (b); \
64  }
65  #define GG(a, b, c, d, x, s, ac) { \
66  (a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \
67  (a) = ROTATE_LEFT ((a), (s)); \
68  (a) += (b); \
69  }
70  #define HH(a, b, c, d, x, s, ac) { \
71  (a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \
72  (a) = ROTATE_LEFT ((a), (s)); \
73  (a) += (b); \
74  }
75  #define II(a, b, c, d, x, s, ac) { \
76  (a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \
77  (a) = ROTATE_LEFT ((a), (s)); \
78  (a) += (b); \
79  }
80 
81 static status_e init(struct heivs_hash_t *hash, void *_state, size_t state_size)
82 {
83  struct MD5_CTX *context = (struct MD5_CTX *)_state;
84 
85  (void)hash;
86  (void)state_size;
87  context->count[0] = context->count[1] = 0;
88 
89  /* Load magic initialization constants.*/
90  context->state[0] = 0x67452301;
91  context->state[1] = 0xefcdab89;
92  context->state[2] = 0x98badcfe;
93  context->state[3] = 0x10325476;
94 
95  return NO_ERROR;
96 }
97 
98 /* MD5 block update operation. Continues an MD5 message-digest
99  operation, processing another message block, and updating the
100  context.
101  */
102 void MD5Update (context, input, inputLen)
103 struct MD5_CTX *context; /* context */
104 uint8_t *input; /* input block */
105 uint32_t inputLen; /* length of input block */
106 {
107  uint32_t i, index, partLen;
108 
109  /* Compute number of bytes mod 64 */
110  index = (uint32_t)((context->count[0] >> 3) & 0x3F);
111 
112  /* Update number of bits */
113  if ((context->count[0] += ((uint32_t)inputLen << 3))
114  < ((uint32_t)inputLen << 3))
115  context->count[1]++;
116  context->count[1] += ((uint32_t)inputLen >> 29);
117 
118  partLen = 64 - index;
119 
120  /* Transform as many times as possible.
121 */
122  if (inputLen >= partLen) {
123  memcpy
124  ((void *)&context->buffer[index], (void *)input, partLen);
125  MD5Transform (context->state, context->buffer);
126 
127  for (i = partLen; i + 63 < inputLen; i += 64)
128  MD5Transform (context->state, &input[i]);
129 
130  index = 0;
131  }
132  else
133  i = 0;
134 
135  /* Buffer remaining input */
136  memcpy
137  ((void *)&context->buffer[index], (void *)&input[i],
138  inputLen-i);
139 }
140 
141 
142 static status_e run(struct heivs_hash_t *hash, void *_state, const void *_data, size_t size)
143 {
144  struct MD5_CTX *context = (struct MD5_CTX *)_state;
145 
146  (void)hash;
147  MD5Update(context, (uint8_t*)_data, size);
148 
149  return NO_ERROR;
150 }
151 
152 static status_e finish(struct heivs_hash_t *hash, void *_state)
153 {
154  struct MD5_CTX *context = (struct MD5_CTX *)_state;
155 
156  uint8_t bits[8];
157  uint32_t index, padLen;
158  uint8_t digest[128/8];
159 
160  (void)hash;
161  /* Save number of bits */
162  Encode (bits, context->count, 8);
163 
164  /* Pad out to 56 mod 64.
165  */
166  index = (uint32_t)((context->count[0] >> 3) & 0x3f);
167  padLen = (index < 56) ? (56 - index) : (120 - index);
168  MD5Update(context, PADDING, padLen);
169 
170  /* Append length (before padding) */
171  MD5Update(context, bits, 8);
172 
173  /* Store state in digest */
174  Encode(digest, context->state, 16);
175 
176  /* Zeroize sensitive information.
177  */
178  memset((void *)context, 0, sizeof (*context));
179 
180  memcpy(context, digest, sizeof(digest));
181 
182  return NO_ERROR;
183 }
184 
185 static void MD5Transform (uint32_t state[4], uint8_t block[64])
186 {
187  uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
188 
189  Decode (x, block, 64);
190 
191  /* Round 1 */
192  FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
193  FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
194  FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
195  FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
196  FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
197  FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
198  FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
199  FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
200  FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
201  FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
202  FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
203  FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
204  FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
205  FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
206  FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
207  FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
208 
209  /* Round 2 */
210  GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
211  GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
212  GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
213  GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
214  GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
215  GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
216  GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
217  GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
218  GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
219  GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
220  GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
221  GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
222  GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
223  GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
224  GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
225  GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
226 
227  /* Round 3 */
228  HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
229  HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
230  HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
231  HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
232  HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
233  HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
234  HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
235  HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
236  HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
237  HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
238  HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
239  HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
240  HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
241  HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
242  HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
243  HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
244 
245  /* Round 4 */
246  II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
247  II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
248  II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
249  II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
250  II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
251  II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
252  II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
253  II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
254  II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
255  II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
256  II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
257  II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
258  II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
259  II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
260  II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
261  II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
262 
263  state[0] += a;
264  state[1] += b;
265  state[2] += c;
266  state[3] += d;
267 
268  /* Zeroize sensitive information. */
269  memset(x, 0, sizeof (x));
270 }
271 
272 static void Encode (output, input, len)
273 uint8_t *output;
274 uint32_t *input;
275 uint32_t len;
276 {
277  uint32_t i, j;
278 
279  for (i = 0, j = 0; j < len; i++, j += 4) {
280  output[j] = (uint8_t)(input[i] & 0xff);
281  output[j+1] = (uint8_t)((input[i] >> 8) & 0xff);
282  output[j+2] = (uint8_t)((input[i] >> 16) & 0xff);
283  output[j+3] = (uint8_t)((input[i] >> 24) & 0xff);
284  }
285 }
286 
287 /* Decodes input (uint8_t) into output (uint32_t). Assumes len is
288  a multiple of 4.
289  */
290 static void Decode (output, input, len)
291 uint32_t *output;
292 uint8_t *input;
293 uint32_t len;
294 {
295  uint32_t i, j;
296 
297  for (i = 0, j = 0; j < len; i++, j += 4)
298  output[i] = ((uint32_t)input[j]) | (((uint32_t)input[j+1]) << 8) |
299  (((uint32_t)input[j+2]) << 16) | (((uint32_t)input[j+3]) << 24);
300 }
301 
302 struct heivs_hash_t hash_md5 =
303 {
304  .name = "hash_md5",
305  .init = init,
306  .run = run,
307  .finish = finish,
308 
309  .state_size = sizeof(struct MD5_CTX),
310  .hash_size = 128/8,
311 };
void * memcpy(void *dest, const void *src, size_t n)
void * memset(void *dest, int n, size_t n)
status_e
Known errors.
Definition: error.h:21
No error.
Definition: error.h:28