Fork of CyaSSL for my specific settings

Dependents:   CyaSSL_Example

Fork of CyaSSL by wolf SSL

Committer:
wolfSSL
Date:
Sat Jul 12 07:18:23 2014 +0000
Revision:
0:1239e9b70ca2
CyaSSL 3.0.0;

Who changed what in which revision?

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