@@ -41,6 +41,8 @@ struct QCryptoCipherBuiltinDESRFB {
41
41
42
42
typedef struct QCryptoCipherBuiltin QCryptoCipherBuiltin;
43
43
struct QCryptoCipherBuiltin {
44
+ QCryptoCipher base;
45
+
44
46
union {
45
47
QCryptoCipherBuiltinAES aes;
46
48
QCryptoCipherBuiltinDESRFB desrfb;
@@ -65,10 +67,7 @@ struct QCryptoCipherBuiltin {
65
67
66
68
static void qcrypto_cipher_free_aes (QCryptoCipher *cipher)
67
69
{
68
- QCryptoCipherBuiltin *ctxt = cipher->opaque ;
69
-
70
- g_free (ctxt);
71
- cipher->opaque = NULL ;
70
+ g_free (cipher);
72
71
}
73
72
74
73
@@ -152,7 +151,8 @@ static int qcrypto_cipher_encrypt_aes(QCryptoCipher *cipher,
152
151
size_t len,
153
152
Error **errp)
154
153
{
155
- QCryptoCipherBuiltin *ctxt = cipher->opaque ;
154
+ QCryptoCipherBuiltin *ctxt
155
+ = container_of (cipher, QCryptoCipherBuiltin, base);
156
156
157
157
switch (cipher->mode ) {
158
158
case QCRYPTO_CIPHER_MODE_ECB:
@@ -186,7 +186,8 @@ static int qcrypto_cipher_decrypt_aes(QCryptoCipher *cipher,
186
186
size_t len,
187
187
Error **errp)
188
188
{
189
- QCryptoCipherBuiltin *ctxt = cipher->opaque ;
189
+ QCryptoCipherBuiltin *ctxt
190
+ = container_of (cipher, QCryptoCipherBuiltin, base);
190
191
191
192
switch (cipher->mode ) {
192
193
case QCRYPTO_CIPHER_MODE_ECB:
@@ -217,7 +218,9 @@ static int qcrypto_cipher_setiv_aes(QCryptoCipher *cipher,
217
218
const uint8_t *iv, size_t niv,
218
219
Error **errp)
219
220
{
220
- QCryptoCipherBuiltin *ctxt = cipher->opaque ;
221
+ QCryptoCipherBuiltin *ctxt
222
+ = container_of (cipher, QCryptoCipherBuiltin, base);
223
+
221
224
if (niv != AES_BLOCK_SIZE) {
222
225
error_setg (errp, " IV must be %d bytes not %zu" ,
223
226
AES_BLOCK_SIZE, niv);
@@ -232,7 +235,7 @@ static int qcrypto_cipher_setiv_aes(QCryptoCipher *cipher,
232
235
233
236
234
237
235
- static QCryptoCipherBuiltin *
238
+ static QCryptoCipher *
236
239
qcrypto_cipher_init_aes (QCryptoCipherMode mode,
237
240
const uint8_t *key, size_t nkey,
238
241
Error **errp)
@@ -289,7 +292,7 @@ qcrypto_cipher_init_aes(QCryptoCipherMode mode,
289
292
ctxt->encrypt = qcrypto_cipher_encrypt_aes;
290
293
ctxt->decrypt = qcrypto_cipher_decrypt_aes;
291
294
292
- return ctxt;
295
+ return & ctxt-> base ;
293
296
294
297
error:
295
298
g_free (ctxt);
@@ -299,11 +302,11 @@ qcrypto_cipher_init_aes(QCryptoCipherMode mode,
299
302
300
303
static void qcrypto_cipher_free_des_rfb (QCryptoCipher *cipher)
301
304
{
302
- QCryptoCipherBuiltin *ctxt = cipher->opaque ;
305
+ QCryptoCipherBuiltin *ctxt
306
+ = container_of (cipher, QCryptoCipherBuiltin, base);
303
307
304
308
g_free (ctxt->state .desrfb .key );
305
309
g_free (ctxt);
306
- cipher->opaque = NULL ;
307
310
}
308
311
309
312
@@ -313,7 +316,8 @@ static int qcrypto_cipher_encrypt_des_rfb(QCryptoCipher *cipher,
313
316
size_t len,
314
317
Error **errp)
315
318
{
316
- QCryptoCipherBuiltin *ctxt = cipher->opaque ;
319
+ QCryptoCipherBuiltin *ctxt
320
+ = container_of (cipher, QCryptoCipherBuiltin, base);
317
321
size_t i;
318
322
319
323
if (len % 8 ) {
@@ -338,7 +342,8 @@ static int qcrypto_cipher_decrypt_des_rfb(QCryptoCipher *cipher,
338
342
size_t len,
339
343
Error **errp)
340
344
{
341
- QCryptoCipherBuiltin *ctxt = cipher->opaque ;
345
+ QCryptoCipherBuiltin *ctxt
346
+ = container_of (cipher, QCryptoCipherBuiltin, base);
342
347
size_t i;
343
348
344
349
if (len % 8 ) {
@@ -366,7 +371,7 @@ static int qcrypto_cipher_setiv_des_rfb(QCryptoCipher *cipher,
366
371
}
367
372
368
373
369
- static QCryptoCipherBuiltin *
374
+ static QCryptoCipher *
370
375
qcrypto_cipher_init_des_rfb (QCryptoCipherMode mode,
371
376
const uint8_t *key, size_t nkey,
372
377
Error **errp)
@@ -391,7 +396,7 @@ qcrypto_cipher_init_des_rfb(QCryptoCipherMode mode,
391
396
ctxt->encrypt = qcrypto_cipher_encrypt_des_rfb;
392
397
ctxt->decrypt = qcrypto_cipher_decrypt_des_rfb;
393
398
394
- return ctxt;
399
+ return & ctxt-> base ;
395
400
}
396
401
397
402
@@ -421,14 +426,12 @@ bool qcrypto_cipher_supports(QCryptoCipherAlgorithm alg,
421
426
}
422
427
423
428
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)
429
434
{
430
- QCryptoCipherBuiltin *ctxt;
431
-
432
435
switch (mode) {
433
436
case QCRYPTO_CIPHER_MODE_ECB:
434
437
case QCRYPTO_CIPHER_MODE_CBC:
@@ -446,29 +449,25 @@ static QCryptoCipherBuiltin *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg,
446
449
447
450
switch (alg) {
448
451
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);
451
453
case QCRYPTO_CIPHER_ALG_AES_128:
452
454
case QCRYPTO_CIPHER_ALG_AES_192:
453
455
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);
456
457
default :
457
458
error_setg (errp,
458
459
" Unsupported cipher algorithm %s" ,
459
460
QCryptoCipherAlgorithm_str (alg));
460
461
return NULL ;
461
462
}
462
-
463
- return ctxt;
464
463
}
465
464
466
465
static void
467
466
qcrypto_builtin_cipher_ctx_free (QCryptoCipher *cipher)
468
467
{
469
- QCryptoCipherBuiltin *ctxt;
468
+ QCryptoCipherBuiltin *ctxt
469
+ = container_of (cipher, QCryptoCipherBuiltin, base);
470
470
471
- ctxt = cipher->opaque ;
472
471
ctxt->free (cipher);
473
472
}
474
473
@@ -480,7 +479,8 @@ qcrypto_builtin_cipher_encrypt(QCryptoCipher *cipher,
480
479
size_t len,
481
480
Error **errp)
482
481
{
483
- QCryptoCipherBuiltin *ctxt = cipher->opaque ;
482
+ QCryptoCipherBuiltin *ctxt
483
+ = container_of (cipher, QCryptoCipherBuiltin, base);
484
484
485
485
if (len & (ctxt->blocksize - 1 )) {
486
486
error_setg (errp, " Length %zu must be a multiple of block size %zu" ,
@@ -499,7 +499,8 @@ qcrypto_builtin_cipher_decrypt(QCryptoCipher *cipher,
499
499
size_t len,
500
500
Error **errp)
501
501
{
502
- QCryptoCipherBuiltin *ctxt = cipher->opaque ;
502
+ QCryptoCipherBuiltin *ctxt
503
+ = container_of (cipher, QCryptoCipherBuiltin, base);
503
504
504
505
if (len & (ctxt->blocksize - 1 )) {
505
506
error_setg (errp, " Length %zu must be a multiple of block size %zu" ,
@@ -516,7 +517,8 @@ qcrypto_builtin_cipher_setiv(QCryptoCipher *cipher,
516
517
const uint8_t *iv, size_t niv,
517
518
Error **errp)
518
519
{
519
- QCryptoCipherBuiltin *ctxt = cipher->opaque ;
520
+ QCryptoCipherBuiltin *ctxt
521
+ = container_of (cipher, QCryptoCipherBuiltin, base);
520
522
521
523
return ctxt->setiv (cipher, iv, niv, errp);
522
524
}
0 commit comments