iPXE
Data Structures | Functions | Variables
sha1.c File Reference

SHA-1 algorithm. More...

#include <stdint.h>
#include <string.h>
#include <byteswap.h>
#include <assert.h>
#include <ipxe/rotate.h>
#include <ipxe/crypto.h>
#include <ipxe/asn1.h>
#include <ipxe/sha1.h>

Go to the source code of this file.

Data Structures

struct  sha1_variables
 SHA-1 variables. More...
struct  sha1_step
 An SHA-1 step function. More...

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static uint32_t sha1_f_0_19 (struct sha1_variables *v)
 f(a,b,c,d) for steps 0 to 19
static uint32_t sha1_f_20_39_60_79 (struct sha1_variables *v)
 f(a,b,c,d) for steps 20 to 39 and 60 to 79
static uint32_t sha1_f_40_59 (struct sha1_variables *v)
 f(a,b,c,d) for steps 40 to 59
static void sha1_init (void *ctx)
 Initialise SHA-1 algorithm.
static void sha1_digest (struct sha1_context *context)
 Calculate SHA-1 digest of accumulated data.
static void sha1_update (void *ctx, const void *data, size_t len)
 Accumulate data with SHA-1 algorithm.
static void sha1_final (void *ctx, void *out)
 Generate SHA-1 digest.

Variables

static struct sha1_step sha1_steps [4]
 SHA-1 steps.
struct digest_algorithm sha1_algorithm
 SHA-1 algorithm.
static uint8_t oid_sha1 [] = { ASN1_OID_SHA1 }
 "sha1" object identifier
struct asn1_algorithm
oid_sha1_algorithm 
__asn1_algorithm
 "sha1" OID-identified algorithm

Detailed Description

SHA-1 algorithm.

Definition in file sha1.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static uint32_t sha1_f_0_19 ( struct sha1_variables v) [static]

f(a,b,c,d) for steps 0 to 19

Parameters:
vSHA-1 variables
Return values:
ff(a,b,c,d)

Definition at line 60 of file sha1.c.

References sha1_variables::b, sha1_variables::c, and sha1_variables::d.

                                                         {
        return ( ( v->b & v->c ) | ( (~v->b) & v->d ) );
}
static uint32_t sha1_f_20_39_60_79 ( struct sha1_variables v) [static]

f(a,b,c,d) for steps 20 to 39 and 60 to 79

Parameters:
vSHA-1 variables
Return values:
ff(a,b,c,d)

Definition at line 70 of file sha1.c.

References sha1_variables::b, sha1_variables::c, and sha1_variables::d.

                                                                {
        return ( v->b ^ v->c ^ v->d );
}
static uint32_t sha1_f_40_59 ( struct sha1_variables v) [static]

f(a,b,c,d) for steps 40 to 59

Parameters:
vSHA-1 variables
Return values:
ff(a,b,c,d)

Definition at line 80 of file sha1.c.

References sha1_variables::b, sha1_variables::c, and sha1_variables::d.

                                                          {
        return ( ( v->b & v->c ) | ( v->b & v->d ) | ( v->c & v->d ) );
}
static void sha1_init ( void *  ctx) [static]

Initialise SHA-1 algorithm.

Parameters:
ctxSHA-1 context

Definition at line 114 of file sha1.c.

References context, cpu_to_be32, ctx, sha1_digest_data_dwords::dd, sha1_context::ddd, sha1_digest_data::digest, sha1_digest::h, and sha1_context::len.

                                    {
        struct sha1_context *context = ctx;

        context->ddd.dd.digest.h[0] = cpu_to_be32 ( 0x67452301 );
        context->ddd.dd.digest.h[1] = cpu_to_be32 ( 0xefcdab89 );
        context->ddd.dd.digest.h[2] = cpu_to_be32 ( 0x98badcfe );
        context->ddd.dd.digest.h[3] = cpu_to_be32 ( 0x10325476 );
        context->ddd.dd.digest.h[4] = cpu_to_be32 ( 0xc3d2e1f0 );
        context->len = 0;
}
static void sha1_digest ( struct sha1_context context) [static]

