~hamo/ubuntu/precise/grub2/grub2.hi_res

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Evan Broder, Mario Limonciello
  • Date: 2010-11-24 13:59:55 UTC
  • mfrom: (1.17.6 upstream) (17.6.15 experimental)
  • Revision ID: james.westby@ubuntu.com-20101124135955-r6ii5sepayr7jt53
Tags: 1.99~20101124-1ubuntu1
[ Colin Watson ]
* Resynchronise with Debian experimental.  Remaining changes:
  - Adjust for default Ubuntu boot options ("quiet splash").
  - Default to hiding the menu; holding down Shift at boot will show it.
  - Set a monochromatic theme for Ubuntu.
  - Apply Ubuntu GRUB Legacy changes to legacy update-grub script: title,
    recovery mode, quiet option, tweak how memtest86+ is displayed, and
    use UUIDs where appropriate.
  - Fix backslash-escaping in merge_debconf_into_conf.
  - Remove "GNU/Linux" from default distributor string.
  - Add crashkernel= options if kdump and makedumpfile are available.
  - If other operating systems are installed, then automatically unhide
    the menu.  Otherwise, if GRUB_HIDDEN_TIMEOUT is 0, then use keystatus
    if available to check whether Shift is pressed.  If it is, show the
    menu, otherwise boot immediately.  If keystatus is not available, then
    fall back to a short delay interruptible with Escape.
  - Allow Shift to interrupt 'sleep --interruptible'.
  - Don't display introductory message about line editing unless we're
    actually offering a shell prompt.  Don't clear the screen just before
    booting if we never drew the menu in the first place.
  - Remove some verbose messages printed before reading the configuration
    file.
  - Suppress progress messages as the kernel and initrd load for
    non-recovery kernel menu entries.
  - Change prepare_grub_to_access_device to handle filesystems
    loop-mounted on file images.
  - Ignore devices loop-mounted from files in 10_linux.
  - Show the boot menu if the previous boot failed, that is if it failed
    to get to the end of one of the normal runlevels.
  - Don't generate /boot/grub/device.map during grub-install or
    grub-mkconfig by default.
  - Adjust upgrade version checks for Ubuntu.
  - Don't display "GRUB loading" unless Shift is held down.
  - Adjust versions of grub-doc and grub-legacy-doc conflicts to tolerate
    our backport of the grub-doc split.
  - Fix LVM/RAID probing in the absence of /boot/grub/device.map.
  - Look for .mo files in /usr/share/locale-langpack as well, in
    preference.
  - Make sure GRUB_TIMEOUT isn't quoted unnecessarily.
  - Probe all devices in 'grub-probe --target=drive' if
    /boot/grub/device.map is missing.
  - Build-depend on qemu-kvm rather than qemu-system for grub-pc tests.
  - Use qemu rather than qemu-system-i386.
  - Program vesafb on BIOS systems rather than efifb.
  - Add a grub-rescue-efi-amd64 package containing a rescue CD-ROM image
    for EFI-AMD64.
  - On Wubi, don't ask for an install device, but just update wubildr
    using the diverted grub-install.
  - When embedding the core image in a post-MBR gap, check for and avoid
    sectors matching any of a list of known signatures.
  - Disable video_bochs and video_cirrus on PC BIOS systems, as probing
    PCI space seems to break on some systems.
* Downgrade "ACPI shutdown failed" error to a debug message, since it can
  cause spurious test failures.

[ Evan Broder ]
* Enable lua from grub-extras.
* Incorporate the bitop library into lua.
* Add enum_pci function to grub module in lua.
* Switch back to gfxpayload=keep by default, unless the video hardware
  is known to not support it.

