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