md4.c 14 KB

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