iPXE
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/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)
 FILE_SECBOOT (PERMITTED)
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

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.

Detailed Description

MD4 algorithm.

Definition in file md4.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ md4_f_0_15()

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.

67 {
68 return ( ( ( v->b & v->c ) | ( ~v->b & v->d ) ) + v->w[i] );
69}
uint32_t c
Definition md4.c:48
uint32_t b
Definition md4.c:47
uint32_t w[16]
Definition md4.c:50
uint32_t d
Definition md4.c:49

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

◆ md4_f_16_31()

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.

78 {
79 return ( ( ( v->b & v->c ) | ( v->b & v->d ) | ( v->c & v->d ) ) +
80 v->w[ ( ( i << 2 ) | ( i >> 2 ) ) % 16 ] );
81}

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

◆ md4_f_32_47()

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.

90 {
91 static const uint8_t reverse[16] = {
92 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
93 };
94 return ( ( v->b ^ v->c ^ v->d ) + v->w[reverse[i]] );
95}
unsigned char uint8_t
Definition stdint.h:10

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

◆ md4_init()

void md4_init ( void * ctx)
static

Initialise MD4 algorithm.

Parameters
ctxMD4 context

Definition at line 126 of file md4.c.

126 {
127 struct md4_context *context = ctx;
128
129 context->ddd.dd.digest.h[0] = cpu_to_le32 ( 0x67452301 );
130 context->ddd.dd.digest.h[1] = cpu_to_le32 ( 0xefcdab89 );
131 context->ddd.dd.digest.h[2] = cpu_to_le32 ( 0x98badcfe );
132 context->ddd.dd.digest.h[3] = cpu_to_le32 ( 0x10325476 );
133 context->len = 0;
134}
struct golan_eq_context ctx
Definition CIB_PRM.h:0
#define cpu_to_le32(value)
Definition byteswap.h:108
An MD4 context.
Definition md4.h:59
size_t len
Amount of accumulated data.
Definition md4.h:61
union md4_digest_data_dwords ddd
Digest and accumulated data.
Definition md4.h:63
struct md4_digest digest
Digest of data already processed.
Definition md4.h:44
uint32_t h[4]
Hash output.
Definition md4.h:19
struct md4_digest_data dd
Digest and data block.
Definition md4.h:52

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

◆ md4_digest()

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.

141 {
142 union {
143 union md4_digest_data_dwords ddd;
144 struct md4_variables v;
145 } u;
146 uint32_t *a = &u.v.a;
147 uint32_t *b = &u.v.b;
148 uint32_t *c = &u.v.c;
149 uint32_t *d = &u.v.d;
150 uint32_t *w = u.v.w;
151 uint32_t f;
152 uint32_t temp;
153 struct md4_step *step;
154 unsigned int round;
155 unsigned int i;
156
157 /* Sanity checks */
158 assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
159 build_assert ( &u.ddd.dd.digest.h[0] == a );
160 build_assert ( &u.ddd.dd.digest.h[1] == b );
161 build_assert ( &u.ddd.dd.digest.h[2] == c );
162 build_assert ( &u.ddd.dd.digest.h[3] == d );
163 build_assert ( &u.ddd.dd.data.dword[0] == w );
164
165 DBGC ( context, "MD4 digesting:\n" );
166 DBGC_HDA ( context, 0, &context->ddd.dd.digest,
167 sizeof ( context->ddd.dd.digest ) );
168 DBGC_HDA ( context, context->len, &context->ddd.dd.data,
169 sizeof ( context->ddd.dd.data ) );
170
171 /* Convert h[0..3] to host-endian, and initialise a, b, c, d,
172 * and x[0..15]
173 */
174 for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
175 sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
176 le32_to_cpus ( &context->ddd.dword[i] );
177 u.ddd.dword[i] = context->ddd.dword[i];
178 }
179
180 /* Main loop */
181 for ( i = 0 ; i < 48 ; i++ ) {
182 round = ( i / 16 );
183 step = &md4_steps[round];
184 f = step->f ( &u.v, ( i % 16 ) );
185 temp = *d;
186 *d = *c;
187 *c = *b;
188 *b = rol32 ( ( *a + f + step->constant ), r[round][ i % 4 ] );
189 *a = temp;
190 DBGC2 ( context, "%2d : %08x %08x %08x %08x\n",
191 i, *a, *b, *c, *d );
192 }
193
194 /* Add chunk to hash and convert back to little-endian */
195 for ( i = 0 ; i < 4 ; i++ ) {
196 context->ddd.dd.digest.h[i] =
197 cpu_to_le32 ( context->ddd.dd.digest.h[i] +
198 u.ddd.dd.digest.h[i] );
199 }
200
201 DBGC ( context, "MD4 digested:\n" );
202 DBGC_HDA ( context, 0, &context->ddd.dd.digest,
203 sizeof ( context->ddd.dd.digest ) );
204}
unsigned int uint32_t
Definition stdint.h:12
#define build_assert(condition)
Assert a condition at build time (after dead code elimination)
Definition assert.h:77
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
union @104331263140136355135267063077374276003064103115 u
#define DBGC2(...)
Definition compiler.h:522
#define DBGC(...)
Definition compiler.h:505
#define DBGC_HDA(...)
Definition compiler.h:506
#define le32_to_cpus(ptr)
Definition byteswap.h:126
static const uint8_t r[3][4]
MD4 shift amounts.
Definition md4.c:54
static struct md4_step md4_steps[4]
MD4 steps.
Definition md4.c:112
void step(void)
Single-step a single process.
Definition process.c:99
union md4_block data
Accumulated data.
Definition md4.h:46
An MD4 step function.
Definition md4.c:98
uint32_t(* f)(struct md4_variables *v, unsigned int i)
Calculate f(b,c,d,w)
Definition md4.c:106
MD4 variables.
Definition md4.c:42
uint32_t a
Definition md4.c:46
MD4 digest and data block.
Definition md4.h:50
uint32_t dword[sizeof(struct md4_digest_data)/sizeof(uint32_t)]
Raw dwords.
Definition md4.h:55
static u32 rol32(u32 v, int bits)
Rotate 32-bit value left.
Definition wpa_tkip.c:174

