iPXE
md4.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 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  * MD4 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/md4.h>
39 
40 /** MD4 variables */
41 struct md4_variables {
42  /* This layout matches that of struct md4_digest_data,
43  * allowing for efficient endianness-conversion,
44  */
49  uint32_t w[16];
50 } __attribute__ (( packed ));
51 
52 /** MD4 shift amounts */
53 static const uint8_t r[3][4] = {
54  { 3, 7, 11, 19 },
55  { 3, 5, 9, 13 },
56  { 3, 9, 11, 15 },
57 };
58 
59 /**
60  * f(b,c,d,w) for steps 0 to 15
61  *
62  * @v v MD4 variables
63  * @v i Index within round
64  * @ret f f(b,c,d,w)
65  */
66 static uint32_t md4_f_0_15 ( struct md4_variables *v, unsigned int i ) {
67  return ( ( ( v->b & v->c ) | ( ~v->b & v->d ) ) + v->w[i] );
68 }
69 
70 /**
71  * f(b,c,d,w) for steps 16 to 31
72  *
73  * @v v MD4 variables
74  * @v i Index within round
75  * @ret f f(b,c,d,w)
76  */
77 static uint32_t md4_f_16_31 ( struct md4_variables *v, unsigned int i ) {
78  return ( ( ( v->b & v->c ) | ( v->b & v->d ) | ( v->c & v->d ) ) +
79  v->w[ ( ( i << 2 ) | ( i >> 2 ) ) % 16 ] );
80 }
81 
82 /**
83  * f(b,c,d,w) for steps 32 to 47
84  *
85  * @v v MD4 variables
86  * @v i Index within round
87  * @ret f f(b,c,d,w)
88  */
89 static uint32_t md4_f_32_47 ( struct md4_variables *v, unsigned int i ) {
90  static const uint8_t reverse[16] = {
91  0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15
92  };
93  return ( ( v->b ^ v->c ^ v->d ) + v->w[reverse[i]] );
94 }
95 
96 /** An MD4 step function */
97 struct md4_step {
98  /**
99  * Calculate f(b,c,d,w)
100  *
101  * @v v MD4 variables
102  * @v i Index within round
103  * @ret f f(b,c,d,w)
104  */
105  uint32_t ( * f ) ( struct md4_variables *v, unsigned int i );
106  /** Constant */
108 };
109 
110 /** MD4 steps */
111 static struct md4_step md4_steps[4] = {
112  /** 0 to 15 */
113  { .f = md4_f_0_15, .constant = 0x00000000UL },
114  /** 16 to 31 */
115  { .f = md4_f_16_31, .constant = 0x5a827999UL },
116  /** 32 to 47 */
117  { .f = md4_f_32_47, .constant = 0x6ed9eba1UL },
118 };
119 
120 /**
121  * Initialise MD4 algorithm
122  *
123  * @v ctx MD4 context
124  */
125 static void md4_init ( void *ctx ) {
126  struct md4_context *context = ctx;
127 
128  context->ddd.dd.digest.h[0] = cpu_to_le32 ( 0x67452301 );
129  context->ddd.dd.digest.h[1] = cpu_to_le32 ( 0xefcdab89 );
130  context->ddd.dd.digest.h[2] = cpu_to_le32 ( 0x98badcfe );
131  context->ddd.dd.digest.h[3] = cpu_to_le32 ( 0x10325476 );
132  context->len = 0;
133 }
134 
135 /**
136  * Calculate MD4 digest of accumulated data
137  *
138  * @v context MD4 context
139  */
140 static void md4_digest ( struct md4_context *context ) {
141  union {
143  struct md4_variables v;
144  } u;
145  uint32_t *a = &u.v.a;
146  uint32_t *b = &u.v.b;
147  uint32_t *c = &u.v.c;
148  uint32_t *d = &u.v.d;
149  uint32_t *w = u.v.w;
150  uint32_t f;
151  uint32_t temp;
152  struct md4_step *step;
153  unsigned int round;
154  unsigned int i;
155 
156  /* Sanity checks */
157  assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
158  build_assert ( &u.ddd.dd.digest.h[0] == a );
159  build_assert ( &u.ddd.dd.digest.h[1] == b );
160  build_assert ( &u.ddd.dd.digest.h[2] == c );
161  build_assert ( &u.ddd.dd.digest.h[3] == d );
162  build_assert ( &u.ddd.dd.data.dword[0] == w );
163 
164  DBGC ( context, "MD4 digesting:\n" );
165  DBGC_HDA ( context, 0, &context->ddd.dd.digest,
166  sizeof ( context->ddd.dd.digest ) );
167  DBGC_HDA ( context, context->len, &context->ddd.dd.data,
168  sizeof ( context->ddd.dd.data ) );
169 
170  /* Convert h[0..3] to host-endian, and initialise a, b, c, d,
171  * and x[0..15]
172  */
173  for ( i = 0 ; i < ( sizeof ( u.ddd.dword ) /
174  sizeof ( u.ddd.dword[0] ) ) ; i++ ) {
175  le32_to_cpus ( &context->ddd.dword[i] );
176  u.ddd.dword[i] = context->ddd.dword[i];
177  }
178 
179  /* Main loop */
180  for ( i = 0 ; i < 48 ; i++ ) {
181  round = ( i / 16 );
182  step = &md4_steps[round];
183  f = step->f ( &u.v, ( i % 16 ) );
184  temp = *d;
185  *d = *c;
186  *c = *b;
187  *b = rol32 ( ( *a + f + step->constant ), r[round][ i % 4 ] );
188  *a = temp;
189  DBGC2 ( context, "%2d : %08x %08x %08x %08x\n",
190  i, *a, *b, *c, *d );
191  }
192 
193  /* Add chunk to hash and convert back to little-endian */
194  for ( i = 0 ; i < 4 ; i++ ) {
195  context->ddd.dd.digest.h[i] =
196  cpu_to_le32 ( context->ddd.dd.digest.h[i] +
197  u.ddd.dd.digest.h[i] );
198  }
199 
200  DBGC ( context, "MD4 digested:\n" );
201  DBGC_HDA ( context, 0, &context->ddd.dd.digest,
202  sizeof ( context->ddd.dd.digest ) );
203 }
204 
205 /**
206  * Accumulate data with MD4 algorithm
207  *
208  * @v ctx MD4 context
209  * @v data Data
210  * @v len Length of data
211  */
212 static void md4_update ( void *ctx, const void *data, size_t len ) {
213  struct md4_context *context = ctx;
214  const uint8_t *byte = data;
215  size_t offset;
216 
217  /* Accumulate data a byte at a time, performing the digest
218  * whenever we fill the data buffer
219  */
220  while ( len-- ) {
221  offset = ( context->len % sizeof ( context->ddd.dd.data ) );
222  context->ddd.dd.data.byte[offset] = *(byte++);
223  context->len++;
224  if ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 )
225  md4_digest ( context );
226  }
227 }
228 
229 /**
230  * Generate MD4 digest
231  *
232  * @v ctx MD4 context
233  * @v out Output buffer
234  */
235 static void md4_final ( void *ctx, void *out ) {
236  struct md4_context *context = ctx;
237  uint64_t len_bits;
238  uint8_t pad;
239 
240  /* Record length before pre-processing */
241  len_bits = cpu_to_le64 ( ( ( uint64_t ) context->len ) * 8 );
242 
243  /* Pad with a single "1" bit followed by as many "0" bits as required */
244  pad = 0x80;
245  do {
246  md4_update ( ctx, &pad, sizeof ( pad ) );
247  pad = 0x00;
248  } while ( ( context->len % sizeof ( context->ddd.dd.data ) ) !=
249  offsetof ( typeof ( context->ddd.dd.data ), final.len ) );
250 
251  /* Append length (in bits) */
252  md4_update ( ctx, &len_bits, sizeof ( len_bits ) );
253  assert ( ( context->len % sizeof ( context->ddd.dd.data ) ) == 0 );
254 
255  /* Copy out final digest */
256  memcpy ( out, &context->ddd.dd.digest,
257  sizeof ( context->ddd.dd.digest ) );
258 }
259 
260 /** MD4 algorithm */
262  .name = "md4",
263  .ctxsize = sizeof ( struct md4_context ),
264  .blocksize = sizeof ( union md4_block ),
265  .digestsize = sizeof ( struct md4_digest ),
266  .init = md4_init,
267  .update = md4_update,
268  .final = md4_final,
269 };
static void md4_init(void *ctx)
Initialise MD4 algorithm.
Definition: md4.c:125
uint32_t c
Definition: md4.c:30
uint32_t a
Definition: md4.c:45
uint32_t constant
Constant.
Definition: md4.c:107
uint32_t d
Definition: md4.c:48
static struct md4_step md4_steps[4]
MD4 steps.
Definition: md4.c:111
static u32 rol32(u32 v, int bits)
Rotate 32-bit value left.
Definition: wpa_tkip.c:173
#define DBGC(...)
Definition: compiler.h:505
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
struct md4_digest_data dd
Digest and data block.
Definition: md4.h:51
unsigned long long uint64_t
Definition: stdint.h:13
#define cpu_to_le64(value)
Definition: byteswap.h:108
Cryptographic API.
#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
uint32_t a
Definition: md4.c:28
__be32 out[4]
Definition: CIB_PRM.h:36
uint32_t c
Definition: md4.c:47
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:66
uint32_t w[16]
Definition: md4.c:49
void * memcpy(void *dest, const void *src, size_t len) __nonnull
uint32_t b
Definition: md4.c:46
uint32_t h[4]
Hash output.
Definition: md4.h:18
struct md4_step __attribute__
Assertions.
MD4 variables.
Definition: md4.c:41
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define DBGC_HDA(...)
Definition: compiler.h:506
static void md4_update(void *ctx, const void *data, size_t len)
Accumulate data with MD4 algorithm.
Definition: md4.c:212
union md4_block data
Accumulated data.
Definition: md4.h:45
#define build_assert(condition)
Assert a condition at build time (after dead code elimination)
Definition: assert.h:76
static void md4_final(void *ctx, void *out)
Generate MD4 digest.
Definition: md4.c:235
static userptr_t size_t offset
Offset of the first segment within the content.
Definition: deflate.h:259
size_t len
Amount of accumulated data.
Definition: md4.h:60
#define cpu_to_le32(value)
Definition: byteswap.h:107
uint8_t byte[64]
Raw bytes.
Definition: md4.h:24
An MD4 context.
Definition: md4.h:58
An MD4 data block.
Definition: md4.h:22
union md4_digest_data_dwords ddd
Digest and accumulated data.
Definition: md4.h:62
An MD4 digest.
Definition: md4.h:16
struct golan_eq_context ctx
Definition: CIB_PRM.h:28
unsigned char uint8_t
Definition: stdint.h:10
An MD4 step function.
Definition: md4.c:97
unsigned int uint32_t
Definition: stdint.h:12
uint32_t w[16]
Definition: md4.c:32
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:89
MD4 digest and data block.
Definition: md4.h:49
uint32_t(* f)(struct md4_variables *v, unsigned int i)
Calculate f(b,c,d,w)
Definition: md4.c:105
uint32_t len
Length.
Definition: ena.h:14
#define DBGC2(...)
Definition: compiler.h:522
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:77
const char * name
Algorithm name.
Definition: crypto.h:19
void step(void)
Single-step a single process.
Definition: process.c:98
uint32_t b
Definition: md4.c:29
uint32_t d
Definition: md4.c:31
A message digest algorithm.
Definition: crypto.h:17
union @17 u
uint8_t data[48]
Additional event data.
Definition: ena.h:22
uint32_t dword[sizeof(struct md4_digest_data)/sizeof(uint32_t)]
Raw dwords.
Definition: md4.h:54
uint32_t f
Definition: sha256.c:33
struct digest_algorithm md4_algorithm
MD4 algorithm.
Definition: md4.c:261
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition: acpi.c:45
uint32_t digestsize
Digest size (i.e.
Definition: pccrr.h:14
static void md4_digest(struct md4_context *context)
Calculate MD4 digest of accumulated data.
Definition: md4.c:140
MD4 algorithm.
String functions.
struct md4_digest digest
Digest of data already processed.
Definition: md4.h:43
Bit operations.
#define le32_to_cpus(ptr)
Definition: byteswap.h:125
static const uint8_t r[3][4]
MD4 shift amounts.
Definition: md4.c:53