[ Mario Limonciello ]
* Built part_msdos and vfat into bootx64.efi (LP: #677758)

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
/* sha1.c - SHA1 hash function
 
4
 * Copyright (C) 1998, 2001, 2002, 2003, 2008 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, see <http://www.gnu.org/licenses/>.
 
20
 */
 
21
 
 
22
 
 
23
/*  Test vectors:
 
24
 *
 
25
 *  "abc"
 
26
 *  A999 3E36 4706 816A BA3E  2571 7850 C26C 9CD0 D89D
 
27
 *
 
28
 *  "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
 
29
 *  8498 3E44 1C3B D26E BAAE  4AA1 F951 29E5 E546 70F1
 
30
 */
 
31
 
 
32
 
 
33
#ifdef HAVE_STDINT_H 
 
34
# include <stdint.h>
 
35
#endif
 
36
 
 
37
#include "g10lib.h"
 
38
#include "memory.h"
 
39
#include "bithelp.h"
 
40
#include "cipher.h"
 
41
#include "hash-common.h"
 
42
 
 
43
 
 
44
/* A macro to test whether P is properly aligned for an u32 type.
 
45
   Note that config.h provides a suitable replacement for uintptr_t if
 
46
   it does not exist in stdint.h.  */
 
47
/* #if __GNUC__ >= 2 */
 
48
/* # define U32_ALIGNED_P(p) (!(((uintptr_t)p) % __alignof__ (u32))) */
 
49
/* #else */
 
50
/* # define U32_ALIGNED_P(p) (!(((uintptr_t)p) % sizeof (u32))) */
 
51
/* #endif */
 
52
 
 
53
#define TRANSFORM(x,d,n) transform ((x), (d), (n))
 
54
 
 
55
 
 
56
typedef struct 
 
57
{
 
58
  u32           h0,h1,h2,h3,h4;
 
59
  u32           nblocks;
 
60
  unsigned char buf[64];
 
61
  int           count;
 
62
} SHA1_CONTEXT;
 
63
 
 
64
 
 
65
 
 
66
static void
 
67
sha1_init (void *context)
 
68
{
 
69
  SHA1_CONTEXT *hd = context;
 
70
 
 
71
  hd->h0 = 0x67452301;
 
72
  hd->h1 = 0xefcdab89;
 
73
  hd->h2 = 0x98badcfe;
 
74
  hd->h3 = 0x10325476;
 
75
  hd->h4 = 0xc3d2e1f0;
 
76
  hd->nblocks = 0;
 
77
  hd->count = 0;
 
78
}
 
79
 
 
80
 
 
81
/* Round function macros. */
 
82
#define K1  0x5A827999L
 
83
#define K2  0x6ED9EBA1L
 
84
#define K3  0x8F1BBCDCL
 
85
#define K4  0xCA62C1D6L
 
86
#define F1(x,y,z)   ( z ^ ( x & ( y ^ z ) ) )
 
87
#define F2(x,y,z)   ( x ^ y ^ z )
 
88
#define F3(x,y,z)   ( ( x & y ) | ( z & ( x | y ) ) )
 
89
#define F4(x,y,z)   ( x ^ y ^ z )
 
90
#define M(i) ( tm =    x[ i    &0x0f]  \
 
91
                     ^ x[(i-14)&0x0f]  \
 
92
                     ^ x[(i-8) &0x0f]  \
 
93
                     ^ x[(i-3) &0x0f], \
 
94
                     (x[i&0x0f] = rol(tm, 1)))
 
95
#define R(a,b,c,d,e,f,k,m)  do { e += rol( a, 5 )     \
 
96
                                      + f( b, c, d )  \
 
97
                                      + k             \
 
98
                                      + m;            \
 
99
                                 b = rol( b, 30 );    \
 
100
                               } while(0)
 
101
 
 
102
 
 
103
/*
 
104
 * Transform NBLOCKS of each 64 bytes (16 32-bit words) at DATA.
 
105
 */
 
106
static void
 
107
transform (SHA1_CONTEXT *hd, const unsigned char *data, size_t nblocks)
 
