md4.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. /* NSS and OS/400 crypto library do not provide the MD4 hash algorithm, so
  39. * that we have a local implementation of it */
  40. #if defined(USE_NSS) || defined(USE_OS400CRYPTO)
  41. #include "curl_md4.h"
  42. #include "warnless.h"
  43. #ifndef HAVE_OPENSSL
  44. #include <string.h>
  45. /* Any 32-bit or wider unsigned integer data type will do */
  46. typedef unsigned int MD4_u32plus;
  47. typedef struct {
  48. MD4_u32plus lo, hi;
  49. MD4_u32plus a, b, c, d;
  50. unsigned char buffer[64];
  51. MD4_u32plus block[16];
  52. } MD4_CTX;
  53. static void MD4_Init(MD4_CTX *ctx);
  54. static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size);
  55. static void MD4_Final(unsigned char *result, MD4_CTX *ctx);
  56. /*
  57. * The basic MD4 functions.
  58. *
  59. * F and G are optimized compared to their RFC 1320 definitions, with the
  60. * optimization for F borrowed from Colin Plumb's MD5 implementation.
  61. */
  62. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  63. #define G(x, y, z) (((x) & ((y) | (z))) | ((y) & (z)))
  64. #define H(x, y, z) ((x) ^ (y) ^ (z))
  65. /*
  66. * The MD4 transformation for all three rounds.
  67. */
  68. #define STEP(f, a, b, c, d, x, s) \
  69. (a) += f((b), (c), (d)) + (x); \
  70. (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s))));
  71. /*
  72. * SET reads 4 input bytes in little-endian byte order and stores them
  73. * in a properly aligned word in host byte order.
  74. *
  75. * The check for little-endian architectures that tolerate unaligned
  76. * memory accesses is just an optimization. Nothing will break if it
  77. * doesn't work.
  78. */
  79. #if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
  80. #define SET(n) \
  81. (*(MD4_u32plus *)&ptr[(n) * 4])
  82. #define GET(n) \
  83. SET(n)
  84. #else
  85. #define SET(n) \
  86. (ctx->block[(n)] = \
  87. (MD4_u32plus)ptr[(n) * 4] | \
  88. ((MD4_u32plus)ptr[(n) * 4 + 1] << 8) | \
  89. ((MD4_u32plus)ptr[(n) * 4 + 2] << 16) | \
  90. ((MD4_u32plus)ptr[(n) * 4 + 3] << 24))
  91. #define GET(n) \
  92. (ctx->block[(n)])
  93. #endif
  94. /*
  95. * This processes one or more 64-byte data blocks, but does NOT update
  96. * the bit counters. There are no alignment requirements.
  97. */
  98. static const void *body(MD4_CTX *ctx, const void *data, unsigned long size)
  99. {
  100. const unsigned char *ptr;
  101. MD4_u32plus a, b, c, d;
  102. MD4_u32plus saved_a, saved_b, saved_c, saved_d;
  103. ptr = (const unsigned char *)data;
  104. a = ctx->a;
  105. b = ctx->b;
  106. c = ctx->c;
  107. d = ctx->d;
  108. do {
  109. saved_a = a;
  110. saved_b = b;
  111. saved_c = c;
  112. saved_d = d;
  113. /* Round 1 */
  114. STEP(F, a, b, c, d, SET(0), 3)
  115. STEP(F, d, a, b, c, SET(1), 7)
  116. STEP(F, c, d, a, b, SET(2), 11)
  117. STEP(F, b, c, d, a, SET(3), 19)
  118. STEP(F, a, b, c, d, SET(4), 3)
  119. STEP(F, d, a, b, c, SET(5), 7)
  120. STEP(F, c, d, a, b, SET(6), 11)
  121. STEP(F, b, c, d, a, SET(7), 19)
  122. STEP(F, a, b, c, d, SET(8), 3)
  123. STEP(F, d, a, b, c, SET(9), 7)
  124. STEP(F, c, d, a, b, SET(10), 11)
  125. STEP(F, b, c, d, a, SET(11), 19)
  126. STEP(F, a, b, c, d, SET(12), 3)
  127. STEP(F, d, a, b, c, SET(13), 7)
  128. STEP(F, c, d, a, b, SET(14), 11)
  129. STEP(F, b, c, d, a, SET(15), 19)
  130. /* Round 2 */
  131. STEP(G, a, b, c, d, GET(0) + 0x5a827999, 3)
  132. STEP(G, d, a, b, c, GET(4) + 0x5a827999, 5)
  133. STEP(G, c, d, a, b, GET(8) + 0x5a827999, 9)
  134. STEP(G, b, c, d, a, GET(12) + 0x5a827999, 13)
  135. STEP(G, a, b, c, d, GET(1) + 0x5a827999, 3)
  136. STEP(G, d, a, b, c, GET(5) + 0x5a827999, 5)
  137. STEP(G, c, d, a, b, GET(9) + 0x5a827999, 9)
  138. STEP(G, b, c, d, a, GET(13) + 0x5a827999, 13)
  139. STEP(G, a, b, c, d, GET(2) + 0x5a827999, 3)
  140. STEP(G, d, a, b, c, GET(6) + 0x5a827999, 5)
  141. STEP(G, c, d, a, b, GET(10) + 0x5a827999, 9)
  142. STEP(G, b, c, d, a, GET(14) + 0x5a827999, 13)
  143. STEP(G, a, b, c, d, GET(3) + 0x5a827999, 3)
  144. STEP(G, d, a, b, c, GET(7) + 0x5a827999, 5)
  145. STEP(G, c, d, a, b, GET(11) + 0x5a827999, 9)
  146. STEP(G, b, c, d, a, GET(15) + 0x5a827999, 13)
  147. /* Round 3 */
  148. STEP(H, a, b, c, d, GET(0) + 0x6ed9eba1, 3)
  149. STEP(H, d, a, b, c, GET(8) + 0x6ed9eba1, 9)
  150. STEP(H, c, d, a, b, GET(4) + 0x6ed9eba1, 11)
  151. STEP(H, b, c, d, a, GET(12) + 0x6ed9eba1, 15)
  152. STEP(H, a, b, c, d, GET(2) + 0x6ed9eba1, 3)
  153. STEP(H, d, a, b, c, GET(10) + 0x6ed9eba1, 9)
  154. STEP(H, c, d, a, b, GET(6) + 0x6ed9eba1, 11)
  155. STEP(H, b, c, d, a, GET(14) + 0x6ed9eba1, 15)
  156. STEP(H, a, b, c, d, GET(1) + 0x6ed9eba1, 3)
  157. STEP(H, d, a, b, c, GET(9) + 0x6ed9eba1, 9)
  158. STEP(H, c, d, a, b, GET(5) + 0x6ed9eba1, 11)
  159. STEP(H, b, c, d, a, GET(13) + 0x6ed9eba1, 15)
  160. STEP(H, a, b, c, d, GET(3) + 0x6ed9eba1, 3)
  161. STEP(H, d, a, b, c, GET(11) + 0x6ed9eba1, 9)
  162. STEP(H, c, d, a, b, GET(7) + 0x6ed9eba1, 11)
  163. STEP(H, b, c, d, a, GET(15) + 0x6ed9eba1, 15)
  164. a += saved_a;
  165. b += saved_b;
  166. c += saved_c;
  167. d += saved_d;
  168. ptr += 64;
  169. } while(size -= 64);
  170. ctx->a = a;
  171. ctx->b = b;
  172. ctx->c = c;
  173. ctx->d = d;
  174. return ptr;
  175. }
  176. static void MD4_Init(MD4_CTX *ctx)
  177. {
  178. ctx->a = 0x67452301;
  179. ctx->b = 0xefcdab89;
  180. ctx->c = 0x98badcfe;
  181. ctx->d = 0x10325476;
  182. ctx->lo = 0;
  183. ctx->hi = 0;
  184. }
  185. static void MD4_Update(MD4_CTX *ctx, const void *data, unsigned long size)
  186. {
  187. MD4_u32plus saved_lo;
  188. unsigned long used, available;
  189. saved_lo = ctx->lo;
  190. ctx->lo = (saved_lo + size) & 0x1fffffff;
  191. if(ctx->lo < saved_lo)
  192. ctx->hi++;
  193. ctx->hi += (MD4_u32plus)size >> 29;
  194. used = saved_lo & 0x3f;
  195. if(used) {
  196. available = 64 - used;
  197. if(size < available) {
  198. memcpy(&ctx->buffer[used], data, size);
  199. return;
  200. }
  201. memcpy(&ctx->buffer[used], data, available);
  202. data = (const unsigned char *)data + available;
  203. size -= available;
  204. body(ctx, ctx->buffer, 64);
  205. }
  206. if(size >= 64) {
  207. data = body(ctx, data, size & ~(unsigned long)0x3f);
  208. size &= 0x3f;
  209. }
  210. memcpy(ctx->buffer, data, size);
  211. }
  212. static void MD4_Final(unsigned char *result, MD4_CTX *ctx)
  213. {
  214. unsigned long used, available;
  215. used = ctx->lo & 0x3f;
  216. ctx->buffer[used++] = 0x80;
  217. available = 64 - used;
  218. if(available < 8) {
  219. memset(&ctx->buffer[used], 0, available);
  220. body(ctx, ctx->buffer, 64);
  221. used = 0;
  222. available = 64;
  223. }
  224. memset(&ctx->buffer[used], 0, available - 8);
  225. ctx->lo <<= 3;
  226. ctx->buffer[56] = curlx_ultouc((ctx->lo)&0xff);
  227. ctx->buffer[57] = curlx_ultouc((ctx->lo >> 8)&0xff);
  228. ctx->buffer[58] = curlx_ultouc((ctx->lo >> 16)&0xff);
  229. ctx->buffer[59] = curlx_ultouc((ctx->lo >> 24)&0xff);
  230. ctx->buffer[60] = curlx_ultouc((ctx->hi)&0xff);
  231. ctx->buffer[61] = curlx_ultouc((ctx->hi >> 8)&0xff);
  232. ctx->buffer[62] = curlx_ultouc((ctx->hi >> 16)&0xff);
  233. ctx->buffer[63] = curlx_ultouc(ctx->hi >> 24);
  234. body(ctx, ctx->buffer, 64);
  235. result[0] = curlx_ultouc((ctx->a)&0xff);
  236. result[1] = curlx_ultouc((ctx->a >> 8)&0xff);
  237. result[2] = curlx_ultouc((ctx->a >> 16)&0xff);
  238. result[3] = curlx_ultouc(ctx->a >> 24);
  239. result[4] = curlx_ultouc((ctx->b)&0xff);
  240. result[5] = curlx_ultouc((ctx->b >> 8)&0xff);
  241. result[6] = curlx_ultouc((ctx->b >> 16)&0xff);
  242. result[7] = curlx_ultouc(ctx->b >> 24);
  243. result[8] = curlx_ultouc((ctx->c)&0xff);
  244. result[9] = curlx_ultouc((ctx->c >> 8)&0xff);
  245. result[10] = curlx_ultouc((ctx->c >> 16)&0xff);
  246. result[11] = curlx_ultouc(ctx->c >> 24);
  247. result[12] = curlx_ultouc((ctx->d)&0xff);
  248. result[13] = curlx_ultouc((ctx->d >> 8)&0xff);
  249. result[14] = curlx_ultouc((ctx->d >> 16)&0xff);
  250. result[15] = curlx_ultouc(ctx->d >> 24);
  251. memset(ctx, 0, sizeof(*ctx));
  252. }
  253. #endif
  254. void Curl_md4it(unsigned char *output, const unsigned char *input, size_t len)
  255. {
  256. MD4_CTX ctx;
  257. MD4_Init(&ctx);
  258. MD4_Update(&ctx, input, curlx_uztoui(len));
  259. MD4_Final(output, &ctx);
  260. }
  261. #endif /* defined(USE_NSS) || defined(USE_OS400CRYPTO) */