iPXE
md5.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /** @file
27  *
28  * MD5 algorithm
29  *
30  */
31 
32 #include <stdint.h>
33 #include <string.h>
34 #include <byteswap.h>
35 #include <assert.h>
36 #include <ipxe/rotate.h>
37 #include <ipxe/crypto.h>
38 #include <ipxe/md5.h>
39 
40 /** MD5 variables */
41 struct md5_variables {
42  /* This layout matches that of struct md5_digest_data,
43  * allowing for efficient endianness-conversion,
44  */
49  uint32_t w[16];
50 } __attribute__ (( packed ));
51 
52 /** MD5 constants */
53 static const uint32_t k[64] = {
54  0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a,
55  0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
56  0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340,
57  0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
58  0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8,
59  0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
60  0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa,
61  0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
62  0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92,
63  0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
64  0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
65 };
66 
67 /** MD5 shift amounts */
68 static const uint8_t r[4][4] = {
69  { 7, 12, 17, 22 },
70  { 5, 9, 14, 20 },
71  { 4, 11, 16, 23 },
72  { 6, 10, 15, 21 },
73 };
74 
75 /**
76  * f(b,c,d) for steps 0 to 15
77  *
78  * @v v MD5 variables
79  * @ret f f(b,c,d)
80  */
81 static uint32_t md5_f_0_15 ( struct md5_variables *v ) {
82  return ( v->d ^ ( v->b & ( v->c ^ v->d ) ) );
83 }
84 
85 /**
86  * f(b,c,d) for steps 16 to 31
87  *
88  * @v v MD5 variables
89  * @ret f f(b,c,d)
90  */
91 static uint32_t md5_f_16_31 ( struct md5_variables *v ) {
92  return ( v->c ^ ( v->d & ( v->b ^ v->c ) ) );
93 }
94 
95 /**
96  * f(b,c,d) for steps 32 to 47
97  *
98  * @v v MD5 variables
99  * @ret f f(b,c,d)
100  */
101 static uint32_t md5_f_32_47 ( struct md5_variables *v ) {
102  return ( v->b ^ v->c ^ v->d );
103 }
104 
105 /**
106  * f(b,c,d) for steps 48 to 63
107  *
108  * @v v MD5 variables
109  * @ret f f(b,c,d)
110  */
111 static uint32_t md5_f_48_63 ( struct md5_variables *v ) {
112  return ( v->c ^ ( v->b | (~v->d) ) );
113 }
114 
115 /** An MD5 step function */
116 struct md5_step {
117  /**
118  * Calculate f(b,c,d)
119  *
120  * @v v MD5 variables
121  * @ret f f(b,c,d)
122  */
123  uint32_t ( * f ) ( struct md5_variables *v );
124  /** Coefficient of i in g=ni+m */
126  /** Constant term in g=ni+m */
128 };
129 
130 /** MD5 steps */
131 static struct md5_step md5_steps[4] = {
132  /** 0 to 15 */
133  { .f = md5_f_0_15, .coefficient = 1, .constant = 0 },
134  /** 16 to 31 */
135  { .f = md5_f_16_31, .coefficient = 5, .constant = 1 },
136  /** 32 to 47 */
137  { .f = md5_f_32_47, .coefficient = 3, .constant = 5 },
138  /** 48 to 63 */
139  { .f = md5_f_48_63, .coefficient = 7, .constant = 0 },
140 };
141 
142 /**
143  * Initialise MD5 algorithm
144  *
145  * @v ctx MD5 context
146  */
147 static void md5_init ( void *ctx ) {
148  struct md5_context *context = ctx;
149 
150  context->ddd.dd.digest.h[0] = cpu_to_le32 ( 0x67452301 );
151  context->ddd.dd.digest.h[1] = cpu_to_le32 ( 0xefcdab89 );
152  context->ddd.dd.digest.h[2] = cpu_to_le32 ( 0x98badcfe );
153  context->ddd.dd.digest.h[3] = cpu_to_le32 ( 0x10325476 );
154  context->len = 0;
155 }
156 
157 /**
158  * Calculate MD5 digest of accumulated data
159  *
160  * @v context MD5 context
161  */
162 static void md5_digest ( struct md5_context *context ) {
163  union {
165  struct md5_variables v;
166  } u;
167  uint32_t *a = &u.v.a;
168  uint32_t *b = &u.v.b;
169  uint32_t *c = &u.v.c;
170  uint32_t *d = &u.v.d;
171  uint32_t *w = u.v.w;
172  uint32_t f;
173  uint32_t g;
174  uint32_t temp;
175  struct md5_step *step;
176  unsigned int round;
177  unsigned int i;
178 
179  /* Sanity checks */
180  assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
181  build_assert ( &u.ddd.dd.digest.h[0] == a );
182  build_assert ( &u.ddd.dd.digest.h[1] == b );
183  build_assert ( &u.ddd.dd.digest.h[2] == c );
184  build_assert ( &u.ddd.dd.digest.h[3] == d );
185  build_assert ( &u.ddd.dd.data.dword[0] == w );
186 
187  DBGC ( context, "MD5 digesting:\n" );
188  DBGC_HDA ( context, 0, &context->ddd.dd.digest,
189  sizeof ( context->ddd.dd.digest ) );
190  DBGC_HDA ( context, context->len, &context->ddd.dd.data,
191  sizeof ( context->ddd.dd.data ) );
192 
193  /* Convert h[0..3] to host-endian, and initialise a, b, c, d,
194  * and w[0..15]
195  */
196  for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
197  sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
198  le32_to_cpus ( &context->ddd.dword[i] );
199  u.ddd.dword[i] = context->ddd.dword[i];
200  }
201 
202  /* Main loop */
203  for ( i = 0 ; i < 64 ; i++ ) {
204  round = ( i / 16 );
205  step = &md5_steps[round];
206  f = step->f ( &u.v );
207  g = ( ( ( step->coefficient * i ) + step->constant ) % 16 );
208  temp = *d;
209  *d = *c;
210  *c = *b;
211  *b = ( *b + rol32 ( ( *a + f + k[i] + w[g] ),
212  r[round][ i % 4 ] ) );
213  *a = temp;
214  DBGC2 ( context, "%2d : %08x %08x %08x %08x\n",
215  i, *a, *b, *c, *d );
216  }
217 
218  /* Add chunk to hash and convert back to little-endian */
219  for ( i = 0 ; i < 4 ; i++ ) {
220  context->ddd.dd.digest.h[i] =
221  cpu_to_le32 ( context->ddd.dd.digest.h[i] +
222  u.ddd.dd.digest.h[i] );
223  }
224 
225  DBGC ( context, "MD5 digested:\n" );
226  DBGC_HDA ( context, 0, &context->ddd.dd.digest,
227  sizeof ( context->ddd.dd.digest ) );
228 }
229 
230 /**
231  * Accumulate data with MD5 algorithm
232  *
233  * @v ctx MD5 context
234  * @v data Data
235  * @v len Length of data
236  */
237 static void md5_update ( void *ctx, const void *data, size_t len ) {
238  struct md5_context *context = ctx;
239  const uint8_t *byte = data;
240  size_t offset;
241 
242  /* Accumulate data a byte at a time, performing the digest
243  * whenever we fill the data buffer
244  */
245  while ( len-- ) {
246  offset = ( context->len % sizeof ( context->ddd.dd.data ) );
247  context->ddd.dd.data.byte[offset] = *(byte++);
248  context->len++;
249  if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
250  md5_digest ( context );
251  }
252 }
253 
254 /**
255  * Generate MD5 digest
256  *
257  * @v ctx MD5 context
258  * @v out Output buffer
259  */
260 static void md5_final ( void *ctx, void *out ) {
261  struct md5_context *context = ctx;
262  uint64_t len_bits;
263  uint8_t pad;
264 
265  /* Record length before pre-processing */
266  len_bits = cpu_to_le64 ( ( ( uint64_t ) context->len ) * 8 );
267 
268  /* Pad with a single "1" bit followed by as many "0" bits as required */
269  pad = 0x80;
270  do {
271  md5_update ( ctx, &pad, sizeof ( pad ) );
272  pad = 0x00;
273  } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
274  offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
275 
276  /* Append length (in bits) */
277  md5_update ( ctx, &len_bits, sizeof ( len_bits ) );
278  assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
279 
280  /* Copy out final digest */
281  memcpy ( out, &context->ddd.dd.digest,
282  sizeof ( context->ddd.dd.digest ) );
283 }
284 
285 /** MD5 algorithm */
287  .name = "md5",
288  .ctxsize = sizeof ( struct md5_context ),
289  .blocksize = sizeof ( union md5_block ),
290  .digestsize = sizeof ( struct md5_digest ),
291  .init = md5_init,
292  .update = md5_update,
293  .final = md5_final,
294 };
uint32_t w[16]
Definition: md5.c:32
uint32_t c
Definition: md5.c:47
uint32_t a
Definition: md5.c:45
static void md5_init(void *ctx)
Initialise MD5 algorithm.
Definition: md5.c:147
static uint32_t md5_f_16_31(struct md5_variables *v)
f(b,c,d) for steps 16 to 31
Definition: md5.c:91
static const uint8_t r[4][4]
MD5 shift amounts.
Definition: md5.c:68
static u32 rol32(u32 v, int bits)
Rotate 32-bit value left.
Definition: wpa_tkip.c:173
An MD5 step function.
Definition: md5.c:116
uint32_t b
Definition: md5.c:29
uint32_t d
Definition: md5.c:31
uint32_t g
Definition: sha256.c:34
#define DBGC(...)
Definition: compiler.h:505
unsigned long long uint64_t
Definition: stdint.h:13
#define cpu_to_le64(value)
Definition: byteswap.h:108
Cryptographic API.
uint32_t w[16]
Definition: md5.c:49
MD5 variables.
Definition: md5.c:41
#define offsetof(type, field)
Get offset of a field within a structure.
Definition: stddef.h:24
u32 pad[9]
Padding.
Definition: ar9003_mac.h:90
An MD5 data block.
Definition: md5.h:22
uint32_t c
Definition: md5.c:30
uint32_t b
Definition: md5.c:46
MD5 digest and data block.
Definition: md5.h:49
__be32 out[4]
Definition: CIB_PRM.h:36
static uint32_t md5_f_0_15(struct md5_variables *v)
f(b,c,d) for steps 0 to 15
Definition: md5.c:81
void * memcpy(void *dest, const void *src, size_t len) __nonnull
Assertions.
static void md5_final(void *ctx, void *out)
Generate MD5 digest.
Definition: md5.c:260
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
struct md5_digest_data dd
Digest and data block.
Definition: md5.h:51
#define DBGC_HDA(...)
Definition: compiler.h:506
#define build_assert(condition)
Assert a condition at build time (after dead code elimination)
Definition: assert.h:76
static void md5_update(void *ctx, const void *data, size_t len)
Accumulate data with MD5 algorithm.
Definition: md5.c:237
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
struct md5_digest digest
Digest of data already processed.
Definition: md5.h:43
struct md5_step __attribute__
size_t len
Amount of accumulated data.
Definition: md5.h:60
#define cpu_to_le32(value)
Definition: byteswap.h:107
An MD5 digest.
Definition: md5.h:16
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
static void md5_digest(struct md5_context *context)
Calculate MD5 digest of accumulated data.
Definition: md5.c:162
An MD5 context.
Definition: md5.h:58
unsigned char uint8_t
Definition: stdint.h:10
uint32_t dword[sizeof(struct md5_digest_data)/sizeof(uint32_t)]
Raw dwords.
Definition: md5.h:54
static const uint32_t k[64]
MD5 constants.
Definition: md5.c:53
unsigned int uint32_t
Definition: stdint.h:12
static uint32_t md5_f_32_47(struct md5_variables *v)
f(b,c,d) for steps 32 to 47
Definition: md5.c:101
uint8_t byte[64]
Raw bytes.
Definition: md5.h:24
uint32_t len
Length.
Definition: ena.h:14
#define DBGC2(...)
Definition: compiler.h:522
const char * name
Algorithm name.
Definition: crypto.h:19
void step(void)
Single-step a single process.
Definition: process.c:98
uint32_t d
Definition: md5.c:48
uint32_t h[4]
Hash output.
Definition: md5.h:18
static struct md5_step md5_steps[4]
MD5 steps.
Definition: md5.c:131
A message digest algorithm.
Definition: crypto.h:17
union @17 u
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint32_t f
Definition: sha256.c:33
union md5_digest_data_dwords ddd
Digest and accumulated data.
Definition: md5.h:62
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition: acpi.c:45
uint32_t digestsize
Digest size (i.e.
Definition: pccrr.h:14
union md5_block data
Accumulated data.
Definition: md5.h:45
static uint32_t md5_f_48_63(struct md5_variables *v)
f(b,c,d) for steps 48 to 63
Definition: md5.c:111
uint8_t constant
Constant term in g=ni+m.
Definition: md5.c:127
uint32_t(* f)(struct md5_variables *v)
Calculate f(b,c,d)
Definition: md5.c:123
MD5 algorithm.
uint32_t a
Definition: md5.c:28
String functions.
uint8_t coefficient
Coefficient of i in g=ni+m.
Definition: md5.c:125
struct digest_algorithm md5_algorithm
MD5 algorithm.
Definition: md5.c:286
Bit operations.
#define le32_to_cpus(ptr)
Definition: byteswap.h:125