md4.c 9.3 KB

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