Pablo Greco e6a3ae
From 5249fe526e66a92e293bc638ab53eb3d8cd68881 Mon Sep 17 00:00:00 2001
Pablo Greco e6a3ae
From: "Daniel P. Berrange" <berrange@redhat.com>
Pablo Greco e6a3ae
Date: Wed, 24 Apr 2019 09:56:40 +0100
Pablo Greco e6a3ae
Subject: [PATCH 6/9] crypto: convert xts_mult_x to use xts_uint128 type
Pablo Greco e6a3ae
MIME-Version: 1.0
Pablo Greco e6a3ae
Content-Type: text/plain; charset=UTF-8
Pablo Greco e6a3ae
Content-Transfer-Encoding: 8bit
Pablo Greco e6a3ae
Pablo Greco e6a3ae
RH-Author: Daniel P. Berrange <berrange@redhat.com>
Pablo Greco e6a3ae
Message-id: <20190424095643.796-7-berrange@redhat.com>
Pablo Greco e6a3ae
Patchwork-id: 85879
Pablo Greco e6a3ae
O-Subject: [RHEL-8.1.0 qemu-kvm PATCH 6/9] crypto: convert xts_mult_x to use xts_uint128 type
Pablo Greco e6a3ae
Bugzilla: 1680231
Pablo Greco e6a3ae
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: John Snow <jsnow@redhat.com>
Pablo Greco e6a3ae
RH-Acked-by: Eric Blake <eblake@redhat.com>
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Using 64-bit arithmetic increases the performance for xts-aes-128
Pablo Greco e6a3ae
when built with gcrypt:
Pablo Greco e6a3ae
Pablo Greco e6a3ae
  Encrypt: 355 MB/s -> 545 MB/s
Pablo Greco e6a3ae
  Decrypt: 362 MB/s -> 568 MB/s
Pablo Greco e6a3ae
Pablo Greco e6a3ae
Reviewed-by: Alberto Garcia <berto@igalia.com>
Pablo Greco e6a3ae
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
Pablo Greco e6a3ae
(cherry picked from commit 7dac0dd67426753646df0c23c819609b9e704f59)
Pablo Greco e6a3ae
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
Pablo Greco e6a3ae
---
Pablo Greco e6a3ae
 crypto/xts.c | 40 ++++++++++++++++++++++++++++------------
Pablo Greco e6a3ae
 1 file changed, 28 insertions(+), 12 deletions(-)
Pablo Greco e6a3ae
Pablo Greco e6a3ae
diff --git a/crypto/xts.c b/crypto/xts.c
Pablo Greco e6a3ae
index 0ad231f..10ec83f 100644
Pablo Greco e6a3ae
--- a/crypto/xts.c
Pablo Greco e6a3ae
+++ b/crypto/xts.c
Pablo Greco e6a3ae
@@ -24,6 +24,7 @@
Pablo Greco e6a3ae
  */
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
 #include "qemu/osdep.h"
Pablo Greco e6a3ae
+#include "qemu/bswap.h"
Pablo Greco e6a3ae
 #include "crypto/xts.h"
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
 typedef union {
Pablo Greco e6a3ae
@@ -39,19 +40,34 @@ static inline void xts_uint128_xor(xts_uint128 *D,
Pablo Greco e6a3ae
     D->u[1] = S1->u[1] ^ S2->u[1];
Pablo Greco e6a3ae
 }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-static void xts_mult_x(uint8_t *I)
Pablo Greco e6a3ae
+static inline void xts_uint128_cpu_to_les(xts_uint128 *v)
Pablo Greco e6a3ae
 {
Pablo Greco e6a3ae
-    int x;
Pablo Greco e6a3ae
-    uint8_t t, tt;
Pablo Greco e6a3ae
+    cpu_to_le64s(&v->u[0]);
Pablo Greco e6a3ae
+    cpu_to_le64s(&v->u[1]);
Pablo Greco e6a3ae
+}
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
-    for (x = t = 0; x < 16; x++) {
Pablo Greco e6a3ae
-        tt = I[x] >> 7;
Pablo Greco e6a3ae
-        I[x] = ((I[x] << 1) | t) & 0xFF;
Pablo Greco e6a3ae
-        t = tt;
Pablo Greco e6a3ae
-    }
Pablo Greco e6a3ae
-    if (tt) {
Pablo Greco e6a3ae
-        I[0] ^= 0x87;
Pablo Greco e6a3ae
+static inline void xts_uint128_le_to_cpus(xts_uint128 *v)
Pablo Greco e6a3ae
+{
Pablo Greco e6a3ae
+    le64_to_cpus(&v->u[0]);
Pablo Greco e6a3ae
+    le64_to_cpus(&v->u[1]);
Pablo Greco e6a3ae
+}
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+static void xts_mult_x(xts_uint128 *I)
Pablo Greco e6a3ae
+{
Pablo Greco e6a3ae
+    uint64_t tt;
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    xts_uint128_le_to_cpus(I);
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    tt = I->u[0] >> 63;
Pablo Greco e6a3ae
+    I->u[0] <<= 1;
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    if (I->u[1] >> 63) {
Pablo Greco e6a3ae
+        I->u[0] ^= 0x87;
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
+    I->u[1] <<= 1;
Pablo Greco e6a3ae
+    I->u[1] |= tt;
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+    xts_uint128_cpu_to_les(I);
Pablo Greco e6a3ae
 }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
@@ -79,7 +95,7 @@ static void xts_tweak_encdec(const void *ctx,
Pablo Greco e6a3ae
     xts_uint128_xor(dst, dst, iv);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     /* LFSR the tweak */
Pablo Greco e6a3ae
-    xts_mult_x(iv->b);
Pablo Greco e6a3ae
+    xts_mult_x(iv);
Pablo Greco e6a3ae
 }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
@@ -134,7 +150,7 @@ void xts_decrypt(const void *datactx,
Pablo Greco e6a3ae
     if (mo > 0) {
Pablo Greco e6a3ae
         xts_uint128 S, D;
Pablo Greco e6a3ae
         memcpy(&CC, &T, XTS_BLOCK_SIZE);
Pablo Greco e6a3ae
-        xts_mult_x(CC.b);
Pablo Greco e6a3ae
+        xts_mult_x(&CC);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
         /* PP = tweak decrypt block m-1 */
Pablo Greco e6a3ae
         memcpy(&S, src, XTS_BLOCK_SIZE);
Pablo Greco e6a3ae
-- 
Pablo Greco e6a3ae
1.8.3.1
Pablo Greco e6a3ae