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