~ilya-yanok/ubuntu/precise/grub2/fix-for-948716

« back to all changes in this revision

Viewing changes to grub-core/lib/libgcrypt-grub/cipher/md4.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2011-01-17 13:43:06 UTC
  • mto: (17.6.26 experimental)
  • mto: This revision was merged to the branch mainline in revision 102.
  • Revision ID: james.westby@ubuntu.com-20110117134306-fy7qewn4s3qdx2pl
Tags: upstream-1.99~rc1
ImportĀ upstreamĀ versionĀ 1.99~rc1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
5
 *
 
6
 * This file is part of Libgcrypt.
 
7
 *
 
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.
 
12
 *
 
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.
 
17
 *
 
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
 
21
 *
 
22
 * Based on md5.c in libgcrypt, but rewritten to compute md4 checksums
 
23
 * using a public domain md4 implementation with the following comments:
 
24
 *
 
25
 * Modified by Wei Dai from Andrew M. Kuchling's md4.c
 
26
 * The original code and all modifications are in the public domain.
 
27
 *
 
28
 * This is the original introductory comment:
 
29
 *
 
30
 *  md4.c : MD4 hash algorithm.
 
31
 *
 
32
 * Part of the Python Cryptography Toolkit, version 1.1
 
33
 *
 
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.
 
37
 *
 
38
 */
 
39
 
 
40
/* MD4 test suite:
 
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
 
50
 */
 
51
 
 
52
 
 
53
#include "g10lib.h"
 
54
#include "memory.h"
 
55
#include "cipher.h"
 
56
 
 
57
#include "bithelp.h"
 
58
 
 
59
 
 
60
typedef struct {
 
61
    u32 A,B,C,D;          /* chaining variables */
 
62
    u32  nblocks;
 
63
    byte buf[64];
 
64
    int  count;
 
65
} MD4_CONTEXT;
 
66
 
 
67
 
 
68
static void
 
69
md4_init( void *context )
 
70
{
 
71
  MD4_CONTEXT *ctx = context;
 
72
 
 
73
  ctx->A = 0x67452301;
 
74
  ctx->B = 0xefcdab89;
 
75
  ctx->C = 0x98badcfe;
 
76
  ctx->D = 0x10325476;
 
77
 
 
78
  ctx->nblocks = 0;
 
79
  ctx->count = 0;
 
80
}
 
81
 
 
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))
 
85
 
 
86
 
 
87
/****************
 
88
 * transform 64 bytes
 
89
 */
 
90
static void
 
91
transform ( MD4_CONTEXT *ctx, const unsigned char *data )
 
92
{
 
93
  u32 in[16];
 
94
  register u32 A = ctx->A;
 
95
  register u32 B = ctx->B;
 
96
  register u32 C = ctx->C;
 
97
  register u32 D = ctx->D;
 
98
 
 
99
#ifdef WORDS_BIGENDIAN
 
100
  {
 
101
    int i;
 
102
    byte *p2, *p1;
 
103
    for(i=0, p1=data, p2=(byte*)in; i < 16; i++, p2 += 4 )
 
104
      {
 
105
        p2[3] = *p1++;
 
106
        p2[2] = *p1++;
 
107
        p2[1] = *p1++;
 
108
        p2[0] = *p1++;
 
109
      }
 
110
  }
 
111
#else
 
112
  memcpy (in, data, 64);
 
113
#endif
 
114
 
 
115
  /* Round 1.  */
 
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);
 
133
 
 
134
#undef function
 
135
 
 
136
  /* Round 2.  */
 
137
#define function(a,b,c,d,k,s) a=rol(a+G(b,c,d)+in[k]+0x5a827999,s);
 
138
 
 
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);
 
155
 
 
156
#undef function
 
157
 
 
158
  /* Round 3.  */
 
159
#define function(a,b,c,d,k,s) a=rol(a+H(b,c,d)+in[k]+0x6ed9eba1,s);
 
160
 
 
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);
 
177
 
 
178
 
 
179
  /* Put checksum in context given as argument.  */
 
180
  ctx->A += A;
 
181
  ctx->B += B;
 
182
  ctx->C += C;
 
183
  ctx->D += D;
 
184
}
 
185
 
 
186
 
 
187
 
 
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.
 
191
 */
 
192
static void
 
193
md4_write ( void *context, const void *inbuf_arg, size_t inlen)
 
