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