md4.c 14 KB

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