194
{
 
195
  const unsigned char *inbuf = inbuf_arg;
 
196
  MD4_CONTEXT *hd = context;
 
197
 
 
198
  if( hd->count == 64 ) /* flush the buffer */
 
199
    { 
 
200
      transform( hd, hd->buf );
 
201
      _gcry_burn_stack (80+6*sizeof(void*));
 
202
      hd->count = 0;
 
203
      hd->nblocks++;
 
204
    }
 
205
  if( !inbuf )
 
206
    return;
 
207
 
 
208
  if( hd->count )
 
209
    {
 
210
      for( ; inlen && hd->count < 64; inlen-- )
 
211
        hd->buf[hd->count++] = *inbuf++;
 
212
      md4_write( hd, NULL, 0 );
 
213
      if( !inlen )
 
214
        return;
 
215
    }
 
216
  _gcry_burn_stack (80+6*sizeof(void*));
 
217
 
 
218
  while( inlen >= 64 )
 
219
    {
 
220
      transform( hd, inbuf );
 
221
      hd->count = 0;
 
222
      hd->nblocks++;
 
223
      inlen -= 64;
 
224
      inbuf += 64;
 
225
    }
 
226
  for( ; inlen && hd->count < 64; inlen-- )
 
227
    hd->buf[hd->count++] = *inbuf++;
 
228
}
 
229
 
 
230
 
 
231
 
 
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.
 
236
 */
 
237
 
 
238
static void
 
239
md4_final( void *context )
 
240
{
 
241
  MD4_CONTEXT *hd = context;
 
242
  u32 t, msb, lsb;
 
243
  byte *p;
 
244
 
 
245
  md4_write(hd, NULL, 0); /* flush */;
 
246
 
 
247
  t = hd->nblocks;
 
248
  /* multiply by 64 to make a byte count */
 
249
  lsb = t << 6;
 
250
  msb = t >> 26;
 
251
  /* add the count */
 
252
  t = lsb;
 
253
  if( (lsb += hd->count) < t )
 
254
    msb++;
 
255
  /* multiply by 8 to make a bit count */
 
256
  t = lsb;
 
257
  lsb <<= 3;
 
258
  msb <<= 3;
 
259
  msb |= t >> 29;
 
260
  
 
261
  if( hd->count < 56 )  /* enough room */
 
262
    {
 
263
      hd->buf[hd->count++] = 0x80; /* pad */
 
264
      while( hd->count < 56 )
 
265
        hd->buf[hd->count++] = 0;  /* pad */
 
266
    }
 
267
  else /* need one extra block */ 
 
268
    { 
 
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 */
 
274
    }
 
275
  /* append the 64 bit count */
 
276
  hd->buf[56] = lsb        ;
 
277
  hd->buf[57] = lsb >>  8;
 
278
  hd->buf[58] = lsb >> 16;
 
279
  hd->buf[59] = lsb >> 24;
 
280
  hd->buf[60] = msb        ;
 
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*));
 
286
 
 
287
  p = hd->buf;
 
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)
 
293
#endif
 
294
  X(A);
 
295
  X(B);
 
296
  X(C);
 
297
  X(D);
 
298
#undef X
 
299
 
 
300
}
 
301
 
 
302
static byte *
 
303
md4_read (void *context)
 
304
{
 
305
  MD4_CONTEXT *hd = context;
 
306
  return hd->buf;
 
307
}
 
308
 
 
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 };
 
312
 
 
313
static gcry_md_oid_spec_t oid_spec_md4[] =
 
314
  {
 
315
    /* iso.member-body.us.rsadsi.digestAlgorithm.md4 */
 
316
    { "1.2.840.113549.2.4" },
 
317
    { NULL },
 
318
  };
 
319
 
 
320
gcry_md_spec_t _gcry_digest_spec_md4 =
 
321
  {
 
322
    "MD4", asn, DIM (asn), oid_spec_md4,16,
 
323
    md4_init, md4_write, md4_final, md4_read,
 
324
    sizeof (MD4_CONTEXT)
 
325
    ,
 
326
    .blocksize = 64
 
327
  };
 
328
 
 
329
 
 
330
 
 
331
GRUB_MOD_INIT(gcry_md4)
 
332
{
 
333
  grub_md_register (&_gcry_digest_spec_md4);
 
334
}
 
335
 
 
336
GRUB_MOD_FINI(gcry_md4)
 
337
{
 
338
  grub_md_unregister (&_gcry_digest_spec_md4);
 
339
}