Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / crypto / sha1.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  * SHA-1 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/sha1.h>
36
37 /** SHA-1 variables */
38 struct sha1_variables {
39         /* This layout matches that of struct sha1_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 e;
47         uint32_t w[80];
48 } __attribute__ (( packed ));
49
50 /**
51  * f(a,b,c,d) for steps 0 to 19
52  *
53  * @v v         SHA-1 variables
54  * @ret f       f(a,b,c,d)
55  */
56 static uint32_t sha1_f_0_19 ( struct sha1_variables *v ) {
57         return ( ( v->b & v->c ) | ( (~v->b) & v->d ) );
58 }
59
60 /**
61  * f(a,b,c,d) for steps 20 to 39 and 60 to 79
62  *
63  * @v v         SHA-1 variables
64  * @ret f       f(a,b,c,d)
65  */
66 static uint32_t sha1_f_20_39_60_79 ( struct sha1_variables *v ) {
67         return ( v->b ^ v->c ^ v->d );
68 }
69
70 /**
71  * f(a,b,c,d) for steps 40 to 59
72  *
73  * @v v         SHA-1 variables
74  * @ret f       f(a,b,c,d)
75  */
76 static uint32_t sha1_f_40_59 ( struct sha1_variables *v ) {
77         return ( ( v->b & v->c ) | ( v->b & v->d ) | ( v->c & v->d ) );
78 }
79
80 /** An SHA-1 step function */
81 struct sha1_step {
82         /**
83          * Calculate f(a,b,c,d)
84          *
85          * @v v         SHA-1 variables
86          * @ret f       f(a,b,c,d)
87          */
88         uint32_t ( * f ) ( struct sha1_variables *v );
89         /** Constant k */
90         uint32_t k;
91 };
92
93 /** SHA-1 steps */
94 static struct sha1_step sha1_steps[4] = {
95         /** 0 to 19 */
96         { .f = sha1_f_0_19,             .k = 0x5a827999 },
97         /** 20 to 39 */
98         { .f = sha1_f_20_39_60_79,      .k = 0x6ed9eba1 },
99         /** 40 to 59 */
100         { .f = sha1_f_40_59,            .k = 0x8f1bbcdc },
101         /** 60 to 79 */
102         { .f = sha1_f_20_39_60_79,      .k = 0xca62c1d6 },
103 };
104
105 /**
106  * Initialise SHA-1 algorithm
107  *
108  * @v ctx               SHA-1 context
109  */
110 static void sha1_init ( void *ctx ) {
111         struct sha1_context *context = ctx;
112
113         context->ddd.dd.digest.h[0] = cpu_to_be32 ( 0x67452301 );
114         context->ddd.dd.digest.h[1] = cpu_to_be32 ( 0xefcdab89 );
115         context->ddd.dd.digest.h[2] = cpu_to_be32 ( 0x98badcfe );
116         context->ddd.dd.digest.h[3] = cpu_to_be32 ( 0x10325476 );
117         context->ddd.dd.digest.h[4] = cpu_to_be32 ( 0xc3d2e1f0 );
118         context->len = 0;
119 }
120
121 /**
122  * Calculate SHA-1 digest of accumulated data
123  *
124  * @v context           SHA-1 context
125  */
126 static void sha1_digest ( struct sha1_context *context ) {
127         union {
128                 union sha1_digest_data_dwords ddd;
129                 struct sha1_variables v;
130         } u;
131         uint32_t *a = &u.v.a;
132         uint32_t *b = &u.v.b;
133         uint32_t *c = &u.v.c;
134         uint32_t *d = &u.v.d;
135         uint32_t *e = &u.v.e;
136         uint32_t *w = u.v.w;
137         uint32_t f;
138         uint32_t k;
139         uint32_t temp;
140         struct sha1_step *step;
141         unsigned int i;
142
143         /* Sanity checks */
144         assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
145         linker_assert ( &u.ddd.dd.digest.h[0] == a, sha1_bad_layout );
146         linker_assert ( &u.ddd.dd.digest.h[1] == b, sha1_bad_layout );
147         linker_assert ( &u.ddd.dd.digest.h[2] == c, sha1_bad_layout );
148         linker_assert ( &u.ddd.dd.digest.h[3] == d, sha1_bad_layout );
149         linker_assert ( &u.ddd.dd.digest.h[4] == e, sha1_bad_layout );
150         linker_assert ( &u.ddd.dd.data.dword[0] == w, sha1_bad_layout );
151
152         DBGC ( context, "SHA1 digesting:\n" );
153         DBGC_HDA ( context, 0, &context->ddd.dd.digest,
154                    sizeof ( context->ddd.dd.digest ) );
155         DBGC_HDA ( context, context->len, &context->ddd.dd.data,
156                    sizeof ( context->ddd.dd.data ) );
157
158         /* Convert h[0..4] to host-endian, and initialise a, b, c, d,
159          * e, and w[0..15]
160          */
161         for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
162                             sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
163                 be32_to_cpus ( &context->ddd.dword[i] );
164                 u.ddd.dword[i] = context->ddd.dword[i];
165         }
166
167         /* Initialise w[16..79] */
168         for ( i = 16 ; i < 80 ; i++ )
169                 w[i] = rol32 ( ( w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16] ), 1 );
170
171         /* Main loop */
172         for ( i = 0 ; i < 80 ; i++ ) {
173                 step = &sha1_steps[ i / 20 ];
174                 f = step->f ( &u.v );
175                 k = step->k;
176                 temp = ( rol32 ( *a, 5 ) + f + *e + k + w[i] );
177                 *e = *d;
178                 *d = *c;
179                 *c = rol32 ( *b, 30 );
180                 *b = *a;
181                 *a = temp;
182                 DBGC2 ( context, "%2d : %08x %08x %08x %08x %08x\n",
183                         i, *a, *b, *c, *d, *e );
184         }
185
186         /* Add chunk to hash and convert back to big-endian */
187         for ( i = 0 ; i < 5 ; i++ ) {
188                 context->ddd.dd.digest.h[i] =
189                         cpu_to_be32 ( context->ddd.dd.digest.h[i] +
190                                       u.ddd.dd.digest.h[i] );
191         }
192
193         DBGC ( context, "SHA1 digested:\n" );
194         DBGC_HDA ( context, 0, &context->ddd.dd.digest,
195                    sizeof ( context->ddd.dd.digest ) );
196 }
197
198 /**
199  * Accumulate data with SHA-1 algorithm
200  *
201  * @v ctx               SHA-1 context
202  * @v data              Data
203  * @v len               Length of data
204  */
205 static void sha1_update ( void *ctx, const void *data, size_t len ) {
206         struct sha1_context *context = ctx;
207         const uint8_t *byte = data;
208         size_t offset;
209
210         /* Accumulate data a byte at a time, performing the digest
211          * whenever we fill the data buffer
212          */
213         while ( len-- ) {
214                 offset = ( context->len % sizeof ( context->ddd.dd.data ) );
215                 context->ddd.dd.data.byte[offset] = *(byte++);
216                 context->len++;
217                 if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
218                         sha1_digest ( context );
219         }
220 }
221
222 /**
223  * Generate SHA-1 digest
224  *
225  * @v ctx               SHA-1 context
226  * @v out               Output buffer
227  */
228 static void sha1_final ( void *ctx, void *out ) {
229         struct sha1_context *context = ctx;
230         uint64_t len_bits;
231         uint8_t pad;
232
233         /* Record length before pre-processing */
234         len_bits = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );
235
236         /* Pad with a single "1" bit followed by as many "0" bits as required */
237         pad = 0x80;
238         do {
239                 sha1_update ( ctx, &pad, sizeof ( pad ) );
240                 pad = 0x00;
241         } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
242                   offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
243
244         /* Append length (in bits) */
245         sha1_update ( ctx, &len_bits, sizeof ( len_bits ) );
246         assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
247
248         /* Copy out final digest */
249         memcpy ( out, &context->ddd.dd.digest,
250                  sizeof ( context->ddd.dd.digest ) );
251 }
252
253 /** SHA-1 algorithm */
254 struct digest_algorithm sha1_algorithm = {
255         .name           = "sha1",
256         .ctxsize        = sizeof ( struct sha1_context ),
257         .blocksize      = sizeof ( union sha1_block ),
258         .digestsize     = sizeof ( struct sha1_digest ),
259         .init           = sha1_init,
260         .update         = sha1_update,
261         .final          = sha1_final,
262 };
263
264 /** "sha1" object identifier */
265 static uint8_t oid_sha1[] = { ASN1_OID_SHA1 };
266
267 /** "sha1" OID-identified algorithm */
268 struct asn1_algorithm oid_sha1_algorithm __asn1_algorithm = {
269         .name = "sha1",
270         .digest = &sha1_algorithm,
271         .oid = ASN1_OID_CURSOR ( oid_sha1 ),
272 };