108
{
 
109
  register u32 a, b, c, d, e; /* Local copies of the chaining variables.  */
 
110
  register u32 tm;            /* Helper.  */
 
111
  u32 x[16];                  /* The array we work on. */
 
112
  
 
113
  /* Loop over all blocks.  */
 
114
  for ( ;nblocks; nblocks--)
 
115
    {
 
116
#ifdef WORDS_BIGENDIAN
 
117
      memcpy (x, data, 64);
 
118
      data += 64;
 
119
#else
 
120
      {
 
121
        int i;
 
122
        unsigned char *p;
 
123
 
 
124
        for(i=0, p=(unsigned char*)x; i < 16; i++, p += 4 )
 
125
          {
 
126
            p[3] = *data++;
 
127
            p[2] = *data++;
 
128
            p[1] = *data++;
 
129
            p[0] = *data++;
 
130
          }
 
131
      }
 
132
#endif
 
133
      /* Get the values of the chaining variables. */
 
134
      a = hd->h0;
 
135
      b = hd->h1;
 
136
      c = hd->h2;
 
137
      d = hd->h3;
 
138
      e = hd->h4;
 
139
 
 
140
      /* Transform. */
 
141
      R( a, b, c, d, e, F1, K1, x[ 0] );
 
142
      R( e, a, b, c, d, F1, K1, x[ 1] );
 
143
      R( d, e, a, b, c, F1, K1, x[ 2] );
 
144
      R( c, d, e, a, b, F1, K1, x[ 3] );
 
145
      R( b, c, d, e, a, F1, K1, x[ 4] );
 
146
      R( a, b, c, d, e, F1, K1, x[ 5] );
 
147
      R( e, a, b, c, d, F1, K1, x[ 6] );
 
148
      R( d, e, a, b, c, F1, K1, x[ 7] );
 
149
      R( c, d, e, a, b, F1, K1, x[ 8] );
 
150
      R( b, c, d, e, a, F1, K1, x[ 9] );
 
151
      R( a, b, c, d, e, F1, K1, x[10] );
 
152
      R( e, a, b, c, d, F1, K1, x[11] );
 
153
      R( d, e, a, b, c, F1, K1, x[12] );
 
154
      R( c, d, e, a, b, F1, K1, x[13] );
 
155
      R( b, c, d, e, a, F1, K1, x[14] );
 
156
      R( a, b, c, d, e, F1, K1, x[15] );
 
157
      R( e, a, b, c, d, F1, K1, M(16) );
 
158
      R( d, e, a, b, c, F1, K1, M(17) );
 
159
      R( c, d, e, a, b, F1, K1, M(18) );
 
160
      R( b, c, d, e, a, F1, K1, M(19) );
 
161
      R( a, b, c, d, e, F2, K2, M(20) );
 
162
      R( e, a, b, c, d, F2, K2, M(21) );
 
163
      R( d, e, a, b, c, F2, K2, M(22) );
 
164
      R( c, d, e, a, b, F2, K2, M(23) );
 
165
      R( b, c, d, e, a, F2, K2, M(24) );
 
166
      R( a, b, c, d, e, F2, K2, M(25) );
 
167
      R( e, a, b, c, d, F2, K2, M(26) );
 
168
      R( d, e, a, b, c, F2, K2, M(27) );
 
169
      R( c, d, e, a, b, F2, K2, M(28) );
 
170
      R( b, c, d, e, a, F2, K2, M(29) );
 
171
      R( a, b, c, d, e, F2, K2, M(30) );
 
172
      R( e, a, b, c, d, F2, K2, M(31) );
 
173
      R( d, e, a, b, c, F2, K2, M(32) );
 
174
      R( c, d, e, a, b, F2, K2, M(33) );
 
175
      R( b, c, d, e, a, F2, K2, M(34) );
 
176
      R( a, b, c, d, e, F2, K2, M(35) );
 
177
      R( e, a, b, c, d, F2, K2, M(36) );
 
178
      R( d, e, a, b, c, F2, K2, M(37) );
 
179
      R( c, d, e, a, b, F2, K2, M(38) );
 
180
      R( b, c, d, e, a, F2, K2, M(39) );
 
181
      R( a, b, c, d, e, F3, K3, M(40) );
 
182
      R( e, a, b, c, d, F3, K3, M(41) );
 
183
      R( d, e, a, b, c, F3, K3, M(42) );
 
184
      R( c, d, e, a, b, F3, K3, M(43) );
 
185
      R( b, c, d, e, a, F3, K3, M(44) );
 
186
      R( a, b, c, d, e, F3, K3, M(45) );
 
187
      R( e, a, b, c, d, F3, K3, M(46) );
 
188
      R( d, e, a, b, c, F3, K3, M(47) );
 
189
      R( c, d, e, a, b, F3, K3, M(48) );
 
190
      R( b, c, d, e, a, F3, K3, M(49) );
 
191
      R( a, b, c, d, e, F3, K3, M(50) );
 
192
      R( e, a, b, c, d, F3, K3, M(51) );
 
193
      R( d, e, a, b, c, F3, K3, M(52) );
 
194
      R( c, d, e, a, b, F3, K3, M(53) );
 
195
      R( b, c, d, e, a, F3, K3, M(54) );
 
196
      R( a, b, c, d, e, F3, K3, M(55) );
 
197
      R( e, a, b, c, d, F3, K3, M(56) );
 
198
      R( d, e, a, b, c, F3, K3, M(57) );
 
199
      R( c, d, e, a, b, F3, K3, M(58) );
 
200
      R( b, c, d, e, a, F3, K3, M(59) );
 
201
      R( a, b, c, d, e, F4, K4, M(60) );
 
202
      R( e, a, b, c, d, F4, K4, M(61) );
 
203
      R( d, e, a, b, c, F4, K4, M(62) );
 
204
      R( c, d, e, a, b, F4, K4, M(63) );
 
205
      R( b, c, d, e, a, F4, K4, M(64) );
 
206
      R( a, b, c, d, e, F4, K4, M(65) );
 
207
      R( e, a, b, c, d, F4, K4, M(66) );
 
208
      R( d, e, a, b, c, F4, K4, M(67) );
 
209
      R( c, d, e, a, b, F4, K4, M(68) );
 
210
      R( b, c, d, e, a, F4, K4, M(69) );
 
211
      R( a, b, c, d, e, F4, K4, M(70) );
 
212
      R( e, a, b, c, d, F4, K4, M(71) );
 
213
      R( d, e, a, b, c, F4, K4, M(72) );
 
214
      R( c, d, e, a, b, F4, K4, M(73) );
 
215
      R( b, c, d, e, a, F4, K4, M(74) );
 
216
      R( a, b, c, d, e, F4, K4, M(75) );
 
217
      R( e, a, b, c, d, F4, K4, M(76) );
 
218
      R( d, e, a, b, c, F4, K4, M(77) );
 
219
      R( c, d, e, a, b, F4, K4, M(78) );
 
220
      R( b, c, d, e, a, F4, K4, M(79) );
 
221
 
 
222
      /* Update the chaining variables. */
 
223
      hd->h0 += a;
 
224
      hd->h1 += b;
 
225
      hd->h2 += c;
 
226
      hd->h3 += d;
 
227
      hd->h4 += e;
 
228
    }
 
229
}
 
