source: UsbWattMeter/trunk/wolfssl-3.7.0/wolfcrypt/src/md5.c@ 164

Last change on this file since 164 was 164, checked in by coas-nagasima, 6 years ago

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 13.1 KB
Line 
1/* md5.c
2 *
3 * Copyright (C) 2006-2015 wolfSSL Inc.
4 *
5 * This file is part of wolfSSL. (formerly known as CyaSSL)
6 *
7 * wolfSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * wolfSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22
23#ifdef HAVE_CONFIG_H
24 #include <config.h>
25#endif
26
27#include <wolfssl/wolfcrypt/settings.h>
28
29#if !defined(NO_MD5)
30
31#if defined(WOLFSSL_TI_HASH)
32 /* #include <wolfcrypt/src/port/ti/ti-hash.c> included by wc_port.c */
33#else
34
35#ifdef WOLFSSL_PIC32MZ_HASH
36#define wc_InitMd5 wc_InitMd5_sw
37#define wc_Md5Update wc_Md5Update_sw
38#define wc_Md5Final wc_Md5Final_sw
39#endif
40
41#include <wolfssl/wolfcrypt/md5.h>
42#include <wolfssl/wolfcrypt/error-crypt.h>
43
44#ifdef NO_INLINE
45 #include <wolfssl/wolfcrypt/misc.h>
46#else
47 #include <wolfcrypt/src/misc.c>
48#endif
49
50#ifdef FREESCALE_MMCAU
51 #include "cau_api.h"
52 #define XTRANSFORM(S,B) Transform((S), (B))
53#else
54 #define XTRANSFORM(S,B) Transform((S))
55#endif
56
57
58#ifdef STM32F2_HASH
59 /*
60 * STM32F2 hardware MD5 support through the STM32F2 standard peripheral
61 * library. Documentation located in STM32F2xx Standard Peripheral Library
62 * document (See note in README).
63 */
64 #include "stm32f2xx.h"
65
66 void wc_InitMd5(Md5* md5)
67 {
68 /* STM32F2 struct notes:
69 * md5->buffer = first 4 bytes used to hold partial block if needed
70 * md5->buffLen = num bytes currently stored in md5->buffer
71 * md5->loLen = num bytes that have been written to STM32 FIFO
72 */
73 XMEMSET(md5->buffer, 0, MD5_REG_SIZE);
74
75 md5->buffLen = 0;
76 md5->loLen = 0;
77
78 /* initialize HASH peripheral */
79 HASH_DeInit();
80
81 /* configure algo used, algo mode, datatype */
82 HASH->CR &= ~ (HASH_CR_ALGO | HASH_CR_DATATYPE | HASH_CR_MODE);
83 HASH->CR |= (HASH_AlgoSelection_MD5 | HASH_AlgoMode_HASH
84 | HASH_DataType_8b);
85
86 /* reset HASH processor */
87 HASH->CR |= HASH_CR_INIT;
88 }
89
90 void wc_Md5Update(Md5* md5, const byte* data, word32 len)
91 {
92 word32 i = 0;
93 word32 fill = 0;
94 word32 diff = 0;
95
96 /* if saved partial block is available */
97 if (md5->buffLen > 0) {
98 fill = 4 - md5->buffLen;
99
100 /* if enough data to fill, fill and push to FIFO */
101 if (fill <= len) {
102 XMEMCPY((byte*)md5->buffer + md5->buffLen, data, fill);
103 HASH_DataIn(*(uint32_t*)md5->buffer);
104
105 data += fill;
106 len -= fill;
107 md5->loLen += 4;
108 md5->buffLen = 0;
109 } else {
110 /* append partial to existing stored block */
111 XMEMCPY((byte*)md5->buffer + md5->buffLen, data, len);
112 md5->buffLen += len;
113 return;
114 }
115 }
116
117 /* write input block in the IN FIFO */
118 for (i = 0; i < len; i += 4)
119 {
120 diff = len - i;
121 if (diff < 4) {
122 /* store incomplete last block, not yet in FIFO */
123 XMEMSET(md5->buffer, 0, MD5_REG_SIZE);
124 XMEMCPY((byte*)md5->buffer, data, diff);
125 md5->buffLen = diff;
126 } else {
127 HASH_DataIn(*(uint32_t*)data);
128 data+=4;
129 }
130 }
131
132 /* keep track of total data length thus far */
133 md5->loLen += (len - md5->buffLen);
134 }
135
136 void wc_Md5Final(Md5* md5, byte* hash)
137 {
138 __IO uint16_t nbvalidbitsdata = 0;
139
140 /* finish reading any trailing bytes into FIFO */
141 if (md5->buffLen > 0) {
142 HASH_DataIn(*(uint32_t*)md5->buffer);
143 md5->loLen += md5->buffLen;
144 }
145
146 /* calculate number of valid bits in last word of input data */
147 nbvalidbitsdata = 8 * (md5->loLen % MD5_REG_SIZE);
148
149 /* configure number of valid bits in last word of the data */
150 HASH_SetLastWordValidBitsNbr(nbvalidbitsdata);
151
152 /* start HASH processor */
153 HASH_StartDigest();
154
155 /* wait until Busy flag == RESET */
156 while (HASH_GetFlagStatus(HASH_FLAG_BUSY) != RESET) {}
157
158 /* read message digest */
159 md5->digest[0] = HASH->HR[0];
160 md5->digest[1] = HASH->HR[1];
161 md5->digest[2] = HASH->HR[2];
162 md5->digest[3] = HASH->HR[3];
163
164 ByteReverseWords(md5->digest, md5->digest, MD5_DIGEST_SIZE);
165
166 XMEMCPY(hash, md5->digest, MD5_DIGEST_SIZE);
167
168 wc_InitMd5(md5); /* reset state */
169 }
170
171#else /* CTaoCrypt software implementation */
172
173#ifdef min
174#define WOLFSSL_HAVE_MIN
175#endif
176#ifndef WOLFSSL_HAVE_MIN
177#define WOLFSSL_HAVE_MIN
178
179 static INLINE word32 min(word32 a, word32 b)
180 {
181 return a > b ? b : a;
182 }
183
184#endif /* WOLFSSL_HAVE_MIN */
185
186void wc_InitMd5(Md5* md5)
187{
188 md5->digest[0] = 0x67452301L;
189 md5->digest[1] = 0xefcdab89L;
190 md5->digest[2] = 0x98badcfeL;
191 md5->digest[3] = 0x10325476L;
192
193 md5->buffLen = 0;
194 md5->loLen = 0;
195 md5->hiLen = 0;
196}
197
198#ifdef FREESCALE_MMCAU
199static int Transform(Md5* md5, byte* data)
200{
201 int ret = wolfSSL_CryptHwMutexLock();
202 if(ret == 0) {
203 cau_md5_hash_n(data, 1, (unsigned char*)md5->digest);
204 wolfSSL_CryptHwMutexUnLock();
205 }
206 return ret;
207}
208#endif /* FREESCALE_MMCAU */
209
210#ifndef FREESCALE_MMCAU
211
212static void Transform(Md5* md5)
213{
214#define F1(x, y, z) (z ^ (x & (y ^ z)))
215#define F2(x, y, z) F1(z, x, y)
216#define F3(x, y, z) (x ^ y ^ z)
217#define F4(x, y, z) (y ^ (x | ~z))
218
219#define MD5STEP(f, w, x, y, z, data, s) \
220 w = rotlFixed(w + f(x, y, z) + data, s) + x
221
222 /* Copy context->state[] to working vars */
223 word32 a = md5->digest[0];
224 word32 b = md5->digest[1];
225 word32 c = md5->digest[2];
226 word32 d = md5->digest[3];
227
228 MD5STEP(F1, a, b, c, d, md5->buffer[0] + 0xd76aa478, 7);
229 MD5STEP(F1, d, a, b, c, md5->buffer[1] + 0xe8c7b756, 12);
230 MD5STEP(F1, c, d, a, b, md5->buffer[2] + 0x242070db, 17);
231 MD5STEP(F1, b, c, d, a, md5->buffer[3] + 0xc1bdceee, 22);
232 MD5STEP(F1, a, b, c, d, md5->buffer[4] + 0xf57c0faf, 7);
233 MD5STEP(F1, d, a, b, c, md5->buffer[5] + 0x4787c62a, 12);
234 MD5STEP(F1, c, d, a, b, md5->buffer[6] + 0xa8304613, 17);
235 MD5STEP(F1, b, c, d, a, md5->buffer[7] + 0xfd469501, 22);
236 MD5STEP(F1, a, b, c, d, md5->buffer[8] + 0x698098d8, 7);
237 MD5STEP(F1, d, a, b, c, md5->buffer[9] + 0x8b44f7af, 12);
238 MD5STEP(F1, c, d, a, b, md5->buffer[10] + 0xffff5bb1, 17);
239 MD5STEP(F1, b, c, d, a, md5->buffer[11] + 0x895cd7be, 22);
240 MD5STEP(F1, a, b, c, d, md5->buffer[12] + 0x6b901122, 7);
241 MD5STEP(F1, d, a, b, c, md5->buffer[13] + 0xfd987193, 12);
242 MD5STEP(F1, c, d, a, b, md5->buffer[14] + 0xa679438e, 17);
243 MD5STEP(F1, b, c, d, a, md5->buffer[15] + 0x49b40821, 22);
244
245 MD5STEP(F2, a, b, c, d, md5->buffer[1] + 0xf61e2562, 5);
246 MD5STEP(F2, d, a, b, c, md5->buffer[6] + 0xc040b340, 9);
247 MD5STEP(F2, c, d, a, b, md5->buffer[11] + 0x265e5a51, 14);
248 MD5STEP(F2, b, c, d, a, md5->buffer[0] + 0xe9b6c7aa, 20);
249 MD5STEP(F2, a, b, c, d, md5->buffer[5] + 0xd62f105d, 5);
250 MD5STEP(F2, d, a, b, c, md5->buffer[10] + 0x02441453, 9);
251 MD5STEP(F2, c, d, a, b, md5->buffer[15] + 0xd8a1e681, 14);
252 MD5STEP(F2, b, c, d, a, md5->buffer[4] + 0xe7d3fbc8, 20);
253 MD5STEP(F2, a, b, c, d, md5->buffer[9] + 0x21e1cde6, 5);
254 MD5STEP(F2, d, a, b, c, md5->buffer[14] + 0xc33707d6, 9);
255 MD5STEP(F2, c, d, a, b, md5->buffer[3] + 0xf4d50d87, 14);
256 MD5STEP(F2, b, c, d, a, md5->buffer[8] + 0x455a14ed, 20);
257 MD5STEP(F2, a, b, c, d, md5->buffer[13] + 0xa9e3e905, 5);
258 MD5STEP(F2, d, a, b, c, md5->buffer[2] + 0xfcefa3f8, 9);
259 MD5STEP(F2, c, d, a, b, md5->buffer[7] + 0x676f02d9, 14);
260 MD5STEP(F2, b, c, d, a, md5->buffer[12] + 0x8d2a4c8a, 20);
261
262 MD5STEP(F3, a, b, c, d, md5->buffer[5] + 0xfffa3942, 4);
263 MD5STEP(F3, d, a, b, c, md5->buffer[8] + 0x8771f681, 11);
264 MD5STEP(F3, c, d, a, b, md5->buffer[11] + 0x6d9d6122, 16);
265 MD5STEP(F3, b, c, d, a, md5->buffer[14] + 0xfde5380c, 23);
266 MD5STEP(F3, a, b, c, d, md5->buffer[1] + 0xa4beea44, 4);
267 MD5STEP(F3, d, a, b, c, md5->buffer[4] + 0x4bdecfa9, 11);
268 MD5STEP(F3, c, d, a, b, md5->buffer[7] + 0xf6bb4b60, 16);
269 MD5STEP(F3, b, c, d, a, md5->buffer[10] + 0xbebfbc70, 23);
270 MD5STEP(F3, a, b, c, d, md5->buffer[13] + 0x289b7ec6, 4);
271 MD5STEP(F3, d, a, b, c, md5->buffer[0] + 0xeaa127fa, 11);
272 MD5STEP(F3, c, d, a, b, md5->buffer[3] + 0xd4ef3085, 16);
273 MD5STEP(F3, b, c, d, a, md5->buffer[6] + 0x04881d05, 23);
274 MD5STEP(F3, a, b, c, d, md5->buffer[9] + 0xd9d4d039, 4);
275 MD5STEP(F3, d, a, b, c, md5->buffer[12] + 0xe6db99e5, 11);
276 MD5STEP(F3, c, d, a, b, md5->buffer[15] + 0x1fa27cf8, 16);
277 MD5STEP(F3, b, c, d, a, md5->buffer[2] + 0xc4ac5665, 23);
278
279 MD5STEP(F4, a, b, c, d, md5->buffer[0] + 0xf4292244, 6);
280 MD5STEP(F4, d, a, b, c, md5->buffer[7] + 0x432aff97, 10);
281 MD5STEP(F4, c, d, a, b, md5->buffer[14] + 0xab9423a7, 15);
282 MD5STEP(F4, b, c, d, a, md5->buffer[5] + 0xfc93a039, 21);
283 MD5STEP(F4, a, b, c, d, md5->buffer[12] + 0x655b59c3, 6);
284 MD5STEP(F4, d, a, b, c, md5->buffer[3] + 0x8f0ccc92, 10);
285 MD5STEP(F4, c, d, a, b, md5->buffer[10] + 0xffeff47d, 15);
286 MD5STEP(F4, b, c, d, a, md5->buffer[1] + 0x85845dd1, 21);
287 MD5STEP(F4, a, b, c, d, md5->buffer[8] + 0x6fa87e4f, 6);
288 MD5STEP(F4, d, a, b, c, md5->buffer[15] + 0xfe2ce6e0, 10);
289 MD5STEP(F4, c, d, a, b, md5->buffer[6] + 0xa3014314, 15);
290 MD5STEP(F4, b, c, d, a, md5->buffer[13] + 0x4e0811a1, 21);
291 MD5STEP(F4, a, b, c, d, md5->buffer[4] + 0xf7537e82, 6);
292 MD5STEP(F4, d, a, b, c, md5->buffer[11] + 0xbd3af235, 10);
293 MD5STEP(F4, c, d, a, b, md5->buffer[2] + 0x2ad7d2bb, 15);
294 MD5STEP(F4, b, c, d, a, md5->buffer[9] + 0xeb86d391, 21);
295
296 /* Add the working vars back into digest state[] */
297 md5->digest[0] += a;
298 md5->digest[1] += b;
299 md5->digest[2] += c;
300 md5->digest[3] += d;
301}
302
303#endif /* FREESCALE_MMCAU */
304
305
306static INLINE void AddLength(Md5* md5, word32 len)
307{
308 word32 tmp = md5->loLen;
309 if ( (md5->loLen += len) < tmp)
310 md5->hiLen++; /* carry low to high */
311}
312
313
314void wc_Md5Update(Md5* md5, const byte* data, word32 len)
315{
316 /* do block size increments */
317 byte* local = (byte*)md5->buffer;
318
319 while (len) {
320 word32 add = min(len, MD5_BLOCK_SIZE - md5->buffLen);
321 XMEMCPY(&local[md5->buffLen], data, add);
322
323 md5->buffLen += add;
324 data += add;
325 len -= add;
326
327 if (md5->buffLen == MD5_BLOCK_SIZE) {
328 #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
329 ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE);
330 #endif
331 XTRANSFORM(md5, local);
332 AddLength(md5, MD5_BLOCK_SIZE);
333 md5->buffLen = 0;
334 }
335 }
336}
337
338
339void wc_Md5Final(Md5* md5, byte* hash)
340{
341 byte* local = (byte*)md5->buffer;
342
343 AddLength(md5, md5->buffLen); /* before adding pads */
344
345 local[md5->buffLen++] = 0x80; /* add 1 */
346
347 /* pad with zeros */
348 if (md5->buffLen > MD5_PAD_SIZE) {
349 XMEMSET(&local[md5->buffLen], 0, MD5_BLOCK_SIZE - md5->buffLen);
350 md5->buffLen += MD5_BLOCK_SIZE - md5->buffLen;
351
352 #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
353 ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE);
354 #endif
355 XTRANSFORM(md5, local);
356 md5->buffLen = 0;
357 }
358 XMEMSET(&local[md5->buffLen], 0, MD5_PAD_SIZE - md5->buffLen);
359
360 /* put lengths in bits */
361 md5->hiLen = (md5->loLen >> (8*sizeof(md5->loLen) - 3)) +
362 (md5->hiLen << 3);
363 md5->loLen = md5->loLen << 3;
364
365 /* store lengths */
366 #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU)
367 ByteReverseWords(md5->buffer, md5->buffer, MD5_BLOCK_SIZE);
368 #endif
369 /* ! length ordering dependent on digest endian type ! */
370 XMEMCPY(&local[MD5_PAD_SIZE], &md5->loLen, sizeof(word32));
371 XMEMCPY(&local[MD5_PAD_SIZE + sizeof(word32)], &md5->hiLen, sizeof(word32));
372
373 XTRANSFORM(md5, local);
374 #ifdef BIG_ENDIAN_ORDER
375 ByteReverseWords(md5->digest, md5->digest, MD5_DIGEST_SIZE);
376 #endif
377 XMEMCPY(hash, md5->digest, MD5_DIGEST_SIZE);
378
379 wc_InitMd5(md5); /* reset state */
380}
381
382#endif /* STM32F2_HASH */
383
384
385int wc_Md5Hash(const byte* data, word32 len, byte* hash)
386{
387#ifdef WOLFSSL_SMALL_STACK
388 Md5* md5;
389#else
390 Md5 md5[1];
391#endif
392
393#ifdef WOLFSSL_SMALL_STACK
394 md5 = (Md5*)XMALLOC(sizeof(Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER);
395 if (md5 == NULL)
396 return MEMORY_E;
397#endif
398
399 wc_InitMd5(md5);
400 wc_Md5Update(md5, data, len);
401 wc_Md5Final(md5, hash);
402
403#ifdef WOLFSSL_SMALL_STACK
404 XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
405#endif
406
407 return 0;
408}
409
410#endif /* WOLFSSL_TI_HASH */
411
412#endif /* NO_MD5 */
Note: See TracBrowser for help on using the repository browser.