xref: /openbsd/lib/libcrypto/md5/md5.c (revision 82ab76e1)
1 /* $OpenBSD: md5.c,v 1.25 2025/01/24 13:35:04 jsing Exp $ */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58 
59 #include <stdio.h>
60 #include <stdint.h>
61 #include <stdlib.h>
62 #include <string.h>
63 
64 #include <openssl/opensslconf.h>
65 
66 #include <openssl/md5.h>
67 
68 #include "crypto_internal.h"
69 
70 /* Ensure that MD5_LONG and uint32_t are equivalent size. */
71 CTASSERT(sizeof(MD5_LONG) == sizeof(uint32_t));
72 
73 #ifdef MD5_ASM
74 void md5_block_data_order(MD5_CTX *c, const void *p, size_t num);
75 #endif
76 
77 #ifndef MD5_ASM
78 static inline uint32_t
md5_F(uint32_t x,uint32_t y,uint32_t z)79 md5_F(uint32_t x, uint32_t y, uint32_t z)
80 {
81 	return (x & y) | (~x & z);
82 }
83 
84 static inline uint32_t
md5_G(uint32_t x,uint32_t y,uint32_t z)85 md5_G(uint32_t x, uint32_t y, uint32_t z)
86 {
87 	return (x & z) | (y & ~z);
88 }
89 
90 static inline uint32_t
md5_H(uint32_t x,uint32_t y,uint32_t z)91 md5_H(uint32_t x, uint32_t y, uint32_t z)
92 {
93 	return x ^ y ^ z;
94 }
95 
96 static inline uint32_t
md5_I(uint32_t x,uint32_t y,uint32_t z)97 md5_I(uint32_t x, uint32_t y, uint32_t z)
98 {
99 	return y ^ (x | ~z);
100 }
101 
102 static inline void
md5_round1(uint32_t * a,uint32_t b,uint32_t c,uint32_t d,uint32_t x,uint32_t t,uint32_t s)103 md5_round1(uint32_t *a, uint32_t b, uint32_t c, uint32_t d, uint32_t x,
104     uint32_t t, uint32_t s)
105 {
106 	*a = b + crypto_rol_u32(*a + md5_F(b, c, d) + x + t, s);
107 }
108 
109 static inline void
md5_round2(uint32_t * a,uint32_t b,uint32_t c,uint32_t d,uint32_t x,uint32_t t,uint32_t s)110 md5_round2(uint32_t *a, uint32_t b, uint32_t c, uint32_t d, uint32_t x,
111     uint32_t t, uint32_t s)
112 {
113 	*a = b + crypto_rol_u32(*a + md5_G(b, c, d) + x + t, s);
114 }
115 
116 static inline void
md5_round3(uint32_t * a,uint32_t b,uint32_t c,uint32_t d,uint32_t x,uint32_t t,uint32_t s)117 md5_round3(uint32_t *a, uint32_t b, uint32_t c, uint32_t d, uint32_t x,
118     uint32_t t, uint32_t s)
119 {
120 	*a = b + crypto_rol_u32(*a + md5_H(b, c, d) + x + t, s);
121 }
122 
123 static inline void
md5_round4(uint32_t * a,uint32_t b,uint32_t c,uint32_t d,uint32_t x,uint32_t t,uint32_t s)124 md5_round4(uint32_t *a, uint32_t b, uint32_t c, uint32_t d, uint32_t x,
125     uint32_t t, uint32_t s)
126 {
127 	*a = b + crypto_rol_u32(*a + md5_I(b, c, d) + x + t, s);
128 }
129 
130 static void
md5_block_data_order(MD5_CTX * c,const void * _in,size_t num)131 md5_block_data_order(MD5_CTX *c, const void *_in, size_t num)
132 {
133 	const uint8_t *in = _in;
134 	const MD5_LONG *in32;
135 	MD5_LONG A, B, C, D;
136 	MD5_LONG X0, X1, X2, X3, X4, X5, X6, X7,
137 	    X8, X9, X10, X11, X12, X13, X14, X15;
138 
139 	while (num-- > 0) {
140 		A = c->A;
141 		B = c->B;
142 		C = c->C;
143 		D = c->D;
144 
145 		if ((uintptr_t)in % 4 == 0) {
146 			/* Input is 32 bit aligned. */
147 			in32 = (const MD5_LONG *)in;
148 			X0 = le32toh(in32[0]);
149 			X1 = le32toh(in32[1]);
150 			X2 = le32toh(in32[2]);
151 			X3 = le32toh(in32[3]);
152 			X4 = le32toh(in32[4]);
153 			X5 = le32toh(in32[5]);
154 			X6 = le32toh(in32[6]);
155 			X7 = le32toh(in32[7]);
156 			X8 = le32toh(in32[8]);
157 			X9 = le32toh(in32[9]);
158 			X10 = le32toh(in32[10]);
159 			X11 = le32toh(in32[11]);
160 			X12 = le32toh(in32[12]);
161 			X13 = le32toh(in32[13]);
162 			X14 = le32toh(in32[14]);
163 			X15 = le32toh(in32[15]);
164 		} else {
165 			/* Input is not 32 bit aligned. */
166 			X0 = crypto_load_le32toh(&in[0 * 4]);
167 			X1 = crypto_load_le32toh(&in[1 * 4]);
168 			X2 = crypto_load_le32toh(&in[2 * 4]);
169 			X3 = crypto_load_le32toh(&in[3 * 4]);
170 			X4 = crypto_load_le32toh(&in[4 * 4]);
171 			X5 = crypto_load_le32toh(&in[5 * 4]);
172 			X6 = crypto_load_le32toh(&in[6 * 4]);
173 			X7 = crypto_load_le32toh(&in[7 * 4]);
174 			X8 = crypto_load_le32toh(&in[8 * 4]);
175 			X9 = crypto_load_le32toh(&in[9 * 4]);
176 			X10 = crypto_load_le32toh(&in[10 * 4]);
177 			X11 = crypto_load_le32toh(&in[11 * 4]);
178 			X12 = crypto_load_le32toh(&in[12 * 4]);
179 			X13 = crypto_load_le32toh(&in[13 * 4]);
180 			X14 = crypto_load_le32toh(&in[14 * 4]);
181 			X15 = crypto_load_le32toh(&in[15 * 4]);
182 		}
183 		in += MD5_CBLOCK;
184 
185 		md5_round1(&A, B, C, D, X0, 0xd76aa478L, 7);
186 		md5_round1(&D, A, B, C, X1, 0xe8c7b756L, 12);
187 		md5_round1(&C, D, A, B, X2, 0x242070dbL, 17);
188 		md5_round1(&B, C, D, A, X3, 0xc1bdceeeL, 22);
189 		md5_round1(&A, B, C, D, X4, 0xf57c0fafL, 7);
190 		md5_round1(&D, A, B, C, X5, 0x4787c62aL, 12);
191 		md5_round1(&C, D, A, B, X6, 0xa8304613L, 17);
192 		md5_round1(&B, C, D, A, X7, 0xfd469501L, 22);
193 		md5_round1(&A, B, C, D, X8, 0x698098d8L, 7);
194 		md5_round1(&D, A, B, C, X9, 0x8b44f7afL, 12);
195 		md5_round1(&C, D, A, B, X10, 0xffff5bb1L, 17);
196 		md5_round1(&B, C, D, A, X11, 0x895cd7beL, 22);
197 		md5_round1(&A, B, C, D, X12, 0x6b901122L, 7);
198 		md5_round1(&D, A, B, C, X13, 0xfd987193L, 12);
199 		md5_round1(&C, D, A, B, X14, 0xa679438eL, 17);
200 		md5_round1(&B, C, D, A, X15, 0x49b40821L, 22);
201 
202 		md5_round2(&A, B, C, D, X1, 0xf61e2562L, 5);
203 		md5_round2(&D, A, B, C, X6, 0xc040b340L, 9);
204 		md5_round2(&C, D, A, B, X11, 0x265e5a51L, 14);
205 		md5_round2(&B, C, D, A, X0, 0xe9b6c7aaL, 20);
206 		md5_round2(&A, B, C, D, X5, 0xd62f105dL, 5);
207 		md5_round2(&D, A, B, C, X10, 0x02441453L, 9);
208 		md5_round2(&C, D, A, B, X15, 0xd8a1e681L, 14);
209 		md5_round2(&B, C, D, A, X4, 0xe7d3fbc8L, 20);
210 		md5_round2(&A, B, C, D, X9, 0x21e1cde6L, 5);
211 		md5_round2(&D, A, B, C, X14, 0xc33707d6L, 9);
212 		md5_round2(&C, D, A, B, X3, 0xf4d50d87L, 14);
213 		md5_round2(&B, C, D, A, X8, 0x455a14edL, 20);
214 		md5_round2(&A, B, C, D, X13, 0xa9e3e905L, 5);
215 		md5_round2(&D, A, B, C, X2, 0xfcefa3f8L, 9);
216 		md5_round2(&C, D, A, B, X7, 0x676f02d9L, 14);
217 		md5_round2(&B, C, D, A, X12, 0x8d2a4c8aL, 20);
218 
219 		md5_round3(&A, B, C, D, X5, 0xfffa3942L, 4);
220 		md5_round3(&D, A, B, C, X8, 0x8771f681L, 11);
221 		md5_round3(&C, D, A, B, X11, 0x6d9d6122L, 16);
222 		md5_round3(&B, C, D, A, X14, 0xfde5380cL, 23);
223 		md5_round3(&A, B, C, D, X1, 0xa4beea44L, 4);
224 		md5_round3(&D, A, B, C, X4, 0x4bdecfa9L, 11);
225 		md5_round3(&C, D, A, B, X7, 0xf6bb4b60L, 16);
226 		md5_round3(&B, C, D, A, X10, 0xbebfbc70L, 23);
227 		md5_round3(&A, B, C, D, X13, 0x289b7ec6L, 4);
228 		md5_round3(&D, A, B, C, X0, 0xeaa127faL, 11);
229 		md5_round3(&C, D, A, B, X3, 0xd4ef3085L, 16);
230 		md5_round3(&B, C, D, A, X6, 0x04881d05L, 23);
231 		md5_round3(&A, B, C, D, X9, 0xd9d4d039L, 4);
232 		md5_round3(&D, A, B, C, X12, 0xe6db99e5L, 11);
233 		md5_round3(&C, D, A, B, X15, 0x1fa27cf8L, 16);
234 		md5_round3(&B, C, D, A, X2, 0xc4ac5665L, 23);
235 
236 		md5_round4(&A, B, C, D, X0, 0xf4292244L, 6);
237 		md5_round4(&D, A, B, C, X7, 0x432aff97L, 10);
238 		md5_round4(&C, D, A, B, X14, 0xab9423a7L, 15);
239 		md5_round4(&B, C, D, A, X5, 0xfc93a039L, 21);
240 		md5_round4(&A, B, C, D, X12, 0x655b59c3L, 6);
241 		md5_round4(&D, A, B, C, X3, 0x8f0ccc92L, 10);
242 		md5_round4(&C, D, A, B, X10, 0xffeff47dL, 15);
243 		md5_round4(&B, C, D, A, X1, 0x85845dd1L, 21);
244 		md5_round4(&A, B, C, D, X8, 0x6fa87e4fL, 6);
245 		md5_round4(&D, A, B, C, X15, 0xfe2ce6e0L, 10);
246 		md5_round4(&C, D, A, B, X6, 0xa3014314L, 15);
247 		md5_round4(&B, C, D, A, X13, 0x4e0811a1L, 21);
248 		md5_round4(&A, B, C, D, X4, 0xf7537e82L, 6);
249 		md5_round4(&D, A, B, C, X11, 0xbd3af235L, 10);
250 		md5_round4(&C, D, A, B, X2, 0x2ad7d2bbL, 15);
251 		md5_round4(&B, C, D, A, X9, 0xeb86d391L, 21);
252 
253 		c->A += A;
254 		c->B += B;
255 		c->C += C;
256 		c->D += D;
257 	}
258 }
259 #endif
260 
261 int
MD5_Init(MD5_CTX * c)262 MD5_Init(MD5_CTX *c)
263 {
264 	memset(c, 0, sizeof(*c));
265 
266 	c->A = 0x67452301UL;
267 	c->B = 0xefcdab89UL;
268 	c->C = 0x98badcfeUL;
269 	c->D = 0x10325476UL;
270 
271 	return 1;
272 }
273 LCRYPTO_ALIAS(MD5_Init);
274 
275 int
MD5_Update(MD5_CTX * c,const void * data_,size_t len)276 MD5_Update(MD5_CTX *c, const void *data_, size_t len)
277 {
278 	const unsigned char *data = data_;
279 	unsigned char *p;
280 	size_t n;
281 
282 	if (len == 0)
283 		return 1;
284 
285 	/* Update message bit counter. */
286 	crypto_add_u32dw_u64(&c->Nh, &c->Nl, (uint64_t)len << 3);
287 
288 	n = c->num;
289 	if (n != 0) {
290 		p = (unsigned char *)c->data;
291 
292 		if (len >= MD5_CBLOCK || len + n >= MD5_CBLOCK) {
293 			memcpy(p + n, data, MD5_CBLOCK - n);
294 			md5_block_data_order(c, p, 1);
295 			n = MD5_CBLOCK - n;
296 			data += n;
297 			len -= n;
298 			c->num = 0;
299 			memset(p, 0, MD5_CBLOCK);	/* keep it zeroed */
300 		} else {
301 			memcpy(p + n, data, len);
302 			c->num += (unsigned int)len;
303 			return 1;
304 		}
305 	}
306 
307 	n = len/MD5_CBLOCK;
308 	if (n > 0) {
309 		md5_block_data_order(c, data, n);
310 		n *= MD5_CBLOCK;
311 		data += n;
312 		len -= n;
313 	}
314 
315 	if (len != 0) {
316 		p = (unsigned char *)c->data;
317 		c->num = (unsigned int)len;
318 		memcpy(p, data, len);
319 	}
320 	return 1;
321 }
322 LCRYPTO_ALIAS(MD5_Update);
323 
324 void
MD5_Transform(MD5_CTX * c,const unsigned char * data)325 MD5_Transform(MD5_CTX *c, const unsigned char *data)
326 {
327 	md5_block_data_order(c, data, 1);
328 }
329 LCRYPTO_ALIAS(MD5_Transform);
330 
331 int
MD5_Final(unsigned char * md,MD5_CTX * c)332 MD5_Final(unsigned char *md, MD5_CTX *c)
333 {
334 	unsigned char *p = (unsigned char *)c->data;
335 	size_t n = c->num;
336 
337 	p[n] = 0x80; /* there is always room for one */
338 	n++;
339 
340 	if (n > (MD5_CBLOCK - 8)) {
341 		memset(p + n, 0, MD5_CBLOCK - n);
342 		n = 0;
343 		md5_block_data_order(c, p, 1);
344 	}
345 
346 	memset(p + n, 0, MD5_CBLOCK - 8 - n);
347 	c->data[MD5_LBLOCK - 2] = htole32(c->Nl);
348 	c->data[MD5_LBLOCK - 1] = htole32(c->Nh);
349 
350 	md5_block_data_order(c, p, 1);
351 	c->num = 0;
352 	memset(p, 0, MD5_CBLOCK);
353 
354 	crypto_store_htole32(&md[0 * 4], c->A);
355 	crypto_store_htole32(&md[1 * 4], c->B);
356 	crypto_store_htole32(&md[2 * 4], c->C);
357 	crypto_store_htole32(&md[3 * 4], c->D);
358 
359 	return 1;
360 }
361 LCRYPTO_ALIAS(MD5_Final);
362 
363 unsigned char *
MD5(const unsigned char * d,size_t n,unsigned char * md)364 MD5(const unsigned char *d, size_t n, unsigned char *md)
365 {
366 	MD5_CTX c;
367 
368 	if (!MD5_Init(&c))
369 		return NULL;
370 	MD5_Update(&c, d, n);
371 	MD5_Final(md, &c);
372 	explicit_bzero(&c, sizeof(c));
373 	return (md);
374 }
375 LCRYPTO_ALIAS(MD5);
376