md5calc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /***********************************************************
  2. * md5 calculation algorithm
  3. *
  4. * The source code referred to the following URL.
  5. * http://www.geocities.co.jp/SiliconValley-Oakland/8878/lab17/lab17.html
  6. *
  7. ***********************************************************/
  8. #include "../common/random.h"
  9. #include "md5calc.h"
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #ifndef UINT_MAX
  14. #define UINT_MAX 4294967295U
  15. #endif
  16. // Global variable
  17. static unsigned int *pX;
  18. // String Table
  19. static const unsigned int T[] = {
  20. 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, //0
  21. 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, //4
  22. 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, //8
  23. 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, //12
  24. 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, //16
  25. 0xd62f105d, 0x2441453, 0xd8a1e681, 0xe7d3fbc8, //20
  26. 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, //24
  27. 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, //28
  28. 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, //32
  29. 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, //36
  30. 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x4881d05, //40
  31. 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, //44
  32. 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, //48
  33. 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, //52
  34. 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, //56
  35. 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 //60
  36. };
  37. // ROTATE_LEFT The left is made to rotate x [ n-bit ]. This is diverted as it is from RFC.
  38. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  39. // The function used for other calculation
  40. static unsigned int F(unsigned int X, unsigned int Y, unsigned int Z)
  41. {
  42. return (X & Y) | (~X & Z);
  43. }
  44. static unsigned int G(unsigned int X, unsigned int Y, unsigned int Z)
  45. {
  46. return (X & Z) | (Y & ~Z);
  47. }
  48. static unsigned int H(unsigned int X, unsigned int Y, unsigned int Z)
  49. {
  50. return X ^ Y ^ Z;
  51. }
  52. static unsigned int I(unsigned int X, unsigned int Y, unsigned int Z)
  53. {
  54. return Y ^ (X | ~Z);
  55. }
  56. static unsigned int Round(unsigned int a, unsigned int b, unsigned int FGHI,
  57. unsigned int k, unsigned int s, unsigned int i)
  58. {
  59. return b + ROTATE_LEFT(a + FGHI + pX[k] + T[i], s);
  60. }
  61. static void Round1(unsigned int *a, unsigned int b, unsigned int c,
  62. unsigned int d,unsigned int k, unsigned int s, unsigned int i)
  63. {
  64. *a = Round(*a, b, F(b,c,d), k, s, i);
  65. }
  66. static void Round2(unsigned int *a, unsigned int b, unsigned int c,
  67. unsigned int d,unsigned int k, unsigned int s, unsigned int i)
  68. {
  69. *a = Round(*a, b, G(b,c,d), k, s, i);
  70. }
  71. static void Round3(unsigned int *a, unsigned int b, unsigned int c,
  72. unsigned int d,unsigned int k, unsigned int s, unsigned int i)
  73. {
  74. *a = Round(*a, b, H(b,c,d), k, s, i);
  75. }
  76. static void Round4(unsigned int *a, unsigned int b, unsigned int c,
  77. unsigned int d,unsigned int k, unsigned int s, unsigned int i)
  78. {
  79. *a = Round(*a, b, I(b,c,d), k, s, i);
  80. }
  81. static void MD5_Round_Calculate(const unsigned char *block,
  82. unsigned int *A2, unsigned int *B2, unsigned int *C2, unsigned int *D2)
  83. {
  84. //create X It is since it is required.
  85. unsigned int X[16]; //512bit 64byte
  86. int j,k;
  87. //Save A as AA, B as BB, C as CC, and and D as DD (saving of A, B, C, and D)
  88. unsigned int A=*A2, B=*B2, C=*C2, D=*D2;
  89. unsigned int AA = A,BB = B,CC = C,DD = D;
  90. //It is a large region variable reluctantly because of calculation of a round. . . for Round1...4
  91. pX = X;
  92. //Copy block(padding_message) i into X
  93. for (j=0,k=0; j<64; j+=4,k++)
  94. X[k] = ((unsigned int)block[j]) // 8byte*4 -> 32byte conversion
  95. | (((unsigned int)block[j+1]) << 8) // A function called Decode as used in the field of RFC
  96. | (((unsigned int)block[j+2]) << 16)
  97. | (((unsigned int)block[j+3]) << 24);
  98. //Round 1
  99. Round1(&A,B,C,D, 0, 7, 0);
  100. Round1(&D,A,B,C, 1, 12, 1);
  101. Round1(&C,D,A,B, 2, 17, 2);
  102. Round1(&B,C,D,A, 3, 22, 3);
  103. Round1(&A,B,C,D, 4, 7, 4);
  104. Round1(&D,A,B,C, 5, 12, 5);
  105. Round1(&C,D,A,B, 6, 17, 6);
  106. Round1(&B,C,D,A, 7, 22, 7);
  107. Round1(&A,B,C,D, 8, 7, 8);
  108. Round1(&D,A,B,C, 9, 12, 9);
  109. Round1(&C,D,A,B, 10, 17, 10);
  110. Round1(&B,C,D,A, 11, 22, 11);
  111. Round1(&A,B,C,D, 12, 7, 12);
  112. Round1(&D,A,B,C, 13, 12, 13);
  113. Round1(&C,D,A,B, 14, 17, 14);
  114. Round1(&B,C,D,A, 15, 22, 15);
  115. //Round 2
  116. Round2(&A,B,C,D, 1, 5, 16);
  117. Round2(&D,A,B,C, 6, 9, 17);
  118. Round2(&C,D,A,B, 11, 14, 18);
  119. Round2(&B,C,D,A, 0, 20, 19);
  120. Round2(&A,B,C,D, 5, 5, 20);
  121. Round2(&D,A,B,C, 10, 9, 21);
  122. Round2(&C,D,A,B, 15, 14, 22);
  123. Round2(&B,C,D,A, 4, 20, 23);
  124. Round2(&A,B,C,D, 9, 5, 24);
  125. Round2(&D,A,B,C, 14, 9, 25);
  126. Round2(&C,D,A,B, 3, 14, 26);
  127. Round2(&B,C,D,A, 8, 20, 27);
  128. Round2(&A,B,C,D, 13, 5, 28);
  129. Round2(&D,A,B,C, 2, 9, 29);
  130. Round2(&C,D,A,B, 7, 14, 30);
  131. Round2(&B,C,D,A, 12, 20, 31);
  132. //Round 3
  133. Round3(&A,B,C,D, 5, 4, 32);
  134. Round3(&D,A,B,C, 8, 11, 33);
  135. Round3(&C,D,A,B, 11, 16, 34);
  136. Round3(&B,C,D,A, 14, 23, 35);
  137. Round3(&A,B,C,D, 1, 4, 36);
  138. Round3(&D,A,B,C, 4, 11, 37);
  139. Round3(&C,D,A,B, 7, 16, 38);
  140. Round3(&B,C,D,A, 10, 23, 39);
  141. Round3(&A,B,C,D, 13, 4, 40);
  142. Round3(&D,A,B,C, 0, 11, 41);
  143. Round3(&C,D,A,B, 3, 16, 42);
  144. Round3(&B,C,D,A, 6, 23, 43);
  145. Round3(&A,B,C,D, 9, 4, 44);
  146. Round3(&D,A,B,C, 12, 11, 45);
  147. Round3(&C,D,A,B, 15, 16, 46);
  148. Round3(&B,C,D,A, 2, 23, 47);
  149. //Round 4
  150. Round4(&A,B,C,D, 0, 6, 48);
  151. Round4(&D,A,B,C, 7, 10, 49);
  152. Round4(&C,D,A,B, 14, 15, 50);
  153. Round4(&B,C,D,A, 5, 21, 51);
  154. Round4(&A,B,C,D, 12, 6, 52);
  155. Round4(&D,A,B,C, 3, 10, 53);
  156. Round4(&C,D,A,B, 10, 15, 54);
  157. Round4(&B,C,D,A, 1, 21, 55);
  158. Round4(&A,B,C,D, 8, 6, 56);
  159. Round4(&D,A,B,C, 15, 10, 57);
  160. Round4(&C,D,A,B, 6, 15, 58);
  161. Round4(&B,C,D,A, 13, 21, 59);
  162. Round4(&A,B,C,D, 4, 6, 60);
  163. Round4(&D,A,B,C, 11, 10, 61);
  164. Round4(&C,D,A,B, 2, 15, 62);
  165. Round4(&B,C,D,A, 9, 21, 63);
  166. // Then perform the following additions. (let's add)
  167. *A2 = A + AA;
  168. *B2 = B + BB;
  169. *C2 = C + CC;
  170. *D2 = D + DD;
  171. //The clearance of confidential information
  172. memset(pX, 0, sizeof(X));
  173. }
  174. static void MD5_String2binary(const char *string, unsigned char *output)
  175. {
  176. //var
  177. /*8bit*/
  178. unsigned char padding_message[64]; //Extended message 512bit 64byte
  179. unsigned char *pstring; //The position of string in the present scanning notes is held.
  180. /*32bit*/
  181. unsigned int string_byte_len, //The byte chief of string is held.
  182. string_bit_len, //The bit length of string is held.
  183. copy_len, //The number of bytes which is used by 1-3 and which remained
  184. msg_digest[4]; //Message digest 128bit 4byte
  185. unsigned int *A = &msg_digest[0], //The message digest in accordance with RFC (reference)
  186. *B = &msg_digest[1],
  187. *C = &msg_digest[2],
  188. *D = &msg_digest[3];
  189. int i;
  190. //prog
  191. //Step 3.Initialize MD Buffer (although it is the initialization; step 3 of A, B, C, and D -- unavoidable -- a head)
  192. *A = 0x67452301;
  193. *B = 0xefcdab89;
  194. *C = 0x98badcfe;
  195. *D = 0x10325476;
  196. //Step 1.Append Padding Bits (extension of a mark bit)
  197. //1-1
  198. string_byte_len = (unsigned int)strlen(string); //The byte chief of a character sequence is acquired.
  199. pstring = (unsigned char *)string; //The position of the present character sequence is set.
  200. //1-2 Repeat calculation until length becomes less than 64 bytes.
  201. for (i=string_byte_len; 64<=i; i-=64,pstring+=64)
  202. MD5_Round_Calculate(pstring, A,B,C,D);
  203. //1-3
  204. copy_len = string_byte_len % 64; //The number of bytes which remained is computed.
  205. strncpy((char *)padding_message, (char *)pstring, copy_len); //A message is copied to an extended bit sequence.
  206. memset(padding_message+copy_len, 0, 64 - copy_len); //It buries by 0 until it becomes extended bit length.
  207. padding_message[copy_len] |= 0x80; //The next of a message is 1.
  208. //1-4
  209. //If 56 bytes or more (less than 64 bytes) of remainder becomes, it will calculate by extending to 64 bytes.
  210. if (56 <= copy_len) {
  211. MD5_Round_Calculate(padding_message, A,B,C,D);
  212. memset(padding_message, 0, 56); //56 bytes is newly fill uped with 0.
  213. }
  214. //Step 2.Append Length (the information on length is added)
  215. string_bit_len = string_byte_len * 8; //From the byte chief to bit length (32 bytes of low rank)
  216. memcpy(&padding_message[56], &string_bit_len, 4); //32 bytes of low rank is set.
  217. //When bit length cannot be expressed in 32 bytes of low rank, it is a beam raising to a higher rank.
  218. if (UINT_MAX / 8 < string_byte_len) {
  219. unsigned int high = (string_byte_len - UINT_MAX / 8) * 8;
  220. memcpy(&padding_message[60], &high, 4);
  221. } else
  222. memset(&padding_message[60], 0, 4); //In this case, it is good for a higher rank at 0.
  223. //Step 4.Process Message in 16-Word Blocks (calculation of MD5)
  224. MD5_Round_Calculate(padding_message, A,B,C,D);
  225. //Step 5.Output (output)
  226. memcpy(output,msg_digest,16);
  227. }
  228. //-------------------------------------------------------------------
  229. // The function for the exteriors
  230. /** output is the coded binary in the character sequence which wants to code string. */
  231. void MD5_Binary(const char *string, unsigned char *output)
  232. {
  233. MD5_String2binary(string,output);
  234. }
  235. /** output is the coded character sequence in the character sequence which wants to code string. */
  236. void MD5_String(const char *string, char *output)
  237. {
  238. unsigned char digest[16];
  239. MD5_String2binary(string,digest);
  240. sprintf(output, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
  241. digest[ 0], digest[ 1], digest[ 2], digest[ 3],
  242. digest[ 4], digest[ 5], digest[ 6], digest[ 7],
  243. digest[ 8], digest[ 9], digest[10], digest[11],
  244. digest[12], digest[13], digest[14], digest[15]);
  245. }
  246. /** output is a sequence of non-zero characters to be used as password salt. */
  247. void MD5_Salt(unsigned int len, char *output)
  248. {
  249. unsigned int i;
  250. for (i = 0; i < len; ++i)
  251. output[i] = (char)(1 + rnd() % 255);
  252. }