1/* These are the four functions used in the four steps of the MD5 algorithm
2 and defined in the RFC 1321. The first function is a little bit optimized
3 (as found in Colin Plumbs public domain implementation). */
4/* #define FF(b, c, d) ((b & c) | (~b & d)) */
5#define FF(b, c, d) (d ^ (b & (c ^ d)))
6#define FG(b, c, d) FF (d, b, c)
7#define FH(b, c, d) (b ^ c ^ d)
8#define FI(b, c, d) (c ^ (b | ~d))
9
10/* Process LEN bytes of BUFFER, accumulating context into CTX.
11 It is assumed that LEN % 64 == 0. */
12
13void
14__md5_process_block (const void *buffer, size_t len, struct md5_ctx *ctx)
15{
16 md5_uint32 correct_words[16];
17 const md5_uint32 *words = buffer;
18 size_t nwords = len / sizeof (md5_uint32);
19 const md5_uint32 *endp = words + nwords;
20 md5_uint32 A = ctx->A;
21 md5_uint32 B = ctx->B;
22 md5_uint32 C = ctx->C;
23 md5_uint32 D = ctx->D;
24 md5_uint32 lolen = len;
25
26 /* First increment the byte count. RFC 1321 specifies the possible
27 length of the file up to 2^64 bits. Here we only compute the
28 number of bytes. Do a double word increment. */
29 ctx->total[0] += lolen;
30 ctx->total[1] += (len >> 31 >> 1) + (ctx->total[0] < lolen);
31
32 /* Process all bytes in the buffer with 64 bytes in each round of
33 the loop. */
34 while (words < endp)
35 {
36 md5_uint32 *cwp = correct_words;
37 md5_uint32 A_save = A;
38 md5_uint32 B_save = B;
39 md5_uint32 C_save = C;
40 md5_uint32 D_save = D;
41
42 /* First round: using the given function, the context and a constant
43 the next context is computed. Because the algorithms processing
44 unit is a 32-bit word and it is determined to work on words in
45 little endian byte order we perhaps have to change the byte order
46 before the computation. To reduce the work for the next steps
47 we store the swapped words in the array CORRECT_WORDS. */
48
49#define OP(a, b, c, d, s, T) \
50 do \
51 { \
52 a += FF (b, c, d) + (*cwp++ = SWAP (*words)) + T; \
53 ++words; \
54 CYCLIC (a, s); \
55 a += b; \
56 } \
57 while (0)
58
59 /* It is unfortunate that C does not provide an operator for
60 cyclic rotation. Hope the C compiler is smart enough. */
61#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
62
63 /* Before we start, one word to the strange constants.
64 They are defined in RFC 1321 as
65
66 T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
67 */
68
69 /* Round 1. */
70 OP (A, B, C, D, 7, 0xd76aa478);
71 OP (D, A, B, C, 12, 0xe8c7b756);
72 OP (C, D, A, B, 17, 0x242070db);
73 OP (B, C, D, A, 22, 0xc1bdceee);
74 OP (A, B, C, D, 7, 0xf57c0faf);
75 OP (D, A, B, C, 12, 0x4787c62a);
76 OP (C, D, A, B, 17, 0xa8304613);
77 OP (B, C, D, A, 22, 0xfd469501);
78 OP (A, B, C, D, 7, 0x698098d8);
79 OP (D, A, B, C, 12, 0x8b44f7af);
80 OP (C, D, A, B, 17, 0xffff5bb1);
81 OP (B, C, D, A, 22, 0x895cd7be);
82 OP (A, B, C, D, 7, 0x6b901122);
83 OP (D, A, B, C, 12, 0xfd987193);
84 OP (C, D, A, B, 17, 0xa679438e);
85 OP (B, C, D, A, 22, 0x49b40821);
86
87 /* For the second to fourth round we have the possibly swapped words
88 in CORRECT_WORDS. Redefine the macro to take an additional first
89 argument specifying the function to use. */
90#undef OP
91#define OP(f, a, b, c, d, k, s, T) \
92 do \
93 { \
94 a += f (b, c, d) + correct_words[k] + T; \
95 CYCLIC (a, s); \
96 a += b; \
97 } \
98 while (0)
99
100 /* Round 2. */
101 OP (FG, A, B, C, D, 1, 5, 0xf61e2562);
102 OP (FG, D, A, B, C, 6, 9, 0xc040b340);
103 OP (FG, C, D, A, B, 11, 14, 0x265e5a51);
104 OP (FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
105 OP (FG, A, B, C, D, 5, 5, 0xd62f105d);
106 OP (FG, D, A, B, C, 10, 9, 0x02441453);
107 OP (FG, C, D, A, B, 15, 14, 0xd8a1e681);
108 OP (FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
109 OP (FG, A, B, C, D, 9, 5, 0x21e1cde6);
110 OP (FG, D, A, B, C, 14, 9, 0xc33707d6);
111 OP (FG, C, D, A, B, 3, 14, 0xf4d50d87);
112 OP (FG, B, C, D, A, 8, 20, 0x455a14ed);
113 OP (FG, A, B, C, D, 13, 5, 0xa9e3e905);
114 OP (FG, D, A, B, C, 2, 9, 0xfcefa3f8);
115 OP (FG, C, D, A, B, 7, 14, 0x676f02d9);
116 OP (FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
117
118 /* Round 3. */
119 OP (FH, A, B, C, D, 5, 4, 0xfffa3942);
120 OP (FH, D, A, B, C, 8, 11, 0x8771f681);
121 OP (FH, C, D, A, B, 11, 16, 0x6d9d6122);
122 OP (FH, B, C, D, A, 14, 23, 0xfde5380c);
123 OP (FH, A, B, C, D, 1, 4, 0xa4beea44);
124 OP (FH, D, A, B, C, 4, 11, 0x4bdecfa9);
125 OP (FH, C, D, A, B, 7, 16, 0xf6bb4b60);
126 OP (FH, B, C, D, A, 10, 23, 0xbebfbc70);
127 OP (FH, A, B, C, D, 13, 4, 0x289b7ec6);
128 OP (FH, D, A, B, C, 0, 11, 0xeaa127fa);
129 OP (FH, C, D, A, B, 3, 16, 0xd4ef3085);
130 OP (FH, B, C, D, A, 6, 23, 0x04881d05);
131 OP (FH, A, B, C, D, 9, 4, 0xd9d4d039);
132 OP (FH, D, A, B, C, 12, 11, 0xe6db99e5);
133 OP (FH, C, D, A, B, 15, 16, 0x1fa27cf8);
134 OP (FH, B, C, D, A, 2, 23, 0xc4ac5665);
135
136 /* Round 4. */
137 OP (FI, A, B, C, D, 0, 6, 0xf4292244);
138 OP (FI, D, A, B, C, 7, 10, 0x432aff97);
139 OP (FI, C, D, A, B, 14, 15, 0xab9423a7);
140 OP (FI, B, C, D, A, 5, 21, 0xfc93a039);
141 OP (FI, A, B, C, D, 12, 6, 0x655b59c3);
142 OP (FI, D, A, B, C, 3, 10, 0x8f0ccc92);
143 OP (FI, C, D, A, B, 10, 15, 0xffeff47d);
144 OP (FI, B, C, D, A, 1, 21, 0x85845dd1);
145 OP (FI, A, B, C, D, 8, 6, 0x6fa87e4f);
146 OP (FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
147 OP (FI, C, D, A, B, 6, 15, 0xa3014314);
148 OP (FI, B, C, D, A, 13, 21, 0x4e0811a1);
149 OP (FI, A, B, C, D, 4, 6, 0xf7537e82);
150 OP (FI, D, A, B, C, 11, 10, 0xbd3af235);
151 OP (FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
152 OP (FI, B, C, D, A, 9, 21, 0xeb86d391);
153
154 /* Add the starting values of the context. */
155 A += A_save;
156 B += B_save;
157 C += C_save;
158 D += D_save;
159 }
160
161 /* Put checksum in context given as argument. */
162 ctx->A = A;
163 ctx->B = B;
164 ctx->C = C;
165 ctx->D = D;
166}
167