1
/* This file was automatically imported with
2
import_gcry.py. Please don't modify it */
3
/* md4.c - MD4 Message-Digest Algorithm
4
* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
6
* This file is part of Libgcrypt.
8
* Libgcrypt is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU Lesser General Public License as
10
* published by the Free Software Foundation; either version 2.1 of
11
* the License, or (at your option) any later version.
13
* Libgcrypt is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this program; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22
* Based on md5.c in libgcrypt, but rewritten to compute md4 checksums
23
* using a public domain md4 implementation with the following comments:
25
* Modified by Wei Dai from Andrew M. Kuchling's md4.c
26
* The original code and all modifications are in the public domain.
28
* This is the original introductory comment:
30
* md4.c : MD4 hash algorithm.
32
* Part of the Python Cryptography Toolkit, version 1.1
34
* Distribute and use freely; there are no restrictions on further
35
* dissemination and usage except those imposed by the laws of your
36
* country of residence.
41
* MD4 ("") = 31d6cfe0d16ae931b73c59d7e0c089c0
42
* MD4 ("a") = bde52cb31de33e46245e05fbdbd6fb24
43
* MD4 ("abc") = a448017aaf21d8525fc10ae87aa6729d
44
* MD4 ("message digest") = d9130a8164549fe818874806e1c7014b
45
* MD4 ("abcdefghijklmnopqrstuvwxyz") = d79e1c308aa5bbcdeea8ed63df412da9
46
* MD4 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
47
* 043f8582f241db351ce627e153e7f0e4
48
* MD4 ("123456789012345678901234567890123456789012345678901234567890123456
49
* 78901234567890") = e33b4ddc9c38f2199c3e7b164fcc0536
61
u32 A,B,C,D; /* chaining variables */
69
md4_init( void *context )
71
MD4_CONTEXT *ctx = context;
82
#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
83
#define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
84
#define H(x, y, z) ((x) ^ (y) ^ (z))
91
transform ( MD4_CONTEXT *ctx, const unsigned char *data )
94
register u32 A = ctx->A;
95
register u32 B = ctx->B;
96
register u32 C = ctx->C;
97
register u32 D = ctx->D;
99
#ifdef WORDS_BIGENDIAN
103
for(i=0, p1=data, p2=(byte*)in; i < 16; i++, p2 += 4 )
112
memcpy (in, data, 64);
116
#define function(a,b,c,d,k,s) a=rol(a+F(b,c,d)+in[k],s);
117
function(A,B,C,D, 0, 3);
118
function(D,A,B,C, 1, 7);
119
function(C,D,A,B, 2,11);
120
function(B,C,D,A, 3,19);
121
function(A,B,C,D, 4, 3);
122
function(D,A,B,C, 5, 7);
123
function(C,D,A,B, 6,11);
124
function(B,C,D,A, 7,19);
125
function(A,B,C,D, 8, 3);
126
function(D,A,B,C, 9, 7);
127
function(C,D,A,B,10,11);
128
function(B,C,D,A,11,19);
129
function(A,B,C,D,12, 3);
130
function(D,A,B,C,13, 7);
131
function(C,D,A,B,14,11);
132
function(B,C,D,A,15,19);
137
#define function(a,b,c,d,k,s) a=rol(a+G(b,c,d)+in[k]+0x5a827999,s);
139
function(A,B,C,D, 0, 3);
140
function(D,A,B,C, 4, 5);
141
function(C,D,A,B, 8, 9);
142
function(B,C,D,A,12,13);
143
function(A,B,C,D, 1, 3);
144
function(D,A,B,C, 5, 5);
145
function(C,D,A,B, 9, 9);
146
function(B,C,D,A,13,13);
147
function(A,B,C,D, 2, 3);
148
function(D,A,B,C, 6, 5);
149
function(C,D,A,B,10, 9);
150
function(B,C,D,A,14,13);
151
function(A,B,C,D, 3, 3);
152
function(D,A,B,C, 7, 5);
153
function(C,D,A,B,11, 9);
154
function(B,C,D,A,15,13);
159
#define function(a,b,c,d,k,s) a=rol(a+H(b,c,d)+in[k]+0x6ed9eba1,s);
161
function(A,B,C,D, 0, 3);
162
function(D,A,B,C, 8, 9);
163
function(C,D,A,B, 4,11);
164
function(B,C,D,A,12,15);
165
function(A,B,C,D, 2, 3);
166
function(D,A,B,C,10, 9);
167
function(C,D,A,B, 6,11);
168
function(B,C,D,A,14,15);
169
function(A,B,C,D, 1, 3);
170
function(D,A,B,C, 9, 9);
171
function(C,D,A,B, 5,11);
172
function(B,C,D,A,13,15);
173
function(A,B,C,D, 3, 3);
174
function(D,A,B,C,11, 9);
175
function(C,D,A,B, 7,11);
176
function(B,C,D,A,15,15);
179
/* Put checksum in context given as argument. */
188
/* The routine updates the message-digest context to
189
* account for the presence of each of the characters inBuf[0..inLen-1]
190
* in the message whose digest is being computed.
193
md4_write ( void *context, const void *inbuf_arg, size_t inlen)
195
const unsigned char *inbuf = inbuf_arg;
196
MD4_CONTEXT *hd = context;
198
if( hd->count == 64 ) /* flush the buffer */
200
transform( hd, hd->buf );
201
_gcry_burn_stack (80+6*sizeof(void*));
210
for( ; inlen && hd->count < 64; inlen-- )
211
hd->buf[hd->count++] = *inbuf++;
212
md4_write( hd, NULL, 0 );
216
_gcry_burn_stack (80+6*sizeof(void*));
220
transform( hd, inbuf );
226
for( ; inlen && hd->count < 64; inlen-- )
227
hd->buf[hd->count++] = *inbuf++;
232
/* The routine final terminates the message-digest computation and
233
* ends with the desired message digest in mdContext->digest[0...15].
234
* The handle is prepared for a new MD4 cycle.
235
* Returns 16 bytes representing the digest.
239
md4_final( void *context )
241
MD4_CONTEXT *hd = context;
245
md4_write(hd, NULL, 0); /* flush */;
248
/* multiply by 64 to make a byte count */
253
if( (lsb += hd->count) < t )
255
/* multiply by 8 to make a bit count */
261
if( hd->count < 56 ) /* enough room */
263
hd->buf[hd->count++] = 0x80; /* pad */
264
while( hd->count < 56 )
265
hd->buf[hd->count++] = 0; /* pad */
267
else /* need one extra block */
269
hd->buf[hd->count++] = 0x80; /* pad character */
270
while( hd->count < 64 )
271
hd->buf[hd->count++] = 0;
272
md4_write(hd, NULL, 0); /* flush */;
273
memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
275
/* append the 64 bit count */
277
hd->buf[57] = lsb >> 8;
278
hd->buf[58] = lsb >> 16;
279
hd->buf[59] = lsb >> 24;
281
hd->buf[61] = msb >> 8;
282
hd->buf[62] = msb >> 16;
283
hd->buf[63] = msb >> 24;
284
transform( hd, hd->buf );
285
_gcry_burn_stack (80+6*sizeof(void*));
288
#ifdef WORDS_BIGENDIAN
289
#define X(a) do { *p++ = hd->a ; *p++ = hd->a >> 8; \
290
*p++ = hd->a >> 16; *p++ = hd->a >> 24; } while(0)
291
#else /* little endian */
292
#define X(a) do { *(u32*)p = (*hd).a ; p += 4; } while(0)
303
md4_read (void *context)
305
MD4_CONTEXT *hd = context;
309
static byte asn[18] = /* Object ID is 1.2.840.113549.2.4 */
310
{ 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86,0x48,
311
0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 };
313
static gcry_md_oid_spec_t oid_spec_md4[] =
315
/* iso.member-body.us.rsadsi.digestAlgorithm.md4 */
316
{ "1.2.840.113549.2.4" },
320
gcry_md_spec_t _gcry_digest_spec_md4 =
322
"MD4", asn, DIM (asn), oid_spec_md4,16,
323
md4_init, md4_write, md4_final, md4_read,
331
GRUB_MOD_INIT(gcry_md4)
333
grub_md_register (&_gcry_digest_spec_md4);
336
GRUB_MOD_FINI(gcry_md4)
338
grub_md_unregister (&_gcry_digest_spec_md4);