md4.c 14 KB

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