2015-07-01 19:10:32 +02: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 16:54:59 +01:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2015-07-01 19:10:32 +02: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 19:16:55 +01:00
|
|
|
#include "qemu/osdep.h"
|
2020-08-28 19:05:08 +02: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 09:01:28 +01:00
|
|
|
#include "qapi/error.h"
|
2015-07-01 19:10:32 +02:00
|
|
|
#include "crypto/cipher.h"
|
2017-07-14 20:03:58 +02:00
|
|
|
#include "cipherpriv.h"
|
2015-07-01 19:10:32 +02:00
|
|
|
|
2015-07-01 19:10:33 +02:00
|
|
|
|
2020-08-28 19:05:16 +02:00
|
|
|
static const size_t alg_key_len[QCRYPTO_CIPHER_ALG__MAX] = {
|
2015-07-01 19:10:32 +02:00
|
|
|
[QCRYPTO_CIPHER_ALG_AES_128] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_AES_192] = 24,
|
|
|
|
[QCRYPTO_CIPHER_ALG_AES_256] = 32,
|
|
|
|
[QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
|
2016-12-08 03:33:28 +01:00
|
|
|
[QCRYPTO_CIPHER_ALG_3DES] = 24,
|
2016-02-10 18:07:42 +01:00
|
|
|
[QCRYPTO_CIPHER_ALG_CAST5_128] = 16,
|
2016-02-10 18:07:42 +01:00
|
|
|
[QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_SERPENT_192] = 24,
|
|
|
|
[QCRYPTO_CIPHER_ALG_SERPENT_256] = 32,
|
2016-02-10 18:07:42 +01:00
|
|
|
[QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_TWOFISH_192] = 24,
|
|
|
|
[QCRYPTO_CIPHER_ALG_TWOFISH_256] = 32,
|
2015-07-01 19:10:32 +02:00
|
|
|
};
|
|
|
|
|
2020-08-28 19:05:16 +02:00
|
|
|
static const size_t alg_block_len[QCRYPTO_CIPHER_ALG__MAX] = {
|
2015-10-23 17:13:50 +02:00
|
|
|
[QCRYPTO_CIPHER_ALG_AES_128] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_AES_192] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_AES_256] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_DES_RFB] = 8,
|
2016-12-08 03:33:28 +01:00
|
|
|
[QCRYPTO_CIPHER_ALG_3DES] = 8,
|
2016-02-10 18:07:42 +01:00
|
|
|
[QCRYPTO_CIPHER_ALG_CAST5_128] = 8,
|
2016-02-10 18:07:42 +01:00
|
|
|
[QCRYPTO_CIPHER_ALG_SERPENT_128] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_SERPENT_192] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_SERPENT_256] = 16,
|
2016-02-10 18:07:42 +01:00
|
|
|
[QCRYPTO_CIPHER_ALG_TWOFISH_128] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_TWOFISH_192] = 16,
|
|
|
|
[QCRYPTO_CIPHER_ALG_TWOFISH_256] = 16,
|
2015-10-23 17:13:50 +02:00
|
|
|
};
|
|
|
|
|
2020-08-28 19:05:16 +02:00
|
|
|
static const bool mode_need_iv[QCRYPTO_CIPHER_MODE__MAX] = {
|
2015-10-23 17:13:50 +02:00
|
|
|
[QCRYPTO_CIPHER_MODE_ECB] = false,
|
|
|
|
[QCRYPTO_CIPHER_MODE_CBC] = true,
|
2016-02-11 15:05:21 +01:00
|
|
|
[QCRYPTO_CIPHER_MODE_XTS] = true,
|
2016-09-26 11:23:22 +02:00
|
|
|
[QCRYPTO_CIPHER_MODE_CTR] = true,
|
2015-10-23 17:13:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
size_t qcrypto_cipher_get_block_len(QCryptoCipherAlgorithm alg)
|
|
|
|
{
|
2017-02-20 12:23:07 +01:00
|
|
|
assert(alg < G_N_ELEMENTS(alg_key_len));
|
2015-10-23 17:13:50 +02:00
|
|
|
return alg_block_len[alg];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t qcrypto_cipher_get_key_len(QCryptoCipherAlgorithm alg)
|
|
|
|
{
|
2017-02-20 12:23:07 +01:00
|
|
|
assert(alg < G_N_ELEMENTS(alg_key_len));
|
2015-10-23 17:13:50 +02: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 19:10:32 +02:00
|
|
|
static bool
|
|
|
|
qcrypto_cipher_validate_key_length(QCryptoCipherAlgorithm alg,
|
2016-02-11 15:05:21 +01:00
|
|
|
QCryptoCipherMode mode,
|
2015-07-01 19:10:32 +02:00
|
|
|
size_t nkey,
|
|
|
|
Error **errp)
|
|
|
|
{
|
2015-11-19 18:09:01 +01:00
|
|
|
if ((unsigned)alg >= QCRYPTO_CIPHER_ALG__MAX) {
|
2015-07-01 19:10:32 +02:00
|
|
|
error_setg(errp, "Cipher algorithm %d out of range",
|
|
|
|
alg);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-11 15:05:21 +01:00
|
|
|
if (mode == QCRYPTO_CIPHER_MODE_XTS) {
|
2016-12-08 03:33:28 +01:00
|
|
|
if (alg == QCRYPTO_CIPHER_ALG_DES_RFB
|
|
|
|
|| alg == QCRYPTO_CIPHER_ALG_3DES) {
|
|
|
|
error_setg(errp, "XTS mode not compatible with DES-RFB/3DES");
|
2016-02-11 15:05:21 +01: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 19:10:32 +02:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-16 17:36:53 +02:00
|
|
|
#if defined(CONFIG_GCRYPT) || defined(CONFIG_NETTLE)
|
2015-07-01 19:10:33 +02:00
|
|
|
static uint8_t *
|
|
|
|
qcrypto_cipher_munge_des_rfb_key(const uint8_t *key,
|
|
|
|
size_t nkey)
|
|
|
|
{
|
|
|
|
uint8_t *ret = g_new0(uint8_t, nkey);
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < nkey; i++) {
|
|
|
|
uint8_t r = key[i];
|
|
|
|
r = (r & 0xf0) >> 4 | (r & 0x0f) << 4;
|
|
|
|
r = (r & 0xcc) >> 2 | (r & 0x33) << 2;
|
|
|
|
r = (r & 0xaa) >> 1 | (r & 0x55) << 1;
|
|
|
|
ret[i] = r;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
2015-10-16 17:36:53 +02:00
|
|
|
#endif /* CONFIG_GCRYPT || CONFIG_NETTLE */
|
2015-07-01 19:10:33 +02:00
|
|
|
|
2015-10-16 17:36:53 +02:00
|
|
|
#ifdef CONFIG_GCRYPT
|
2020-08-28 19:05:09 +02:00
|
|
|
#include "cipher-gcrypt.c.inc"
|
2015-10-16 17:36:53 +02:00
|
|
|
#elif defined CONFIG_NETTLE
|
2020-08-28 19:05:09 +02:00
|
|
|
#include "cipher-nettle.c.inc"
|
2015-07-01 19:10:33 +02:00
|
|
|
#else
|
2020-08-28 19:05:09 +02:00
|
|
|
#include "cipher-builtin.c.inc"
|
2015-07-01 19:10:33 +02:00
|
|
|
#endif
|
2017-07-14 20:03:58 +02:00
|
|
|
|
|
|
|
QCryptoCipher *qcrypto_cipher_new(QCryptoCipherAlgorithm alg,
|
|
|
|
QCryptoCipherMode mode,
|
|
|
|
const uint8_t *key, size_t nkey,
|
|
|
|
Error **errp)
|
|
|
|
{
|
2020-08-28 19:05:14 +02:00
|
|
|
QCryptoCipher *cipher = NULL;
|
2017-07-14 20:04:06 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_AF_ALG
|
2020-08-28 19:05:14 +02:00
|
|
|
cipher = qcrypto_afalg_cipher_ctx_new(alg, mode, key, nkey, NULL);
|
2017-07-14 20:04:06 +02:00
|
|
|
#endif
|
2017-07-14 20:03:58 +02:00
|
|
|
|
2020-08-28 19:05:14 +02:00
|
|
|
if (!cipher) {
|
|
|
|
cipher = qcrypto_cipher_ctx_new(alg, mode, key, nkey, errp);
|
|
|
|
if (!cipher) {
|
2017-07-14 20:04:06 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-07-14 20:03:58 +02: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 19:05:13 +02:00
|
|
|
const QCryptoCipherDriver *drv = cipher->driver;
|
2017-07-14 20:03:58 +02: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 19:05:13 +02:00
|
|
|
const QCryptoCipherDriver *drv = cipher->driver;
|
2017-07-14 20:03:58 +02: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 19:05:13 +02:00
|
|
|
const QCryptoCipherDriver *drv = cipher->driver;
|
2017-07-14 20:03:58 +02:00
|
|
|
return drv->cipher_setiv(cipher, iv, niv, errp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void qcrypto_cipher_free(QCryptoCipher *cipher)
|
|
|
|
{
|
|
|
|
if (cipher) {
|
2020-08-28 19:05:14 +02:00
|
|
|
cipher->driver->cipher_free(cipher);
|
2017-07-14 20:03:58 +02:00
|
|
|
}
|
|
|
|
}
|