1 /* $OpenBSD: md4.c,v 1.7 2023/08/10 13:41:56 jsing Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59 #include <stdio.h> 60 #include <stdlib.h> 61 #include <string.h> 62 63 #include <openssl/opensslconf.h> 64 #include <openssl/md4.h> 65 66 __BEGIN_HIDDEN_DECLS 67 68 void md4_block_data_order (MD4_CTX *c, const void *p, size_t num); 69 70 __END_HIDDEN_DECLS 71 72 #define DATA_ORDER_IS_LITTLE_ENDIAN 73 74 #define HASH_LONG MD4_LONG 75 #define HASH_CTX MD4_CTX 76 #define HASH_CBLOCK MD4_CBLOCK 77 #define HASH_UPDATE MD4_Update 78 #define HASH_TRANSFORM MD4_Transform 79 #define HASH_FINAL MD4_Final 80 #define HASH_MAKE_STRING(c,s) do { \ 81 unsigned long ll; \ 82 ll=(c)->A; HOST_l2c(ll,(s)); \ 83 ll=(c)->B; HOST_l2c(ll,(s)); \ 84 ll=(c)->C; HOST_l2c(ll,(s)); \ 85 ll=(c)->D; HOST_l2c(ll,(s)); \ 86 } while (0) 87 #define HASH_BLOCK_DATA_ORDER md4_block_data_order 88 89 #include "md32_common.h" 90 LCRYPTO_ALIAS(MD4_Update); 91 LCRYPTO_ALIAS(MD4_Final); 92 LCRYPTO_ALIAS(MD4_Transform); 93 94 /* 95 #define F(x,y,z) (((x) & (y)) | ((~(x)) & (z))) 96 #define G(x,y,z) (((x) & (y)) | ((x) & ((z))) | ((y) & ((z)))) 97 */ 98 99 /* As pointed out by Wei Dai <weidai@eskimo.com>, the above can be 100 * simplified to the code below. Wei attributes these optimizations 101 * to Peter Gutmann's SHS code, and he attributes it to Rich Schroeppel. 102 */ 103 #define F(b,c,d) ((((c) ^ (d)) & (b)) ^ (d)) 104 #define G(b,c,d) (((b) & (c)) | ((b) & (d)) | ((c) & (d))) 105 #define H(b,c,d) ((b) ^ (c) ^ (d)) 106 107 #define R0(a,b,c,d,k,s,t) { \ 108 a+=((k)+(t)+F((b),(c),(d))); \ 109 a=ROTATE(a,s); }; 110 111 #define R1(a,b,c,d,k,s,t) { \ 112 a+=((k)+(t)+G((b),(c),(d))); \ 113 a=ROTATE(a,s); };\ 114 115 #define R2(a,b,c,d,k,s,t) { \ 116 a+=((k)+(t)+H((b),(c),(d))); \ 117 a=ROTATE(a,s); }; 118 119 /* Implemented from RFC1186 The MD4 Message-Digest Algorithm 120 */ 121 122 #define INIT_DATA_A (unsigned long)0x67452301L 123 #define INIT_DATA_B (unsigned long)0xefcdab89L 124 #define INIT_DATA_C (unsigned long)0x98badcfeL 125 #define INIT_DATA_D (unsigned long)0x10325476L 126 127 int 128 MD4_Init(MD4_CTX *c) 129 { 130 memset (c, 0, sizeof(*c)); 131 c->A = INIT_DATA_A; 132 c->B = INIT_DATA_B; 133 c->C = INIT_DATA_C; 134 c->D = INIT_DATA_D; 135 return 1; 136 } 137 LCRYPTO_ALIAS(MD4_Init); 138 139 #ifndef md4_block_data_order 140 #ifdef X 141 #undef X 142 #endif 143 void 144 md4_block_data_order(MD4_CTX *c, const void *data_, size_t num) 145 { 146 const unsigned char *data = data_; 147 unsigned int A, B, C, D, l; 148 unsigned int X0, X1, X2, X3, X4, X5, X6, X7, 149 X8, X9, X10, X11, X12, X13, X14, X15; 150 151 A = c->A; 152 B = c->B; 153 C = c->C; 154 D = c->D; 155 156 for (; num--; ) { 157 HOST_c2l(data, l); 158 X0 = l; 159 HOST_c2l(data, l); 160 X1 = l; 161 /* Round 0 */ 162 R0(A, B, C, D, X0, 3, 0); 163 HOST_c2l(data, l); 164 X2 = l; 165 R0(D, A, B, C, X1, 7, 0); 166 HOST_c2l(data, l); 167 X3 = l; 168 R0(C, D, A, B, X2, 11, 0); 169 HOST_c2l(data, l); 170 X4 = l; 171 R0(B, C, D, A, X3, 19, 0); 172 HOST_c2l(data, l); 173 X5 = l; 174 R0(A, B, C, D, X4, 3, 0); 175 HOST_c2l(data, l); 176 X6 = l; 177 R0(D, A, B, C, X5, 7, 0); 178 HOST_c2l(data, l); 179 X7 = l; 180 R0(C, D, A, B, X6, 11, 0); 181 HOST_c2l(data, l); 182 X8 = l; 183 R0(B, C, D, A, X7, 19, 0); 184 HOST_c2l(data, l); 185 X9 = l; 186 R0(A, B, C, D, X8, 3, 0); 187 HOST_c2l(data, l); 188 X10 = l; 189 R0(D, A,B, C,X9, 7, 0); 190 HOST_c2l(data, l); 191 X11 = l; 192 R0(C, D,A, B,X10, 11, 0); 193 HOST_c2l(data, l); 194 X12 = l; 195 R0(B, C,D, A,X11, 19, 0); 196 HOST_c2l(data, l); 197 X13 = l; 198 R0(A, B,C, D,X12, 3, 0); 199 HOST_c2l(data, l); 200 X14 = l; 201 R0(D, A,B, C,X13, 7, 0); 202 HOST_c2l(data, l); 203 X15 = l; 204 R0(C, D,A, B,X14, 11, 0); 205 R0(B, C,D, A,X15, 19, 0); 206 /* Round 1 */ 207 R1(A, B, C, D, X0, 3, 0x5A827999L); 208 R1(D, A, B, C, X4, 5, 0x5A827999L); 209 R1(C, D, A, B, X8, 9, 0x5A827999L); 210 R1(B, C, D, A, X12, 13, 0x5A827999L); 211 R1(A, B, C, D, X1, 3, 0x5A827999L); 212 R1(D, A, B, C, X5, 5, 0x5A827999L); 213 R1(C, D, A, B, X9, 9, 0x5A827999L); 214 R1(B, C, D, A, X13, 13, 0x5A827999L); 215 R1(A, B, C, D, X2, 3, 0x5A827999L); 216 R1(D, A, B, C, X6, 5, 0x5A827999L); 217 R1(C, D, A, B, X10, 9, 0x5A827999L); 218 R1(B, C, D, A, X14, 13, 0x5A827999L); 219 R1(A, B, C, D, X3, 3, 0x5A827999L); 220 R1(D, A, B, C, X7, 5, 0x5A827999L); 221 R1(C, D, A, B, X11, 9, 0x5A827999L); 222 R1(B, C, D, A, X15, 13, 0x5A827999L); 223 /* Round 2 */ 224 R2(A, B, C, D, X0, 3, 0x6ED9EBA1L); 225 R2(D, A, B, C, X8, 9, 0x6ED9EBA1L); 226 R2(C, D, A, B, X4, 11, 0x6ED9EBA1L); 227 R2(B, C, D, A, X12, 15, 0x6ED9EBA1L); 228 R2(A, B, C, D, X2, 3, 0x6ED9EBA1L); 229 R2(D, A, B, C, X10, 9, 0x6ED9EBA1L); 230 R2(C, D, A, B, X6, 11, 0x6ED9EBA1L); 231 R2(B, C, D, A, X14, 15, 0x6ED9EBA1L); 232 R2(A, B, C, D, X1, 3, 0x6ED9EBA1L); 233 R2(D, A, B, C, X9, 9, 0x6ED9EBA1L); 234 R2(C, D, A, B, X5, 11, 0x6ED9EBA1L); 235 R2(B, C, D, A, X13, 15, 0x6ED9EBA1L); 236 R2(A, B, C, D, X3, 3, 0x6ED9EBA1L); 237 R2(D, A, B, C, X11, 9, 0x6ED9EBA1L); 238 R2(C, D, A, B, X7, 11, 0x6ED9EBA1L); 239 R2(B, C, D, A, X15, 15, 0x6ED9EBA1L); 240 241 A = c->A += A; 242 B = c->B += B; 243 C = c->C += C; 244 D = c->D += D; 245 } 246 } 247 #endif 248 249 unsigned char * 250 MD4(const unsigned char *d, size_t n, unsigned char *md) 251 { 252 MD4_CTX c; 253 static unsigned char m[MD4_DIGEST_LENGTH]; 254 255 if (md == NULL) 256 md = m; 257 if (!MD4_Init(&c)) 258 return NULL; 259 MD4_Update(&c, d, n); 260 MD4_Final(md, &c); 261 explicit_bzero(&c, sizeof(c)); 262 return (md); 263 } 264 LCRYPTO_ALIAS(MD4); 265