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