hl_md5.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * hashlib++ - a simple hash library for C++
  3. *
  4. * Copyright (c) 2007-2010 Benjamin Grüdelbach
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1) Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2) Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  21. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  24. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. //----------------------------------------------------------------------
  29. /*
  30. * The hashlib++ MD5 implementation is derivative from the sourcecode
  31. * published in RFC 1321
  32. *
  33. * Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  34. * rights reserved.
  35. *
  36. * License to copy and use this software is granted provided that it
  37. * is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  38. * Algorithm" in all material mentioning or referencing this software
  39. * or this function.
  40. *
  41. * License is also granted to make and use derivative works provided
  42. * that such works are identified as "derived from the RSA Data
  43. * Security, Inc. MD5 Message-Digest Algorithm" in all material
  44. * mentioning or referencing the derived work.
  45. *
  46. * RSA Data Security, Inc. makes no representations concerning either
  47. * the merchantability of this software or the suitability of this
  48. * software for any particular purpose. It is provided "as is"
  49. * without express or implied warranty of any kind.
  50. *
  51. * These notices must be retained in any copies of any part of this
  52. * documentation and/or software.
  53. */
  54. //----------------------------------------------------------------------
  55. /**
  56. * @file hl_md5.cpp
  57. * @brief This file contains the implementation of the MD5 class
  58. * @date Mo 17 Sep 2007
  59. */
  60. //----------------------------------------------------------------------
  61. //hashlib++ includes
  62. #include "hl_md5.h"
  63. //----------------------------------------------------------------------
  64. // defines
  65. // Constants for MD5Transform routine.
  66. #define S11 7
  67. #define S12 12
  68. #define S13 17
  69. #define S14 22
  70. #define S21 5
  71. #define S22 9
  72. #define S23 14
  73. #define S24 20
  74. #define S31 4
  75. #define S32 11
  76. #define S33 16
  77. #define S34 23
  78. #define S41 6
  79. #define S42 10
  80. #define S43 15
  81. #define S44 21
  82. static unsigned char PADDING[64] = {
  83. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  84. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  85. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  86. };
  87. /* F, G, H and I are basic MD5 functions. */
  88. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  89. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  90. #define H(x, y, z) ((x) ^ (y) ^ (z))
  91. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  92. /*
  93. * ROTATE_LEFT rotates x left n bits.
  94. * cast to unsigned int to guarantee support for 64Bit System
  95. */
  96. #define ROTATE_LEFT(x, n) (((x) << (n)) | (( (unsigned int) x) >> (32-(n))))
  97. /*
  98. FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  99. Rotation is separate from addition to prevent recomputation.
  100. */
  101. #define FF(a, b, c, d, x, s, ac) { \
  102. (a) += F ((b), (c), (d)) + (x) + (unsigned long int)(ac); \
  103. (a) = ROTATE_LEFT ((a), (s)); \
  104. (a) += (b); \
  105. }
  106. #define GG(a, b, c, d, x, s, ac) { \
  107. (a) += G ((b), (c), (d)) + (x) + (unsigned long int)(ac); \
  108. (a) = ROTATE_LEFT ((a), (s)); \
  109. (a) += (b); \
  110. }
  111. #define HH(a, b, c, d, x, s, ac) { \
  112. (a) += H ((b), (c), (d)) + (x) + (unsigned long int)(ac); \
  113. (a) = ROTATE_LEFT ((a), (s)); \
  114. (a) += (b); \
  115. }
  116. #define II(a, b, c, d, x, s, ac) { \
  117. (a) += I ((b), (c), (d)) + (x) + (unsigned long int)(ac); \
  118. (a) = ROTATE_LEFT ((a), (s)); \
  119. (a) += (b); \
  120. }
  121. //----------------------------------------------------------------------
  122. //private member-functions
  123. /**
  124. * @brief Basic transformation. Transforms state based on block.
  125. * @param state state to transform
  126. * @param block block to transform
  127. */
  128. void MD5::MD5Transform (unsigned long int state[4], unsigned char block[64])
  129. {
  130. unsigned long int a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  131. Decode (x, block, 64);
  132. /* Round 1 */
  133. FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
  134. FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
  135. FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
  136. FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
  137. FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
  138. FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
  139. FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
  140. FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
  141. FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
  142. FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
  143. FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  144. FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  145. FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  146. FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  147. FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  148. FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  149. /* Round 2 */
  150. GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
  151. GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
  152. GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  153. GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
  154. GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
  155. GG (d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  156. GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  157. GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
  158. GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
  159. GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  160. GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
  161. GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
  162. GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  163. GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
  164. GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
  165. GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  166. /* Round 3 */
  167. HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
  168. HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
  169. HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  170. HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  171. HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
  172. HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
  173. HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
  174. HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  175. HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  176. HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
  177. HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
  178. HH (b, c, d, a, x[ 6], S34, 0x4881d05); /* 44 */
  179. HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
  180. HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  181. HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  182. HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
  183. /* Round 4 */
  184. II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
  185. II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
  186. II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  187. II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
  188. II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  189. II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
  190. II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  191. II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
  192. II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
  193. II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  194. II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
  195. II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  196. II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
  197. II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  198. II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
  199. II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
  200. state[0] += a;
  201. state[1] += b;
  202. state[2] += c;
  203. state[3] += d;
  204. /*
  205. * Zeroize sensitive information.
  206. */
  207. MD5_memset ((POINTER)x, 0, sizeof (x));
  208. }
  209. /**
  210. * @brief Encodes input data
  211. * @param output Encoded data as OUT parameter
  212. * @param input Input data
  213. * @param len The length of the input assuming it is a
  214. * multiple of 4
  215. */
  216. void MD5::Encode (unsigned char *output, unsigned long int *input, unsigned int len)
  217. {
  218. unsigned int i, j;
  219. for (i = 0, j = 0; j < len; i++, j += 4) {
  220. output[j] = (unsigned char)(input[i] & 0xff);
  221. output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
  222. output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
  223. output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
  224. }
  225. }
  226. /**
  227. * @brief Decodes input data into output
  228. * @param output Decoded data as OUT parameter
  229. * @param input Input data
  230. * @param len The length of the input assuming it is a
  231. * multiple of 4
  232. */
  233. void MD5::Decode (unsigned long int *output, unsigned char *input, unsigned int len)
  234. {
  235. unsigned int i, j;
  236. for (i = 0, j = 0; j < len; i++, j += 4)
  237. output[i] = ((unsigned long int)input[j]) |
  238. (((unsigned long int)input[j+1]) << 8) |
  239. (((unsigned long int)input[j+2]) << 16) |
  240. (((unsigned long int)input[j+3]) << 24);
  241. }
  242. /**
  243. * @brief internal memory management
  244. * @param output OUT parameter where POINTER is a unsigned
  245. * char*
  246. * @param input Data to copy where POINTER is a unsigned char*
  247. * @param len The length of the data
  248. */
  249. void MD5::MD5_memcpy (POINTER output, POINTER input, unsigned int len)
  250. {
  251. /*
  252. * TODO-Note: Replace "for loop" with standard memcpy if possible.
  253. */
  254. unsigned int i;
  255. for (i = 0; i < len; i++)
  256. output[i] = input[i];
  257. }
  258. /**
  259. * @brief internal memory management
  260. * @param output OUT parameter where POINTER is an unsigned
  261. * char*
  262. * @param value Value to fill the memory with
  263. * @param len The length of the data
  264. *
  265. */
  266. void MD5::MD5_memset (POINTER output,int value,unsigned int len)
  267. {
  268. /*
  269. * TODO-Note: Replace "for loop" with standard memset if possible.
  270. */
  271. unsigned int i;
  272. for (i = 0; i < len; i++)
  273. ((char *)output)[i] = (char)value;
  274. }
  275. //----------------------------------------------------------------------
  276. //public member-functions
  277. /**
  278. * @brief Initialization begins an operation,
  279. * writing a new context
  280. * @param context The HL_MD5_CTX context to initialize
  281. */
  282. void MD5::MD5Init (HL_MD5_CTX *context)
  283. {
  284. context->count[0] = context->count[1] = 0;
  285. context->state[0] = 0x67452301;
  286. context->state[1] = 0xefcdab89;
  287. context->state[2] = 0x98badcfe;
  288. context->state[3] = 0x10325476;
  289. }
  290. /**
  291. * @brief Block update operation. Continues an md5
  292. * message-digest operation, processing another
  293. * message block, and updating the context.
  294. * @param context The HL_MD5_CTX context to update
  295. * @param input The data to write into the context
  296. * @param inputLen The length of the input data
  297. */
  298. void MD5::MD5Update (HL_MD5_CTX *context, unsigned char *input, unsigned int inputLen)
  299. {
  300. unsigned int i, index, partLen;
  301. /* Compute number of bytes mod 64 */
  302. index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  303. /* Update number of bits */
  304. if ( (context->count[0] += ((unsigned long int)inputLen << 3))
  305. < ((unsigned long int)inputLen << 3))
  306. context->count[1]++;
  307. context->count[1] += ((unsigned long int)inputLen >> 29);
  308. partLen = 64 - index;
  309. /*
  310. * Transform as many times as possible.
  311. */
  312. if (inputLen >= partLen)
  313. {
  314. MD5_memcpy ((POINTER)&context->buffer[index], (POINTER)input, partLen);
  315. MD5Transform (context->state, context->buffer);
  316. for (i = partLen; i + 63 < inputLen; i += 64)
  317. MD5Transform (context->state, &input[i]);
  318. index = 0;
  319. }
  320. else
  321. i = 0;
  322. /* Buffer remaining input */
  323. MD5_memcpy ((POINTER)&context->buffer[index],
  324. (POINTER)&input[i],
  325. inputLen-i);
  326. }
  327. /**
  328. * @brief Finalization ends the md5 message-digest
  329. * operation, writing the the message digest and
  330. * zeroizing the context.
  331. * @param digest This is an OUT parameter which contains
  332. * the created hash after the method returns
  333. * @param context The context to finalize
  334. */
  335. void MD5::MD5Final (unsigned char digest[16], HL_MD5_CTX *context)
  336. {
  337. unsigned char bits[8];
  338. unsigned int index, padLen;
  339. /* Save number of bits */
  340. Encode (bits, context->count, 8);
  341. /*
  342. * Pad out to 56 mod 64.
  343. */
  344. index = (unsigned int)((context->count[0] >> 3) & 0x3f);
  345. padLen = (index < 56) ? (56 - index) : (120 - index);
  346. MD5Update (context, PADDING, padLen);
  347. /* Append length (before padding) */
  348. MD5Update (context, bits, 8);
  349. /* Store state in digest */
  350. Encode (digest, context->state, 16);
  351. /*
  352. * Zeroize sensitive information.
  353. */
  354. MD5_memset ((POINTER)context, 0, sizeof (*context));
  355. }
  356. //----------------------------------------------------------------------
  357. //EOF