diff options
author | Richard Henderson | 2020-08-28 19:05:08 +0200 |
---|---|---|
committer | Daniel P. Berrangé | 2020-09-10 12:02:23 +0200 |
commit | eba29771c006d6a689e946fa57334a2ce370c45c (patch) | |
tree | b2a2a6559e18f89dabbea68cc124875956b7a37d /crypto/cipher-nettle.c | |
parent | tests: fix output message formatting for crypto benchmarks (diff) | |
download | qemu-eba29771c006d6a689e946fa57334a2ce370c45c.tar.gz qemu-eba29771c006d6a689e946fa57334a2ce370c45c.tar.xz qemu-eba29771c006d6a689e946fa57334a2ce370c45c.zip |
crypto: Assume blocksize is a power of 2
The check in the encode/decode path using full division has a
noticeable amount of overhead. By asserting the blocksize is
a power of 2, we can reduce this check to a mask.
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Diffstat (limited to 'crypto/cipher-nettle.c')
-rw-r--r-- | crypto/cipher-nettle.c | 5 |
1 files changed, 3 insertions, 2 deletions
diff --git a/crypto/cipher-nettle.c b/crypto/cipher-nettle.c index 7e9a4cc199..0677fdfd33 100644 --- a/crypto/cipher-nettle.c +++ b/crypto/cipher-nettle.c @@ -576,6 +576,7 @@ static QCryptoCipherNettle *qcrypto_cipher_ctx_new(QCryptoCipherAlgorithm alg, QCryptoCipherAlgorithm_str(alg)); goto error; } + g_assert(is_power_of_2(ctx->blocksize)); if (mode == QCRYPTO_CIPHER_MODE_XTS && ctx->blocksize != XTS_BLOCK_SIZE) { @@ -613,7 +614,7 @@ qcrypto_nettle_cipher_encrypt(QCryptoCipher *cipher, { QCryptoCipherNettle *ctx = cipher->opaque; - if (len % ctx->blocksize) { + if (len & (ctx->blocksize - 1)) { error_setg(errp, "Length %zu must be a multiple of block size %zu", len, ctx->blocksize); return -1; @@ -666,7 +667,7 @@ qcrypto_nettle_cipher_decrypt(QCryptoCipher *cipher, { QCryptoCipherNettle *ctx = cipher->opaque; - if (len % ctx->blocksize) { + if (len & (ctx->blocksize - 1)) { error_setg(errp, "Length %zu must be a multiple of block size %zu", len, ctx->blocksize); return -1; |