1 | /* md5.c
|
---|
2 | *
|
---|
3 | * Copyright (C) 2006-2020 wolfSSL Inc.
|
---|
4 | *
|
---|
5 | * This file is part of wolfSSL.
|
---|
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-1335, USA
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 |
|
---|
24 | #ifdef HAVE_CONFIG_H
|
---|
25 | #include <config.h>
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | #include <wolfssl/wolfcrypt/settings.h>
|
---|
29 |
|
---|
30 | #if !defined(NO_MD5)
|
---|
31 |
|
---|
32 | #if defined(WOLFSSL_TI_HASH)
|
---|
33 | /* #include <wolfcrypt/src/port/ti/ti-hash.c> included by wc_port.c */
|
---|
34 |
|
---|
35 | #else
|
---|
36 |
|
---|
37 | #include <wolfssl/wolfcrypt/md5.h>
|
---|
38 | #include <wolfssl/wolfcrypt/error-crypt.h>
|
---|
39 | #include <wolfssl/wolfcrypt/logging.h>
|
---|
40 | #include <wolfssl/wolfcrypt/hash.h>
|
---|
41 |
|
---|
42 | #ifdef NO_INLINE
|
---|
43 | #include <wolfssl/wolfcrypt/misc.h>
|
---|
44 | #else
|
---|
45 | #define WOLFSSL_MISC_INCLUDED
|
---|
46 | #include <wolfcrypt/src/misc.c>
|
---|
47 | #endif
|
---|
48 |
|
---|
49 |
|
---|
50 | /* Hardware Acceleration */
|
---|
51 | #if defined(STM32_HASH)
|
---|
52 |
|
---|
53 | /* Supports CubeMX HAL or Standard Peripheral Library */
|
---|
54 | #define HAVE_MD5_CUST_API
|
---|
55 |
|
---|
56 | int wc_InitMd5_ex(wc_Md5* md5, void* heap, int devId)
|
---|
57 | {
|
---|
58 | if (md5 == NULL) {
|
---|
59 | return BAD_FUNC_ARG;
|
---|
60 | }
|
---|
61 |
|
---|
62 | (void)devId;
|
---|
63 | (void)heap;
|
---|
64 |
|
---|
65 | wc_Stm32_Hash_Init(&md5->stmCtx);
|
---|
66 |
|
---|
67 | return 0;
|
---|
68 | }
|
---|
69 |
|
---|
70 | int wc_Md5Update(wc_Md5* md5, const byte* data, word32 len)
|
---|
71 | {
|
---|
72 | int ret;
|
---|
73 |
|
---|
74 | if (md5 == NULL || (data == NULL && len > 0)) {
|
---|
75 | return BAD_FUNC_ARG;
|
---|
76 | }
|
---|
77 |
|
---|
78 | ret = wolfSSL_CryptHwMutexLock();
|
---|
79 | if (ret == 0) {
|
---|
80 | ret = wc_Stm32_Hash_Update(&md5->stmCtx, HASH_AlgoSelection_MD5,
|
---|
81 | data, len);
|
---|
82 | wolfSSL_CryptHwMutexUnLock();
|
---|
83 | }
|
---|
84 | return ret;
|
---|
85 | }
|
---|
86 |
|
---|
87 | int wc_Md5Final(wc_Md5* md5, byte* hash)
|
---|
88 | {
|
---|
89 | int ret;
|
---|
90 |
|
---|
91 | if (md5 == NULL || hash == NULL) {
|
---|
92 | return BAD_FUNC_ARG;
|
---|
93 | }
|
---|
94 |
|
---|
95 | ret = wolfSSL_CryptHwMutexLock();
|
---|
96 | if (ret == 0) {
|
---|
97 | ret = wc_Stm32_Hash_Final(&md5->stmCtx, HASH_AlgoSelection_MD5,
|
---|
98 | hash, WC_MD5_DIGEST_SIZE);
|
---|
99 | wolfSSL_CryptHwMutexUnLock();
|
---|
100 | }
|
---|
101 |
|
---|
102 | (void)wc_InitMd5(md5); /* reset state */
|
---|
103 |
|
---|
104 | return ret;
|
---|
105 | }
|
---|
106 |
|
---|
107 | #elif defined(FREESCALE_MMCAU_SHA)
|
---|
108 |
|
---|
109 | #ifdef FREESCALE_MMCAU_CLASSIC_SHA
|
---|
110 | #include "cau_api.h"
|
---|
111 | #else
|
---|
112 | #include "fsl_mmcau.h"
|
---|
113 | #endif
|
---|
114 |
|
---|
115 | #define XTRANSFORM(S,B) Transform((S), (B))
|
---|
116 | #define XTRANSFORM_LEN(S,B,L) Transform_Len((S), (B), (L))
|
---|
117 |
|
---|
118 | #ifndef WC_HASH_DATA_ALIGNMENT
|
---|
119 | /* these hardware API's require 4 byte (word32) alignment */
|
---|
120 | #define WC_HASH_DATA_ALIGNMENT 4
|
---|
121 | #endif
|
---|
122 |
|
---|
123 | static int Transform(wc_Md5* md5, const byte* data)
|
---|
124 | {
|
---|
125 | int ret = wolfSSL_CryptHwMutexLock();
|
---|
126 | if (ret == 0) {
|
---|
127 | #ifdef FREESCALE_MMCAU_CLASSIC_SHA
|
---|
128 | cau_md5_hash_n((byte*)data, 1, (unsigned char*)md5->digest);
|
---|
129 | #else
|
---|
130 | MMCAU_MD5_HashN((byte*)data, 1, (uint32_t*)md5->digest);
|
---|
131 | #endif
|
---|
132 | wolfSSL_CryptHwMutexUnLock();
|
---|
133 | }
|
---|
134 | return ret;
|
---|
135 | }
|
---|
136 |
|
---|
137 | static int Transform_Len(wc_Md5* md5, const byte* data, word32 len)
|
---|
138 | {
|
---|
139 | int ret = wolfSSL_CryptHwMutexLock();
|
---|
140 | if (ret == 0) {
|
---|
141 | #if defined(WC_HASH_DATA_ALIGNMENT) && WC_HASH_DATA_ALIGNMENT > 0
|
---|
142 | if ((size_t)data % WC_HASH_DATA_ALIGNMENT) {
|
---|
143 | /* data pointer is NOT aligned,
|
---|
144 | * so copy and perform one block at a time */
|
---|
145 | byte* local = (byte*)md5->buffer;
|
---|
146 | while (len >= WC_MD5_BLOCK_SIZE) {
|
---|
147 | XMEMCPY(local, data, WC_MD5_BLOCK_SIZE);
|
---|
148 | #ifdef FREESCALE_MMCAU_CLASSIC_SHA
|
---|
149 | cau_md5_hash_n(local, 1, (unsigned char*)md5->digest);
|
---|
150 | #else
|
---|
151 | MMCAU_MD5_HashN(local, 1, (uint32_t*)md5->digest);
|
---|
152 | #endif
|
---|
153 | data += WC_MD5_BLOCK_SIZE;
|
---|
154 | len -= WC_MD5_BLOCK_SIZE;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | else
|
---|
158 | #endif
|
---|
159 | {
|
---|
160 | #ifdef FREESCALE_MMCAU_CLASSIC_SHA
|
---|
161 | cau_md5_hash_n((byte*)data, len / WC_MD5_BLOCK_SIZE,
|
---|
162 | (unsigned char*)md5->digest);
|
---|
163 | #else
|
---|
164 | MMCAU_MD5_HashN((byte*)data, len / WC_MD5_BLOCK_SIZE,
|
---|
165 | (uint32_t*)md5->digest);
|
---|
166 | #endif
|
---|
167 | }
|
---|
168 | wolfSSL_CryptHwMutexUnLock();
|
---|
169 | }
|
---|
170 | return ret;
|
---|
171 | }
|
---|
172 |
|
---|
173 | #elif defined(WOLFSSL_PIC32MZ_HASH)
|
---|
174 | #include <wolfssl/wolfcrypt/port/pic32/pic32mz-crypt.h>
|
---|
175 | #define HAVE_MD5_CUST_API
|
---|
176 |
|
---|
177 | #elif defined(WOLFSSL_IMX6_CAAM) && !defined(NO_IMX6_CAAM_HASH)
|
---|
178 | /* functions implemented in wolfcrypt/src/port/caam/caam_sha.c */
|
---|
179 | #define HAVE_MD5_CUST_API
|
---|
180 | #else
|
---|
181 | #define NEED_SOFT_MD5
|
---|
182 | #endif /* End Hardware Acceleration */
|
---|
183 |
|
---|
184 | #ifdef NEED_SOFT_MD5
|
---|
185 |
|
---|
186 | #define XTRANSFORM(S,B) Transform((S),(B))
|
---|
187 |
|
---|
188 | #define F1(x, y, z) (z ^ (x & (y ^ z)))
|
---|
189 | #define F2(x, y, z) F1(z, x, y)
|
---|
190 | #define F3(x, y, z) (x ^ y ^ z)
|
---|
191 | #define F4(x, y, z) (y ^ (x | ~z))
|
---|
192 |
|
---|
193 | #define MD5STEP(f, w, x, y, z, data, s) \
|
---|
194 | w = rotlFixed(w + f(x, y, z) + data, s) + x
|
---|
195 |
|
---|
196 | static int Transform(wc_Md5* md5, const byte* data)
|
---|
197 | {
|
---|
198 | word32* buffer = (word32*)data;
|
---|
199 | /* Copy context->state[] to working vars */
|
---|
200 | word32 a = md5->digest[0];
|
---|
201 | word32 b = md5->digest[1];
|
---|
202 | word32 c = md5->digest[2];
|
---|
203 | word32 d = md5->digest[3];
|
---|
204 |
|
---|
205 | MD5STEP(F1, a, b, c, d, buffer[0] + 0xd76aa478, 7);
|
---|
206 | MD5STEP(F1, d, a, b, c, buffer[1] + 0xe8c7b756, 12);
|
---|
207 | MD5STEP(F1, c, d, a, b, buffer[2] + 0x242070db, 17);
|
---|
208 | MD5STEP(F1, b, c, d, a, buffer[3] + 0xc1bdceee, 22);
|
---|
209 | MD5STEP(F1, a, b, c, d, buffer[4] + 0xf57c0faf, 7);
|
---|
210 | MD5STEP(F1, d, a, b, c, buffer[5] + 0x4787c62a, 12);
|
---|
211 | MD5STEP(F1, c, d, a, b, buffer[6] + 0xa8304613, 17);
|
---|
212 | MD5STEP(F1, b, c, d, a, buffer[7] + 0xfd469501, 22);
|
---|
213 | MD5STEP(F1, a, b, c, d, buffer[8] + 0x698098d8, 7);
|
---|
214 | MD5STEP(F1, d, a, b, c, buffer[9] + 0x8b44f7af, 12);
|
---|
215 | MD5STEP(F1, c, d, a, b, buffer[10] + 0xffff5bb1, 17);
|
---|
216 | MD5STEP(F1, b, c, d, a, buffer[11] + 0x895cd7be, 22);
|
---|
217 | MD5STEP(F1, a, b, c, d, buffer[12] + 0x6b901122, 7);
|
---|
218 | MD5STEP(F1, d, a, b, c, buffer[13] + 0xfd987193, 12);
|
---|
219 | MD5STEP(F1, c, d, a, b, buffer[14] + 0xa679438e, 17);
|
---|
220 | MD5STEP(F1, b, c, d, a, buffer[15] + 0x49b40821, 22);
|
---|
221 |
|
---|
222 | MD5STEP(F2, a, b, c, d, buffer[1] + 0xf61e2562, 5);
|
---|
223 | MD5STEP(F2, d, a, b, c, buffer[6] + 0xc040b340, 9);
|
---|
224 | MD5STEP(F2, c, d, a, b, buffer[11] + 0x265e5a51, 14);
|
---|
225 | MD5STEP(F2, b, c, d, a, buffer[0] + 0xe9b6c7aa, 20);
|
---|
226 | MD5STEP(F2, a, b, c, d, buffer[5] + 0xd62f105d, 5);
|
---|
227 | MD5STEP(F2, d, a, b, c, buffer[10] + 0x02441453, 9);
|
---|
228 | MD5STEP(F2, c, d, a, b, buffer[15] + 0xd8a1e681, 14);
|
---|
229 | MD5STEP(F2, b, c, d, a, buffer[4] + 0xe7d3fbc8, 20);
|
---|
230 | MD5STEP(F2, a, b, c, d, buffer[9] + 0x21e1cde6, 5);
|
---|
231 | MD5STEP(F2, d, a, b, c, buffer[14] + 0xc33707d6, 9);
|
---|
232 | MD5STEP(F2, c, d, a, b, buffer[3] + 0xf4d50d87, 14);
|
---|
233 | MD5STEP(F2, b, c, d, a, buffer[8] + 0x455a14ed, 20);
|
---|
234 | MD5STEP(F2, a, b, c, d, buffer[13] + 0xa9e3e905, 5);
|
---|
235 | MD5STEP(F2, d, a, b, c, buffer[2] + 0xfcefa3f8, 9);
|
---|
236 | MD5STEP(F2, c, d, a, b, buffer[7] + 0x676f02d9, 14);
|
---|
237 | MD5STEP(F2, b, c, d, a, buffer[12] + 0x8d2a4c8a, 20);
|
---|
238 |
|
---|
239 | MD5STEP(F3, a, b, c, d, buffer[5] + 0xfffa3942, 4);
|
---|
240 | MD5STEP(F3, d, a, b, c, buffer[8] + 0x8771f681, 11);
|
---|
241 | MD5STEP(F3, c, d, a, b, buffer[11] + 0x6d9d6122, 16);
|
---|
242 | MD5STEP(F3, b, c, d, a, buffer[14] + 0xfde5380c, 23);
|
---|
243 | MD5STEP(F3, a, b, c, d, buffer[1] + 0xa4beea44, 4);
|
---|
244 | MD5STEP(F3, d, a, b, c, buffer[4] + 0x4bdecfa9, 11);
|
---|
245 | MD5STEP(F3, c, d, a, b, buffer[7] + 0xf6bb4b60, 16);
|
---|
246 | MD5STEP(F3, b, c, d, a, buffer[10] + 0xbebfbc70, 23);
|
---|
247 | MD5STEP(F3, a, b, c, d, buffer[13] + 0x289b7ec6, 4);
|
---|
248 | MD5STEP(F3, d, a, b, c, buffer[0] + 0xeaa127fa, 11);
|
---|
249 | MD5STEP(F3, c, d, a, b, buffer[3] + 0xd4ef3085, 16);
|
---|
250 | MD5STEP(F3, b, c, d, a, buffer[6] + 0x04881d05, 23);
|
---|
251 | MD5STEP(F3, a, b, c, d, buffer[9] + 0xd9d4d039, 4);
|
---|
252 | MD5STEP(F3, d, a, b, c, buffer[12] + 0xe6db99e5, 11);
|
---|
253 | MD5STEP(F3, c, d, a, b, buffer[15] + 0x1fa27cf8, 16);
|
---|
254 | MD5STEP(F3, b, c, d, a, buffer[2] + 0xc4ac5665, 23);
|
---|
255 |
|
---|
256 | MD5STEP(F4, a, b, c, d, buffer[0] + 0xf4292244, 6);
|
---|
257 | MD5STEP(F4, d, a, b, c, buffer[7] + 0x432aff97, 10);
|
---|
258 | MD5STEP(F4, c, d, a, b, buffer[14] + 0xab9423a7, 15);
|
---|
259 | MD5STEP(F4, b, c, d, a, buffer[5] + 0xfc93a039, 21);
|
---|
260 | MD5STEP(F4, a, b, c, d, buffer[12] + 0x655b59c3, 6);
|
---|
261 | MD5STEP(F4, d, a, b, c, buffer[3] + 0x8f0ccc92, 10);
|
---|
262 | MD5STEP(F4, c, d, a, b, buffer[10] + 0xffeff47d, 15);
|
---|
263 | MD5STEP(F4, b, c, d, a, buffer[1] + 0x85845dd1, 21);
|
---|
264 | MD5STEP(F4, a, b, c, d, buffer[8] + 0x6fa87e4f, 6);
|
---|
265 | MD5STEP(F4, d, a, b, c, buffer[15] + 0xfe2ce6e0, 10);
|
---|
266 | MD5STEP(F4, c, d, a, b, buffer[6] + 0xa3014314, 15);
|
---|
267 | MD5STEP(F4, b, c, d, a, buffer[13] + 0x4e0811a1, 21);
|
---|
268 | MD5STEP(F4, a, b, c, d, buffer[4] + 0xf7537e82, 6);
|
---|
269 | MD5STEP(F4, d, a, b, c, buffer[11] + 0xbd3af235, 10);
|
---|
270 | MD5STEP(F4, c, d, a, b, buffer[2] + 0x2ad7d2bb, 15);
|
---|
271 | MD5STEP(F4, b, c, d, a, buffer[9] + 0xeb86d391, 21);
|
---|
272 |
|
---|
273 | /* Add the working vars back into digest state[] */
|
---|
274 | md5->digest[0] += a;
|
---|
275 | md5->digest[1] += b;
|
---|
276 | md5->digest[2] += c;
|
---|
277 | md5->digest[3] += d;
|
---|
278 |
|
---|
279 | return 0;
|
---|
280 | }
|
---|
281 | #endif /* NEED_SOFT_MD5 */
|
---|
282 |
|
---|
283 | #ifndef HAVE_MD5_CUST_API
|
---|
284 |
|
---|
285 | static WC_INLINE void AddLength(wc_Md5* md5, word32 len)
|
---|
286 | {
|
---|
287 | word32 tmp = md5->loLen;
|
---|
288 | if ((md5->loLen += len) < tmp) {
|
---|
289 | md5->hiLen++; /* carry low to high */
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | static int _InitMd5(wc_Md5* md5)
|
---|
294 | {
|
---|
295 | int ret = 0;
|
---|
296 |
|
---|
297 | md5->digest[0] = 0x67452301L;
|
---|
298 | md5->digest[1] = 0xefcdab89L;
|
---|
299 | md5->digest[2] = 0x98badcfeL;
|
---|
300 | md5->digest[3] = 0x10325476L;
|
---|
301 |
|
---|
302 | md5->buffLen = 0;
|
---|
303 | md5->loLen = 0;
|
---|
304 | md5->hiLen = 0;
|
---|
305 | #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
|
---|
306 | md5->flags = 0;
|
---|
307 | #endif
|
---|
308 |
|
---|
309 | return ret;
|
---|
310 | }
|
---|
311 |
|
---|
312 | int wc_InitMd5_ex(wc_Md5* md5, void* heap, int devId)
|
---|
313 | {
|
---|
314 | int ret = 0;
|
---|
315 |
|
---|
316 | if (md5 == NULL)
|
---|
317 | return BAD_FUNC_ARG;
|
---|
318 |
|
---|
319 | md5->heap = heap;
|
---|
320 |
|
---|
321 | ret = _InitMd5(md5);
|
---|
322 | if (ret != 0)
|
---|
323 | return ret;
|
---|
324 |
|
---|
325 | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_MD5)
|
---|
326 | ret = wolfAsync_DevCtxInit(&md5->asyncDev, WOLFSSL_ASYNC_MARKER_MD5,
|
---|
327 | md5->heap, devId);
|
---|
328 | #else
|
---|
329 | (void)devId;
|
---|
330 | #endif
|
---|
331 | return ret;
|
---|
332 | }
|
---|
333 |
|
---|
334 | /* do block size increments/updates */
|
---|
335 | int wc_Md5Update(wc_Md5* md5, const byte* data, word32 len)
|
---|
336 | {
|
---|
337 | int ret = 0;
|
---|
338 | word32 blocksLen;
|
---|
339 | byte* local;
|
---|
340 |
|
---|
341 | if (md5 == NULL || (data == NULL && len > 0)) {
|
---|
342 | return BAD_FUNC_ARG;
|
---|
343 | }
|
---|
344 |
|
---|
345 | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_MD5)
|
---|
346 | if (md5->asyncDev.marker == WOLFSSL_ASYNC_MARKER_MD5) {
|
---|
347 | #if defined(HAVE_INTEL_QA)
|
---|
348 | return IntelQaSymMd5(&md5->asyncDev, NULL, data, len);
|
---|
349 | #endif
|
---|
350 | }
|
---|
351 | #endif /* WOLFSSL_ASYNC_CRYPT */
|
---|
352 |
|
---|
353 | /* check that internal buffLen is valid */
|
---|
354 | if (md5->buffLen >= WC_MD5_BLOCK_SIZE)
|
---|
355 | return BUFFER_E;
|
---|
356 |
|
---|
357 | if (data == NULL && len == 0) {
|
---|
358 | /* valid, but do nothing */
|
---|
359 | return 0;
|
---|
360 | }
|
---|
361 |
|
---|
362 | /* add length for final */
|
---|
363 | AddLength(md5, len);
|
---|
364 |
|
---|
365 | local = (byte*)md5->buffer;
|
---|
366 |
|
---|
367 | /* process any remainder from previous operation */
|
---|
368 | if (md5->buffLen > 0) {
|
---|
369 | blocksLen = min(len, WC_MD5_BLOCK_SIZE - md5->buffLen);
|
---|
370 | XMEMCPY(&local[md5->buffLen], data, blocksLen);
|
---|
371 |
|
---|
372 | md5->buffLen += blocksLen;
|
---|
373 | data += blocksLen;
|
---|
374 | len -= blocksLen;
|
---|
375 |
|
---|
376 | if (md5->buffLen == WC_MD5_BLOCK_SIZE) {
|
---|
377 | #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
|
---|
378 | ByteReverseWords(md5->buffer, md5->buffer, WC_MD5_BLOCK_SIZE);
|
---|
379 | #endif
|
---|
380 |
|
---|
381 | ret = XTRANSFORM(md5, (const byte*)local);
|
---|
382 | if (ret != 0)
|
---|
383 | return ret;
|
---|
384 |
|
---|
385 | md5->buffLen = 0;
|
---|
386 | }
|
---|
387 | }
|
---|
388 |
|
---|
389 | /* process blocks */
|
---|
390 | #ifdef XTRANSFORM_LEN
|
---|
391 | /* get number of blocks */
|
---|
392 | /* 64-1 = 0x3F (~ Inverted = 0xFFFFFFC0) */
|
---|
393 | /* len (masked by 0xFFFFFFC0) returns block aligned length */
|
---|
394 | blocksLen = len & ~(WC_MD5_BLOCK_SIZE-1);
|
---|
395 | if (blocksLen > 0) {
|
---|
396 | /* Byte reversal performed in function if required. */
|
---|
397 | XTRANSFORM_LEN(md5, data, blocksLen);
|
---|
398 | data += blocksLen;
|
---|
399 | len -= blocksLen;
|
---|
400 | }
|
---|
401 | #else
|
---|
402 | while (len >= WC_MD5_BLOCK_SIZE) {
|
---|
403 | word32* local32 = md5->buffer;
|
---|
404 | /* optimization to avoid memcpy if data pointer is properly aligned */
|
---|
405 | /* Big Endian requires byte swap, so can't use data directly */
|
---|
406 | #if defined(WC_HASH_DATA_ALIGNMENT) && !defined(BIG_ENDIAN_ORDER)
|
---|
407 | if (((size_t)data % WC_HASH_DATA_ALIGNMENT) == 0) {
|
---|
408 | local32 = (word32*)data;
|
---|
409 | }
|
---|
410 | else
|
---|
411 | #endif
|
---|
412 | {
|
---|
413 | XMEMCPY(local32, data, WC_MD5_BLOCK_SIZE);
|
---|
414 | }
|
---|
415 |
|
---|
416 | data += WC_MD5_BLOCK_SIZE;
|
---|
417 | len -= WC_MD5_BLOCK_SIZE;
|
---|
418 |
|
---|
419 | #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
|
---|
420 | ByteReverseWords(local32, local32, WC_MD5_BLOCK_SIZE);
|
---|
421 | #endif
|
---|
422 |
|
---|
423 | ret = XTRANSFORM(md5, (const byte*)local32);
|
---|
424 | }
|
---|
425 | #endif /* XTRANSFORM_LEN */
|
---|
426 |
|
---|
427 | /* save remainder */
|
---|
428 | if (len > 0) {
|
---|
429 | XMEMCPY(local, data, len);
|
---|
430 | md5->buffLen = len;
|
---|
431 | }
|
---|
432 |
|
---|
433 | return ret;
|
---|
434 | }
|
---|
435 |
|
---|
436 | int wc_Md5Final(wc_Md5* md5, byte* hash)
|
---|
437 | {
|
---|
438 | byte* local;
|
---|
439 |
|
---|
440 | if (md5 == NULL || hash == NULL) {
|
---|
441 | return BAD_FUNC_ARG;
|
---|
442 | }
|
---|
443 |
|
---|
444 | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_MD5)
|
---|
445 | if (md5->asyncDev.marker == WOLFSSL_ASYNC_MARKER_MD5) {
|
---|
446 | #if defined(HAVE_INTEL_QA)
|
---|
447 | return IntelQaSymMd5(&md5->asyncDev, hash, NULL, WC_MD5_DIGEST_SIZE);
|
---|
448 | #endif
|
---|
449 | }
|
---|
450 | #endif /* WOLFSSL_ASYNC_CRYPT */
|
---|
451 |
|
---|
452 | local = (byte*)md5->buffer;
|
---|
453 |
|
---|
454 | local[md5->buffLen++] = 0x80; /* add 1 */
|
---|
455 |
|
---|
456 | /* pad with zeros */
|
---|
457 | if (md5->buffLen > WC_MD5_PAD_SIZE) {
|
---|
458 | XMEMSET(&local[md5->buffLen], 0, WC_MD5_BLOCK_SIZE - md5->buffLen);
|
---|
459 | md5->buffLen += WC_MD5_BLOCK_SIZE - md5->buffLen;
|
---|
460 |
|
---|
461 | #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
|
---|
462 | ByteReverseWords(md5->buffer, md5->buffer, WC_MD5_BLOCK_SIZE);
|
---|
463 | #endif
|
---|
464 | XTRANSFORM(md5, local);
|
---|
465 | md5->buffLen = 0;
|
---|
466 | }
|
---|
467 | XMEMSET(&local[md5->buffLen], 0, WC_MD5_PAD_SIZE - md5->buffLen);
|
---|
468 |
|
---|
469 | #if defined(BIG_ENDIAN_ORDER) && !defined(FREESCALE_MMCAU_SHA)
|
---|
470 | ByteReverseWords(md5->buffer, md5->buffer, WC_MD5_BLOCK_SIZE);
|
---|
471 | #endif
|
---|
472 |
|
---|
473 | /* put lengths in bits */
|
---|
474 | md5->hiLen = (md5->loLen >> (8 * sizeof(md5->loLen) - 3)) +
|
---|
475 | (md5->hiLen << 3);
|
---|
476 | md5->loLen = md5->loLen << 3;
|
---|
477 |
|
---|
478 | /* store lengths */
|
---|
479 | /* ! length ordering dependent on digest endian type ! */
|
---|
480 | XMEMCPY(&local[WC_MD5_PAD_SIZE], &md5->loLen, sizeof(word32));
|
---|
481 | XMEMCPY(&local[WC_MD5_PAD_SIZE + sizeof(word32)], &md5->hiLen, sizeof(word32));
|
---|
482 |
|
---|
483 | /* final transform and result to hash */
|
---|
484 | XTRANSFORM(md5, local);
|
---|
485 | #ifdef BIG_ENDIAN_ORDER
|
---|
486 | ByteReverseWords(md5->digest, md5->digest, WC_MD5_DIGEST_SIZE);
|
---|
487 | #endif
|
---|
488 | XMEMCPY(hash, md5->digest, WC_MD5_DIGEST_SIZE);
|
---|
489 |
|
---|
490 | return _InitMd5(md5); /* reset state */
|
---|
491 | }
|
---|
492 | #endif /* !HAVE_MD5_CUST_API */
|
---|
493 |
|
---|
494 |
|
---|
495 | int wc_InitMd5(wc_Md5* md5)
|
---|
496 | {
|
---|
497 | if (md5 == NULL) {
|
---|
498 | return BAD_FUNC_ARG;
|
---|
499 | }
|
---|
500 | return wc_InitMd5_ex(md5, NULL, INVALID_DEVID);
|
---|
501 | }
|
---|
502 |
|
---|
503 | void wc_Md5Free(wc_Md5* md5)
|
---|
504 | {
|
---|
505 | if (md5 == NULL)
|
---|
506 | return;
|
---|
507 | #if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_MD5)
|
---|
508 | wolfAsync_DevCtxFree(&md5->asyncDev, WOLFSSL_ASYNC_MARKER_MD5);
|
---|
509 | #endif /* WOLFSSL_ASYNC_CRYPT */
|
---|
510 |
|
---|
511 | #ifdef WOLFSSL_PIC32MZ_HASH
|
---|
512 | wc_Md5Pic32Free(md5);
|
---|
513 | #endif
|
---|
514 | }
|
---|
515 |
|
---|
516 | int wc_Md5GetHash(wc_Md5* md5, byte* hash)
|
---|
517 | {
|
---|
518 | int ret;
|
---|
519 | wc_Md5 tmpMd5;
|
---|
520 |
|
---|
521 | if (md5 == NULL || hash == NULL)
|
---|
522 | return BAD_FUNC_ARG;
|
---|
523 |
|
---|
524 | ret = wc_Md5Copy(md5, &tmpMd5);
|
---|
525 | if (ret == 0) {
|
---|
526 | ret = wc_Md5Final(&tmpMd5, hash);
|
---|
527 | }
|
---|
528 |
|
---|
529 | return ret;
|
---|
530 | }
|
---|
531 |
|
---|
532 | int wc_Md5Copy(wc_Md5* src, wc_Md5* dst)
|
---|
533 | {
|
---|
534 | int ret = 0;
|
---|
535 |
|
---|
536 | if (src == NULL || dst == NULL)
|
---|
537 | return BAD_FUNC_ARG;
|
---|
538 |
|
---|
539 | XMEMCPY(dst, src, sizeof(wc_Md5));
|
---|
540 |
|
---|
541 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
542 | ret = wolfAsync_DevCopy(&src->asyncDev, &dst->asyncDev);
|
---|
543 | #endif
|
---|
544 | #ifdef WOLFSSL_PIC32MZ_HASH
|
---|
545 | ret = wc_Pic32HashCopy(&src->cache, &dst->cache);
|
---|
546 | #endif
|
---|
547 | #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
|
---|
548 | dst->flags |= WC_HASH_FLAG_ISCOPY;
|
---|
549 | #endif
|
---|
550 |
|
---|
551 | return ret;
|
---|
552 | }
|
---|
553 |
|
---|
554 | #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
|
---|
555 | int wc_Md5SetFlags(wc_Md5* md5, word32 flags)
|
---|
556 | {
|
---|
557 | if (md5) {
|
---|
558 | md5->flags = flags;
|
---|
559 | }
|
---|
560 | return 0;
|
---|
561 | }
|
---|
562 | int wc_Md5GetFlags(wc_Md5* md5, word32* flags)
|
---|
563 | {
|
---|
564 | if (md5 && flags) {
|
---|
565 | *flags = md5->flags;
|
---|
566 | }
|
---|
567 | return 0;
|
---|
568 | }
|
---|
569 | #endif
|
---|
570 |
|
---|
571 | #endif /* WOLFSSL_TI_HASH */
|
---|
572 | #endif /* NO_MD5 */
|
---|