1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
/*
md5.c
small size MD5 20080116
public domain
written by Nikola Vladov
inspired by ditlibc, libtomcrypt, bglibs
*/
#include <stdint.h>
#include <endian.h>
#include "md5.h"
#ifndef __BYTE_ORDER
#error __BYTE_ORDER is undefined
#endif
#if (__BYTE_ORDER == __BIG_ENDIAN)
/*
Block copy and convert byte order to little-endian.
dst must be 32bit aligned.
Length is the number of 32bit words
*/
static void load_lendian (uint32_t *dst, const uint8_t *src, int length) {
while (length--) {
*dst=(((uint32_t)src[3])<<24) |
(((uint32_t)src[2])<<16) |
(((uint32_t)src[1])<< 8) |
(uint32_t)src[0];
src+=4;
dst++;
}
}
#define STORE32L(buf,s,len) \
load_lendian((uint32_t *)(buf), (const uint8_t *)(s), len)
#else
#define STORE32L(buf,s,len) byte_copy(buf,len*4,s)
#endif
#define ROL(a,s) ((a<<s)|(a>>(32-s)))
#define F(x,y,z) (z^(x&(y^z)))
#define G(x,y,z) (y^(z&(y^x)))
#define H(x,y,z) (x^y^z)
#define I(x,y,z) (y^(x|(~z)))
#define SET4(A,B,C,D,a,b,c,d) A =a; B =b; C =c; D =d
#define PLUS4(A,B,C,D,a,b,c,d) A+=a; B+=b; C+=c; D+=d
static const unsigned char Worder[64] = {
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
1,6,11,0,5,10,15,4,9,14,3,8,13,2,7,12,
5,8,11,14,1,4,7,10,13,0,3,6,9,12,15,2,
0,7,14,5,12,3,10,1,8,15,6,13,4,11,2,9
};
static const unsigned char Rorder[64] = {
7,12,17,22,7,12,17,22,7,12,17,22,7,12,17,22,
5,9,14,20,5,9,14,20,5,9,14,20,5,9,14,20,
4,11,16,23,4,11,16,23,4,11,16,23,4,11,16,23,
6,10,15,21,6,10,15,21,6,10,15,21,6,10,15,21
};
static const uint32_t Korder[64] = {
0xd76aa478UL, 0xe8c7b756UL, 0x242070dbUL, 0xc1bdceeeUL, 0xf57c0fafUL,
0x4787c62aUL, 0xa8304613UL, 0xfd469501UL, 0x698098d8UL, 0x8b44f7afUL,
0xffff5bb1UL, 0x895cd7beUL, 0x6b901122UL, 0xfd987193UL, 0xa679438eUL,
0x49b40821UL, 0xf61e2562UL, 0xc040b340UL, 0x265e5a51UL, 0xe9b6c7aaUL,
0xd62f105dUL, 0x02441453UL, 0xd8a1e681UL, 0xe7d3fbc8UL, 0x21e1cde6UL,
0xc33707d6UL, 0xf4d50d87UL, 0x455a14edUL, 0xa9e3e905UL, 0xfcefa3f8UL,
0x676f02d9UL, 0x8d2a4c8aUL, 0xfffa3942UL, 0x8771f681UL, 0x6d9d6122UL,
0xfde5380cUL, 0xa4beea44UL, 0x4bdecfa9UL, 0xf6bb4b60UL, 0xbebfbc70UL,
0x289b7ec6UL, 0xeaa127faUL, 0xd4ef3085UL, 0x04881d05UL, 0xd9d4d039UL,
0xe6db99e5UL, 0x1fa27cf8UL, 0xc4ac5665UL, 0xf4292244UL, 0x432aff97UL,
0xab9423a7UL, 0xfc93a039UL, 0x655b59c3UL, 0x8f0ccc92UL, 0xffeff47dUL,
0x85845dd1UL, 0x6fa87e4fUL, 0xfe2ce6e0UL, 0xa3014314UL, 0x4e0811a1UL,
0xf7537e82UL, 0xbd3af235UL, 0x2ad7d2bbUL, 0xeb86d391UL
};
static void md5_compress(md5_state *md, const unsigned char *buf) {
uint32_t i, a, b, c, d, t, *W = (uint32_t *)md->buf;
STORE32L(W,buf,16);
SET4(a,b,c,d, md->state[0], md->state[1], md->state[2], md->state[3]);
for (i = 0; i < 64; ++i) {
if (i<16) a += F(b,c,d);
else if (i<32) a += G(b,c,d);
else if (i<48) a += H(b,c,d);
else a += I(b,c,d);
a += W[Worder[i]] + Korder[i];
t = ROL(a, Rorder[i]) + b;
SET4(a,d,c,b, d,c,b,t);
}
PLUS4(md->state[0], md->state[1], md->state[2], md->state[3], a,b,c,d);
}
/**
Initialize the hash state
@param md The hash state you wish to initialize
*/
static const uint32_t init_state[4] = {
0x67452301UL, 0xefcdab89UL, 0x98badcfeUL, 0x10325476UL
};
void md5_init(md5_state *md) /*EXTRACT_INCL*/{
md->length = 0;
byte_copy(md->state, sizeof(init_state), init_state);
}
/**
Process a block of memory though the hash
@param md The hash state
@param in The data to hash
@param inlen The length of the data (octets)
*/
void md5_update(md5_state *md,const unsigned char *vdata,unsigned long data_len) /*EXTRACT_INCL*/{
const unsigned char *data = vdata;
unsigned use, mlen = md->length % 64;
md->length += data_len;
if (mlen > 0 && data_len >= (use = 64 - mlen)) {
byte_copy(md->buf + mlen, use, data);
md5_compress(md, md->buf);
mlen = 0;
data_len -= use;
data += use;
}
while (data_len >= 64) {
md5_compress (md, data);
data_len -= 64;
data += 64;
}
byte_copy(md->buf + mlen, data_len, data);
}
/**
Terminate the hash to get the digest
@param md The hash state
@param out [out] The destination of the hash (16 bytes)
*/
void md5_final(md5_state *md, unsigned char *out) /*EXTRACT_INCL*/{
uint64_t length = md->length;
uint32_t mlen = length % 64;
/* increase the length of the message */
length *= 8;
/* append the '1' bit */
md->buf[mlen++] = (unsigned char)0x80;
/* if the length is currently above 56 bytes we append zeros
* then compress. Then we can fall back to padding zeros and length
* encoding like normal.
*/
byte_zero(md->buf + mlen, 64-mlen);
if (mlen > 64-8) {
md5_compress(md, md->buf);
byte_zero(md->buf, 64-8);
}
/* store length */
#if (__BYTE_ORDER == __BIG_ENDIAN)
length = (length<<32)|(length>>32)
#endif
STORE32L(md->buf+56, &length, 2);
md5_compress(md, md->buf);
/* copy output */
#if (__BYTE_ORDER == __BIG_ENDIAN)
STORE32L(md->state, md->state, 4);
#endif
byte_copy(out, 16, md->state);
byte_zero(md, sizeof(md5_state));
}
|