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