49 static inline winpr_MD5_u32plus F(winpr_MD5_u32plus x, winpr_MD5_u32plus y, winpr_MD5_u32plus z)
51 return ((z) ^ ((x) & ((y) ^ (z))));
53 static inline winpr_MD5_u32plus G(winpr_MD5_u32plus x, winpr_MD5_u32plus y, winpr_MD5_u32plus z)
55 return ((y) ^ ((z) & ((x) ^ (y))));
57 static inline winpr_MD5_u32plus H(winpr_MD5_u32plus x, winpr_MD5_u32plus y, winpr_MD5_u32plus z)
59 return (((x) ^ (y)) ^ (z));
61 static inline winpr_MD5_u32plus H2(winpr_MD5_u32plus x, winpr_MD5_u32plus y, winpr_MD5_u32plus z)
63 return ((x) ^ ((y) ^ (z)));
65 static inline winpr_MD5_u32plus I(winpr_MD5_u32plus x, winpr_MD5_u32plus y, winpr_MD5_u32plus z)
67 return ((y) ^ ((x) | ~(z)));
73 #define STEP(f, a, b, c, d, x, t, s) \
74 (a) += f((b), (c), (d)) + (x) + (t); \
75 (a) = (((a) << (s)) | (((a)&0xffffffff) >> (32 - (s)))); \
93 #if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
94 #define SET(n) (*(const winpr_MD5_u32plus*)&ptr[4ULL * (n)])
98 (ctx->block[(n)] = (winpr_MD5_u32plus)ptr[4ULL * (n)] | \
99 ((winpr_MD5_u32plus)ptr[4ULL * (n) + 1] << 8) | \
100 ((winpr_MD5_u32plus)ptr[4ULL * (n) + 2] << 16) | \
101 ((winpr_MD5_u32plus)ptr[4ULL * (n) + 3] << 24))
102 #define GET(n) (ctx->block[(n)])
109 static const void* body(
WINPR_MD5_CTX* ctx,
const void* data,
unsigned long size)
111 const unsigned char* ptr = (
const unsigned char*)data;
113 winpr_MD5_u32plus a = ctx->a;
114 winpr_MD5_u32plus b = ctx->b;
115 winpr_MD5_u32plus c = ctx->c;
116 winpr_MD5_u32plus d = ctx->d;
120 const winpr_MD5_u32plus saved_a = a;
121 const winpr_MD5_u32plus saved_b = b;
122 const winpr_MD5_u32plus saved_c = c;
123 const winpr_MD5_u32plus saved_d = d;
126 STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
127 STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
128 STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
129 STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
130 STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
131 STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
132 STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
133 STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
134 STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
135 STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
136 STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
137 STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
138 STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
139 STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
140 STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
141 STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
144 STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
145 STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
146 STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
147 STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
148 STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
149 STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
150 STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
151 STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
152 STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
153 STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
154 STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
155 STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
156 STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
157 STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
158 STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
159 STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
162 STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
163 STEP(H2, d, a, b, c, GET(8), 0x8771f681, 11)
164 STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
165 STEP(H2, b, c, d, a, GET(14), 0xfde5380c, 23)
166 STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
167 STEP(H2, d, a, b, c, GET(4), 0x4bdecfa9, 11)
168 STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
169 STEP(H2, b, c, d, a, GET(10), 0xbebfbc70, 23)
170 STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
171 STEP(H2, d, a, b, c, GET(0), 0xeaa127fa, 11)
172 STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
173 STEP(H2, b, c, d, a, GET(6), 0x04881d05, 23)
174 STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
175 STEP(H2, d, a, b, c, GET(12), 0xe6db99e5, 11)
176 STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
177 STEP(H2, b, c, d, a, GET(2), 0xc4ac5665, 23)
180 STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
181 STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
182 STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
183 STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
184 STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
185 STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
186 STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
187 STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
188 STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
189 STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
190 STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
191 STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
192 STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
193 STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
194 STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
195 STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
203 } while (size -= 64);
224 void winpr_MD5_Update(
WINPR_MD5_CTX* ctx,
const void* data,
unsigned long size)
226 winpr_MD5_u32plus saved_lo = ctx->lo;
227 if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
229 ctx->hi += size >> 29;
231 unsigned long used = saved_lo & 0x3f;
235 unsigned long available = 64 - used;
237 if (size < available)
239 memcpy(&ctx->buffer[used], data, size);
243 memcpy(&ctx->buffer[used], data, available);
244 data = (
const unsigned char*)data + available;
246 body(ctx, ctx->buffer, 64);
251 data = body(ctx, data, size & ~(
unsigned long)0x3f);
255 memcpy(ctx->buffer, data, size);
258 static inline void mdOUT(
unsigned char* dst, winpr_MD5_u32plus src)
260 (dst)[0] = (
unsigned char)(src);
261 (dst)[1] = (
unsigned char)((src) >> 8);
262 (dst)[2] = (
unsigned char)((src) >> 16);
263 (dst)[3] = (
unsigned char)((src) >> 24);
266 void winpr_MD5_Final(
unsigned char* result,
WINPR_MD5_CTX* ctx)
268 unsigned long used = ctx->lo & 0x3f;
270 ctx->buffer[used++] = 0x80;
272 unsigned long available = 64 - used;
276 memset(&ctx->buffer[used], 0, available);
277 body(ctx, ctx->buffer, 64);
282 memset(&ctx->buffer[used], 0, available - 8);
285 mdOUT(&ctx->buffer[56], ctx->lo);
286 mdOUT(&ctx->buffer[60], ctx->hi);
288 body(ctx, ctx->buffer, 64);
290 mdOUT(&result[0], ctx->a);
291 mdOUT(&result[4], ctx->b);
292 mdOUT(&result[8], ctx->c);
293 mdOUT(&result[12], ctx->d);
295 memset(ctx, 0,
sizeof(*ctx));