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

MD5 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/md5.h>

Go to the source code of this file.

Data Structures

struct  md5_variables
 MD5 variables. More...
struct  md5_step
 An MD5 step function. More...

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static uint32_t md5_f_0_15 (struct md5_variables *v)
 f(b,c,d) for steps 0 to 15
static uint32_t md5_f_16_31 (struct md5_variables *v)
 f(b,c,d) for steps 16 to 31
static uint32_t md5_f_32_47 (struct md5_variables *v)
 f(b,c,d) for steps 32 to 47
static uint32_t md5_f_48_63 (struct md5_variables *v)
 f(b,c,d) for steps 48 to 63
static void md5_init (void *ctx)
 Initialise MD5 algorithm.
static void md5_digest (struct md5_context *context)
 Calculate MD5 digest of accumulated data.
static void md5_update (void *ctx, const void *data, size_t len)
 Accumulate data with MD5 algorithm.
static void md5_final (void *ctx, void *out)
 Generate MD5 digest.

Variables

static const uint32_t k [64]
 MD5 constants.
static const uint8_t r [4][4]
 MD5 shift amounts.
static struct md5_step md5_steps [4]
 MD5 steps.
struct digest_algorithm md5_algorithm
 MD5 algorithm.
static uint8_t oid_md5 [] = { ASN1_OID_MD5 }
 "md5" object identifier
struct asn1_algorithm
oid_md5_algorithm 
__asn1_algorithm
 "md5" OID-identified algorithm

Detailed Description

MD5 algorithm.

Definition in file md5.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static uint32_t md5_f_0_15 ( struct md5_variables v) [static]

f(b,c,d) for steps 0 to 15

Parameters:
vMD5 variables
Return values:
ff(b,c,d)

Definition at line 82 of file md5.c.

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

                                                       {
        return ( v->d ^ ( v->b & ( v->c ^ v->d ) ) );
}
static uint32_t md5_f_16_31 ( struct md5_variables v) [static]

f(b,c,d) for steps 16 to 31

Parameters:
vMD5 variables
Return values:
ff(b,c,d)

Definition at line 92 of file md5.c.

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

                                                        {
        return ( v->c ^ ( v->d & ( v->b ^ v->c ) ) );
}
static uint32_t md5_f_32_47 ( struct md5_variables v) [static]

f(b,c,d) for steps 32 to 47

Parameters:
vMD5 variables
Return values:
ff(b,c,d)

Definition at line 102 of file md5.c.

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

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

f(b,c,d) for steps 48 to 63

Parameters:
vMD5 variables
Return values:
ff(b,c,d)

Definition at line 112 of file md5.c.

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

                                                        {
        return ( v->c ^ ( v->b | (~v->d) ) );
}
static void md5_init ( void *  ctx) [static]

Initialise MD5 algorithm.

Parameters:
ctxMD5 context

Definition at line 148 of file md5.c.

References context, cpu_to_le32, ctx, md5_digest_data_dwords::dd, md5_context::ddd, md5_digest_data::digest, md5_digest::h, and md5_context::len.

                                   {
        struct md5_context *context = ctx;

        context->ddd.dd.digest.h[0] = cpu_to_le32 ( 0x67452301 );
        context->ddd.dd.digest.h[1] = cpu_to_le32 ( 0xefcdab89 );
        context->ddd.dd.digest.h[2] = cpu_to_le32 ( 0x98badcfe );
        context->ddd.dd.digest.h[3] = cpu_to_le32 ( 0x10325476 );
        context->len = 0;
}
static void md5_digest ( struct md5_context context) [static]

Calculate MD5 digest of accumulated data.

Parameters:
contextMD5 context

Definition at line 163 of file md5.c.

References md5_variables::a, assert, md5_variables::b, md5_variables::c, md5_step::coefficient, md5_step::constant, cpu_to_le32, md5_variables::d, md5_digest_data::data, DBGC, DBGC2, DBGC_HDA, md5_digest_data_dwords::dd, md5_context::ddd, md5_digest_data::digest, md5_digest_data_dwords::dword, md5_step::f, md5_digest::h, le32_to_cpus, md5_context::len, linker_assert, rol32(), step(), u, and md5_variables::w.

