~ubuntu-branches/ubuntu/trusty/grub2/trusty

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2014-01-16 15:18:04 UTC
  • mfrom: (17.6.38 experimental)
  • Revision ID: package-import@ubuntu.com-20140116151804-3foouk7fpqcq3sxx
Tags: 2.02~beta2-2
* Convert patch handling to git-dpm.
* Add bi-endian support to ELF parser (Tomohiro B Berry).
* Adjust restore_mkdevicemap.patch to mark get_kfreebsd_version as static,
  to appease "gcc -Werror=missing-prototypes".
* Cherry-pick from upstream:
  - Change grub-macbless' manual page section to 8.
* Install grub-glue-efi, grub-macbless, grub-render-label, and
  grub-syslinux2cfg.
* grub-shell: Pass -no-pad to xorriso when building floppy images.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
#include <grub/dl.h>
4
4
GRUB_MOD_LICENSE ("GPLv3+");
5
5
/* sha512.c - SHA384 and SHA512 hash functions
6
 
 *      Copyright (C) 2003, 2008 Free Software Foundation, Inc.
 
6
 * Copyright (C) 2003, 2008, 2009 Free Software Foundation, Inc.
7
7
 *
8
8
 * This file is part of Libgcrypt.
9
9
 *
100
100
}
101
101
 
102
102
 
 
103
static inline u64
 
104
ROTR (u64 x, u64 n)
 
105
{
 
106
  return ((x >> n) | (x << (64 - n)));
 
107
}
 
108
 
 
109
static inline u64
 
110
Ch (u64 x, u64 y, u64 z)
 
111
{
 
112
  return ((x & y) ^ ( ~x & z));
 
113
}
 
114
 
 
115
static inline u64
 
116
Maj (u64 x, u64 y, u64 z)
 
117
{
 
118
  return ((x & y) ^ (x & z) ^ (y & z));
 
119
}
 
120
 
 
121
static inline u64
 
122
Sum0 (u64 x)
 
123
{
 
124
  return (ROTR (x, 28) ^ ROTR (x, 34) ^ ROTR (x, 39));
 
125
}
 
126
 
 
127
static inline u64
 
128
Sum1 (u64 x)
 
129
{
 
130
  return (ROTR (x, 14) ^ ROTR (x, 18) ^ ROTR (x, 41));
 
131
}
 
132
 
103
133
/****************
104
134
 * Transform the message W which consists of 16 64-bit-words
105
135
 */
184
214
  }
185
215
#endif
186
216
 
187
 
#define ROTR(x,n) (((x)>>(n)) | ((x)<<(64-(n))))
188
 
#define Ch(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
189
 
#define Maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
190
 
#define Sum0(x) (ROTR((x),28) ^ ROTR((x),34) ^ ROTR((x),39))
191
 
#define Sum1(x) (ROTR((x),14) ^ ROTR((x),18) ^ ROTR((x),41))
192
217
#define S0(x) (ROTR((x),1) ^ ROTR((x),8) ^ ((x)>>7))
193
218
#define S1(x) (ROTR((x),19) ^ ROTR((x),61) ^ ((x)>>6))
194
219
 
195
220
  for (t = 16; t < 80; t++)
196
221
    w[t] = S1 (w[t - 2]) + w[t - 7] + S0 (w[t - 15]) + w[t - 16];
197
222
 
198
 
  for (t = 0; t < 80; t++)
 
223
 
 
224
  for (t = 0; t < 80; )
