Skip to content

Commit

Permalink
crypto: Allocate QCryptoCipher with the subclass
Browse files Browse the repository at this point in the history
Merge the allocation of "opaque" into the allocation of "cipher".
This is step one in reducing the indirection in these classes.

Signed-off-by: Richard Henderson <[email protected]>
Signed-off-by: Daniel P. Berrangé <[email protected]>
  • Loading branch information
rth7680 authored and berrange committed Sep 10, 2020
1 parent 7b5dbfb commit 3eedf5c
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 77 deletions.
3 changes: 3 additions & 0 deletions crypto/afalgpriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define QCRYPTO_AFALGPRIV_H

#include <linux/if_alg.h>
#include "crypto/cipher.h"

#define SALG_TYPE_LEN_MAX 14
#define SALG_NAME_LEN_MAX 64
Expand All @@ -32,6 +33,8 @@
typedef struct QCryptoAFAlg QCryptoAFAlg;

struct QCryptoAFAlg {
QCryptoCipher base;

int tfmfd;
int opfd;
struct msghdr *msg;
Expand Down
20 changes: 12 additions & 8 deletions crypto/cipher-afalg.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ qcrypto_afalg_cipher_format_name(QCryptoCipherAlgorithm alg,
return name;
}

QCryptoAFAlg *
QCryptoCipher *
qcrypto_afalg_cipher_ctx_new(QCryptoCipherAlgorithm alg,
QCryptoCipherMode mode,
const uint8_t *key,
Expand Down Expand Up @@ -109,17 +109,17 @@ qcrypto_afalg_cipher_ctx_new(QCryptoCipherAlgorithm alg,
}
afalg->cmsg = CMSG_FIRSTHDR(afalg->msg);

return afalg;
return &afalg->base;
}

static int
qcrypto_afalg_cipher_setiv(QCryptoCipher *cipher,
const uint8_t *iv,
size_t niv, Error **errp)
{
QCryptoAFAlg *afalg = container_of(cipher, QCryptoAFAlg, base);
struct af_alg_iv *alg_iv;
size_t expect_niv;
QCryptoAFAlg *afalg = cipher->opaque;

expect_niv = qcrypto_cipher_get_iv_len(cipher->alg, cipher->mode);
if (niv != expect_niv) {
Expand Down Expand Up @@ -200,22 +200,26 @@ qcrypto_afalg_cipher_encrypt(QCryptoCipher *cipher,
const void *in, void *out,
size_t len, Error **errp)
{
return qcrypto_afalg_cipher_op(cipher->opaque, in, out,
len, true, errp);
QCryptoAFAlg *afalg = container_of(cipher, QCryptoAFAlg, base);

return qcrypto_afalg_cipher_op(afalg, in, out, len, true, errp);
}

static int
qcrypto_afalg_cipher_decrypt(QCryptoCipher *cipher,
const void *in, void *out,
size_t len, Error **errp)
{
return qcrypto_afalg_cipher_op(cipher->opaque, in, out,
len, false, errp);
QCryptoAFAlg *afalg = container_of(cipher, QCryptoAFAlg, base);

return qcrypto_afalg_cipher_op(afalg, in, out, len, false, errp);
}

static void qcrypto_afalg_comm_ctx_free(QCryptoCipher *cipher)
{
qcrypto_afalg_comm_free(cipher->opaque);
QCryptoAFAlg *afalg = container_of(cipher, QCryptoAFAlg, base);

qcrypto_afalg_comm_free(afalg);
}

const struct QCryptoCipherDriver qcrypto_cipher_afalg_driver = {
Expand Down
68 changes: 35 additions & 33 deletions crypto/cipher-builtin.c.inc
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ struct QCryptoCipherBuiltinDESRFB {

typedef struct QCryptoCipherBuiltin QCryptoCipherBuiltin;
struct QCryptoCipherBuiltin {
QCryptoCipher base;

union {
QCryptoCipherBuiltinAES aes;
QCryptoCipherBuiltinDESRFB desrfb;
Expand All @@ -65,10 +67,7 @@ struct QCryptoCipherBuiltin {

static void qcrypto_cipher_free_aes(QCryptoCipher *cipher)
{
QCryptoCipherBuiltin *ctxt = cipher->opaque;

g_free(ctxt);
cipher->opaque = NULL;
g_free(cipher);
}


Expand Down Expand Up @@ -152,7 +151,8 @@ static int qcrypto_cipher_encrypt_aes(QCryptoCipher *cipher,
size_t len,
Error **errp)
{
QCryptoCipherBuiltin *ctxt = cipher->opaque;
QCryptoCipherBuiltin *ctxt
= container_of(cipher, QCryptoCipherBuiltin, base);

switch (cipher->mode) {
case QCRYPTO_CIPHER_MODE_ECB:
Expand Down Expand Up @@ -186,7 +186,8 @@ static int qcrypto_cipher_decrypt_aes(QCryptoCipher *cipher,
size_t len,
Error **errp)
{
QCryptoCipherBuiltin *ctxt = cipher->opaque;
QCryptoCipherBuiltin *ctxt
= container_of(cipher, QCryptoCipherBuiltin, base);

switch (cipher->mode) {
case QCRYPTO_CIPHER_MODE_ECB:
Expand Down Expand Up @@ -217,7 +218,9 @@ static int qcrypto_cipher_setiv_aes(QCryptoCipher *cipher,
const uint8_t *iv, size_t niv,
Error **errp)
{
QCryptoCipherBuiltin *ctxt = cipher->opaque;
QCryptoCipherBuiltin *ctxt
= container_of(cipher, QCryptoCipherBuiltin, base);

if (niv != AES_BLOCK_SIZE) {
error_setg(errp, "IV must be %d bytes not %zu",
AES_BLOCK_SIZE, niv);
Expand All @@ -232,7 +235,7 @@ static int qcrypto_cipher_setiv_aes(QCryptoCipher *cipher,



static QCryptoCipherBuiltin *
static QCryptoCipher *
qcrypto_cipher_init_aes(QCryptoCipherMode mode,
const uint8_t *key, size_t nkey,
Error **errp)
Expand Down Expand Up @@ -289,7 +292,7 @@ qcrypto_cipher_init_aes(QCryptoCipherMode mode,
ctxt->encrypt = qcrypto_cipher_encrypt_aes;
ctxt->decrypt = qcrypto_cipher_decrypt_aes;

return ctxt;
return &ctxt->base;

error:
g_free(ctxt);
Expand All @@ -299,11 +302,11 @@ qcrypto_cipher_init_aes(QCryptoCipherMode mode,

static void qcrypto_cipher_free_des_rfb(QCryptoCipher *cipher)
{
QCryptoCipherBuiltin *ctxt = cipher->opaque;
QCryptoCipherBuiltin *ctxt
= container_of(cipher, QCryptoCipherBuiltin, base);

g_free(ctxt->state.desrfb.key);
g_free(ctxt);
cipher->opaque = NULL;
}


Expand All @@ -313,7 +316,8 @@ static int qcrypto_cipher_encrypt_des_rfb(QCryptoCipher *cipher,
size_t len,
Error **errp)
{
QCryptoCipherBuiltin *ctxt = cipher->opaque;
QCryptoCipherBuiltin *ctxt
= container_of(cipher, QCryptoCipherBuiltin, base);
size_t i;

if (len % 8) {
Expand All @@ -338,7 +342,8 @@ static int qcrypto_cipher_decrypt_des_rfb(QCryptoCipher *cipher,
size_t len,
Error **errp)
{
QCryptoCipherBuiltin *ctxt = cipher->opaque;
QCryptoCipherBuiltin *ctxt
= container_of(cipher, QCryptoCipherBuiltin, base);
size_t i;

if (len % 8) {
Expand Down Expand Up @@ -366,7 +371,7 @@ static int qcrypto_cipher_setiv_des_rfb(QCryptoCipher *cipher,
}


static QCryptoCipherBuiltin *
static QCryptoCipher *
qcrypto_cipher_init_des_rfb(QCryptoCipherMode mode,
const uint8_t *key, size_t nkey,
Error **errp)
Expand All @@ -391,7 +396,7 @@ qcrypto_cipher_init_des_rfb(QCryptoCipherMode mode,
ctxt->encrypt = qcrypto_cipher_encrypt_des_rfb;
ctxt->decrypt = qcrypto_cipher_decrypt_des_rfb;

return ctxt;
return &ctxt->base;
}


Expand Down Expand Up @@ -421,14 +426,12 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
}


static QCryptoCipherBuiltin *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
QCryptoCipherMode mode,
const uint8_t *key,
size_t nkey,
Error **errp)
static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
QCryptoCipherMode mode,
const uint8_t *key,
size_t nkey,
Error **errp)
{
QCryptoCipherBuiltin *ctxt;

switch (mode) {
case QCRYPTO_CIPHER_MODE_ECB:
case QCRYPTO_CIPHER_MODE_CBC:
Expand All @@ -446,29 +449,25 @@ static QCryptoCipherBuiltin *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,

switch (alg) {
case QCRYPTO_CIPHER_ALG_DES_RFB:
ctxt = qcrypto_cipher_init_des_rfb(mode, key, nkey, errp);
break;
return qcrypto_cipher_init_des_rfb(mode, key, nkey, errp);
case QCRYPTO_CIPHER_ALG_AES_128:
case QCRYPTO_CIPHER_ALG_AES_192:
case QCRYPTO_CIPHER_ALG_AES_256:
ctxt = qcrypto_cipher_init_aes(mode, key, nkey, errp);
break;
return qcrypto_cipher_init_aes(mode, key, nkey, errp);
default:
error_setg(errp,
"Unsupported cipher algorithm %s",
QCryptoCipherAlgorithm_str(alg));
return NULL;
}

return ctxt;
}

static void
qcrypto_builtin_cipher_ctx_free(QCryptoCipher *cipher)
{
QCryptoCipherBuiltin *ctxt;
QCryptoCipherBuiltin *ctxt
= container_of(cipher, QCryptoCipherBuiltin, base);

ctxt = cipher->opaque;
ctxt->free(cipher);
}

Expand All @@ -480,7 +479,8 @@ qcrypto_builtin_cipher_encrypt(QCryptoCipher *cipher,
size_t len,
Error **errp)
{
QCryptoCipherBuiltin *ctxt = cipher->opaque;
QCryptoCipherBuiltin *ctxt
= container_of(cipher, QCryptoCipherBuiltin, base);

if (len & (ctxt->blocksize - 1)) {
error_setg(errp, "Length %zu must be a multiple of block size %zu",
Expand All @@ -499,7 +499,8 @@ qcrypto_builtin_cipher_decrypt(QCryptoCipher *cipher,
size_t len,
Error **errp)
{
QCryptoCipherBuiltin *ctxt = cipher->opaque;
QCryptoCipherBuiltin *ctxt
= container_of(cipher, QCryptoCipherBuiltin, base);

if (len & (ctxt->blocksize - 1)) {
error_setg(errp, "Length %zu must be a multiple of block size %zu",
Expand All @@ -516,7 +517,8 @@ qcrypto_builtin_cipher_setiv(QCryptoCipher *cipher,
const uint8_t *iv, size_t niv,
Error **errp)
{
QCryptoCipherBuiltin *ctxt = cipher->opaque;
QCryptoCipherBuiltin *ctxt
= container_of(cipher, QCryptoCipherBuiltin, base);

return ctxt->setiv(cipher, iv, niv, errp);
}
Expand Down
23 changes: 13 additions & 10 deletions crypto/cipher-gcrypt.c.inc
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,

typedef struct QCryptoCipherGcrypt QCryptoCipherGcrypt;
struct QCryptoCipherGcrypt {
QCryptoCipher base;
gcry_cipher_hd_t handle;
size_t blocksize;
#ifdef CONFIG_QEMU_PRIVATE_XTS
Expand Down Expand Up @@ -86,11 +87,11 @@ qcrypto_gcrypt_cipher_free_ctx(QCryptoCipherGcrypt *ctx,
}


static QCryptoCipherGcrypt *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
QCryptoCipherMode mode,
const uint8_t *key,
size_t nkey,
Error **errp)
static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
QCryptoCipherMode mode,
const uint8_t *key,
size_t nkey,
Error **errp)
{
QCryptoCipherGcrypt *ctx;
gcry_error_t err;
Expand Down Expand Up @@ -257,7 +258,7 @@ static QCryptoCipherGcrypt *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
}
#endif

return ctx;
return &ctx->base;

error:
qcrypto_gcrypt_cipher_free_ctx(ctx, mode);
Expand All @@ -268,7 +269,9 @@ static QCryptoCipherGcrypt *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
static void
qcrypto_gcrypt_cipher_ctx_free(QCryptoCipher *cipher)
{
qcrypto_gcrypt_cipher_free_ctx(cipher->opaque, cipher->mode);
QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base);

qcrypto_gcrypt_cipher_free_ctx(ctx, cipher->mode);
}


Expand Down Expand Up @@ -301,7 +304,7 @@ qcrypto_gcrypt_cipher_encrypt(QCryptoCipher *cipher,
size_t len,
Error **errp)
{
QCryptoCipherGcrypt *ctx = cipher->opaque;
QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base);
gcry_error_t err;

if (len & (ctx->blocksize - 1)) {
Expand Down Expand Up @@ -340,7 +343,7 @@ qcrypto_gcrypt_cipher_decrypt(QCryptoCipher *cipher,
size_t len,
Error **errp)
{
QCryptoCipherGcrypt *ctx = cipher->opaque;
QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base);
gcry_error_t err;

if (len & (ctx->blocksize - 1)) {
Expand Down Expand Up @@ -376,7 +379,7 @@ qcrypto_gcrypt_cipher_setiv(QCryptoCipher *cipher,
const uint8_t *iv, size_t niv,
Error **errp)
{
QCryptoCipherGcrypt *ctx = cipher->opaque;
QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base);
gcry_error_t err;

if (niv != ctx->blocksize) {
Expand Down
Loading

0 comments on commit 3eedf5c

Please sign in to comment.