iPXE
sha1.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * SHA-1 algorithm
00029  *
00030  */
00031 
00032 #include <stdint.h>
00033 #include <string.h>
00034 #include <byteswap.h>
00035 #include <assert.h>
00036 #include <ipxe/rotate.h>
00037 #include <ipxe/crypto.h>
00038 #include <ipxe/asn1.h>
00039 #include <ipxe/sha1.h>
00040 
00041 /** SHA-1 variables */
00042 struct sha1_variables {
00043         /* This layout matches that of struct sha1_digest_data,
00044          * allowing for efficient endianness-conversion,
00045          */
00046         uint32_t a;
00047         uint32_t b;
00048         uint32_t c;
00049         uint32_t d;
00050         uint32_t e;
00051         uint32_t w[80];
00052 } __attribute__ (( packed ));
00053 
00054 /**
00055  * f(a,b,c,d) for steps 0 to 19
00056  *
00057  * @v v         SHA-1 variables
00058  * @ret f       f(a,b,c,d)
00059  */
00060 static uint32_t sha1_f_0_19 ( struct sha1_variables *v ) {
00061         return ( ( v->b & v->c ) | ( (~v->b) & v->d ) );
00062 }
00063 
00064 /**
00065  * f(a,b,c,d) for steps 20 to 39 and 60 to 79
00066  *
00067  * @v v         SHA-1 variables
00068  * @ret f       f(a,b,c,d)
00069  */
00070 static uint32_t sha1_f_20_39_60_79 ( struct sha1_variables *v ) {
00071         return ( v->b ^ v->c ^ v->d );
00072 }
00073 
00074 /**
00075  * f(a,b,c,d) for steps 40 to 59
00076  *
00077  * @v v         SHA-1 variables
00078  * @ret f       f(a,b,c,d)
00079  */
00080 static uint32_t sha1_f_40_59 ( struct sha1_variables *v ) {
00081         return ( ( v->b & v->c ) | ( v->b & v->d ) | ( v->c & v->d ) );
00082 }
00083 
00084 /** An SHA-1 step function */
00085 struct sha1_step {
00086         /**
00087          * Calculate f(a,b,c,d)
00088          *
00089          * @v v         SHA-1 variables
00090          * @ret f       f(a,b,c,d)
00091          */
00092         uint32_t ( * f ) ( struct sha1_variables *v );
00093         /** Constant k */
00094         uint32_t k;
00095 };
00096 
00097 /** SHA-1 steps */
00098 static struct sha1_step sha1_steps[4] = {
00099         /** 0 to 19 */
00100         { .f = sha1_f_0_19,             .k = 0x5a827999 },
00101         /** 20 to 39 */
00102         { .f = sha1_f_20_39_60_79,      .k = 0x6ed9eba1 },
00103         /** 40 to 59 */
00104         { .f = sha1_f_40_59,            .k = 0x8f1bbcdc },
00105         /** 60 to 79 */
00106         { .f = sha1_f_20_39_60_79,      .k = 0xca62c1d6 },
00107 };
00108 
00109 /**
00110  * Initialise SHA-1 algorithm
00111  *
00112  * @v ctx               SHA-1 context
00113  */
00114 static void sha1_init ( void *ctx ) {
00115         struct sha1_context *context = ctx;
00116 
00117         context->ddd.dd.digest.h[0] = cpu_to_be32 ( 0x67452301 );
00118         context->ddd.dd.digest.h[1] = cpu_to_be32 ( 0xefcdab89 );
00119         context->ddd.dd.digest.h[2] = cpu_to_be32 ( 0x98badcfe );
00120         context->ddd.dd.digest.h[3] = cpu_to_be32 ( 0x10325476 );
00121         context->ddd.dd.digest.h[4] = cpu_to_be32 ( 0xc3d2e1f0 );
00122         context->len = 0;
00123 }
00124 
00125 /**
00126  * Calculate SHA-1 digest of accumulated data
00127  *
00128  * @v context           SHA-1 context
00129  */
00130 static void sha1_digest ( struct sha1_context *context ) {
00131         union {
00132                 union sha1_digest_data_dwords ddd;
00133                 struct sha1_variables v;
00134         } u;
00135         uint32_t *a = &u.v.a;
00136         uint32_t *b = &u.v.b;
00137         uint32_t *c = &u.v.c;
00138         uint32_t *d = &u.v.d;
00139         uint32_t *e = &u.v.e;
00140         uint32_t *w = u.v.w;
00141         uint32_t f;
00142         uint32_t k;
00143         uint32_t temp;
00144         struct sha1_step *step;
00145         unsigned int i;
00146 
00147         /* Sanity checks */
00148         assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
00149         linker_assert ( &u.ddd.dd.digest.h[0] == a, sha1_bad_layout );
00150         linker_assert ( &u.ddd.dd.digest.h[1] == b, sha1_bad_layout );
00151         linker_assert ( &u.ddd.dd.digest.h[2] == c, sha1_bad_layout );
00152         linker_assert ( &u.ddd.dd.digest.h[3] == d, sha1_bad_layout );
00153         linker_assert ( &u.ddd.dd.digest.h[4] == e, sha1_bad_layout );
00154         linker_assert ( &u.ddd.dd.data.dword[0] == w, sha1_bad_layout );
00155 
00156         DBGC ( context, "SHA1 digesting:\n" );
00157         DBGC_HDA ( context, 0, &context->ddd.dd.digest,
00158                    sizeof ( context->ddd.dd.digest ) );
00159         DBGC_HDA ( context, context->len, &context->ddd.dd.data,
00160                    sizeof ( context->ddd.dd.data ) );
00161 
00162         /* Convert h[0..4] to host-endian, and initialise a, b, c, d,
00163          * e, and w[0..15]
00164          */
00165         for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
00166                             sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
00167                 be32_to_cpus ( &context->ddd.dword[i] );
00168                 u.ddd.dword[i] = context->ddd.dword[i];
00169         }
00170 
00171         /* Initialise w[16..79] */
00172         for ( i = 16 ; i < 80 ; i++ )
00173                 w[i] = rol32 ( ( w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16] ), 1 );
00174 
00175         /* Main loop */
00176         for ( i = 0 ; i < 80 ; i++ ) {
00177                 step = &sha1_steps[ i / 20 ];
00178                 f = step->f ( &u.v );
00179                 k = step->k;
00180                 temp = ( rol32 ( *a, 5 ) + f + *e + k + w[i] );
00181                 *e = *d;
00182                 *d = *c;
00183                 *c = rol32 ( *b, 30 );
00184                 *b = *a;
00185                 *a = temp;
00186                 DBGC2 ( context, "%2d : %08x %08x %08x %08x %08x\n",
00187                         i, *a, *b, *c, *d, *e );
00188         }
00189 
00190         /* Add chunk to hash and convert back to big-endian */
00191         for ( i = 0 ; i < 5 ; i++ ) {
00192                 context->ddd.dd.digest.h[i] =
00193                         cpu_to_be32 ( context->ddd.dd.digest.h[i] +
00194                                       u.ddd.dd.digest.h[i] );
00195         }
00196 
00197         DBGC ( context, "SHA1 digested:\n" );
00198         DBGC_HDA ( context, 0, &context->ddd.dd.digest,
00199                    sizeof ( context->ddd.dd.digest ) );
00200 }
00201 
00202 /**
00203  * Accumulate data with SHA-1 algorithm
00204  *
00205  * @v ctx               SHA-1 context
00206  * @v data              Data
00207  * @v len               Length of data
00208  */
00209 static void sha1_update ( void *ctx, const void *data, size_t len ) {
00210         struct sha1_context *context = ctx;
00211         const uint8_t *byte = data;
00212         size_t offset;
00213 
00214         /* Accumulate data a byte at a time, performing the digest
00215          * whenever we fill the data buffer
00216          */
00217         while ( len-- ) {
00218                 offset = ( context->len % sizeof ( context->ddd.dd.data ) );
00219                 context->ddd.dd.data.byte[offset] = *(byte++);
00220                 context->len++;
00221                 if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
00222                         sha1_digest ( context );
00223         }
00224 }
00225 
00226 /**
00227  * Generate SHA-1 digest
00228  *
00229  * @v ctx               SHA-1 context
00230  * @v out               Output buffer
00231  */
00232 static void sha1_final ( void *ctx, void *out ) {
00233         struct sha1_context *context = ctx;
00234         uint64_t len_bits;
00235         uint8_t pad;
00236 
00237         /* Record length before pre-processing */
00238         len_bits = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );
00239 
00240         /* Pad with a single "1" bit followed by as many "0" bits as required */
00241         pad = 0x80;
00242         do {
00243                 sha1_update ( ctx, &pad, sizeof ( pad ) );
00244                 pad = 0x00;
00245         } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
00246                   offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
00247 
00248         /* Append length (in bits) */
00249         sha1_update ( ctx, &len_bits, sizeof ( len_bits ) );
00250         assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
00251 
00252         /* Copy out final digest */
00253         memcpy ( out, &context->ddd.dd.digest,
00254                  sizeof ( context->ddd.dd.digest ) );
00255 }
00256 
00257 /** SHA-1 algorithm */
00258 struct digest_algorithm sha1_algorithm = {
00259         .name           = "sha1",
00260         .ctxsize        = sizeof ( struct sha1_context ),
00261         .blocksize      = sizeof ( union sha1_block ),
00262         .digestsize     = sizeof ( struct sha1_digest ),
00263         .init           = sha1_init,
00264         .update         = sha1_update,
00265         .final          = sha1_final,
00266 };
00267 
00268 /** "sha1" object identifier */
00269 static uint8_t oid_sha1[] = { ASN1_OID_SHA1 };
00270 
00271 /** "sha1" OID-identified algorithm */
00272 struct asn1_algorithm oid_sha1_algorithm __asn1_algorithm = {
00273         .name = "sha1",
00274         .digest = &sha1_algorithm,
00275         .oid = ASN1_OID_CURSOR ( oid_sha1 ),
00276 };