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