Debian Codegraph @

deps/curl/lib/md4.c

raw
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <[email protected]>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #if !defined(CURL_DISABLE_CRYPTO_AUTH)
26
27 #include "curl_md4.h"
28 #include "warnless.h"
29
30 #ifdef USE_OPENSSL
31 #include <openssl/opensslconf.h>
32 #endif /* USE_OPENSSL */
33
34 #ifdef USE_MBEDTLS
35 #include <mbedtls/config.h>
36 #include <mbedtls/version.h>
37
38 #if(MBEDTLS_VERSION_NUMBER >= 0x02070000)
39 #define HAS_MBEDTLS_RESULT_CODE_BASED_FUNCTIONS
40 #endif
41 #endif /* USE_MBEDTLS */
42
43 #if defined(USE_GNUTLS_NETTLE)
44
45 #include <nettle/md4.h>
46
47 #include "curl_memory.h"
48
49 /* The last #include file should be: */
50 #include "memdebug.h"
51
52 typedef struct md4_ctx MD4_CTX;
53
54 static void MD4_Init(MD4_CTX *ctx)
55 {
56 md4_init(ctx);
57 }
58
59 static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
60 {
61 md4_update(ctx, size, data);
62 }
63
64 static void MD4_Final(unsigned char *result, MD4_CTX *ctx)
65 {
66 md4_digest(ctx, MD4_DIGEST_SIZE, result);
67 }
68
69 #elif defined(USE_GNUTLS)
70
71 #include <gcrypt.h>
72
73 #include "curl_memory.h"
74
75 /* The last #include file should be: */
76 #include "memdebug.h"
77
78 typedef gcry_md_hd_t MD4_CTX;
79
80 static void MD4_Init(MD4_CTX *ctx)
81 {
82 gcry_md_open(ctx, GCRY_MD_MD4, 0);
83 }
84
85 static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
86 {
87 gcry_md_write(*ctx, data, size);
88 }
89
90 static void MD4_Final(unsigned char *result, MD4_CTX *ctx)
91 {
92 memcpy(result, gcry_md_read(*ctx, 0), MD4_DIGEST_LENGTH);
93 gcry_md_close(*ctx);
94 }
95
96 #elif defined(USE_OPENSSL) && !defined(OPENSSL_NO_MD4)
97 /* When OpenSSL is available we use the MD4-functions from OpenSSL */
98 #include <openssl/md4.h>
99
100 #elif (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && \
101 (__MAC_OS_X_VERSION_MAX_ALLOWED >= 1040)) || \
102 (defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && \
103 (__IPHONE_OS_VERSION_MAX_ALLOWED >= 20000))
104
105 #include <CommonCrypto/CommonDigest.h>
106
107 #include "curl_memory.h"
108
109 /* The last #include file should be: */
110 #include "memdebug.h"
111
112 typedef CC_MD4_CTX MD4_CTX;
113
114 static void MD4_Init(MD4_CTX *ctx)
115 {
116 (void)CC_MD4_Init(ctx);
117 }
118
119 static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
120 {
121 (void)CC_MD4_Update(ctx, data, (CC_LONG)size);
122 }
123
124 static void MD4_Final(unsigned char *result, MD4_CTX *ctx)
125 {
126 (void)CC_MD4_Final(result, ctx);
127 }
128
129 #elif defined(USE_WIN32_CRYPTO)
130
131 #include <wincrypt.h>
132
133 #include "curl_memory.h"
134
135 /* The last #include file should be: */
136 #include "memdebug.h"
137
138 typedef struct {
139 HCRYPTPROV hCryptProv;
140 HCRYPTHASH hHash;
141 } MD4_CTX;
142
143 static void MD4_Init(MD4_CTX *ctx)
144 {
145 ctx->hCryptProv = 0;
146 ctx->hHash = 0;
147
148 if(CryptAcquireContext(&ctx->hCryptProv, NULL, NULL, PROV_RSA_FULL,
149 CRYPT_VERIFYCONTEXT)) {
150 CryptCreateHash(ctx->hCryptProv, CALG_MD4, 0, 0, &ctx->hHash);
151 }
152 }
153
154 static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
155 {
156 CryptHashData(ctx->hHash, (BYTE *)data, (unsigned int) size, 0);
157 }
158
159 static void MD4_Final(unsigned char *result, MD4_CTX *ctx)
160 {
161 unsigned long length = 0;
162
163 CryptGetHashParam(ctx->hHash, HP_HASHVAL, NULL, &length, 0);
164 if(length == MD4_DIGEST_LENGTH)
165 CryptGetHashParam(ctx->hHash, HP_HASHVAL, result, &length, 0);
166
167 if(ctx->hHash)
168 CryptDestroyHash(ctx->hHash);
169
170 if(ctx->hCryptProv)
171 CryptReleaseContext(ctx->hCryptProv, 0);
172 }
173
174 #elif(defined(USE_MBEDTLS) && defined(MBEDTLS_MD4_C))
175
176 #include <mbedtls/md4.h>
177
178 #include "curl_memory.h"
179
180 /* The last #include file should be: */
181 #include "memdebug.h"
182
183 typedef struct {
184 void *data;
185 unsigned long size;
186 } MD4_CTX;
187
188 static void MD4_Init(MD4_CTX *ctx)
189 {
190 ctx->data = NULL;
191 ctx->size = 0;
192 }
193
194 static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
195 {
196 if(ctx->data == NULL) {
197 ctx->data = malloc(size);
198 if(ctx->data != NULL) {
199 memcpy(ctx->data, data, size);
200 ctx->size = size;
201 }
202 }
203 }
204
205 static void MD4_Final(unsigned char *result, MD4_CTX *ctx)
206 {
207 if(ctx->data != NULL) {
208 #if !defined(HAS_MBEDTLS_RESULT_CODE_BASED_FUNCTIONS)
209 mbedtls_md4(ctx->data, ctx->size, result);
210 #else
211 (void) mbedtls_md4_ret(ctx->data, ctx->size, result);
212 #endif
213
214 Curl_safefree(ctx->data);
215 ctx->size = 0;
216 }
217 }
218
219 #else
220 /* When no other crypto library is available, or the crypto library doesn't
221 * support MD4, we use this code segment this implementation of it
222 *
223 * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
224 * MD4 Message-Digest Algorithm (RFC 1320).
225 *
226 * Homepage:
227 https://openwall.info/wiki/people/solar/software/public-domain-source-code/md4
228 *
229 * Author:
230 * Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
231 *
232 * This software was written by Alexander Peslyak in 2001. No copyright is
233 * claimed, and the software is hereby placed in the public domain. In case
234 * this attempt to disclaim copyright and place the software in the public
235 * domain is deemed null and void, then the software is Copyright (c) 2001
236 * Alexander Peslyak and it is hereby released to the general public under the
237 * following terms:
238 *
239 * Redistribution and use in source and binary forms, with or without
240 * modification, are permitted.
241 *
242 * There's ABSOLUTELY NO WARRANTY, express or implied.
243 *
244 * (This is a heavily cut-down "BSD license".)
245 *
246 * This differs from Colin Plumb's older public domain implementation in that
247 * no exactly 32-bit integer data type is required (any 32-bit or wider
248 * unsigned integer data type will do), there's no compile-time endianness
249 * configuration, and the function prototypes match OpenSSL's. No code from
250 * Colin Plumb's implementation has been reused; this comment merely compares
251 * the properties of the two independent implementations.
252 *
253 * The primary goals of this implementation are portability and ease of use.
254 * It is meant to be fast, but not as fast as possible. Some known
255 * optimizations are not included to reduce source code size and avoid
256 * compile-time configuration.
257 */
258
259
260 #include <string.h>
261
262 /* Any 32-bit or wider unsigned integer data type will do */
263 typedef unsigned int MD4_u32plus;
264
265 typedef struct {
266 MD4_u32plus lo, hi;
267 MD4_u32plus a, b, c, d;
268 unsigned char buffer[64];
269 MD4_u32plus block[16];
270 } MD4_CTX;
271
272 static void MD4_Init(MD4_CTX *ctx);
273 static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size);
274 static void MD4_Final(unsigned char *result, MD4_CTX *ctx);
275
276 /*
277 * The basic MD4 functions.
278 *
279 * F and G are optimized compared to their RFC 1320 definitions, with the
280 * optimization for F borrowed from Colin Plumb's MD5 implementation.
281 */
282 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
283 #define G(x, y, z) (((x) & ((y) | (z))) | ((y) & (z)))
284 #define H(x, y, z) ((x) ^ (y) ^ (z))
285
286 /*
287 * The MD4 transformation for all three rounds.
288 */
289 #define STEP(f, a, b, c, d, x, s) \
290 (a) += f((b), (c), (d)) + (x); \
291 (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s))));
292
293 /*
294 * SET reads 4 input bytes in little-endian byte order and stores them
295 * in a properly aligned word in host byte order.
296 *
297 * The check for little-endian architectures that tolerate unaligned
298 * memory accesses is just an optimization. Nothing will break if it
299 * doesn't work.
300 */
301 #if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
302 #define SET(n) \
303 (*(MD4_u32plus *)(void *)&ptr[(n) * 4])
304 #define GET(n) \
305 SET(n)
306 #else
307 #define SET(n) \
308 (ctx->block[(n)] = \
309 (MD4_u32plus)ptr[(n) * 4] | \
310 ((MD4_u32plus)ptr[(n) * 4 + 1] << 8) | \
311 ((MD4_u32plus)ptr[(n) * 4 + 2] << 16) | \
312 ((MD4_u32plus)ptr[(n) * 4 + 3] << 24))
313 #define GET(n) \
314 (ctx->block[(n)])
315 #endif
316
317 /*
318 * This processes one or more 64-byte data blocks, but does NOT update
319 * the bit counters. There are no alignment requirements.
320 */
321 static const void *body(MD4_CTX *ctx, const void *data, unsigned long size)
322 {
323 const unsigned char *ptr;
324 MD4_u32plus a, b, c, d;
325
326 ptr = (const unsigned char *)data;
327
328 a = ctx->a;
329 b = ctx->b;
330 c = ctx->c;
331 d = ctx->d;
332
333 do {
334 MD4_u32plus saved_a, saved_b, saved_c, saved_d;
335
336 saved_a = a;
337 saved_b = b;
338 saved_c = c;
339 saved_d = d;
340
341 /* Round 1 */
342 STEP(F, a, b, c, d, SET(0), 3)
343 STEP(F, d, a, b, c, SET(1), 7)
344 STEP(F, c, d, a, b, SET(2), 11)
345 STEP(F, b, c, d, a, SET(3), 19)
346 STEP(F, a, b, c, d, SET(4), 3)
347 STEP(F, d, a, b, c, SET(5), 7)
348 STEP(F, c, d, a, b, SET(6), 11)
349 STEP(F, b, c, d, a, SET(7), 19)
350 STEP(F, a, b, c, d, SET(8), 3)
351 STEP(F, d, a, b, c, SET(9), 7)
352 STEP(F, c, d, a, b, SET(10), 11)
353 STEP(F, b, c, d, a, SET(11), 19)
354 STEP(F, a, b, c, d, SET(12), 3)
355 STEP(F, d, a, b, c, SET(13), 7)
356 STEP(F, c, d, a, b, SET(14), 11)
357 STEP(F, b, c, d, a, SET(15), 19)
358
359 /* Round 2 */
360 STEP(G, a, b, c, d, GET(0) + 0x5a827999, 3)
361 STEP(G, d, a, b, c, GET(4) + 0x5a827999, 5)
362 STEP(G, c, d, a, b, GET(8) + 0x5a827999, 9)
363 STEP(G, b, c, d, a, GET(12) + 0x5a827999, 13)
364 STEP(G, a, b, c, d, GET(1) + 0x5a827999, 3)
365 STEP(G, d, a, b, c, GET(5) + 0x5a827999, 5)
366 STEP(G, c, d, a, b, GET(9) + 0x5a827999, 9)
367 STEP(G, b, c, d, a, GET(13) + 0x5a827999, 13)
368 STEP(G, a, b, c, d, GET(2) + 0x5a827999, 3)
369 STEP(G, d, a, b, c, GET(6) + 0x5a827999, 5)
370 STEP(G, c, d, a, b, GET(10) + 0x5a827999, 9)
371 STEP(G, b, c, d, a, GET(14) + 0x5a827999, 13)
372 STEP(G, a, b, c, d, GET(3) + 0x5a827999, 3)
373 STEP(G, d, a, b, c, GET(7) + 0x5a827999, 5)
374 STEP(G, c, d, a, b, GET(11) + 0x5a827999, 9)
375 STEP(G, b, c, d, a, GET(15) + 0x5a827999, 13)
376
377 /* Round 3 */
378 STEP(H, a, b, c, d, GET(0) + 0x6ed9eba1, 3)
379 STEP(H, d, a, b, c, GET(8) + 0x6ed9eba1, 9)
380 STEP(H, c, d, a, b, GET(4) + 0x6ed9eba1, 11)
381 STEP(H, b, c, d, a, GET(12) + 0x6ed9eba1, 15)
382 STEP(H, a, b, c, d, GET(2) + 0x6ed9eba1, 3)
383 STEP(H, d, a, b, c, GET(10) + 0x6ed9eba1, 9)
384 STEP(H, c, d, a, b, GET(6) + 0x6ed9eba1, 11)
385 STEP(H, b, c, d, a, GET(14) + 0x6ed9eba1, 15)
386 STEP(H, a, b, c, d, GET(1) + 0x6ed9eba1, 3)
387 STEP(H, d, a, b, c, GET(9) + 0x6ed9eba1, 9)
388 STEP(H, c, d, a, b, GET(5) + 0x6ed9eba1, 11)
389 STEP(H, b, c, d, a, GET(13) + 0x6ed9eba1, 15)
390 STEP(H, a, b, c, d, GET(3) + 0x6ed9eba1, 3)
391 STEP(H, d, a, b, c, GET(11) + 0x6ed9eba1, 9)
392 STEP(H, c, d, a, b, GET(7) + 0x6ed9eba1, 11)
393 STEP(H, b, c, d, a, GET(15) + 0x6ed9eba1, 15)
394
395 a += saved_a;
396 b += saved_b;
397 c += saved_c;
398 d += saved_d;
399
400 ptr += 64;
401 } while(size -= 64);
402
403 ctx->a = a;
404 ctx->b = b;
405 ctx->c = c;
406 ctx->d = d;
407
408 return ptr;
409 }
410
411 static void MD4_Init(MD4_CTX *ctx)
412 {
413 ctx->a = 0x67452301;
414 ctx->b = 0xefcdab89;
415 ctx->c = 0x98badcfe;
416 ctx->d = 0x10325476;
417
418 ctx->lo = 0;
419 ctx->hi = 0;
420 }
421
422 static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
423 {
424 MD4_u32plus saved_lo;
425 unsigned long used;
426
427 saved_lo = ctx->lo;
428 ctx->lo = (saved_lo + size) & 0x1fffffff;
429 if(ctx->lo < saved_lo)
430 ctx->hi++;
431 ctx->hi += (MD4_u32plus)size >> 29;
432
433 used = saved_lo & 0x3f;
434
435 if(used) {
436 unsigned long available = 64 - used;
437
438 if(size < available) {
439 memcpy(&ctx->buffer[used], data, size);
440 return;
441 }
442
443 memcpy(&ctx->buffer[used], data, available);
444 data = (const unsigned char *)data + available;
445 size -= available;
446 body(ctx, ctx->buffer, 64);
447 }
448
449 if(size >= 64) {
450 data = body(ctx, data, size & ~(unsigned long)0x3f);
451 size &= 0x3f;
452 }
453
454 memcpy(ctx->buffer, data, size);
455 }
456
457 static void MD4_Final(unsigned char *result, MD4_CTX *ctx)
458 {
459 unsigned long used, available;
460
461 used = ctx->lo & 0x3f;
462
463 ctx->buffer[used++] = 0x80;
464
465 available = 64 - used;
466
467 if(available < 8) {
468 memset(&ctx->buffer[used], 0, available);
469 body(ctx, ctx->buffer, 64);
470 used = 0;
471 available = 64;
472 }
473
474 memset(&ctx->buffer[used], 0, available - 8);
475
476 ctx->lo <<= 3;
477 ctx->buffer[56] = curlx_ultouc((ctx->lo)&0xff);
478 ctx->buffer[57] = curlx_ultouc((ctx->lo >> 8)&0xff);
479 ctx->buffer[58] = curlx_ultouc((ctx->lo >> 16)&0xff);
480 ctx->buffer[59] = curlx_ultouc((ctx->lo >> 24)&0xff);
481 ctx->buffer[60] = curlx_ultouc((ctx->hi)&0xff);
482 ctx->buffer[61] = curlx_ultouc((ctx->hi >> 8)&0xff);
483 ctx->buffer[62] = curlx_ultouc((ctx->hi >> 16)&0xff);
484 ctx->buffer[63] = curlx_ultouc(ctx->hi >> 24);
485
486 body(ctx, ctx->buffer, 64);
487
488 result[0] = curlx_ultouc((ctx->a)&0xff);
489 result[1] = curlx_ultouc((ctx->a >> 8)&0xff);
490 result[2] = curlx_ultouc((ctx->a >> 16)&0xff);
491 result[3] = curlx_ultouc(ctx->a >> 24);
492 result[4] = curlx_ultouc((ctx->b)&0xff);
493 result[5] = curlx_ultouc((ctx->b >> 8)&0xff);
494 result[6] = curlx_ultouc((ctx->b >> 16)&0xff);
495 result[7] = curlx_ultouc(ctx->b >> 24);
496 result[8] = curlx_ultouc((ctx->c)&0xff);
497 result[9] = curlx_ultouc((ctx->c >> 8)&0xff);
498 result[10] = curlx_ultouc((ctx->c >> 16)&0xff);
499 result[11] = curlx_ultouc(ctx->c >> 24);
500 result[12] = curlx_ultouc((ctx->d)&0xff);
501 result[13] = curlx_ultouc((ctx->d >> 8)&0xff);
502 result[14] = curlx_ultouc((ctx->d >> 16)&0xff);
503 result[15] = curlx_ultouc(ctx->d >> 24);
504
505 memset(ctx, 0, sizeof(*ctx));
506 }
507
508 #endif /* CRYPTO LIBS */
509
510 void Curl_md4it(unsigned char *output, const unsigned char *input,
511 const size_t len)
512 {
513 MD4_CTX ctx;
514
515 MD4_Init(&ctx);
516 MD4_Update(&ctx, input, curlx_uztoui(len));
517 MD4_Final(output, &ctx);
518 }
519
520 #endif /* CURL_DISABLE_CRYPTO_AUTH */