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

« back to all changes in this revision

Viewing changes to cipher/cipher.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
/* cipher.c  -  cipher dispatcher
2
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
3
 
 *               2005, 2007 Free Software Foundation, Inc.
 
3
 *               2005, 2007, 2008 Free Software Foundation, Inc.
4
4
 *
5
5
 * This file is part of Libgcrypt.
6
6
 *
38
38
#define NEED_16BYTE_ALIGNED_CONTEXT 1
39
39
#endif
40
40
 
 
41
/* A dummy extraspec so that we do not need to tests the extraspec
 
42
   field from the module specification against NULL and instead
 
43
   directly test the respective fields of extraspecs.  */
 
44
static cipher_extra_spec_t dummy_extra_spec;
 
45
 
41
46
/* This is the list of the default ciphers, which are included in
42
47
   libgcrypt.  */
43
48
static struct cipher_table_entry
44
49
{
45
50
  gcry_cipher_spec_t *cipher;
 
51
  cipher_extra_spec_t *extraspec;
46
52
  unsigned int algorithm;
 
53
  int fips_allowed;
47
54
} cipher_table[] =
48
55
  {
49
56
#if USE_BLOWFISH
50
 
    { &_gcry_cipher_spec_blowfish,   GCRY_CIPHER_BLOWFISH },
 
57
    { &_gcry_cipher_spec_blowfish,
 
58
      &dummy_extra_spec,                  GCRY_CIPHER_BLOWFISH },
51
59
#endif
52
60
#if USE_DES
53
 
    { &_gcry_cipher_spec_des,        GCRY_CIPHER_DES },
54
 
    { &_gcry_cipher_spec_tripledes,  GCRY_CIPHER_3DES },
 
61
    { &_gcry_cipher_spec_des,       
 
62
      &dummy_extra_spec,                  GCRY_CIPHER_DES },
 
63
    { &_gcry_cipher_spec_tripledes,
 
64
      &_gcry_cipher_extraspec_tripledes,  GCRY_CIPHER_3DES, 1 },
55
65
#endif
56
66
#if USE_ARCFOUR
57
 
    { &_gcry_cipher_spec_arcfour,    GCRY_CIPHER_ARCFOUR },
 
67
    { &_gcry_cipher_spec_arcfour,    
 
68
      &dummy_extra_spec,                  GCRY_CIPHER_ARCFOUR },
58
69
#endif
59
70
#if USE_CAST5
60
 
    { &_gcry_cipher_spec_cast5,      GCRY_CIPHER_CAST5 },
 
71
    { &_gcry_cipher_spec_cast5,      
 
72
      &dummy_extra_spec,                  GCRY_CIPHER_CAST5 },
61
73
#endif
62
74
#if USE_AES
63
 
    { &_gcry_cipher_spec_aes,        GCRY_CIPHER_AES},
64
 
    { &_gcry_cipher_spec_aes192,     GCRY_CIPHER_AES192},
65
 
    { &_gcry_cipher_spec_aes256,     GCRY_CIPHER_AES256},
 
75
    { &_gcry_cipher_spec_aes,        
 
76
      &_gcry_cipher_extraspec_aes,        GCRY_CIPHER_AES,    1 },
 
77
    { &_gcry_cipher_spec_aes192,     
 
78
      &_gcry_cipher_extraspec_aes192,     GCRY_CIPHER_AES192, 1 },
 
79
    { &_gcry_cipher_spec_aes256,     
 
80
      &_gcry_cipher_extraspec_aes256,     GCRY_CIPHER_AES256, 1 },
66
81
#endif
67
82
#if USE_TWOFISH
68
 
    { &_gcry_cipher_spec_twofish,    GCRY_CIPHER_TWOFISH },
69
 
    { &_gcry_cipher_spec_twofish128, GCRY_CIPHER_TWOFISH128 },
 
83
    { &_gcry_cipher_spec_twofish,
 
84
      &dummy_extra_spec,                  GCRY_CIPHER_TWOFISH },
 
85
    { &_gcry_cipher_spec_twofish128,     
 
86
      &dummy_extra_spec,                  GCRY_CIPHER_TWOFISH128 },
70
87
#endif
71
88
#if USE_SERPENT
72
 
    { &_gcry_cipher_spec_serpent128, GCRY_CIPHER_SERPENT128 },
73
 
    { &_gcry_cipher_spec_serpent192, GCRY_CIPHER_SERPENT192 },
74
 
    { &_gcry_cipher_spec_serpent256, GCRY_CIPHER_SERPENT256 },
 
89
    { &_gcry_cipher_spec_serpent128, 
 
90
      &dummy_extra_spec,                  GCRY_CIPHER_SERPENT128 },
 
91
    { &_gcry_cipher_spec_serpent192,
 
92
      &dummy_extra_spec,                  GCRY_CIPHER_SERPENT192 },
 
93
    { &_gcry_cipher_spec_serpent256, 
 
94
      &dummy_extra_spec,                  GCRY_CIPHER_SERPENT256 },
75
95
#endif
76
96
#if USE_RFC2268
77
 
    { &_gcry_cipher_spec_rfc2268_40, GCRY_CIPHER_RFC2268_40 },
 
97
    { &_gcry_cipher_spec_rfc2268_40,
 
98
      &dummy_extra_spec,                  GCRY_CIPHER_RFC2268_40 },
78
99
#endif
79
100
#if USE_SEED
80
 
    { &_gcry_cipher_spec_seed,       GCRY_CIPHER_SEED },
 
101
    { &_gcry_cipher_spec_seed, 
 
102
      &dummy_extra_spec,                  GCRY_CIPHER_SEED },
81
103
#endif
82
104
#if USE_CAMELLIA
83
 
    { &_gcry_cipher_spec_camellia128, GCRY_CIPHER_CAMELLIA128 },
84
 
    { &_gcry_cipher_spec_camellia192, GCRY_CIPHER_CAMELLIA192 },
85
 
    { &_gcry_cipher_spec_camellia256, GCRY_CIPHER_CAMELLIA256 },
 
105
    { &_gcry_cipher_spec_camellia128,
 
106
      &dummy_extra_spec,                  GCRY_CIPHER_CAMELLIA128 },
 
107
    { &_gcry_cipher_spec_camellia192, 
 
108
      &dummy_extra_spec,                  GCRY_CIPHER_CAMELLIA192 },
 
109
    { &_gcry_cipher_spec_camellia256,
 
110
      &dummy_extra_spec,                  GCRY_CIPHER_CAMELLIA256 },
86
111
#endif
87
112
    { NULL                    }
88
113
  };
