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

Detailed Description

MD5 algorithm.

Definition in file md5.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ md5_f_0_15()

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.

82 {
83 return ( v->d ^ ( v->b & ( v->c ^ v->d ) ) );
84}
uint32_t d
Definition md5.c:49
uint32_t b
Definition md5.c:47
uint32_t c
Definition md5.c:48

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

◆ md5_f_16_31()

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.

92 {
93 return ( v->c ^ ( v->d & ( v->b ^ v->c ) ) );
94}

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

◆ md5_f_32_47()

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.

102 {
103 return ( v->b ^ v->c ^ v->d );
104}

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

◆ md5_f_48_63()

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.

112 {
113 return ( v->c ^ ( v->b | (~v->d) ) );
114}

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

◆ md5_init()

void md5_init ( void * ctx)
static

Initialise MD5 algorithm.

Parameters
ctxMD5 context

Definition at line 148 of file md5.c.

148 {
149 struct md5_context *context = ctx;
150
151 context->ddd.dd.digest.h[0] = cpu_to_le32 ( 0x67452301 );
152 context->ddd.dd.digest.h[1] = cpu_to_le32 ( 0xefcdab89 );
153 context->ddd.dd.digest.h[2] = cpu_to_le32 ( 0x98badcfe );
154 context->ddd.dd.digest.h[3] = cpu_to_le32 ( 0x10325476 );
155 context->len = 0;
156}
struct golan_eq_context ctx
Definition CIB_PRM.h:0
#define cpu_to_le32(value)
Definition byteswap.h:108
An MD5 context.
Definition md5.h:59
size_t len
Amount of accumulated data.
Definition md5.h:61
union md5_digest_data_dwords ddd
Digest and accumulated data.
Definition md5.h:63
struct md5_digest digest
Digest of data already processed.
Definition md5.h:44
uint32_t h[4]
Hash output.
Definition md5.h:19
struct md5_digest_data dd
Digest and data block.
Definition md5.h:52

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

◆ md5_digest()

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.

163 {
164 union {
165 union md5_digest_data_dwords ddd;
166 struct md5_variables v;
167 } u;
168 uint32_t *a = &u.v.a;
169 uint32_t *b = &u.v.b;
170 uint32_t *c = &u.v.c;
171 uint32_t *d = &u.v.d;
172 uint32_t *w = u.v.w;
173 uint32_t f;
174 uint32_t g;
175 uint32_t temp;
176 struct md5_step *step;
177 unsigned int round;
178 unsigned int i;
179
180 /* Sanity checks */
181 assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
182 build_assert ( &u.ddd.dd.digest.h[0] == a );
183 build_assert ( &u.ddd.dd.digest.h[1] == b );
184 build_assert ( &u.ddd.dd.digest.h[2] == c );
185 build_assert ( &u.ddd.dd.digest.h[3] == d );
186 build_assert ( &u.ddd.dd.data.dword[0] == w );
187
188 DBGC ( context, "MD5 digesting:\n" );
189 DBGC_HDA ( context, 0, &context->ddd.dd.digest,
190 sizeof ( context->ddd.dd.digest ) );
191 DBGC_HDA ( context, context->len, &context->ddd.dd.data,
192 sizeof ( context->ddd.dd.data ) );
193
194 /* Convert h[0..3] to host-endian, and initialise a, b, c, d,
195 * and w[0..15]
196 */
197 for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
198 sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
199 le32_to_cpus ( &context->ddd.dword[i] );
200 u.ddd.dword[i] = context->ddd.dword[i];
201 }
202
203 /* Main loop */
204 for ( i = 0 ; i < 64 ; i++ ) {
205 round = ( i / 16 );
206 step = &md5_steps[round];
207 f = step->f ( &u.v );
208 g = ( ( ( step->coefficient * i ) + step->constant ) % 16 );
209 temp = *d;
210 *d = *c;
211 *c = *b;
212 *b = ( *b + rol32 ( ( *a + f + k[i] + w[g] ),
213 r[round][ i % 4 ] ) );
214 *a = temp;
215 DBGC2 ( context, "%2d : %08x %08x %08x %08x\n",
216 i, *a, *b, *c, *d );
217 }
218
219 /* Add chunk to hash and convert back to little-endian */
220 for ( i = 0 ; i < 4 ; i++ ) {
221 context->ddd.dd.digest.h[i] =
222 cpu_to_le32 ( context->ddd.dd.digest.h[i] +
223 u.ddd.dd.digest.h[i] );
224 }
225
226 DBGC ( context, "MD5 digested:\n" );
227 DBGC_HDA ( context, 0, &context->ddd.dd.digest,
228 sizeof ( context->ddd.dd.digest ) );
229}
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 md5_step md5_steps[4]
MD5 steps.
Definition md5.c:132
static const uint32_t k[64]
MD5 constants.
Definition md5.c:54
void step(void)
Single-step a single process.
Definition process.c:99
union md5_block data
Accumulated data.
Definition md5.h:46
An MD5 step function.
Definition md5.c:117
uint32_t(* f)(struct md5_variables *v)
Calculate f(b,c,d)
Definition md5.c:124
MD5 variables.
Definition md5.c:42
uint32_t w[16]
Definition md5.c:50
uint32_t a
Definition md5.c:46
MD5 digest and data block.
Definition md5.h:50
uint32_t dword[sizeof(struct md5_digest_data)/sizeof(uint32_t)]
Raw dwords.
Definition md5.h:55
static u32 rol32(u32 v, int bits)
Rotate 32-bit value left.
Definition wpa_tkip.c:174

