Skip to content

Commit 06edd6d

Browse files
committed
crypt: bundle md4 and use it with mbedTLS
mbedTLS v2 optionally includes MD4 support, but it needs to be explicitly opted-in during build time. mbedTLS v3 removes it entirely. Provide our own md4, using the reference implementation from RFC 1320. Use it when compiling with mbedTLS (at any version level).
1 parent e0ebaba commit 06edd6d

File tree

3 files changed

+312
-21
lines changed

3 files changed

+312
-21
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ ELSEIF(CRYPT STREQUAL "mbedtls")
9797
ENDIF()
9898

9999
ADD_DEFINITIONS(-DCRYPT_MBEDTLS)
100-
FILE(GLOB NTLM_SRC_CRYPT "${PATH_SRC}/crypt_mbedtls.c")
100+
FILE(GLOB NTLM_SRC_CRYPT "${PATH_SRC}/crypt_mbedtls.c" "${PATH_SRC}/crypt_builtin_md4.c")
101101

102102
LIST(APPEND NTLM_INCLUDES ${MBEDTLS_INCLUDE_DIR})
103103
LIST(APPEND NTLM_LIBS ${MBEDTLS_LIBRARIES})

src/crypt_builtin_md4.c

Lines changed: 311 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,311 @@
1+
/*
2+
* Copyright (c) Edward Thomson. All rights reserved.
3+
*
4+
* This file is part of ntlmclient, distributed under the MIT license.
5+
* For full terms and copyright information, and for third-party
6+
* copyright information, see the included LICENSE.txt file.
7+
*/
8+
9+
#include <stdlib.h>
10+
#include <string.h>
11+
12+
#include "ntlm.h"
13+
#include "crypt.h"
14+
15+
/*
16+
* Below is the MD4 code from RFC 1320, with minor modifications
17+
* to make it compile on a modern compiler. It is included since
18+
* many system crypto libraries lack MD4, sensibly.
19+
*/
20+
21+
/* MD4C.C - RSA Data Security, Inc., MD4 message-digest algorithm
22+
*/
23+
24+
/* Copyright (C) 1990-2, RSA Data Security, Inc. All rights reserved.
25+
26+
License to copy and use this software is granted provided that it
27+
is identified as the "RSA Data Security, Inc. MD4 Message-Digest
28+
Algorithm" in all material mentioning or referencing this software
29+
or this function.
30+
31+
License is also granted to make and use derivative works provided
32+
that such works are identified as "derived from the RSA Data
33+
Security, Inc. MD4 Message-Digest Algorithm" in all material
34+
mentioning or referencing the derived work.
35+
36+
RSA Data Security, Inc. makes no representations concerning either
37+
the merchantability of this software or the suitability of this
38+
software for any particular purpose. It is provided "as is"
39+
without express or implied warranty of any kind.
40+
41+
These notices must be retained in any copies of any part of this
42+
documentation and/or software.
43+
*/
44+
45+
/* POINTER defines a generic pointer type */
46+
typedef unsigned char *POINTER;
47+
48+
/* UINT2 defines a two byte word */
49+
typedef uint16_t UINT2;
50+
51+
/* UINT4 defines a four byte word */
52+
typedef uint32_t UINT4;
53+
54+
#define MD4_memcpy memcpy
55+
#define MD4_memset memset
56+
57+
/* MD4 context. */
58+
typedef struct {
59+
UINT4 state[4]; /* state (ABCD) */
60+
UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */
61+
unsigned char buffer[64]; /* input buffer */
62+
} MD4_CTX;
63+
64+
/* Constants for MD4Transform routine.
65+
*/
66+
#define S11 3
67+
#define S12 7
68+
#define S13 11
69+
#define S14 19
70+
#define S21 3
71+
#define S22 5
72+
#define S23 9
73+
#define S24 13
74+
#define S31 3
75+
#define S32 9
76+
#define S33 11
77+
#define S34 15
78+
79+
static void MD4Transform(UINT4 [4], const unsigned char [64]);
80+
static void Encode (unsigned char *, UINT4 *, unsigned int);
81+
static void Decode (UINT4 *, const unsigned char *, unsigned int);
82+
83+
static unsigned char PADDING[64] = {
84+
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
85+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
87+
};
88+
89+
/* F, G and H are basic MD4 functions.
90+
*/
91+
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
92+
#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
93+
#define H(x, y, z) ((x) ^ (y) ^ (z))
94+
95+
/* ROTATE_LEFT rotates x left n bits.
96+
*/
97+
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
98+
99+
/* FF, GG and HH are transformations for rounds 1, 2 and 3 */
100+
/* Rotation is separate from addition to prevent recomputation */
101+
#define FF(a, b, c, d, x, s) { \
102+
(a) += F ((b), (c), (d)) + (x); \
103+
(a) = ROTATE_LEFT ((a), (s)); \
104+
}
105+
#define GG(a, b, c, d, x, s) { \
106+
(a) += G ((b), (c), (d)) + (x) + (UINT4)0x5a827999; \
107+
(a) = ROTATE_LEFT ((a), (s)); \
108+
}
109+
#define HH(a, b, c, d, x, s) { \
110+
(a) += H ((b), (c), (d)) + (x) + (UINT4)0x6ed9eba1; \
111+
(a) = ROTATE_LEFT ((a), (s)); \
112+
}
113+
114+
/* MD4 initialization. Begins an MD4 operation, writing a new context.
115+
*/
116+
static void MD4Init (MD4_CTX *context)
117+
{
118+
context->count[0] = context->count[1] = 0;
119+
120+
/* Load magic initialization constants.
121+
*/
122+
context->state[0] = 0x67452301;
123+
context->state[1] = 0xefcdab89;
124+
context->state[2] = 0x98badcfe;
125+
context->state[3] = 0x10325476;
126+
}
127+
128+
/* MD4 block update operation. Continues an MD4 message-digest
129+
operation, processing another message block, and updating the
130+
context.
131+
*/
132+
static void MD4Update (MD4_CTX *context, const unsigned char *input, unsigned int inputLen)
133+
{
134+
unsigned int i, index, partLen;
135+
136+
/* Compute number of bytes mod 64 */
137+
index = (unsigned int)((context->count[0] >> 3) & 0x3F);
138+
/* Update number of bits */
139+
if ((context->count[0] += ((UINT4)inputLen << 3))
140+
< ((UINT4)inputLen << 3))
141+
context->count[1]++;
142+
context->count[1] += ((UINT4)inputLen >> 29);
143+
144+
partLen = 64 - index;
145+
146+
/* Transform as many times as possible.
147+
*/
148+
if (inputLen >= partLen) {
149+
MD4_memcpy
150+
((POINTER)&context->buffer[index], (POINTER)input, partLen);
151+
MD4Transform (context->state, context->buffer);
152+
153+
for (i = partLen; i + 63 < inputLen; i += 64)
154+
MD4Transform (context->state, &input[i]);
155+
156+
index = 0;
157+
}
158+
else
159+
i = 0;
160+
161+
/* Buffer remaining input */
162+
MD4_memcpy
163+
((POINTER)&context->buffer[index], (POINTER)&input[i],
164+
inputLen-i);
165+
}
166+
167+
/* MD4 finalization. Ends an MD4 message-digest operation, writing the
168+
the message digest and zeroizing the context.
169+
*/
170+
static void MD4Final (unsigned char digest[16], MD4_CTX *context)
171+
{
172+
unsigned char bits[8];
173+
unsigned int index, padLen;
174+
175+
/* Save number of bits */
176+
Encode (bits, context->count, 8);
177+
178+
/* Pad out to 56 mod 64.
179+
*/
180+
index = (unsigned int)((context->count[0] >> 3) & 0x3f);
181+
padLen = (index < 56) ? (56 - index) : (120 - index);
182+
MD4Update (context, PADDING, padLen);
183+
184+
/* Append length (before padding) */
185+
MD4Update (context, bits, 8);
186+
/* Store state in digest */
187+
Encode (digest, context->state, 16);
188+
189+
/* Zeroize sensitive information.
190+
*/
191+
MD4_memset ((POINTER)context, 0, sizeof (*context));
192+
}
193+
194+
/* MD4 basic transformation. Transforms state based on block.
195+
*/
196+
static void MD4Transform (UINT4 state[4], const unsigned char block[64])
197+
{
198+
UINT4 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
199+
200+
Decode (x, block, 64);
201+
202+
/* Round 1 */
203+
FF (a, b, c, d, x[ 0], S11); /* 1 */
204+
FF (d, a, b, c, x[ 1], S12); /* 2 */
205+
FF (c, d, a, b, x[ 2], S13); /* 3 */
206+
FF (b, c, d, a, x[ 3], S14); /* 4 */
207+
FF (a, b, c, d, x[ 4], S11); /* 5 */
208+
FF (d, a, b, c, x[ 5], S12); /* 6 */
209+
FF (c, d, a, b, x[ 6], S13); /* 7 */
210+
FF (b, c, d, a, x[ 7], S14); /* 8 */
211+
FF (a, b, c, d, x[ 8], S11); /* 9 */
212+
FF (d, a, b, c, x[ 9], S12); /* 10 */
213+
FF (c, d, a, b, x[10], S13); /* 11 */
214+
FF (b, c, d, a, x[11], S14); /* 12 */
215+
FF (a, b, c, d, x[12], S11); /* 13 */
216+
FF (d, a, b, c, x[13], S12); /* 14 */
217+
FF (c, d, a, b, x[14], S13); /* 15 */
218+
FF (b, c, d, a, x[15], S14); /* 16 */
219+
220+
/* Round 2 */
221+
GG (a, b, c, d, x[ 0], S21); /* 17 */
222+
GG (d, a, b, c, x[ 4], S22); /* 18 */
223+
GG (c, d, a, b, x[ 8], S23); /* 19 */
224+
GG (b, c, d, a, x[12], S24); /* 20 */
225+
GG (a, b, c, d, x[ 1], S21); /* 21 */
226+
GG (d, a, b, c, x[ 5], S22); /* 22 */
227+
GG (c, d, a, b, x[ 9], S23); /* 23 */
228+
GG (b, c, d, a, x[13], S24); /* 24 */
229+
GG (a, b, c, d, x[ 2], S21); /* 25 */
230+
GG (d, a, b, c, x[ 6], S22); /* 26 */
231+
GG (c, d, a, b, x[10], S23); /* 27 */
232+
GG (b, c, d, a, x[14], S24); /* 28 */
233+
GG (a, b, c, d, x[ 3], S21); /* 29 */
234+
GG (d, a, b, c, x[ 7], S22); /* 30 */
235+
GG (c, d, a, b, x[11], S23); /* 31 */
236+
GG (b, c, d, a, x[15], S24); /* 32 */
237+
238+
/* Round 3 */
239+
HH (a, b, c, d, x[ 0], S31); /* 33 */
240+
HH (d, a, b, c, x[ 8], S32); /* 34 */
241+
HH (c, d, a, b, x[ 4], S33); /* 35 */
242+
HH (b, c, d, a, x[12], S34); /* 36 */
243+
HH (a, b, c, d, x[ 2], S31); /* 37 */
244+
HH (d, a, b, c, x[10], S32); /* 38 */
245+
HH (c, d, a, b, x[ 6], S33); /* 39 */
246+
HH (b, c, d, a, x[14], S34); /* 40 */
247+
HH (a, b, c, d, x[ 1], S31); /* 41 */
248+
HH (d, a, b, c, x[ 9], S32); /* 42 */
249+
HH (c, d, a, b, x[ 5], S33); /* 43 */
250+
HH (b, c, d, a, x[13], S34); /* 44 */
251+
HH (a, b, c, d, x[ 3], S31); /* 45 */
252+
HH (d, a, b, c, x[11], S32); /* 46 */
253+
HH (c, d, a, b, x[ 7], S33); /* 47 */
254+
HH (b, c, d, a, x[15], S34); /* 48 */
255+
256+
state[0] += a;
257+
state[1] += b;
258+
state[2] += c;
259+
state[3] += d;
260+
261+
/* Zeroize sensitive information.
262+
*/
263+
MD4_memset ((POINTER)x, 0, sizeof (x));
264+
}
265+
266+
/* Encodes input (UINT4) into output (unsigned char). Assumes len is
267+
a multiple of 4.
268+
*/
269+
static void Encode (unsigned char *output, UINT4 *input, unsigned int len)
270+
{
271+
unsigned int i, j;
272+
273+
for (i = 0, j = 0; j < len; i++, j += 4) {
274+
output[j] = (unsigned char)(input[i] & 0xff);
275+
output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
276+
output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
277+
output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
278+
}
279+
}
280+
281+
/* Decodes input (unsigned char) into output (UINT4). Assumes len is
282+
a multiple of 4.
283+
*/
284+
static void Decode (UINT4 *output, const unsigned char *input, unsigned int len)
285+
{
286+
unsigned int i, j;
287+
288+
for (i = 0, j = 0; j < len; i++, j += 4)
289+
output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
290+
(((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
291+
}
292+
293+
bool ntlm_md4_digest(
294+
unsigned char out[CRYPT_MD4_DIGESTSIZE],
295+
ntlm_client *ntlm,
296+
const unsigned char *in,
297+
size_t in_len)
298+
{
299+
MD4_CTX ctx;
300+
301+
NTLM_UNUSED(ntlm);
302+
303+
if (in_len > UINT_MAX)
304+
return false;
305+
306+
MD4Init(&ctx);
307+
MD4Update(&ctx, in, (unsigned int)in_len);
308+
MD4Final (out, &ctx);
309+
310+
return true;
311+
}

src/crypt_mbedtls.c

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "mbedtls/ctr_drbg.h"
1313
#include "mbedtls/des.h"
1414
#include "mbedtls/entropy.h"
15-
#include "mbedtls/md4.h"
1615

1716
#include "ntlm.h"
1817
#include "crypt.h"
@@ -88,25 +87,6 @@ bool ntlm_des_encrypt(
8887
return success;
8988
}
9089

91-
bool ntlm_md4_digest(
92-
unsigned char out[CRYPT_MD4_DIGESTSIZE],
93-
ntlm_client *ntlm,
94-
const unsigned char *in,
95-
size_t in_len)
96-
{
97-
mbedtls_md4_context ctx;
98-
99-
NTLM_UNUSED(ntlm);
100-
101-
mbedtls_md4_init(&ctx);
102-
mbedtls_md4_starts(&ctx);
103-
mbedtls_md4_update(&ctx, in, in_len);
104-
mbedtls_md4_finish(&ctx, out);
105-
mbedtls_md4_free(&ctx);
106-
107-
return true;
108-
}
109-
11090
bool ntlm_hmac_md5_init(
11191
ntlm_client *ntlm,
11292
const unsigned char *key,

0 commit comments

Comments
 (0)