mirror of
https://github.com/izzy2lost/xemu.git
synced 2026-07-06 00:20:22 -07:00
Merge tag 'ak-pull-request' of https://gitlab.com/berrange/qemu into staging
Merge asymmetric cipher crypto support This extends the internal crypto APIs to support the use of asymmetric ciphers. # -----BEGIN PGP SIGNATURE----- # # iQIzBAABCAAdFiEE2vOm/bJrYpEtDo4/vobrtBUQT98FAmKPWdgACgkQvobrtBUQ # T9/dXA//XozeQbIK9y/1wb60LXiqHiHDMi8Ct1oEpNsLaL4lsp09VjtmxggqMfad # MjxQjKdOVMVPISRnrKCJQ6qiGKQB7C/php1ZxOPdG4zgf2Ofl312GHZCLjqLkpB8 # KnhdFB31coI45EQ+agk5ZO8Baml85yY4sALLofGXV3xatJswH1HoMAmDATe5ebko # ox7qd/S9Q4bpZA4v+8fUbvX2zI95hZta8+4d2Irx542gO8KibYKRVmffJhcKx6hy # 4x7iTEaGQQn3DFMbVxsvb4wLwx1v8sSS6C2rHuGZY67ZzDnYhAdaHG9CaWR3uvtS # vs7EcEWqn45SfJ/FaYUyon/btsawJrXP9NISmns4J6TYoN6sJJVxk9T9A/hlqtEE # /iwTfp/Se+o2JDLgC+JHQz8maj4igloGNhF8+u4lXBLEpT7tlvaxhkrcPo9Um7ay # bWpmLoxVN5vEvOnsrfLhK6LGPIzfjP4tYX0xwWy5Lm/DZ1LinJOONPXjArFr3TaQ # rcS6L15ZaiFu9bYUyN1Uf7V7VydiVV8RlkuTqJ614gSX0v+GCMR1J+0WsQ4DtPlT # G6WP0EnnD4Ulg9XpSMte2GXKQ0d8c7hTKr3/RW+BuvvgP5T4P7guBTRhmufRiip6 # BByKpXrQ72yGm6U+nTtEVFdUWVER31U0ufsW64hdM+LGfiG7fUE= # =X589 # -----END PGP SIGNATURE----- # gpg: Signature made Thu 26 May 2022 03:43:36 AM PDT # gpg: using RSA key DAF3A6FDB26B62912D0E8E3FBE86EBB415104FDF # gpg: Good signature from "Daniel P. Berrange <dan@berrange.com>" [full] # gpg: aka "Daniel P. Berrange <berrange@redhat.com>" [full] * tag 'ak-pull-request' of https://gitlab.com/berrange/qemu: tests/crypto: Add test suite for RSA keys test/crypto: Add test suite for crypto akcipher crypto: Implement RSA algorithm by gcrypt crypto: Implement RSA algorithm by hogweed crypto: add ASN.1 DER decoder crypto: Introduce akcipher crypto class qapi: crypto-akcipher: Introduce akcipher types to qapi Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,451 @@
|
||||
/*
|
||||
* QEMU Crypto akcipher algorithms
|
||||
*
|
||||
* Copyright (c) 2022 Bytedance
|
||||
* Author: lei he <helei.sig11@bytedance.com>
|
||||
*
|
||||
* 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
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <nettle/rsa.h>
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "qemu/host-utils.h"
|
||||
#include "crypto/akcipher.h"
|
||||
#include "crypto/random.h"
|
||||
#include "qapi/error.h"
|
||||
#include "sysemu/cryptodev.h"
|
||||
#include "rsakey.h"
|
||||
|
||||
typedef struct QCryptoNettleRSA {
|
||||
QCryptoAkCipher akcipher;
|
||||
struct rsa_public_key pub;
|
||||
struct rsa_private_key priv;
|
||||
QCryptoRSAPaddingAlgorithm padding_alg;
|
||||
QCryptoHashAlgorithm hash_alg;
|
||||
} QCryptoNettleRSA;
|
||||
|
||||
static void qcrypto_nettle_rsa_free(QCryptoAkCipher *akcipher)
|
||||
{
|
||||
QCryptoNettleRSA *rsa = (QCryptoNettleRSA *)akcipher;
|
||||
if (!rsa) {
|
||||
return;
|
||||
}
|
||||
|
||||
rsa_public_key_clear(&rsa->pub);
|
||||
rsa_private_key_clear(&rsa->priv);
|
||||
g_free(rsa);
|
||||
}
|
||||
|
||||
static QCryptoAkCipher *qcrypto_nettle_rsa_new(
|
||||
const QCryptoAkCipherOptionsRSA *opt,
|
||||
QCryptoAkCipherKeyType type,
|
||||
const uint8_t *key, size_t keylen,
|
||||
Error **errp);
|
||||
|
||||
QCryptoAkCipher *qcrypto_akcipher_new(const QCryptoAkCipherOptions *opts,
|
||||
QCryptoAkCipherKeyType type,
|
||||
const uint8_t *key, size_t keylen,
|
||||
Error **errp)
|
||||
{
|
||||
switch (opts->alg) {
|
||||
case QCRYPTO_AKCIPHER_ALG_RSA:
|
||||
return qcrypto_nettle_rsa_new(&opts->u.rsa, type, key, keylen, errp);
|
||||
|
||||
default:
|
||||
error_setg(errp, "Unsupported algorithm: %u", opts->alg);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void qcrypto_nettle_rsa_set_akcipher_size(QCryptoAkCipher *akcipher,
|
||||
int key_size)
|
||||
{
|
||||
akcipher->max_plaintext_len = key_size;
|
||||
akcipher->max_ciphertext_len = key_size;
|
||||
akcipher->max_signature_len = key_size;
|
||||
akcipher->max_dgst_len = key_size;
|
||||
}
|
||||
|
||||
static int qcrypt_nettle_parse_rsa_private_key(QCryptoNettleRSA *rsa,
|
||||
const uint8_t *key,
|
||||
size_t keylen,
|
||||
Error **errp)
|
||||
{
|
||||
g_autoptr(QCryptoAkCipherRSAKey) rsa_key = qcrypto_akcipher_rsakey_parse(
|
||||
QCRYPTO_AKCIPHER_KEY_TYPE_PRIVATE, key, keylen, errp);
|
||||
|
||||
if (!rsa_key) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
nettle_mpz_init_set_str_256_u(rsa->pub.n, rsa_key->n.len, rsa_key->n.data);
|
||||
nettle_mpz_init_set_str_256_u(rsa->pub.e, rsa_key->e.len, rsa_key->e.data);
|
||||
nettle_mpz_init_set_str_256_u(rsa->priv.d, rsa_key->d.len, rsa_key->d.data);
|
||||
nettle_mpz_init_set_str_256_u(rsa->priv.p, rsa_key->p.len, rsa_key->p.data);
|
||||
nettle_mpz_init_set_str_256_u(rsa->priv.q, rsa_key->q.len, rsa_key->q.data);
|
||||
nettle_mpz_init_set_str_256_u(rsa->priv.a, rsa_key->dp.len,
|
||||
rsa_key->dp.data);
|
||||
nettle_mpz_init_set_str_256_u(rsa->priv.b, rsa_key->dq.len,
|
||||
rsa_key->dq.data);
|
||||
nettle_mpz_init_set_str_256_u(rsa->priv.c, rsa_key->u.len, rsa_key->u.data);
|
||||
|
||||
if (!rsa_public_key_prepare(&rsa->pub)) {
|
||||
error_setg(errp, "Failed to check RSA key");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Since in the kernel's unit test, the p, q, a, b, c of some
|
||||
* private keys is 0, only the simplest length check is done here
|
||||
*/
|
||||
if (rsa_key->p.len > 1 &&
|
||||
rsa_key->q.len > 1 &&
|
||||
rsa_key->dp.len > 1 &&
|
||||
rsa_key->dq.len > 1 &&
|
||||
rsa_key->u.len > 1) {
|
||||
if (!rsa_private_key_prepare(&rsa->priv)) {
|
||||
error_setg(errp, "Failed to check RSA key");
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
rsa->priv.size = rsa->pub.size;
|
||||
}
|
||||
qcrypto_nettle_rsa_set_akcipher_size(
|
||||
(QCryptoAkCipher *)rsa, rsa->priv.size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int qcrypt_nettle_parse_rsa_public_key(QCryptoNettleRSA *rsa,
|
||||
const uint8_t *key,
|
||||
size_t keylen,
|
||||
Error **errp)
|
||||
{
|
||||
g_autoptr(QCryptoAkCipherRSAKey) rsa_key = qcrypto_akcipher_rsakey_parse(
|
||||
QCRYPTO_AKCIPHER_KEY_TYPE_PUBLIC, key, keylen, errp);
|
||||
|
||||
if (!rsa_key) {
|
||||
return -1;
|
||||
}
|
||||
nettle_mpz_init_set_str_256_u(rsa->pub.n, rsa_key->n.len, rsa_key->n.data);
|
||||
nettle_mpz_init_set_str_256_u(rsa->pub.e, rsa_key->e.len, rsa_key->e.data);
|
||||
|
||||
if (!rsa_public_key_prepare(&rsa->pub)) {
|
||||
error_setg(errp, "Failed to check RSA key");
|
||||
return -1;
|
||||
}
|
||||
qcrypto_nettle_rsa_set_akcipher_size(
|
||||
(QCryptoAkCipher *)rsa, rsa->pub.size);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void wrap_nettle_random_func(void *ctx, size_t len, uint8_t *out)
|
||||
{
|
||||
qcrypto_random_bytes(out, len, &error_abort);
|
||||
}
|
||||
|
||||
static int qcrypto_nettle_rsa_encrypt(QCryptoAkCipher *akcipher,
|
||||
const void *data, size_t data_len,
|
||||
void *enc, size_t enc_len,
|
||||
Error **errp)
|
||||
{
|
||||
|
||||
QCryptoNettleRSA *rsa = (QCryptoNettleRSA *)akcipher;
|
||||
mpz_t c;
|
||||
int ret = -1;
|
||||
|
||||
if (data_len > rsa->pub.size) {
|
||||
error_setg(errp, "Plaintext length %zu is greater than key size: %zu",
|
||||
data_len, rsa->pub.size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (enc_len < rsa->pub.size) {
|
||||
error_setg(errp, "Ciphertext buffer length %zu is less than "
|
||||
"key size: %zu", enc_len, rsa->pub.size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Nettle do not support RSA encryption without any padding */
|
||||
switch (rsa->padding_alg) {
|
||||
case QCRYPTO_RSA_PADDING_ALG_RAW:
|
||||
error_setg(errp, "RSA with raw padding is not supported");
|
||||
break;
|
||||
|
||||
case QCRYPTO_RSA_PADDING_ALG_PKCS1:
|
||||
mpz_init(c);
|
||||
if (rsa_encrypt(&rsa->pub, NULL, wrap_nettle_random_func,
|
||||
data_len, (uint8_t *)data, c) != 1) {
|
||||
error_setg(errp, "Failed to encrypt");
|
||||
} else {
|
||||
nettle_mpz_get_str_256(enc_len, (uint8_t *)enc, c);
|
||||
ret = nettle_mpz_sizeinbase_256_u(c);
|
||||
}
|
||||
mpz_clear(c);
|
||||
break;
|
||||
|
||||
default:
|
||||
error_setg(errp, "Unknown padding");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int qcrypto_nettle_rsa_decrypt(QCryptoAkCipher *akcipher,
|
||||
const void *enc, size_t enc_len,
|
||||
void *data, size_t data_len,
|
||||
Error **errp)
|
||||
{
|
||||
QCryptoNettleRSA *rsa = (QCryptoNettleRSA *)akcipher;
|
||||
mpz_t c;
|
||||
int ret = -1;
|
||||
|
||||
if (enc_len > rsa->priv.size) {
|
||||
error_setg(errp, "Ciphertext length %zu is greater than key size: %zu",
|
||||
enc_len, rsa->priv.size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
switch (rsa->padding_alg) {
|
||||
case QCRYPTO_RSA_PADDING_ALG_RAW:
|
||||
error_setg(errp, "RSA with raw padding is not supported");
|
||||
break;
|
||||
|
||||
case QCRYPTO_RSA_PADDING_ALG_PKCS1:
|
||||
nettle_mpz_init_set_str_256_u(c, enc_len, enc);
|
||||
if (!rsa_decrypt(&rsa->priv, &data_len, (uint8_t *)data, c)) {
|
||||
error_setg(errp, "Failed to decrypt");
|
||||
} else {
|
||||
ret = data_len;
|
||||
}
|
||||
|
||||
mpz_clear(c);
|
||||
break;
|
||||
|
||||
default:
|
||||
error_setg(errp, "Unknown padding algorithm: %d", rsa->padding_alg);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int qcrypto_nettle_rsa_sign(QCryptoAkCipher *akcipher,
|
||||
const void *data, size_t data_len,
|
||||
void *sig, size_t sig_len, Error **errp)
|
||||
{
|
||||
QCryptoNettleRSA *rsa = (QCryptoNettleRSA *)akcipher;
|
||||
int ret = -1, rv;
|
||||
mpz_t s;
|
||||
|
||||
/**
|
||||
* The RSA algorithm cannot be used for signature/verification
|
||||
* without padding.
|
||||
*/
|
||||
if (rsa->padding_alg == QCRYPTO_RSA_PADDING_ALG_RAW) {
|
||||
error_setg(errp, "Try to make signature without padding");
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (data_len > rsa->priv.size) {
|
||||
error_setg(errp, "Data length %zu is greater than key size: %zu",
|
||||
data_len, rsa->priv.size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (sig_len < rsa->priv.size) {
|
||||
error_setg(errp, "Signature buffer length %zu is less than "
|
||||
"key size: %zu", sig_len, rsa->priv.size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
mpz_init(s);
|
||||
switch (rsa->hash_alg) {
|
||||
case QCRYPTO_HASH_ALG_MD5:
|
||||
rv = rsa_md5_sign_digest(&rsa->priv, data, s);
|
||||
break;
|
||||
|
||||
case QCRYPTO_HASH_ALG_SHA1:
|
||||
rv = rsa_sha1_sign_digest(&rsa->priv, data, s);
|
||||
break;
|
||||
|
||||
case QCRYPTO_HASH_ALG_SHA256:
|
||||
rv = rsa_sha256_sign_digest(&rsa->priv, data, s);
|
||||
break;
|
||||
|
||||
case QCRYPTO_HASH_ALG_SHA512:
|
||||
rv = rsa_sha512_sign_digest(&rsa->priv, data, s);
|
||||
break;
|
||||
|
||||
default:
|
||||
error_setg(errp, "Unknown hash algorithm: %d", rsa->hash_alg);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (rv != 1) {
|
||||
error_setg(errp, "Failed to make signature");
|
||||
goto cleanup;
|
||||
}
|
||||
nettle_mpz_get_str_256(sig_len, (uint8_t *)sig, s);
|
||||
ret = nettle_mpz_sizeinbase_256_u(s);
|
||||
|
||||
cleanup:
|
||||
mpz_clear(s);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int qcrypto_nettle_rsa_verify(QCryptoAkCipher *akcipher,
|
||||
const void *sig, size_t sig_len,
|
||||
const void *data, size_t data_len,
|
||||
Error **errp)
|
||||
{
|
||||
QCryptoNettleRSA *rsa = (QCryptoNettleRSA *)akcipher;
|
||||
|
||||
int ret = -1, rv;
|
||||
mpz_t s;
|
||||
|
||||
/**
|
||||
* The RSA algorithm cannot be used for signature/verification
|
||||
* without padding.
|
||||
*/
|
||||
if (rsa->padding_alg == QCRYPTO_RSA_PADDING_ALG_RAW) {
|
||||
error_setg(errp, "Try to verify signature without padding");
|
||||
return ret;
|
||||
}
|
||||
if (data_len > rsa->pub.size) {
|
||||
error_setg(errp, "Data length %zu is greater than key size: %zu",
|
||||
data_len, rsa->pub.size);
|
||||
return ret;
|
||||
}
|
||||
if (sig_len < rsa->pub.size) {
|
||||
error_setg(errp, "Signature length %zu is greater than key size: %zu",
|
||||
sig_len, rsa->pub.size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
nettle_mpz_init_set_str_256_u(s, sig_len, sig);
|
||||
switch (rsa->hash_alg) {
|
||||
case QCRYPTO_HASH_ALG_MD5:
|
||||
rv = rsa_md5_verify_digest(&rsa->pub, data, s);
|
||||
break;
|
||||
|
||||
case QCRYPTO_HASH_ALG_SHA1:
|
||||
rv = rsa_sha1_verify_digest(&rsa->pub, data, s);
|
||||
break;
|
||||
|
||||
case QCRYPTO_HASH_ALG_SHA256:
|
||||
rv = rsa_sha256_verify_digest(&rsa->pub, data, s);
|
||||
break;
|
||||
|
||||
case QCRYPTO_HASH_ALG_SHA512:
|
||||
rv = rsa_sha512_verify_digest(&rsa->pub, data, s);
|
||||
break;
|
||||
|
||||
default:
|
||||
error_setg(errp, "Unsupported hash algorithm: %d", rsa->hash_alg);
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (rv != 1) {
|
||||
error_setg(errp, "Failed to verify signature");
|
||||
goto cleanup;
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
cleanup:
|
||||
mpz_clear(s);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
QCryptoAkCipherDriver nettle_rsa = {
|
||||
.encrypt = qcrypto_nettle_rsa_encrypt,
|
||||
.decrypt = qcrypto_nettle_rsa_decrypt,
|
||||
.sign = qcrypto_nettle_rsa_sign,
|
||||
.verify = qcrypto_nettle_rsa_verify,
|
||||
.free = qcrypto_nettle_rsa_free,
|
||||
};
|
||||
|
||||
static QCryptoAkCipher *qcrypto_nettle_rsa_new(
|
||||
const QCryptoAkCipherOptionsRSA *opt,
|
||||
QCryptoAkCipherKeyType type,
|
||||
const uint8_t *key, size_t keylen,
|
||||
Error **errp)
|
||||
{
|
||||
QCryptoNettleRSA *rsa = g_new0(QCryptoNettleRSA, 1);
|
||||
|
||||
rsa->padding_alg = opt->padding_alg;
|
||||
rsa->hash_alg = opt->hash_alg;
|
||||
rsa->akcipher.driver = &nettle_rsa;
|
||||
rsa_public_key_init(&rsa->pub);
|
||||
rsa_private_key_init(&rsa->priv);
|
||||
|
||||
switch (type) {
|
||||
case QCRYPTO_AKCIPHER_KEY_TYPE_PRIVATE:
|
||||
if (qcrypt_nettle_parse_rsa_private_key(rsa, key, keylen, errp) != 0) {
|
||||
goto error;
|
||||
}
|
||||
break;
|
||||
|
||||
case QCRYPTO_AKCIPHER_KEY_TYPE_PUBLIC:
|
||||
if (qcrypt_nettle_parse_rsa_public_key(rsa, key, keylen, errp) != 0) {
|
||||
goto error;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
error_setg(errp, "Unknown akcipher key type %d", type);
|
||||
goto error;
|
||||
}
|
||||
|
||||
return (QCryptoAkCipher *)rsa;
|
||||
|
||||
error:
|
||||
qcrypto_nettle_rsa_free((QCryptoAkCipher *)rsa);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
bool qcrypto_akcipher_supports(QCryptoAkCipherOptions *opts)
|
||||
{
|
||||
switch (opts->alg) {
|
||||
case QCRYPTO_AKCIPHER_ALG_RSA:
|
||||
switch (opts->u.rsa.padding_alg) {
|
||||
case QCRYPTO_RSA_PADDING_ALG_PKCS1:
|
||||
switch (opts->u.rsa.hash_alg) {
|
||||
case QCRYPTO_HASH_ALG_MD5:
|
||||
case QCRYPTO_HASH_ALG_SHA1:
|
||||
case QCRYPTO_HASH_ALG_SHA256:
|
||||
case QCRYPTO_HASH_ALG_SHA512:
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
case QCRYPTO_RSA_PADDING_ALG_RAW:
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* QEMU Crypto akcipher algorithms
|
||||
*
|
||||
* Copyright (c) 2022 Bytedance
|
||||
* Author: zhenwei pi <pizhenwei@bytedance.com>
|
||||
*
|
||||
* 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
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "crypto/akcipher.h"
|
||||
#include "akcipherpriv.h"
|
||||
|
||||
#if defined(CONFIG_GCRYPT)
|
||||
#include "akcipher-gcrypt.c.inc"
|
||||
#elif defined(CONFIG_NETTLE) && defined(CONFIG_HOGWEED)
|
||||
#include "akcipher-nettle.c.inc"
|
||||
#else
|
||||
QCryptoAkCipher *qcrypto_akcipher_new(const QCryptoAkCipherOptions *opts,
|
||||
QCryptoAkCipherKeyType type,
|
||||
const uint8_t *key, size_t keylen,
|
||||
Error **errp)
|
||||
{
|
||||
QCryptoAkCipher *akcipher = NULL;
|
||||
|
||||
return akcipher;
|
||||
}
|
||||
|
||||
bool qcrypto_akcipher_supports(QCryptoAkCipherOptions *opts)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
|
||||
int qcrypto_akcipher_encrypt(QCryptoAkCipher *akcipher,
|
||||
const void *in, size_t in_len,
|
||||
void *out, size_t out_len, Error **errp)
|
||||
{
|
||||
const QCryptoAkCipherDriver *drv = akcipher->driver;
|
||||
|
||||
return drv->encrypt(akcipher, in, in_len, out, out_len, errp);
|
||||
}
|
||||
|
||||
int qcrypto_akcipher_decrypt(QCryptoAkCipher *akcipher,
|
||||
const void *in, size_t in_len,
|
||||
void *out, size_t out_len, Error **errp)
|
||||
{
|
||||
const QCryptoAkCipherDriver *drv = akcipher->driver;
|
||||
|
||||
return drv->decrypt(akcipher, in, in_len, out, out_len, errp);
|
||||
}
|
||||
|
||||
int qcrypto_akcipher_sign(QCryptoAkCipher *akcipher,
|
||||
const void *in, size_t in_len,
|
||||
void *out, size_t out_len, Error **errp)
|
||||
{
|
||||
const QCryptoAkCipherDriver *drv = akcipher->driver;
|
||||
|
||||
return drv->sign(akcipher, in, in_len, out, out_len, errp);
|
||||
}
|
||||
|
||||
int qcrypto_akcipher_verify(QCryptoAkCipher *akcipher,
|
||||
const void *in, size_t in_len,
|
||||
const void *in2, size_t in2_len, Error **errp)
|
||||
{
|
||||
const QCryptoAkCipherDriver *drv = akcipher->driver;
|
||||
|
||||
return drv->verify(akcipher, in, in_len, in2, in2_len, errp);
|
||||
}
|
||||
|
||||
int qcrypto_akcipher_max_plaintext_len(QCryptoAkCipher *akcipher)
|
||||
{
|
||||
return akcipher->max_plaintext_len;
|
||||
}
|
||||
|
||||
int qcrypto_akcipher_max_ciphertext_len(QCryptoAkCipher *akcipher)
|
||||
{
|
||||
return akcipher->max_ciphertext_len;
|
||||
}
|
||||
|
||||
int qcrypto_akcipher_max_signature_len(QCryptoAkCipher *akcipher)
|
||||
{
|
||||
return akcipher->max_signature_len;
|
||||
}
|
||||
|
||||
int qcrypto_akcipher_max_dgst_len(QCryptoAkCipher *akcipher)
|
||||
{
|
||||
return akcipher->max_dgst_len;
|
||||
}
|
||||
|
||||
void qcrypto_akcipher_free(QCryptoAkCipher *akcipher)
|
||||
{
|
||||
const QCryptoAkCipherDriver *drv = akcipher->driver;
|
||||
|
||||
drv->free(akcipher);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* QEMU Crypto asymmetric algorithms
|
||||
*
|
||||
* Copyright (c) 2022 Bytedance
|
||||
* Author: zhenwei pi <pizhenwei@bytedance.com>
|
||||
*
|
||||
* 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
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef QCRYPTO_AKCIPHERPRIV_H
|
||||
#define QCRYPTO_AKCIPHERPRIV_H
|
||||
|
||||
#include "qapi/qapi-types-crypto.h"
|
||||
|
||||
typedef struct QCryptoAkCipherDriver QCryptoAkCipherDriver;
|
||||
|
||||
struct QCryptoAkCipher {
|
||||
QCryptoAkCipherAlgorithm alg;
|
||||
QCryptoAkCipherKeyType type;
|
||||
int max_plaintext_len;
|
||||
int max_ciphertext_len;
|
||||
int max_signature_len;
|
||||
int max_dgst_len;
|
||||
QCryptoAkCipherDriver *driver;
|
||||
};
|
||||
|
||||
struct QCryptoAkCipherDriver {
|
||||
int (*encrypt)(QCryptoAkCipher *akcipher,
|
||||
const void *in, size_t in_len,
|
||||
void *out, size_t out_len, Error **errp);
|
||||
int (*decrypt)(QCryptoAkCipher *akcipher,
|
||||
const void *out, size_t out_len,
|
||||
void *in, size_t in_len, Error **errp);
|
||||
int (*sign)(QCryptoAkCipher *akcipher,
|
||||
const void *in, size_t in_len,
|
||||
void *out, size_t out_len, Error **errp);
|
||||
int (*verify)(QCryptoAkCipher *akcipher,
|
||||
const void *in, size_t in_len,
|
||||
const void *in2, size_t in2_len, Error **errp);
|
||||
void (*free)(QCryptoAkCipher *akcipher);
|
||||
};
|
||||
|
||||
#endif /* QCRYPTO_AKCIPHER_H */
|
||||
+189
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
* QEMU Crypto ASN.1 DER decoder
|
||||
*
|
||||
* Copyright (c) 2022 Bytedance
|
||||
* Author: lei he <helei.sig11@bytedance.com>
|
||||
*
|
||||
* 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
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "crypto/der.h"
|
||||
|
||||
enum QCryptoDERTypeTag {
|
||||
QCRYPTO_DER_TYPE_TAG_BOOL = 0x1,
|
||||
QCRYPTO_DER_TYPE_TAG_INT = 0x2,
|
||||
QCRYPTO_DER_TYPE_TAG_BIT_STR = 0x3,
|
||||
QCRYPTO_DER_TYPE_TAG_OCT_STR = 0x4,
|
||||
QCRYPTO_DER_TYPE_TAG_OCT_NULL = 0x5,
|
||||
QCRYPTO_DER_TYPE_TAG_OCT_OID = 0x6,
|
||||
QCRYPTO_DER_TYPE_TAG_SEQ = 0x10,
|
||||
QCRYPTO_DER_TYPE_TAG_SET = 0x11,
|
||||
};
|
||||
|
||||
#define QCRYPTO_DER_CONSTRUCTED_MASK 0x20
|
||||
#define QCRYPTO_DER_SHORT_LEN_MASK 0x80
|
||||
|
||||
static uint8_t qcrypto_der_peek_byte(const uint8_t **data, size_t *dlen)
|
||||
{
|
||||
return **data;
|
||||
}
|
||||
|
||||
static void qcrypto_der_cut_nbytes(const uint8_t **data,
|
||||
size_t *dlen,
|
||||
size_t nbytes)
|
||||
{
|
||||
*data += nbytes;
|
||||
*dlen -= nbytes;
|
||||
}
|
||||
|
||||
static uint8_t qcrypto_der_cut_byte(const uint8_t **data, size_t *dlen)
|
||||
{
|
||||
uint8_t val = qcrypto_der_peek_byte(data, dlen);
|
||||
|
||||
qcrypto_der_cut_nbytes(data, dlen, 1);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
static int qcrypto_der_invoke_callback(QCryptoDERDecodeCb cb, void *ctx,
|
||||
const uint8_t *value, size_t vlen,
|
||||
Error **errp)
|
||||
{
|
||||
if (!cb) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return cb(ctx, value, vlen, errp);
|
||||
}
|
||||
|
||||
static int qcrypto_der_extract_definite_data(const uint8_t **data, size_t *dlen,
|
||||
QCryptoDERDecodeCb cb, void *ctx,
|
||||
Error **errp)
|
||||
{
|
||||
const uint8_t *value;
|
||||
size_t vlen = 0;
|
||||
uint8_t byte_count = qcrypto_der_cut_byte(data, dlen);
|
||||
|
||||
/* short format of definite-length */
|
||||
if (!(byte_count & QCRYPTO_DER_SHORT_LEN_MASK)) {
|
||||
if (byte_count > *dlen) {
|
||||
error_setg(errp, "Invalid content length: %u", byte_count);
|
||||
return -1;
|
||||
}
|
||||
|
||||
value = *data;
|
||||
vlen = byte_count;
|
||||
qcrypto_der_cut_nbytes(data, dlen, vlen);
|
||||
|
||||
if (qcrypto_der_invoke_callback(cb, ctx, value, vlen, errp) != 0) {
|
||||
return -1;
|
||||
}
|
||||
return vlen;
|
||||
}
|
||||
|
||||
/* Ignore highest bit */
|
||||
byte_count &= ~QCRYPTO_DER_SHORT_LEN_MASK;
|
||||
|
||||
/*
|
||||
* size_t is enough to store the value of length, although the DER
|
||||
* encoding standard supports larger length.
|
||||
*/
|
||||
if (byte_count > sizeof(size_t)) {
|
||||
error_setg(errp, "Invalid byte count of content length: %u",
|
||||
byte_count);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (byte_count > *dlen) {
|
||||
error_setg(errp, "Invalid content length: %u", byte_count);
|
||||
return -1;
|
||||
}
|
||||
while (byte_count--) {
|
||||
vlen <<= 8;
|
||||
vlen += qcrypto_der_cut_byte(data, dlen);
|
||||
}
|
||||
|
||||
if (vlen > *dlen) {
|
||||
error_setg(errp, "Invalid content length: %zu", vlen);
|
||||
return -1;
|
||||
}
|
||||
|
||||
value = *data;
|
||||
qcrypto_der_cut_nbytes(data, dlen, vlen);
|
||||
|
||||
if (qcrypto_der_invoke_callback(cb, ctx, value, vlen, errp) != 0) {
|
||||
return -1;
|
||||
}
|
||||
return vlen;
|
||||
}
|
||||
|
||||
static int qcrypto_der_extract_data(const uint8_t **data, size_t *dlen,
|
||||
QCryptoDERDecodeCb cb, void *ctx,
|
||||
Error **errp)
|
||||
{
|
||||
uint8_t val;
|
||||
if (*dlen < 1) {
|
||||
error_setg(errp, "Need more data");
|
||||
return -1;
|
||||
}
|
||||
val = qcrypto_der_peek_byte(data, dlen);
|
||||
|
||||
/* must use definite length format */
|
||||
if (val == QCRYPTO_DER_SHORT_LEN_MASK) {
|
||||
error_setg(errp, "Only definite length format is allowed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return qcrypto_der_extract_definite_data(data, dlen, cb, ctx, errp);
|
||||
}
|
||||
|
||||
int qcrypto_der_decode_int(const uint8_t **data, size_t *dlen,
|
||||
QCryptoDERDecodeCb cb, void *ctx, Error **errp)
|
||||
{
|
||||
uint8_t tag;
|
||||
if (*dlen < 1) {
|
||||
error_setg(errp, "Need more data");
|
||||
return -1;
|
||||
}
|
||||
tag = qcrypto_der_cut_byte(data, dlen);
|
||||
|
||||
/* INTEGER must encoded in primitive-form */
|
||||
if (tag != QCRYPTO_DER_TYPE_TAG_INT) {
|
||||
error_setg(errp, "Invalid integer type tag: %u", tag);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return qcrypto_der_extract_data(data, dlen, cb, ctx, errp);
|
||||
}
|
||||
|
||||
int qcrypto_der_decode_seq(const uint8_t **data, size_t *dlen,
|
||||
QCryptoDERDecodeCb cb, void *ctx, Error **errp)
|
||||
{
|
||||
uint8_t tag;
|
||||
if (*dlen < 1) {
|
||||
error_setg(errp, "Need more data");
|
||||
return -1;
|
||||
}
|
||||
tag = qcrypto_der_cut_byte(data, dlen);
|
||||
|
||||
/* SEQUENCE must use constructed form */
|
||||
if (tag != (QCRYPTO_DER_TYPE_TAG_SEQ | QCRYPTO_DER_CONSTRUCTED_MASK)) {
|
||||
error_setg(errp, "Invalid type sequence tag: %u", tag);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return qcrypto_der_extract_data(data, dlen, cb, ctx, errp);
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (c) 2022 Bytedance
|
||||
* Author: lei he <helei.sig11@bytedance.com>
|
||||
*
|
||||
* 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
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef QCRYPTO_ASN1_DECODER_H
|
||||
#define QCRYPTO_ASN1_DECODER_H
|
||||
|
||||
#include "qapi/error.h"
|
||||
|
||||
/* Simple decoder used to parse DER encoded rsa keys. */
|
||||
|
||||
/**
|
||||
* @opaque: user context.
|
||||
* @value: the starting address of |value| part of 'Tag-Length-Value' pattern.
|
||||
* @vlen: length of the |value|.
|
||||
* Returns: 0 for success, any other value is considered an error.
|
||||
*/
|
||||
typedef int (*QCryptoDERDecodeCb) (void *opaque, const uint8_t *value,
|
||||
size_t vlen, Error **errp);
|
||||
|
||||
/**
|
||||
* qcrypto_der_decode_int:
|
||||
* @data: pointer to address of input data
|
||||
* @dlen: pointer to length of input data
|
||||
* @cb: callback invoked when decode succeed, if cb equals NULL, no
|
||||
* callback will be invoked
|
||||
* @opaque: parameter passed to cb
|
||||
*
|
||||
* Decode integer from DER-encoded data.
|
||||
*
|
||||
* Returns: On success, *data points to rest data, and *dlen
|
||||
* will be set to the rest length of data, if cb is not NULL, must
|
||||
* return 0 to make decode success, at last, the length of the data
|
||||
* part of the decoded INTEGER will be returned. Otherwise, -1 is
|
||||
* returned.
|
||||
*/
|
||||
int qcrypto_der_decode_int(const uint8_t **data,
|
||||
size_t *dlen,
|
||||
QCryptoDERDecodeCb cb,
|
||||
void *opaque,
|
||||
Error **errp);
|
||||
|
||||
/**
|
||||
* qcrypto_der_decode_seq:
|
||||
*
|
||||
* Decode sequence from DER-encoded data, similar with der_decode_int.
|
||||
*
|
||||
* @data: pointer to address of input data
|
||||
* @dlen: pointer to length of input data
|
||||
* @cb: callback invoked when decode succeed, if cb equals NULL, no
|
||||
* callback will be invoked
|
||||
* @opaque: parameter passed to cb
|
||||
*
|
||||
* Returns: On success, *data points to rest data, and *dlen
|
||||
* will be set to the rest length of data, if cb is not NULL, must
|
||||
* return 0 to make decode success, at last, the length of the data
|
||||
* part of the decoded SEQUENCE will be returned. Otherwise, -1 is
|
||||
* returned.
|
||||
*/
|
||||
int qcrypto_der_decode_seq(const uint8_t **data,
|
||||
size_t *dlen,
|
||||
QCryptoDERDecodeCb cb,
|
||||
void *opaque,
|
||||
Error **errp);
|
||||
|
||||
#endif /* QCRYPTO_ASN1_DECODER_H */
|
||||
@@ -1,10 +1,12 @@
|
||||
crypto_ss.add(genh)
|
||||
crypto_ss.add(files(
|
||||
'afsplit.c',
|
||||
'akcipher.c',
|
||||
'block-luks.c',
|
||||
'block-qcow.c',
|
||||
'block.c',
|
||||
'cipher.c',
|
||||
'der.c',
|
||||
'hash.c',
|
||||
'hmac.c',
|
||||
'ivgen-essiv.c',
|
||||
@@ -19,10 +21,14 @@ crypto_ss.add(files(
|
||||
'tlscredspsk.c',
|
||||
'tlscredsx509.c',
|
||||
'tlssession.c',
|
||||
'rsakey.c',
|
||||
))
|
||||
|
||||
if nettle.found()
|
||||
crypto_ss.add(nettle, files('hash-nettle.c', 'hmac-nettle.c', 'pbkdf-nettle.c'))
|
||||
if hogweed.found()
|
||||
crypto_ss.add(gmp, hogweed)
|
||||
endif
|
||||
if xts == 'private'
|
||||
crypto_ss.add(files('xts.c'))
|
||||
endif
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
/*
|
||||
* QEMU Crypto akcipher algorithms
|
||||
*
|
||||
* Copyright (c) 2022 Bytedance
|
||||
* Author: lei he <helei.sig11@bytedance.com>
|
||||
*
|
||||
* 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
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "der.h"
|
||||
#include "rsakey.h"
|
||||
|
||||
static int extract_mpi(void *ctx, const uint8_t *value,
|
||||
size_t vlen, Error **errp)
|
||||
{
|
||||
QCryptoAkCipherMPI *mpi = (QCryptoAkCipherMPI *)ctx;
|
||||
if (vlen == 0) {
|
||||
error_setg(errp, "Empty mpi field");
|
||||
return -1;
|
||||
}
|
||||
mpi->data = g_memdup2(value, vlen);
|
||||
mpi->len = vlen;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int extract_version(void *ctx, const uint8_t *value,
|
||||
size_t vlen, Error **errp)
|
||||
{
|
||||
uint8_t *version = (uint8_t *)ctx;
|
||||
if (vlen != 1 || *value > 1) {
|
||||
error_setg(errp, "Invalid rsakey version");
|
||||
return -1;
|
||||
}
|
||||
*version = *value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int extract_seq_content(void *ctx, const uint8_t *value,
|
||||
size_t vlen, Error **errp)
|
||||
{
|
||||
const uint8_t **content = (const uint8_t **)ctx;
|
||||
if (vlen == 0) {
|
||||
error_setg(errp, "Empty sequence");
|
||||
return -1;
|
||||
}
|
||||
*content = value;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* RsaPubKey ::= SEQUENCE {
|
||||
* n INTEGER
|
||||
* e INTEGER
|
||||
* }
|
||||
*/
|
||||
static QCryptoAkCipherRSAKey *qcrypto_builtin_rsa_public_key_parse(
|
||||
const uint8_t *key, size_t keylen, Error **errp)
|
||||
{
|
||||
QCryptoAkCipherRSAKey *rsa = g_new0(QCryptoAkCipherRSAKey, 1);
|
||||
const uint8_t *seq;
|
||||
size_t seq_length;
|
||||
int decode_ret;
|
||||
|
||||
decode_ret = qcrypto_der_decode_seq(&key, &keylen,
|
||||
extract_seq_content, &seq, errp);
|
||||
if (decode_ret < 0 || keylen != 0) {
|
||||
goto error;
|
||||
}
|
||||
seq_length = decode_ret;
|
||||
|
||||
if (qcrypto_der_decode_int(&seq, &seq_length, extract_mpi,
|
||||
&rsa->n, errp) < 0 ||
|
||||
qcrypto_der_decode_int(&seq, &seq_length, extract_mpi,
|
||||
&rsa->e, errp) < 0) {
|
||||
goto error;
|
||||
}
|
||||
if (seq_length != 0) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
return rsa;
|
||||
|
||||
error:
|
||||
if (errp && !*errp) {
|
||||
error_setg(errp, "Invalid RSA public key");
|
||||
}
|
||||
qcrypto_akcipher_rsakey_free(rsa);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* RsaPrivKey ::= SEQUENCE {
|
||||
* version INTEGER
|
||||
* n INTEGER
|
||||
* e INTEGER
|
||||
* d INTEGER
|
||||
* p INTEGER
|
||||
* q INTEGER
|
||||
* dp INTEGER
|
||||
* dq INTEGER
|
||||
* u INTEGER
|
||||
* otherPrimeInfos OtherPrimeInfos OPTIONAL
|
||||
* }
|
||||
*/
|
||||
static QCryptoAkCipherRSAKey *qcrypto_builtin_rsa_private_key_parse(
|
||||
const uint8_t *key, size_t keylen, Error **errp)
|
||||
{
|
||||
QCryptoAkCipherRSAKey *rsa = g_new0(QCryptoAkCipherRSAKey, 1);
|
||||
uint8_t version;
|
||||
const uint8_t *seq;
|
||||
int decode_ret;
|
||||
size_t seq_length;
|
||||
|
||||
decode_ret = qcrypto_der_decode_seq(&key, &keylen, extract_seq_content,
|
||||
&seq, errp);
|
||||
if (decode_ret < 0 || keylen != 0) {
|
||||
goto error;
|
||||
}
|
||||
seq_length = decode_ret;
|
||||
|
||||
decode_ret = qcrypto_der_decode_int(&seq, &seq_length, extract_version,
|
||||
&version, errp);
|
||||
|
||||
if (qcrypto_der_decode_int(&seq, &seq_length, extract_mpi,
|
||||
&rsa->n, errp) < 0 ||
|
||||
qcrypto_der_decode_int(&seq, &seq_length, extract_mpi,
|
||||
&rsa->e, errp) < 0 ||
|
||||
qcrypto_der_decode_int(&seq, &seq_length, extract_mpi,
|
||||
&rsa->d, errp) < 0 ||
|
||||
qcrypto_der_decode_int(&seq, &seq_length, extract_mpi, &rsa->p,
|
||||
errp) < 0 ||
|
||||
qcrypto_der_decode_int(&seq, &seq_length, extract_mpi, &rsa->q,
|
||||
errp) < 0 ||
|
||||
qcrypto_der_decode_int(&seq, &seq_length, extract_mpi, &rsa->dp,
|
||||
errp) < 0 ||
|
||||
qcrypto_der_decode_int(&seq, &seq_length, extract_mpi, &rsa->dq,
|
||||
errp) < 0 ||
|
||||
qcrypto_der_decode_int(&seq, &seq_length, extract_mpi, &rsa->u,
|
||||
errp) < 0) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
/**
|
||||
* According to the standard, otherPrimeInfos must be present for version 1.
|
||||
* There is no strict verification here, this is to be compatible with
|
||||
* the unit test of the kernel. TODO: remove this until linux kernel's
|
||||
* unit-test is fixed.
|
||||
*/
|
||||
if (version == 1 && seq_length != 0) {
|
||||
if (qcrypto_der_decode_seq(&seq, &seq_length, NULL, NULL, errp) < 0) {
|
||||
goto error;
|
||||
}
|
||||
if (seq_length != 0) {
|
||||
goto error;
|
||||
}
|
||||
return rsa;
|
||||
}
|
||||
if (seq_length != 0) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
return rsa;
|
||||
|
||||
error:
|
||||
if (errp && !*errp) {
|
||||
error_setg(errp, "Invalid RSA private key");
|
||||
}
|
||||
qcrypto_akcipher_rsakey_free(rsa);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
QCryptoAkCipherRSAKey *qcrypto_akcipher_rsakey_parse(
|
||||
QCryptoAkCipherKeyType type, const uint8_t *key,
|
||||
size_t keylen, Error **errp)
|
||||
{
|
||||
switch (type) {
|
||||
case QCRYPTO_AKCIPHER_KEY_TYPE_PRIVATE:
|
||||
return qcrypto_builtin_rsa_private_key_parse(key, keylen, errp);
|
||||
|
||||
case QCRYPTO_AKCIPHER_KEY_TYPE_PUBLIC:
|
||||
return qcrypto_builtin_rsa_public_key_parse(key, keylen, errp);
|
||||
|
||||
default:
|
||||
error_setg(errp, "Unknown key type: %d", type);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* QEMU Crypto akcipher algorithms
|
||||
*
|
||||
* Copyright (c) 2022 Bytedance
|
||||
* Author: lei he <helei.sig11@bytedance.com>
|
||||
*
|
||||
* 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
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <nettle/asn1.h>
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "qapi/error.h"
|
||||
#include "rsakey.h"
|
||||
|
||||
static bool DumpMPI(struct asn1_der_iterator *i, QCryptoAkCipherMPI *mpi)
|
||||
{
|
||||
mpi->data = g_memdup2(i->data, i->length);
|
||||
mpi->len = i->length;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool GetMPI(struct asn1_der_iterator *i, QCryptoAkCipherMPI *mpi)
|
||||
{
|
||||
if (asn1_der_iterator_next(i) != ASN1_ITERATOR_PRIMITIVE ||
|
||||
i->type != ASN1_INTEGER) {
|
||||
return false;
|
||||
}
|
||||
return DumpMPI(i, mpi);
|
||||
}
|
||||
|
||||
/**
|
||||
* RsaPrivKey ::= SEQUENCE {
|
||||
* version INTEGER
|
||||
* n INTEGER
|
||||
* e INTEGER
|
||||
* d INTEGER
|
||||
* p INTEGER
|
||||
* q INTEGER
|
||||
* dp INTEGER
|
||||
* dq INTEGER
|
||||
* u INTEGER
|
||||
* otherPrimeInfos OtherPrimeInfos OPTIONAL
|
||||
* }
|
||||
*/
|
||||
static QCryptoAkCipherRSAKey *qcrypto_nettle_rsa_private_key_parse(
|
||||
const uint8_t *key, size_t keylen, Error **errp)
|
||||
{
|
||||
QCryptoAkCipherRSAKey *rsa = g_new0(QCryptoAkCipherRSAKey, 1);
|
||||
struct asn1_der_iterator i;
|
||||
uint32_t version;
|
||||
int tag;
|
||||
|
||||
/* Parse entire struct */
|
||||
if (asn1_der_iterator_first(&i, keylen, key) != ASN1_ITERATOR_CONSTRUCTED ||
|
||||
i.type != ASN1_SEQUENCE ||
|
||||
asn1_der_decode_constructed_last(&i) != ASN1_ITERATOR_PRIMITIVE ||
|
||||
i.type != ASN1_INTEGER ||
|
||||
!asn1_der_get_uint32(&i, &version) ||
|
||||
version > 1 ||
|
||||
!GetMPI(&i, &rsa->n) ||
|
||||
!GetMPI(&i, &rsa->e) ||
|
||||
!GetMPI(&i, &rsa->d) ||
|
||||
!GetMPI(&i, &rsa->p) ||
|
||||
!GetMPI(&i, &rsa->q) ||
|
||||
!GetMPI(&i, &rsa->dp) ||
|
||||
!GetMPI(&i, &rsa->dq) ||
|
||||
!GetMPI(&i, &rsa->u)) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
if (version == 1) {
|
||||
tag = asn1_der_iterator_next(&i);
|
||||
/**
|
||||
* According to the standard otherPrimeInfos must be present for
|
||||
* version 1. There is no strict verification here, this is to be
|
||||
* compatible with the unit test of the kernel. TODO: remove this
|
||||
* until linux-kernel's unit-test is fixed;
|
||||
*/
|
||||
if (tag == ASN1_ITERATOR_END) {
|
||||
return rsa;
|
||||
}
|
||||
if (tag != ASN1_ITERATOR_CONSTRUCTED ||
|
||||
i.type != ASN1_SEQUENCE) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
if (asn1_der_iterator_next(&i) != ASN1_ITERATOR_END) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
return rsa;
|
||||
|
||||
error:
|
||||
error_setg(errp, "Failed to parse RSA private key");
|
||||
qcrypto_akcipher_rsakey_free(rsa);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* RsaPubKey ::= SEQUENCE {
|
||||
* n INTEGER
|
||||
* e INTEGER
|
||||
* }
|
||||
*/
|
||||
static QCryptoAkCipherRSAKey *qcrypto_nettle_rsa_public_key_parse(
|
||||
const uint8_t *key, size_t keylen, Error **errp)
|
||||
{
|
||||
|
||||
QCryptoAkCipherRSAKey *rsa = g_new0(QCryptoAkCipherRSAKey, 1);
|
||||
struct asn1_der_iterator i;
|
||||
|
||||
if (asn1_der_iterator_first(&i, keylen, key) != ASN1_ITERATOR_CONSTRUCTED ||
|
||||
i.type != ASN1_SEQUENCE ||
|
||||
asn1_der_decode_constructed_last(&i) != ASN1_ITERATOR_PRIMITIVE ||
|
||||
!DumpMPI(&i, &rsa->n) ||
|
||||
!GetMPI(&i, &rsa->e) ||
|
||||
asn1_der_iterator_next(&i) != ASN1_ITERATOR_END) {
|
||||
goto error;
|
||||
}
|
||||
|
||||
return rsa;
|
||||
|
||||
error:
|
||||
error_setg(errp, "Failed to parse RSA public key");
|
||||
qcrypto_akcipher_rsakey_free(rsa);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
QCryptoAkCipherRSAKey *qcrypto_akcipher_rsakey_parse(
|
||||
QCryptoAkCipherKeyType type, const uint8_t *key,
|
||||
size_t keylen, Error **errp)
|
||||
{
|
||||
switch (type) {
|
||||
case QCRYPTO_AKCIPHER_KEY_TYPE_PRIVATE:
|
||||
return qcrypto_nettle_rsa_private_key_parse(key, keylen, errp);
|
||||
|
||||
case QCRYPTO_AKCIPHER_KEY_TYPE_PUBLIC:
|
||||
return qcrypto_nettle_rsa_public_key_parse(key, keylen, errp);
|
||||
|
||||
default:
|
||||
error_setg(errp, "Unknown key type: %d", type);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* QEMU Crypto RSA key parser
|
||||
*
|
||||
* Copyright (c) 2022 Bytedance
|
||||
* Author: lei he <helei.sig11@bytedance.com>
|
||||
*
|
||||
* 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
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "rsakey.h"
|
||||
|
||||
void qcrypto_akcipher_rsakey_free(QCryptoAkCipherRSAKey *rsa_key)
|
||||
{
|
||||
if (!rsa_key) {
|
||||
return;
|
||||
}
|
||||
g_free(rsa_key->n.data);
|
||||
g_free(rsa_key->e.data);
|
||||
g_free(rsa_key->d.data);
|
||||
g_free(rsa_key->p.data);
|
||||
g_free(rsa_key->q.data);
|
||||
g_free(rsa_key->dp.data);
|
||||
g_free(rsa_key->dq.data);
|
||||
g_free(rsa_key->u.data);
|
||||
g_free(rsa_key);
|
||||
}
|
||||
|
||||
#if defined(CONFIG_NETTLE) && defined(CONFIG_HOGWEED)
|
||||
#include "rsakey-nettle.c.inc"
|
||||
#else
|
||||
#include "rsakey-builtin.c.inc"
|
||||
#endif
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* QEMU Crypto RSA key parser
|
||||
*
|
||||
* Copyright (c) 2022 Bytedance
|
||||
* Author: lei he <helei.sig11@bytedance.com>
|
||||
*
|
||||
* 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
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef QCRYPTO_RSAKEY_H
|
||||
#define QCRYPTO_RSAKEY_H
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "qemu/host-utils.h"
|
||||
#include "crypto/akcipher.h"
|
||||
|
||||
typedef struct QCryptoAkCipherRSAKey QCryptoAkCipherRSAKey;
|
||||
typedef struct QCryptoAkCipherMPI QCryptoAkCipherMPI;
|
||||
|
||||
/**
|
||||
* Multiple precious integer, encoded as two' complement,
|
||||
* copied directly from DER encoded ASN.1 structures.
|
||||
*/
|
||||
struct QCryptoAkCipherMPI {
|
||||
uint8_t *data;
|
||||
size_t len;
|
||||
};
|
||||
|
||||
/* See rfc2437: https://datatracker.ietf.org/doc/html/rfc2437 */
|
||||
struct QCryptoAkCipherRSAKey {
|
||||
/* The modulus */
|
||||
QCryptoAkCipherMPI n;
|
||||
/* The public exponent */
|
||||
QCryptoAkCipherMPI e;
|
||||
/* The private exponent */
|
||||
QCryptoAkCipherMPI d;
|
||||
/* The first factor */
|
||||
QCryptoAkCipherMPI p;
|
||||
/* The second factor */
|
||||
QCryptoAkCipherMPI q;
|
||||
/* The first factor's exponent */
|
||||
QCryptoAkCipherMPI dp;
|
||||
/* The second factor's exponent */
|
||||
QCryptoAkCipherMPI dq;
|
||||
/* The CRT coefficient */
|
||||
QCryptoAkCipherMPI u;
|
||||
};
|
||||
|
||||
/**
|
||||
* Parse DER encoded ASN.1 RSA keys, expected ASN.1 schemas:
|
||||
* RsaPrivKey ::= SEQUENCE {
|
||||
* version INTEGER
|
||||
* n INTEGER
|
||||
* e INTEGER
|
||||
* d INTEGER
|
||||
* p INTEGER
|
||||
* q INTEGER
|
||||
* dp INTEGER
|
||||
* dq INTEGER
|
||||
* u INTEGER
|
||||
* otherPrimeInfos OtherPrimeInfos OPTIONAL
|
||||
* }
|
||||
*
|
||||
* RsaPubKey ::= SEQUENCE {
|
||||
* n INTEGER
|
||||
* e INTEGER
|
||||
* }
|
||||
*
|
||||
* Returns: On success QCryptoAkCipherRSAKey is returned, otherwise returns NULL
|
||||
*/
|
||||
QCryptoAkCipherRSAKey *qcrypto_akcipher_rsakey_parse(
|
||||
QCryptoAkCipherKeyType type,
|
||||
const uint8_t *key, size_t keylen, Error **errp);
|
||||
|
||||
void qcrypto_akcipher_rsakey_free(QCryptoAkCipherRSAKey *key);
|
||||
|
||||
G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoAkCipherRSAKey,
|
||||
qcrypto_akcipher_rsakey_free);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* QEMU Crypto asymmetric algorithms
|
||||
*
|
||||
* Copyright (c) 2022 Bytedance
|
||||
* Author: zhenwei pi <pizhenwei@bytedance.com>
|
||||
*
|
||||
* 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
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef QCRYPTO_AKCIPHER_H
|
||||
#define QCRYPTO_AKCIPHER_H
|
||||
|
||||
#include "qapi/qapi-types-crypto.h"
|
||||
|
||||
typedef struct QCryptoAkCipher QCryptoAkCipher;
|
||||
|
||||
/**
|
||||
* qcrypto_akcipher_supports:
|
||||
* @opts: the asymmetric key algorithm and related options
|
||||
*
|
||||
* Determine if asymmetric key cipher decribed with @opts is
|
||||
* supported by the current configured build
|
||||
*
|
||||
* Returns: true if it is supported, false otherwise.
|
||||
*/
|
||||
bool qcrypto_akcipher_supports(QCryptoAkCipherOptions *opts);
|
||||
|
||||
/**
|
||||
* qcrypto_akcipher_new:
|
||||
* @opts: specify the algorithm and the related arguments
|
||||
* @type: private or public key type
|
||||
* @key: buffer to store the key
|
||||
* @key_len: the length of key buffer
|
||||
* @errp: error pointer
|
||||
*
|
||||
* Create akcipher context
|
||||
*
|
||||
* Returns: On success, a new QCryptoAkCipher initialized with @opt
|
||||
* is created and returned, otherwise NULL is returned.
|
||||
*/
|
||||
|
||||
QCryptoAkCipher *qcrypto_akcipher_new(const QCryptoAkCipherOptions *opts,
|
||||
QCryptoAkCipherKeyType type,
|
||||
const uint8_t *key, size_t key_len,
|
||||
Error **errp);
|
||||
|
||||
/**
|
||||
* qcrypto_akcipher_encrypt:
|
||||
* @akcipher: akcipher context
|
||||
* @in: plaintext pending to be encrypted
|
||||
* @in_len: length of plaintext, less or equal to the size reported
|
||||
* by a call to qcrypto_akcipher_max_plaintext_len()
|
||||
* @out: buffer to store the ciphertext
|
||||
* @out_len: length of ciphertext, less or equal to the size reported
|
||||
* by a call to qcrypto_akcipher_max_ciphertext_len()
|
||||
* @errp: error pointer
|
||||
*
|
||||
* Encrypt @in and write ciphertext into @out
|
||||
*
|
||||
* Returns: length of ciphertext if encrypt succeed,
|
||||
* otherwise -1 is returned
|
||||
*/
|
||||
int qcrypto_akcipher_encrypt(QCryptoAkCipher *akcipher,
|
||||
const void *in, size_t in_len,
|
||||
void *out, size_t out_len, Error **errp);
|
||||
|
||||
/**
|
||||
* qcrypto_akcipher_decrypt:
|
||||
* @akcipher: akcipher context
|
||||
* @in: ciphertext to be decrypted
|
||||
* @in_len: the length of ciphertext, less or equal to the size reported
|
||||
* by a call to qcrypto_akcipher_max_ciphertext_len()
|
||||
* @out: buffer to store the plaintext
|
||||
* @out_len: length of the plaintext buffer, less or equal to the size
|
||||
* reported by a call to qcrypto_akcipher_max_plaintext_len()
|
||||
* @errp: error pointer
|
||||
*
|
||||
* Decrypt @in and write plaintext into @out
|
||||
*
|
||||
* Returns: length of plaintext if decrypt succeed,
|
||||
* otherwise -1 is returned
|
||||
*/
|
||||
int qcrypto_akcipher_decrypt(QCryptoAkCipher *akcipher,
|
||||
const void *in, size_t in_len,
|
||||
void *out, size_t out_len, Error **errp);
|
||||
|
||||
/**
|
||||
* qcrypto_akcipher_sign:
|
||||
* @akcipher: akcipher context
|
||||
* @in: data to be signed
|
||||
* @in_len: the length of data, less or equal to the size reported
|
||||
* by a call to qcrypto_akcipher_max_dgst_len()
|
||||
* @out: buffer to store the signature
|
||||
* @out_len: length of the signature buffer, less or equal to the size
|
||||
* by a call to qcrypto_akcipher_max_signature_len()
|
||||
* @errp: error pointer
|
||||
*
|
||||
* Generate signature for @in, write into @out
|
||||
*
|
||||
* Returns: length of signature if succeed,
|
||||
* otherwise -1 is returned
|
||||
*/
|
||||
int qcrypto_akcipher_sign(QCryptoAkCipher *akcipher,
|
||||
const void *in, size_t in_len,
|
||||
void *out, size_t out_len, Error **errp);
|
||||
|
||||
/**
|
||||
* qcrypto_akcipher_verify:
|
||||
* @akcipher: akcipher context
|
||||
* @in: pointer to the signature
|
||||
* @in_len: length of signature, ess or equal to the size reported
|
||||
* by a call to qcrypto_akcipher_max_signature_len()
|
||||
* @in2: pointer to original data
|
||||
* @in2_len: the length of original data, less or equal to the size
|
||||
* by a call to qcrypto_akcipher_max_dgst_len()
|
||||
* @errp: error pointer
|
||||
*
|
||||
* Verify @in and @in2 match or not
|
||||
*
|
||||
* Returns: 0 for succeed,
|
||||
* otherwise -1 is returned
|
||||
*/
|
||||
int qcrypto_akcipher_verify(QCryptoAkCipher *akcipher,
|
||||
const void *in, size_t in_len,
|
||||
const void *in2, size_t in2_len, Error **errp);
|
||||
|
||||
int qcrypto_akcipher_max_plaintext_len(QCryptoAkCipher *akcipher);
|
||||
|
||||
int qcrypto_akcipher_max_ciphertext_len(QCryptoAkCipher *akcipher);
|
||||
|
||||
int qcrypto_akcipher_max_signature_len(QCryptoAkCipher *akcipher);
|
||||
|
||||
int qcrypto_akcipher_max_dgst_len(QCryptoAkCipher *akcipher);
|
||||
|
||||
/**
|
||||
* qcrypto_akcipher_free:
|
||||
* @akcipher: akcipher context
|
||||
*
|
||||
* Free the akcipher context
|
||||
*
|
||||
*/
|
||||
void qcrypto_akcipher_free(QCryptoAkCipher *akcipher);
|
||||
|
||||
G_DEFINE_AUTOPTR_CLEANUP_FUNC(QCryptoAkCipher, qcrypto_akcipher_free)
|
||||
|
||||
#endif /* QCRYPTO_AKCIPHER_H */
|
||||
+11
@@ -1121,6 +1121,7 @@ endif
|
||||
# gcrypt over nettle for performance reasons.
|
||||
gcrypt = not_found
|
||||
nettle = not_found
|
||||
hogweed = not_found
|
||||
xts = 'none'
|
||||
|
||||
if get_option('nettle').enabled() and get_option('gcrypt').enabled()
|
||||
@@ -1158,6 +1159,15 @@ if not gnutls_crypto.found()
|
||||
endif
|
||||
endif
|
||||
|
||||
gmp = dependency('gmp', required: false, method: 'pkg-config', kwargs: static_kwargs)
|
||||
if nettle.found() and gmp.found()
|
||||
hogweed = dependency('hogweed', version: '>=3.4',
|
||||
method: 'pkg-config',
|
||||
required: get_option('nettle'),
|
||||
kwargs: static_kwargs)
|
||||
endif
|
||||
|
||||
|
||||
gtk = not_found
|
||||
gtkx11 = not_found
|
||||
vte = not_found
|
||||
@@ -1769,6 +1779,7 @@ config_host_data.set('CONFIG_GNUTLS_CRYPTO', gnutls_crypto.found())
|
||||
config_host_data.set('CONFIG_TASN1', tasn1.found())
|
||||
config_host_data.set('CONFIG_GCRYPT', gcrypt.found())
|
||||
config_host_data.set('CONFIG_NETTLE', nettle.found())
|
||||
config_host_data.set('CONFIG_HOGWEED', hogweed.found())
|
||||
config_host_data.set('CONFIG_QEMU_PRIVATE_XTS', xts == 'private')
|
||||
config_host_data.set('CONFIG_MALLOC_TRIM', has_malloc_trim)
|
||||
config_host_data.set('CONFIG_STATX', has_statx)
|
||||
|
||||
@@ -534,3 +534,67 @@
|
||||
'data': { '*loaded': { 'type': 'bool', 'features': ['deprecated'] },
|
||||
'*sanity-check': 'bool',
|
||||
'*passwordid': 'str' } }
|
||||
##
|
||||
# @QCryptoAkCipherAlgorithm:
|
||||
#
|
||||
# The supported algorithms for asymmetric encryption ciphers
|
||||
#
|
||||
# @rsa: RSA algorithm
|
||||
#
|
||||
# Since: 7.1
|
||||
##
|
||||
{ 'enum': 'QCryptoAkCipherAlgorithm',
|
||||
'prefix': 'QCRYPTO_AKCIPHER_ALG',
|
||||
'data': ['rsa']}
|
||||
|
||||
##
|
||||
# @QCryptoAkCipherKeyType:
|
||||
#
|
||||
# The type of asymmetric keys.
|
||||
#
|
||||
# Since: 7.1
|
||||
##
|
||||
{ 'enum': 'QCryptoAkCipherKeyType',
|
||||
'prefix': 'QCRYPTO_AKCIPHER_KEY_TYPE',
|
||||
'data': ['public', 'private']}
|
||||
|
||||
##
|
||||
# @QCryptoRSAPaddingAlgorithm:
|
||||
#
|
||||
# The padding algorithm for RSA.
|
||||
#
|
||||
# @raw: no padding used
|
||||
# @pkcs1: pkcs1#v1.5
|
||||
#
|
||||
# Since: 7.1
|
||||
##
|
||||
{ 'enum': 'QCryptoRSAPaddingAlgorithm',
|
||||
'prefix': 'QCRYPTO_RSA_PADDING_ALG',
|
||||
'data': ['raw', 'pkcs1']}
|
||||
|
||||
##
|
||||
# @QCryptoAkCipherOptionsRSA:
|
||||
#
|
||||
# Specific parameters for RSA algorithm.
|
||||
#
|
||||
# @hash-alg: QCryptoHashAlgorithm
|
||||
# @padding-alg: QCryptoRSAPaddingAlgorithm
|
||||
#
|
||||
# Since: 7.1
|
||||
##
|
||||
{ 'struct': 'QCryptoAkCipherOptionsRSA',
|
||||
'data': { 'hash-alg':'QCryptoHashAlgorithm',
|
||||
'padding-alg': 'QCryptoRSAPaddingAlgorithm'}}
|
||||
|
||||
##
|
||||
# @QCryptoAkCipherOptions:
|
||||
#
|
||||
# The options that are available for all asymmetric key algorithms
|
||||
# when creating a new QCryptoAkCipher.
|
||||
#
|
||||
# Since: 7.1
|
||||
##
|
||||
{ 'union': 'QCryptoAkCipherOptions',
|
||||
'base': { 'alg': 'QCryptoAkCipherAlgorithm' },
|
||||
'discriminator': 'alg',
|
||||
'data': { 'rsa': 'QCryptoAkCipherOptionsRSA' }}
|
||||
|
||||
@@ -0,0 +1,137 @@
|
||||
/*
|
||||
* QEMU Crypto akcipher speed benchmark
|
||||
*
|
||||
* Copyright (c) 2022 Bytedance
|
||||
*
|
||||
* Authors:
|
||||
* lei he <helei.sig11@bytedance.com>
|
||||
*
|
||||
* This work is licensed under the terms of the GNU GPL, version 2 or
|
||||
* (at your option) any later version. See the COPYING file in the
|
||||
* top-level directory.
|
||||
*/
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "crypto/init.h"
|
||||
#include "crypto/akcipher.h"
|
||||
#include "standard-headers/linux/virtio_crypto.h"
|
||||
|
||||
#include "test_akcipher_keys.inc"
|
||||
|
||||
static QCryptoAkCipher *create_rsa_akcipher(const uint8_t *priv_key,
|
||||
size_t keylen,
|
||||
QCryptoRSAPaddingAlgorithm padding,
|
||||
QCryptoHashAlgorithm hash)
|
||||
{
|
||||
QCryptoAkCipherOptions opt;
|
||||
QCryptoAkCipher *rsa;
|
||||
|
||||
opt.alg = QCRYPTO_AKCIPHER_ALG_RSA;
|
||||
opt.u.rsa.padding_alg = padding;
|
||||
opt.u.rsa.hash_alg = hash;
|
||||
rsa = qcrypto_akcipher_new(&opt, QCRYPTO_AKCIPHER_KEY_TYPE_PRIVATE,
|
||||
priv_key, keylen, &error_abort);
|
||||
return rsa;
|
||||
}
|
||||
|
||||
static void test_rsa_speed(const uint8_t *priv_key, size_t keylen,
|
||||
size_t key_size)
|
||||
{
|
||||
#define BYTE 8
|
||||
#define SHA1_DGST_LEN 20
|
||||
#define SIGN_TIMES 10000
|
||||
#define VERIFY_TIMES 100000
|
||||
#define PADDING QCRYPTO_RSA_PADDING_ALG_PKCS1
|
||||
#define HASH QCRYPTO_HASH_ALG_SHA1
|
||||
|
||||
g_autoptr(QCryptoAkCipher) rsa =
|
||||
create_rsa_akcipher(priv_key, keylen, PADDING, HASH);
|
||||
g_autofree uint8_t *dgst = NULL;
|
||||
g_autofree uint8_t *signature = NULL;
|
||||
size_t count;
|
||||
|
||||
dgst = g_new0(uint8_t, SHA1_DGST_LEN);
|
||||
memset(dgst, g_test_rand_int(), SHA1_DGST_LEN);
|
||||
signature = g_new0(uint8_t, key_size / BYTE);
|
||||
|
||||
g_test_message("benchmark rsa%zu (%s-%s) sign...", key_size,
|
||||
QCryptoRSAPaddingAlgorithm_str(PADDING),
|
||||
QCryptoHashAlgorithm_str(HASH));
|
||||
g_test_timer_start();
|
||||
for (count = 0; count < SIGN_TIMES; ++count) {
|
||||
g_assert(qcrypto_akcipher_sign(rsa, dgst, SHA1_DGST_LEN,
|
||||
signature, key_size / BYTE,
|
||||
&error_abort) > 0);
|
||||
}
|
||||
g_test_timer_elapsed();
|
||||
g_test_message("rsa%zu (%s-%s) sign %zu times in %.2f seconds,"
|
||||
" %.2f times/sec ",
|
||||
key_size, QCryptoRSAPaddingAlgorithm_str(PADDING),
|
||||
QCryptoHashAlgorithm_str(HASH),
|
||||
count, g_test_timer_last(),
|
||||
(double)count / g_test_timer_last());
|
||||
|
||||
g_test_message("benchmark rsa%zu (%s-%s) verification...", key_size,
|
||||
QCryptoRSAPaddingAlgorithm_str(PADDING),
|
||||
QCryptoHashAlgorithm_str(HASH));
|
||||
g_test_timer_start();
|
||||
for (count = 0; count < VERIFY_TIMES; ++count) {
|
||||
g_assert(qcrypto_akcipher_verify(rsa, signature, key_size / BYTE,
|
||||
dgst, SHA1_DGST_LEN,
|
||||
&error_abort) == 0);
|
||||
}
|
||||
g_test_timer_elapsed();
|
||||
g_test_message("rsa%zu (%s-%s) verify %zu times in %.2f seconds,"
|
||||
" %.2f times/sec ",
|
||||
key_size, QCryptoRSAPaddingAlgorithm_str(PADDING),
|
||||
QCryptoHashAlgorithm_str(HASH),
|
||||
count, g_test_timer_last(),
|
||||
(double)count / g_test_timer_last());
|
||||
}
|
||||
|
||||
static void test_rsa_1024_speed(const void *opaque)
|
||||
{
|
||||
size_t key_size = (size_t)opaque;
|
||||
test_rsa_speed(rsa1024_priv_key, sizeof(rsa1024_priv_key), key_size);
|
||||
}
|
||||
|
||||
static void test_rsa_2048_speed(const void *opaque)
|
||||
{
|
||||
size_t key_size = (size_t)opaque;
|
||||
test_rsa_speed(rsa2048_priv_key, sizeof(rsa2048_priv_key), key_size);
|
||||
}
|
||||
|
||||
static void test_rsa_4096_speed(const void *opaque)
|
||||
{
|
||||
size_t key_size = (size_t)opaque;
|
||||
test_rsa_speed(rsa4096_priv_key, sizeof(rsa4096_priv_key), key_size);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char *alg = NULL;
|
||||
char *size = NULL;
|
||||
g_test_init(&argc, &argv, NULL);
|
||||
g_assert(qcrypto_init(NULL) == 0);
|
||||
|
||||
#define ADD_TEST(asym_alg, keysize) \
|
||||
if ((!alg || g_str_equal(alg, #asym_alg)) && \
|
||||
(!size || g_str_equal(size, #keysize))) \
|
||||
g_test_add_data_func( \
|
||||
"/crypto/akcipher/" #asym_alg "-" #keysize, \
|
||||
(void *)keysize, \
|
||||
test_ ## asym_alg ## _ ## keysize ## _speed)
|
||||
|
||||
if (argc >= 2) {
|
||||
alg = argv[1];
|
||||
}
|
||||
if (argc >= 3) {
|
||||
size = argv[2];
|
||||
}
|
||||
|
||||
ADD_TEST(rsa, 1024);
|
||||
ADD_TEST(rsa, 2048);
|
||||
ADD_TEST(rsa, 4096);
|
||||
|
||||
return g_test_run();
|
||||
}
|
||||
@@ -20,6 +20,7 @@ if have_block
|
||||
'benchmark-crypto-hash': [crypto],
|
||||
'benchmark-crypto-hmac': [crypto],
|
||||
'benchmark-crypto-cipher': [crypto],
|
||||
'benchmark-crypto-akcipher': [crypto],
|
||||
}
|
||||
endif
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -77,7 +77,9 @@ if have_block
|
||||
'test-crypto-hash': [crypto],
|
||||
'test-crypto-hmac': [crypto],
|
||||
'test-crypto-cipher': [crypto],
|
||||
'test-crypto-akcipher': [crypto],
|
||||
'test-crypto-secret': [crypto, keyutils],
|
||||
'test-crypto-der': [crypto],
|
||||
'test-authz-simple': [authz],
|
||||
'test-authz-list': [authz],
|
||||
'test-authz-listfile': [authz],
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,290 @@
|
||||
/*
|
||||
* QEMU Crypto akcipher algorithms
|
||||
*
|
||||
* Copyright (c) 2022 Bytedance
|
||||
* Author: lei he <helei@bytedance.com>
|
||||
*
|
||||
* 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
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* 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/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "qemu/osdep.h"
|
||||
#include "crypto/der.h"
|
||||
|
||||
/* rsa(512) private key, generated by openssl */
|
||||
static const uint8_t test_rsa512_priv_key[] =
|
||||
"\x30\x82\x01\x39" /* SEQUENCE, offset: 0, length: 313 */
|
||||
"\x02\x01\x00" /* INTEGER, offset: 4, length: 1 */
|
||||
"\x02\x41" /* INTEGER, offset: 7, length: 65 */
|
||||
"\x00\xb9\xe1\x22\xdb\x56\x2f\xb6\xf7\xf0\x0a\x87\x43\x07\x12\xdb"
|
||||
"\x6d\xb6\x2b\x41\x8d\x2c\x3c\xa5\xdd\x78\x9a\x8f\xab\x8e\xf2\x4a"
|
||||
"\xc8\x34\x0c\x12\x4f\x11\x90\xc6\xc2\xa5\xd0\xcd\xfb\xfc\x2c\x95"
|
||||
"\x56\x82\xdf\x39\xf3\x3b\x1d\x62\x26\x97\xb7\x93\x25\xc7\xec\x7e"
|
||||
"\xf7"
|
||||
"\x02\x03\x01\x00\x01" /* INTEGER, offset: 74, length: 3 */
|
||||
"\x02\x40" /* INTEGER, offset: 79, length: 64 */
|
||||
"\x1e\x80\xfe\xda\x65\xdb\x70\xb8\x61\x91\x28\xbf\x6c\x32\xc1\x05"
|
||||
"\xd1\x26\x6a\x1c\x83\xcc\xf4\x1f\x53\x42\x72\x1f\x62\x57\x0a\xc4"
|
||||
"\x66\x76\x30\x87\xb9\xb1\xb9\x6a\x63\xfd\x8f\x3e\xfc\x35\x3f\xd6"
|
||||
"\x2e\x6c\xc8\x70\x8a\x17\xc1\x28\x6a\xfe\x51\x56\xb3\x92\x6f\x09"
|
||||
"\x02\x21" /* INTEGER, offset: 145, length: 33 */
|
||||
"\x00\xe3\x2e\x2d\x8d\xba\x1c\x34\x4c\x49\x9f\xc1\xa6\xdd\xd7\x13"
|
||||
"\x8d\x05\x48\xdd\xff\x5c\x30\xbc\x6b\xc4\x18\x9d\xfc\xa2\xd0\x9b"
|
||||
"\x4d"
|
||||
"\x02\x21" /* INTEGER, offset: 180, length: 33 */
|
||||
"\x00\xd1\x75\xaf\x4b\xc6\x1a\xb0\x98\x14\x42\xae\x33\xf3\x44\xde"
|
||||
"\x21\xcb\x04\xda\xfb\x1e\x35\x92\xcd\x69\xc0\x83\x06\x83\x8e\x39"
|
||||
"\x53"
|
||||
"\x02\x20" /* INTEGER, offset: 215, length: 32 */
|
||||
"\x68\x8d\x2a\xf7\xcb\xcc\x09\x21\x86\xcc\x98\x21\xc4\x7c\xa4\x09"
|
||||
"\xc5\x81\xd8\x71\x1a\x2b\x6f\xbb\xa4\xde\xb3\x6e\xbe\x3b\x85\x0d"
|
||||
"\x02\x20" /* INTEGER, offset: 249, length: 32 */
|
||||
"\x64\x06\x0e\xef\xe0\x6a\x5e\x6a\x41\x42\x96\x6d\xb8\x7d\xea\x95"
|
||||
"\xb8\x9d\x58\xf5\x12\x38\x03\x22\x94\x9d\x99\xf4\x42\x5e\x68\x81"
|
||||
"\x02\x20" /* INTEGER, offset: 283, length: 32 */
|
||||
"\x7f\x1d\x87\xe8\x55\x30\x75\xc7\x29\xec\xc9\x65\x76\x5a\x6a\xa3"
|
||||
"\x4a\x6e\xe1\x26\x65\xd1\x76\xd5\xb9\xd1\x8b\xa8\x73\xe2\x6a\x9e";
|
||||
|
||||
static const uint8_t test_rsa2048_priv_key[] =
|
||||
"\x30\x82\x04\xa6" /* SEQUENCE, offset: 0, length 1190 */
|
||||
"\x02\x01\x00" /* INTEGER, offset: 4, length: 1 */
|
||||
"\x02\x82\x01\x01" /* INTEGER, offset: 7, length: 257 */
|
||||
"\x00\xd1\x48\xc2\xc1\x1d\x4f\x94\xf2\xbb\x9b\xe2\x2d\xe1\xea\x4c"
|
||||
"\xce\x41\x72\xe3\x41\x7e\x9d\x91\x85\xa3\x4e\xe1\x2c\xf6\x52\x6d"
|
||||
"\xf9\x84\x64\xdf\x87\x28\x4a\xc9\x9d\x78\x93\x47\xc8\xd9\x66\x2e"
|
||||
"\xf4\xc6\xf0\x32\x15\x1a\xe8\xaf\x5a\xca\x3a\xd3\x3e\xf6\xde\x86"
|
||||
"\xdd\x9b\xa6\x4d\x74\x58\xf0\x11\x7f\x66\xd5\x1c\xd8\xde\xa3\xf8"
|
||||
"\xa3\xfc\x33\x55\x89\xa9\xc3\xea\x5b\x2e\x31\x06\xf8\xcb\x9e\x6e"
|
||||
"\xb2\x68\x0d\xe6\xc3\x5c\x2d\xf8\xa2\xbd\x00\x1a\xf6\xb6\xdd\x14"
|
||||
"\x8d\x11\x6d\x2d\xc6\x0c\x09\xe6\xf6\xb9\x8b\x87\x4c\x9f\x4d\x63"
|
||||
"\xd3\x94\xf4\x32\xca\xcf\x5e\xbf\xe2\x7f\x73\x5a\x65\xec\x82\x0d"
|
||||
"\x7f\x30\x25\x03\xd4\x3a\xff\xa2\xe8\xd6\xb5\x1f\x4f\x36\x64\x61"
|
||||
"\xc3\x5f\xb2\x9e\x0c\x53\x04\x19\x34\x99\xe8\xe3\xe6\xd3\x2f\x45"
|
||||
"\x58\x8e\x5d\x54\x5a\xa0\xc0\x5e\x51\x9b\x22\x15\xec\x26\x6f\x72"
|
||||
"\x68\xe9\xbf\x5d\x1d\xb5\xd9\xe4\x81\x1a\x92\x66\xa8\xcb\x73\x46"
|
||||
"\xab\x96\x7b\xf8\x9c\xf5\xb5\x9e\x2b\x13\x71\xe0\x01\x0c\x59\x1b"
|
||||
"\x63\x9f\xb7\xd1\xcd\x47\x8e\xc7\x3a\xbe\xcb\x47\xa7\x23\x43\xa7"
|
||||
"\x7d\xbd\x2c\x4e\x22\x37\xcc\xf9\x1b\x1b\xbb\xed\xec\xf0\x47\x92"
|
||||
"\x43"
|
||||
"\x02\x03\x01\x00\x01" /* INTEGER, offset 268, length 3 */
|
||||
"\x02\x82\x01\x01" /* INTEGER, offset 273, length 257 */
|
||||
"\x00\x8d\x21\x97\x0c\x29\x9a\xf8\x23\xf4\x76\x3b\xc1\x9b\x3e\xa8"
|
||||
"\x8a\xd2\xc2\x0a\x14\xa9\xb0\xd2\x68\x9f\x67\x5b\x1c\x3a\x03\xfe"
|
||||
"\x5b\xac\x77\x65\xf1\xbc\x2f\x2a\xe5\x01\x61\xb8\x9f\xee\x53\x25"
|
||||
"\x49\x36\x3a\xd6\x5b\x3b\x29\x3c\xcf\x69\xde\xdf\x83\xef\x70\xc2"
|
||||
"\xdc\x00\xd1\xd6\x1b\xa6\xba\x45\xe2\x77\x53\x31\xbf\xe1\xec\x0b"
|
||||
"\x89\x72\x52\x9f\xd5\x54\xe1\x64\x52\x16\xc5\x43\x21\x56\x16\xc2"
|
||||
"\x29\x97\x58\x00\x8d\x2f\xc5\x64\x8d\x42\x0d\x27\x21\xc6\xd1\x31"
|
||||
"\xc1\xab\xc5\xc7\x7f\x6d\xb0\xe3\xca\xef\xf6\xf2\xc7\xae\x09\xbf"
|
||||
"\x4d\xc0\x4e\x90\x2c\x28\xb9\xcc\x22\x74\xf2\xd5\xff\x4d\x86\xf6"
|
||||
"\xec\x45\x1f\xbf\x25\x4c\x30\x26\x76\x4f\x09\x13\x83\xef\x35\x73"
|
||||
"\xa3\xa2\xb1\x40\xcf\x07\x7a\x83\xae\xea\x00\xea\x74\xc7\x54\x6a"
|
||||
"\x88\x19\xed\x35\xd3\x7e\x5e\xac\x51\xc1\x1e\x5e\x2c\x57\x72\x20"
|
||||
"\x10\x6a\x0c\x47\xe1\xf0\x36\x70\xd2\xa7\x57\x64\x47\x46\x9f\xca"
|
||||
"\x23\x8a\x48\x50\x1d\x33\x6a\x86\x46\x69\xed\x54\x65\x6b\x9e\xab"
|
||||
"\x1f\x84\x87\xf4\x92\x8a\x6c\x44\x20\xaa\x8d\xd8\x50\xde\x45\x74"
|
||||
"\xe0\xa8\xc7\xb9\x38\x74\x24\x51\x33\xf0\x39\x54\x6c\x11\xae\xc2"
|
||||
"\x29"
|
||||
"\x02\x81\x81" /* INTEGER, offset 534, length 129 */
|
||||
"\x00\xe8\x26\xd1\xf9\xa0\xd3\x0e\x3f\x2f\x89\x9b\x94\x16\x12\xd1"
|
||||
"\xae\x3c\x53\x9c\xcf\xc6\xf7\x03\xf5\xdf\x39\xdc\x25\x5d\xcb\xb8"
|
||||
"\xb9\x74\x3e\x3b\x36\xf6\xa0\x8d\xb1\x0e\xd8\xfe\x8c\xcd\x01\x13"
|
||||
"\x77\x73\x08\x0f\x32\xbd\xe6\x95\xdc\xd0\x14\x7d\x44\xdc\x3e\xd9"
|
||||
"\xaa\x8a\x32\xe6\x0e\x76\xb6\x05\xc5\x6b\x87\x78\x9a\x32\xe2\xf8"
|
||||
"\x78\xba\x58\x75\x58\xd5\x26\x9d\x9a\x0f\xb6\xca\xb5\x27\xd8\x58"
|
||||
"\xae\x3f\x49\x54\xd2\x2b\xac\x28\x39\x88\x31\x42\x12\x08\xea\x0b"
|
||||
"\x39\x58\xae\xf3\x82\xa0\xe2\x75\x7c\x96\xa9\xb8\x57\x29\x6d\xd7"
|
||||
"\x37"
|
||||
"\x02\x81\x81" /* INTEGER, offset 666, length 129 */
|
||||
"\x00\xe6\xc8\x91\x50\x49\x97\x56\x70\x6e\x25\xf5\x77\x25\xa5\x41"
|
||||
"\xfe\xd7\x25\x1b\xc1\x4a\xff\x37\x44\x2b\x46\xa0\xdf\xe8\x02\x09"
|
||||
"\xdd\xa8\x41\xa1\x12\x84\x3c\xf8\xc2\x13\x3e\xb8\x4b\x22\x01\xac"
|
||||
"\xa6\x09\xb2\xe9\xcd\xc8\x51\xee\xde\xa3\x1e\x6b\xfe\xb1\xf8\xb6"
|
||||
"\x9e\x48\x36\x62\x0b\x05\xfa\x38\xc1\x06\x04\x58\x95\x4d\x25\x13"
|
||||
"\x6d\x0b\x12\x0b\xc9\x6d\x59\xfc\x33\x03\x36\x01\x12\x09\x72\x74"
|
||||
"\x5e\x98\x65\x66\x2f\x3a\xde\xd8\xd4\xee\x6f\x82\xe6\x36\x49\x12"
|
||||
"\x6a\x94\x28\xe9\x28\x9e\xef\x29\xdc\xdf\xab\x94\x65\x02\x4e\x4b"
|
||||
"\x55"
|
||||
"\x02\x81\x81" /* INTEGER, offset 798, length 129 */
|
||||
"\x00\xc9\xda\xb7\x48\x6e\x66\x15\x45\x2b\x78\x63\x26\x67\xeb\x05"
|
||||
"\x16\x92\xad\xc0\xf3\x88\xf4\xcf\x24\xc2\x6b\xf4\xd7\x28\xaf\x32"
|
||||
"\x77\x4e\x73\xad\xd9\x24\xa8\x85\x8b\x26\x75\xd7\x1f\x66\x41\x41"
|
||||
"\x43\xe3\x69\x66\x8d\xa0\x41\x16\x9d\x60\xef\xef\xdc\x28\x05\x1e"
|
||||
"\x0e\x03\x0c\x2e\xac\xf4\xdb\x60\x39\x40\x3e\x12\xc7\x40\xe7\xc9"
|
||||
"\x54\x6f\xf2\xea\x55\xcb\x40\x40\x58\xec\xc0\xeb\x90\x88\x8c\xbc"
|
||||
"\xcf\x05\x88\x25\x90\x79\x18\xc0\x01\x06\x42\x8e\x48\x50\x27\xf0"
|
||||
"\x8a\x74\x69\xea\xa1\xf2\x71\xf5\xe5\xd6\xba\xcb\xe6\x3d\xc7\x9c"
|
||||
"\x11"
|
||||
"\x02\x81\x81" /* INTEGER, offset 930, length 129 */
|
||||
"\x00\xc9\xf5\x04\xad\x34\xe9\x39\xdc\x83\x97\xb6\x3a\x40\xf8\x60"
|
||||
"\x4b\x69\xec\xf0\x5f\xf3\x88\x69\xcd\xbe\xed\x3c\xc5\x14\x5c\x0c"
|
||||
"\x54\x2b\xf4\xda\xc6\xc0\x70\x36\xe4\x67\x41\x00\xb7\xc7\x17\x9e"
|
||||
"\x05\x63\x01\x6d\x77\x06\x71\x24\xcf\x32\x01\xe2\x51\xed\x5e\x90"
|
||||
"\x38\xed\x4a\xa1\xfb\xb1\x8c\x69\xf4\x08\x96\xef\x0a\x20\x8b\x6c"
|
||||
"\x77\x85\x33\x92\x9a\xff\x95\xba\x8c\xcd\xa7\x89\xc2\x46\x00\x21"
|
||||
"\xf3\xd1\xfb\x12\x34\x0c\x99\x8d\x38\xb1\x3b\x66\x5a\x9d\x70\xce"
|
||||
"\xab\xf3\xe1\xe5\x40\x05\xed\x97\x3d\xd1\x82\x6e\x07\x02\xc0\x8f"
|
||||
"\x4d"
|
||||
"\x02\x81\x81" /* INTEGER, offset 1062, length 129 */
|
||||
"\x00\xe4\x96\x79\xa8\x6a\x70\xdd\x67\x42\xff\x15\x11\x9e\x01\x71"
|
||||
"\xac\xf1\x70\x7d\x87\xe2\x6e\x0c\x4d\xbb\x21\x15\xbb\xa7\x4e\x0c"
|
||||
"\x09\x7e\x82\xca\x91\xbe\xd0\xdd\x9c\x8c\xb0\x77\x64\x30\x1b\x7e"
|
||||
"\xbb\x69\xcb\x4c\xde\xd6\x6a\xb9\x72\x15\x79\xdc\x05\x99\x69\x8b"
|
||||
"\x24\xa1\xad\x13\x35\x31\xc0\x0b\xf1\xd2\x06\x7c\x94\x1a\x21\x2f"
|
||||
"\x02\xb9\xf0\xd0\xbb\xf7\xb7\x78\xf9\x3d\x76\x60\xd6\x6b\x5f\x35"
|
||||
"\x88\x14\x33\xe6\xbc\xca\x6b\x88\x90\x57\x3b\x0c\xa3\x6e\x47\xdf"
|
||||
"\x4e\x2f\x4c\xf9\xab\x97\x38\xe4\x20\x32\x32\x96\xc8\x9e\x79\xd3"
|
||||
"\x12";
|
||||
|
||||
#define MAX_CHECKER_COUNT 32
|
||||
|
||||
typedef struct QCryptoAns1DecoderResultChecker QCryptoAns1DecoderResultChecker;
|
||||
struct QCryptoAns1DecoderResultChecker {
|
||||
int (*action) (const uint8_t **data, size_t *dlen,
|
||||
QCryptoDERDecodeCb cb, void *opaque, Error **errp);
|
||||
QCryptoDERDecodeCb cb;
|
||||
const uint8_t *exp_value;
|
||||
size_t exp_vlen;
|
||||
};
|
||||
|
||||
typedef struct QCryptoAns1DecoderTestData QCryptoAns1DecoderTestData;
|
||||
struct QCryptoAns1DecoderTestData {
|
||||
const char *path;
|
||||
const uint8_t *test_data;
|
||||
size_t test_data_len;
|
||||
QCryptoAns1DecoderResultChecker checker[MAX_CHECKER_COUNT];
|
||||
};
|
||||
|
||||
typedef struct QCryptoAns1DecoderTestContext QCryptoAns1DecoderTestContext;
|
||||
struct QCryptoAns1DecoderTestContext {
|
||||
const uint8_t *data;
|
||||
size_t dlen;
|
||||
};
|
||||
|
||||
static int checker_callback(void *opaque, const uint8_t *value,
|
||||
size_t vlen, Error **errp)
|
||||
{
|
||||
QCryptoAns1DecoderResultChecker *checker =
|
||||
(QCryptoAns1DecoderResultChecker *)opaque;
|
||||
|
||||
g_assert(value == checker->exp_value);
|
||||
g_assert(vlen == checker->exp_vlen);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_ans1(const void *opaque)
|
||||
{
|
||||
const QCryptoAns1DecoderTestData *test_data =
|
||||
(QCryptoAns1DecoderTestData *)opaque;
|
||||
QCryptoAns1DecoderTestContext ctx[MAX_CHECKER_COUNT];
|
||||
int seq_depth = 0, checker_idx = 0;
|
||||
ctx[seq_depth].data = test_data->test_data;
|
||||
ctx[seq_depth].dlen = test_data->test_data_len;
|
||||
bool all_checker_completed = false;
|
||||
|
||||
do {
|
||||
const QCryptoAns1DecoderResultChecker *checker =
|
||||
&test_data->checker[checker_idx++];
|
||||
QCryptoAns1DecoderTestContext *c = &ctx[seq_depth];
|
||||
if (!checker->action) {
|
||||
all_checker_completed = true;
|
||||
break;
|
||||
}
|
||||
g_assert(checker->action(&c->data, &c->dlen, checker_callback,
|
||||
(void *)checker, &error_abort)
|
||||
== checker->exp_vlen);
|
||||
if (checker->action == qcrypto_der_decode_seq) {
|
||||
++seq_depth;
|
||||
ctx[seq_depth].data = checker->exp_value;
|
||||
ctx[seq_depth].dlen = checker->exp_vlen;
|
||||
}
|
||||
while (seq_depth != 0 && ctx[seq_depth].dlen == 0) {
|
||||
--seq_depth;
|
||||
}
|
||||
|
||||
} while (true);
|
||||
g_assert(seq_depth == 0);
|
||||
g_assert(ctx[seq_depth].dlen == 0);
|
||||
g_assert(all_checker_completed);
|
||||
}
|
||||
|
||||
static QCryptoAns1DecoderTestData test_data[] = {
|
||||
{
|
||||
.path = "/crypto/der/parse-rsa512-priv-key",
|
||||
.test_data = test_rsa512_priv_key,
|
||||
.test_data_len = sizeof(test_rsa512_priv_key) - 1,
|
||||
.checker = {
|
||||
{ qcrypto_der_decode_seq, checker_callback,
|
||||
test_rsa512_priv_key + 4, 313 },
|
||||
{ qcrypto_der_decode_int, checker_callback,
|
||||
test_rsa512_priv_key + 4 + 2, 1 },
|
||||
{ qcrypto_der_decode_int, checker_callback,
|
||||
test_rsa512_priv_key + 7 + 2, 65 },
|
||||
{ qcrypto_der_decode_int, checker_callback,
|
||||
test_rsa512_priv_key + 74 + 2, 3 },
|
||||
{ qcrypto_der_decode_int, checker_callback,
|
||||
test_rsa512_priv_key + 79 + 2, 64 },
|
||||
{ qcrypto_der_decode_int, checker_callback,
|
||||
test_rsa512_priv_key + 145 + 2, 33 },
|
||||
{ qcrypto_der_decode_int, checker_callback,
|
||||
test_rsa512_priv_key + 180 + 2, 33 },
|
||||
{ qcrypto_der_decode_int, checker_callback,
|
||||
test_rsa512_priv_key + 215 + 2, 32 },
|
||||
{ qcrypto_der_decode_int, checker_callback,
|
||||
test_rsa512_priv_key + 249 + 2, 32 },
|
||||
{ qcrypto_der_decode_int, checker_callback,
|
||||
test_rsa512_priv_key + 283 + 2, 32 },
|
||||
},
|
||||
},
|
||||
{
|
||||
.path = "/crypto/der/parse-rsa2048-priv-key",
|
||||
.test_data = test_rsa2048_priv_key,
|
||||
.test_data_len = sizeof(test_rsa2048_priv_key) - 1,
|
||||
.checker = {
|
||||
{ qcrypto_der_decode_seq, checker_callback,
|
||||
test_rsa2048_priv_key + 4, 1190 },
|
||||
{ qcrypto_der_decode_int, checker_callback,
|
||||
test_rsa2048_priv_key + 4 + 2, 1 },
|
||||
{ qcrypto_der_decode_int, checker_callback,
|
||||
test_rsa2048_priv_key + 7 + 4, 257 },
|
||||
{ qcrypto_der_decode_int, checker_callback,
|
||||
test_rsa2048_priv_key + 268 + 2, 3 },
|
||||
{ qcrypto_der_decode_int, checker_callback,
|
||||
test_rsa2048_priv_key + 273 + 4, 257 },
|
||||
{ qcrypto_der_decode_int, checker_callback,
|
||||
test_rsa2048_priv_key + 534 + 3, 129 },
|
||||
{ qcrypto_der_decode_int, checker_callback,
|
||||
test_rsa2048_priv_key + 666 + 3, 129 },
|
||||
{ qcrypto_der_decode_int, checker_callback,
|
||||
test_rsa2048_priv_key + 798 + 3, 129 },
|
||||
{ qcrypto_der_decode_int, checker_callback,
|
||||
test_rsa2048_priv_key + 930 + 3, 129 },
|
||||
{ qcrypto_der_decode_int, checker_callback,
|
||||
test_rsa2048_priv_key + 1062 + 3, 129 },
|
||||
},
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
size_t i;
|
||||
g_test_init(&argc, &argv, NULL);
|
||||
|
||||
for (i = 0; i < G_N_ELEMENTS(test_data); i++) {
|
||||
g_test_add_data_func(test_data[i].path, &test_data[i], test_ans1);
|
||||
}
|
||||
|
||||
return g_test_run();
|
||||
}
|
||||
Reference in New Issue
Block a user