b38b0f
From e485d0702e2acf9b83aaf42f4cc62d804c1c282a Mon Sep 17 00:00:00 2001
b38b0f
From: "Daniel P. Berrange" <berrange@redhat.com>
b38b0f
Date: Wed, 24 Apr 2019 09:56:38 +0100
b38b0f
Subject: [PATCH 4/9] crypto: introduce a xts_uint128 data type
b38b0f
MIME-Version: 1.0
b38b0f
Content-Type: text/plain; charset=UTF-8
b38b0f
Content-Transfer-Encoding: 8bit
b38b0f
b38b0f
RH-Author: Daniel P. Berrange <berrange@redhat.com>
b38b0f
Message-id: <20190424095643.796-5-berrange@redhat.com>
b38b0f
Patchwork-id: 85882
b38b0f
O-Subject: [RHEL-8.1.0 qemu-kvm PATCH 4/9] crypto: introduce a xts_uint128 data type
b38b0f
Bugzilla: 1680231
b38b0f
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
b38b0f
RH-Acked-by: John Snow <jsnow@redhat.com>
b38b0f
RH-Acked-by: Eric Blake <eblake@redhat.com>
b38b0f
b38b0f
The new type is designed to allow use of 64-bit arithmetic instead
b38b0f
of operating 1-byte at a time. The following patches will use this to
b38b0f
improve performance.
b38b0f
b38b0f
Reviewed-by: Alberto Garcia <berto@igalia.com>
b38b0f
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
b38b0f
(cherry picked from commit cc36930e4077eb3dbee6cd30d2d826ec62b3490a)
b38b0f
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
b38b0f
---
b38b0f
 crypto/xts.c | 46 ++++++++++++++++++++++++++--------------------
b38b0f
 1 file changed, 26 insertions(+), 20 deletions(-)
b38b0f
b38b0f
diff --git a/crypto/xts.c b/crypto/xts.c
b38b0f
index 3c1a92f..bee23f8 100644
b38b0f
--- a/crypto/xts.c
b38b0f
+++ b/crypto/xts.c
b38b0f
@@ -26,6 +26,12 @@
b38b0f
 #include "qemu/osdep.h"
b38b0f
 #include "crypto/xts.h"
b38b0f
 
b38b0f
+typedef union {
b38b0f
+    uint8_t b[XTS_BLOCK_SIZE];
b38b0f
+    uint64_t u[2];
b38b0f
+} xts_uint128;
b38b0f
+
b38b0f
+
b38b0f
 static void xts_mult_x(uint8_t *I)