Referenced by md5_update().

                                                       {
        union {
                union md5_digest_data_dwords ddd;
                struct md5_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 *w = u.v.w;
        uint32_t f;
        uint32_t g;
        uint32_t temp;
        struct md5_step *step;
        unsigned int round;
        unsigned int i;

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

        DBGC ( context, "MD5 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..3] to host-endian, and initialise a, b, c, d,
         * and w[0..15]
         */
        for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
                            sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
                le32_to_cpus ( &context->ddd.dword[i] );
                u.ddd.dword[i] = context->ddd.dword[i];
        }

        /* Main loop */
        for ( i = 0 ; i < 64 ; i++ ) {
                round = ( i / 16 );
                step = &md5_steps[round];
                f = step->f ( &u.v );
                g = ( ( ( step->coefficient * i ) + step->constant ) % 16 );
                temp = *d;
                *d = *c;
                *c = *b;
                *b = ( *b + rol32 ( ( *a + f + k[i] + w[g] ),
                                    r[round][ i % 4 ] ) );
                *a = temp;
                DBGC2 ( context, "%2d : %08x %08x %08x %08x\n",
                        i, *a, *b, *c, *d );
        }

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

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

Accumulate data with MD5 algorithm.

Parameters:
ctxMD5 context
dataData
lenLength of data

Definition at line 238 of file md5.c.

References md5_block::byte, byte, context, ctx, md5_digest_data::data, data, md5_digest_data_dwords::dd, md5_context::ddd, md5_context::len, md5_digest(), and offset.

Referenced by md5_final().

                                                                   {
        struct md5_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 )
                        md5_digest ( context );
        }
}
static void md5_final ( void *  ctx,
void *  out 
) [static]

Generate MD5 digest.

Parameters:
ctxMD5 context
outOutput buffer

Definition at line 261 of file md5.c.

References assert, context, cpu_to_le64, ctx, md5_digest_data::data, md5_digest_data_dwords::dd, md5_context::ddd, md5_digest_data::digest, md5_context::len, md5_update(), memcpy(), offsetof, and pad.

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

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

        /* Pad with a single "1" bit followed by as many "0" bits as required */
        pad = 0x80;
        do {
                md5_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) */
        md5_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

const uint32_t k[64] [static]
Initial value:
 {
        0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a,
        0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
        0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340,
        0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
        0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8,
        0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
        0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa,
        0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
        0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92,
        0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
        0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
}

MD5 constants.

Definition at line 54 of file md5.c.

Referenced by ath9k_adjust_pdadc_values(), ath9k_change_gain_boundary_setting(), ath9k_hw_fill_vpd_table(), ath9k_hw_get_gain_boundaries_pdadcs(), sha1_digest(), and vxgetlink().

const uint8_t r[4][4] [static]
Initial value:
 {
        {  7, 12, 17, 22 },
        {  5,  9, 14, 20 },
        {  4, 11, 16, 23 },
        {  6, 10, 15, 21 },
}

MD5 shift amounts.

Definition at line 69 of file md5.c.

struct md5_step md5_steps[4] [static]
Initial value:
 {
        
        { .f = md5_f_0_15,      .coefficient = 1,       .constant = 0 },
        
        { .f = md5_f_16_31,     .coefficient = 5,       .constant = 1 },
        
        { .f = md5_f_32_47,     .coefficient = 3,       .constant = 5 },
        
        { .f = md5_f_48_63,     .coefficient = 7,       .constant = 0 },
}

MD5 steps.

Definition at line 132 of file md5.c.

Initial value:
 {
        .name           = "md5",
        .ctxsize        = sizeof ( struct md5_context ),
        .blocksize      = sizeof ( union md5_block ),
        .digestsize     = sizeof ( struct md5_digest ),
        .init           = md5_init,
        .update         = md5_update,
        .final          = md5_final,
}

MD5 algorithm.

Definition at line 287 of file md5.c.

Referenced by dbg_md5_da(), http_digest_final(), http_digest_init(), http_digest_update(), iscsi_handle_chap_i_value(), iscsi_handle_chap_r_value(), md5_sha1_final(), md5_sha1_init(), md5_sha1_update(), md5_test_exec(), md5sum_exec(), ntlm_key(), ntlm_response(), tkip_kie_mic(), and tls_prf().

uint8_t oid_md5[] = { ASN1_OID_MD5 } [static]

"md5" object identifier

Definition at line 298 of file md5.c.

struct asn1_algorithm oid_md5_algorithm __asn1_algorithm
Initial value:
 {
        .name = "md5",
        .digest = &md5_algorithm,
        .oid = ASN1_OID_CURSOR ( oid_md5 ),
}

"md5" OID-identified algorithm

Definition at line 301 of file md5.c.