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

MD4 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/md4.h>

Go to the source code of this file.

Data Structures

struct  md4_variables
 MD4 variables. More...
struct  md4_step
 An MD4 step function. More...

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static uint32_t md4_f_0_15 (struct md4_variables *v, unsigned int i)
 f(b,c,d,w) for steps 0 to 15
static uint32_t md4_f_16_31 (struct md4_variables *v, unsigned int i)
 f(b,c,d,w) for steps 16 to 31
static uint32_t md4_f_32_47 (struct md4_variables *v, unsigned int i)
 f(b,c,d,w) for steps 32 to 47
static void md4_init (void *ctx)
 Initialise MD4 algorithm.
static void md4_digest (struct md4_context *context)
 Calculate MD4 digest of accumulated data.
static void md4_update (void *ctx, const void *data, size_t len)
 Accumulate data with MD4 algorithm.
static void md4_final (void *ctx, void *out)
 Generate MD4 digest.

Variables

struct md4_variables __attribute__
static const uint8_t r [3][4]
 MD4 shift amounts.
static struct md4_step md4_steps [4]
 MD4 steps.
struct digest_algorithm md4_algorithm
 MD4 algorithm.
static uint8_t oid_md4 [] = { ASN1_OID_MD4 }
 "md4" object identifier
struct asn1_algorithm
oid_md4_algorithm 
__asn1_algorithm
 "md4" OID-identified algorithm

Detailed Description

MD4 algorithm.

Definition in file md4.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static uint32_t md4_f_0_15 ( struct md4_variables v,
unsigned int  i 
) [static]

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

Parameters:
vMD4 variables
iIndex within round
Return values:
ff(b,c,d,w)

Definition at line 67 of file md4.c.

References md4_variables::b, md4_variables::c, md4_variables::d, and md4_variables::w.

                                                                       {
        return ( ( ( v->b & v->c ) | ( ~v->b & v->d ) ) + v->w[i] );
}
static uint32_t md4_f_16_31 ( struct md4_variables v,
unsigned int  i 
) [static]

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

Parameters:
vMD4 variables
iIndex within round
Return values:
ff(b,c,d,w)

Definition at line 78 of file md4.c.

References md4_variables::b, md4_variables::c, md4_variables::d, and md4_variables::w.

                                                                        {
        return ( ( ( v->b & v->c ) | ( v->b & v->d ) | ( v->c & v->d ) ) +
                 v->w[ ( ( i << 2 ) | ( i >> 2 ) ) % 16 ] );
}
static uint32_t md4_f_32_47 ( struct md4_variables v,
unsigned int  i 
) [static]

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

Parameters:
vMD4 variables
iIndex within round
Return values:
ff(b,c,d,w)

Definition at line 90 of file md4.c.

References md4_variables::b, md4_variables::c, md4_variables::d, and md4_variables::w.

                                                                        {
        static const uint8_t reverse[16] = {
                0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
        };
        return ( ( v->b ^ v->c ^ v->d ) + v->w[reverse[i]] );
}
static void md4_init ( void *  ctx) [static]

Initialise MD4 algorithm.

Parameters:
ctxMD4 context

Definition at line 126 of file md4.c.

