2 * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
36 #include <ipxe/rotate.h>
37 #include <ipxe/crypto.h>
38 #include <ipxe/asn1.h>
39 #include <ipxe/sha1.h>
41 /** SHA-1 variables */
42 struct sha1_variables {
43 /* This layout matches that of struct sha1_digest_data,
44 * allowing for efficient endianness-conversion,
52 } __attribute__ (( packed ));
55 * f(a,b,c,d) for steps 0 to 19
57 * @v v SHA-1 variables
60 static uint32_t sha1_f_0_19 ( struct sha1_variables *v ) {
61 return ( ( v->b & v->c ) | ( (~v->b) & v->d ) );
65 * f(a,b,c,d) for steps 20 to 39 and 60 to 79
67 * @v v SHA-1 variables
70 static uint32_t sha1_f_20_39_60_79 ( struct sha1_variables *v ) {
71 return ( v->b ^ v->c ^ v->d );
75 * f(a,b,c,d) for steps 40 to 59
77 * @v v SHA-1 variables
80 static uint32_t sha1_f_40_59 ( struct sha1_variables *v ) {
81 return ( ( v->b & v->c ) | ( v->b & v->d ) | ( v->c & v->d ) );
84 /** An SHA-1 step function */
87 * Calculate f(a,b,c,d)
89 * @v v SHA-1 variables
92 uint32_t ( * f ) ( struct sha1_variables *v );
98 static struct sha1_step sha1_steps[4] = {
100 { .f = sha1_f_0_19, .k = 0x5a827999 },
102 { .f = sha1_f_20_39_60_79, .k = 0x6ed9eba1 },
104 { .f = sha1_f_40_59, .k = 0x8f1bbcdc },
106 { .f = sha1_f_20_39_60_79, .k = 0xca62c1d6 },
110 * Initialise SHA-1 algorithm
112 * @v ctx SHA-1 context
114 static void sha1_init ( void *ctx ) {
115 struct sha1_context *context = ctx;
117 context->ddd.dd.digest.h[0] = cpu_to_be32 ( 0x67452301 );
118 context->ddd.dd.digest.h[1] = cpu_to_be32 ( 0xefcdab89 );
119 context->ddd.dd.digest.h[2] = cpu_to_be32 ( 0x98badcfe );
120 context->ddd.dd.digest.h[3] = cpu_to_be32 ( 0x10325476 );
121 context->ddd.dd.digest.h[4] = cpu_to_be32 ( 0xc3d2e1f0 );
126 * Calculate SHA-1 digest of accumulated data
128 * @v context SHA-1 context
130 static void sha1_digest ( struct sha1_context *context ) {
132 union sha1_digest_data_dwords ddd;
133 struct sha1_variables v;
135 uint32_t *a = &u.v.a;
136 uint32_t *b = &u.v.b;
137 uint32_t *c = &u.v.c;
138 uint32_t *d = &u.v.d;
139 uint32_t *e = &u.v.e;
144 struct sha1_step *step;
148 assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
149 linker_assert ( &u.ddd.dd.digest.h[0] == a, sha1_bad_layout );
150 linker_assert ( &u.ddd.dd.digest.h[1] == b, sha1_bad_layout );
151 linker_assert ( &u.ddd.dd.digest.h[2] == c, sha1_bad_layout );
152 linker_assert ( &u.ddd.dd.digest.h[3] == d, sha1_bad_layout );
153 linker_assert ( &u.ddd.dd.digest.h[4] == e, sha1_bad_layout );
154 linker_assert ( &u.ddd.dd.data.dword[0] == w, sha1_bad_layout );
156 DBGC ( context, "SHA1 digesting:\n" );
157 DBGC_HDA ( context, 0, &context->ddd.dd.digest,
158 sizeof ( context->ddd.dd.digest ) );
159 DBGC_HDA ( context, context->len, &context->ddd.dd.data,
160 sizeof ( context->ddd.dd.data ) );
162 /* Convert h[0..4] to host-endian, and initialise a, b, c, d,
165 for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
166 sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
167 be32_to_cpus ( &context->ddd.dword[i] );
168 u.ddd.dword[i] = context->ddd.dword[i];
171 /* Initialise w[16..79] */
172 for ( i = 16 ; i < 80 ; i++ )
173 w[i] = rol32 ( ( w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16] ), 1 );
176 for ( i = 0 ; i < 80 ; i++ ) {
177 step = &sha1_steps[ i / 20 ];
178 f = step->f ( &u.v );
180 temp = ( rol32 ( *a, 5 ) + f + *e + k + w[i] );
183 *c = rol32 ( *b, 30 );
186 DBGC2 ( context, "%2d : %08x %08x %08x %08x %08x\n",
187 i, *a, *b, *c, *d, *e );
190 /* Add chunk to hash and convert back to big-endian */
191 for ( i = 0 ; i < 5 ; i++ ) {
192 context->ddd.dd.digest.h[i] =
193 cpu_to_be32 ( context->ddd.dd.digest.h[i] +
194 u.ddd.dd.digest.h[i] );
197 DBGC ( context, "SHA1 digested:\n" );
198 DBGC_HDA ( context, 0, &context->ddd.dd.digest,
199 sizeof ( context->ddd.dd.digest ) );
203 * Accumulate data with SHA-1 algorithm
205 * @v ctx SHA-1 context
207 * @v len Length of data
209 static void sha1_update ( void *ctx, const void *data, size_t len ) {
210 struct sha1_context *context = ctx;
211 const uint8_t *byte = data;
214 /* Accumulate data a byte at a time, performing the digest
215 * whenever we fill the data buffer
218 offset = ( context->len % sizeof ( context->ddd.dd.data ) );
219 context->ddd.dd.data.byte[offset] = *(byte++);
221 if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
222 sha1_digest ( context );
227 * Generate SHA-1 digest
229 * @v ctx SHA-1 context
230 * @v out Output buffer
232 static void sha1_final ( void *ctx, void *out ) {
233 struct sha1_context *context = ctx;
237 /* Record length before pre-processing */
238 len_bits = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );
240 /* Pad with a single "1" bit followed by as many "0" bits as required */
243 sha1_update ( ctx, &pad, sizeof ( pad ) );
245 } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
246 offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
248 /* Append length (in bits) */
249 sha1_update ( ctx, &len_bits, sizeof ( len_bits ) );
250 assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
252 /* Copy out final digest */
253 memcpy ( out, &context->ddd.dd.digest,
254 sizeof ( context->ddd.dd.digest ) );
257 /** SHA-1 algorithm */
258 struct digest_algorithm sha1_algorithm = {
260 .ctxsize = sizeof ( struct sha1_context ),
261 .blocksize = sizeof ( union sha1_block ),
262 .digestsize = sizeof ( struct sha1_digest ),
264 .update = sha1_update,
268 /** "sha1" object identifier */
269 static uint8_t oid_sha1[] = { ASN1_OID_SHA1 };
271 /** "sha1" OID-identified algorithm */
272 struct asn1_algorithm oid_sha1_algorithm __asn1_algorithm = {
274 .digest = &sha1_algorithm,
275 .oid = ASN1_OID_CURSOR ( oid_sha1 ),