s/qmail 4.3.24
Next generation secure email transport
Loading...
Searching...
No Matches
md5c.c
Go to the documentation of this file.
1/* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
2 */
3
4/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
5 rights reserved.
6
7 License to copy and use this software is granted provided that it
8 is identified as the "RSA Data Security, Inc. MD5 Message-Digest
9 Algorithm" in all material mentioning or referencing this software
10 or this function.
11
12 License is also granted to make and use derivative works provided
13 that such works are identified as "derived from the RSA Data
14 Security, Inc. MD5 Message-Digest Algorithm" in all material
15 mentioning or referencing the derived work.
16
17 RSA Data Security, Inc. makes no representations concerning either
18 the merchantability of this software or the suitability of this
19 software for any particular purpose. It is provided "as is"
20 without express or implied warranty of any kind.
21
22 These notices must be retained in any copies of any part of this
23 documentation and/or software.
24 */
25
26/* Redone with current POSIX integer data types -- feh, Aug 2024 */
27
28#include "md5.h"
29#include "byte.h"
30#include <stdint.h>
31
32/* Constants for MD5Transform routine. */
33#define S11 7
34#define S12 12
35#define S13 17
36#define S14 22
37#define S21 5
38#define S22 9
39#define S23 14
40#define S24 20
41#define S31 4
42#define S32 11
43#define S33 16
44#define S34 23
45#define S41 6
46#define S42 10
47#define S43 15
48#define S44 21
49
50static void MD5Transform(uint32_t [4],unsigned char [64]);
51static void Encode(unsigned char *,uint32_t *,unsigned int);
52static void Decode(uint32_t *,unsigned char *,unsigned int);
53
54static unsigned char PADDING[64] = {
55 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
58};
59
60/* F, G, H and I are basic MD5 functions. */
61#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
62#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
63#define H(x, y, z) ((x) ^ (y) ^ (z))
64#define I(x, y, z) ((y) ^ ((x) | (~z)))
65
66/* ROTATE_LEFT rotates x left n bits. */
67#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
68
69/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
70 Rotation is separate from addition to prevent recomputation. */
71
72#define FF(a, b, c, d, x, s, ac) { \
73 (a) += F ((b), (c), (d)) + (x) + (ac); \
74 (a) = ROTATE_LEFT ((a), (s)); \
75 (a) += (b); \
76 }
77#define GG(a, b, c, d, x, s, ac) { \
78 (a) += G ((b), (c), (d)) + (x) + (ac); \
79 (a) = ROTATE_LEFT ((a), (s)); \
80 (a) += (b); \
81 }
82#define HH(a, b, c, d, x, s, ac) { \
83 (a) += H ((b), (c), (d)) + (x) + (ac); \
84 (a) = ROTATE_LEFT ((a), (s)); \
85 (a) += (b); \
86 }
87#define II(a, b, c, d, x, s, ac) { \
88 (a) += I ((b), (c), (d)) + (x) + (ac); \
89 (a) = ROTATE_LEFT ((a), (s)); \
90 (a) += (b); \
91 }
92
93/* MD5 initialization. Begins an MD5 operation, writing a new context. */
94void MD5Init(MD5_CTX *context)
95{
96 context->count[0] = context->count[1] = 0;
97
98 /* Load magic initialization constants. */
99 context->state[0] = 0x67452301;
100 context->state[1] = 0xefcdab89;
101 context->state[2] = 0x98badcfe;
102 context->state[3] = 0x10325476;
103}
104
105/* MD5 block update operation. Continues an MD5 message-digest operation,
106 processing another message block, and updating the context. */
107
108void MD5Update(MD5_CTX *context,unsigned char *input,uint32_t inputLen)
109{
110 uint32_t i, index, partLen;
111
112 /* Compute number of bytes mod 64 */
113 index = (context->count[0] >> 3) & 0x3F;
114
115 /* Update number of bits */
116 if ((context->count[0] += inputLen << 3)
117 < (inputLen << 3))
118 context->count[1]++;
119 context->count[1] += (inputLen >> 29);
120
121 partLen = 64 - index;
122
123 /* Transform as many times as possible. */
124 if (inputLen >= partLen) {
125 byte_copy((char *)&context->buffer[index],partLen,(char *)input);
126 MD5Transform(context->state,context->buffer);
127
128 for (i = partLen; i + 63 < inputLen; i += 64)
129 MD5Transform (context->state, &input[i]);
130
131 index = 0;
132 }
133 else
134 i = 0;
135
136 /* Buffer remaining input */
137 byte_copy((char *)&context->buffer[index],inputLen - i,(char *)&input[i]);
138}
139
140/* MD5 finalization. Ends an MD5 message-digest operation, writing the
141 the message digest and zeroizing the context. */
142
143void MD5Final(unsigned char digest[16],MD5_CTX *context)
144{
145 unsigned char bits[8];
146 uint32_t index, padLen;
147
148 /* Save number of bits */
149 Encode(bits, context->count, 8);
150
151 /* Pad out to 56 mod 64. */
152 index = (context->count[0] >> 3) & 0x3f;
153 padLen = (index < 56) ? (56 - index) : (120 - index);
154 MD5Update (context, PADDING, padLen);
155
156 /* Append length (before padding) */
157 MD5Update (context, bits, 8);
158
159 /* Store state in digest */
160 Encode(digest, context->state, 16);
161
162 /* Zeroize sensitive information. */
163 byte_zero((char *)context,sizeof(context));
164}
165
166/* MD5 basic transformation. Transforms state based on block. */
167
168static void MD5Transform (uint32_t state[4], unsigned char block[64])
169{
170 uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
171
172 Decode (x,block,64);
173
174 /* Round 1 */
175 FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
176 FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
177 FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
178 FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
179 FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
180 FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
181 FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
182 FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
183 FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
184 FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
185 FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
186 FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
187 FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
188 FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
189 FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
190 FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
191
192 /* Round 2 */
193 GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
194 GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
195 GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
196 GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
197 GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
198 GG (d, a, b, c, x[10], S22, 0x02441453); /* 22 */
199 GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
200 GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
201 GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
202 GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
203 GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
204 GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
205 GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
206 GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
207 GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
208 GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
209
210 /* Round 3 */
211 HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
212 HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
213 HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
214 HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
215 HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
216 HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
217 HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
218 HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
219 HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
220 HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
221 HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
222 HH (b, c, d, a, x[ 6], S34, 0x04881d05); /* 44 */
223 HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
224 HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
225 HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
226 HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
227
228 /* Round 4 */
229 II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
230 II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
231 II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
232 II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
233 II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
234 II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
235 II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
236 II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
237 II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
238 II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
239 II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
240 II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
241 II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
242 II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
243 II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
244 II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
245
246 state[0] += a;
247 state[1] += b;
248 state[2] += c;
249 state[3] += d;
250
251 /* Zeroize sensitive information. */
252 byte_zero((char *)x,sizeof(x));
253}
254
255/* Encodes input (uint32_t) into output (unsigned char).
256 Assumes len is a multiple of 4. */
257
258static void Encode(unsigned char *output,uint32_t *input,unsigned int len)
259{
260 unsigned int i, j;
261
262 for (i = 0, j = 0; j < len; i++, j += 4) {
263 output[j] = (unsigned char) (input[i] & 0xff);
264 output[j+1] = (unsigned char) ((input[i] >> 8) & 0xff);
265 output[j+2] = (unsigned char) ((input[i] >> 16) & 0xff);
266 output[j+3] = (unsigned char) ((input[i] >> 24) & 0xff);
267 }
268}
269
270/* Decodes input (unsigned char) into output (uint32_t).
271 Assumes len is a multiple of 4. */
272
273static void Decode(uint32_t *output,unsigned char *input,unsigned int len)
274{
275 unsigned int i, j;
276
277 for (i = 0, j = 0; j < len; i++, j += 4)
278 output[i] = ((uint32_t)input[j]) | (((uint32_t)input[j+1]) << 8) |
279 (((uint32_t)input[j+2]) << 16) | (((uint32_t)input[j+3]) << 24);
280}
buffer b
Definition auto-gid.c:10
void MD5Init(MD5_CTX *context)
Definition md5c.c:94
#define FF(a, b, c, d, x, s, ac)
Definition md5c.c:72
#define S24
Definition md5c.c:40
#define S33
Definition md5c.c:43
#define S32
Definition md5c.c:42
#define S12
Definition md5c.c:34
#define S42
Definition md5c.c:46
#define S11
Definition md5c.c:33
#define S43
Definition md5c.c:47
#define S23
Definition md5c.c:39
#define GG(a, b, c, d, x, s, ac)
Definition md5c.c:77
#define S44
Definition md5c.c:48
#define S14
Definition md5c.c:36
void MD5Update(MD5_CTX *context, unsigned char *input, uint32_t inputLen)
Definition md5c.c:108
#define HH(a, b, c, d, x, s, ac)
Definition md5c.c:82
void MD5Final(unsigned char digest[16], MD5_CTX *context)
Definition md5c.c:143
#define S13
Definition md5c.c:35
#define S41
Definition md5c.c:45
#define S21
Definition md5c.c:37
#define II(a, b, c, d, x, s, ac)
Definition md5c.c:87
#define S22
Definition md5c.c:38
#define S31
Definition md5c.c:41
#define S34
Definition md5c.c:44
int j
Definition qmail-send.c:926
struct del * d[CHANNELS]
Definition qmail-send.c:726
Definition md5.h:31
unsigned char buffer[64]
Definition md5.h:34
uint32_t count[2]
Definition md5.h:33
uint32_t state[4]
Definition md5.h:32