Calculate SHA-1 digest of accumulated data.

Parameters:
contextSHA-1 context

Definition at line 130 of file sha1.c.

References sha1_variables::a, assert, sha1_variables::b, be32_to_cpus, sha1_variables::c, cpu_to_be32, sha1_variables::d, sha1_digest_data::data, DBGC, DBGC2, DBGC_HDA, sha1_digest_data_dwords::dd, sha1_context::ddd, sha1_digest_data::digest, sha1_digest_data_dwords::dword, sha1_variables::e, sha1_step::f, sha1_digest::h, k, sha1_step::k, sha1_context::len, linker_assert, rol32(), step(), u, and sha1_variables::w.

Referenced by sha1_update().

                                                         {
        union {
                union sha1_digest_data_dwords ddd;
                struct sha1_variables v;
        } u;
        uint32_t *a = &u.v.a;
        uint32_t *b = &u.v.b;
        uint32_t *c = &u.v.c;
        uint32_t *d = &u.v.d;
        uint32_t *e = &u.v.e;
        uint32_t *w = u.v.w;
        uint32_t f;
        uint32_t k;
        uint32_t temp;
        struct sha1_step *step;
        unsigned int i;

        /* Sanity checks */
        assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
        linker_assert ( &u.ddd.dd.digest.h[0] == a, sha1_bad_layout );
        linker_assert ( &u.ddd.dd.digest.h[1] == b, sha1_bad_layout );
        linker_assert ( &u.ddd.dd.digest.h[2] == c, sha1_bad_layout );
        linker_assert ( &u.ddd.dd.digest.h[3] == d, sha1_bad_layout );
        linker_assert ( &u.ddd.dd.digest.h[4] == e, sha1_bad_layout );
        linker_assert ( &u.ddd.dd.data.dword[0] == w, sha1_bad_layout );

        DBGC ( context, "SHA1 digesting:\n" );
        DBGC_HDA ( context, 0, &context->ddd.dd.digest,
                   sizeof ( context->ddd.dd.digest ) );
        DBGC_HDA ( context, context->len, &context->ddd.dd.data,
                   sizeof ( context->ddd.dd.data ) );

        /* Convert h[0..4] to host-endian, and initialise a, b, c, d,
         * e, and w[0..15]
         */
        for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
                            sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
                be32_to_cpus ( &context->ddd.dword[i] );
                u.ddd.dword[i] = context->ddd.dword[i];
        }

        /* Initialise w[16..79] */
        for ( i = 16 ; i < 80 ; i++ )
                w[i] = rol32 ( ( w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16] ), 1 );

        /* Main loop */
        for ( i = 0 ; i < 80 ; i++ ) {
                step = &sha1_steps[ i / 20 ];
                f = step->f ( &u.v );
                k = step->k;
                temp = ( rol32 ( *a, 5 ) + f + *e + k + w[i] );
                *e = *d;
                *d = *c;
                *c = rol32 ( *b, 30 );
                *b = *a;
                *a = temp;
                DBGC2 ( context, "%2d : %08x %08x %08x %08x %08x\n",
                        i, *a, *b, *c, *d, *e );
        }

        /* Add chunk to hash and convert back to big-endian */
        for ( i = 0 ; i < 5 ; i++ ) {
                context->ddd.dd.digest.h[i] =
                        cpu_to_be32 ( context->ddd.dd.digest.h[i] +
                                      u.ddd.dd.digest.h[i] );
        }

        DBGC ( context, "SHA1 digested:\n" );
        DBGC_HDA ( context, 0, &context->ddd.dd.digest,
                   sizeof ( context->ddd.dd.digest ) );
}
static void sha1_update ( void *  ctx,
const void *  data,
size_t  len 
) [static]