230
 
 
231
 
 
232
/* Update the message digest with the contents
 
233
 * of INBUF with length INLEN.
 
234
 */
 
235
static void
 
236
sha1_write( void *context, const void *inbuf_arg, size_t inlen)
 
237
{
 
238
  const unsigned char *inbuf = inbuf_arg;
 
239
  SHA1_CONTEXT *hd = context;
 
240
  size_t nblocks;
 
241
 
 
242
  if (hd->count == 64)  /* Flush the buffer. */
 
243
    {
 
244
      TRANSFORM( hd, hd->buf, 1 );
 
245
      _gcry_burn_stack (88+4*sizeof(void*));
 
246
      hd->count = 0;
 
247
      hd->nblocks++;
 
248
    }
 
249
  if (!inbuf)
 
250
    return;
 
251
 
 
252
  if (hd->count)
 
253
    {
 
254
      for (; inlen && hd->count < 64; inlen--)
 
255
        hd->buf[hd->count++] = *inbuf++;
 
256
      sha1_write (hd, NULL, 0);
 
257
      if (!inlen)
 
258
        return;
 
259
    }
 
260
 
 
261
  nblocks = inlen / 64;
 
262
  if (nblocks)
 
263
    {
 
264
      TRANSFORM (hd, inbuf, nblocks);
 
265
      hd->count = 0;
 
266
      hd->nblocks += nblocks;
 
267
      inlen -= nblocks * 64;
 
268
      inbuf += nblocks * 64;
 
269
    }
 
270
  _gcry_burn_stack (88+4*sizeof(void*));
 
271
 
 
272
  /* Save remaining bytes.  */
 
273
  for (; inlen && hd->count < 64; inlen--)
 
274
    hd->buf[hd->count++] = *inbuf++;
 
275
}
 
276
 
 
277
 
 
278
/* The routine final terminates the computation and
 
279
 * returns the digest.
 
280
 * The handle is prepared for a new cycle, but adding bytes to the
 
281
 * handle will the destroy the returned buffer.
 
282
 * Returns: 20 bytes representing the digest.
 
283
 */
 
284
 
 
285
static void
 
286
sha1_final(void *context)
 
