1
0

md4.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * !checksrc! disable COPYRIGHT
  3. * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc.
  4. * MD4 Message-Digest Algorithm (RFC 1320).
  5. *
  6. * Homepage:
  7. https://openwall.info/wiki/people/solar/software/public-domain-source-code/md4
  8. *
  9. * Author:
  10. * Alexander Peslyak, better known as Solar Designer <solar at openwall.com>
  11. *
  12. * This software was written by Alexander Peslyak in 2001. No copyright is
  13. * claimed, and the software is hereby placed in the public domain. In case
  14. * this attempt to disclaim copyright and place the software in the public
  15. * domain is deemed null and void, then the software is Copyright (c) 2001
  16. * Alexander Peslyak and it is hereby released to the general public under the
  17. * following terms:
  18. *
  19. * Redistribution and use in source and binary forms, with or without
  20. * modification, are permitted.
  21. *
  22. * There's ABSOLUTELY NO WARRANTY, express or implied.
  23. *
  24. * (This is a heavily cut-down "BSD license".)
  25. *
  26. * This differs from Colin Plumb's older public domain implementation in that
  27. * no exactly 32-bit integer data type is required (any 32-bit or wider
  28. * unsigned integer data type will do), there's no compile-time endianness
  29. * configuration, and the function prototypes match OpenSSL's. No code from
  30. * Colin Plumb's implementation has been reused; this comment merely compares
  31. * the properties of the two independent implementations.
  32. *
  33. * The primary goals of this implementation are portability and ease of use.
  34. * It is meant to be fast, but not as fast as possible. Some known
  35. * optimizations are not included to reduce source code size and avoid
  36. * compile-time configuration.
  37. */
  38. #include "curl_setup.h"
  39. /* The NSS, OS/400, and when not included, OpenSSL and mbed TLS crypto
  40. * libraries do not provide the MD4 hash algorithm, so we use this
  41. * implementation of it */
  42. #if defined(USE_NSS) || defined(USE_OS400CRYPTO) || \
  43. (defined(USE_OPENSSL) && defined(OPENSSL_NO_MD4)) || \
  44. (defined(USE_MBEDTLS) && !defined(MBEDTLS_MD4_C))
  45. #include "curl_md4.h"
  46. #include "warnless.h"
  47. #ifndef HAVE_OPENSSL
  48. #include <string.h>
  49. /* Any 32-bit or wider unsigned integer data type will do */
  50. typedef unsigned int MD4_u32plus;
  51. typedef struct {
  52. MD4_u32plus lo, hi;
  53. MD4_u32plus a, b, c, d;
  54. unsigned char buffer[64];
  55. MD4_u32plus block[16];
  56. } MD4_CTX;
  57. static void MD4_Init(MD4_CTX *ctx);
  58. static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size);
  59. static void MD4_Final(unsigned char *result, MD4_CTX *ctx);
  60. /*
  61. * The basic MD4 functions.
  62. *
  63. * F and G are optimized compared to their RFC 1320 definitions, with the
  64. * optimization for F borrowed from Colin Plumb's MD5 implementation.
  65. */
  66. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  67. #define G(x, y, z) (((x) & ((y) | (z))) | ((y) & (z)))
  68. #define H(x, y, z) ((x) ^ (y) ^ (z))
  69. /*
  70. * The MD4 transformation for all three rounds.
  71. */
  72. #define STEP(f, a, b, c, d, x, s) \
  73. (a) += f((b), (c), (d)) + (x); \
  74. (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s))));
  75. /*
  76. * SET reads 4 input bytes in little-endian byte order and stores them
  77. * in a properly aligned word in host byte order.
  78. *
  79. * The check for little-endian architectures that tolerate unaligned
  80. * memory accesses is just an optimization. Nothing will break if it
  81. * doesn't work.
  82. */
  83. #if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
  84. #define SET(n) \
  85. (*(MD4_u32plus *)(void *)&ptr[(n) * 4])
  86. #define GET(n) \
  87. SET(n)
  88. #else
  89. #define SET(n) \
  90. (ctx->block[(n)] = \
  91. (MD4_u32plus)ptr[(n) * 4] | \
  92. ((MD4_u32plus)ptr[(n) * 4 + 1] << 8) | \
  93. ((MD4_u32plus)ptr[(n) * 4 + 2] << 16) | \
  94. ((MD4_u32plus)ptr[(n) * 4 + 3] << 24))
  95. #define GET(n) \
  96. (ctx->block[(n)])
  97. #endif
  98. /*
  99. * This processes one or more 64-byte data blocks, but does NOT update
  100. * the bit counters. There are no alignment requirements.
  101. */
  102. static const void *body(MD4_CTX *ctx, const void *data, unsigned long size)
  103. {
  104. const unsigned char *ptr;
  105. MD4_u32plus a, b, c, d;
  106. ptr = (const unsigned char *)data;
  107. a = ctx->a;
  108. b = ctx->b;
  109. c = ctx->c;
  110. d = ctx->d;
  111. do {
  112. MD4_u32plus saved_a, saved_b, saved_c, saved_d;
  113. saved_a = a;
  114. saved_b = b;
  115. saved_c = c;
  116. saved_d = d;
  117. /* Round 1 */
  118. STEP(F, a, b, c, d, SET(0), 3)
  119. STEP(F, d, a, b, c, SET(1), 7)
  120. STEP(F, c, d, a, b, SET(2), 11)
  121. STEP(F, b, c, d, a, SET(3), 19)
  122. STEP(F, a, b, c, d, SET(4), 3)
  123. STEP(F, d, a, b, c, SET(5), 7)
  124. STEP(F, c, d, a, b, SET(6), 11)
  125. STEP(F, b, c, d, a, SET(7), 19)
  126. STEP(F, a, b, c, d, SET(8), 3)
  127. STEP(F, d, a, b, c, SET(9), 7)
  128. STEP(F, c, d, a, b, SET(10), 11)
  129. STEP(F, b, c, d, a, SET(11), 19)
  130. STEP(F, a, b, c, d, SET(12), 3)
  131. STEP(F, d, a, b, c, SET(13), 7)
  132. STEP(F, c, d, a, b, SET(14), 11)
  133. STEP(F, b, c, d, a, SET(15), 19)
  134. /* Round 2 */
  135. STEP(G, a, b, c, d, GET(0) + 0x5a827999, 3)
  136. STEP(G, d, a, b, c, GET(4) + 0x5a827999, 5)
  137. STEP(G, c, d, a, b, GET(8) + 0x5a827999, 9)
  138. STEP(G, b, c, d, a, GET(12) + 0x5a827999, 13)
  139. STEP(G, a, b, c, d, GET(1) + 0x5a827999, 3)
  140. STEP(G, d, a, b, c, GET(5) + 0x5a827999, 5)
  141. STEP(G, c, d, a, b, GET(9) + 0x5a827999, 9)
  142. STEP(G, b, c, d, a, GET(13) + 0x5a827999, 13)
  143. STEP(G, a, b, c, d, GET(2) + 0x5a827999, 3)
  144. STEP(G, d, a, b, c, GET(6) + 0x5a827999, 5)
  145. STEP(G, c, d, a, b, GET(10) + 0x5a827999, 9)
  146. STEP(G, b, c, d, a, GET(14) + 0x5a827999, 13)
  147. STEP(G, a, b, c, d, GET(3) + 0x5a827999, 3)
  148. STEP(G, d, a, b, c, GET(7) + 0x5a827999, 5)
  149. STEP(G, c, d, a, b, GET(11) + 0x5a827999, 9)
  150. STEP(G, b, c, d, a, GET(15) + 0x5a827999, 13)
  151. /* Round 3 */
  152. STEP(H, a, b, c, d, GET(0) + 0x6ed9eba1, 3)
  153. STEP(H, d, a, b, c, GET(8) + 0x6ed9eba1, 9)
  154. STEP(H, c, d, a, b, GET(4) + 0x6ed9eba1, 11)
  155. STEP(H, b, c, d, a, GET(12) + 0x6ed9eba1, 15)
  156. STEP(H, a, b, c, d, GET(2) + 0x6ed9eba1, 3)
  157. STEP(H, d, a, b, c, GET(10) + 0x6ed9eba1, 9)
  158. STEP(H, c, d, a, b, GET(6) + 0x6ed9eba1, 11)
  159. STEP(H, b, c, d, a, GET(14) + 0x6ed9eba1, 15)
  160. STEP(H, a, b, c, d, GET(1) + 0x6ed9eba1, 3)
  161. STEP(H, d, a, b, c, GET(9) + 0x6ed9eba1, 9)
  162. STEP(H, c, d, a, b, GET(5) + 0x6ed9eba1, 11)
  163. STEP(H, b, c, d, a, GET(13) + 0x6ed9eba1, 15)
  164. STEP(H, a, b, c, d, GET(3) + 0x6ed9eba1, 3)
  165. STEP(H, d, a, b, c, GET(11) + 0x6ed9eba1, 9)
  166. STEP(H, c, d, a, b, GET(7) + 0x6ed9eba1, 11)
  167. STEP(H, b, c, d, a, GET(15) + 0x6ed9eba1, 15)
  168. a += saved_a;
  169. b += saved_b;
  170. c += saved_c;
  171. d += saved_d;
  172. ptr += 64;
  173. } while(size -= 64);
  174. ctx->a = a;
  175. ctx->b = b;
  176. ctx->c = c;
  177. ctx->d = d;
  178. return ptr;
  179. }
  180. static void MD4_Init(MD4_CTX *ctx)
  181. {
  182. ctx->a = 0x67452301;
  183. ctx->b = 0xefcdab89;
  184. ctx->c = 0x98badcfe;
  185. ctx->d = 0x10325476;
  186. ctx->lo = 0;
  187. ctx->hi = 0;
  188. }
  189. static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
  190. {
  191. MD4_u32plus saved_lo;
  192. unsigned long used;
  193. saved_lo = ctx->lo;
  194. ctx->lo = (saved_lo + size) & 0x1fffffff;
  195. if(ctx->lo < saved_lo)
  196. ctx->hi++;
  197. ctx->hi += (MD4_u32plus)size >> 29;
  198. used = saved_lo & 0x3f;
  199. if(used) {
  200. unsigned long available = 64 - used;
  201. if(size < available) {
  202. memcpy(&ctx->buffer[used], data, size);
  203. return;
  204. }
  205. memcpy(&ctx->buffer[used], data, available);
  206. data = (const unsigned char *)data + available;
  207. size -= available;
  208. body(ctx, ctx->buffer, 64);
  209. }
  210. if(size >= 64) {
  211. data = body(ctx, data, size & ~(unsigned long)0x3f);
  212. size &= 0x3f;
  213. }
  214. memcpy(ctx->buffer, data, size);
  215. }
  216. static void MD4_Final(unsigned char *result, MD4_CTX *ctx)
  217. {
  218. unsigned long used, available;
  219. used = ctx->lo & 0x3f;
  220. ctx->buffer[used++] = 0x80;
  221. available = 64 - used;
  222. if(available < 8) {
  223. memset(&ctx->buffer[used], 0, available);
  224. body(ctx, ctx->buffer, 64);
  225. used = 0;
  226. available = 64;
  227. }
  228. memset(&ctx->buffer[used], 0, available - 8);
  229. ctx->lo <<= 3;
  230. ctx->buffer[56] = curlx_ultouc((ctx->lo)&0xff);
  231. ctx->buffer[57] = curlx_ultouc((ctx->lo >> 8)&0xff);
  232. ctx->buffer[58] = curlx_ultouc((ctx->lo >> 16)&0xff);
  233. ctx->buffer[59] = curlx_ultouc((ctx->lo >> 24)&0xff);
  234. ctx->buffer[60] = curlx_ultouc((ctx->hi)&0xff);
  235. ctx->buffer[61] = curlx_ultouc((ctx->hi >> 8)&0xff);
  236. ctx->buffer[62] = curlx_ultouc((ctx->hi >> 16)&0xff);
  237. ctx->buffer[63] = curlx_ultouc(ctx->hi >> 24);
  238. body(ctx, ctx->buffer, 64);
  239. result[0] = curlx_ultouc((ctx->a)&0xff);
  240. result[1] = curlx_ultouc((ctx->a >> 8)&0xff);
  241. result[2] = curlx_ultouc((ctx->a >> 16)&0xff);
  242. result[3] = curlx_ultouc(ctx->a >> 24);
  243. result[4] = curlx_ultouc((ctx->b)&0xff);
  244. result[5] = curlx_ultouc((ctx->b >> 8)&0xff);
  245. result[6] = curlx_ultouc((ctx->b >> 16)&0xff);
  246. result[7] = curlx_ultouc(ctx->b >> 24);
  247. result[8] = curlx_ultouc((ctx->c)&0xff);
  248. result[9] = curlx_ultouc((ctx->c >> 8)&0xff);
  249. result[10] = curlx_ultouc((ctx->c >> 16)&0xff);
  250. result[11] = curlx_ultouc(ctx->c >> 24);
  251. result[12] = curlx_ultouc((ctx->d)&0xff);
  252. result[13] = curlx_ultouc((ctx->d >> 8)&0xff);
  253. result[14] = curlx_ultouc((ctx->d >> 16)&0xff);
  254. result[15] = curlx_ultouc(ctx->d >> 24);
  255. memset(ctx, 0, sizeof(*ctx));
  256. }
  257. #endif
  258. void Curl_md4it(unsigned char *output, const unsigned char *input, size_t len)
  259. {
  260. MD4_CTX ctx;
  261. MD4_Init(&ctx);
  262. MD4_Update(&ctx, input, curlx_uztoui(len));
  263. MD4_Final(output, &ctx);
  264. }
  265. #endif /* defined(USE_NSS) || defined(USE_OS400CRYPTO) ||
  266. (defined(USE_OPENSSL) && defined(OPENSSL_NO_MD4)) ||
  267. (defined(USE_MBEDTLS) && !defined(MBEDTLS_MD4_C)) */