yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

Blame SOURCES/kvm-crypto-convert-xts_tweak_encdec-to-use-xts_uint128-t.patch

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