crypto/block: introduce qcrypto_block_*crypt_helper functions

Introduce QCryptoBlock-based functions and use them where possible.
This is needed to implement thread-safe encrypt/decrypt operations.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Reviewed-by: Alberto Garcia <berto@igalia.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Vladimir Sementsov-Ogievskiy 2018-12-07 19:13:50 +03:00 committed by Daniel P. Berrangé
parent 0270417c87
commit 0f0d596cb1
4 changed files with 54 additions and 16 deletions

View File

@ -1409,10 +1409,9 @@ qcrypto_block_luks_decrypt(QCryptoBlock *block,
{ {
assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE)); assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE));
assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE)); assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE));
return qcrypto_block_cipher_decrypt_helper(block->cipher, return qcrypto_block_decrypt_helper(block,
block->niv, block->ivgen, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,
QCRYPTO_BLOCK_LUKS_SECTOR_SIZE, offset, buf, len, errp);
offset, buf, len, errp);
} }
@ -1425,10 +1424,9 @@ qcrypto_block_luks_encrypt(QCryptoBlock *block,
{ {
assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE)); assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE));
assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE)); assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE));
return qcrypto_block_cipher_encrypt_helper(block->cipher, return qcrypto_block_encrypt_helper(block,
block->niv, block->ivgen, QCRYPTO_BLOCK_LUKS_SECTOR_SIZE,
QCRYPTO_BLOCK_LUKS_SECTOR_SIZE, offset, buf, len, errp);
offset, buf, len, errp);
} }

View File

@ -152,10 +152,9 @@ qcrypto_block_qcow_decrypt(QCryptoBlock *block,
{ {
assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE)); assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE));
assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE)); assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE));
return qcrypto_block_cipher_decrypt_helper(block->cipher, return qcrypto_block_decrypt_helper(block,
block->niv, block->ivgen, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE,
QCRYPTO_BLOCK_QCOW_SECTOR_SIZE, offset, buf, len, errp);
offset, buf, len, errp);
} }
@ -168,10 +167,9 @@ qcrypto_block_qcow_encrypt(QCryptoBlock *block,
{ {
assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE)); assert(QEMU_IS_ALIGNED(offset, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE));
assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE)); assert(QEMU_IS_ALIGNED(len, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE));
return qcrypto_block_cipher_encrypt_helper(block->cipher, return qcrypto_block_encrypt_helper(block,
block->niv, block->ivgen, QCRYPTO_BLOCK_QCOW_SECTOR_SIZE,
QCRYPTO_BLOCK_QCOW_SECTOR_SIZE, offset, buf, len, errp);
offset, buf, len, errp);
} }

View File

@ -277,3 +277,31 @@ int qcrypto_block_cipher_encrypt_helper(QCryptoCipher *cipher,
offset, buf, len, offset, buf, len,
qcrypto_cipher_encrypt, errp); qcrypto_cipher_encrypt, errp);
} }
int qcrypto_block_decrypt_helper(QCryptoBlock *block,
int sectorsize,
uint64_t offset,
uint8_t *buf,
size_t len,
Error **errp)
{
return do_qcrypto_block_cipher_encdec(block->cipher, block->niv,
block->ivgen,
sectorsize, offset, buf, len,
qcrypto_cipher_decrypt, errp);
}
int qcrypto_block_encrypt_helper(QCryptoBlock *block,
int sectorsize,
uint64_t offset,
uint8_t *buf,
size_t len,
Error **errp)
{
return do_qcrypto_block_cipher_encdec(block->cipher, block->niv,
block->ivgen,
sectorsize, offset, buf, len,
qcrypto_cipher_encrypt, errp);
}

View File

@ -96,4 +96,18 @@ int qcrypto_block_cipher_encrypt_helper(QCryptoCipher *cipher,
size_t len, size_t len,
Error **errp); Error **errp);
int qcrypto_block_decrypt_helper(QCryptoBlock *block,
int sectorsize,
uint64_t offset,
uint8_t *buf,
size_t len,
Error **errp);
int qcrypto_block_encrypt_helper(QCryptoBlock *block,
int sectorsize,
uint64_t offset,
uint8_t *buf,
size_t len,
Error **errp);
#endif /* QCRYPTO_BLOCKPRIV_H */ #endif /* QCRYPTO_BLOCKPRIV_H */