Skip to content

Commit

Permalink
crypto: extend mode as a parameter in qcrypto_cipher_supports()
Browse files Browse the repository at this point in the history
It can't guarantee all cipher modes are supported
if one cipher algorithm is supported by a backend.
Let's extend qcrypto_cipher_supports() to take both
the algorithm and mode as parameters.

Signed-off-by: Gonglei <[email protected]>
Signed-off-by: Daniel P. Berrange <[email protected]>
  • Loading branch information
gongleiarei authored and berrange committed Oct 19, 2016
1 parent e8ddc2e commit f844836
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 9 deletions.
3 changes: 2 additions & 1 deletion block/qcow.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ static int qcow_open(BlockDriverState *bs, QDict *options, int flags,
ret = -EINVAL;
goto fail;
}
if (!qcrypto_cipher_supports(QCRYPTO_CIPHER_ALG_AES_128)) {
if (!qcrypto_cipher_supports(QCRYPTO_CIPHER_ALG_AES_128,
QCRYPTO_CIPHER_MODE_CBC)) {
error_setg(errp, "AES cipher not available");
ret = -EINVAL;
goto fail;
Expand Down
3 changes: 2 additions & 1 deletion block/qcow2.c
Original file line number Diff line number Diff line change
Expand Up @@ -959,7 +959,8 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags,
ret = -EINVAL;
goto fail;
}
if (!qcrypto_cipher_supports(QCRYPTO_CIPHER_ALG_AES_128)) {
if (!qcrypto_cipher_supports(QCRYPTO_CIPHER_ALG_AES_128,
QCRYPTO_CIPHER_MODE_CBC)) {
error_setg(errp, "AES cipher not available");
ret = -EINVAL;
goto fail;
Expand Down
14 changes: 13 additions & 1 deletion crypto/cipher-builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,14 +400,26 @@ static int qcrypto_cipher_init_des_rfb(QCryptoCipher *cipher,
}


bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg)
bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
QCryptoCipherMode mode)
{
switch (alg) {
case QCRYPTO_CIPHER_ALG_DES_RFB:
case QCRYPTO_CIPHER_ALG_AES_128:
case QCRYPTO_CIPHER_ALG_AES_192:
case QCRYPTO_CIPHER_ALG_AES_256:
break;
default:
return false;
}

switch (mode) {
case QCRYPTO_CIPHER_MODE_ECB:
case QCRYPTO_CIPHER_MODE_CBC:
case QCRYPTO_CIPHER_MODE_XTS:
return true;
case QCRYPTO_CIPHER_MODE_CTR:
return false;
default:
return false;
}
Expand Down
13 changes: 12 additions & 1 deletion crypto/cipher-gcrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
#include <gcrypt.h>


bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg)
bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
QCryptoCipherMode mode)
{
switch (alg) {
case QCRYPTO_CIPHER_ALG_DES_RFB:
Expand All @@ -37,6 +38,16 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg)
case QCRYPTO_CIPHER_ALG_SERPENT_256:
case QCRYPTO_CIPHER_ALG_TWOFISH_128:
case QCRYPTO_CIPHER_ALG_TWOFISH_256:
break;
default:
return false;
}

switch (mode) {
case QCRYPTO_CIPHER_MODE_ECB:
case QCRYPTO_CIPHER_MODE_CBC:
case QCRYPTO_CIPHER_MODE_XTS:
case QCRYPTO_CIPHER_MODE_CTR:
return true;
default:
return false;
Expand Down
13 changes: 12 additions & 1 deletion crypto/cipher-nettle.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ struct QCryptoCipherNettle {
size_t blocksize;
};

bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg)
bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
QCryptoCipherMode mode)
{
switch (alg) {
case QCRYPTO_CIPHER_ALG_DES_RFB:
Expand All @@ -205,6 +206,16 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg)
case QCRYPTO_CIPHER_ALG_TWOFISH_128:
case QCRYPTO_CIPHER_ALG_TWOFISH_192:
case QCRYPTO_CIPHER_ALG_TWOFISH_256:
break;
default:
return false;
}

switch (mode) {
case QCRYPTO_CIPHER_MODE_ECB:
case QCRYPTO_CIPHER_MODE_CBC:
case QCRYPTO_CIPHER_MODE_XTS:
case QCRYPTO_CIPHER_MODE_CTR:
return true;
default:
return false;
Expand Down
6 changes: 4 additions & 2 deletions include/crypto/cipher.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ struct QCryptoCipher {
/**
* qcrypto_cipher_supports:
* @alg: the cipher algorithm
* @mode: the cipher mode
*
* Determine if @alg cipher algorithm is supported by the
* Determine if @alg cipher algorithm in @mode is supported by the
* current configured build
*
* Returns: true if the algorithm is supported, false otherwise
*/
bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg);
bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
QCryptoCipherMode mode);

/**
* qcrypto_cipher_get_block_len:
Expand Down
2 changes: 1 addition & 1 deletion tests/test-crypto-cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ int main(int argc, char **argv)
g_assert(qcrypto_init(NULL) == 0);

for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
if (qcrypto_cipher_supports(test_data[i].alg)) {
if (qcrypto_cipher_supports(test_data[i].alg, test_data[i].mode)) {
g_test_add_data_func(test_data[i].path, &test_data[i], test_cipher);
}
}
Expand Down
2 changes: 1 addition & 1 deletion ui/vnc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3606,7 +3606,7 @@ void vnc_display_open(const char *id, Error **errp)
goto fail;
}
if (!qcrypto_cipher_supports(
QCRYPTO_CIPHER_ALG_DES_RFB)) {
QCRYPTO_CIPHER_ALG_DES_RFB, QCRYPTO_CIPHER_MODE_ECB)) {
error_setg(errp,
"Cipher backend does not support DES RFB algorithm");
goto fail;
Expand Down

0 comments on commit f844836

Please sign in to comment.