Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / crypto / sha256.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-256 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/sha256.h>
36
37 /** SHA-256 variables */
38 struct sha256_variables {
39         /* This layout matches that of struct sha256_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 f;
48         uint32_t g;
49         uint32_t h;
50         uint32_t w[64];
51 } __attribute__ (( packed ));
52
53 /** SHA-256 constants */
54 static const uint32_t k[64] = {
55         0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1,
56         0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
57         0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786,
58         0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
59         0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
60         0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
61         0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
62         0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
63         0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
64         0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
65         0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
66 };
67
68 /**
69  * Initialise SHA-256 algorithm
70  *
71  * @v ctx               SHA-256 context
72  */
73 static void sha256_init ( void *ctx ) {
74         struct sha256_context *context = ctx;
75
76         context->ddd.dd.digest.h[0] = cpu_to_be32 ( 0x6a09e667 );
77         context->ddd.dd.digest.h[1] = cpu_to_be32 ( 0xbb67ae85 );
78         context->ddd.dd.digest.h[2] = cpu_to_be32 ( 0x3c6ef372 );
79         context->ddd.dd.digest.h[3] = cpu_to_be32 ( 0xa54ff53a );
80         context->ddd.dd.digest.h[4] = cpu_to_be32 ( 0x510e527f );
81         context->ddd.dd.digest.h[5] = cpu_to_be32 ( 0x9b05688c );
82         context->ddd.dd.digest.h[6] = cpu_to_be32 ( 0x1f83d9ab );
83         context->ddd.dd.digest.h[7] = cpu_to_be32 ( 0x5be0cd19 );
84         context->len = 0;
85 }
86
87 /**
88  * Calculate SHA-256 digest of accumulated data
89  *
90  * @v context           SHA-256 context
91  */
92 static void sha256_digest ( struct sha256_context *context ) {
93         union {
94                 union sha256_digest_data_dwords ddd;
95                 struct sha256_variables v;
96         } u;
97         uint32_t *a = &u.v.a;
98         uint32_t *b = &u.v.b;
99         uint32_t *c = &u.v.c;
100         uint32_t *d = &u.v.d;
101         uint32_t *e = &u.v.e;
102         uint32_t *f = &u.v.f;
103         uint32_t *g = &u.v.g;
104         uint32_t *h = &u.v.h;
105         uint32_t *w = u.v.w;
106         uint32_t s0;
107         uint32_t s1;
108         uint32_t maj;
109         uint32_t t1;
110         uint32_t t2;
111         uint32_t ch;
112         unsigned int i;
113
114         /* Sanity checks */
115         assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
116         linker_assert ( &u.ddd.dd.digest.h[0] == a, sha256_bad_layout );
117         linker_assert ( &u.ddd.dd.digest.h[1] == b, sha256_bad_layout );
118         linker_assert ( &u.ddd.dd.digest.h[2] == c, sha256_bad_layout );
119         linker_assert ( &u.ddd.dd.digest.h[3] == d, sha256_bad_layout );
120         linker_assert ( &u.ddd.dd.digest.h[4] == e, sha256_bad_layout );
121         linker_assert ( &u.ddd.dd.digest.h[5] == f, sha256_bad_layout );
122         linker_assert ( &u.ddd.dd.digest.h[6] == g, sha256_bad_layout );
123         linker_assert ( &u.ddd.dd.digest.h[7] == h, sha256_bad_layout );
124         linker_assert ( &u.ddd.dd.data.dword[0] == w, sha256_bad_layout );
125
126         DBGC ( context, "SHA256 digesting:\n" );
127         DBGC_HDA ( context, 0, &context->ddd.dd.digest,
128                    sizeof ( context->ddd.dd.digest ) );
129         DBGC_HDA ( context, context->len, &context->ddd.dd.data,
130                    sizeof ( context->ddd.dd.data ) );
131
132         /* Convert h[0..7] to host-endian, and initialise a, b, c, d,
133          * e, f, g, h, and w[0..15]
134          */
135         for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
136                             sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
137                 be32_to_cpus ( &context->ddd.dword[i] );
138                 u.ddd.dword[i] = context->ddd.dword[i];
139         }
140
141         /* Initialise w[16..63] */
142         for ( i = 16 ; i < 64 ; i++ ) {
143                 s0 = ( ror32 ( w[i-15], 7 ) ^ ror32 ( w[i-15], 18 ) ^
144                        ( w[i-15] >> 3 ) );
145                 s1 = ( ror32 ( w[i-2], 17 ) ^ ror32 ( w[i-2], 19 ) ^
146                        ( w[i-2] >> 10 ) );
147                 w[i] = ( w[i-16] + s0 + w[i-7] + s1 );
148         }
149
150         /* Main loop */
151         for ( i = 0 ; i < 64 ; i++ ) {
152                 s0 = ( ror32 ( *a, 2 ) ^ ror32 ( *a, 13 ) ^ ror32 ( *a, 22 ) );
153                 maj = ( ( *a & *b ) ^ ( *a & *c ) ^ ( *b & *c ) );
154                 t2 = ( s0 + maj );
155                 s1 = ( ror32 ( *e, 6 ) ^ ror32 ( *e, 11 ) ^ ror32 ( *e, 25 ) );
156                 ch = ( ( *e & *f ) ^ ( (~*e) & *g ) );
157                 t1 = ( *h + s1 + ch + k[i] + w[i] );
158                 *h = *g;
159                 *g = *f;
160                 *f = *e;
161                 *e = ( *d + t1 );
162                 *d = *c;
163                 *c = *b;
164                 *b = *a;
165                 *a = ( t1 + t2 );
166                 DBGC2 ( context, "%2d : %08x %08x %08x %08x %08x %08x %08x "
167                         "%08x\n", i, *a, *b, *c, *d, *e, *f, *g, *h );
168         }
169
170         /* Add chunk to hash and convert back to big-endian */
171         for ( i = 0 ; i < 8 ; i++ ) {
172                 context->ddd.dd.digest.h[i] =
173                         cpu_to_be32 ( context->ddd.dd.digest.h[i] +
174                                       u.ddd.dd.digest.h[i] );
175         }
176
177         DBGC ( context, "SHA256 digested:\n" );
178         DBGC_HDA ( context, 0, &context->ddd.dd.digest,
179                    sizeof ( context->ddd.dd.digest ) );
180 }
181
182 /**
183  * Accumulate data with SHA-256 algorithm
184  *
185  * @v ctx               SHA-256 context
186  * @v data              Data
187  * @v len               Length of data
188  */
189 static void sha256_update ( void *ctx, const void *data, size_t len ) {
190         struct sha256_context *context = ctx;
191         const uint8_t *byte = data;
192         size_t offset;
193
194         /* Accumulate data a byte at a time, performing the digest
195          * whenever we fill the data buffer
196          */
197         while ( len-- ) {
198                 offset = ( context->len % sizeof ( context->ddd.dd.data ) );
199                 context->ddd.dd.data.byte[offset] = *(byte++);
200                 context->len++;
201                 if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
202                         sha256_digest ( context );
203         }
204 }
205
206 /**
207  * Generate SHA-256 digest
208  *
209  * @v ctx               SHA-256 context
210  * @v out               Output buffer
211  */
212 static void sha256_final ( void *ctx, void *out ) {
213         struct sha256_context *context = ctx;
214         uint64_t len_bits;
215         uint8_t pad;
216
217         /* Record length before pre-processing */
218         len_bits = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );
219
220         /* Pad with a single "1" bit followed by as many "0" bits as required */
221         pad = 0x80;
222         do {
223                 sha256_update ( ctx, &pad, sizeof ( pad ) );
224                 pad = 0x00;
225         } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
226                   offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
227
228         /* Append length (in bits) */
229         sha256_update ( ctx, &len_bits, sizeof ( len_bits ) );
230         assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
231
232         /* Copy out final digest */
233         memcpy ( out, &context->ddd.dd.digest,
234                  sizeof ( context->ddd.dd.digest ) );
235 }
236
237 /** SHA-256 algorithm */
238 struct digest_algorithm sha256_algorithm = {
239         .name           = "sha256",
240         .ctxsize        = sizeof ( struct sha256_context ),
241         .blocksize      = sizeof ( union sha256_block ),
242         .digestsize     = sizeof ( struct sha256_digest ),
243         .init           = sha256_init,
244         .update         = sha256_update,
245         .final          = sha256_final,
246 };
247
248 /** "sha256" object identifier */
249 static uint8_t oid_sha256[] = { ASN1_OID_SHA256 };
250
251 /** "sha256" OID-identified algorithm */
252 struct asn1_algorithm oid_sha256_algorithm __asn1_algorithm = {
253         .name = "sha256",
254         .digest = &sha256_algorithm,
255         .oid = ASN1_OID_CURSOR ( oid_sha256 ),
256 };