Accumulate data with SHA-1 algorithm.

Parameters:
ctxSHA-1 context
dataData
lenLength of data

Definition at line 209 of file sha1.c.

References sha1_block::byte, byte, context, ctx, sha1_digest_data::data, data, sha1_digest_data_dwords::dd, sha1_context::ddd, sha1_context::len, offset, and sha1_digest().

Referenced by sha1_final().

                                                                    {
        struct sha1_context *context = ctx;
        const uint8_t *byte = data;
        size_t offset;

        /* Accumulate data a byte at a time, performing the digest
         * whenever we fill the data buffer
         */
        while ( len-- ) {
                offset = ( context->len % sizeof ( context->ddd.dd.data ) );
                context->ddd.dd.data.byte[offset] = *(byte++);
                context->len++;
                if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
                        sha1_digest ( context );
        }
}
static void sha1_final ( void *  ctx,
void *  out 
) [static]

Generate SHA-1 digest.

Parameters:
ctxSHA-1 context
outOutput buffer

Definition at line 232 of file sha1.c.

References assert, context, cpu_to_be64, ctx, sha1_digest_data::data, sha1_digest_data_dwords::dd, sha1_context::ddd, sha1_digest_data::digest, sha1_context::len, memcpy(), offsetof, pad, and sha1_update().

                                                {
        struct sha1_context *context = ctx;
        uint64_t len_bits;
        uint8_t pad;

        /* Record length before pre-processing */
        len_bits = cpu_to_be64 ( ( ( uint64_t ) context->len ) * 8 );

        /* Pad with a single "1" bit followed by as many "0" bits as required */
        pad = 0x80;
        do {
                sha1_update ( ctx, &pad, sizeof ( pad ) );
                pad = 0x00;
        } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
                  offsetof ( typeof ( context->ddd.dd.data ), final.len ) );

        /* Append length (in bits) */
        sha1_update ( ctx, &len_bits, sizeof ( len_bits ) );
        assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );

        /* Copy out final digest */
        memcpy ( out, &context->ddd.dd.digest,
                 sizeof ( context->ddd.dd.digest ) );
}

Variable Documentation

struct sha1_step sha1_steps[4] [static]
Initial value:
 {
        
        { .f = sha1_f_0_19,             .k = 0x5a827999 },
        
        { .f = sha1_f_20_39_60_79,      .k = 0x6ed9eba1 },
        
        { .f = sha1_f_40_59,            .k = 0x8f1bbcdc },
        
        { .f = sha1_f_20_39_60_79,      .k = 0xca62c1d6 },
}

SHA-1 steps.

Definition at line 98 of file sha1.c.

Initial value:
 {
        .name           = "sha1",
        .ctxsize        = sizeof ( struct sha1_context ),
        .blocksize      = sizeof ( union sha1_block ),
        .digestsize     = sizeof ( struct sha1_digest ),
        .init           = sha1_init,
        .update         = sha1_update,
        .final          = sha1_final,
}

SHA-1 algorithm.

Definition at line 258 of file sha1.c.

Referenced by ccmp_kie_mic(), certstat(), md5_sha1_final(), md5_sha1_init(), md5_sha1_update(), ocsp_compare_responder_key_hash(), pbkdf2_sha1_f(), prf_sha1(), sha1_test_exec(), sha1sum_exec(), tls_prf(), and x509_name().

uint8_t oid_sha1[] = { ASN1_OID_SHA1 } [static]

"sha1" object identifier

Definition at line 269 of file sha1.c.

struct asn1_algorithm oid_sha1_algorithm __asn1_algorithm
Initial value:
 {
        .name = "sha1",
        .digest = &sha1_algorithm,
        .oid = ASN1_OID_CURSOR ( oid_sha1 ),
}

"sha1" OID-identified algorithm

Definition at line 272 of file sha1.c.