Fork of CyaSSL for my specific settings

Dependents:   CyaSSL_Example

Fork of CyaSSL by wolf SSL

Committer:
d0773d
Date:
Tue Mar 03 22:52:52 2015 +0000
Revision:
4:28ac50e1d49c
Parent:
0:1239e9b70ca2
CyaSSL example

Who changed what in which revision?

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