Skip to content

Commit 3eedf5c

Browse files
rth7680berrange
authored andcommitted
crypto: Allocate QCryptoCipher with the subclass
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]>
1 parent 7b5dbfb commit 3eedf5c

File tree

8 files changed

+84
-77
lines changed

8 files changed

+84
-77
lines changed

crypto/afalgpriv.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define QCRYPTO_AFALGPRIV_H
1616

1717
#include <linux/if_alg.h>
18+
#include "crypto/cipher.h"
1819

1920
#define SALG_TYPE_LEN_MAX 14
2021
#define SALG_NAME_LEN_MAX 64
@@ -32,6 +33,8 @@
3233
typedef struct QCryptoAFAlg QCryptoAFAlg;
3334

3435
struct QCryptoAFAlg {
36+
QCryptoCipher base;
37+
3538
int tfmfd;
3639
int opfd;
3740
struct msghdr *msg;

crypto/cipher-afalg.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ qcrypto_afalg_cipher_format_name(QCryptoCipherAlgorithm alg,
5858
return name;
5959
}
6060

61-
QCryptoAFAlg *
61+
QCryptoCipher *
6262
qcrypto_afalg_cipher_ctx_new(QCryptoCipherAlgorithm alg,
6363
QCryptoCipherMode mode,
6464
const uint8_t *key,
@@ -109,17 +109,17 @@ qcrypto_afalg_cipher_ctx_new(QCryptoCipherAlgorithm alg,
109109
}
110110
afalg->cmsg = CMSG_FIRSTHDR(afalg->msg);
111111

112-
return afalg;
112+
return &afalg->base;
113113
}
114114

115115
static int
116116
qcrypto_afalg_cipher_setiv(QCryptoCipher *cipher,
117117
const uint8_t *iv,
118118
size_t niv, Error **errp)
119119
{
120+
QCryptoAFAlg *afalg = container_of(cipher, QCryptoAFAlg, base);
120121
struct af_alg_iv *alg_iv;
121122
size_t expect_niv;
122-
QCryptoAFAlg *afalg = cipher->opaque;
123123

124124
expect_niv = qcrypto_cipher_get_iv_len(cipher->alg, cipher->mode);
125125
if (niv != expect_niv) {
@@ -200,22 +200,26 @@ qcrypto_afalg_cipher_encrypt(QCryptoCipher *cipher,
200200
const void *in, void *out,
201201
size_t len, Error **errp)
202202
{
203-
return qcrypto_afalg_cipher_op(cipher->opaque, in, out,
204-
len, true, errp);
203+
QCryptoAFAlg *afalg = container_of(cipher, QCryptoAFAlg, base);
204+
205+
return qcrypto_afalg_cipher_op(afalg, in, out, len, true, errp);
205206
}
206207

207208
static int
208209
qcrypto_afalg_cipher_decrypt(QCryptoCipher *cipher,
209210
const void *in, void *out,
210211
size_t len, Error **errp)
211212
{
212-
return qcrypto_afalg_cipher_op(cipher->opaque, in, out,
213-
len, false, errp);
213+
QCryptoAFAlg *afalg = container_of(cipher, QCryptoAFAlg, base);
214+
215+
return qcrypto_afalg_cipher_op(afalg, in, out, len, false, errp);
214216
}
215217

216218
static void qcrypto_afalg_comm_ctx_free(QCryptoCipher *cipher)
217219
{
218-
qcrypto_afalg_comm_free(cipher->opaque);
220+
QCryptoAFAlg *afalg = container_of(cipher, QCryptoAFAlg, base);
221+
222+
qcrypto_afalg_comm_free(afalg);
219223
}
220224

221225
const struct QCryptoCipherDriver qcrypto_cipher_afalg_driver = {

crypto/cipher-builtin.c.inc

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ struct QCryptoCipherBuiltinDESRFB {
4141

4242
typedef struct QCryptoCipherBuiltin QCryptoCipherBuiltin;
4343
struct QCryptoCipherBuiltin {
44+
QCryptoCipher base;
45+
4446
union {
4547
QCryptoCipherBuiltinAES aes;
4648
QCryptoCipherBuiltinDESRFB desrfb;
@@ -65,10 +67,7 @@ struct QCryptoCipherBuiltin {
6567

6668
static void qcrypto_cipher_free_aes(QCryptoCipher *cipher)
6769
{
68-
QCryptoCipherBuiltin *ctxt = cipher->opaque;
69-
70-
g_free(ctxt);
71-
cipher->opaque = NULL;
70+
g_free(cipher);
7271
}
7372

7473

@@ -152,7 +151,8 @@ static int qcrypto_cipher_encrypt_aes(QCryptoCipher *cipher,
152151
size_t len,
153152
Error **errp)
154153
{
155-
QCryptoCipherBuiltin *ctxt = cipher->opaque;
154+
QCryptoCipherBuiltin *ctxt
155+
= container_of(cipher, QCryptoCipherBuiltin, base);
156156

157157
switch (cipher->mode) {
158158
case QCRYPTO_CIPHER_MODE_ECB:
@@ -186,7 +186,8 @@ static int qcrypto_cipher_decrypt_aes(QCryptoCipher *cipher,
186186
size_t len,
187187
Error **errp)
188188
{
189-
QCryptoCipherBuiltin *ctxt = cipher->opaque;
189+
QCryptoCipherBuiltin *ctxt
190+
= container_of(cipher, QCryptoCipherBuiltin, base);
190191

191192
switch (cipher->mode) {
192193
case QCRYPTO_CIPHER_MODE_ECB:
@@ -217,7 +218,9 @@ static int qcrypto_cipher_setiv_aes(QCryptoCipher *cipher,
217218
const uint8_t *iv, size_t niv,
218219
Error **errp)
219220
{
220-
QCryptoCipherBuiltin *ctxt = cipher->opaque;
221+
QCryptoCipherBuiltin *ctxt
222+
= container_of(cipher, QCryptoCipherBuiltin, base);
223+
221224
if (niv != AES_BLOCK_SIZE) {
222225
error_setg(errp, "IV must be %d bytes not %zu",
223226
AES_BLOCK_SIZE, niv);
@@ -232,7 +235,7 @@ static int qcrypto_cipher_setiv_aes(QCryptoCipher *cipher,
232235

233236

234237

235-
static QCryptoCipherBuiltin *
238+
static QCryptoCipher *
236239
qcrypto_cipher_init_aes(QCryptoCipherMode mode,
237240
const uint8_t *key, size_t nkey,
238241
Error **errp)
@@ -289,7 +292,7 @@ qcrypto_cipher_init_aes(QCryptoCipherMode mode,
289292
ctxt->encrypt = qcrypto_cipher_encrypt_aes;
290293
ctxt->decrypt = qcrypto_cipher_decrypt_aes;
291294

292-
return ctxt;
295+
return &ctxt->base;
293296

294297
error:
295298
g_free(ctxt);
@@ -299,11 +302,11 @@ qcrypto_cipher_init_aes(QCryptoCipherMode mode,
299302

300303
static void qcrypto_cipher_free_des_rfb(QCryptoCipher *cipher)
301304
{
302-
QCryptoCipherBuiltin *ctxt = cipher->opaque;
305+
QCryptoCipherBuiltin *ctxt
306+
= container_of(cipher, QCryptoCipherBuiltin, base);
303307

304308
g_free(ctxt->state.desrfb.key);
305309
g_free(ctxt);
306-
cipher->opaque = NULL;
307310
}
308311

309312

@@ -313,7 +316,8 @@ static int qcrypto_cipher_encrypt_des_rfb(QCryptoCipher *cipher,
313316
size_t len,
314317
Error **errp)
315318
{
316-
QCryptoCipherBuiltin *ctxt = cipher->opaque;
319+
QCryptoCipherBuiltin *ctxt
320+
= container_of(cipher, QCryptoCipherBuiltin, base);
317321
size_t i;
318322

319323
if (len % 8) {
@@ -338,7 +342,8 @@ static int qcrypto_cipher_decrypt_des_rfb(QCryptoCipher *cipher,
338342
size_t len,
339343
Error **errp)
340344
{
341-
QCryptoCipherBuiltin *ctxt = cipher->opaque;
345+
QCryptoCipherBuiltin *ctxt
346+
= container_of(cipher, QCryptoCipherBuiltin, base);
342347
size_t i;
343348

344349
if (len % 8) {
@@ -366,7 +371,7 @@ static int qcrypto_cipher_setiv_des_rfb(QCryptoCipher *cipher,
366371
}
367372

368373

369-
static QCryptoCipherBuiltin *
374+
static QCryptoCipher *
370375
qcrypto_cipher_init_des_rfb(QCryptoCipherMode mode,
371376
const uint8_t *key, size_t nkey,
372377
Error **errp)
@@ -391,7 +396,7 @@ qcrypto_cipher_init_des_rfb(QCryptoCipherMode mode,
391396
ctxt->encrypt = qcrypto_cipher_encrypt_des_rfb;
392397
ctxt->decrypt = qcrypto_cipher_decrypt_des_rfb;
393398

394-
return ctxt;
399+
return &ctxt->base;
395400
}
396401

397402

@@ -421,14 +426,12 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
421426
}
422427

423428

424-
static QCryptoCipherBuiltin *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
425-
QCryptoCipherMode mode,
426-
const uint8_t *key,
427-
size_t nkey,
428-
Error **errp)
429+
static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
430+
QCryptoCipherMode mode,
431+
const uint8_t *key,
432+
size_t nkey,
433+
Error **errp)
429434
{
430-
QCryptoCipherBuiltin *ctxt;
431-
432435
switch (mode) {
433436
case QCRYPTO_CIPHER_MODE_ECB:
434437
case QCRYPTO_CIPHER_MODE_CBC:
@@ -446,29 +449,25 @@ static QCryptoCipherBuiltin *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
446449

447450
switch (alg) {
448451
case QCRYPTO_CIPHER_ALG_DES_RFB:
449-
ctxt = qcrypto_cipher_init_des_rfb(mode, key, nkey, errp);
450-
break;
452+
return qcrypto_cipher_init_des_rfb(mode, key, nkey, errp);
451453
case QCRYPTO_CIPHER_ALG_AES_128:
452454
case QCRYPTO_CIPHER_ALG_AES_192:
453455
case QCRYPTO_CIPHER_ALG_AES_256:
454-
ctxt = qcrypto_cipher_init_aes(mode, key, nkey, errp);
455-
break;
456+
return qcrypto_cipher_init_aes(mode, key, nkey, errp);
456457
default:
457458
error_setg(errp,
458459
"Unsupported cipher algorithm %s",
459460
QCryptoCipherAlgorithm_str(alg));
460461
return NULL;
461462
}
462-
463-
return ctxt;
464463
}
465464

466465
static void
467466
qcrypto_builtin_cipher_ctx_free(QCryptoCipher *cipher)
468467
{
469-
QCryptoCipherBuiltin *ctxt;
468+
QCryptoCipherBuiltin *ctxt
469+
= container_of(cipher, QCryptoCipherBuiltin, base);
470470

471-
ctxt = cipher->opaque;
472471
ctxt->free(cipher);
473472
}
474473

@@ -480,7 +479,8 @@ qcrypto_builtin_cipher_encrypt(QCryptoCipher *cipher,
480479
size_t len,
481480
Error **errp)
482481
{
483-
QCryptoCipherBuiltin *ctxt = cipher->opaque;
482+
QCryptoCipherBuiltin *ctxt
483+
= container_of(cipher, QCryptoCipherBuiltin, base);
484484

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

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

521523
return ctxt->setiv(cipher, iv, niv, errp);
522524
}

crypto/cipher-gcrypt.c.inc

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
5858

5959
typedef struct QCryptoCipherGcrypt QCryptoCipherGcrypt;
6060
struct QCryptoCipherGcrypt {
61+
QCryptoCipher base;
6162
gcry_cipher_hd_t handle;
6263
size_t blocksize;
6364
#ifdef CONFIG_QEMU_PRIVATE_XTS
@@ -86,11 +87,11 @@ qcrypto_gcrypt_cipher_free_ctx(QCryptoCipherGcrypt *ctx,
8687
}
8788

8889

89-
static QCryptoCipherGcrypt *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
90-
QCryptoCipherMode mode,
91-
const uint8_t *key,
92-
size_t nkey,
93-
Error **errp)
90+
static QCryptoCipher *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
91+
QCryptoCipherMode mode,
92+
const uint8_t *key,
93+
size_t nkey,
94+
Error **errp)
9495
{
9596
QCryptoCipherGcrypt *ctx;
9697
gcry_error_t err;
@@ -257,7 +258,7 @@ static QCryptoCipherGcrypt *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
257258
}
258259
#endif
259260

260-
return ctx;
261+
return &ctx->base;
261262

262263
error:
263264
qcrypto_gcrypt_cipher_free_ctx(ctx, mode);
@@ -268,7 +269,9 @@ static QCryptoCipherGcrypt *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
268269
static void
269270
qcrypto_gcrypt_cipher_ctx_free(QCryptoCipher *cipher)
270271
{
271-
qcrypto_gcrypt_cipher_free_ctx(cipher->opaque, cipher->mode);
272+
QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base);
273+
274+
qcrypto_gcrypt_cipher_free_ctx(ctx, cipher->mode);
272275
}
273276

274277

@@ -301,7 +304,7 @@ qcrypto_gcrypt_cipher_encrypt(QCryptoCipher *cipher,
301304
size_t len,
302305
Error **errp)
303306
{
304-
QCryptoCipherGcrypt *ctx = cipher->opaque;
307+
QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base);
305308
gcry_error_t err;
306309

307310
if (len & (ctx->blocksize - 1)) {
@@ -340,7 +343,7 @@ qcrypto_gcrypt_cipher_decrypt(QCryptoCipher *cipher,
340343
size_t len,
341344
Error **errp)
342345
{
343-
QCryptoCipherGcrypt *ctx = cipher->opaque;
346+
QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base);
344347
gcry_error_t err;
345348

346349
if (len & (ctx->blocksize - 1)) {
@@ -376,7 +379,7 @@ qcrypto_gcrypt_cipher_setiv(QCryptoCipher *cipher,
376379
const uint8_t *iv, size_t niv,
377380
Error **errp)
378381
{
379-
QCryptoCipherGcrypt *ctx = cipher->opaque;
382+
QCryptoCipherGcrypt *ctx = container_of(cipher, QCryptoCipherGcrypt, base);
380383
gcry_error_t err;
381384

382385
if (niv != ctx->blocksize) {

0 commit comments

Comments
 (0)