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