root/src/common/md5calc.c @ 7

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