104
129
      ath_mutex_lock (&ciphers_registered_lock);   \
105
130
      if (! default_ciphers_registered)            \
106
131
        {                                          \
107
 
          gcry_cipher_register_default ();         \
 
132
          cipher_register_default ();              \
108
133
          default_ciphers_registered = 1;          \
109
134
        }                                          \
110
135
      ath_mutex_unlock (&ciphers_registered_lock); \
136
161
  size_t actual_handle_size;     /* Allocated size of this handle. */
137
162
  size_t handle_offset;          /* Offset to the malloced block.  */
138
163
  gcry_cipher_spec_t *cipher;
 
164
  cipher_extra_spec_t *extraspec;
139
165
  gcry_module_t module;
140
166
 
141
167
  /* The algorithm id.  This is a hack required because the module
251
277
   CIPHER_TABLE.  Note, that this function gets only used by the macro
252
278
   REGISTER_DEFAULT_CIPHERS which protects it using a mutex. */
253
279
static void
254
 
gcry_cipher_register_default (void)
 
280
cipher_register_default (void)
255
281
{
256
282
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
257
283
  int i;
269
295
      if (! cipher_table[i].cipher->stdecrypt)
270
296
        cipher_table[i].cipher->stdecrypt = dummy_decrypt_stream;
271
297
 
 
298
      if ( fips_mode () && !cipher_table[i].fips_allowed )
 
299
        continue;
 
300
 
272
301
      err = _gcry_module_add (&ciphers_registered,
273
302
                              cipher_table[i].algorithm,
274
303
                              (void *) cipher_table[i].cipher,
 
304
                              (void *) cipher_table[i].extraspec,
275
305
                              NULL);
276
306
    }
277
307
 
340
370
   CIPHER.  On success, a new algorithm ID is stored in ALGORITHM_ID
