md4.c 14 KB

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