wolfSSL SSL/TLS library, support up to TLS1.3

Dependents:   CyaSSL-Twitter-OAuth4Tw Example-client-tls-cert TwitterReader TweetTest ... more

Committer:
wolfSSL
Date:
Tue Aug 22 10:48:22 2017 +0000
Revision:
13:f67a6c6013ca
Parent:
11:cee25a834751
wolfSSL3.12.0 with TLS1.3

Who changed what in which revision?

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