References md4_variables::a, assert, md4_variables::b, build_assert, md4_variables::c, 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, md4_steps, r, rol32(), step(), u, and md4_variables::w.

Referenced by md4_update().

◆ md4_update()

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.

213 {
214 struct md4_context *context = ctx;
215 const uint8_t *byte = data;
216 size_t offset;
217
218 /* Accumulate data a byte at a time, performing the digest
219 * whenever we fill the data buffer
220 */
221 while ( len-- ) {
222 offset = ( context->len % sizeof ( context->ddd.dd.data ) );
223 context->ddd.dd.data.byte[offset] = *(byte++);
224 context->len++;
225 if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
226 md4_digest ( context );
227 }
228}
uint16_t offset
Offset to command line.
Definition bzimage.h:3
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
static void md4_digest(struct md4_context *context)
Calculate MD4 digest of accumulated data.
Definition md4.c:141
uint8_t byte[64]
Raw bytes.
Definition md4.h:25

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

Referenced by md4_final().

◆ md4_final()

void md4_final ( void * ctx,
void * out )
static

Generate MD4 digest.

Parameters
ctxMD4 context
outOutput buffer

Definition at line 236 of file md4.c.

236 {
237 struct md4_context *context = ctx;
238 uint64_t len_bits;
239 uint8_t pad;
240
241 /* Record length before pre-processing */
242 len_bits = cpu_to_le64 ( ( ( uint64_t ) context->len ) * 8 );
243
244 /* Pad with a single "1" bit followed by as many "0" bits as required */
245 pad = 0x80;
246 do {
247 md4_update ( ctx, &pad, sizeof ( pad ) );
248 pad = 0x00;
249 } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
250 offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
251
252 /* Append length (in bits) */
253 md4_update ( ctx, &len_bits, sizeof ( len_bits ) );
254 assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
255
256 /* Copy out final digest */
257 memcpy ( out, &context->ddd.dd.digest,
258 sizeof ( context->ddd.dd.digest ) );
259}
__be32 out[4]
Definition CIB_PRM.h:8
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition acpi.c:48
u32 pad[9]
Padding.
Definition ar9003_mac.h:23
unsigned long long uint64_t
Definition stdint.h:13
#define cpu_to_le64(value)
Definition byteswap.h:109
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static void md4_update(void *ctx, const void *data, size_t len)
Accumulate data with MD4 algorithm.
Definition md4.c:213
#define offsetof(type, field)
Get offset of a field within a structure.
Definition stddef.h:25

References assert, 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, out, pad, and typeof().

Variable Documentation

◆ r

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.

54 {
55 { 3, 7, 11, 19 },
56 { 3, 5, 9, 13 },
57 { 3, 9, 11, 15 },
58};

Referenced by __ath9k_hw_init(), __attribute__(), af_packet_nic_poll(), 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(), ecdsa_alloc(), ecdsa_sign_rs(), ecdsa_verify_rs(), fls(), genesis_mac_init(), gma_write16(), gma_write16(), md4_digest(), md5_digest(), tap_poll(), xm_write16(), and xm_write32().

◆ md4_steps

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 },
}
static uint32_t md4_f_32_47(struct md4_variables *v, unsigned int i)
f(b,c,d,w) for steps 32 to 47
Definition md4.c:90
static uint32_t md4_f_16_31(struct md4_variables *v, unsigned int i)
f(b,c,d,w) for steps 16 to 31
Definition md4.c:78
static uint32_t md4_f_0_15(struct md4_variables *v, unsigned int i)
f(b,c,d,w) for steps 0 to 15
Definition md4.c:67

MD4 steps.

Definition at line 112 of file md4.c.

112 {
113 /** 0 to 15 */
114 { .f = md4_f_0_15, .constant = 0x00000000UL },
115 /** 16 to 31 */
116 { .f = md4_f_16_31, .constant = 0x5a827999UL },
117 /** 32 to 47 */
118 { .f = md4_f_32_47, .constant = 0x6ed9eba1UL },
119};

Referenced by md4_digest().

◆ md4_algorithm

struct digest_algorithm md4_algorithm
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,
}
static void md4_final(void *ctx, void *out)
Generate MD4 digest.
Definition md4.c:236
static void md4_init(void *ctx)
Initialise MD4 algorithm.
Definition md4.c:126
uint32_t digestsize
Digest size (i.e.
Definition pccrr.h:1
An MD4 digest.
Definition md4.h:17
An MD4 data block.
Definition md4.h:23

MD4 algorithm.

Definition at line 262 of file md4.c.

262 {
263 .name = "md4",
264 .ctxsize = sizeof ( struct md4_context ),
265 .blocksize = sizeof ( union md4_block ),
266 .digestsize = sizeof ( struct md4_digest ),
267 .init = md4_init,
268 .update = md4_update,
269 .final = md4_final,
270};

Referenced by DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), md4_test_exec(), md4sum_exec(), mschapv2_hash_hash(), mschapv2_password_hash(), and ntlm_key().