2015-07-01 20:10:32 +03:00
|
|
|
/*
|
|
|
|
* QEMU Crypto cipher algorithms
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2019-02-13 18:54:59 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2015-07-01 20:10:32 +03:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:16:55 +03:00
|
|
|
#include "qemu/osdep.h"
|
2020-08-28 20:05:08 +03:00
|
|
|
#include "qemu/host-utils.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2015-07-01 20:10:32 +03:00
|
|
|
#include "crypto/cipher.h"
|
2017-07-14 21:03:58 +03:00
|
|
|
#include "cipherpriv.h"
|
2015-07-01 20:10:32 +03:00
|
|
|
|
2015-07-01 20:10:33 +03:00
|
|
|
|
2020-08-28 20:05:16 +03:00
|
|
|
static const size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = {
|
2015-07-01 20:10:32 +03:00
|
|
|
[QCRYPTO_CIPHER_ALG_AES_128] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_AES_192] = 24,
|
|
|
|
[QCRYPTO_CIPHER_ALG_AES_256] = 32,
|
crypto: replace 'des-rfb' cipher with 'des'
Currently the crypto layer exposes support for a 'des-rfb'
algorithm which is just normal single-DES, with the bits
in each key byte reversed. This special key munging is
required by the RFB protocol password authentication
mechanism.
Since the crypto layer is generic shared code, it makes
more sense to do the key byte munging in the VNC server
code, and expose normal single-DES support.
Replacing cipher 'des-rfb' by 'des' looks like an incompatible
interface change, but it doesn't matter. While the QMP schema
allows any QCryptoCipherAlgorithm for the 'cipher-alg' field
in QCryptoBlockCreateOptionsLUKS, the code restricts what can
be used at runtime. Thus the only effect is a change in error
message.
Original behaviour:
$ qemu-img create -f luks --object secret,id=sec0,data=123 -o cipher-alg=des-rfb,key-secret=sec0 demo.luks 1G
Formatting 'demo.luks', fmt=luks size=1073741824 key-secret=sec0 cipher-alg=des-rfb
qemu-img: demo.luks: Algorithm 'des-rfb' not supported
New behaviour:
$ qemu-img create -f luks --object secret,id=sec0,data=123 -o cipher-alg=des-rfb,key-secret=sec0 demo.luks 1G
Formatting 'demo.luks', fmt=luks size=1073741824 key-secret=sec0 cipher-alg=des-fish
qemu-img: demo.luks: Invalid parameter 'des-rfb'
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2021-06-29 16:25:32 +03:00
|
|
|
[QCRYPTO_CIPHER_ALG_DES] = 8,
|
2016-12-08 05:33:28 +03:00
|
|
|
[QCRYPTO_CIPHER_ALG_3DES] = 24,
|
2016-02-10 20:07:42 +03:00
|
|
|
[QCRYPTO_CIPHER_ALG_CAST5_128] = 16,
|
2016-02-10 20:07:42 +03:00
|
|
|
[QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_SERPENT_192] = 24,
|
|
|
|
[QCRYPTO_CIPHER_ALG_SERPENT_256] = 32,
|
2016-02-10 20:07:42 +03:00
|
|
|
[QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_TWOFISH_192] = 24,
|
|
|
|
[QCRYPTO_CIPHER_ALG_TWOFISH_256] = 32,
|
2015-07-01 20:10:32 +03:00
|
|
|
};
|
|
|
|
|
2020-08-28 20:05:16 +03:00
|
|
|
static const size_t alg_block_len[QCRYPTO_CIPHER_ALG__MAX] = {
|
2015-10-23 18:13:50 +03:00
|
|
|
[QCRYPTO_CIPHER_ALG_AES_128] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_AES_192] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_AES_256] = 16,
|
crypto: replace 'des-rfb' cipher with 'des'
Currently the crypto layer exposes support for a 'des-rfb'
algorithm which is just normal single-DES, with the bits
in each key byte reversed. This special key munging is
required by the RFB protocol password authentication
mechanism.
Since the crypto layer is generic shared code, it makes
more sense to do the key byte munging in the VNC server
code, and expose normal single-DES support.
Replacing cipher 'des-rfb' by 'des' looks like an incompatible
interface change, but it doesn't matter. While the QMP schema
allows any QCryptoCipherAlgorithm for the 'cipher-alg' field
in QCryptoBlockCreateOptionsLUKS, the code restricts what can
be used at runtime. Thus the only effect is a change in error
message.
Original behaviour:
$ qemu-img create -f luks --object secret,id=sec0,data=123 -o cipher-alg=des-rfb,key-secret=sec0 demo.luks 1G
Formatting 'demo.luks', fmt=luks size=1073741824 key-secret=sec0 cipher-alg=des-rfb
qemu-img: demo.luks: Algorithm 'des-rfb' not supported
New behaviour:
$ qemu-img create -f luks --object secret,id=sec0,data=123 -o cipher-alg=des-rfb,key-secret=sec0 demo.luks 1G
Formatting 'demo.luks', fmt=luks size=1073741824 key-secret=sec0 cipher-alg=des-fish
qemu-img: demo.luks: Invalid parameter 'des-rfb'
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2021-06-29 16:25:32 +03:00
|
|
|
[QCRYPTO_CIPHER_ALG_DES] = 8,
|
2016-12-08 05:33:28 +03:00
|
|
|
[QCRYPTO_CIPHER_ALG_3DES] = 8,
|
2016-02-10 20:07:42 +03:00
|
|
|
[QCRYPTO_CIPHER_ALG_CAST5_128] = 8,
|
2016-02-10 20:07:42 +03:00
|
|
|
[QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_SERPENT_192] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_SERPENT_256] = 16,
|
2016-02-10 20:07:42 +03:00
|
|
|
[QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_TWOFISH_192] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_TWOFISH_256] = 16,
|
2015-10-23 18:13:50 +03:00
|
|
|
};
|
|
|
|
|
2020-08-28 20:05:16 +03:00
|
|
|
static const bool mode_need_iv[QCRYPTO_CIPHER_MODE__MAX] = {
|
2015-10-23 18:13:50 +03:00
|
|
|
[QCRYPTO_CIPHER_MODE_ECB] = false,
|
|
|
|
[QCRYPTO_CIPHER_MODE_CBC] = true,
|
2016-02-11 17:05:21 +03:00
|
|
|
[QCRYPTO_CIPHER_MODE_XTS] = true,
|
2016-09-26 12:23:22 +03:00
|
|
|
[QCRYPTO_CIPHER_MODE_CTR] = true,
|
2015-10-23 18:13:50 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
size_t qcrypto_cipher_get_block_len(QCryptoCipherAlgorithm alg)
|
|
|
|
{
|
2017-02-20 14:23:07 +03:00
|
|
|
assert(alg < G_N_ELEMENTS(alg_key_len));
|
2015-10-23 18:13:50 +03:00
|
|
|
return alg_block_len[alg];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t qcrypto_cipher_get_key_len(QCryptoCipherAlgorithm alg)
|
|
|
|
{
|
2017-02-20 14:23:07 +03:00
|
|
|
assert(alg < G_N_ELEMENTS(alg_key_len));
|
2015-10-23 18:13:50 +03:00
|
|
|
return alg_key_len[alg];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t qcrypto_cipher_get_iv_len(QCryptoCipherAlgorithm alg,
|
|
|
|
QCryptoCipherMode mode)
|
|
|
|
{
|
|
|
|
if (alg >= G_N_ELEMENTS(alg_block_len)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (mode >= G_N_ELEMENTS(mode_need_iv)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mode_need_iv[mode]) {
|
|
|
|
return alg_block_len[alg];
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-01 20:10:32 +03:00
|
|
|
static bool
|
|
|
|
qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg,
|
2016-02-11 17:05:21 +03:00
|
|
|
QCryptoCipherMode mode,
|
2015-07-01 20:10:32 +03:00
|
|
|
size_t nkey,
|
|
|
|
Error **errp)
|
|
|
|
{
|
2015-11-19 20:09:01 +03:00
|
|
|
if ((unsigned)alg >= QCRYPTO_CIPHER_ALG__MAX) {
|
2015-07-01 20:10:32 +03:00
|
|
|
error_setg(errp, "Cipher algorithm %d out of range",
|
|
|
|
alg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-11 17:05:21 +03:00
|
|
|
if (mode == QCRYPTO_CIPHER_MODE_XTS) {
|
crypto: replace 'des-rfb' cipher with 'des'
Currently the crypto layer exposes support for a 'des-rfb'
algorithm which is just normal single-DES, with the bits
in each key byte reversed. This special key munging is
required by the RFB protocol password authentication
mechanism.
Since the crypto layer is generic shared code, it makes
more sense to do the key byte munging in the VNC server
code, and expose normal single-DES support.
Replacing cipher 'des-rfb' by 'des' looks like an incompatible
interface change, but it doesn't matter. While the QMP schema
allows any QCryptoCipherAlgorithm for the 'cipher-alg' field
in QCryptoBlockCreateOptionsLUKS, the code restricts what can
be used at runtime. Thus the only effect is a change in error
message.
Original behaviour:
$ qemu-img create -f luks --object secret,id=sec0,data=123 -o cipher-alg=des-rfb,key-secret=sec0 demo.luks 1G
Formatting 'demo.luks', fmt=luks size=1073741824 key-secret=sec0 cipher-alg=des-rfb
qemu-img: demo.luks: Algorithm 'des-rfb' not supported
New behaviour:
$ qemu-img create -f luks --object secret,id=sec0,data=123 -o cipher-alg=des-rfb,key-secret=sec0 demo.luks 1G
Formatting 'demo.luks', fmt=luks size=1073741824 key-secret=sec0 cipher-alg=des-fish
qemu-img: demo.luks: Invalid parameter 'des-rfb'
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
2021-06-29 16:25:32 +03:00
|
|
|
if (alg == QCRYPTO_CIPHER_ALG_DES ||
|
|
|
|
alg == QCRYPTO_CIPHER_ALG_3DES) {
|
|
|
|
error_setg(errp, "XTS mode not compatible with DES/3DES");
|
2016-02-11 17:05:21 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (nkey % 2) {
|
|
|
|
error_setg(errp, "XTS cipher key length should be a multiple of 2");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (alg_key_len[alg] != (nkey / 2)) {
|
|
|
|
error_setg(errp, "Cipher key length %zu should be %zu",
|
|
|
|
nkey, alg_key_len[alg] * 2);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (alg_key_len[alg] != nkey) {
|
|
|
|
error_setg(errp, "Cipher key length %zu should be %zu",
|
|
|
|
nkey, alg_key_len[alg]);
|
|
|
|
return false;
|
|
|
|
}
|
2015-07-01 20:10:32 +03:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-16 18:36:53 +03:00
|
|
|
#ifdef CONFIG_GCRYPT
|
2020-08-28 20:05:09 +03:00
|
|
|
#include "cipher-gcrypt.c.inc"
|
2015-10-16 18:36:53 +03:00
|
|
|
#elif defined CONFIG_NETTLE
|
2020-08-28 20:05:09 +03:00
|
|
|
#include "cipher-nettle.c.inc"
|
2021-06-29 15:25:55 +03:00
|
|
|
#elif defined CONFIG_GNUTLS_CRYPTO
|
|
|
|
#include "cipher-gnutls.c.inc"
|
2015-07-01 20:10:33 +03:00
|
|
|
#else
|
2020-08-28 20:05:09 +03:00
|
|
|
#include "cipher-builtin.c.inc"
|
2015-07-01 20:10:33 +03:00
|
|
|
#endif
|
2017-07-14 21:03:58 +03:00
|
|
|
|
|
|
|
QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
|
|
|
|
QCryptoCipherMode mode,
|
|
|
|
const uint8_t *key, size_t nkey,
|
|
|
|
Error **errp)
|
|
|
|
{
|
2020-08-28 20:05:14 +03:00
|
|
|
QCryptoCipher *cipher = NULL;
|
2017-07-14 21:04:06 +03:00
|
|
|
|
|
|
|
#ifdef CONFIG_AF_ALG
|
2020-08-28 20:05:14 +03:00
|
|
|
cipher = qcrypto_afalg_cipher_ctx_new(alg, mode, key, nkey, NULL);
|
2017-07-14 21:04:06 +03:00
|
|
|
#endif
|
2017-07-14 21:03:58 +03:00
|
|
|
|
2020-08-28 20:05:14 +03:00
|
|
|
if (!cipher) {
|
|
|
|
cipher = qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp);
|
|
|
|
if (!cipher) {
|
2017-07-14 21:04:06 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-07-14 21:03:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cipher->alg = alg;
|
|
|
|
cipher->mode = mode;
|
|
|
|
|
|
|
|
return cipher;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int qcrypto_cipher_encrypt(QCryptoCipher *cipher,
|
|
|
|
const void *in,
|
|
|
|
void *out,
|
|
|
|
size_t len,
|
|
|
|
Error **errp)
|
|
|
|
{
|
2020-08-28 20:05:13 +03:00
|
|
|
const QCryptoCipherDriver *drv = cipher->driver;
|
2017-07-14 21:03:58 +03:00
|
|
|
return drv->cipher_encrypt(cipher, in, out, len, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int qcrypto_cipher_decrypt(QCryptoCipher *cipher,
|
|
|
|
const void *in,
|
|
|
|
void *out,
|
|
|
|
size_t len,
|
|
|
|
Error **errp)
|
|
|
|
{
|
2020-08-28 20:05:13 +03:00
|
|
|
const QCryptoCipherDriver *drv = cipher->driver;
|
2017-07-14 21:03:58 +03:00
|
|
|
return drv->cipher_decrypt(cipher, in, out, len, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int qcrypto_cipher_setiv(QCryptoCipher *cipher,
|
|
|
|
const uint8_t *iv, size_t niv,
|
|
|
|
Error **errp)
|
|
|
|
{
|
2020-08-28 20:05:13 +03:00
|
|
|
const QCryptoCipherDriver *drv = cipher->driver;
|
2017-07-14 21:03:58 +03:00
|
|
|
return drv->cipher_setiv(cipher, iv, niv, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void qcrypto_cipher_free(QCryptoCipher *cipher)
|
|
|
|
{
|
|
|
|
if (cipher) {
|
2020-08-28 20:05:14 +03:00
|
|
|
cipher->driver->cipher_free(cipher);
|
2017-07-14 21:03:58 +03:00
|
|
|
}
|
|
|
|
}
|