annotate src/md5/md5.c @ 170:0f0e4e7cd762

added misc/list-versions This script helps to check if the versions numbers in the project are the same as the one for the release. This script is motivated by the 0.2.27 release in which masqmail introduces itself as being version 0.2.26.
author meillo@marmaro.de
date Mon, 19 Jul 2010 14:01:13 +0200
parents 52c82d755215
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
162
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
1 /*
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
2 * This is an OpenSSL-compatible implementation of the RSA Data Security,
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
3 * Inc. MD5 Message-Digest Algorithm (RFC 1321).
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
4 *
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
5 * Written by Solar Designer <solar at openwall.com> in 2001, and placed
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
6 * in the public domain. There's absolutely no warranty.
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
7 *
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
8 * This differs from Colin Plumb's older public domain implementation in
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
9 * that no 32-bit integer data type is required, there's no compile-time
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
10 * endianness configuration, and the function prototypes match OpenSSL's.
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
11 * The primary goals are portability and ease of use.
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
12 *
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
13 * This implementation is meant to be fast, but not as fast as possible.
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
14 * Some known optimizations are not included to reduce source code size
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
15 * and avoid compile-time configuration.
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
16 */
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
17
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
18 #ifndef HAVE_OPENSSL
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
19
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
20 #include <string.h>
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
21
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
22 #include "md5.h"
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
23
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
24 /*
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
25 * The basic MD5 functions.
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
26 *
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
27 * F and G are optimized compared to their RFC 1321 definitions for
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
28 * architectures that lack an AND-NOT instruction, just like in Colin Plumb's
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
29 * implementation.
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
30 */
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
31 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
32 #define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
33 #define H(x, y, z) ((x) ^ (y) ^ (z))
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
34 #define I(x, y, z) ((y) ^ ((x) | ~(z)))
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
35
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
36 /*
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
37 * The MD5 transformation for all four rounds.
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
38 */
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
39 #define STEP(f, a, b, c, d, x, t, s) \
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
40 (a) += f((b), (c), (d)) + (x) + (t); \
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
41 (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
42 (a) += (b);
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
43
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
44 /*
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
45 * SET reads 4 input bytes in little-endian byte order and stores them
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
46 * in a properly aligned word in host byte order.
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
47 *
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
48 * The check for little-endian architectures that tolerate unaligned
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
49 * memory accesses is just an optimization. Nothing will break if it
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
50 * doesn't work.
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
51 */
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
52 #if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
53 #define SET(n) \
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
54 (*(MD5_u32plus *)&ptr[(n) * 4])
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
55 #define GET(n) \
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
56 SET(n)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
57 #else
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
58 #define SET(n) \
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
59 (ctx->block[(n)] = \
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
60 (MD5_u32plus)ptr[(n) * 4] | \
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
61 ((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
62 ((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
63 ((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
64 #define GET(n) \
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
65 (ctx->block[(n)])
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
66 #endif
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
67
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
68 /*
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
69 * This processes one or more 64-byte data blocks, but does NOT update
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
70 * the bit counters. There are no alignment requirements.
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
71 */
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
72 static void *body(MD5_CTX *ctx, void *data, unsigned long size)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
73 {
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
74 unsigned char *ptr;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
75 MD5_u32plus a, b, c, d;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
76 MD5_u32plus saved_a, saved_b, saved_c, saved_d;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
77
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
78 ptr = data;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
79
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
80 a = ctx->a;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
81 b = ctx->b;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
82 c = ctx->c;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
83 d = ctx->d;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
84
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
85 do {
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
86 saved_a = a;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
87 saved_b = b;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
88 saved_c = c;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
89 saved_d = d;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
90
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
91 /* Round 1 */
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
92 STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
93 STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
94 STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
95 STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
96 STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
97 STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
98 STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
99 STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
100 STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
101 STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
102 STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
103 STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
104 STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
105 STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
106 STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
107 STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
108
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
109 /* Round 2 */
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
110 STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
111 STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
112 STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
113 STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
114 STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
115 STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
116 STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
117 STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
118 STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
119 STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
120 STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
121 STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
122 STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
123 STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
124 STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
125 STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
126
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
127 /* Round 3 */
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
128 STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
129 STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
130 STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
131 STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
132 STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
133 STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
134 STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
135 STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
136 STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
137 STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
138 STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
139 STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
140 STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
141 STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
142 STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
143 STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
144
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
145 /* Round 4 */
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
146 STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
147 STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
148 STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
149 STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
150 STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
151 STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
152 STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
153 STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
154 STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
155 STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
156 STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
157 STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
158 STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
159 STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
160 STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
161 STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
162
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
163 a += saved_a;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
164 b += saved_b;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
165 c += saved_c;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
166 d += saved_d;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
167
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
168 ptr += 64;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
169 } while (size -= 64);
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
170
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
171 ctx->a = a;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
172 ctx->b = b;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
173 ctx->c = c;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
174 ctx->d = d;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
175
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
176 return ptr;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
177 }
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
178
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
179 void MD5_Init(MD5_CTX *ctx)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
180 {
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
181 ctx->a = 0x67452301;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
182 ctx->b = 0xefcdab89;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
183 ctx->c = 0x98badcfe;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
184 ctx->d = 0x10325476;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
185
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
186 ctx->lo = 0;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
187 ctx->hi = 0;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
188 }
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
189
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
190 void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
191 {
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
192 MD5_u32plus saved_lo;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
193 unsigned long used, free;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
194
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
195 saved_lo = ctx->lo;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
196 if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
197 ctx->hi++;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
198 ctx->hi += size >> 29;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
199
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
200 used = saved_lo & 0x3f;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
201
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
202 if (used) {
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
203 free = 64 - used;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
204
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
205 if (size < free) {
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
206 memcpy(&ctx->buffer[used], data, size);
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
207 return;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
208 }
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
209
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
210 memcpy(&ctx->buffer[used], data, free);
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
211 data = (unsigned char *)data + free;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
212 size -= free;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
213 body(ctx, ctx->buffer, 64);
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
214 }
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
215
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
216 if (size >= 64) {
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
217 data = body(ctx, data, size & ~(unsigned long)0x3f);
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
218 size &= 0x3f;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
219 }
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
220
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
221 memcpy(ctx->buffer, data, size);
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
222 }
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
223
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
224 void MD5_Final(unsigned char *result, MD5_CTX *ctx)
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
225 {
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
226 unsigned long used, free;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
227
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
228 used = ctx->lo & 0x3f;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
229
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
230 ctx->buffer[used++] = 0x80;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
231
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
232 free = 64 - used;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
233
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
234 if (free < 8) {
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
235 memset(&ctx->buffer[used], 0, free);
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
236 body(ctx, ctx->buffer, 64);
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
237 used = 0;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
238 free = 64;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
239 }
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
240
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
241 memset(&ctx->buffer[used], 0, free - 8);
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
242
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
243 ctx->lo <<= 3;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
244 ctx->buffer[56] = ctx->lo;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
245 ctx->buffer[57] = ctx->lo >> 8;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
246 ctx->buffer[58] = ctx->lo >> 16;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
247 ctx->buffer[59] = ctx->lo >> 24;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
248 ctx->buffer[60] = ctx->hi;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
249 ctx->buffer[61] = ctx->hi >> 8;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
250 ctx->buffer[62] = ctx->hi >> 16;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
251 ctx->buffer[63] = ctx->hi >> 24;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
252
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
253 body(ctx, ctx->buffer, 64);
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
254
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
255 result[0] = ctx->a;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
256 result[1] = ctx->a >> 8;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
257 result[2] = ctx->a >> 16;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
258 result[3] = ctx->a >> 24;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
259 result[4] = ctx->b;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
260 result[5] = ctx->b >> 8;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
261 result[6] = ctx->b >> 16;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
262 result[7] = ctx->b >> 24;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
263 result[8] = ctx->c;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
264 result[9] = ctx->c >> 8;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
265 result[10] = ctx->c >> 16;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
266 result[11] = ctx->c >> 24;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
267 result[12] = ctx->d;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
268 result[13] = ctx->d >> 8;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
269 result[14] = ctx->d >> 16;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
270 result[15] = ctx->d >> 24;
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
271
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
272 memset(ctx, 0, sizeof(*ctx));
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
273 }
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
274
52c82d755215 replaced the MD5 implementation with the one of Solar Designer
meillo@marmaro.de
parents:
diff changeset
275 #endif