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