~ubuntu-branches/ubuntu/natty/libgcrypt11/natty-proposed

« back to all changes in this revision

Viewing changes to cipher/sha512.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2009-02-21 13:46:58 UTC
  • mto: (1.1.6 upstream) (2.1.3 squeeze)
  • mto: This revision was merged to the branch mainline in revision 18.
  • Revision ID: james.westby@ubuntu.com-20090221134658-855twvcr4ezk2ron
ImportĀ upstreamĀ versionĀ 1.4.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* sha512.c - SHA384 and SHA512 hash functions
2
 
 *      Copyright (C) 2003 Free Software Foundation, Inc.
3
 
 *
4
 
 * Please see below for more legal information!
 
2
 *      Copyright (C) 2003, 2008 Free Software Foundation, Inc.
5
3
 *
6
4
 * This file is part of Libgcrypt.
7
5
 *
15
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
14
 * GNU Lesser General Public License for more details.
17
15
 *
18
 
 * You should have received a copy of the GNU Lesser General Public License
19
 
 * along with this program; if not, write to the Free Software
20
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 
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/>.
21
18
 */
22
19
 
23
20
 
54
51
#include "g10lib.h"
55
52
#include "bithelp.h"
56
53
#include "cipher.h"
 
54
#include "hash-common.h"
57
55
 
58
56
typedef struct
59
57
{
362
360
  return hd->buf;
363
361
}
364
362
 
 
363
 
 
364
 
 
365
/* 
 
366
     Self-test section.
 
367
 */
 
368
 
 
369
 
 
370
static gpg_err_code_t
 
371
selftests_sha384 (int extended, selftest_report_func_t report)
 
372
{
 
373
  const char *what;
 
374
  const char *errtxt;
 
375
  
 
376
  what = "short string";
 
377
  errtxt = _gcry_hash_selftest_check_one
 
378
    (GCRY_MD_SHA384, 0, 
 
379
     "abc", 3,
 
380
     "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50\x07"
 
381
     "\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff\x5b\xed"
 
382
     "\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34\xc8\x25\xa7", 48);
 
383
  if (errtxt)
 
384
    goto failed;
 
385
 
 
386
  if (extended)
 
387
    {
 
388
      what = "long string";
 
389
      errtxt = _gcry_hash_selftest_check_one
 
390
        (GCRY_MD_SHA384, 0, 
 
391
         "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
 
392
         "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 112, 
 
393
         "\x09\x33\x0C\x33\xF7\x11\x47\xE8\x3D\x19\x2F\xC7\x82\xCD\x1B\x47"
 
394
         "\x53\x11\x1B\x17\x3B\x3B\x05\xD2\x2F\xA0\x80\x86\xE3\xB0\xF7\x12"
 
395
         "\xFC\xC7\xC7\x1A\x55\x7E\x2D\xB9\x66\xC3\xE9\xFA\x91\x74\x60\x39",
 
396
         48);
 
397
      if (errtxt)
 
398
        goto failed;
 
399
 
 
400
      what = "one million \"a\"";
 
401
      errtxt = _gcry_hash_selftest_check_one
 
402
        (GCRY_MD_SHA384, 1,
 
403
         NULL, 0,
 
404
         "\x9D\x0E\x18\x09\x71\x64\x74\xCB\x08\x6E\x83\x4E\x31\x0A\x4A\x1C"
 
405
         "\xED\x14\x9E\x9C\x00\xF2\x48\x52\x79\x72\xCE\xC5\x70\x4C\x2A\x5B"
 
406
         "\x07\xB8\xB3\xDC\x38\xEC\xC4\xEB\xAE\x97\xDD\xD8\x7F\x3D\x89\x85",
 
407
         48);
 
408
      if (errtxt)
 
409
        goto failed;
 
410
    }
 
411
 
 
412
  return 0; /* Succeeded. */
 
413
 
 
414
 failed:
 
415
  if (report)
 
416
    report ("digest", GCRY_MD_SHA384, what, errtxt);
 
417
  return GPG_ERR_SELFTEST_FAILED;
 
418
}
 
419
 
 
420
static gpg_err_code_t
 
421
selftests_sha512 (int extended, selftest_report_func_t report)
 
