KiCad PCB EDA Suite
md5_hash.cpp
Go to the documentation of this file.
1 // Code by: B-Con (http://b-con.us)
2 // Released under the GNU GPL
3 // MD5 Hash Digest implementation (little endian byte order)
4 
5 
6 #include <cstring>
7 #include <cstdio>
8 
9 #include <md5_hash.h>
10 
11 // DBL_INT_ADD treats two unsigned ints a and b as one 64-bit integer and adds c to it
12 #define DBL_INT_ADD(a,b,c) if (a > 0xffffffff - c) ++b; a += c;
13 #define ROTLEFT(a,b) ((a << b) | (a >> (32-b)))
14 
15 #define F(x,y,z) ((x & y) | (~x & z))
16 #define G(x,y,z) ((x & z) | (y & ~z))
17 #define H(x,y,z) (x ^ y ^ z)
18 #define I(x,y,z) (y ^ (x | ~z))
19 
20 #define FF(a,b,c,d,m,s,t) { a += F(b,c,d) + m + t; \
21  a = b + ROTLEFT(a,s); }
22 #define GG(a,b,c,d,m,s,t) { a += G(b,c,d) + m + t; \
23  a = b + ROTLEFT(a,s); }
24 #define HH(a,b,c,d,m,s,t) { a += H(b,c,d) + m + t; \
25  a = b + ROTLEFT(a,s); }
26 #define II(a,b,c,d,m,s,t) { a += I(b,c,d) + m + t; \
27  a = b + ROTLEFT(a,s); }
28 
29 
30 
31 
33 {
34  Init();
35 }
36 
37 MD5_HASH::MD5_HASH( const MD5_HASH& aOther )
38 {
39  m_valid = aOther.m_valid;
40  m_ctx = aOther.m_ctx;
41  memcpy( m_hash, aOther.m_hash, 16 );
42 }
43 
45 {
46 
47 }
48 
50 {
51  m_valid = aOther.m_valid;
52  m_ctx = aOther.m_ctx;
53  memcpy( m_hash, aOther.m_hash, 16 );
54 
55  return *this;
56 }
57 
58 
60 {
61  m_valid = false;
62  md5_init(&m_ctx);
63 }
64 
65 void MD5_HASH::Hash ( uint8_t *data, uint32_t length )
66 {
67  md5_update(&m_ctx, data, length);
68 }
69 
70 void MD5_HASH::Hash ( int value )
71 {
72  md5_update(&m_ctx, (uint8_t*) &value, sizeof(int) );
73 }
74 
76 {
78  m_valid = true;
79 
80 }
81 
82 bool MD5_HASH::operator==( const MD5_HASH& aOther ) const
83 {
84  return ( memcmp( m_hash, aOther.m_hash, 16 ) == 0 );
85 }
86 
87 bool MD5_HASH::operator!=( const MD5_HASH& aOther ) const
88 {
89  return ( memcmp( m_hash, aOther.m_hash, 16 ) != 0 );
90 }
91 
92 
93 std::string MD5_HASH::Format()
94 {
95  std::string data;
96 
97  // Build a hexadecimal string from the 16 bytes of MD5_HASH:
98  for( int ii = 0; ii < 16; ++ii )
99  {
100  char lsb = ( m_hash[ii] & 0x0F ) + '0';
101 
102  if( lsb > '9' )
103  lsb += 'A'-'9';
104 
105  char msb = ( ( m_hash[ii] >> 4 ) & 0x0F ) + '0';
106 
107  if( msb > '9' )
108  msb += 'A'-'9';
109 
110  data += msb;
111  data += lsb;
112  data += ' ';
113  }
114 
115  return data;
116 }
117 
118 
119 void MD5_HASH::md5_transform(MD5_CTX *ctx, uint8_t data[])
120 {
121  uint32_t a,b,c,d,m[16],i,j;
122 
123  // MD5 specifies big endian byte order, but this implementation assumes a little
124  // endian byte order CPU. Reverse all the bytes upon input, and re-reverse them
125  // on output (in md5_final()).
126  for (i=0,j=0; i < 16; ++i, j += 4)
127  m[i] = (data[j]) + (data[j+1] << 8) + (data[j+2] << 16) + (data[j+3] << 24);
128 
129  a = ctx->state[0];
130  b = ctx->state[1];
131  c = ctx->state[2];
132  d = ctx->state[3];
133 
134  FF(a,b,c,d,m[0], 7,0xd76aa478);
135  FF(d,a,b,c,m[1], 12,0xe8c7b756);
136  FF(c,d,a,b,m[2], 17,0x242070db);
137  FF(b,c,d,a,m[3], 22,0xc1bdceee);
138  FF(a,b,c,d,m[4], 7,0xf57c0faf);
139  FF(d,a,b,c,m[5], 12,0x4787c62a);
140  FF(c,d,a,b,m[6], 17,0xa8304613);
141  FF(b,c,d,a,m[7], 22,0xfd469501);
142  FF(a,b,c,d,m[8], 7,0x698098d8);
143  FF(d,a,b,c,m[9], 12,0x8b44f7af);
144  FF(c,d,a,b,m[10],17,0xffff5bb1);
145  FF(b,c,d,a,m[11],22,0x895cd7be);
146  FF(a,b,c,d,m[12], 7,0x6b901122);
147  FF(d,a,b,c,m[13],12,0xfd987193);
148  FF(c,d,a,b,m[14],17,0xa679438e);
149  FF(b,c,d,a,m[15],22,0x49b40821);
150 
151  GG(a,b,c,d,m[1], 5,0xf61e2562);
152  GG(d,a,b,c,m[6], 9,0xc040b340);
153  GG(c,d,a,b,m[11],14,0x265e5a51);
154  GG(b,c,d,a,m[0], 20,0xe9b6c7aa);
155  GG(a,b,c,d,m[5], 5,0xd62f105d);
156  GG(d,a,b,c,m[10], 9,0x02441453);
157  GG(c,d,a,b,m[15],14,0xd8a1e681);
158  GG(b,c,d,a,m[4], 20,0xe7d3fbc8);
159  GG(a,b,c,d,m[9], 5,0x21e1cde6);
160  GG(d,a,b,c,m[14], 9,0xc33707d6);
161  GG(c,d,a,b,m[3], 14,0xf4d50d87);
162  GG(b,c,d,a,m[8], 20,0x455a14ed);
163  GG(a,b,c,d,m[13], 5,0xa9e3e905);
164  GG(d,a,b,c,m[2], 9,0xfcefa3f8);
165  GG(c,d,a,b,m[7], 14,0x676f02d9);
166  GG(b,c,d,a,m[12],20,0x8d2a4c8a);
167 
168  HH(a,b,c,d,m[5], 4,0xfffa3942);
169  HH(d,a,b,c,m[8], 11,0x8771f681);
170  HH(c,d,a,b,m[11],16,0x6d9d6122);
171  HH(b,c,d,a,m[14],23,0xfde5380c);
172  HH(a,b,c,d,m[1], 4,0xa4beea44);
173  HH(d,a,b,c,m[4], 11,0x4bdecfa9);
174  HH(c,d,a,b,m[7], 16,0xf6bb4b60);
175  HH(b,c,d,a,m[10],23,0xbebfbc70);
176  HH(a,b,c,d,m[13], 4,0x289b7ec6);
177  HH(d,a,b,c,m[0], 11,0xeaa127fa);
178  HH(c,d,a,b,m[3], 16,0xd4ef3085);
179  HH(b,c,d,a,m[6], 23,0x04881d05);
180  HH(a,b,c,d,m[9], 4,0xd9d4d039);
181  HH(d,a,b,c,m[12],11,0xe6db99e5);
182  HH(c,d,a,b,m[15],16,0x1fa27cf8);
183  HH(b,c,d,a,m[2], 23,0xc4ac5665);
184 
185  II(a,b,c,d,m[0], 6,0xf4292244);
186  II(d,a,b,c,m[7], 10,0x432aff97);
187  II(c,d,a,b,m[14],15,0xab9423a7);
188  II(b,c,d,a,m[5], 21,0xfc93a039);
189  II(a,b,c,d,m[12], 6,0x655b59c3);
190  II(d,a,b,c,m[3], 10,0x8f0ccc92);
191  II(c,d,a,b,m[10],15,0xffeff47d);
192  II(b,c,d,a,m[1], 21,0x85845dd1);
193  II(a,b,c,d,m[8], 6,0x6fa87e4f);
194  II(d,a,b,c,m[15],10,0xfe2ce6e0);
195  II(c,d,a,b,m[6], 15,0xa3014314);
196  II(b,c,d,a,m[13],21,0x4e0811a1);
197  II(a,b,c,d,m[4], 6,0xf7537e82);
198  II(d,a,b,c,m[11],10,0xbd3af235);
199  II(c,d,a,b,m[2], 15,0x2ad7d2bb);
200  II(b,c,d,a,m[9], 21,0xeb86d391);
201 
202  ctx->state[0] += a;
203  ctx->state[1] += b;
204  ctx->state[2] += c;
205  ctx->state[3] += d;
206 }
207 
209 {
210  ctx->datalen = 0;
211  ctx->bitlen[0] = 0;
212  ctx->bitlen[1] = 0;
213  ctx->state[0] = 0x67452301;
214  ctx->state[1] = 0xEFCDAB89;
215  ctx->state[2] = 0x98BADCFE;
216  ctx->state[3] = 0x10325476;
217 }
218 
219 void MD5_HASH::md5_update(MD5_CTX *ctx, uint8_t data[], uint32_t len)
220 {
221  uint32_t i;
222 
223  for (i=0; i < len; ++i) {
224  ctx->data[ctx->datalen] = data[i];
225  ctx->datalen++;
226  if (ctx->datalen == 64) {
227  md5_transform(ctx,ctx->data);
228  DBL_INT_ADD(ctx->bitlen[0],ctx->bitlen[1],512);
229  ctx->datalen = 0;
230  }
231  }
232 }
233 
234 void MD5_HASH::md5_final(MD5_CTX *ctx, uint8_t hash[])
235 {
236  uint32_t i;
237 
238  i = ctx->datalen;
239 
240  // Pad whatever data is left in the buffer.
241  if (ctx->datalen < 56) {
242  ctx->data[i++] = 0x80;
243  while (i < 56)
244  ctx->data[i++] = 0x00;
245  }
246  else if (ctx->datalen >= 56) {
247  ctx->data[i++] = 0x80;
248  while (i < 64)
249  ctx->data[i++] = 0x00;
250  md5_transform(ctx,ctx->data);
251  memset(ctx->data,0,56);
252  }
253 
254  // Append to the padding the total message's length in bits and transform.
255  DBL_INT_ADD(ctx->bitlen[0],ctx->bitlen[1],8 * ctx->datalen);
256  ctx->data[56] = ctx->bitlen[0];
257  ctx->data[57] = ctx->bitlen[0] >> 8;
258  ctx->data[58] = ctx->bitlen[0] >> 16;
259  ctx->data[59] = ctx->bitlen[0] >> 24;
260  ctx->data[60] = ctx->bitlen[1];
261  ctx->data[61] = ctx->bitlen[1] >> 8;
262  ctx->data[62] = ctx->bitlen[1] >> 16;
263  ctx->data[63] = ctx->bitlen[1] >> 24;
264  md5_transform(ctx,ctx->data);
265 
266  // Since this implementation uses little endian byte ordering and MD uses big endian,
267  // reverse all the bytes when copying the final state to the output hash.
268  for (i=0; i < 4; ++i) {
269  hash[i] = (ctx->state[0] >> (i*8)) & 0x000000ff;
270  hash[i+4] = (ctx->state[1] >> (i*8)) & 0x000000ff;
271  hash[i+8] = (ctx->state[2] >> (i*8)) & 0x000000ff;
272  hash[i+12] = (ctx->state[3] >> (i*8)) & 0x000000ff;
273  }
274 }
MD5_HASH()
Definition: md5_hash.cpp:32
#define FF(a, b, c, d, m, s, t)
Definition: md5_hash.cpp:20
uint32_t bitlen[2]
Definition: md5_hash.h:42
bool operator!=(const MD5_HASH &aOther) const
Definition: md5_hash.cpp:87
void md5_final(MD5_CTX *ctx, uint8_t hash[])
Definition: md5_hash.cpp:234
#define GG(a, b, c, d, m, s, t)
Definition: md5_hash.cpp:22
#define II(a, b, c, d, m, s, t)
Definition: md5_hash.cpp:26
std::string Format()
Definition: md5_hash.cpp:93
void Hash(uint8_t *data, uint32_t length)
Definition: md5_hash.cpp:65
void md5_init(MD5_CTX *ctx)
Definition: md5_hash.cpp:208
#define HH(a, b, c, d, m, s, t)
Definition: md5_hash.cpp:24
bool m_valid
Definition: md5_hash.h:51
uint32_t state[4]
Definition: md5_hash.h:43
void md5_update(MD5_CTX *ctx, uint8_t data[], uint32_t len)
Definition: md5_hash.cpp:219
uint32_t datalen
Definition: md5_hash.h:41
void md5_transform(MD5_CTX *ctx, uint8_t data[])
Definition: md5_hash.cpp:119
uint8_t m_hash[16]
Definition: md5_hash.h:53
uint8_t data[64]
Definition: md5_hash.h:40
bool operator==(const MD5_HASH &aOther) const
Definition: md5_hash.cpp:82
void Init()
Definition: md5_hash.cpp:59
#define DBL_INT_ADD(a, b, c)
Definition: md5_hash.cpp:12
void Finalize()
Definition: md5_hash.cpp:75
MD5_CTX m_ctx
Definition: md5_hash.h:52
MD5_HASH & operator=(const MD5_HASH &aOther)
Definition: md5_hash.cpp:49
~MD5_HASH()
Definition: md5_hash.cpp:44