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

« back to all changes in this revision

Viewing changes to lib/libgcrypt/cipher/sha1.c

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson, Colin Watson, Robert Millan, Updated translations
  • Date: 2010-11-22 12:24:56 UTC
  • mfrom: (1.26.4 upstream) (17.3.36 sid)
  • mto: (17.3.43 sid)
  • mto: This revision was merged to the branch mainline in revision 89.
  • Revision ID: james.westby@ubuntu.com-20101122122456-y82z3sfb7k4zfdcc
Tags: 1.99~20101122-1
[ Colin Watson ]
* New Bazaar snapshot.  Too many changes to list in full, but some of the
  more user-visible ones are as follows:
  - GRUB script:
    + Function parameters, "break", "continue", "shift", "setparams",
      "return", and "!".
    + "export" command supports multiple variable names.
    + Multi-line quoted strings support.
    + Wildcard expansion.
  - sendkey support.
  - USB hotunplugging and USB serial support.
  - Rename CD-ROM to cd on BIOS.
  - Add new --boot-directory option to grub-install, grub-reboot, and
    grub-set-default; the old --root-directory option is still accepted
    but was often confusing.
  - Basic btrfs detection/UUID support (but no file reading yet).
  - bash-completion for utilities.
  - If a device is listed in device.map, always assume that it is
    BIOS-visible rather than using extra layers such as LVM or RAID.
  - Add grub-mknetdir script (closes: #550658).
  - Remove deprecated "root" command.
  - Handle RAID devices containing virtio components.
  - GRUB Legacy configuration file support (via grub-menulst2cfg).
  - Keyboard layout support (via grub-mklayout and grub-kbdcomp).
  - Check generated grub.cfg for syntax errors before saving.
  - Pause execution for at most ten seconds if any errors are displayed,
    so that the user has a chance to see them.
  - Support submenus.
  - Write embedding zone using Reed-Solomon, so that it's robust against
    being partially overwritten (closes: #550702, #591416, #593347).
  - GRUB_DISABLE_LINUX_RECOVERY and GRUB_DISABLE_NETBSD_RECOVERY merged
    into a single GRUB_DISABLE_RECOVERY variable.
  - Fix loader memory allocation failure (closes: #551627).
  - Don't call savedefault on recovery entries (closes: #589325).
  - Support triple-indirect blocks on ext2 (closes: #543924).
  - Recognise DDF1 fake RAID (closes: #603354).

[ Robert Millan ]
* Use dpkg architecture wildcards.

[ Updated translations ]
* Slovenian (Vanja Cvelbar).  Closes: #604003
* Dzongkha (dawa pemo via Tenzin Dendup).  Closes: #604102

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* sha1.c - SHA1 hash function
2
 
 * Copyright (C) 1998, 2001, 2002, 2003, 2008 Free Software Foundation, Inc.
3
 
 *
4
 
 * This file is part of Libgcrypt.
5
 
 *
6
 
 * Libgcrypt is free software; you can redistribute it and/or modify
7
 
 * it under the terms of the GNU Lesser General Public License as
8
 
 * published by the Free Software Foundation; either version 2.1 of
9
 
 * the License, or (at your option) any later version.
10
 
 *
11
 
 * Libgcrypt is distributed in the hope that it will be useful,
12
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
 * GNU Lesser General Public License for more details.
15
 
 *
16
 
 * You should have received a copy of the GNU Lesser General Public
17
 
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
18
 
 */
19
 
 
20
 
 
21
 
/*  Test vectors:
22
 
 *
23
 
 *  "abc"
24
 
 *  A999 3E36 4706 816A BA3E  2571 7850 C26C 9CD0 D89D
25
 
 *
26
 
 *  "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
27
 
 *  8498 3E44 1C3B D26E BAAE  4AA1 F951 29E5 E546 70F1
28
 
 */
29
 
 
30
 
 
31
 
#include <config.h>
32
 
#include <stdio.h>
33
 
#include <stdlib.h>
34
 
#include <string.h>
35
 
#ifdef HAVE_STDINT_H 
36
 
# include <stdint.h>
37
 
#endif
38
 
 
39
 
#include "g10lib.h"
40
 
#include "memory.h"
41
 
#include "bithelp.h"
42
 
#include "cipher.h"
43
 
#include "hash-common.h"
44
 
 
45
 
 
46
 
/* A macro to test whether P is properly aligned for an u32 type.
47
 
   Note that config.h provides a suitable replacement for uintptr_t if
48
 
   it does not exist in stdint.h.  */
49
 
/* #if __GNUC__ >= 2 */
50
 
/* # define U32_ALIGNED_P(p) (!(((uintptr_t)p) % __alignof__ (u32))) */
51
 
/* #else */
52
 
/* # define U32_ALIGNED_P(p) (!(((uintptr_t)p) % sizeof (u32))) */
53
 
/* #endif */
54
 
 
55
 
#define TRANSFORM(x,d,n) transform ((x), (d), (n))
56
 
 
57
 
 
58
 
typedef struct 
59
 
{
60
 
  u32           h0,h1,h2,h3,h4;
61
 
  u32           nblocks;
62
 
  unsigned char buf[64];
63
 
  int           count;
64
 
} SHA1_CONTEXT;
65
 
 
66
 
 
67
 
 
68
 
static void
69
 
sha1_init (void *context)
70
 
{
71
 
  SHA1_CONTEXT *hd = context;
72
 
 
73
 
  hd->h0 = 0x67452301;
74
 
  hd->h1 = 0xefcdab89;
75
 
  hd->h2 = 0x98badcfe;
76
 
  hd->h3 = 0x10325476;
77
 
  hd->h4 = 0xc3d2e1f0;
78
 
  hd->nblocks = 0;
79
 
  hd->count = 0;
80
 
}
81
 
 
82
 
 
83
 
/* Round function macros. */
84
 
#define K1  0x5A827999L
85
 
#define K2  0x6ED9EBA1L
86
 
#define K3  0x8F1BBCDCL
87
 
#define K4  0xCA62C1D6L
88
 
#define F1(x,y,z)   ( z ^ ( x & ( y ^ z ) ) )
89
 
#define F2(x,y,z)   ( x ^ y ^ z )
90
 
#define F3(x,y,z)   ( ( x & y ) | ( z & ( x | y ) ) )
91
 
#define F4(x,y,z)   ( x ^ y ^ z )
92
 
#define M(i) ( tm =    x[ i    &0x0f]  \
93
 
                     ^ x[(i-14)&0x0f]  \
94
 
                     ^ x[(i-8) &0x0f]  \
95
 
                     ^ x[(i-3) &0x0f], \
96
 
                     (x[i&0x0f] = rol(tm, 1)))
97
 
#define R(a,b,c,d,e,f,k,m)  do { e += rol( a, 5 )     \
98
 
                                      + f( b, c, d )  \
99
 
                                      + k             \
100
 
                                      + m;            \
101
 
                                 b = rol( b, 30 );    \
102
 
                               } while(0)
103
 
 
104
 
 
105
 
/*
106
 
 * Transform NBLOCKS of each 64 bytes (16 32-bit words) at DATA.
107
 
 */
108
 
static void
109
 
transform (SHA1_CONTEXT *hd, const unsigned char *data, size_t nblocks)
110
 
{
111
 
  register u32 a, b, c, d, e; /* Local copies of the chaining variables.  */
112
 
  register u32 tm;            /* Helper.  */
113
 
  u32 x[16];                  /* The array we work on. */
114
 
  
115
 
  /* Loop over all blocks.  */
116
 
  for ( ;nblocks; nblocks--)
117
 
    {
118
 
#ifdef WORDS_BIGENDIAN
119
 
      memcpy (x, data, 64);
120
 
      data += 64;
121
 
#else
122
 
      {
123
 
        int i;
124
 
        unsigned char *p;
125
 
 
126
 
        for(i=0, p=(unsigned char*)x; i < 16; i++, p += 4 )
127
 
          {
128
 
            p[3] = *data++;
129
 
            p[2] = *data++;
130
 
            p[1] = *data++;
131
 
            p[0] = *data++;
132
 
          }
133
 
      }
134
 
#endif
135
 
      /* Get the values of the chaining variables. */
136
 
      a = hd->h0;
137
 
      b = hd->h1;
138
 
      c = hd->h2;
139
 
      d = hd->h3;
140
 
      e = hd->h4;
141
 
 
142
 
      /* Transform. */
143
 
      R( a, b, c, d, e, F1, K1, x[ 0] );
144
 
      R( e, a, b, c, d, F1, K1, x[ 1] );
145
 
      R( d, e, a, b, c, F1, K1, x[ 2] );
146
 
      R( c, d, e, a, b, F1, K1, x[ 3] );
147
 
      R( b, c, d, e, a, F1, K1, x[ 4] );
148
 
      R( a, b, c, d, e, F1, K1, x[ 5] );
149
 
      R( e, a, b, c, d, F1, K1, x[ 6] );
150
 
      R( d, e, a, b, c, F1, K1, x[ 7] );
151
 
      R( c, d, e, a, b, F1, K1, x[ 8] );
152
 
      R( b, c, d, e, a, F1, K1, x[ 9] );
153
 
      R( a, b, c, d, e, F1, K1, x[10] );
154
 
      R( e, a, b, c, d, F1, K1, x[11] );
155
 
      R( d, e, a, b, c, F1, K1, x[12] );
156
 
      R( c, d, e, a, b, F1, K1, x[13] );
157
 
      R( b, c, d, e, a, F1, K1, x[14] );
158
 
      R( a, b, c, d, e, F1, K1, x[15] );
159
 
      R( e, a, b, c, d, F1, K1, M(16) );
160
 
      R( d, e, a, b, c, F1, K1, M(17) );
161
 
      R( c, d, e, a, b, F1, K1, M(18) );
162
 
      R( b, c, d, e, a, F1, K1, M(19) );
163
 
      R( a, b, c, d, e, F2, K2, M(20) );
164
 
      R( e, a, b, c, d, F2, K2, M(21) );
165
 
      R( d, e, a, b, c, F2, K2, M(22) );
166
 
      R( c, d, e, a, b, F2, K2, M(23) );
167
 
      R( b, c, d, e, a, F2, K2, M(24) );
168
 
      R( a, b, c, d, e, F2, K2, M(25) );
169
 
      R( e, a, b, c, d, F2, K2, M(26) );
170
 
      R( d, e, a, b, c, F2, K2, M(27) );
171
 
      R( c, d, e, a, b, F2, K2, M(28) );
172
 
      R( b, c, d, e, a, F2, K2, M(29) );
173
 
      R( a, b, c, d, e, F2, K2, M(30) );
174
 
      R( e, a, b, c, d, F2, K2, M(31) );
175
 
      R( d, e, a, b, c, F2, K2, M(32) );
176
 
      R( c, d, e, a, b, F2, K2, M(33) );
177
 
      R( b, c, d, e, a, F2, K2, M(34) );
178
 
      R( a, b, c, d, e, F2, K2, M(35) );
179
 
      R( e, a, b, c, d, F2, K2, M(36) );
180
 
      R( d, e, a, b, c, F2, K2, M(37) );
181
 
      R( c, d, e, a, b, F2, K2, M(38) );
182
 
      R( b, c, d, e, a, F2, K2, M(39) );
183
 
      R( a, b, c, d, e, F3, K3, M(40) );
184
 
      R( e, a, b, c, d, F3, K3, M(41) );
185
 
      R( d, e, a, b, c, F3, K3, M(42) );
186
 
      R( c, d, e, a, b, F3, K3, M(43) );
187
 
      R( b, c, d, e, a, F3, K3, M(44) );
188
 
      R( a, b, c, d, e, F3, K3, M(45) );
189
 
      R( e, a, b, c, d, F3, K3, M(46) );
190
 
      R( d, e, a, b, c, F3, K3, M(47) );
191
 
      R( c, d, e, a, b, F3, K3, M(48) );
192
 
      R( b, c, d, e, a, F3, K3, M(49) );
193
 
      R( a, b, c, d, e, F3, K3, M(50) );
194
 
      R( e, a, b, c, d, F3, K3, M(51) );
195
 
      R( d, e, a, b, c, F3, K3, M(52) );
196
 
      R( c, d, e, a, b, F3, K3, M(53) );
197
 
      R( b, c, d, e, a, F3, K3, M(54) );
198
 
      R( a, b, c, d, e, F3, K3, M(55) );
199
 
      R( e, a, b, c, d, F3, K3, M(56) );
200
 
      R( d, e, a, b, c, F3, K3, M(57) );
201
 
      R( c, d, e, a, b, F3, K3, M(58) );
202
 
      R( b, c, d, e, a, F3, K3, M(59) );
203
 
      R( a, b, c, d, e, F4, K4, M(60) );
204
 
      R( e, a, b, c, d, F4, K4, M(61) );
205
 
      R( d, e, a, b, c, F4, K4, M(62) );
206
 
      R( c, d, e, a, b, F4, K4, M(63) );
207
 
      R( b, c, d, e, a, F4, K4, M(64) );
208
 
      R( a, b, c, d, e, F4, K4, M(65) );
209
 
      R( e, a, b, c, d, F4, K4, M(66) );
210
 
      R( d, e, a, b, c, F4, K4, M(67) );
211
 
      R( c, d, e, a, b, F4, K4, M(68) );
212
 
      R( b, c, d, e, a, F4, K4, M(69) );
213
 
      R( a, b, c, d, e, F4, K4, M(70) );
214
 
      R( e, a, b, c, d, F4, K4, M(71) );
215
 
      R( d, e, a, b, c, F4, K4, M(72) );
216
 
      R( c, d, e, a, b, F4, K4, M(73) );
217
 
      R( b, c, d, e, a, F4, K4, M(74) );
218
 
      R( a, b, c, d, e, F4, K4, M(75) );
219
 
      R( e, a, b, c, d, F4, K4, M(76) );
220
 
      R( d, e, a, b, c, F4, K4, M(77) );
221
 
      R( c, d, e, a, b, F4, K4, M(78) );
222
 
      R( b, c, d, e, a, F4, K4, M(79) );
223
 
 
224
 
      /* Update the chaining variables. */
225
 
      hd->h0 += a;
226
 
      hd->h1 += b;
227
 
      hd->h2 += c;
228
 
      hd->h3 += d;
229
 
      hd->h4 += e;
230
 
    }
231
 
}
232
 
 
233
 
 
234
 
/* Update the message digest with the contents
235
 
 * of INBUF with length INLEN.
236
 
 */
237
 
static void
238
 
sha1_write( void *context, const void *inbuf_arg, size_t inlen)
239
 
{
240
 
  const unsigned char *inbuf = inbuf_arg;
241
 
  SHA1_CONTEXT *hd = context;
242
 
  size_t nblocks;
243
 
 
244
 
  if (hd->count == 64)  /* Flush the buffer. */
245
 
    {
246
 
      TRANSFORM( hd, hd->buf, 1 );
247
 
      _gcry_burn_stack (88+4*sizeof(void*));
248
 
      hd->count = 0;
249
 
      hd->nblocks++;
250
 
    }
251
 
  if (!inbuf)
252
 
    return;
253
 
 
254
 
  if (hd->count)
255
 
    {
256
 
      for (; inlen && hd->count < 64; inlen--)
257
 
        hd->buf[hd->count++] = *inbuf++;
258
 
      sha1_write (hd, NULL, 0);
259
 
      if (!inlen)
260
 
        return;
261
 
    }
262
 
 
263
 
  nblocks = inlen / 64;
264
 
  if (nblocks)
265
 
    {
266
 
      TRANSFORM (hd, inbuf, nblocks);
267
 
      hd->count = 0;
268
 
      hd->nblocks += nblocks;
269
 
      inlen -= nblocks * 64;
270
 
      inbuf += nblocks * 64;
271
 
    }
272
 
  _gcry_burn_stack (88+4*sizeof(void*));
273
 
 
274
 
  /* Save remaining bytes.  */
275
 
  for (; inlen && hd->count < 64; inlen--)
276
 
    hd->buf[hd->count++] = *inbuf++;
277
 
}
278
 
 
279
 
 
280
 
/* The routine final terminates the computation and
281
 
 * returns the digest.
282
 
 * The handle is prepared for a new cycle, but adding bytes to the
283
 
 * handle will the destroy the returned buffer.
284
 
 * Returns: 20 bytes representing the digest.
285
 
 */
286
 
 
287
 
static void
288
 
sha1_final(void *context)
289
 
{
290
 
  SHA1_CONTEXT *hd = context;
291
 
  
292
 
  u32 t, msb, lsb;
293
 
  unsigned char *p;
294
 
 
295
 
  sha1_write(hd, NULL, 0); /* flush */;
296
 
 
297
 
  t = hd->nblocks;
298
 
  /* multiply by 64 to make a byte count */
299
 
  lsb = t << 6;
300
 
  msb = t >> 26;
301
 
  /* add the count */
302
 
  t = lsb;
303
 
  if( (lsb += hd->count) < t )
304
 
    msb++;
305
 
  /* multiply by 8 to make a bit count */
306
 
  t = lsb;
307
 
  lsb <<= 3;
308
 
  msb <<= 3;
309
 
  msb |= t >> 29;
310
 
 
311
 
  if( hd->count < 56 )  /* enough room */
312
 
    {
313
 
      hd->buf[hd->count++] = 0x80; /* pad */
314
 
      while( hd->count < 56 )
315
 
        hd->buf[hd->count++] = 0;  /* pad */
316
 
    }
317
 
  else  /* need one extra block */
318
 
    {
319
 
      hd->buf[hd->count++] = 0x80; /* pad character */
320
 
      while( hd->count < 64 )
321
 
        hd->buf[hd->count++] = 0;
322
 
      sha1_write(hd, NULL, 0);  /* flush */;
323
 
      memset(hd->buf, 0, 56 ); /* fill next block with zeroes */
324
 
    }
325
 
  /* append the 64 bit count */
326
 
  hd->buf[56] = msb >> 24;
327
 
  hd->buf[57] = msb >> 16;
328
 
  hd->buf[58] = msb >>  8;
329
 
  hd->buf[59] = msb        ;
330
 
  hd->buf[60] = lsb >> 24;
331
 
  hd->buf[61] = lsb >> 16;
332
 
  hd->buf[62] = lsb >>  8;
333
 
  hd->buf[63] = lsb        ;
334
 
  TRANSFORM( hd, hd->buf, 1 );
335
 
  _gcry_burn_stack (88+4*sizeof(void*));
336
 
 
337
 
  p = hd->buf;
338
 
#ifdef WORDS_BIGENDIAN
339
 
#define X(a) do { *(u32*)p = hd->h##a ; p += 4; } while(0)
340
 
#else /* little endian */
341
 
#define X(a) do { *p++ = hd->h##a >> 24; *p++ = hd->h##a >> 16;  \
342
 
                  *p++ = hd->h##a >> 8; *p++ = hd->h##a; } while(0)
343
 
#endif
344
 
  X(0);
345
 
  X(1);
346
 
  X(2);
347
 
  X(3);
348
 
  X(4);
349
 
#undef X
350
 
 
351
 
}
352
 
 
353
 
static unsigned char *
354
 
sha1_read( void *context )
355
 
{
356
 
  SHA1_CONTEXT *hd = context;
357
 
 
358
 
  return hd->buf;
359
 
}
360
 
 
361
 
/****************
362
 
 * Shortcut functions which puts the hash value of the supplied buffer
363
 
 * into outbuf which must have a size of 20 bytes.
364
 
 */
365
 
void
366
 
_gcry_sha1_hash_buffer (void *outbuf, const void *buffer, size_t length)
367
 
{
368
 
  SHA1_CONTEXT hd;
369
 
 
370
 
  sha1_init (&hd);
371
 
  sha1_write (&hd, buffer, length);
372
 
  sha1_final (&hd);
373
 
  memcpy (outbuf, hd.buf, 20);
374
 
}
375
 
 
376
 
 
377
 
 
378
 
/* 
379
 
     Self-test section.
380
 
 */
381
 
 
382
 
 
383
 
static gpg_err_code_t
384
 
selftests_sha1 (int extended, selftest_report_func_t report)
385
 
{
386
 
  const char *what;
387
 
  const char *errtxt;
388
 
  
389
 
  what = "short string";
390
 
  errtxt = _gcry_hash_selftest_check_one
391
 
    (GCRY_MD_SHA1, 0, 
392
 
     "abc", 3,
393
 
     "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
394
 
     "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D", 20);
395
 
  if (errtxt)
396
 
    goto failed;
397
 
 
398
 
  if (extended)
399
 
    {
400
 
      what = "long string";
401
 
      errtxt = _gcry_hash_selftest_check_one
402
 
        (GCRY_MD_SHA1, 0, 
403
 
         "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56,
404
 
         "\x84\x98\x3E\x44\x1C\x3B\xD2\x6E\xBA\xAE"
405
 
         "\x4A\xA1\xF9\x51\x29\xE5\xE5\x46\x70\xF1", 20);
406
 
      if (errtxt)
407
 
        goto failed;
408
 
      
409
 
      what = "one million \"a\"";
410
 
      errtxt = _gcry_hash_selftest_check_one
411
 
        (GCRY_MD_SHA1, 1,
412
 
         NULL, 0,
413
 
         "\x34\xAA\x97\x3C\xD4\xC4\xDA\xA4\xF6\x1E"
414
 
         "\xEB\x2B\xDB\xAD\x27\x31\x65\x34\x01\x6F", 20);
415
 
      if (errtxt)
416
 
        goto failed;
417
 
    }
418
 
 
419
 
  return 0; /* Succeeded. */
420
 
 
421
 
 failed:
422
 
  if (report)
423
 
    report ("digest", GCRY_MD_SHA1, what, errtxt);
424
 
  return GPG_ERR_SELFTEST_FAILED;
425
 
}
426
 
 
427
 
 
428
 
/* Run a full self-test for ALGO and return 0 on success.  */
429
 
static gpg_err_code_t
430
 
run_selftests (int algo, int extended, selftest_report_func_t report)
431
 
{
432
 
  gpg_err_code_t ec;
433
 
 
434
 
  switch (algo)
435
 
    {
436
 
    case GCRY_MD_SHA1:
437
 
      ec = selftests_sha1 (extended, report);
438
 
      break;
439
 
    default:
440
 
      ec = GPG_ERR_DIGEST_ALGO;
441
 
      break;
442
 
        
443
 
    }
444
 
  return ec;
445
 
}
446
 
 
447
 
 
448
 
 
449
 
 
450
 
static unsigned char asn[15] = /* Object ID is 1.3.14.3.2.26 */
451
 
  { 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03,
452
 
    0x02, 0x1a, 0x05, 0x00, 0x04, 0x14 };
453
 
 
454
 
static gcry_md_oid_spec_t oid_spec_sha1[] =
455
 
  {
456
 
    /* iso.member-body.us.rsadsi.pkcs.pkcs-1.5 (sha1WithRSAEncryption) */
457
 
    { "1.2.840.113549.1.1.5" },
458
 
    /* iso.member-body.us.x9-57.x9cm.3 (dsaWithSha1)*/
459
 
    { "1.2.840.10040.4.3" },
460
 
    /* from NIST's OIW  (sha1) */
461
 
    { "1.3.14.3.2.26" },
462
 
    /* from NIST OIW (sha-1WithRSAEncryption) */
463
 
    { "1.3.14.3.2.29" },
464
 
    /* iso.member-body.us.ansi-x9-62.signatures.ecdsa-with-sha1 */
465
 
    { "1.2.840.10045.4.1" },
466
 
    { NULL },
467
 
  };
468
 
 
469
 
gcry_md_spec_t _gcry_digest_spec_sha1 =
470
 
  {
471
 
    "SHA1", asn, DIM (asn), oid_spec_sha1, 20,
472
 
    sha1_init, sha1_write, sha1_final, sha1_read,
473
 
    sizeof (SHA1_CONTEXT)
474
 
  };
475
 
md_extra_spec_t _gcry_digest_extraspec_sha1 = 
476
 
  {
477
 
    run_selftests
478
 
  };
479