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
20 FILE_LICENCE ( GPL2_OR_LATER );
32 #include <ipxe/rotate.h>
33 #include <ipxe/crypto.h>
34 #include <ipxe/asn1.h>
35 #include <ipxe/sha1.h>
37 /** SHA-1 variables */
38 struct sha1_variables {
39 /* This layout matches that of struct sha1_digest_data,
40 * allowing for efficient endianness-conversion,
48 } __attribute__ (( packed ));
51 * f(a,b,c,d) for steps 0 to 19
53 * @v v SHA-1 variables
56 static uint32_t sha1_f_0_19 ( struct sha1_variables *v ) {
57 return ( ( v->b & v->c ) | ( (~v->b) & v->d ) );
61 * f(a,b,c,d) for steps 20 to 39 and 60 to 79
63 * @v v SHA-1 variables
66 static uint32_t sha1_f_20_39_60_79 ( struct sha1_variables *v ) {
67 return ( v->b ^ v->c ^ v->d );
71 * f(a,b,c,d) for steps 40 to 59
73 * @v v SHA-1 variables
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 ) );
80 /** An SHA-1 step function */
83 * Calculate f(a,b,c,d)
85 * @v v SHA-1 variables
88 uint32_t ( * f ) ( struct sha1_variables *v );
94 static struct sha1_step sha1_steps[4] = {
96 { .f = sha1_f_0_19, .k = 0x5a827999 },
98 { .f = sha1_f_20_39_60_79, .k = 0x6ed9eba1 },
100 { .f = sha1_f_40_59, .k = 0x8f1bbcdc },
102 { .f = sha1_f_20_39_60_79, .k = 0xca62c1d6 },
106 * Initialise SHA-1 algorithm
108 * @v ctx SHA-1 context
110 static void sha1_init ( void *ctx ) {
111 struct sha1_context *context = ctx;
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 );
122 * Calculate SHA-1 digest of accumulated data
124 * @v context SHA-1 context
126 static void sha1_digest ( struct sha1_context *context ) {
128 union sha1_digest_data_dwords ddd;
129 struct sha1_variables v;
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;
140 struct sha1_step *step;
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 );
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 ) );
158 /* Convert h[0..4] to host-endian, and initialise a, b, c, d,
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];
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 );
172 for ( i = 0 ; i < 80 ; i++ ) {
173 step = &sha1_steps[ i / 20 ];
174 f = step->f ( &u.v );
176 temp = ( rol32 ( *a, 5 ) + f + *e + k + w[i] );
179 *c = rol32 ( *b, 30 );
182 DBGC2 ( context, "%2d : %08x %08x %08x %08x %08x\n",
183 i, *a, *b, *c, *d, *e );
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] );
193 DBGC ( context, "SHA1 digested:\n" );
194 DBGC_HDA ( context, 0, &context->ddd.dd.digest,
195 sizeof ( context->ddd.dd.digest ) );
199 * Accumulate data with SHA-1 algorithm
201 * @v ctx SHA-1 context
203 * @v len Length of data
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;
210 /* Accumulate data a byte at a time, performing the digest
211 * whenever we fill the data buffer
214 offset = ( context->len % sizeof ( context->ddd.dd.data ) );
215 context->ddd.dd.data.byte[offset] = *(byte++);
217 if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
218 sha1_digest ( context );
223 * Generate SHA-1 digest
225 * @v ctx SHA-1 context
226 * @v out Output buffer
228 static void sha1_final ( void *ctx, void *out ) {
229 struct sha1_context *context = ctx;
233 /* Record length before pre-processing */
234 len_bits = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );
236 /* Pad with a single "1" bit followed by as many "0" bits as required */
239 sha1_update ( ctx, &pad, sizeof ( pad ) );
241 } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
242 offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
244 /* Append length (in bits) */
245 sha1_update ( ctx, &len_bits, sizeof ( len_bits ) );
246 assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
248 /* Copy out final digest */
249 memcpy ( out, &context->ddd.dd.digest,
250 sizeof ( context->ddd.dd.digest ) );
253 /** SHA-1 algorithm */
254 struct digest_algorithm sha1_algorithm = {
256 .ctxsize = sizeof ( struct sha1_context ),
257 .blocksize = sizeof ( union sha1_block ),
258 .digestsize = sizeof ( struct sha1_digest ),
260 .update = sha1_update,
264 /** "sha1" object identifier */
265 static uint8_t oid_sha1[] = { ASN1_OID_SHA1 };
267 /** "sha1" OID-identified algorithm */
268 struct asn1_algorithm oid_sha1_algorithm __asn1_algorithm = {
270 .digest = &sha1_algorithm,
271 .oid = ASN1_OID_CURSOR ( oid_sha1 ),