References context, cpu_to_le32, ctx, md4_digest_data_dwords::dd, md4_context::ddd, md4_digest_data::digest, md4_digest::h, and md4_context::len.

                                   {
        struct md4_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 md4_digest ( struct md4_context context) [static]

Calculate MD4 digest of accumulated data.

Parameters:
contextMD4 context

Definition at line 141 of file md4.c.

References md4_variables::a, assert, md4_variables::b, md4_variables::c, md4_step::constant, cpu_to_le32, md4_variables::d, md4_digest_data::data, DBGC, DBGC2, DBGC_HDA, md4_digest_data_dwords::dd, md4_context::ddd, md4_digest_data::digest, md4_digest_data_dwords::dword, md4_step::f, md4_digest::h, le32_to_cpus, md4_context::len, linker_assert, rol32(), step(), u, and md4_variables::w.

Referenced by md4_update().

                                                       {
        union {
                union md4_digest_data_dwords ddd;
                struct md4_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 temp;
        struct md4_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, md4_bad_layout );
        linker_assert ( &u.ddd.dd.digest.h[1] == b, md4_bad_layout );
        linker_assert ( &u.ddd.dd.digest.h[2] == c, md4_bad_layout );
        linker_assert ( &u.ddd.dd.digest.h[3] == d, md4_bad_layout );
        linker_assert ( &u.ddd.dd.data.dword[0] == w, md4_bad_layout );

        DBGC ( context, "MD4 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 x[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 < 48 ; i++ ) {
                round = ( i / 16 );
                step = &md4_steps[round];
                f = step->f ( &u.v, ( i % 16 ) );
                temp = *d;
                *d = *c;
                *c = *b;
                *b = rol32 ( ( *a + f + step->constant ), 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, "MD4 digested:\n" );
        DBGC_HDA ( context, 0, &context->ddd.dd.digest,
                   sizeof ( context->ddd.dd.digest ) );
}
static void md4_update ( void *  ctx,
const void *  data,
size_t  len 
) [static]

Accumulate data with MD4 algorithm.

Parameters:
ctxMD4 context
dataData
lenLength of data

Definition at line 213 of file md4.c.

References md4_block::byte, byte, context, ctx, md4_digest_data::data, data, md4_digest_data_dwords::dd, md4_context::ddd, md4_context::len, md4_digest(), and offset.

Referenced by md4_final().

                                                                   {
        struct md4_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 )
                        md4_digest ( context );
        }
}
static void md4_final ( void *  ctx,
void *  out 
) [static]

Generate MD4 digest.

Parameters:
ctxMD4 context
outOutput buffer

Definition at line 236 of file md4.c.

References assert, context, cpu_to_le64, ctx, md4_digest_data::data, md4_digest_data_dwords::dd, md4_context::ddd, md4_digest_data::digest, md4_context::len, md4_update(), memcpy(), offsetof, and pad.

                                               {
        struct md4_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 {
                md4_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) */
        md4_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 uint8_t r[3][4] [static]
Initial value:
 {
        {  3,  7, 11, 19 },
        {  3,  5,  9, 13 },
        {  3,  9, 11, 15 },
}

MD4 shift amounts.

Definition at line 54 of file md4.c.

Referenced by __ath9k_hw_init(), __attribute__(), ar5008_write_rf_array(), ath9k_hw_channel_change(), ath9k_hw_reset(), ath9k_hw_write_array(), ath9k_start(), ath_radio_disable(), ath_reset(), ath_set_channel(), bcom_phy_init(), fls(), and genesis_mac_init().

struct md4_step md4_steps[4] [static]
Initial value:
 {
        
        { .f = md4_f_0_15,      .constant = 0x00000000UL },
        
        { .f = md4_f_16_31,     .constant = 0x5a827999UL },
        
        { .f = md4_f_32_47,     .constant = 0x6ed9eba1UL },
}

MD4 steps.

Definition at line 112 of file md4.c.

Initial value:
 {
        .name           = "md4",
        .ctxsize        = sizeof ( struct md4_context ),
        .blocksize      = sizeof ( union md4_block ),
        .digestsize     = sizeof ( struct md4_digest ),
        .init           = md4_init,
        .update         = md4_update,
        .final          = md4_final,
}

MD4 algorithm.

Definition at line 262 of file md4.c.

Referenced by md4_test_exec(), and ntlm_key().

uint8_t oid_md4[] = { ASN1_OID_MD4 } [static]

"md4" object identifier

Definition at line 273 of file md4.c.

struct asn1_algorithm oid_md4_algorithm __asn1_algorithm
Initial value:
 {
        .name = "md4",
        .digest = &md4_algorithm,
        .oid = ASN1_OID_CURSOR ( oid_md4 ),
}

"md4" OID-identified algorithm

Definition at line 276 of file md4.c.