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