cyassl re-port with cellular comms, PSK test
Dependencies: VodafoneUSBModem_bleedingedge2 mbed-rtos mbed-src
cyassllib/ctaocrypt/src/md4.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 | /* md4.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 | #ifdef HAVE_CONFIG_H |
ashleymills | 0:e979170e02e7 | 23 | #include <config.h> |
ashleymills | 0:e979170e02e7 | 24 | #endif |
ashleymills | 0:e979170e02e7 | 25 | |
ashleymills | 0:e979170e02e7 | 26 | |
ashleymills | 0:e979170e02e7 | 27 | #ifndef NO_MD4 |
ashleymills | 0:e979170e02e7 | 28 | |
ashleymills | 0:e979170e02e7 | 29 | #include <cyassl/ctaocrypt/md4.h> |
ashleymills | 0:e979170e02e7 | 30 | #ifdef NO_INLINE |
ashleymills | 0:e979170e02e7 | 31 | #include <cyassl/ctaocrypt/misc.h> |
ashleymills | 0:e979170e02e7 | 32 | #else |
ashleymills | 0:e979170e02e7 | 33 | #include <ctaocrypt/src/misc.c> |
ashleymills | 0:e979170e02e7 | 34 | #endif |
ashleymills | 0:e979170e02e7 | 35 | |
ashleymills | 0:e979170e02e7 | 36 | |
ashleymills | 0:e979170e02e7 | 37 | #ifndef min |
ashleymills | 0:e979170e02e7 | 38 | |
ashleymills | 0:e979170e02e7 | 39 | static INLINE word32 min(word32 a, word32 b) |
ashleymills | 0:e979170e02e7 | 40 | { |
ashleymills | 0:e979170e02e7 | 41 | return a > b ? b : a; |
ashleymills | 0:e979170e02e7 | 42 | } |
ashleymills | 0:e979170e02e7 | 43 | |
ashleymills | 0:e979170e02e7 | 44 | #endif /* min */ |
ashleymills | 0:e979170e02e7 | 45 | |
ashleymills | 0:e979170e02e7 | 46 | |
ashleymills | 0:e979170e02e7 | 47 | void InitMd4(Md4* md4) |
ashleymills | 0:e979170e02e7 | 48 | { |
ashleymills | 0:e979170e02e7 | 49 | md4->digest[0] = 0x67452301L; |
ashleymills | 0:e979170e02e7 | 50 | md4->digest[1] = 0xefcdab89L; |
ashleymills | 0:e979170e02e7 | 51 | md4->digest[2] = 0x98badcfeL; |
ashleymills | 0:e979170e02e7 | 52 | md4->digest[3] = 0x10325476L; |
ashleymills | 0:e979170e02e7 | 53 | |
ashleymills | 0:e979170e02e7 | 54 | md4->buffLen = 0; |
ashleymills | 0:e979170e02e7 | 55 | md4->loLen = 0; |
ashleymills | 0:e979170e02e7 | 56 | md4->hiLen = 0; |
ashleymills | 0:e979170e02e7 | 57 | } |
ashleymills | 0:e979170e02e7 | 58 | |
ashleymills | 0:e979170e02e7 | 59 | |
ashleymills | 0:e979170e02e7 | 60 | static void Transform(Md4* md4) |
ashleymills | 0:e979170e02e7 | 61 | { |
ashleymills | 0:e979170e02e7 | 62 | #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) |
ashleymills | 0:e979170e02e7 | 63 | #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) |
ashleymills | 0:e979170e02e7 | 64 | #define H(x, y, z) ((x) ^ (y) ^ (z)) |
ashleymills | 0:e979170e02e7 | 65 | |
ashleymills | 0:e979170e02e7 | 66 | /* Copy context->state[] to working vars */ |
ashleymills | 0:e979170e02e7 | 67 | word32 A = md4->digest[0]; |
ashleymills | 0:e979170e02e7 | 68 | word32 B = md4->digest[1]; |
ashleymills | 0:e979170e02e7 | 69 | word32 C = md4->digest[2]; |
ashleymills | 0:e979170e02e7 | 70 | word32 D = md4->digest[3]; |
ashleymills | 0:e979170e02e7 | 71 | |
ashleymills | 0:e979170e02e7 | 72 | #define function(a,b,c,d,k,s) a=rotlFixed(a+F(b,c,d)+md4->buffer[k],s); |
ashleymills | 0:e979170e02e7 | 73 | function(A,B,C,D, 0, 3); |
ashleymills | 0:e979170e02e7 | 74 | function(D,A,B,C, 1, 7); |
ashleymills | 0:e979170e02e7 | 75 | function(C,D,A,B, 2,11); |
ashleymills | 0:e979170e02e7 | 76 | function(B,C,D,A, 3,19); |
ashleymills | 0:e979170e02e7 | 77 | function(A,B,C,D, 4, 3); |
ashleymills | 0:e979170e02e7 | 78 | function(D,A,B,C, 5, 7); |
ashleymills | 0:e979170e02e7 | 79 | function(C,D,A,B, 6,11); |
ashleymills | 0:e979170e02e7 | 80 | function(B,C,D,A, 7,19); |
ashleymills | 0:e979170e02e7 | 81 | function(A,B,C,D, 8, 3); |
ashleymills | 0:e979170e02e7 | 82 | function(D,A,B,C, 9, 7); |
ashleymills | 0:e979170e02e7 | 83 | function(C,D,A,B,10,11); |
ashleymills | 0:e979170e02e7 | 84 | function(B,C,D,A,11,19); |
ashleymills | 0:e979170e02e7 | 85 | function(A,B,C,D,12, 3); |
ashleymills | 0:e979170e02e7 | 86 | function(D,A,B,C,13, 7); |
ashleymills | 0:e979170e02e7 | 87 | function(C,D,A,B,14,11); |
ashleymills | 0:e979170e02e7 | 88 | function(B,C,D,A,15,19); |
ashleymills | 0:e979170e02e7 | 89 | |
ashleymills | 0:e979170e02e7 | 90 | #undef function |
ashleymills | 0:e979170e02e7 | 91 | #define function(a,b,c,d,k,s) \ |
ashleymills | 0:e979170e02e7 | 92 | a=rotlFixed(a+G(b,c,d)+md4->buffer[k]+0x5a827999,s); |
ashleymills | 0:e979170e02e7 | 93 | |
ashleymills | 0:e979170e02e7 | 94 | function(A,B,C,D, 0, 3); |
ashleymills | 0:e979170e02e7 | 95 | function(D,A,B,C, 4, 5); |
ashleymills | 0:e979170e02e7 | 96 | function(C,D,A,B, 8, 9); |
ashleymills | 0:e979170e02e7 | 97 | function(B,C,D,A,12,13); |
ashleymills | 0:e979170e02e7 | 98 | function(A,B,C,D, 1, 3); |
ashleymills | 0:e979170e02e7 | 99 | function(D,A,B,C, 5, 5); |
ashleymills | 0:e979170e02e7 | 100 | function(C,D,A,B, 9, 9); |
ashleymills | 0:e979170e02e7 | 101 | function(B,C,D,A,13,13); |
ashleymills | 0:e979170e02e7 | 102 | function(A,B,C,D, 2, 3); |
ashleymills | 0:e979170e02e7 | 103 | function(D,A,B,C, 6, 5); |
ashleymills | 0:e979170e02e7 | 104 | function(C,D,A,B,10, 9); |
ashleymills | 0:e979170e02e7 | 105 | function(B,C,D,A,14,13); |
ashleymills | 0:e979170e02e7 | 106 | function(A,B,C,D, 3, 3); |
ashleymills | 0:e979170e02e7 | 107 | function(D,A,B,C, 7, 5); |
ashleymills | 0:e979170e02e7 | 108 | function(C,D,A,B,11, 9); |
ashleymills | 0:e979170e02e7 | 109 | function(B,C,D,A,15,13); |
ashleymills | 0:e979170e02e7 | 110 | |
ashleymills | 0:e979170e02e7 | 111 | #undef function |
ashleymills | 0:e979170e02e7 | 112 | #define function(a,b,c,d,k,s) \ |
ashleymills | 0:e979170e02e7 | 113 | a=rotlFixed(a+H(b,c,d)+md4->buffer[k]+0x6ed9eba1,s); |
ashleymills | 0:e979170e02e7 | 114 | |
ashleymills | 0:e979170e02e7 | 115 | function(A,B,C,D, 0, 3); |
ashleymills | 0:e979170e02e7 | 116 | function(D,A,B,C, 8, 9); |
ashleymills | 0:e979170e02e7 | 117 | function(C,D,A,B, 4,11); |
ashleymills | 0:e979170e02e7 | 118 | function(B,C,D,A,12,15); |
ashleymills | 0:e979170e02e7 | 119 | function(A,B,C,D, 2, 3); |
ashleymills | 0:e979170e02e7 | 120 | function(D,A,B,C,10, 9); |
ashleymills | 0:e979170e02e7 | 121 | function(C,D,A,B, 6,11); |
ashleymills | 0:e979170e02e7 | 122 | function(B,C,D,A,14,15); |
ashleymills | 0:e979170e02e7 | 123 | function(A,B,C,D, 1, 3); |
ashleymills | 0:e979170e02e7 | 124 | function(D,A,B,C, 9, 9); |
ashleymills | 0:e979170e02e7 | 125 | function(C,D,A,B, 5,11); |
ashleymills | 0:e979170e02e7 | 126 | function(B,C,D,A,13,15); |
ashleymills | 0:e979170e02e7 | 127 | function(A,B,C,D, 3, 3); |
ashleymills | 0:e979170e02e7 | 128 | function(D,A,B,C,11, 9); |
ashleymills | 0:e979170e02e7 | 129 | function(C,D,A,B, 7,11); |
ashleymills | 0:e979170e02e7 | 130 | function(B,C,D,A,15,15); |
ashleymills | 0:e979170e02e7 | 131 | |
ashleymills | 0:e979170e02e7 | 132 | /* Add the working vars back into digest state[] */ |
ashleymills | 0:e979170e02e7 | 133 | md4->digest[0] += A; |
ashleymills | 0:e979170e02e7 | 134 | md4->digest[1] += B; |
ashleymills | 0:e979170e02e7 | 135 | md4->digest[2] += C; |
ashleymills | 0:e979170e02e7 | 136 | md4->digest[3] += D; |
ashleymills | 0:e979170e02e7 | 137 | } |
ashleymills | 0:e979170e02e7 | 138 | |
ashleymills | 0:e979170e02e7 | 139 | |
ashleymills | 0:e979170e02e7 | 140 | static INLINE void AddLength(Md4* md4, word32 len) |
ashleymills | 0:e979170e02e7 | 141 | { |
ashleymills | 0:e979170e02e7 | 142 | word32 tmp = md4->loLen; |
ashleymills | 0:e979170e02e7 | 143 | if ( (md4->loLen += len) < tmp) |
ashleymills | 0:e979170e02e7 | 144 | md4->hiLen++; /* carry low to high */ |
ashleymills | 0:e979170e02e7 | 145 | } |
ashleymills | 0:e979170e02e7 | 146 | |
ashleymills | 0:e979170e02e7 | 147 | |
ashleymills | 0:e979170e02e7 | 148 | void Md4Update(Md4* md4, const byte* data, word32 len) |
ashleymills | 0:e979170e02e7 | 149 | { |
ashleymills | 0:e979170e02e7 | 150 | /* do block size increments */ |
ashleymills | 0:e979170e02e7 | 151 | byte* local = (byte*)md4->buffer; |
ashleymills | 0:e979170e02e7 | 152 | |
ashleymills | 0:e979170e02e7 | 153 | while (len) { |
ashleymills | 0:e979170e02e7 | 154 | word32 add = min(len, MD4_BLOCK_SIZE - md4->buffLen); |
ashleymills | 0:e979170e02e7 | 155 | XMEMCPY(&local[md4->buffLen], data, add); |
ashleymills | 0:e979170e02e7 | 156 | |
ashleymills | 0:e979170e02e7 | 157 | md4->buffLen += add; |
ashleymills | 0:e979170e02e7 | 158 | data += add; |
ashleymills | 0:e979170e02e7 | 159 | len -= add; |
ashleymills | 0:e979170e02e7 | 160 | |
ashleymills | 0:e979170e02e7 | 161 | if (md4->buffLen == MD4_BLOCK_SIZE) { |
ashleymills | 0:e979170e02e7 | 162 | #ifdef BIG_ENDIAN_ORDER |
ashleymills | 0:e979170e02e7 | 163 | ByteReverseBytes(local, local, MD4_BLOCK_SIZE); |
ashleymills | 0:e979170e02e7 | 164 | #endif |
ashleymills | 0:e979170e02e7 | 165 | Transform(md4); |
ashleymills | 0:e979170e02e7 | 166 | AddLength(md4, MD4_BLOCK_SIZE); |
ashleymills | 0:e979170e02e7 | 167 | md4->buffLen = 0; |
ashleymills | 0:e979170e02e7 | 168 | } |
ashleymills | 0:e979170e02e7 | 169 | } |
ashleymills | 0:e979170e02e7 | 170 | } |
ashleymills | 0:e979170e02e7 | 171 | |
ashleymills | 0:e979170e02e7 | 172 | |
ashleymills | 0:e979170e02e7 | 173 | void Md4Final(Md4* md4, byte* hash) |
ashleymills | 0:e979170e02e7 | 174 | { |
ashleymills | 0:e979170e02e7 | 175 | byte* local = (byte*)md4->buffer; |
ashleymills | 0:e979170e02e7 | 176 | |
ashleymills | 0:e979170e02e7 | 177 | AddLength(md4, md4->buffLen); /* before adding pads */ |
ashleymills | 0:e979170e02e7 | 178 | |
ashleymills | 0:e979170e02e7 | 179 | local[md4->buffLen++] = 0x80; /* add 1 */ |
ashleymills | 0:e979170e02e7 | 180 | |
ashleymills | 0:e979170e02e7 | 181 | /* pad with zeros */ |
ashleymills | 0:e979170e02e7 | 182 | if (md4->buffLen > MD4_PAD_SIZE) { |
ashleymills | 0:e979170e02e7 | 183 | XMEMSET(&local[md4->buffLen], 0, MD4_BLOCK_SIZE - md4->buffLen); |
ashleymills | 0:e979170e02e7 | 184 | md4->buffLen += MD4_BLOCK_SIZE - md4->buffLen; |
ashleymills | 0:e979170e02e7 | 185 | |
ashleymills | 0:e979170e02e7 | 186 | #ifdef BIG_ENDIAN_ORDER |
ashleymills | 0:e979170e02e7 | 187 | ByteReverseBytes(local, local, MD4_BLOCK_SIZE); |
ashleymills | 0:e979170e02e7 | 188 | #endif |
ashleymills | 0:e979170e02e7 | 189 | Transform(md4); |
ashleymills | 0:e979170e02e7 | 190 | md4->buffLen = 0; |
ashleymills | 0:e979170e02e7 | 191 | } |
ashleymills | 0:e979170e02e7 | 192 | XMEMSET(&local[md4->buffLen], 0, MD4_PAD_SIZE - md4->buffLen); |
ashleymills | 0:e979170e02e7 | 193 | |
ashleymills | 0:e979170e02e7 | 194 | /* put lengths in bits */ |
ashleymills | 0:e979170e02e7 | 195 | md4->hiLen = (md4->loLen >> (8*sizeof(md4->loLen) - 3)) + |
ashleymills | 0:e979170e02e7 | 196 | (md4->hiLen << 3); |
ashleymills | 0:e979170e02e7 | 197 | md4->loLen = md4->loLen << 3; |
ashleymills | 0:e979170e02e7 | 198 | |
ashleymills | 0:e979170e02e7 | 199 | /* store lengths */ |
ashleymills | 0:e979170e02e7 | 200 | #ifdef BIG_ENDIAN_ORDER |
ashleymills | 0:e979170e02e7 | 201 | ByteReverseBytes(local, local, MD4_BLOCK_SIZE); |
ashleymills | 0:e979170e02e7 | 202 | #endif |
ashleymills | 0:e979170e02e7 | 203 | /* ! length ordering dependent on digest endian type ! */ |
ashleymills | 0:e979170e02e7 | 204 | XMEMCPY(&local[MD4_PAD_SIZE], &md4->loLen, sizeof(word32)); |
ashleymills | 0:e979170e02e7 | 205 | XMEMCPY(&local[MD4_PAD_SIZE + sizeof(word32)], &md4->hiLen, sizeof(word32)); |
ashleymills | 0:e979170e02e7 | 206 | |
ashleymills | 0:e979170e02e7 | 207 | Transform(md4); |
ashleymills | 0:e979170e02e7 | 208 | #ifdef BIG_ENDIAN_ORDER |
ashleymills | 0:e979170e02e7 | 209 | ByteReverseWords(md4->digest, md4->digest, MD4_DIGEST_SIZE); |
ashleymills | 0:e979170e02e7 | 210 | #endif |
ashleymills | 0:e979170e02e7 | 211 | XMEMCPY(hash, md4->digest, MD4_DIGEST_SIZE); |
ashleymills | 0:e979170e02e7 | 212 | |
ashleymills | 0:e979170e02e7 | 213 | InitMd4(md4); /* reset state */ |
ashleymills | 0:e979170e02e7 | 214 | } |
ashleymills | 0:e979170e02e7 | 215 | |
ashleymills | 0:e979170e02e7 | 216 | |
ashleymills | 0:e979170e02e7 | 217 | #endif /* NO_MD4 */ |
ashleymills | 0:e979170e02e7 | 218 |