MD4Engine.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // MD4Engine.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/MD4Engine.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Crypt
  8. // Module: MD4Engine
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. //
  16. // MD4 (RFC 1320) algorithm:
  17. // Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  18. // rights reserved.
  19. //
  20. // License to copy and use this software is granted provided that it
  21. // is identified as the "RSA Data Security, Inc. MD4 Message-Digest
  22. // Algorithm" in all material mentioning or referencing this software
  23. // or this function.
  24. //
  25. // License is also granted to make and use derivative works provided
  26. // that such works are identified as "derived from the RSA Data
  27. // Security, Inc. MD4 Message-Digest Algorithm" in all material
  28. // mentioning or referencing the derived work.
  29. //
  30. // RSA Data Security, Inc. makes no representations concerning either
  31. // the merchantability of this software or the suitability of this
  32. // software for any particular purpose. It is provided "as is"
  33. // without express or implied warranty of any kind.
  34. //
  35. // These notices must be retained in any copies of any part of this
  36. // documentation and/or software.
  37. //
  38. #include "Poco/MD4Engine.h"
  39. #include <cstring>
  40. namespace Poco {
  41. MD4Engine::MD4Engine()
  42. {
  43. _digest.reserve(16);
  44. reset();
  45. }
  46. MD4Engine::~MD4Engine()
  47. {
  48. reset();
  49. }
  50. void MD4Engine::updateImpl(const void* input_, std::size_t inputLen)
  51. {
  52. const unsigned char* input = (const unsigned char*) input_;
  53. unsigned int i, index, partLen;
  54. /* Compute number of bytes mod 64 */
  55. index = (unsigned int)((_context.count[0] >> 3) & 0x3F);
  56. /* Update number of bits */
  57. if ((_context.count[0] += ((UInt32) inputLen << 3)) < ((UInt32) inputLen << 3))
  58. _context.count[1]++;
  59. _context.count[1] += ((UInt32) inputLen >> 29);
  60. partLen = 64 - index;
  61. /* Transform as many times as possible. */
  62. if (inputLen >= partLen)
  63. {
  64. std::memcpy(&_context.buffer[index], input, partLen);
  65. transform(_context.state, _context.buffer);
  66. for (i = partLen; i + 63 < inputLen; i += 64)
  67. transform(_context.state, &input[i]);
  68. index = 0;
  69. }
  70. else i = 0;
  71. /* Buffer remaining input */
  72. std::memcpy(&_context.buffer[index], &input[i], inputLen-i);
  73. }
  74. std::size_t MD4Engine::digestLength() const
  75. {
  76. return DIGEST_SIZE;
  77. }
  78. void MD4Engine::reset()
  79. {
  80. std::memset(&_context, 0, sizeof(_context));
  81. _context.count[0] = _context.count[1] = 0;
  82. _context.state[0] = 0x67452301;
  83. _context.state[1] = 0xefcdab89;
  84. _context.state[2] = 0x98badcfe;
  85. _context.state[3] = 0x10325476;
  86. }
  87. const DigestEngine::Digest& MD4Engine::digest()
  88. {
  89. static const unsigned char PADDING[64] =
  90. {
  91. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  92. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  93. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  94. };
  95. unsigned char bits[8];
  96. unsigned int index, padLen;
  97. /* Save number of bits */
  98. encode(bits, _context.count, 8);
  99. /* Pad out to 56 mod 64. */
  100. index = (unsigned int)((_context.count[0] >> 3) & 0x3f);
  101. padLen = (index < 56) ? (56 - index) : (120 - index);
  102. update(PADDING, padLen);
  103. /* Append length (before padding) */
  104. update(bits, 8);
  105. /* Store state in digest */
  106. unsigned char digest[16];
  107. encode(digest, _context.state, 16);
  108. _digest.clear();
  109. _digest.insert(_digest.begin(), digest, digest + sizeof(digest));
  110. /* Zeroize sensitive information. */
  111. std::memset(&_context, 0, sizeof (_context));
  112. reset();
  113. return _digest;
  114. }
  115. /* Constants for MD4Transform routine. */
  116. #define S11 3
  117. #define S12 7
  118. #define S13 11
  119. #define S14 19
  120. #define S21 3
  121. #define S22 5
  122. #define S23 9
  123. #define S24 13
  124. #define S31 3
  125. #define S32 9
  126. #define S33 11
  127. #define S34 15
  128. /* F, G and H are basic MD4 functions. */
  129. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  130. #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
  131. #define H(x, y, z) ((x) ^ (y) ^ (z))
  132. /* ROTATE_LEFT rotates x left n bits. */
  133. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  134. /* FF, GG and HH are transformations for rounds 1, 2 and 3 */
  135. /* Rotation is separate from addition to prevent recomputation */
  136. #define FF(a, b, c, d, x, s) { \
  137. (a) += F ((b), (c), (d)) + (x); \
  138. (a) = ROTATE_LEFT ((a), (s)); \
  139. }
  140. #define GG(a, b, c, d, x, s) { \
  141. (a) += G ((b), (c), (d)) + (x) + (UInt32)0x5a827999; \
  142. (a) = ROTATE_LEFT ((a), (s)); \
  143. }
  144. #define HH(a, b, c, d, x, s) { \
  145. (a) += H ((b), (c), (d)) + (x) + (UInt32)0x6ed9eba1; \
  146. (a) = ROTATE_LEFT ((a), (s)); \
  147. }
  148. void MD4Engine::transform (UInt32 state[4], const unsigned char block[64])
  149. {
  150. UInt32 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  151. decode(x, block, 64);
  152. /* Round 1 */
  153. FF (a, b, c, d, x[ 0], S11); /* 1 */
  154. FF (d, a, b, c, x[ 1], S12); /* 2 */
  155. FF (c, d, a, b, x[ 2], S13); /* 3 */
  156. FF (b, c, d, a, x[ 3], S14); /* 4 */
  157. FF (a, b, c, d, x[ 4], S11); /* 5 */
  158. FF (d, a, b, c, x[ 5], S12); /* 6 */
  159. FF (c, d, a, b, x[ 6], S13); /* 7 */
  160. FF (b, c, d, a, x[ 7], S14); /* 8 */
  161. FF (a, b, c, d, x[ 8], S11); /* 9 */
  162. FF (d, a, b, c, x[ 9], S12); /* 10 */
  163. FF (c, d, a, b, x[10], S13); /* 11 */
  164. FF (b, c, d, a, x[11], S14); /* 12 */
  165. FF (a, b, c, d, x[12], S11); /* 13 */
  166. FF (d, a, b, c, x[13], S12); /* 14 */
  167. FF (c, d, a, b, x[14], S13); /* 15 */
  168. FF (b, c, d, a, x[15], S14); /* 16 */
  169. /* Round 2 */
  170. GG (a, b, c, d, x[ 0], S21); /* 17 */
  171. GG (d, a, b, c, x[ 4], S22); /* 18 */
  172. GG (c, d, a, b, x[ 8], S23); /* 19 */
  173. GG (b, c, d, a, x[12], S24); /* 20 */
  174. GG (a, b, c, d, x[ 1], S21); /* 21 */
  175. GG (d, a, b, c, x[ 5], S22); /* 22 */
  176. GG (c, d, a, b, x[ 9], S23); /* 23 */
  177. GG (b, c, d, a, x[13], S24); /* 24 */
  178. GG (a, b, c, d, x[ 2], S21); /* 25 */
  179. GG (d, a, b, c, x[ 6], S22); /* 26 */
  180. GG (c, d, a, b, x[10], S23); /* 27 */
  181. GG (b, c, d, a, x[14], S24); /* 28 */
  182. GG (a, b, c, d, x[ 3], S21); /* 29 */
  183. GG (d, a, b, c, x[ 7], S22); /* 30 */
  184. GG (c, d, a, b, x[11], S23); /* 31 */
  185. GG (b, c, d, a, x[15], S24); /* 32 */
  186. /* Round 3 */
  187. HH (a, b, c, d, x[ 0], S31); /* 33 */
  188. HH (d, a, b, c, x[ 8], S32); /* 34 */
  189. HH (c, d, a, b, x[ 4], S33); /* 35 */
  190. HH (b, c, d, a, x[12], S34); /* 36 */
  191. HH (a, b, c, d, x[ 2], S31); /* 37 */
  192. HH (d, a, b, c, x[10], S32); /* 38 */
  193. HH (c, d, a, b, x[ 6], S33); /* 39 */
  194. HH (b, c, d, a, x[14], S34); /* 40 */
  195. HH (a, b, c, d, x[ 1], S31); /* 41 */
  196. HH (d, a, b, c, x[ 9], S32); /* 42 */
  197. HH (c, d, a, b, x[ 5], S33); /* 43 */
  198. HH (b, c, d, a, x[13], S34); /* 44 */
  199. HH (a, b, c, d, x[ 3], S31); /* 45 */
  200. HH (d, a, b, c, x[11], S32); /* 46 */
  201. HH (c, d, a, b, x[ 7], S33); /* 47 */
  202. HH (b, c, d, a, x[15], S34); /* 48 */
  203. state[0] += a;
  204. state[1] += b;
  205. state[2] += c;
  206. state[3] += d;
  207. /* Zeroize sensitive information. */
  208. std::memset(x, 0, sizeof(x));
  209. }
  210. void MD4Engine::encode(unsigned char* output, const UInt32* input, std::size_t len)
  211. {
  212. unsigned int i, j;
  213. for (i = 0, j = 0; j < len; i++, j += 4)
  214. {
  215. output[j] = (unsigned char)(input[i] & 0xff);
  216. output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  217. output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  218. output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  219. }
  220. }
  221. void MD4Engine::decode(UInt32* output, const unsigned char* input, std::size_t len)
  222. {
  223. unsigned int i, j;
  224. for (i = 0, j = 0; j < len; i++, j += 4)
  225. output[i] = ((UInt32)input[j]) | (((UInt32)input[j+1]) << 8) |
  226. (((UInt32)input[j+2]) << 16) | (((UInt32)input[j+3]) << 24);
  227. }
  228. } // namespace Poco