md4.c 14 KB

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