1 /*
2  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /** @file
27  *
28  * MD5 algorithm
29  *
30  */
31 
32 #include <stdint.h>
33 #include <string.h>
34 #include <byteswap.h>
35 #include <assert.h>
36 #include <ipxe/rotate.h>
37 #include <ipxe/crypto.h>
38 #include <ipxe/asn1.h>
39 #include <ipxe/md5.h>
40 
41 /** MD5 variables */
42 struct md5_variables {
43 	/* This layout matches that of struct md5_digest_data,
44 	 * allowing for efficient endianness-conversion,
45 	 */
46 	uint32_t a;
47 	uint32_t b;
48 	uint32_t c;
49 	uint32_t d;
50 	uint32_t w[16];
51 } __attribute__ (( packed ));
52 
53 /** MD5 constants */
54 static const uint32_t k[64] = {
55 	0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a,
56 	0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
57 	0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340,
58 	0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
59 	0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8,
60 	0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
61 	0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa,
62 	0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
63 	0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92,
64 	0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
65 	0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
66 };
67 
68 /** MD5 shift amounts */
69 static const uint8_t r[4][4] = {
70 	{  7, 12, 17, 22 },
71 	{  5,  9, 14, 20 },
72 	{  4, 11, 16, 23 },
73 	{  6, 10, 15, 21 },
74 };
75 
76 /**
77  * f(b,c,d) for steps 0 to 15
78  *
79  * @v v		MD5 variables
80  * @ret f	f(b,c,d)
81  */
md5_f_0_15(struct md5_variables * v)82 static uint32_t md5_f_0_15 ( struct md5_variables *v ) {
83 	return ( v->d ^ ( v->b & ( v->c ^ v->d ) ) );
84 }
85 
86 /**
87  * f(b,c,d) for steps 16 to 31
88  *
89  * @v v		MD5 variables
90  * @ret f	f(b,c,d)
91  */
md5_f_16_31(struct md5_variables * v)92 static uint32_t md5_f_16_31 ( struct md5_variables *v ) {
93 	return ( v->c ^ ( v->d & ( v->b ^ v->c ) ) );
94 }
95 
96 /**
97  * f(b,c,d) for steps 32 to 47
98  *
99  * @v v		MD5 variables
100  * @ret f	f(b,c,d)
101  */
md5_f_32_47(struct md5_variables * v)102 static uint32_t md5_f_32_47 ( struct md5_variables *v ) {
103 	return ( v->b ^ v->c ^ v->d );
104 }
105 
106 /**
107  * f(b,c,d) for steps 48 to 63
108  *
109  * @v v		MD5 variables
110  * @ret f	f(b,c,d)
111  */
md5_f_48_63(struct md5_variables * v)112 static uint32_t md5_f_48_63 ( struct md5_variables *v ) {
113 	return ( v->c ^ ( v->b | (~v->d) ) );
114 }
115 
116 /** An MD5 step function */
117 struct md5_step {
118 	/**
119 	 * Calculate f(b,c,d)
120 	 *
121 	 * @v v		MD5 variables
122 	 * @ret f	f(b,c,d)
123 	 */
124 	uint32_t ( * f ) ( struct md5_variables *v );
125 	/** Coefficient of i in g=ni+m */
126 	uint8_t coefficient;
127 	/** Constant term in g=ni+m */
128 	uint8_t constant;
129 };
130 
131 /** MD5 steps */
132 static struct md5_step md5_steps[4] = {
133 	/** 0 to 15 */
134 	{ .f = md5_f_0_15,	.coefficient = 1,	.constant = 0 },
135 	/** 16 to 31 */
136 	{ .f = md5_f_16_31,	.coefficient = 5,	.constant = 1 },
137 	/** 32 to 47 */
138 	{ .f = md5_f_32_47,	.coefficient = 3,	.constant = 5 },
139 	/** 48 to 63 */
140 	{ .f = md5_f_48_63,	.coefficient = 7,	.constant = 0 },
141 };
142 
143 /**
144  * Initialise MD5 algorithm
145  *
146  * @v ctx		MD5 context
147  */
md5_init(void * ctx)148 static void md5_init ( void *ctx ) {
149 	struct md5_context *context = ctx;
150 
151 	context->ddd.dd.digest.h[0] = cpu_to_le32 ( 0x67452301 );
152 	context->ddd.dd.digest.h[1] = cpu_to_le32 ( 0xefcdab89 );
153 	context->ddd.dd.digest.h[2] = cpu_to_le32 ( 0x98badcfe );
154 	context->ddd.dd.digest.h[3] = cpu_to_le32 ( 0x10325476 );
155 	context->len = 0;
156 }
157 
158 /**
159  * Calculate MD5 digest of accumulated data
160  *
161  * @v context		MD5 context
162  */
md5_digest(struct md5_context * context)163 static void md5_digest ( struct md5_context *context ) {
164         union {
165 		union md5_digest_data_dwords ddd;
166 		struct md5_variables v;
167 	} u;
168 	uint32_t *a = &u.v.a;
169 	uint32_t *b = &u.v.b;
170 	uint32_t *c = &u.v.c;
171 	uint32_t *d = &u.v.d;
172 	uint32_t *w = u.v.w;
173 	uint32_t f;
174 	uint32_t g;
175 	uint32_t temp;
176 	struct md5_step *step;
177 	unsigned int round;
178 	unsigned int i;
179 
180 	/* Sanity checks */
181 	assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
182 	linker_assert ( &u.ddd.dd.digest.h[0] == a, md5_bad_layout );
183 	linker_assert ( &u.ddd.dd.digest.h[1] == b, md5_bad_layout );
184 	linker_assert ( &u.ddd.dd.digest.h[2] == c, md5_bad_layout );
185 	linker_assert ( &u.ddd.dd.digest.h[3] == d, md5_bad_layout );
186 	linker_assert ( &u.ddd.dd.data.dword[0] == w, md5_bad_layout );
187 
188 	DBGC ( context, "MD5 digesting:\n" );
189 	DBGC_HDA ( context, 0, &context->ddd.dd.digest,
190 		   sizeof ( context->ddd.dd.digest ) );
191 	DBGC_HDA ( context, context->len, &context->ddd.dd.data,
192 		   sizeof ( context->ddd.dd.data ) );
193 
194 	/* Convert h[0..3] to host-endian, and initialise a, b, c, d,
195 	 * and w[0..15]
196 	 */
197 	for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
198 			    sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
199 		le32_to_cpus ( &context->ddd.dword[i] );
200 		u.ddd.dword[i] = context->ddd.dword[i];
201 	}
202 
203 	/* Main loop */
204 	for ( i = 0 ; i < 64 ; i++ ) {
205 		round = ( i / 16 );
206 		step = &md5_steps[round];
207 		f = step->f ( &u.v );
208 		g = ( ( ( step->coefficient * i ) + step->constant ) % 16 );
209 		temp = *d;
210 		*d = *c;
211 		*c = *b;
212 		*b = ( *b + rol32 ( ( *a + f + k[i] + w[g] ),
213 				    r[round][ i % 4 ] ) );
214 		*a = temp;
215 		DBGC2 ( context, "%2d : %08x %08x %08x %08x\n",
216 			i, *a, *b, *c, *d );
217 	}
218 
219 	/* Add chunk to hash and convert back to little-endian */
220 	for ( i = 0 ; i < 4 ; i++ ) {
221 		context->ddd.dd.digest.h[i] =
222 			cpu_to_le32 ( context->ddd.dd.digest.h[i] +
223 				      u.ddd.dd.digest.h[i] );
224 	}
225 
226 	DBGC ( context, "MD5 digested:\n" );
227 	DBGC_HDA ( context, 0, &context->ddd.dd.digest,
228 		   sizeof ( context->ddd.dd.digest ) );
229 }
230 
231 /**
232  * Accumulate data with MD5 algorithm
233  *
234  * @v ctx		MD5 context
235  * @v data		Data
236  * @v len		Length of data
237  */
md5_update(void * ctx,const void * data,size_t len)238 static void md5_update ( void *ctx, const void *data, size_t len ) {
239 	struct md5_context *context = ctx;
240 	const uint8_t *byte = data;
241 	size_t offset;
242 
243 	/* Accumulate data a byte at a time, performing the digest
244 	 * whenever we fill the data buffer
245 	 */
246 	while ( len-- ) {
247 		offset = ( context->len % sizeof ( context->ddd.dd.data ) );
248 		context->ddd.dd.data.byte[offset] = *(byte++);
249 		context->len++;
250 		if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
251 			md5_digest ( context );
252 	}
253 }
254 
255 /**
256  * Generate MD5 digest
257  *
258  * @v ctx		MD5 context
259  * @v out		Output buffer
260  */
md5_final(void * ctx,void * out)261 static void md5_final ( void *ctx, void *out ) {
262 	struct md5_context *context = ctx;
263 	uint64_t len_bits;
264 	uint8_t pad;
265 
266 	/* Record length before pre-processing */
267 	len_bits = cpu_to_le64 ( ( ( uint64_t ) context->len ) * 8 );
268 
269 	/* Pad with a single "1" bit followed by as many "0" bits as required */
270 	pad = 0x80;
271 	do {
272 		md5_update ( ctx, &pad, sizeof ( pad ) );
273 		pad = 0x00;
274 	} while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
275 		  offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
276 
277 	/* Append length (in bits) */
278 	md5_update ( ctx, &len_bits, sizeof ( len_bits ) );
279 	assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
280 
281 	/* Copy out final digest */
282 	memcpy ( out, &context->ddd.dd.digest,
283 		 sizeof ( context->ddd.dd.digest ) );
284 }
285 
286 /** MD5 algorithm */
287 struct digest_algorithm md5_algorithm = {
288 	.name		= "md5",
289 	.ctxsize	= sizeof ( struct md5_context ),
290 	.blocksize	= sizeof ( union md5_block ),
291 	.digestsize	= sizeof ( struct md5_digest ),
292 	.init		= md5_init,
293 	.update		= md5_update,
294 	.final		= md5_final,
295 };
296 
297 /** "md5" object identifier */
298 static uint8_t oid_md5[] = { ASN1_OID_MD5 };
299 
300 /** "md5" OID-identified algorithm */
301 struct asn1_algorithm oid_md5_algorithm __asn1_algorithm = {
302 	.name = "md5",
303 	.digest = &md5_algorithm,
304 	.oid = ASN1_OID_CURSOR ( oid_md5 ),
305 };
306