38
38
#define NEED_16BYTE_ALIGNED_CONTEXT 1
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;
41
46
/* This is the list of the default ciphers, which are included in
43
48
static struct cipher_table_entry
45
50
gcry_cipher_spec_t *cipher;
51
cipher_extra_spec_t *extraspec;
46
52
unsigned int algorithm;
50
{ &_gcry_cipher_spec_blowfish, GCRY_CIPHER_BLOWFISH },
57
{ &_gcry_cipher_spec_blowfish,
58
&dummy_extra_spec, GCRY_CIPHER_BLOWFISH },
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 },
57
{ &_gcry_cipher_spec_arcfour, GCRY_CIPHER_ARCFOUR },
67
{ &_gcry_cipher_spec_arcfour,
68
&dummy_extra_spec, GCRY_CIPHER_ARCFOUR },
60
{ &_gcry_cipher_spec_cast5, GCRY_CIPHER_CAST5 },
71
{ &_gcry_cipher_spec_cast5,
72
&dummy_extra_spec, GCRY_CIPHER_CAST5 },
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 },
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 },
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 },
77
{ &_gcry_cipher_spec_rfc2268_40, GCRY_CIPHER_RFC2268_40 },
97
{ &_gcry_cipher_spec_rfc2268_40,
98
&dummy_extra_spec, GCRY_CIPHER_RFC2268_40 },
80
{ &_gcry_cipher_spec_seed, GCRY_CIPHER_SEED },
101
{ &_gcry_cipher_spec_seed,
102
&dummy_extra_spec, GCRY_CIPHER_SEED },
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 },
1601
_gcry_cipher_setkey (gcry_cipher_hd_t hd, const void *key, size_t keylen)
1603
return cipher_setkey (hd, (void*)key, keylen);
1608
_gcry_cipher_setiv (gcry_cipher_hd_t hd, const void *iv, size_t ivlen)
1610
cipher_setiv (hd, iv, ivlen);
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
1618
_gcry_cipher_setctr (gcry_cipher_hd_t hd, const void *ctr, size_t ctrlen)
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);
1625
return gpg_error (GPG_ERR_INV_ARG);
1527
1631
gcry_cipher_ctl( gcry_cipher_hd_t h, int cmd, void *buffer, size_t buflen)
1529
1633
gcry_err_code_t rc = GPG_ERR_NO_ERROR;
1533
case GCRYCTL_SET_KEY:
1637
case GCRYCTL_SET_KEY: /* Deprecated; use gcry_cipher_setkey. */
1534
1638
rc = cipher_setkey( h, buffer, buflen );
1536
case GCRYCTL_SET_IV:
1641
case GCRYCTL_SET_IV: /* Deprecated; use gcry_cipher_setiv. */
1537
1642
cipher_setiv( h, buffer, buflen );
1539
1645
case GCRYCTL_RESET:
1540
1646
cipher_reset (h);
1542
1649
case GCRYCTL_CFB_SYNC:
1543
1650
cipher_sync( h );
1545
1653
case GCRYCTL_SET_CBC_CTS:
1547
1655
if (h->flags & GCRY_CIPHER_CBC_MAC)
1606
1753
return gcry_error (err);
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.
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
1756
/* Return information about the given cipher algorithm ALGO.
1758
WHAT select the kind of information returned:
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.
1767
Return the blocklength of the algorithm ALGO counted in octets.
1768
BUFFER and NBYTES must be zero.
1771
Returns 0 if the specified algorithm ALGO is available for use.
1772
BUFFER and NBYTES must be zero.
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)
1632
1781
gcry_cipher_algo_info (int algo, int what, void *buffer, size_t *nbytes)
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.
1838
This is a convenience functions which should be preferred over
1839
gcry_cipher_algo_info because it allows for proper type
1685
1842
gcry_cipher_get_algo_keylen (int algo)
1689
if (gcry_cipher_algo_info( algo, GCRYCTL_GET_KEYLEN, NULL, &n))
1846
if (gcry_cipher_algo_info (algo, GCRYCTL_GET_KEYLEN, NULL, &n))
1851
/* This functions returns the blocklength of the algorithm ALGO
1852
counted in octets. On error 0 is returned.
1854
This is a convenience functions which should be preferred over
1855
gcry_cipher_algo_info because it allows for proper type
1696
1858
gcry_cipher_get_algo_blklen (int algo)
1897
/* Run the selftests for cipher algorithm ALGO with optional reporting
1900
_gcry_cipher_selftest (int algo, int extended, selftest_report_func_t report)
1902
gcry_module_t module = NULL;
1903
cipher_extra_spec_t *extraspec = NULL;
1904
gcry_err_code_t ec = 0;
1906
REGISTER_DEFAULT_CIPHERS;
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);
1917
ec = GPG_ERR_CIPHER_ALGO;
1919
report ("cipher", algo, "module",
1920
module && !(module->flags & FLAG_MODULE_DISABLED)?
1921
"no selftest available" :
1922
module? "algorithm disabled" : "algorithm not found");
1927
ath_mutex_lock (&ciphers_registered_lock);
1928
_gcry_module_release (module);
1929
ath_mutex_unlock (&ciphers_registered_lock);
1931
return gpg_error (ec);