422
{
 
423
  const char *what;
 
424
  const char *errtxt;
 
425
  
 
426
  what = "short string";
 
427
  errtxt = _gcry_hash_selftest_check_one
 
428
    (GCRY_MD_SHA512, 0, 
 
429
     "abc", 3,
 
430
     "\xDD\xAF\x35\xA1\x93\x61\x7A\xBA\xCC\x41\x73\x49\xAE\x20\x41\x31"
 
431
     "\x12\xE6\xFA\x4E\x89\xA9\x7E\xA2\x0A\x9E\xEE\xE6\x4B\x55\xD3\x9A"
 
432
     "\x21\x92\x99\x2A\x27\x4F\xC1\xA8\x36\xBA\x3C\x23\xA3\xFE\xEB\xBD"
 
433
     "\x45\x4D\x44\x23\x64\x3C\xE8\x0E\x2A\x9A\xC9\x4F\xA5\x4C\xA4\x9F", 64);
 
434
  if (errtxt)
 
435
    goto failed;
 
436
 
 
437
  if (extended)
 
438
    {
 
439
      what = "long string";
 
440
      errtxt = _gcry_hash_selftest_check_one
 
441
        (GCRY_MD_SHA512, 0, 
 
442
         "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
 
443
         "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", 112, 
 
444
         "\x8E\x95\x9B\x75\xDA\xE3\x13\xDA\x8C\xF4\xF7\x28\x14\xFC\x14\x3F"
 
445
         "\x8F\x77\x79\xC6\xEB\x9F\x7F\xA1\x72\x99\xAE\xAD\xB6\x88\x90\x18"
 
446
         "\x50\x1D\x28\x9E\x49\x00\xF7\xE4\x33\x1B\x99\xDE\xC4\xB5\x43\x3A"
 
447
         "\xC7\xD3\x29\xEE\xB6\xDD\x26\x54\x5E\x96\xE5\x5B\x87\x4B\xE9\x09",
 
448
         64);
 
449
      if (errtxt)
 
450
        goto failed;
 
451
      
 
452
      what = "one million \"a\"";
 
453
      errtxt = _gcry_hash_selftest_check_one
 
454
        (GCRY_MD_SHA512, 1,
 
455
         NULL, 0,
 
456
         "\xE7\x18\x48\x3D\x0C\xE7\x69\x64\x4E\x2E\x42\xC7\xBC\x15\xB4\x63"
 
457
         "\x8E\x1F\x98\xB1\x3B\x20\x44\x28\x56\x32\xA8\x03\xAF\xA9\x73\xEB"
 
458
         "\xDE\x0F\xF2\x44\x87\x7E\xA6\x0A\x4C\xB0\x43\x2C\xE5\x77\xC3\x1B"
 
459
         "\xEB\x00\x9C\x5C\x2C\x49\xAA\x2E\x4E\xAD\xB2\x17\xAD\x8C\xC0\x9B",
 
460
         64);
 
461
      if (errtxt)
 
462
        goto failed;
 
463
    }
 
464
 
 
465
  return 0; /* Succeeded. */
 
466
 
 
467
 failed:
 
468
  if (report)
 
469
    report ("digest", GCRY_MD_SHA512, what, errtxt);
 
470
  return GPG_ERR_SELFTEST_FAILED;
 
471
}
 
472
 
 
473
 
 
474
/* Run a full self-test for ALGO and return 0 on success.  */
 
475
static gpg_err_code_t
 
476
run_selftests (int algo, int extended, selftest_report_func_t report)
 
477
{
 
478
  gpg_err_code_t ec;
 
479
 
 
480
  switch (algo)
 
481
    {
 
482
    case GCRY_MD_SHA384:
 
483
      ec = selftests_sha384 (extended, report);
 
484
      break;
 
485
    case GCRY_MD_SHA512:
 
486
      ec = selftests_sha512 (extended, report);
 
487
      break;
 
488
    default:
 
489
      ec = GPG_ERR_DIGEST_ALGO;
 
490
      break;
 
491
        
 
492
    }
 
493
  return ec;
 
494
}
 
495
 
 
496
 
 
497
 
 
498
 
365
499
static byte sha512_asn[] =      /* Object ID is 2.16.840.1.101.3.4.2.3 */
366
 
{
367
 
  0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
368
 
  0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05,
369
 
  0x00, 0x04, 0x40
370
 
};
 
500
  {
 
501
    0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
 
502
    0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05,
 
503
    0x00, 0x04, 0x40
 
504
  };
371
505
 
372
506
static gcry_md_oid_spec_t oid_spec_sha512[] =
373
507
  {
379
513
    { NULL }
380
514
  };
381
515
 
382
 
gcry_md_spec_t _gcry_digest_spec_sha512 = {
383
 
  "SHA512", sha512_asn, DIM (sha512_asn), oid_spec_sha512, 64,
384
 
  sha512_init, sha512_write, sha512_final, sha512_read,
385
 
  sizeof (SHA512_CONTEXT),
386
 
};
 
516
gcry_md_spec_t _gcry_digest_spec_sha512 = 
 
517
  {
 
518
    "SHA512", sha512_asn, DIM (sha512_asn), oid_spec_sha512, 64,
 
519
    sha512_init, sha512_write, sha512_final, sha512_read,
 
520
    sizeof (SHA512_CONTEXT),
 
521
  };
 
522
md_extra_spec_t _gcry_digest_extraspec_sha512 = 
 
523
  {
 
524
    run_selftests
 
525
  };
387
526
 
388
527
static byte sha384_asn[] =      /* Object ID is 2.16.840.1.101.3.4.2.2 */
389
 
{
390
 
  0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
391
 
  0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05,
392
 
  0x00, 0x04, 0x30
393
 
};
 
528
  {
 
529
    0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
 
530
    0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05,
 
531
    0x00, 0x04, 0x30
 
532
  };
394
533
 
395
534
static gcry_md_oid_spec_t oid_spec_sha384[] =
396
535
  {
402
541
    { NULL },
403
542
  };
404
543
 
405
 
gcry_md_spec_t _gcry_digest_spec_sha384 = {
406
 
  "SHA384", sha384_asn, DIM (sha384_asn), oid_spec_sha384, 48,
407
 
  sha384_init, sha512_write, sha512_final, sha512_read,
408
 
  sizeof (SHA512_CONTEXT),
409
 
};
 
544
gcry_md_spec_t _gcry_digest_spec_sha384 = 
 
545
  {
 
546
    "SHA384", sha384_asn, DIM (sha384_asn), oid_spec_sha384, 48,
 
547
    sha384_init, sha512_write, sha512_final, sha512_read,
 
548
    sizeof (SHA512_CONTEXT),
 
549
  };
 
550
md_extra_spec_t _gcry_digest_extraspec_sha384 = 
 
551
  {
 
552
    run_selftests
 
553
  };