Pablo Greco e6a3ae
From e485d0702e2acf9b83aaf42f4cc62d804c1c282a 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:38 +0100
Pablo Greco e6a3ae
Subject: [PATCH 4/9] crypto: introduce a xts_uint128 data 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-5-berrange@redhat.com>
Pablo Greco e6a3ae
Patchwork-id: 85882
Pablo Greco e6a3ae
O-Subject: [RHEL-8.1.0 qemu-kvm PATCH 4/9] crypto: introduce a xts_uint128 data 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
The new type is designed to allow use of 64-bit arithmetic instead
Pablo Greco e6a3ae
of operating 1-byte at a time. The following patches will use this to
Pablo Greco e6a3ae
improve performance.
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 cc36930e4077eb3dbee6cd30d2d826ec62b3490a)
Pablo Greco e6a3ae
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
Pablo Greco e6a3ae
---
Pablo Greco e6a3ae
 crypto/xts.c | 46 ++++++++++++++++++++++++++--------------------
Pablo Greco e6a3ae
 1 file changed, 26 insertions(+), 20 deletions(-)
Pablo Greco e6a3ae
Pablo Greco e6a3ae
diff --git a/crypto/xts.c b/crypto/xts.c
Pablo Greco e6a3ae
index 3c1a92f..bee23f8 100644
Pablo Greco e6a3ae
--- a/crypto/xts.c
Pablo Greco e6a3ae
+++ b/crypto/xts.c
Pablo Greco e6a3ae
@@ -26,6 +26,12 @@
Pablo Greco e6a3ae
 #include "qemu/osdep.h"
Pablo Greco e6a3ae
 #include "crypto/xts.h"
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
+typedef union {
Pablo Greco e6a3ae
+    uint8_t b[XTS_BLOCK_SIZE];
Pablo Greco e6a3ae
+    uint64_t u[2];
Pablo Greco e6a3ae
+} xts_uint128;
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
+
Pablo Greco e6a3ae
 static void xts_mult_x(uint8_t *I)
