md5.c
Go to the documentation of this file.
1 /**
2  * @file md5.c
3  * @brief MD5 (Message-Digest Algorithm)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneCrypto Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @section Description
28  *
29  * The MD5 algorithm takes as input a message of arbitrary length and produces
30  * as output a 128-bit message digest of the input. Refer to RFC 1321
31  *
32  * @author Oryx Embedded SARL (www.oryx-embedded.com)
33  * @version 1.9.6
34  **/
35 
36 //Switch to the appropriate trace level
37 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
38 
39 //Dependencies
40 #include "core/crypto.h"
41 #include "hash/md5.h"
42 
43 //Check crypto library configuration
44 #if (MD5_SUPPORT == ENABLED)
45 
46 //MD5 auxiliary functions
47 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
48 #define G(x, y, z) (((x) & (z)) | ((y) & ~(z)))
49 #define H(x, y, z) ((x) ^ (y) ^ (z))
50 #define I(x, y, z) ((y) ^ ((x) | ~(z)))
51 
52 #define FF(a, b, c, d, x, s, k) a += F(b, c, d) + (x) + (k), a = ROL32(a, s) + (b)
53 #define GG(a, b, c, d, x, s, k) a += G(b, c, d) + (x) + (k), a = ROL32(a, s) + (b)
54 #define HH(a, b, c, d, x, s, k) a += H(b, c, d) + (x) + (k), a = ROL32(a, s) + (b)
55 #define II(a, b, c, d, x, s, k) a += I(b, c, d) + (x) + (k), a = ROL32(a, s) + (b)
56 
57 //MD5 padding
58 static const uint8_t padding[64] =
59 {
60  0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
61  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
62  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
63  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
64 };
65 
66 //MD5 constants
67 static const uint32_t k[64] =
68 {
69  0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE, 0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501,
70  0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE, 0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821,
71  0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA, 0xD62F105D, 0x02441453, 0xD8A1E681, 0xE7D3FBC8,
72  0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED, 0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A,
73  0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C, 0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70,
74  0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x04881D05, 0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665,
75  0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039, 0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1,
76  0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1, 0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391
77 };
78 
79 //MD5 object identifier (1.2.840.113549.2.5)
80 const uint8_t md5Oid[8] = {0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x02, 0x05};
81 
82 //Common interface for hash algorithms
84 {
85  "MD5",
86  md5Oid,
87  sizeof(md5Oid),
88  sizeof(Md5Context),
92  FALSE,
98 };
99 
100 
101 /**
102  * @brief Digest a message using MD5
103  * @param[in] data Pointer to the message being hashed
104  * @param[in] length Length of the message
105  * @param[out] digest Pointer to the calculated digest
106  * @return Error code
107  **/
108 
109 error_t md5Compute(const void *data, size_t length, uint8_t *digest)
110 {
111  //Allocate a memory buffer to hold the MD5 context
112  Md5Context *context = cryptoAllocMem(sizeof(Md5Context));
113  //Failed to allocate memory?
114  if(context == NULL)
115  return ERROR_OUT_OF_MEMORY;
116 
117  //Initialize the MD5 context
118  md5Init(context);
119  //Digest the message
120  md5Update(context, data, length);
121  //Finalize the MD5 message digest
122  md5Final(context, digest);
123 
124  //Free previously allocated memory
125  cryptoFreeMem(context);
126  //Successful processing
127  return NO_ERROR;
128 }
129 
130 
131 /**
132  * @brief Initialize MD5 message digest context
133  * @param[in] context Pointer to the MD5 context to initialize
134  **/
135 
136 void md5Init(Md5Context *context)
137 {
138  //Set initial hash value
139  context->h[0] = 0x67452301;
140  context->h[1] = 0xEFCDAB89;
141  context->h[2] = 0x98BADCFE;
142  context->h[3] = 0x10325476;
143 
144  //Number of bytes in the buffer
145  context->size = 0;
146  //Total length of the message
147  context->totalSize = 0;
148 }
149 
150 
151 /**
152  * @brief Update the MD5 context with a portion of the message being hashed
153  * @param[in] context Pointer to the MD5 context
154  * @param[in] data Pointer to the buffer being hashed
155  * @param[in] length Length of the buffer
156  **/
157 
158 void md5Update(Md5Context *context, const void *data, size_t length)
159 {
160  size_t n;
161 
162  //Process the incoming data
163  while(length > 0)
164  {
165  //The buffer can hold at most 64 bytes
166  n = MIN(length, 64 - context->size);
167 
168  //Copy the data to the buffer
169  cryptoMemcpy(context->buffer + context->size, data, n);
170 
171  //Update the MD5 context
172  context->size += n;
173  context->totalSize += n;
174  //Advance the data pointer
175  data = (uint8_t *) data + n;
176  //Remaining bytes to process
177  length -= n;
178 
179  //Process message in 16-word blocks
180  if(context->size == 64)
181  {
182  //Transform the 16-word block
183  md5ProcessBlock(context);
184  //Empty the buffer
185  context->size = 0;
186  }
187  }
188 }
189 
190 
191 /**
192  * @brief Finish the MD5 message digest
193  * @param[in] context Pointer to the MD5 context
194  * @param[out] digest Calculated digest (optional parameter)
195  **/
196 
197 void md5Final(Md5Context *context, uint8_t *digest)
198 {
199  uint_t i;
200  size_t paddingSize;
201  uint64_t totalSize;
202 
203  //Length of the original message (before padding)
204  totalSize = context->totalSize * 8;
205 
206  //Pad the message so that its length is congruent to 56 modulo 64
207  if(context->size < 56)
208  paddingSize = 56 - context->size;
209  else
210  paddingSize = 64 + 56 - context->size;
211 
212  //Append padding
213  md5Update(context, padding, paddingSize);
214 
215  //Append the length of the original message
216  context->x[14] = htole32((uint32_t) totalSize);
217  context->x[15] = htole32((uint32_t) (totalSize >> 32));
218 
219  //Calculate the message digest
220  md5ProcessBlock(context);
221 
222  //Convert from host byte order to little-endian byte order
223  for(i = 0; i < 4; i++)
224  {
225  context->h[i] = htole32(context->h[i]);
226  }
227 
228  //Copy the resulting digest
229  if(digest != NULL)
230  cryptoMemcpy(digest, context->digest, MD5_DIGEST_SIZE);
231 }
232 
233 
234 /**
235  * @brief Finish the MD5 message digest (no padding is added)
236  * @param[in] context Pointer to the MD5 context
237  * @param[out] digest Calculated digest
238  **/
239 
240 void md5FinalRaw(Md5Context *context, uint8_t *digest)
241 {
242  uint_t i;
243 
244  //Convert from host byte order to little-endian byte order
245  for(i = 0; i < 4; i++)
246  {
247  context->h[i] = htole32(context->h[i]);
248  }
249 
250  //Copy the resulting digest
251  cryptoMemcpy(digest, context->digest, MD5_DIGEST_SIZE);
252 
253  //Convert from little-endian byte order to host byte order
254  for(i = 0; i < 4; i++)
255  {
256  context->h[i] = letoh32(context->h[i]);
257  }
258 }
259 
260 
261 /**
262  * @brief Process message in 16-word blocks
263  * @param[in] context Pointer to the MD5 context
264  **/
265 
267 {
268  uint_t i;
269 
270  //Initialize the 4 working registers
271  uint32_t a = context->h[0];
272  uint32_t b = context->h[1];
273  uint32_t c = context->h[2];
274  uint32_t d = context->h[3];
275 
276  //Process message in 16-word blocks
277  uint32_t *x = context->x;
278 
279  //Convert from little-endian byte order to host byte order
280  for(i = 0; i < 16; i++)
281  {
282  x[i] = letoh32(x[i]);
283  }
284 
285  //Round 1
286  FF(a, b, c, d, x[0], 7, k[0]);
287  FF(d, a, b, c, x[1], 12, k[1]);
288  FF(c, d, a, b, x[2], 17, k[2]);
289  FF(b, c, d, a, x[3], 22, k[3]);
290  FF(a, b, c, d, x[4], 7, k[4]);
291  FF(d, a, b, c, x[5], 12, k[5]);
292  FF(c, d, a, b, x[6], 17, k[6]);
293  FF(b, c, d, a, x[7], 22, k[7]);
294  FF(a, b, c, d, x[8], 7, k[8]);
295  FF(d, a, b, c, x[9], 12, k[9]);
296  FF(c, d, a, b, x[10], 17, k[10]);
297  FF(b, c, d, a, x[11], 22, k[11]);
298  FF(a, b, c, d, x[12], 7, k[12]);
299  FF(d, a, b, c, x[13], 12, k[13]);
300  FF(c, d, a, b, x[14], 17, k[14]);
301  FF(b, c, d, a, x[15], 22, k[15]);
302 
303  //Round 2
304  GG(a, b, c, d, x[1], 5, k[16]);
305  GG(d, a, b, c, x[6], 9, k[17]);
306  GG(c, d, a, b, x[11], 14, k[18]);
307  GG(b, c, d, a, x[0], 20, k[19]);
308  GG(a, b, c, d, x[5], 5, k[20]);
309  GG(d, a, b, c, x[10], 9, k[21]);
310  GG(c, d, a, b, x[15], 14, k[22]);
311  GG(b, c, d, a, x[4], 20, k[23]);
312  GG(a, b, c, d, x[9], 5, k[24]);
313  GG(d, a, b, c, x[14], 9, k[25]);
314  GG(c, d, a, b, x[3], 14, k[26]);
315  GG(b, c, d, a, x[8], 20, k[27]);
316  GG(a, b, c, d, x[13], 5, k[28]);
317  GG(d, a, b, c, x[2], 9, k[29]);
318  GG(c, d, a, b, x[7], 14, k[30]);
319  GG(b, c, d, a, x[12], 20, k[31]);
320 
321  //Round 3
322  HH(a, b, c, d, x[5], 4, k[32]);
323  HH(d, a, b, c, x[8], 11, k[33]);
324  HH(c, d, a, b, x[11], 16, k[34]);
325  HH(b, c, d, a, x[14], 23, k[35]);
326  HH(a, b, c, d, x[1], 4, k[36]);
327  HH(d, a, b, c, x[4], 11, k[37]);
328  HH(c, d, a, b, x[7], 16, k[38]);
329  HH(b, c, d, a, x[10], 23, k[39]);
330  HH(a, b, c, d, x[13], 4, k[40]);
331  HH(d, a, b, c, x[0], 11, k[41]);
332  HH(c, d, a, b, x[3], 16, k[42]);
333  HH(b, c, d, a, x[6], 23, k[43]);
334  HH(a, b, c, d, x[9], 4, k[44]);
335  HH(d, a, b, c, x[12], 11, k[45]);
336  HH(c, d, a, b, x[15], 16, k[46]);
337  HH(b, c, d, a, x[2], 23, k[47]);
338 
339  //Round 4
340  II(a, b, c, d, x[0], 6, k[48]);
341  II(d, a, b, c, x[7], 10, k[49]);
342  II(c, d, a, b, x[14], 15, k[50]);
343  II(b, c, d, a, x[5], 21, k[51]);
344  II(a, b, c, d, x[12], 6, k[52]);
345  II(d, a, b, c, x[3], 10, k[53]);
346  II(c, d, a, b, x[10], 15, k[54]);
347  II(b, c, d, a, x[1], 21, k[55]);
348  II(a, b, c, d, x[8], 6, k[56]);
349  II(d, a, b, c, x[15], 10, k[57]);
350  II(c, d, a, b, x[6], 15, k[58]);
351  II(b, c, d, a, x[13], 21, k[59]);
352  II(a, b, c, d, x[4], 6, k[60]);
353  II(d, a, b, c, x[11], 10, k[61]);
354  II(c, d, a, b, x[2], 15, k[62]);
355  II(b, c, d, a, x[9], 21, k[63]);
356 
357  //Update the hash value
358  context->h[0] += a;
359  context->h[1] += b;
360  context->h[2] += c;
361  context->h[3] += d;
362 }
363 
364 #endif
uint8_t length
Definition: dtls_misc.h:149
uint8_t digest[16]
Definition: md5.h:63
void(* HashAlgoInit)(void *context)
Definition: crypto.h:1094
const HashAlgo md5HashAlgo
Definition: md5.c:83
uint8_t a
Definition: ndp.h:410
#define GG(a, b, c, d, x, s, k)
Definition: md5.c:53
uint8_t b[6]
Definition: dtls_misc.h:139
#define HH(a, b, c, d, x, s, k)
Definition: md5.c:54
#define MD5_BLOCK_SIZE
Definition: md5.h:38
void md5Init(Md5Context *context)
Initialize MD5 message digest context.
Definition: md5.c:136
@ ERROR_OUT_OF_MEMORY
Definition: error.h:63
error_t md5Compute(const void *data, size_t length, uint8_t *digest)
Digest a message using MD5.
Definition: md5.c:109
#define MD5_MIN_PAD_SIZE
Definition: md5.h:42
void(* HashAlgoUpdate)(void *context, const void *data, size_t length)
Definition: crypto.h:1095
void md5Final(Md5Context *context, uint8_t *digest)
Finish the MD5 message digest.
Definition: md5.c:197
uint32_t x[16]
Definition: md5.h:67
#define letoh32(value)
Definition: cpu_endian.h:414
uint8_t buffer[64]
Definition: md5.h:68
void md5Update(Md5Context *context, const void *data, size_t length)
Update the MD5 context with a portion of the message being hashed.
Definition: md5.c:158
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:42
#define htole32(value)
Definition: cpu_endian.h:406
General definitions for cryptographic algorithms.
MD5 algorithm context.
Definition: md5.h:58
#define II(a, b, c, d, x, s, k)
Definition: md5.c:55
#define MIN(a, b)
Definition: os_port.h:62
uint64_t totalSize
Definition: md5.h:71
#define MD5_DIGEST_SIZE
Definition: md5.h:40
void(* HashAlgoFinal)(void *context, uint8_t *digest)
Definition: crypto.h:1096
void md5ProcessBlock(Md5Context *context)
Process message in 16-word blocks.
Definition: md5.c:266
const uint8_t md5Oid[8]
Definition: md5.c:80
uint8_t n
MD5 (Message-Digest Algorithm)
#define cryptoMemcpy(dest, src, length)
Definition: crypto.h:642
#define cryptoFreeMem(p)
Definition: crypto.h:630
uint32_t totalSize
#define FF(a, b, c, d, x, s, k)
Definition: md5.c:52
size_t size
Definition: md5.h:70
#define cryptoAllocMem(size)
Definition: crypto.h:625
Common interface for hash algorithms.
Definition: crypto.h:1128
unsigned int uint_t
Definition: compiler_port.h:45
uint8_t data[]
Definition: dtls_misc.h:176
error_t(* HashAlgoCompute)(const void *data, size_t length, uint8_t *digest)
Definition: crypto.h:1093
void(* HashAlgoFinalRaw)(void *context, uint8_t *digest)
Definition: crypto.h:1097
uint32_t h[4]
Definition: md5.h:62
@ NO_ERROR
Success.
Definition: error.h:44
void md5FinalRaw(Md5Context *context, uint8_t *digest)
Finish the MD5 message digest (no padding is added)
Definition: md5.c:240
uint8_t c
Definition: ndp.h:513