287
{
 
288
  SHA1_CONTEXT *hd = context;
 
289
  
 
290
  u32 t, msb, lsb;
 
291
  unsigned char *p;
 
292
 
 
293
  sha1_write(hd, NULL, 0); /* flush */;
 
294
 
 
295
  t = hd->nblocks;
 
296
  /* multiply by 64 to make a byte count */
 
297
  lsb = t << 6;
 
298
  msb = t >> 26;
 
299
  /* add the count */
 
300
  t = lsb;
 
301
  if( (lsb += hd->count) < t )
 
302
    msb++;
 
303
  /* multiply by 8 to make a bit count */
 
304
  t = lsb;
 
305
  lsb <<= 3;
 
306
  msb <<= 3;
 
307
  msb |= t >> 29;
 
308
 
 
309
  if( hd->count < 56 )  /* enough room */
 
310
    {
 
311
      hd->buf[hd->count++] = 0x80; /* pad */
 
312
      while( hd->count < 56 )
 
313
        hd->buf[hd->count++] = 0;  /* pad */
 
314
    }
 
315
  else  /* need one extra block */
 
316
    {
 
317
      hd->buf[hd->count++] = 0x80; /* pad character */
 
318
      while( hd->count < 64 )
 
319
        hd->buf[hd->count++] = 0;
 
320
      sha1_write(hd, NULL, 0);  /* flush */;
 
321
      memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
 
322
    }
 
323
  /* append the 64 bit count */
 
324
  hd->buf[56] = msb >> 24;
 
325
  hd->buf[57] = msb >> 16;
 
326
  hd->buf[58] = msb >>  8;
 
327
  hd->buf[59] = msb        ;
 
328
  hd->buf[60] = lsb >> 24;
 
329
  hd->buf[61] = lsb >> 16;
 
330
  hd->buf[62] = lsb >>  8;
 
331
  hd->buf[63] = lsb        ;
 
332
  TRANSFORM( hd, hd->buf, 1 );
 
333
  _gcry_burn_stack (88+4*sizeof(void*));
 
334
 
 
335
  p = hd->buf;
 
336
#ifdef WORDS_BIGENDIAN
 
337
#define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
 
338
#else /* little endian */
 
339
#define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16;  \
 
340
                  *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
 
341
#endif
 
342
  X(0);
 
343
  X(1);
 
344
  X(2);
 
345
  X(3);
 
346
  X(4);
 
347
#undef X
 
348
 
 
349
}
 
350
 
 
351
static unsigned char *
 
352
sha1_read( void *context )
 
353
{
 
354
  SHA1_CONTEXT *hd = context;
 
355
 
 
356
  return hd->buf;
 
357
}
 
358
 
 
359
/****************
 
360
 * Shortcut functions which puts the hash value of the supplied buffer
 
361
 * into outbuf which must have a size of 20 bytes.
 
362
 */
 
363
 
 
364
 
 
365
 
 
366
/* 
 
367
     Self-test section.
 
368
 */
 
369
 
 
370
 
 
371
 
 
372
 
 
373
/* Run a full self-test for ALGO and return 0 on success.  */
 
374
 
 
375
 
 
376
 
 
377
 
 
378
static unsigned char asn[15] = /* Object ID is 1.3.14.3.2.26 */
 
379
  { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
 
380
    0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 };
 
381
 
 
382
static gcry_md_oid_spec_t oid_spec_sha1[] =
 
383
  {
 
384
    /* iso.member-body.us.rsadsi.pkcs.pkcs-1.5 (sha1WithRSAEncryption) */
 
385
    { "1.2.840.113549.1.1.5" },
 
386
    /* iso.member-body.us.x9-57.x9cm.3 (dsaWithSha1)*/
 
387
    { "1.2.840.10040.4.3" },
 
388
    /* from NIST's OIW  (sha1) */
 
389
    { "1.3.14.3.2.26" },
 
390
    /* from NIST OIW (sha-1WithRSAEncryption) */
 
391
    { "1.3.14.3.2.29" },
 
392
    /* iso.member-body.us.ansi-x9-62.signatures.ecdsa-with-sha1 */
 
393
    { "1.2.840.10045.4.1" },
 
394
    { NULL },
 
395
  };
 
396
 
 
397
gcry_md_spec_t _gcry_digest_spec_sha1 =
 
398
  {
 
399
    "SHA1", asn, DIM (asn), oid_spec_sha1, 20,
 
400
    sha1_init, sha1_write, sha1_final, sha1_read,
 
401
    sizeof (SHA1_CONTEXT)
 
402
    ,
 
403
    .blocksize = 64
 
404
  };
 
405
 
 
406
 
 
407
 
 
408
GRUB_MOD_INIT(gcry_sha1)
 
409
{
 
410
  grub_md_register (&_gcry_digest_spec_sha1);
 
411
}
 
412
 
 
413
GRUB_MOD_FINI(gcry_sha1)
 
414
{
 
415
  grub_md_unregister (&_gcry_digest_spec_sha1);
 
416
}