199
225
    {
200
226
      u64 t1, t2;
201
227
 
 
228
      /* Performance on a AMD Athlon(tm) Dual Core Processor 4050e
 
229
         with gcc 4.3.3 using gcry_md_hash_buffer of each 10000 bytes
 
230
         initialized to 0,1,2,3...255,0,... and 1000 iterations:
 
231
 
 
232
         Not unrolled with macros:  440ms
 
233
         Unrolled with macros:      350ms
 
234
         Unrolled with inline:      330ms
 
235
      */
 
236
#if 0 /* Not unrolled.  */
202
237
      t1 = h + Sum1 (e) + Ch (e, f, g) + k[t] + w[t];
203
238
      t2 = Sum0 (a) + Maj (a, b, c);
204
239
      h = g;
209
244
      c = b;
210
245
      b = a;
211
246
      a = t1 + t2;
212
 
 
213
 
      /* printf("t=%d a=%016llX b=%016llX c=%016llX d=%016llX "
214
 
          "e=%016llX f=%016llX g=%016llX h=%016llX\n",t,a,b,c,d,e,f,g,h); */
 
247
      t++;
 
248
#else /* Unrolled to interweave the chain variables.  */
 
249
      t1 = h + Sum1 (e) + Ch (e, f, g) + k[t] + w[t];
 
250
      t2 = Sum0 (a) + Maj (a, b, c);
 
251
      d += t1;
 
252
      h  = t1 + t2;
 
253
 
 
254
      t1 = g + Sum1 (d) + Ch (d, e, f) + k[t+1] + w[t+1];
 
255
      t2 = Sum0 (h) + Maj (h, a, b);
 
256
      c += t1;
 
257
      g  = t1 + t2;
 
258
 
 
259
      t1 = f + Sum1 (c) + Ch (c, d, e) + k[t+2] + w[t+2];
 
260
      t2 = Sum0 (g) + Maj (g, h, a);
 
261
      b += t1;
 
262
      f  = t1 + t2;
 
263
 
 
264
      t1 = e + Sum1 (b) + Ch (b, c, d) + k[t+3] + w[t+3];
 
265
      t2 = Sum0 (f) + Maj (f, g, h);
 
266
      a += t1;
 
267
      e  = t1 + t2;
 
268
 
 
269
      t1 = d + Sum1 (a) + Ch (a, b, c) + k[t+4] + w[t+4];
 
270
      t2 = Sum0 (e) + Maj (e, f, g);
 
271
      h += t1;
 
272
      d  = t1 + t2;
 
273
 
 
274
      t1 = c + Sum1 (h) + Ch (h, a, b) + k[t+5] + w[t+5];
 
275
      t2 = Sum0 (d) + Maj (d, e, f);
 
276
      g += t1;
 
277
      c  = t1 + t2;
 
278
 
 
279
      t1 = b + Sum1 (g) + Ch (g, h, a) + k[t+6] + w[t+6];
 
280
      t2 = Sum0 (c) + Maj (c, d, e);
 
281
      f += t1;
 
282
      b  = t1 + t2;
 
283
 
 
284
      t1 = a + Sum1 (f) + Ch (f, g, h) + k[t+7] + w[t+7];
 
285
      t2 = Sum0 (b) + Maj (b, c, d);
 
286
      e += t1;
 
287
      a  = t1 + t2;
 
288
 
 
289
      t += 8;
 
290
#endif
215
291
    }
216
292
 
217
 
  /* update chaining vars */
 
293
  /* Update chaining vars.  */
218
294
  hd->h0 += a;
219
295
  hd->h1 += b;
220
296
  hd->h2 += c;
364
440
 
365
441
 
366
442
 
367
 
/* 
 
443
/*
368
444
     Self-test section.
369
445
 */
370
446
 
394
470
    { NULL }
395
471
  };
396
472
 
397
 
gcry_md_spec_t _gcry_digest_spec_sha512 = 
 
473
gcry_md_spec_t _gcry_digest_spec_sha512 =
398
474
  {
399
475
    "SHA512", sha512_asn, DIM (sha512_asn), oid_spec_sha512, 64,
400
476
    sha512_init, sha512_write, sha512_final, sha512_read,
414
490
 
415
491
static gcry_md_oid_spec_t oid_spec_sha384[] =
416
492
  {
417
 
    { "2.16.840.1.101.3.4.2.2" }, 
 
493
    { "2.16.840.1.101.3.4.2.2" },
418
494
 
419
495
    /* PKCS#1 sha384WithRSAEncryption */
420
496
    { "1.2.840.113549.1.1.12" },
422
498
    { NULL },
423
499
  };
424
500
 
425
 
gcry_md_spec_t _gcry_digest_spec_sha384 = 
 
501
gcry_md_spec_t _gcry_digest_spec_sha384 =
426
502
  {
427
503
    "SHA384", sha384_asn, DIM (sha384_asn), oid_spec_sha384, 48,
428
504
    sha384_init, sha512_write, sha512_final, sha512_read,
436
512
 
437
513
GRUB_MOD_INIT(gcry_sha512)
438
514
{
 
515
  COMPILE_TIME_ASSERT(sizeof (SHA512_CONTEXT) <= GRUB_CRYPTO_MAX_MD_CONTEXT_SIZE);
 
516
  COMPILE_TIME_ASSERT(sizeof (SHA512_CONTEXT) <= GRUB_CRYPTO_MAX_MD_CONTEXT_SIZE);
439
517
  grub_md_register (&_gcry_digest_spec_sha512);
440
518
  grub_md_register (&_gcry_digest_spec_sha384);
441
519
}