References md5_variables::a, assert, md5_variables::b, build_assert, md5_variables::c, 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, k, le32_to_cpus, md5_context::len, md5_steps, r, rol32(), step(), u, and md5_variables::w.

Referenced by md5_update().

◆ md5_update()

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.

238 {
239 struct md5_context *context = ctx;
240 const uint8_t *byte = data;
241 size_t offset;
242
243 /* Accumulate data a byte at a time, performing the digest
244 * whenever we fill the data buffer
245 */
246 while ( len-- ) {
247 offset = ( context->len % sizeof ( context->ddd.dd.data ) );
248 context->ddd.dd.data.byte[offset] = *(byte++);
249 context->len++;
250 if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
251 md5_digest ( context );
252 }
253}
unsigned char uint8_t
Definition stdint.h:10
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 md5_digest(struct md5_context *context)
Calculate MD5 digest of accumulated data.
Definition md5.c:163
uint8_t byte[64]
Raw bytes.
Definition md5.h:25

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

Referenced by md5_final().

◆ md5_final()

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

Generate MD5 digest.

Parameters
ctxMD5 context
outOutput buffer

Definition at line 261 of file md5.c.

261 {
262 struct md5_context *context = ctx;
263 uint64_t len_bits;
264 uint8_t pad;
265
266 /* Record length before pre-processing */
267 len_bits = cpu_to_le64 ( ( ( uint64_t ) context->len ) * 8 );
268
269 /* Pad with a single "1" bit followed by as many "0" bits as required */
270 pad = 0x80;
271 do {
272 md5_update ( ctx, &pad, sizeof ( pad ) );
273 pad = 0x00;
274 } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
275 offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
276
277 /* Append length (in bits) */
278 md5_update ( ctx, &len_bits, sizeof ( len_bits ) );
279 assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
280
281 /* Copy out final digest */
282 memcpy ( out, &context->ddd.dd.digest,
283 sizeof ( context->ddd.dd.digest ) );
284}
__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 md5_update(void *ctx, const void *data, size_t len)
Accumulate data with MD5 algorithm.
Definition md5.c:238
#define offsetof(type, field)
Get offset of a field within a structure.
Definition stddef.h:25

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

Variable Documentation

◆ k

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.

54 {
55 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a,
56 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
57 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340,
58 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
59 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8,
60 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
61 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa,
62 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
63 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92,
64 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
65 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
66};

Referenced by ath9k_adjust_pdadc_values(), ath9k_change_gain_boundary_setting(), ath9k_hw_fill_vpd_table(), ath9k_hw_get_gain_boundaries_pdadcs(), atl_hw_reset(), atl_hw_reset_flb_(), atl_hw_reset_rbl_(), ecdsa_alloc(), ecdsa_sign_rs(), md5_digest(), sha1_digest(), sha256_digest(), sha512_digest(), and vxgetlink().

◆ r

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.

69 {
70 { 7, 12, 17, 22 },
71 { 5, 9, 14, 20 },
72 { 4, 11, 16, 23 },
73 { 6, 10, 15, 21 },
74};

◆ md5_steps

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 },
}
static uint32_t md5_f_0_15(struct md5_variables *v)
f(b,c,d) for steps 0 to 15
Definition md5.c:82
static uint32_t md5_f_32_47(struct md5_variables *v)
f(b,c,d) for steps 32 to 47
Definition md5.c:102
static uint32_t md5_f_48_63(struct md5_variables *v)
f(b,c,d) for steps 48 to 63
Definition md5.c:112
static uint32_t md5_f_16_31(struct md5_variables *v)
f(b,c,d) for steps 16 to 31
Definition md5.c:92

MD5 steps.

Definition at line 132 of file md5.c.

132 {
133 /** 0 to 15 */
134 { .f = md5_f_0_15, .coefficient = 1, .constant = 0 },
135 /** 16 to 31 */
136 { .f = md5_f_16_31, .coefficient = 5, .constant = 1 },
137 /** 32 to 47 */
138 { .f = md5_f_32_47, .coefficient = 3, .constant = 5 },
139 /** 48 to 63 */
140 { .f = md5_f_48_63, .coefficient = 7, .constant = 0 },
141};

Referenced by md5_digest().

◆ md5_algorithm

struct digest_algorithm md5_algorithm
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,
}
static void md5_final(void *ctx, void *out)
Generate MD5 digest.
Definition md5.c:261
static void md5_init(void *ctx)
Initialise MD5 algorithm.
Definition md5.c:148
uint32_t digestsize
Digest size (i.e.
Definition pccrr.h:1
An MD5 digest.
Definition md5.h:17
An MD5 data block.
Definition md5.h:23

MD5 algorithm.

Definition at line 287 of file md5.c.

287 {
288 .name = "md5",
289 .ctxsize = sizeof ( struct md5_context ),
290 .blocksize = sizeof ( union md5_block ),
291 .digestsize = sizeof ( struct md5_digest ),
292 .init = md5_init,
293 .update = md5_update,
294 .final = md5_final,
295};

Referenced by dbg_md5_da(), DIGEST_TEST(), DIGEST_TEST(), DIGEST_TEST(), eap_rx_md5(), HMAC_TEST(), 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(), PUBKEY_SIGN_TEST(), tkip_kie_mic(), and tls_prf().