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