|
|
383d26 |
From 8c411fd4449466ba46b272d60b739015a6b47e19 Mon Sep 17 00:00:00 2001
|
|
|
383d26 |
From: "Daniel P. Berrange" <berrange@redhat.com>
|
|
|
383d26 |
Date: Wed, 24 Apr 2019 10:30:26 +0200
|
|
|
383d26 |
Subject: [PATCH 07/12] crypto: convert xts_tweak_encdec to use xts_uint128
|
|
|
383d26 |
type
|
|
|
383d26 |
MIME-Version: 1.0
|
|
|
383d26 |
Content-Type: text/plain; charset=UTF-8
|
|
|
383d26 |
Content-Transfer-Encoding: 8bit
|
|
|
383d26 |
|
|
|
383d26 |
RH-Author: Daniel P. Berrange <berrange@redhat.com>
|
|
|
383d26 |
Message-id: <20190424103030.2925-6-berrange@redhat.com>
|
|
|
383d26 |
Patchwork-id: 85890
|
|
|
383d26 |
O-Subject: [RHEL-7.7 qemu-kvm-rhev PATCH 5/9] crypto: convert xts_tweak_encdec to use xts_uint128 type
|
|
|
383d26 |
Bugzilla: 1666336
|
|
|
383d26 |
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
|
|
|
383d26 |
RH-Acked-by: John Snow <jsnow@redhat.com>
|
|
|
383d26 |
RH-Acked-by: Eric Blake <eblake@redhat.com>
|
|
|
383d26 |
|
|
|
383d26 |
Using 64-bit arithmetic increases the performance for xts-aes-128
|
|
|
383d26 |
when built with gcrypt:
|
|
|
383d26 |
|
|
|
383d26 |
Encrypt: 272 MB/s -> 355 MB/s
|
|
|
383d26 |
Decrypt: 275 MB/s -> 362 MB/s
|
|
|
383d26 |
|
|
|
383d26 |
Reviewed-by: Alberto Garcia <berto@igalia.com>
|
|
|
383d26 |
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
|
|
|
383d26 |
(cherry picked from commit db217c69f0849add67cfa2cd6601c329398be12c)
|
|
|
383d26 |
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
|
|
|
383d26 |
---
|
|
|
383d26 |
crypto/xts.c | 84 +++++++++++++++++++++++++++++++++++++++++-------------------
|
|
|
383d26 |
1 file changed, 58 insertions(+), 26 deletions(-)
|
|
|
383d26 |
|
|
|
383d26 |
diff --git a/crypto/xts.c b/crypto/xts.c
|
|
|
383d26 |
index bee23f8..0ad231f 100644
|
|
|
383d26 |
--- a/crypto/xts.c
|
|
|
383d26 |
+++ b/crypto/xts.c
|
|
|
383d26 |
@@ -31,6 +31,13 @@ typedef union {
|
|
|
383d26 |
uint64_t u[2];
|
|
|
383d26 |
} xts_uint128;
|
|
|
383d26 |
|
|
|
383d26 |
+static inline void xts_uint128_xor(xts_uint128 *D,
|
|
|
383d26 |
+ const xts_uint128 *S1,
|
|
|
383d26 |
+ const xts_uint128 *S2)
|
|
|
383d26 |
+{
|
|
|
383d26 |
+ D->u[0] = S1->u[0] ^ S2->u[0];
|
|
|
383d26 |
+ D->u[1] = S1->u[1] ^ S2->u[1];
|
|
|
383d26 |
+}
|
|
|
383d26 |
|
|
|
383d26 |
static void xts_mult_x(uint8_t *I)
|
|
|
383d26 |
{
|
|
|
383d26 |
@@ -60,25 +67,19 @@ static void xts_mult_x(uint8_t *I)
|
|
|
383d26 |
*/
|
|
|
383d26 |
static void xts_tweak_encdec(const void *ctx,
|
|
|
383d26 |
xts_cipher_func *func,
|
|
|
383d26 |
- const uint8_t *src,
|
|
|
383d26 |
- uint8_t *dst,
|
|
|
383d26 |
- uint8_t *iv)
|
|
|
383d26 |
+ const xts_uint128 *src,
|
|
|
383d26 |
+ xts_uint128 *dst,
|
|
|
383d26 |
+ xts_uint128 *iv)
|
|
|
383d26 |
{
|
|
|
383d26 |
- unsigned long x;
|
|
|
383d26 |
-
|
|
|
383d26 |
/* tweak encrypt block i */
|
|
|
383d26 |
- for (x = 0; x < XTS_BLOCK_SIZE; x++) {
|
|
|
383d26 |
- dst[x] = src[x] ^ iv[x];
|
|
|
383d26 |
- }
|
|
|
383d26 |
+ xts_uint128_xor(dst, src, iv);
|
|
|
383d26 |
|
|
|
383d26 |
- func(ctx, XTS_BLOCK_SIZE, dst, dst);
|
|
|
383d26 |
+ func(ctx, XTS_BLOCK_SIZE, dst->b, dst->b);
|
|
|
383d26 |
|
|
|
383d26 |
- for (x = 0; x < XTS_BLOCK_SIZE; x++) {
|
|
|
383d26 |
- dst[x] = dst[x] ^ iv[x];
|
|
|
383d26 |
- }
|
|
|
383d26 |
+ xts_uint128_xor(dst, dst, iv);
|
|
|
383d26 |
|
|
|
383d26 |
/* LFSR the tweak */
|
|
|
383d26 |
- xts_mult_x(iv);
|
|
|
383d26 |
+ xts_mult_x(iv->b);
|
|
|
383d26 |
}
|
|
|
383d26 |
|
|
|
383d26 |
|
|
|
383d26 |
@@ -110,20 +111,34 @@ void xts_decrypt(const void *datactx,
|
|
|
383d26 |
/* encrypt the iv */
|
|
|
383d26 |
encfunc(tweakctx, XTS_BLOCK_SIZE, T.b, iv);
|
|
|
383d26 |
|
|
|
383d26 |
- for (i = 0; i < lim; i++) {
|
|
|
383d26 |
- xts_tweak_encdec(datactx, decfunc, src, dst, T.b);
|
|
|
383d26 |
-
|
|
|
383d26 |
- src += XTS_BLOCK_SIZE;
|
|
|
383d26 |
- dst += XTS_BLOCK_SIZE;
|
|
|
383d26 |
+ if (QEMU_PTR_IS_ALIGNED(src, sizeof(uint64_t)) &&
|
|
|
383d26 |
+ QEMU_PTR_IS_ALIGNED(dst, sizeof(uint64_t))) {
|
|
|
383d26 |
+ xts_uint128 *S = (xts_uint128 *)src;
|
|
|
383d26 |
+ xts_uint128 *D = (xts_uint128 *)dst;
|
|
|
383d26 |
+ for (i = 0; i < lim; i++, S++, D++) {
|
|
|
383d26 |
+ xts_tweak_encdec(datactx, decfunc, S, D, &T);
|
|
|
383d26 |
+ }
|
|
|
383d26 |
+ } else {
|
|
|
383d26 |
+ xts_uint128 D;
|
|
|
383d26 |
+
|
|
|
383d26 |
+ for (i = 0; i < lim; i++) {
|
|
|
383d26 |
+ memcpy(&D, src, XTS_BLOCK_SIZE);
|
|
|
383d26 |
+ xts_tweak_encdec(datactx, decfunc, &D, &D, &T);
|
|
|
383d26 |
+ memcpy(dst, &D, XTS_BLOCK_SIZE);
|
|
|
383d26 |
+ src += XTS_BLOCK_SIZE;
|
|
|
383d26 |
+ dst += XTS_BLOCK_SIZE;
|
|
|
383d26 |
+ }
|
|
|
383d26 |
}
|
|
|
383d26 |
|
|
|
383d26 |
/* if length is not a multiple of XTS_BLOCK_SIZE then */
|
|
|
383d26 |
if (mo > 0) {
|
|
|
383d26 |
+ xts_uint128 S, D;
|
|
|
383d26 |
memcpy(&CC, &T, XTS_BLOCK_SIZE);
|
|
|
383d26 |
xts_mult_x(CC.b);
|
|
|
383d26 |
|
|
|
383d26 |
/* PP = tweak decrypt block m-1 */
|
|
|
383d26 |
- xts_tweak_encdec(datactx, decfunc, src, PP.b, CC.b);
|
|
|
383d26 |
+ memcpy(&S, src, XTS_BLOCK_SIZE);
|
|
|
383d26 |
+ xts_tweak_encdec(datactx, decfunc, &S, &PP, &CC;;
|
|
|
383d26 |
|
|
|
383d26 |
/* Pm = first length % XTS_BLOCK_SIZE bytes of PP */
|
|
|
383d26 |
for (i = 0; i < mo; i++) {
|
|
|
383d26 |
@@ -135,7 +150,8 @@ void xts_decrypt(const void *datactx,
|
|
|
383d26 |
}
|
|
|
383d26 |
|
|
|
383d26 |
/* Pm-1 = Tweak uncrypt CC */
|
|
|
383d26 |
- xts_tweak_encdec(datactx, decfunc, CC.b, dst, T.b);
|
|
|
383d26 |
+ xts_tweak_encdec(datactx, decfunc, &CC, &D, &T);
|
|
|
383d26 |
+ memcpy(dst, &D, XTS_BLOCK_SIZE);
|
|
|
383d26 |
}
|
|
|
383d26 |
|
|
|
383d26 |
/* Decrypt the iv back */
|
|
|
383d26 |
@@ -171,17 +187,32 @@ void xts_encrypt(const void *datactx,
|
|
|
383d26 |
/* encrypt the iv */
|
|
|
383d26 |
encfunc(tweakctx, XTS_BLOCK_SIZE, T.b, iv);
|
|
|
383d26 |
|
|
|
383d26 |
- for (i = 0; i < lim; i++) {
|
|
|
383d26 |
- xts_tweak_encdec(datactx, encfunc, src, dst, T.b);
|
|
|
383d26 |
+ if (QEMU_PTR_IS_ALIGNED(src, sizeof(uint64_t)) &&
|
|
|
383d26 |
+ QEMU_PTR_IS_ALIGNED(dst, sizeof(uint64_t))) {
|
|
|
383d26 |
+ xts_uint128 *S = (xts_uint128 *)src;
|
|
|
383d26 |
+ xts_uint128 *D = (xts_uint128 *)dst;
|
|
|
383d26 |
+ for (i = 0; i < lim; i++, S++, D++) {
|
|
|
383d26 |
+ xts_tweak_encdec(datactx, encfunc, S, D, &T);
|
|
|
383d26 |
+ }
|
|
|
383d26 |
+ } else {
|
|
|
383d26 |
+ xts_uint128 D;
|
|
|
383d26 |
+
|
|
|
383d26 |
+ for (i = 0; i < lim; i++) {
|
|
|
383d26 |
+ memcpy(&D, src, XTS_BLOCK_SIZE);
|
|
|
383d26 |
+ xts_tweak_encdec(datactx, encfunc, &D, &D, &T);
|
|
|
383d26 |
+ memcpy(dst, &D, XTS_BLOCK_SIZE);
|
|
|
383d26 |
|
|
|
383d26 |
- dst += XTS_BLOCK_SIZE;
|
|
|
383d26 |
- src += XTS_BLOCK_SIZE;
|
|
|
383d26 |
+ dst += XTS_BLOCK_SIZE;
|
|
|
383d26 |
+ src += XTS_BLOCK_SIZE;
|
|
|
383d26 |
+ }
|
|
|
383d26 |
}
|
|
|
383d26 |
|
|
|
383d26 |
/* if length is not a multiple of XTS_BLOCK_SIZE then */
|
|
|
383d26 |
if (mo > 0) {
|
|
|
383d26 |
+ xts_uint128 S, D;
|
|
|
383d26 |
/* CC = tweak encrypt block m-1 */
|
|
|
383d26 |
- xts_tweak_encdec(datactx, encfunc, src, CC.b, T.b);
|
|
|
383d26 |
+ memcpy(&S, src, XTS_BLOCK_SIZE);
|
|
|
383d26 |
+ xts_tweak_encdec(datactx, encfunc, &S, &CC, &T);
|
|
|
383d26 |
|
|
|
383d26 |
/* Cm = first length % XTS_BLOCK_SIZE bytes of CC */
|
|
|
383d26 |
for (i = 0; i < mo; i++) {
|
|
|
383d26 |
@@ -194,7 +225,8 @@ void xts_encrypt(const void *datactx,
|
|
|
383d26 |
}
|
|
|
383d26 |
|
|
|
383d26 |
/* Cm-1 = Tweak encrypt PP */
|
|
|
383d26 |
- xts_tweak_encdec(datactx, encfunc, PP.b, dst, T.b);
|
|
|
383d26 |
+ xts_tweak_encdec(datactx, encfunc, &PP, &D, &T);
|
|
|
383d26 |
+ memcpy(dst, &D, XTS_BLOCK_SIZE);
|
|
|
383d26 |
}
|
|
|
383d26 |
|
|
|
383d26 |
/* Decrypt the iv back */
|
|
|
383d26 |
--
|
|
|
383d26 |
1.8.3.1
|
|
|
383d26 |
|