Pablo Greco e6a3ae
 {
Pablo Greco e6a3ae
     int x;
Pablo Greco e6a3ae
@@ -85,7 +91,7 @@ void xts_decrypt(const void *datactx,
Pablo Greco e6a3ae
                  uint8_t *dst,
Pablo Greco e6a3ae
                  const uint8_t *src)
Pablo Greco e6a3ae
 {
Pablo Greco e6a3ae
-    uint8_t PP[XTS_BLOCK_SIZE], CC[XTS_BLOCK_SIZE], T[XTS_BLOCK_SIZE];
Pablo Greco e6a3ae
+    xts_uint128 PP, CC, T;
Pablo Greco e6a3ae
     unsigned long i, m, mo, lim;
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     /* get number of blocks */
Pablo Greco e6a3ae
@@ -102,10 +108,10 @@ void xts_decrypt(const void *datactx,
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     /* encrypt the iv */
Pablo Greco e6a3ae
-    encfunc(tweakctx, XTS_BLOCK_SIZE, T, iv);
Pablo Greco e6a3ae
+    encfunc(tweakctx, XTS_BLOCK_SIZE, T.b, iv);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     for (i = 0; i < lim; i++) {
Pablo Greco e6a3ae
-        xts_tweak_encdec(datactx, decfunc, src, dst, T);
Pablo Greco e6a3ae
+        xts_tweak_encdec(datactx, decfunc, src, dst, T.b);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
         src += XTS_BLOCK_SIZE;
Pablo Greco e6a3ae
         dst += XTS_BLOCK_SIZE;
Pablo Greco e6a3ae
@@ -113,27 +119,27 @@ void xts_decrypt(const void *datactx,
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     /* if length is not a multiple of XTS_BLOCK_SIZE then */
Pablo Greco e6a3ae
     if (mo > 0) {
Pablo Greco e6a3ae
-        memcpy(CC, T, XTS_BLOCK_SIZE);
Pablo Greco e6a3ae
-        xts_mult_x(CC);
Pablo Greco e6a3ae
+        memcpy(&CC, &T, XTS_BLOCK_SIZE);
Pablo Greco e6a3ae
+        xts_mult_x(CC.b);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
         /* PP = tweak decrypt block m-1 */
Pablo Greco e6a3ae
-        xts_tweak_encdec(datactx, decfunc, src, PP, CC);
Pablo Greco e6a3ae
+        xts_tweak_encdec(datactx, decfunc, src, PP.b, CC.b);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
         /* Pm = first length % XTS_BLOCK_SIZE bytes of PP */
Pablo Greco e6a3ae
         for (i = 0; i < mo; i++) {
Pablo Greco e6a3ae
-            CC[i] = src[XTS_BLOCK_SIZE + i];
Pablo Greco e6a3ae
-            dst[XTS_BLOCK_SIZE + i] = PP[i];
Pablo Greco e6a3ae
+            CC.b[i] = src[XTS_BLOCK_SIZE + i];
Pablo Greco e6a3ae
+            dst[XTS_BLOCK_SIZE + i] = PP.b[i];
Pablo Greco e6a3ae
         }
Pablo Greco e6a3ae
         for (; i < XTS_BLOCK_SIZE; i++) {
Pablo Greco e6a3ae
-            CC[i] = PP[i];
Pablo Greco e6a3ae
+            CC.b[i] = PP.b[i];
Pablo Greco e6a3ae
         }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
         /* Pm-1 = Tweak uncrypt CC */
Pablo Greco e6a3ae
-        xts_tweak_encdec(datactx, decfunc, CC, dst, T);
Pablo Greco e6a3ae
+        xts_tweak_encdec(datactx, decfunc, CC.b, dst, T.b);
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     /* Decrypt the iv back */
Pablo Greco e6a3ae
-    decfunc(tweakctx, XTS_BLOCK_SIZE, iv, T);
Pablo Greco e6a3ae
+    decfunc(tweakctx, XTS_BLOCK_SIZE, iv, T.b);
Pablo Greco e6a3ae
 }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
@@ -146,7 +152,7 @@ void xts_encrypt(const void *datactx,
Pablo Greco e6a3ae
                  uint8_t *dst,
Pablo Greco e6a3ae
                  const uint8_t *src)
Pablo Greco e6a3ae
 {
Pablo Greco e6a3ae
-    uint8_t PP[XTS_BLOCK_SIZE], CC[XTS_BLOCK_SIZE], T[XTS_BLOCK_SIZE];
Pablo Greco e6a3ae
+    xts_uint128 PP, CC, T;
Pablo Greco e6a3ae
     unsigned long i, m, mo, lim;
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     /* get number of blocks */
Pablo Greco e6a3ae
@@ -163,10 +169,10 @@ void xts_encrypt(const void *datactx,
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     /* encrypt the iv */
Pablo Greco e6a3ae
-    encfunc(tweakctx, XTS_BLOCK_SIZE, T, iv);
Pablo Greco e6a3ae
+    encfunc(tweakctx, XTS_BLOCK_SIZE, T.b, iv);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     for (i = 0; i < lim; i++) {
Pablo Greco e6a3ae
-        xts_tweak_encdec(datactx, encfunc, src, dst, T);
Pablo Greco e6a3ae
+        xts_tweak_encdec(datactx, encfunc, src, dst, T.b);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
         dst += XTS_BLOCK_SIZE;
Pablo Greco e6a3ae
         src += XTS_BLOCK_SIZE;
Pablo Greco e6a3ae
@@ -175,22 +181,22 @@ void xts_encrypt(const void *datactx,
Pablo Greco e6a3ae
     /* if length is not a multiple of XTS_BLOCK_SIZE then */
Pablo Greco e6a3ae
     if (mo > 0) {
Pablo Greco e6a3ae
         /* CC = tweak encrypt block m-1 */
Pablo Greco e6a3ae
-        xts_tweak_encdec(datactx, encfunc, src, CC, T);
Pablo Greco e6a3ae
+        xts_tweak_encdec(datactx, encfunc, src, CC.b, T.b);
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
         /* Cm = first length % XTS_BLOCK_SIZE bytes of CC */
Pablo Greco e6a3ae
         for (i = 0; i < mo; i++) {
Pablo Greco e6a3ae
-            PP[i] = src[XTS_BLOCK_SIZE + i];
Pablo Greco e6a3ae
-            dst[XTS_BLOCK_SIZE + i] = CC[i];
Pablo Greco e6a3ae
+            PP.b[i] = src[XTS_BLOCK_SIZE + i];
Pablo Greco e6a3ae
+            dst[XTS_BLOCK_SIZE + i] = CC.b[i];
Pablo Greco e6a3ae
         }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
         for (; i < XTS_BLOCK_SIZE; i++) {
Pablo Greco e6a3ae
-            PP[i] = CC[i];
Pablo Greco e6a3ae
+            PP.b[i] = CC.b[i];
Pablo Greco e6a3ae
         }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
         /* Cm-1 = Tweak encrypt PP */
Pablo Greco e6a3ae
-        xts_tweak_encdec(datactx, encfunc, PP, dst, T);
Pablo Greco e6a3ae
+        xts_tweak_encdec(datactx, encfunc, PP.b, dst, T.b);
Pablo Greco e6a3ae
     }
Pablo Greco e6a3ae
 
Pablo Greco e6a3ae
     /* Decrypt the iv back */
Pablo Greco e6a3ae
-    decfunc(tweakctx, XTS_BLOCK_SIZE, iv, T);
Pablo Greco e6a3ae
+    decfunc(tweakctx, XTS_BLOCK_SIZE, iv, T.b);
Pablo Greco e6a3ae
 }
Pablo Greco e6a3ae
-- 
Pablo Greco e6a3ae
1.8.3.1
Pablo Greco e6a3ae