wolfSSL 3.11.1 for TLS1.3 beta

Fork of wolfSSL by wolf SSL

Committer:
wolfSSL
Date:
Tue May 30 01:44:10 2017 +0000
Revision:
11:cee25a834751
wolfSSL 3.11.0

Who changed what in which revision?

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