341
371
   and a pointer representhing this module is stored in MODULE.  */
342
372
gcry_error_t
343
 
gcry_cipher_register (gcry_cipher_spec_t *cipher,
344
 
                      int *algorithm_id,
345
 
                      gcry_module_t *module)
 
373
_gcry_cipher_register (gcry_cipher_spec_t *cipher,
 
374
                       cipher_extra_spec_t *extraspec,
 
375
                       int *algorithm_id,
 
376
                       gcry_module_t *module)
346
377
{
347
378
  gcry_err_code_t err = 0;
348
379
  gcry_module_t mod;
349
380
 
 
381
  /* We do not support module loading in fips mode.  */
 
382
  if (fips_mode ())
 
383
    return gpg_error (GPG_ERR_NOT_SUPPORTED);
 
384
 
350
385
  ath_mutex_lock (&ciphers_registered_lock);
351
386
  err = _gcry_module_add (&ciphers_registered, 0,
352
 
                          (void *) cipher, &mod);
 
387
                          (void *)cipher, 
 
388
                          (void *)(extraspec? extraspec : &dummy_extra_spec), 
 
389
                          &mod);
353
390
  ath_mutex_unlock (&ciphers_registered_lock);
354
391
 
355
392
  if (! err)
529
566
}
530
567
 
531
568
 
532
 
/* Return 0 if the cipher algorithm with indentifier ALGORITHM is
533
 
   available. Returns a basic error code value if it is not available.  */
 
569
/* Return 0 if the cipher algorithm with identifier ALGORITHM is
 
570
   available. Returns a basic error code value if it is not
 
571
   available.  */
534
572
static gcry_err_code_t
535
573
check_cipher_algo (int algorithm)
536
574
{
572
610
  if (cipher)
573
611
    {
574
612
      len = ((gcry_cipher_spec_t *) cipher->spec)->keylen;
575
 
      if (! len)
 
613
      if (!len)
576
614
        log_bug ("cipher %d w/o key length\n", algorithm);
577
615
      _gcry_module_release (cipher);
578
616
    }
632
670
{
633
671
  int secure = (flags & GCRY_CIPHER_SECURE);
634
672
  gcry_cipher_spec_t *cipher = NULL;
 
673
  cipher_extra_spec_t *extraspec = NULL;
635
674
  gcry_module_t module = NULL;
636
675
  gcry_cipher_hd_t h = NULL;
637
676
  gcry_err_code_t err = 0;
657
696
          _gcry_module_release (module);
658
697
        }
659
698
      else
660
 
        cipher = (gcry_cipher_spec_t *) module->spec;
 
699
        {
 
700
          cipher = (gcry_cipher_spec_t *) module->spec;
 
701
          extraspec = module->extraspec;
 
702
        }
661
703
    }
662
704
  else
663
705
    err = GPG_ERR_CIPHER_ALGO;
694
736
        break;
695
737
 
696
738
      case GCRY_CIPHER_MODE_NONE:
697
 
        /* FIXME: issue a warning when this mode is used */
 
739
        /* This mode may be used for debugging.  It copies the main
 
740
           text verbatim to the ciphertext.  We do not allow this in
 
741
           fips mode or if no debug flag has been set.  */
 
742
        if (fips_mode () || !_gcry_get_debug_flag (0))
 
743
          err = GPG_ERR_INV_CIPHER_MODE;
698
744
        break;
699
745
 
700
746
      default:
742
788
          h->actual_handle_size = size - off;
743
789
          h->handle_offset = off;
744
790
          h->cipher = cipher;
 
791
          h->extraspec = extraspec;
745
792
          h->module = module;
746
793
          h->algo = algo;
747
794
          h->mode = mode;
