A library for setting up Secure Socket Layer (SSL) connections and verifying remote hosts using certificates. Contains only the source files for mbed platform implementation of the library.

Dependents:   HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL HTTPClient-SSL

Committer:
Vanger
Date:
Mon Jan 19 21:45:42 2015 +0000
Revision:
0:b86d15c6ba29
Updated CyaSSL Library to 3.3.0. Changed Settings and functions to be implemented for mbed platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Vanger 0:b86d15c6ba29 1 /* md4.c
Vanger 0:b86d15c6ba29 2 *
Vanger 0:b86d15c6ba29 3 * Copyright (C) 2006-2014 wolfSSL Inc.
Vanger 0:b86d15c6ba29 4 *
Vanger 0:b86d15c6ba29 5 * This file is part of CyaSSL.
Vanger 0:b86d15c6ba29 6 *
Vanger 0:b86d15c6ba29 7 * CyaSSL is free software; you can redistribute it and/or modify
Vanger 0:b86d15c6ba29 8 * it under the terms of the GNU General Public License as published by
Vanger 0:b86d15c6ba29 9 * the Free Software Foundation; either version 2 of the License, or
Vanger 0:b86d15c6ba29 10 * (at your option) any later version.
Vanger 0:b86d15c6ba29 11 *
Vanger 0:b86d15c6ba29 12 * CyaSSL is distributed in the hope that it will be useful,
Vanger 0:b86d15c6ba29 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Vanger 0:b86d15c6ba29 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Vanger 0:b86d15c6ba29 15 * GNU General Public License for more details.
Vanger 0:b86d15c6ba29 16 *
Vanger 0:b86d15c6ba29 17 * You should have received a copy of the GNU General Public License
Vanger 0:b86d15c6ba29 18 * along with this program; if not, write to the Free Software
Vanger 0:b86d15c6ba29 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
Vanger 0:b86d15c6ba29 20 */
Vanger 0:b86d15c6ba29 21
Vanger 0:b86d15c6ba29 22 #ifdef HAVE_CONFIG_H
Vanger 0:b86d15c6ba29 23 #include <config.h>
Vanger 0:b86d15c6ba29 24 #endif
Vanger 0:b86d15c6ba29 25
Vanger 0:b86d15c6ba29 26 #include <cyassl/ctaocrypt/settings.h>
Vanger 0:b86d15c6ba29 27
Vanger 0:b86d15c6ba29 28 #ifndef NO_MD4
Vanger 0:b86d15c6ba29 29
Vanger 0:b86d15c6ba29 30 #include <cyassl/ctaocrypt/md4.h>
Vanger 0:b86d15c6ba29 31 #ifdef NO_INLINE
Vanger 0:b86d15c6ba29 32 #include <cyassl/ctaocrypt/misc.h>
Vanger 0:b86d15c6ba29 33 #else
Vanger 0:b86d15c6ba29 34 #include <ctaocrypt/src/misc.c>
Vanger 0:b86d15c6ba29 35 #endif
Vanger 0:b86d15c6ba29 36
Vanger 0:b86d15c6ba29 37
Vanger 0:b86d15c6ba29 38 #ifndef min
Vanger 0:b86d15c6ba29 39
Vanger 0:b86d15c6ba29 40 static INLINE word32 min(word32 a, word32 b)
Vanger 0:b86d15c6ba29 41 {
Vanger 0:b86d15c6ba29 42 return a > b ? b : a;
Vanger 0:b86d15c6ba29 43 }
Vanger 0:b86d15c6ba29 44
Vanger 0:b86d15c6ba29 45 #endif /* min */
Vanger 0:b86d15c6ba29 46
Vanger 0:b86d15c6ba29 47
Vanger 0:b86d15c6ba29 48 void InitMd4(Md4* md4)
Vanger 0:b86d15c6ba29 49 {
Vanger 0:b86d15c6ba29 50 md4->digest[0] = 0x67452301L;
Vanger 0:b86d15c6ba29 51 md4->digest[1] = 0xefcdab89L;
Vanger 0:b86d15c6ba29 52 md4->digest[2] = 0x98badcfeL;
Vanger 0:b86d15c6ba29 53 md4->digest[3] = 0x10325476L;
Vanger 0:b86d15c6ba29 54
Vanger 0:b86d15c6ba29 55 md4->buffLen = 0;
Vanger 0:b86d15c6ba29 56 md4->loLen = 0;
Vanger 0:b86d15c6ba29 57 md4->hiLen = 0;
Vanger 0:b86d15c6ba29 58 }
Vanger 0:b86d15c6ba29 59
Vanger 0:b86d15c6ba29 60
Vanger 0:b86d15c6ba29 61 static void Transform(Md4* md4)
Vanger 0:b86d15c6ba29 62 {
Vanger 0:b86d15c6ba29 63 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
Vanger 0:b86d15c6ba29 64 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
Vanger 0:b86d15c6ba29 65 #define H(x, y, z) ((x) ^ (y) ^ (z))
Vanger 0:b86d15c6ba29 66
Vanger 0:b86d15c6ba29 67 /* Copy context->state[] to working vars */
Vanger 0:b86d15c6ba29 68 word32 A = md4->digest[0];
Vanger 0:b86d15c6ba29 69 word32 B = md4->digest[1];
Vanger 0:b86d15c6ba29 70 word32 C = md4->digest[2];
Vanger 0:b86d15c6ba29 71 word32 D = md4->digest[3];
Vanger 0:b86d15c6ba29 72
Vanger 0:b86d15c6ba29 73 #define function(a,b,c,d,k,s) a=rotlFixed(a+F(b,c,d)+md4->buffer[k],s);
Vanger 0:b86d15c6ba29 74 function(A,B,C,D, 0, 3);
Vanger 0:b86d15c6ba29 75 function(D,A,B,C, 1, 7);
Vanger 0:b86d15c6ba29 76 function(C,D,A,B, 2,11);
Vanger 0:b86d15c6ba29 77 function(B,C,D,A, 3,19);
Vanger 0:b86d15c6ba29 78 function(A,B,C,D, 4, 3);
Vanger 0:b86d15c6ba29 79 function(D,A,B,C, 5, 7);
Vanger 0:b86d15c6ba29 80 function(C,D,A,B, 6,11);
Vanger 0:b86d15c6ba29 81 function(B,C,D,A, 7,19);
Vanger 0:b86d15c6ba29 82 function(A,B,C,D, 8, 3);
Vanger 0:b86d15c6ba29 83 function(D,A,B,C, 9, 7);
Vanger 0:b86d15c6ba29 84 function(C,D,A,B,10,11);
Vanger 0:b86d15c6ba29 85 function(B,C,D,A,11,19);
Vanger 0:b86d15c6ba29 86 function(A,B,C,D,12, 3);
Vanger 0:b86d15c6ba29 87 function(D,A,B,C,13, 7);
Vanger 0:b86d15c6ba29 88 function(C,D,A,B,14,11);
Vanger 0:b86d15c6ba29 89 function(B,C,D,A,15,19);
Vanger 0:b86d15c6ba29 90
Vanger 0:b86d15c6ba29 91 #undef function
Vanger 0:b86d15c6ba29 92 #define function(a,b,c,d,k,s) \
Vanger 0:b86d15c6ba29 93 a=rotlFixed(a+G(b,c,d)+md4->buffer[k]+0x5a827999,s);
Vanger 0:b86d15c6ba29 94
Vanger 0:b86d15c6ba29 95 function(A,B,C,D, 0, 3);
Vanger 0:b86d15c6ba29 96 function(D,A,B,C, 4, 5);
Vanger 0:b86d15c6ba29 97 function(C,D,A,B, 8, 9);
Vanger 0:b86d15c6ba29 98 function(B,C,D,A,12,13);
Vanger 0:b86d15c6ba29 99 function(A,B,C,D, 1, 3);
Vanger 0:b86d15c6ba29 100 function(D,A,B,C, 5, 5);
Vanger 0:b86d15c6ba29 101 function(C,D,A,B, 9, 9);
Vanger 0:b86d15c6ba29 102 function(B,C,D,A,13,13);
Vanger 0:b86d15c6ba29 103 function(A,B,C,D, 2, 3);
Vanger 0:b86d15c6ba29 104 function(D,A,B,C, 6, 5);
Vanger 0:b86d15c6ba29 105 function(C,D,A,B,10, 9);
Vanger 0:b86d15c6ba29 106 function(B,C,D,A,14,13);
Vanger 0:b86d15c6ba29 107 function(A,B,C,D, 3, 3);
Vanger 0:b86d15c6ba29 108 function(D,A,B,C, 7, 5);
Vanger 0:b86d15c6ba29 109 function(C,D,A,B,11, 9);
Vanger 0:b86d15c6ba29 110 function(B,C,D,A,15,13);
Vanger 0:b86d15c6ba29 111
Vanger 0:b86d15c6ba29 112 #undef function
Vanger 0:b86d15c6ba29 113 #define function(a,b,c,d,k,s) \
Vanger 0:b86d15c6ba29 114 a=rotlFixed(a+H(b,c,d)+md4->buffer[k]+0x6ed9eba1,s);
Vanger 0:b86d15c6ba29 115
Vanger 0:b86d15c6ba29 116 function(A,B,C,D, 0, 3);
Vanger 0:b86d15c6ba29 117 function(D,A,B,C, 8, 9);
Vanger 0:b86d15c6ba29 118 function(C,D,A,B, 4,11);
Vanger 0:b86d15c6ba29 119 function(B,C,D,A,12,15);
Vanger 0:b86d15c6ba29 120 function(A,B,C,D, 2, 3);
Vanger 0:b86d15c6ba29 121 function(D,A,B,C,10, 9);
Vanger 0:b86d15c6ba29 122 function(C,D,A,B, 6,11);
Vanger 0:b86d15c6ba29 123 function(B,C,D,A,14,15);
Vanger 0:b86d15c6ba29 124 function(A,B,C,D, 1, 3);
Vanger 0:b86d15c6ba29 125 function(D,A,B,C, 9, 9);
Vanger 0:b86d15c6ba29 126 function(C,D,A,B, 5,11);
Vanger 0:b86d15c6ba29 127 function(B,C,D,A,13,15);
Vanger 0:b86d15c6ba29 128 function(A,B,C,D, 3, 3);
Vanger 0:b86d15c6ba29 129 function(D,A,B,C,11, 9);
Vanger 0:b86d15c6ba29 130 function(C,D,A,B, 7,11);
Vanger 0:b86d15c6ba29 131 function(B,C,D,A,15,15);
Vanger 0:b86d15c6ba29 132
Vanger 0:b86d15c6ba29 133 /* Add the working vars back into digest state[] */
Vanger 0:b86d15c6ba29 134 md4->digest[0] += A;
Vanger 0:b86d15c6ba29 135 md4->digest[1] += B;
Vanger 0:b86d15c6ba29 136 md4->digest[2] += C;
Vanger 0:b86d15c6ba29 137 md4->digest[3] += D;
Vanger 0:b86d15c6ba29 138 }
Vanger 0:b86d15c6ba29 139
Vanger 0:b86d15c6ba29 140
Vanger 0:b86d15c6ba29 141 static INLINE void AddLength(Md4* md4, word32 len)
Vanger 0:b86d15c6ba29 142 {
Vanger 0:b86d15c6ba29 143 word32 tmp = md4->loLen;
Vanger 0:b86d15c6ba29 144 if ( (md4->loLen += len) < tmp)
Vanger 0:b86d15c6ba29 145 md4->hiLen++; /* carry low to high */
Vanger 0:b86d15c6ba29 146 }
Vanger 0:b86d15c6ba29 147
Vanger 0:b86d15c6ba29 148
Vanger 0:b86d15c6ba29 149 void Md4Update(Md4* md4, const byte* data, word32 len)
Vanger 0:b86d15c6ba29 150 {
Vanger 0:b86d15c6ba29 151 /* do block size increments */
Vanger 0:b86d15c6ba29 152 byte* local = (byte*)md4->buffer;
Vanger 0:b86d15c6ba29 153
Vanger 0:b86d15c6ba29 154 while (len) {
Vanger 0:b86d15c6ba29 155 word32 add = min(len, MD4_BLOCK_SIZE - md4->buffLen);
Vanger 0:b86d15c6ba29 156 XMEMCPY(&local[md4->buffLen], data, add);
Vanger 0:b86d15c6ba29 157
Vanger 0:b86d15c6ba29 158 md4->buffLen += add;
Vanger 0:b86d15c6ba29 159 data += add;
Vanger 0:b86d15c6ba29 160 len -= add;
Vanger 0:b86d15c6ba29 161
Vanger 0:b86d15c6ba29 162 if (md4->buffLen == MD4_BLOCK_SIZE) {
Vanger 0:b86d15c6ba29 163 #ifdef BIG_ENDIAN_ORDER
Vanger 0:b86d15c6ba29 164 ByteReverseWords(md4->buffer, md4->buffer, MD4_BLOCK_SIZE);
Vanger 0:b86d15c6ba29 165 #endif
Vanger 0:b86d15c6ba29 166 Transform(md4);
Vanger 0:b86d15c6ba29 167 AddLength(md4, MD4_BLOCK_SIZE);
Vanger 0:b86d15c6ba29 168 md4->buffLen = 0;
Vanger 0:b86d15c6ba29 169 }
Vanger 0:b86d15c6ba29 170 }
Vanger 0:b86d15c6ba29 171 }
Vanger 0:b86d15c6ba29 172
Vanger 0:b86d15c6ba29 173
Vanger 0:b86d15c6ba29 174 void Md4Final(Md4* md4, byte* hash)
Vanger 0:b86d15c6ba29 175 {
Vanger 0:b86d15c6ba29 176 byte* local = (byte*)md4->buffer;
Vanger 0:b86d15c6ba29 177
Vanger 0:b86d15c6ba29 178 AddLength(md4, md4->buffLen); /* before adding pads */
Vanger 0:b86d15c6ba29 179
Vanger 0:b86d15c6ba29 180 local[md4->buffLen++] = 0x80; /* add 1 */
Vanger 0:b86d15c6ba29 181
Vanger 0:b86d15c6ba29 182 /* pad with zeros */
Vanger 0:b86d15c6ba29 183 if (md4->buffLen > MD4_PAD_SIZE) {
Vanger 0:b86d15c6ba29 184 XMEMSET(&local[md4->buffLen], 0, MD4_BLOCK_SIZE - md4->buffLen);
Vanger 0:b86d15c6ba29 185 md4->buffLen += MD4_BLOCK_SIZE - md4->buffLen;
Vanger 0:b86d15c6ba29 186
Vanger 0:b86d15c6ba29 187 #ifdef BIG_ENDIAN_ORDER
Vanger 0:b86d15c6ba29 188 ByteReverseWords(md4->buffer, md4->buffer, MD4_BLOCK_SIZE);
Vanger 0:b86d15c6ba29 189 #endif
Vanger 0:b86d15c6ba29 190 Transform(md4);
Vanger 0:b86d15c6ba29 191 md4->buffLen = 0;
Vanger 0:b86d15c6ba29 192 }
Vanger 0:b86d15c6ba29 193 XMEMSET(&local[md4->buffLen], 0, MD4_PAD_SIZE - md4->buffLen);
Vanger 0:b86d15c6ba29 194
Vanger 0:b86d15c6ba29 195 /* put lengths in bits */
Vanger 0:b86d15c6ba29 196 md4->hiLen = (md4->loLen >> (8*sizeof(md4->loLen) - 3)) +
Vanger 0:b86d15c6ba29 197 (md4->hiLen << 3);
Vanger 0:b86d15c6ba29 198 md4->loLen = md4->loLen << 3;
Vanger 0:b86d15c6ba29 199
Vanger 0:b86d15c6ba29 200 /* store lengths */
Vanger 0:b86d15c6ba29 201 #ifdef BIG_ENDIAN_ORDER
Vanger 0:b86d15c6ba29 202 ByteReverseWords(md4->buffer, md4->buffer, MD4_BLOCK_SIZE);
Vanger 0:b86d15c6ba29 203 #endif
Vanger 0:b86d15c6ba29 204 /* ! length ordering dependent on digest endian type ! */
Vanger 0:b86d15c6ba29 205 XMEMCPY(&local[MD4_PAD_SIZE], &md4->loLen, sizeof(word32));
Vanger 0:b86d15c6ba29 206 XMEMCPY(&local[MD4_PAD_SIZE + sizeof(word32)], &md4->hiLen, sizeof(word32));
Vanger 0:b86d15c6ba29 207
Vanger 0:b86d15c6ba29 208 Transform(md4);
Vanger 0:b86d15c6ba29 209 #ifdef BIG_ENDIAN_ORDER
Vanger 0:b86d15c6ba29 210 ByteReverseWords(md4->digest, md4->digest, MD4_DIGEST_SIZE);
Vanger 0:b86d15c6ba29 211 #endif
Vanger 0:b86d15c6ba29 212 XMEMCPY(hash, md4->digest, MD4_DIGEST_SIZE);
Vanger 0:b86d15c6ba29 213
Vanger 0:b86d15c6ba29 214 InitMd4(md4); /* reset state */
Vanger 0:b86d15c6ba29 215 }
Vanger 0:b86d15c6ba29 216
Vanger 0:b86d15c6ba29 217
Vanger 0:b86d15c6ba29 218 #endif /* NO_MD4 */
Vanger 0:b86d15c6ba29 219