MD5Engine.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //
  2. // MD5Engine.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/MD5Engine.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Crypt
  8. // Module: MD5Engine
  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. // MD5 (RFC 1321) 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. MD5 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. MD5 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/MD5Engine.h"
  39. #include <cstring>
  40. namespace Poco {
  41. MD5Engine::MD5Engine()
  42. {
  43. _digest.reserve(16);
  44. reset();
  45. }
  46. MD5Engine::~MD5Engine()
  47. {
  48. reset();
  49. }
  50. void MD5Engine::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 MD5Engine::digestLength() const
  75. {
  76. return DIGEST_SIZE;
  77. }
  78. void MD5Engine::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& MD5Engine::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 MD5Transform routine. */
  116. #define S11 7
  117. #define S12 12
  118. #define S13 17
  119. #define S14 22
  120. #define S21 5
  121. #define S22 9
  122. #define S23 14
  123. #define S24 20
  124. #define S31 4
  125. #define S32 11
  126. #define S33 16
  127. #define S34 23
  128. #define S41 6
  129. #define S42 10
  130. #define S43 15
  131. #define S44 21
  132. /* F, G, H and I are basic MD5 functions. */
  133. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  134. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  135. #define H(x, y, z) ((x) ^ (y) ^ (z))
  136. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  137. /* ROTATE_LEFT rotates x left n bits. */
  138. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  139. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  140. Rotation is separate from addition to prevent recomputation. */
  141. #define FF(a, b, c, d, x, s, ac) { \
  142. (a) += F ((b), (c), (d)) + (x) + (UInt32)(ac); \
  143. (a) = ROTATE_LEFT ((a), (s)); \
  144. (a) += (b); \
  145. }
  146. #define GG(a, b, c, d, x, s, ac) { \
  147. (a) += G ((b), (c), (d)) + (x) + (UInt32)(ac); \
  148. (a) = ROTATE_LEFT ((a), (s)); \
  149. (a) += (b); \
  150. }
  151. #define HH(a, b, c, d, x, s, ac) { \
  152. (a) += H ((b), (c), (d)) + (x) + (UInt32)(ac); \
  153. (a) = ROTATE_LEFT ((a), (s)); \
  154. (a) += (b); \
  155. }
  156. #define II(a, b, c, d, x, s, ac) { \
  157. (a) += I ((b), (c), (d)) + (x) + (UInt32)(ac); \
  158. (a) = ROTATE_LEFT ((a), (s)); \
  159. (a) += (b); \
  160. }
  161. void MD5Engine::transform (UInt32 state[4], const unsigned char block[64])
  162. {
  163. UInt32 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  164. decode(x, block, 64);
  165. /* Round 1 */
  166. FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  167. FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  168. FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  169. FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  170. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  171. FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  172. FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  173. FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  174. FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  175. FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  176. FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  177. FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  178. FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  179. FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  180. FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  181. FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  182. /* Round 2 */
  183. GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  184. GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  185. GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  186. GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  187. GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  188. GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  189. GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  190. GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  191. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  192. GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  193. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  194. GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  195. GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  196. GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  197. GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  198. GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  199. /* Round 3 */
  200. HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  201. HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  202. HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  203. HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  204. HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  205. HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  206. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  207. HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  208. HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  209. HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  210. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  211. HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  212. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  213. HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  214. HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  215. HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  216. /* Round 4 */
  217. II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  218. II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  219. II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  220. II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  221. II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  222. II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  223. II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  224. II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  225. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  226. II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  227. II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  228. II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  229. II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  230. II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  231. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  232. II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  233. state[0] += a;
  234. state[1] += b;
  235. state[2] += c;
  236. state[3] += d;
  237. /* Zeroize sensitive information. */
  238. std::memset(x, 0, sizeof(x));
  239. }
  240. void MD5Engine::encode(unsigned char* output, const UInt32* input, std::size_t len)
  241. {
  242. unsigned int i, j;
  243. for (i = 0, j = 0; j < len; i++, j += 4)
  244. {
  245. output[j] = (unsigned char)(input[i] & 0xff);
  246. output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  247. output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  248. output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  249. }
  250. }
  251. void MD5Engine::decode(UInt32* output, const unsigned char* input, std::size_t len)
  252. {
  253. unsigned int i, j;
  254. for (i = 0, j = 0; j < len; i++, j += 4)
  255. output[i] = ((UInt32)input[j]) | (((UInt32)input[j+1]) << 8) |
  256. (((UInt32)input[j+2]) << 16) | (((UInt32)input[j+3]) << 24);
  257. }
  258. } // namespace Poco