793
840
{
794
841
  size_t off;
795
842
 
796
 
  if (! h)
 
843
  if (!h)
797
844
    return;
798
845
 
799
846
  if ((h->magic != CTX_MAGIC_SECURE)
824
871
/* Set the key to be used for the encryption context C to KEY with
825
872
   length KEYLEN.  The length should match the required length. */
826
873
static gcry_error_t
827
 
cipher_setkey (gcry_cipher_hd_t c, byte *key, unsigned keylen)
 
874
cipher_setkey (gcry_cipher_hd_t c, byte *key, unsigned int keylen)
828
875
{
829
876
  gcry_err_code_t ret;
830
877
 
831
878
  ret = (*c->cipher->setkey) (&c->context.c, key, keylen);
832
 
  if (! ret)
833
 
    /* Duplicate initial context.  */
834
 
    memcpy ((void *) ((char *) &c->context.c + c->cipher->contextsize),
835
 
            (void *) &c->context.c,
836
 
            c->cipher->contextsize);
 
879
  if (!ret)
 
880
    {
 
881
      /* Duplicate initial context.  */
 
882
      memcpy ((void *) ((char *) &c->context.c + c->cipher->contextsize),
 
883
              (void *) &c->context.c,
 
884
              c->cipher->contextsize);
 
885
    }
837
886
 
838
887
  return gcry_error (ret);
839
888
}
848
897
  if (iv) 
849
898
    {
850
899
      if (ivlen != c->cipher->blocksize)
851
 
        log_info ("WARNING: cipher_setiv: ivlen=%u blklen=%u\n",
852
 
                  ivlen, (unsigned int)c->cipher->blocksize);
 
900
        {
 
901
          log_info ("WARNING: cipher_setiv: ivlen=%u blklen=%u\n",
 
902
                    ivlen, (unsigned int)c->cipher->blocksize);
 
903
          fips_signal_error ("IV length does not match blocklength");
 
904
        }
853
905
      if (ivlen > c->cipher->blocksize)
854
906
        ivlen = c->cipher->blocksize;
855
907
      memcpy (c->u_iv.iv, iv, ivlen);
876
928
do_ecb_encrypt( gcry_cipher_hd_t c, byte *outbuf, const byte *inbuf,
877
929
                unsigned int nblocks )
878
930
{
879
 
    unsigned int n;
880
 
 
881
 
    for(n=0; n < nblocks; n++ ) {
882
 
        c->cipher->encrypt ( &c->context.c, outbuf, (byte*)/*arggg*/inbuf );
883
 
        inbuf  += c->cipher->blocksize;
884
 
        outbuf += c->cipher->blocksize;
 
931
  unsigned int n;
 
932
  
 
933
  for (n=0; n < nblocks; n++ )
 
934
    {
 
935
      c->cipher->encrypt ( &c->context.c, outbuf, (byte*)/*arggg*/inbuf );
 
936
      inbuf  += c->cipher->blocksize;
 
937
      outbuf += c->cipher->blocksize;
885
938
    }
886
939
}
887
940
 
889
942
do_ecb_decrypt( gcry_cipher_hd_t c, byte *outbuf, const byte *inbuf,
890
943
                unsigned int nblocks )
891
944
{
892
 
    unsigned n;
 
945
  unsigned int n;
893
946
 
894
 
    for(n=0; n < nblocks; n++ ) {
895
 
        c->cipher->decrypt ( &c->context.c, outbuf, (byte*)/*arggg*/inbuf );
896
 
        inbuf  += c->cipher->blocksize;
897
 
        outbuf += c->cipher->blocksize;
 
947
  for (n=0; n < nblocks; n++ ) 
 
948
    {
 
949
      c->cipher->decrypt ( &c->context.c, outbuf, (byte*)/*arggg*/inbuf );
 
950
      inbuf  += c->cipher->blocksize;
 
951
      outbuf += c->cipher->blocksize;
898
952
    }
899
953
}
900
954
 
994
1048
      for (n=0; n < nblocks; n++ ) 
995
1049
        {
996
1050
          /* Because outbuf and inbuf might be the same, we have to
997
 
           * save the original ciphertext block.  We use lastiv for
 
1051
           * save the original ciphertext block.  We use LASTIV for
998
1052
           * this here because it is not used otherwise. */
999
1053
          memcpy (c->lastiv, inbuf, blocksize);
1000
1054
          c->cipher->decrypt ( &c->context.c, outbuf, inbuf );
1377
1431
                               outbuf, (byte*)/*arggg*/inbuf, nbytes );
1378
1432
        break;
1379
1433
      case GCRY_CIPHER_MODE_NONE:
1380
 
        if( inbuf != outbuf )
1381
 
            memmove( outbuf, inbuf, nbytes );
 
1434
        if (fips_mode () || !_gcry_get_debug_flag (0))
 
1435
          {
 
1436
            fips_signal_error ("cipher mode NONE used");
 
1437
            rc = GPG_ERR_INV_CIPHER_MODE;
 
1438
          }
 
1439
        else
 
1440
          {
 
1441
            if ( inbuf != outbuf )
 
1442
              memmove (outbuf, inbuf, nbytes);
 
1443
          }
1382
1444
        break;
1383
1445
      default:
1384
1446
        log_fatal("cipher_encrypt: invalid mode %d\n", c->mode );
1400
1462
  gcry_err_code_t err;
1401
1463
 
1402
1464
  if (!in)
1403
 
    /* Caller requested in-place encryption. */
1404
 
    /* Actually cipher_encrypt() does not need to know about it, but
1405
 
     * we may change it in the future to get better performance.  */
1406
 
    err = cipher_encrypt (h, out, out, outsize);
 
1465
    {
 
1466
      /* Caller requested in-place encryption. */
 
1467
      /* Actually cipher_encrypt() does not need to know about it, but
 
1468
       * we may change it in the future to get better performance.  */
 
1469
      err = cipher_encrypt (h, out, out, outsize);
 
1470
    }
1407
1471
  else if (outsize < ((h->flags & GCRY_CIPHER_CBC_MAC) ?
1408
1472
                      h->cipher->blocksize : inlen))
1409
1473
    err = GPG_ERR_TOO_SHORT;
1466
1530
                               outbuf, (byte*)/*arggg*/inbuf, nbytes );
1467
1531
        break;
1468
1532
      case GCRY_CIPHER_MODE_NONE:
1469
 
        if( inbuf != outbuf )
1470
 
            memmove( outbuf, inbuf, nbytes );
 
1533
        if (fips_mode () || !_gcry_get_debug_flag (0))
 
1534
          {
 
1535
            fips_signal_error ("cipher mode NONE used");
 
1536
            rc = GPG_ERR_INV_CIPHER_MODE;
 
1537
          }
 
1538
        else
 
1539
          {
 
1540
            if (inbuf != outbuf)
 
1541
              memmove (outbuf, inbuf, nbytes);
 
1542
          }
1471
1543
        break;
1472
1544
      default:
1473
1545
        log_fatal ("cipher_decrypt: invalid mode %d\n", c->mode );
1485
1557
  gcry_err_code_t err = 0;
1486
1558
 
1487
1559
  if (!in)
1488
 
    /* Caller requested in-place encryption. */
1489
 
    /* Actually cipher_encrypt() does not need to know about it, but
1490
 
     * we may change it in the future to get better performance.  */
1491
 
    err = cipher_decrypt (h, out, out, outsize);
 
1560
    {
 
1561
      /* Caller requested in-place encryption. */
 
1562
      /* Actually cipher_encrypt() does not need to know about it, but
 
1563
       * we may change it in the future to get better performance.  */
 
1564
      err = cipher_decrypt (h, out, out, outsize);
 
1565
    }
1492
1566
  else if (outsize < inlen)
1493
1567
    err = GPG_ERR_TOO_SHORT;
1494
1568
  else if (((h->mode == GCRY_CIPHER_MODE_ECB)
1510
1584
 * the corresponding flag is set.
1511
1585
 */
1512
1586
static void
1513
 
cipher_sync( gcry_cipher_hd_t c )
 
1587
cipher_sync (gcry_cipher_hd_t c)
1514
1588
{
1515
1589
  if ((c->flags & GCRY_CIPHER_ENABLE_SYNC) && c->unused)
1516
1590
    {
1524
1598
 
1525
1599
 
1526
1600
gcry_error_t
 
1601
_gcry_cipher_setkey (gcry_cipher_hd_t hd, const void *key, size_t keylen)
 
1602
{
 
1603
  return cipher_setkey (hd, (void*)key, keylen);
 
1604
}
 
1605
 
 
1606
 
 
1607
gcry_error_t
 
1608
_gcry_cipher_setiv (gcry_cipher_hd_t hd, const void *iv, size_t ivlen)
 
1609
{
 
1610
  cipher_setiv (hd, iv, ivlen);
 
1611
  return 0;
 
1612
}
 
1613
 
 
1614
/* Set counter for CTR mode.  (CTR,CTRLEN) must denote a buffer of
 
1615
   block size length, or (NULL,0) to set the CTR to the all-zero
 
1616
   block. */
 
1617
gpg_error_t
 
1618
_gcry_cipher_setctr (gcry_cipher_hd_t hd, const void *ctr, size_t ctrlen)
 
1619
{
 
1620
  if (ctr && ctrlen == hd->cipher->blocksize)
 
1621
    memcpy (hd->ctr, ctr, hd->cipher->blocksize);
 
1622
  else if (!ctr || !ctrlen)
 
1623
    memset (hd->ctr, 0, hd->cipher->blocksize);
 
1624
  else
 
1625
    return gpg_error (GPG_ERR_INV_ARG);
 
1626
  return 0;
 
1627
}
 
1628
 
 
1629
 
 
1630
gcry_error_t
1527
1631
gcry_cipher_ctl( gcry_cipher_hd_t h, int cmd, void *buffer, size_t buflen)
1528
1632
{
1529
1633
  gcry_err_code_t rc = GPG_ERR_NO_ERROR;
1530
1634
 
1531
1635
  switch (cmd)
1532
1636
    {
1533
 
    case GCRYCTL_SET_KEY:
 
1637
    case GCRYCTL_SET_KEY:  /* Deprecated; use gcry_cipher_setkey.  */
1534
1638
      rc = cipher_setkey( h, buffer, buflen );
1535
1639
      break;
1536
 
    case GCRYCTL_SET_IV:
 
1640
 
 
1641
    case GCRYCTL_SET_IV:   /* Deprecated; use gcry_cipher_setiv.  */
1537
1642
      cipher_setiv( h, buffer, buflen );
1538
1643
      break;
 
1644
 
1539
1645
    case GCRYCTL_RESET:
1540
1646
      cipher_reset (h);
1541
1647
      break;
 
1648
 
1542
1649
    case GCRYCTL_CFB_SYNC:
1543
1650
      cipher_sync( h );
1544
1651
      break;
 
1652
 
1545
1653
    case GCRYCTL_SET_CBC_CTS:
1546
1654
      if (buflen)
1547
1655
        if (h->flags & GCRY_CIPHER_CBC_MAC)
1551
1659
      else
1552
1660
        h->flags &= ~GCRY_CIPHER_CBC_CTS;
1553
1661
      break;
 
1662
 
1554
1663
    case GCRYCTL_SET_CBC_MAC:
1555
1664
      if (buflen)
1556
1665
        if (h->flags & GCRY_CIPHER_CBC_CTS)
1560
1669
      else
1561
1670
        h->flags &= ~GCRY_CIPHER_CBC_MAC;
1562
1671
      break;
 
1672
 
1563
1673
    case GCRYCTL_DISABLE_ALGO:
1564
 
      /* this one expects a NULL handle and buffer pointing to an
1565
 
       * integer with the algo number.
1566
 
       */
 
1674
      /* This command expects NULL for H and BUFFER to point to an
 
1675
         integer with the algo number.  */
1567
1676
      if( h || !buffer || buflen != sizeof(int) )
1568
1677
        return gcry_error (GPG_ERR_CIPHER_ALGO);
1569
1678
      disable_cipher_algo( *(int*)buffer );
1570
1679
      break;
1571
 
    case GCRYCTL_SET_CTR:
 
1680
 
 
1681
    case GCRYCTL_SET_CTR: /* Deprecated; use gcry_cipher_setctr.  */
1572
1682
      if (buffer && buflen == h->cipher->blocksize)
1573
1683
        memcpy (h->ctr, buffer, h->cipher->blocksize);
1574
1684
      else if (buffer == NULL || buflen == 0)
1577
1687
        rc = GPG_ERR_INV_ARG;
1578
1688
      break;
1579
1689
 
 
1690
    case 61:  /* Disable weak key detection (private).  */
 
1691
      if (h->extraspec->set_extra_info)
 
1692
        rc = h->extraspec->set_extra_info 
 
1693
          (&h->context.c, CIPHER_INFO_NO_WEAK_KEY, NULL, 0);
 
1694
      else
 
1695
        rc = GPG_ERR_NOT_SUPPORTED;
 
1696
      break;
 
1697
 
 
1698
    case 62: /* Return current input vector (private).  */
 
1699
      /* This is the input block as used in CFB and OFB mode which has
 
1700
         initially been set as IV.  The returned format is: 
 
1701
           1 byte  Actual length of the block in bytes.
 
1702
           n byte  The block.
 
1703
         If the provided buffer is too short, an error is returned. */
 
1704
      if (buflen < (1 + h->cipher->blocksize))
 
1705
        rc = GPG_ERR_TOO_SHORT;
 
1706
      else
 
1707
        {
 
1708
          unsigned char *ivp;
 
1709
          unsigned char *dst = buffer;
 
1710
          int n = h->unused;
 
1711
          
 
1712
          if (!n)
 
1713
            n = h->cipher->blocksize;
 
1714
          gcry_assert (n <= h->cipher->blocksize);
 
1715
          *dst++ = n;
 
1716
          ivp = h->u_iv.iv + h->cipher->blocksize - n;
 
1717
          while (n--)
 
1718
            *dst++ = *ivp++;
 
1719
        }
 
1720
      break;
 
1721
 
1580
1722
    default:
1581
1723
      rc = GPG_ERR_INV_OP;
1582
1724
    }
1585
1727
}
1586
1728
 
1587
1729
 
1588
 
/****************
1589
 
 * Return information about the cipher handle.
 
1730
/* Return information about the cipher handle H.  CMD is the kind of
 
1731
   information requested.  BUFFER and NBYTES are reserved for now.
 
1732
 
 
1733
   There are no values for CMD yet defined.  
 
1734
 
 
1735
   The fucntion always returns GPG_ERR_INV_OP.
 
1736
   
1590
1737
 */
1591
1738
gcry_error_t
1592
 
gcry_cipher_info( gcry_cipher_hd_t h, int cmd, void *buffer, size_t *nbytes)
 
1739
gcry_cipher_info (gcry_cipher_hd_t h, int cmd, void *buffer, size_t *nbytes)
1593
1740
{
1594
1741
  gcry_err_code_t err = GPG_ERR_NO_ERROR;
1595
1742
 
1606
1753
  return gcry_error (err);
1607
1754
}
1608
1755
 
1609
 
/****************
1610
 
 * Return information about the given cipher algorithm
1611
 
 * WHAT select the kind of information returned:
1612
 
 *  GCRYCTL_GET_KEYLEN:
1613
 
 *      Return the length of the key, if the algorithm
1614
 
 *      supports multiple key length, the maximum supported value
1615
 
 *      is returnd.  The length is return as number of octets.
1616
 
 *      buffer and nbytes must be zero.
1617
 
 *      The keylength is returned in _bytes_.
1618
 
 *  GCRYCTL_GET_BLKLEN:
1619
 
 *      Return the blocklength of the algorithm counted in octets.
1620
 
 *      buffer and nbytes must be zero.
1621
 
 *  GCRYCTL_TEST_ALGO:
1622
 
 *      Returns 0 when the specified algorithm is available for use.
1623
 
 *      buffer and nbytes must be zero.
1624
 
 *
1625
 
 * Note:  Because this function is in most cases used to return an
1626
 
 * integer value, we can make it easier for the caller to just look at
1627
 
 * the return value.  The caller will in all cases consult the value
1628
 
 * and thereby detecting whether a error occured or not (i.e. while checking
1629
 
 * the block size)
 
1756
/* Return information about the given cipher algorithm ALGO.
 
1757
 
 
1758
   WHAT select the kind of information returned:
 
1759
 
 
1760
    GCRYCTL_GET_KEYLEN:
 
1761
        Return the length of the key.  If the algorithm ALGO
 
1762
        supports multiple key lengths, the maximum supported key length
 
1763
        is returned.  The key length is returned as number of octets.
 
1764
        BUFFER and NBYTES must be zero.
 
1765
 
 
1766
    GCRYCTL_GET_BLKLEN:
 
1767
        Return the blocklength of the algorithm ALGO counted in octets.
 
1768
        BUFFER and NBYTES must be zero.
 
1769
 
 
1770
    GCRYCTL_TEST_ALGO:
 
1771
        Returns 0 if the specified algorithm ALGO is available for use.
 
1772
        BUFFER and NBYTES must be zero.
 
1773
  
 
1774
   Note: Because this function is in most cases used to return an
 
1775
   integer value, we can make it easier for the caller to just look at
 
1776
   the return value.  The caller will in all cases consult the value
 
1777
   and thereby detecting whether a error occured or not (i.e. while
 
1778
   checking the block size)
1630
1779
 */
1631
1780
gcry_error_t
1632
1781
gcry_cipher_algo_info (int algo, int what, void *buffer, size_t *nbytes)
1681
1830
}
1682
1831
 
1683
1832
 
 
1833
/* This function returns length of the key for algorithm ALGO.  If the
 
1834
   algorithm supports multiple key lengths, the maximum supported key
 
1835
   length is returned.  On error 0 is returned.  The key length is
 
1836
   returned as number of octets.
 
1837
 
 
1838
   This is a convenience functions which should be preferred over
 
1839
   gcry_cipher_algo_info because it allows for proper type
 
1840
   checking.  */
1684
1841
size_t
1685
1842
gcry_cipher_get_algo_keylen (int algo) 
1686
1843
{
1687
1844
  size_t n;
1688
1845
 
1689
 
  if (gcry_cipher_algo_info( algo, GCRYCTL_GET_KEYLEN, NULL, &n))
 
1846
  if (gcry_cipher_algo_info (algo, GCRYCTL_GET_KEYLEN, NULL, &n))
1690
1847
    n = 0;
1691
1848
  return n;
1692
1849
}
1693
1850
 
 
1851
/* This functions returns the blocklength of the algorithm ALGO
 
1852
   counted in octets.  On error 0 is returned.
1694
1853
 
 
1854
   This is a convenience functions which should be preferred over
 
1855
   gcry_cipher_algo_info because it allows for proper type
 
1856
   checking.  */
1695
1857
size_t
1696
1858
gcry_cipher_get_algo_blklen (int algo) 
1697
1859
{
1702
1864
  return n;
1703
1865
}
1704
1866
 
1705
 
 
 
1867
/* Explicitly initialize this module.  */
1706
1868
gcry_err_code_t
1707
1869
_gcry_cipher_init (void)
1708
1870
{
1730
1892
 
1731
1893
  return err;
1732
1894
}
 
1895
 
 
1896
 
 
1897
/* Run the selftests for cipher algorithm ALGO with optional reporting
 
1898
   function REPORT.  */
 
1899
gpg_error_t
 
1900
_gcry_cipher_selftest (int algo, int extended, selftest_report_func_t report)
 
1901
{
 
1902
  gcry_module_t module = NULL;
 
1903
  cipher_extra_spec_t *extraspec = NULL;
 
1904
  gcry_err_code_t ec = 0;
 
1905
 
 
1906
  REGISTER_DEFAULT_CIPHERS;
 
1907
 
 
1908
  ath_mutex_lock (&ciphers_registered_lock);
 
1909
  module = _gcry_module_lookup_id (ciphers_registered, algo);
 
1910
  if (module && !(module->flags & FLAG_MODULE_DISABLED))
 
1911
    extraspec = module->extraspec;
 
1912
  ath_mutex_unlock (&ciphers_registered_lock);
 
1913
  if (extraspec && extraspec->selftest)
 
1914
    ec = extraspec->selftest (algo, extended, report);
 
1915
  else
 
1916
    {
 
1917
      ec = GPG_ERR_CIPHER_ALGO;
 
1918
      if (report)
 
1919
        report ("cipher", algo, "module", 
 
1920
                module && !(module->flags & FLAG_MODULE_DISABLED)?
 
1921
                "no selftest available" :
 
1922
                module? "algorithm disabled" : "algorithm not found");
 
1923
    }
 
1924
 
 
1925
  if (module)
 
1926
    {
 
1927
      ath_mutex_lock (&ciphers_registered_lock);
 
1928
      _gcry_module_release (module);
 
1929
      ath_mutex_unlock (&ciphers_registered_lock);
 
1930
    }
 
1931
  return gpg_error (ec);
 
1932
}