Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / lib / libgcrypt-grub / cipher / md4.c
bloba112f003b36d6d2066522554dd00c0e9359e58e6
1 /* This file was automatically imported with
2 import_gcry.py. Please don't modify it */
3 #include <grub/dl.h>
4 GRUB_MOD_LICENSE ("GPLv3+");
5 /* md4.c - MD4 Message-Digest Algorithm
6 * Copyright (C) 2002, 2003 Free Software Foundation, Inc.
8 * This file is part of Libgcrypt.
10 * Libgcrypt is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2.1 of
13 * the License, or (at your option) any later version.
15 * Libgcrypt is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
24 * Based on md5.c in libgcrypt, but rewritten to compute md4 checksums
25 * using a public domain md4 implementation with the following comments:
27 * Modified by Wei Dai from Andrew M. Kuchling's md4.c
28 * The original code and all modifications are in the public domain.
30 * This is the original introductory comment:
32 * md4.c : MD4 hash algorithm.
34 * Part of the Python Cryptography Toolkit, version 1.1
36 * Distribute and use freely; there are no restrictions on further
37 * dissemination and usage except those imposed by the laws of your
38 * country of residence.
42 /* MD4 test suite:
43 * MD4 ("") = 31d6cfe0d16ae931b73c59d7e0c089c0
44 * MD4 ("a") = bde52cb31de33e46245e05fbdbd6fb24
45 * MD4 ("abc") = a448017aaf21d8525fc10ae87aa6729d
46 * MD4 ("message digest") = d9130a8164549fe818874806e1c7014b
47 * MD4 ("abcdefghijklmnopqrstuvwxyz") = d79e1c308aa5bbcdeea8ed63df412da9
48 * MD4 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") =
49 * 043f8582f241db351ce627e153e7f0e4
50 * MD4 ("123456789012345678901234567890123456789012345678901234567890123456
51 * 78901234567890") = e33b4ddc9c38f2199c3e7b164fcc0536
55 #include "g10lib.h"
56 #include "memory.h"
57 #include "cipher.h"
59 #include "bithelp.h"
62 typedef struct {
63 u32 A,B,C,D; /* chaining variables */
64 u32 nblocks;
65 byte buf[64];
66 int count;
67 } MD4_CONTEXT;
70 static void
71 md4_init( void *context )
73 MD4_CONTEXT *ctx = context;
75 ctx->A = 0x67452301;
76 ctx->B = 0xefcdab89;
77 ctx->C = 0x98badcfe;
78 ctx->D = 0x10325476;
80 ctx->nblocks = 0;
81 ctx->count = 0;
84 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
85 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
86 #define H(x, y, z) ((x) ^ (y) ^ (z))
89 /****************
90 * transform 64 bytes
92 static void
93 transform ( MD4_CONTEXT *ctx, const unsigned char *data )
95 u32 in[16];
96 register u32 A = ctx->A;
97 register u32 B = ctx->B;
98 register u32 C = ctx->C;
99 register u32 D = ctx->D;
101 #ifdef WORDS_BIGENDIAN
103 int i;
104 byte *p2;
105 const byte *p1;
106 for(i=0, p1=data, p2=(byte*)in; i < 16; i++, p2 += 4 )
108 p2[3] = *p1++;
109 p2[2] = *p1++;
110 p2[1] = *p1++;
111 p2[0] = *p1++;
114 #else
115 memcpy (in, data, 64);
116 #endif
118 /* Round 1. */
119 #define function(a,b,c,d,k,s) a=rol(a+F(b,c,d)+in[k],s);
120 function(A,B,C,D, 0, 3);
121 function(D,A,B,C, 1, 7);
122 function(C,D,A,B, 2,11);
123 function(B,C,D,A, 3,19);
124 function(A,B,C,D, 4, 3);
125 function(D,A,B,C, 5, 7);
126 function(C,D,A,B, 6,11);
127 function(B,C,D,A, 7,19);
128 function(A,B,C,D, 8, 3);
129 function(D,A,B,C, 9, 7);
130 function(C,D,A,B,10,11);
131 function(B,C,D,A,11,19);
132 function(A,B,C,D,12, 3);
133 function(D,A,B,C,13, 7);
134 function(C,D,A,B,14,11);
135 function(B,C,D,A,15,19);
137 #undef function
139 /* Round 2. */
140 #define function(a,b,c,d,k,s) a=rol(a+G(b,c,d)+in[k]+0x5a827999,s);
142 function(A,B,C,D, 0, 3);
143 function(D,A,B,C, 4, 5);
144 function(C,D,A,B, 8, 9);
145 function(B,C,D,A,12,13);
146 function(A,B,C,D, 1, 3);
147 function(D,A,B,C, 5, 5);
148 function(C,D,A,B, 9, 9);
149 function(B,C,D,A,13,13);
150 function(A,B,C,D, 2, 3);
151 function(D,A,B,C, 6, 5);
152 function(C,D,A,B,10, 9);
153 function(B,C,D,A,14,13);
154 function(A,B,C,D, 3, 3);
155 function(D,A,B,C, 7, 5);
156 function(C,D,A,B,11, 9);
157 function(B,C,D,A,15,13);
159 #undef function
161 /* Round 3. */
162 #define function(a,b,c,d,k,s) a=rol(a+H(b,c,d)+in[k]+0x6ed9eba1,s);
164 function(A,B,C,D, 0, 3);
165 function(D,A,B,C, 8, 9);
166 function(C,D,A,B, 4,11);
167 function(B,C,D,A,12,15);
168 function(A,B,C,D, 2, 3);
169 function(D,A,B,C,10, 9);
170 function(C,D,A,B, 6,11);
171 function(B,C,D,A,14,15);
172 function(A,B,C,D, 1, 3);
173 function(D,A,B,C, 9, 9);
174 function(C,D,A,B, 5,11);
175 function(B,C,D,A,13,15);
176 function(A,B,C,D, 3, 3);
177 function(D,A,B,C,11, 9);
178 function(C,D,A,B, 7,11);
179 function(B,C,D,A,15,15);
182 /* Put checksum in context given as argument. */
183 ctx->A += A;
184 ctx->B += B;
185 ctx->C += C;
186 ctx->D += D;
191 /* The routine updates the message-digest context to
192 * account for the presence of each of the characters inBuf[0..inLen-1]
193 * in the message whose digest is being computed.
195 static void
196 md4_write ( void *context, const void *inbuf_arg, size_t inlen)
198 const unsigned char *inbuf = inbuf_arg;
199 MD4_CONTEXT *hd = context;
201 if( hd->count == 64 ) /* flush the buffer */
203 transform( hd, hd->buf );
204 _gcry_burn_stack (80+6*sizeof(void*));
205 hd->count = 0;
206 hd->nblocks++;
208 if( !inbuf )
209 return;
211 if( hd->count )
213 for( ; inlen && hd->count < 64; inlen-- )
214 hd->buf[hd->count++] = *inbuf++;
215 md4_write( hd, NULL, 0 );
216 if( !inlen )
217 return;
219 _gcry_burn_stack (80+6*sizeof(void*));
221 while( inlen >= 64 )
223 transform( hd, inbuf );
224 hd->count = 0;
225 hd->nblocks++;
226 inlen -= 64;
227 inbuf += 64;
229 for( ; inlen && hd->count < 64; inlen-- )
230 hd->buf[hd->count++] = *inbuf++;
235 /* The routine final terminates the message-digest computation and
236 * ends with the desired message digest in mdContext->digest[0...15].
237 * The handle is prepared for a new MD4 cycle.
238 * Returns 16 bytes representing the digest.
241 static void
242 md4_final( void *context )
244 MD4_CONTEXT *hd = context;
245 u32 t, msb, lsb;
246 byte *p;
248 md4_write(hd, NULL, 0); /* flush */;
250 t = hd->nblocks;
251 /* multiply by 64 to make a byte count */
252 lsb = t << 6;
253 msb = t >> 26;
254 /* add the count */
255 t = lsb;
256 if( (lsb += hd->count) < t )
257 msb++;
258 /* multiply by 8 to make a bit count */
259 t = lsb;
260 lsb <<= 3;
261 msb <<= 3;
262 msb |= t >> 29;
264 if( hd->count < 56 ) /* enough room */
266 hd->buf[hd->count++] = 0x80; /* pad */
267 while( hd->count < 56 )
268 hd->buf[hd->count++] = 0; /* pad */
270 else /* need one extra block */
272 hd->buf[hd->count++] = 0x80; /* pad character */
273 while( hd->count < 64 )
274 hd->buf[hd->count++] = 0;
275 md4_write(hd, NULL, 0); /* flush */;
276 memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
278 /* append the 64 bit count */
279 hd->buf[56] = lsb ;
280 hd->buf[57] = lsb >> 8;
281 hd->buf[58] = lsb >> 16;
282 hd->buf[59] = lsb >> 24;
283 hd->buf[60] = msb ;
284 hd->buf[61] = msb >> 8;
285 hd->buf[62] = msb >> 16;
286 hd->buf[63] = msb >> 24;
287 transform( hd, hd->buf );
288 _gcry_burn_stack (80+6*sizeof(void*));
290 p = hd->buf;
291 #ifdef WORDS_BIGENDIAN
292 #define X(a) do { *p++ = hd->a ; *p++ = hd->a >> 8; \
293 *p++ = hd->a >> 16; *p++ = hd->a >> 24; } while(0)
294 #else /* little endian */
295 #define X(a) do { *(u32*)p = (*hd).a ; p += 4; } while(0)
296 #endif
297 X(A);
298 X(B);
299 X(C);
300 X(D);
301 #undef X
305 static byte *
306 md4_read (void *context)
308 MD4_CONTEXT *hd = context;
309 return hd->buf;
312 static byte asn[18] = /* Object ID is 1.2.840.113549.2.4 */
313 { 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86,0x48,
314 0x86, 0xf7, 0x0d, 0x02, 0x04, 0x05, 0x00, 0x04, 0x10 };
316 static gcry_md_oid_spec_t oid_spec_md4[] =
318 /* iso.member-body.us.rsadsi.digestAlgorithm.md4 */
319 { "1.2.840.113549.2.4" },
320 { NULL },
323 gcry_md_spec_t _gcry_digest_spec_md4 =
325 "MD4", asn, DIM (asn), oid_spec_md4,16,
326 md4_init, md4_write, md4_final, md4_read,
327 sizeof (MD4_CONTEXT)
329 #ifdef GRUB_UTIL
330 .modname = "gcry_md4",
331 #endif
332 .blocksize = 64
337 GRUB_MOD_INIT(gcry_md4)
339 grub_md_register (&_gcry_digest_spec_md4);
342 GRUB_MOD_FINI(gcry_md4)
344 grub_md_unregister (&_gcry_digest_spec_md4);