iPXE
md5.c File Reference

MD5 algorithm. More...

#include <stdint.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_compress (struct md5_digest_data *dd, const struct md5_digest *digest)
 Calculate MD5 digest of accumulated data.
 MDHASH_ALGORITHM (md5, md5_algorithm, md5_compress, __LITTLE_ENDIAN, struct md5_digest_data, md5_init, MD5_DIGEST_SIZE)
 MD5 algorithm.

Variables

static const uint32_t k [64]
 MD5 constants.
static const uint8_t r [4][4]
 MD5 shift amounts.
static const struct md5_step md5_steps [4]
 MD5 steps.
static const struct md5_digest md5_init
 MD5 initial digest values.

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 78 of file md5.c.

78 {
79 return ( v->d ^ ( v->b & ( v->c ^ v->d ) ) );
80}
uint32_t d
Definition md5.c:45
uint32_t b
Definition md5.c:43
uint32_t c
Definition md5.c:44

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 88 of file md5.c.

88 {
89 return ( v->c ^ ( v->d & ( v->b ^ v->c ) ) );
90}

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 98 of file md5.c.

98 {
99 return ( v->b ^ v->c ^ v->d );
100}

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 108 of file md5.c.

108 {
109 return ( v->c ^ ( v->b | (~v->d) ) );
110}

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

◆ md5_compress()

void md5_compress ( struct md5_digest_data * dd,
const struct md5_digest * digest )
static

Calculate MD5 digest of accumulated data.

Parameters
ddDigest and data block
digestCopy of current digest value

Definition at line 150 of file md5.c.

151 {
152 union {
153 struct md5_digest_data dd;
154 struct md5_variables v;
155 } *u = container_of ( dd, typeof ( *u ), dd );
156 struct md5_variables *v = &u->v;
157 const struct md5_step *step;
158 uint32_t *a = &v->a;
159 uint32_t *b = &v->b;
160 uint32_t *c = &v->c;
161 uint32_t *d = &v->d;
162 uint32_t *w = v->w;
163 uint32_t f;
164 uint32_t g;
165 uint32_t temp;
166 unsigned int round;
167 unsigned int i;
168
169 /* Sanity checks */
170 build_assert ( &u->dd.digest.h[0] == a );
171 build_assert ( &u->dd.digest.h[1] == b );
172 build_assert ( &u->dd.digest.h[2] == c );
173 build_assert ( &u->dd.digest.h[3] == d );
174 build_assert ( &u->dd.data.dword[0] == w );
175 build_assert ( sizeof ( u->dd ) == sizeof ( u->v ) );
176
177 /* Main loop */
178 for ( i = 0 ; i < 64 ; i++ ) {
179 round = ( i / 16 );
180 step = &md5_steps[round];
181 f = step->f ( v );
182 g = ( ( ( step->coefficient * i ) + step->constant ) % 16 );
183 temp = *d;
184 *d = *c;
185 *c = *b;
186 *b = ( *b + rol32 ( ( *a + f + k[i] + w[g] ),
187 r[round][ i % 4 ] ) );
188 *a = temp;
189 DBGC2 ( &md5_algorithm, "%2d : %08x %08x %08x %08x\n",
190 i, *a, *b, *c, *d );
191 }
192
193 /* Add chunk to hash */
194 for ( i = 0 ; i < 4 ; i++ )
195 dd->digest.h[i] += digest->h[i];
196}
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition acpi.c:48
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
union @104331263140136355135267063077374276003064103115 u
#define DBGC2(...)
Definition compiler.h:547
static const uint8_t r[3][4]
MD4 shift amounts.
Definition md4.c:50
static const struct md5_step md5_steps[4]
MD5 steps.
Definition md5.c:128
static const uint32_t k[64]
MD5 constants.
Definition md5.c:50
struct digest_algorithm md5_algorithm
void step(void)
Single-step a single process.
Definition process.c:99
#define container_of(ptr, type, field)
Get containing structure.
Definition stddef.h:36
MD5 digest and data block.
Definition md5.h:43
struct md5_digest digest
Digest of data already processed.
Definition md5.h:45
uint32_t h[4]
Hash output.
Definition md5.h:20
An MD5 step function.
Definition md5.c:113
uint32_t(* f)(struct md5_variables *v)
Calculate f(b,c,d).
Definition md5.c:120
MD5 variables.
Definition md5.c:40
uint32_t w[16]
Definition md5.c:46
uint32_t a
Definition md5.c:42
static u32 rol32(u32 v, int bits)
Rotate 32-bit value left.
Definition wpa_tkip.c:174

References md5_variables::a, md5_variables::b, build_assert, md5_variables::c, container_of, md5_variables::d, DBGC2, md5_digest_data::digest, md5_step::f, md5_digest::h, k, md5_algorithm, md5_steps, r, rol32(), step(), typeof(), u, and md5_variables::w.

Referenced by MDHASH_ALGORITHM().

◆ MDHASH_ALGORITHM()

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 50 of file md5.c.

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

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_compress(), sha1_compress(), sha256_compress(), sha512_compress(), 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 65 of file md5.c.

65 {
66 { 7, 12, 17, 22 },
67 { 5, 9, 14, 20 },
68 { 4, 11, 16, 23 },
69 { 6, 10, 15, 21 },
70};

◆ md5_steps

const 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:78
static uint32_t md5_f_32_47(struct md5_variables *v)
f(b,c,d) for steps 32 to 47
Definition md5.c:98
static uint32_t md5_f_48_63(struct md5_variables *v)
f(b,c,d) for steps 48 to 63
Definition md5.c:108
static uint32_t md5_f_16_31(struct md5_variables *v)
f(b,c,d) for steps 16 to 31
Definition md5.c:88

MD5 steps.

Definition at line 128 of file md5.c.

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

Referenced by md5_compress().

◆ md5_init

const struct md5_digest md5_init
static
Initial value:
= {
.h = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 }
}

MD5 initial digest values.

Definition at line 140 of file md5.c.

140 {
141 .h = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 }
142};

Referenced by MDHASH_ALGORITHM().