b38b0f
 {
b38b0f
     int x;
b38b0f
@@ -85,7 +91,7 @@ void xts_decrypt(const void *datactx,
b38b0f
                  uint8_t *dst,
b38b0f
                  const uint8_t *src)
b38b0f
 {
b38b0f
-    uint8_t PP[XTS_BLOCK_SIZE], CC[XTS_BLOCK_SIZE], T[XTS_BLOCK_SIZE];
b38b0f
+    xts_uint128 PP, CC, T;
b38b0f
     unsigned long i, m, mo, lim;
b38b0f
 
b38b0f
     /* get number of blocks */
b38b0f
@@ -102,10 +108,10 @@ void xts_decrypt(const void *datactx,
b38b0f
     }
b38b0f
 
b38b0f
     /* encrypt the iv */
b38b0f
-    encfunc(tweakctx, XTS_BLOCK_SIZE, T, iv);
b38b0f
+    encfunc(tweakctx, XTS_BLOCK_SIZE, T.b, iv);
b38b0f
 
b38b0f
     for (i = 0; i < lim; i++) {
b38b0f
-        xts_tweak_encdec(datactx, decfunc, src, dst, T);
b38b0f
+        xts_tweak_encdec(datactx, decfunc, src, dst, T.b);
b38b0f
 
b38b0f
         src += XTS_BLOCK_SIZE;
b38b0f
         dst += XTS_BLOCK_SIZE;
b38b0f
@@ -113,27 +119,27 @@ void xts_decrypt(const void *datactx,
b38b0f
 
b38b0f
     /* if length is not a multiple of XTS_BLOCK_SIZE then */
b38b0f
     if (mo > 0) {
b38b0f
-        memcpy(CC, T, XTS_BLOCK_SIZE);
b38b0f
-        xts_mult_x(CC);
b38b0f
+        memcpy(&CC, &T, XTS_BLOCK_SIZE);
b38b0f
+        xts_mult_x(CC.b);
b38b0f
 
b38b0f
         /* PP = tweak decrypt block m-1 */
b38b0f
-        xts_tweak_encdec(datactx, decfunc, src, PP, CC);
b38b0f
+        xts_tweak_encdec(datactx, decfunc, src, PP.b, CC.b);
b38b0f
 
b38b0f
         /* Pm = first length % XTS_BLOCK_SIZE bytes of PP */
b38b0f
         for (i = 0; i < mo; i++) {
b38b0f
-            CC[i] = src[XTS_BLOCK_SIZE + i];
b38b0f
-            dst[XTS_BLOCK_SIZE + i] = PP[i];
b38b0f
+            CC.b[i] = src[XTS_BLOCK_SIZE + i];
b38b0f
+            dst[XTS_BLOCK_SIZE + i] = PP.b[i];
b38b0f
         }
b38b0f
         for (; i < XTS_BLOCK_SIZE; i++) {
b38b0f
-            CC[i] = PP[i];
b38b0f
+            CC.b[i] = PP.b[i];
b38b0f
         }
b38b0f
 
b38b0f
         /* Pm-1 = Tweak uncrypt CC */
b38b0f
-        xts_tweak_encdec(datactx, decfunc, CC, dst, T);
b38b0f
+        xts_tweak_encdec(datactx, decfunc, CC.b, dst, T.b);
b38b0f
     }
b38b0f
 
b38b0f
     /* Decrypt the iv back */
b38b0f
-    decfunc(tweakctx, XTS_BLOCK_SIZE, iv, T);
b38b0f
+    decfunc(tweakctx, XTS_BLOCK_SIZE, iv, T.b);
b38b0f
 }
b38b0f
 
b38b0f
 
b38b0f
@@ -146,7 +152,7 @@ void xts_encrypt(const void *datactx,
b38b0f
                  uint8_t *dst,
b38b0f
                  const uint8_t *src)
b38b0f
 {
b38b0f
-    uint8_t PP[XTS_BLOCK_SIZE], CC[XTS_BLOCK_SIZE], T[XTS_BLOCK_SIZE];
b38b0f
+    xts_uint128 PP, CC, T;
b38b0f
     unsigned long i, m, mo, lim;
b38b0f
 
b38b0f
     /* get number of blocks */
b38b0f
@@ -163,10 +169,10 @@ void xts_encrypt(const void *datactx,
b38b0f
     }
b38b0f
 
b38b0f
     /* encrypt the iv */
b38b0f
-    encfunc(tweakctx, XTS_BLOCK_SIZE, T, iv);
b38b0f
+    encfunc(tweakctx, XTS_BLOCK_SIZE, T.b, iv);
b38b0f
 
b38b0f
     for (i = 0; i < lim; i++) {
b38b0f
-        xts_tweak_encdec(datactx, encfunc, src, dst, T);
b38b0f
+        xts_tweak_encdec(datactx, encfunc, src, dst, T.b);
b38b0f
 
b38b0f
         dst += XTS_BLOCK_SIZE;
b38b0f
         src += XTS_BLOCK_SIZE;
b38b0f
@@ -175,22 +181,22 @@ void xts_encrypt(const void *datactx,
b38b0f
     /* if length is not a multiple of XTS_BLOCK_SIZE then */
b38b0f
     if (mo > 0) {
b38b0f
         /* CC = tweak encrypt block m-1 */
b38b0f
-        xts_tweak_encdec(datactx, encfunc, src, CC, T);
b38b0f
+        xts_tweak_encdec(datactx, encfunc, src, CC.b, T.b);
b38b0f
 
b38b0f
         /* Cm = first length % XTS_BLOCK_SIZE bytes of CC */
b38b0f
         for (i = 0; i < mo; i++) {
b38b0f
-            PP[i] = src[XTS_BLOCK_SIZE + i];
b38b0f
-            dst[XTS_BLOCK_SIZE + i] = CC[i];
b38b0f
+            PP.b[i] = src[XTS_BLOCK_SIZE + i];
b38b0f
+            dst[XTS_BLOCK_SIZE + i] = CC.b[i];
b38b0f
         }
b38b0f
 
b38b0f
         for (; i < XTS_BLOCK_SIZE; i++) {
b38b0f
-            PP[i] = CC[i];
b38b0f
+            PP.b[i] = CC.b[i];
b38b0f
         }
b38b0f
 
b38b0f
         /* Cm-1 = Tweak encrypt PP */
b38b0f
-        xts_tweak_encdec(datactx, encfunc, PP, dst, T);
b38b0f
+        xts_tweak_encdec(datactx, encfunc, PP.b, dst, T.b);
b38b0f
     }
b38b0f
 
b38b0f
     /* Decrypt the iv back */
b38b0f
-    decfunc(tweakctx, XTS_BLOCK_SIZE, iv, T);
b38b0f
+    decfunc(tweakctx, XTS_BLOCK_SIZE, iv, T.b);
b38b0f
 }
b38b0f
-- 
b38b0f
1.8.3.1
b38b0f