Blame SOURCES/kvm-crypto-remove-code-duplication-in-tweak-encrypt-decr.patch

7711c0
From 272c652c38c9f9bcce2eb8e4e32ab68d48c3531a Mon Sep 17 00:00:00 2001
7711c0
From: "Daniel P. Berrange" <berrange@redhat.com>
7711c0
Date: Wed, 24 Apr 2019 10:30:24 +0200
7711c0
Subject: [PATCH 05/12] crypto: remove code duplication in tweak
7711c0
 encrypt/decrypt
7711c0
MIME-Version: 1.0
7711c0
Content-Type: text/plain; charset=UTF-8
7711c0
Content-Transfer-Encoding: 8bit
7711c0
7711c0
RH-Author: Daniel P. Berrange <berrange@redhat.com>
7711c0
Message-id: <20190424103030.2925-4-berrange@redhat.com>
7711c0
Patchwork-id: 85891
7711c0
O-Subject: [RHEL-7.7 qemu-kvm-rhev PATCH 3/9] crypto: remove code duplication in tweak encrypt/decrypt
7711c0
Bugzilla: 1666336
7711c0
RH-Acked-by: Philippe Mathieu-Daudé <philmd@redhat.com>
7711c0
RH-Acked-by: John Snow <jsnow@redhat.com>
7711c0
RH-Acked-by: Eric Blake <eblake@redhat.com>
7711c0
7711c0
The tweak encrypt/decrypt functions are identical except for the
7711c0
comments, so can be merged. Profiling data shows that the compiler is
7711c0
in fact already merging the two merges in the object files.
7711c0
7711c0
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
7711c0
Reviewed-by: Alberto Garcia <berto@igalia.com>
7711c0
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
7711c0
(cherry picked from commit 299ec87838babdf38be618cf2d81aef2500758bd)
7711c0
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
7711c0
---
7711c0
 crypto/xts.c | 64 ++++++++++++++----------------------------------------------
7711c0
 1 file changed, 15 insertions(+), 49 deletions(-)
7711c0
7711c0
diff --git a/crypto/xts.c b/crypto/xts.c
7711c0
index 9521234..3c1a92f 100644
7711c0
--- a/crypto/xts.c
7711c0
+++ b/crypto/xts.c
7711c0
@@ -43,20 +43,20 @@ static void xts_mult_x(uint8_t *I)
7711c0
 
7711c0
 
7711c0
 /**
7711c0
- * xts_tweak_uncrypt:
7711c0
+ * xts_tweak_encdec:
7711c0
  * @param ctxt: the cipher context
7711c0
  * @param func: the cipher function
7711c0
- * @src: buffer providing the cipher text of XTS_BLOCK_SIZE bytes
7711c0
- * @dst: buffer to output the plain text of XTS_BLOCK_SIZE bytes
7711c0
+ * @src: buffer providing the input text of XTS_BLOCK_SIZE bytes
7711c0
+ * @dst: buffer to output the output text of XTS_BLOCK_SIZE bytes
7711c0
  * @iv: the initialization vector tweak of XTS_BLOCK_SIZE bytes
7711c0
  *
7711c0
- * Decrypt data with a tweak
7711c0
+ * Encrypt/decrypt data with a tweak
7711c0
  */
7711c0
-static void xts_tweak_decrypt(const void *ctx,
7711c0
-                              xts_cipher_func *func,
7711c0
-                              const uint8_t *src,
7711c0
-                              uint8_t *dst,
7711c0
-                              uint8_t *iv)
7711c0
+static void xts_tweak_encdec(const void *ctx,
7711c0
+                             xts_cipher_func *func,
7711c0
+                             const uint8_t *src,
7711c0
+                             uint8_t *dst,
7711c0
+                             uint8_t *iv)
7711c0
 {
7711c0
     unsigned long x;
7711c0
 
7711c0
@@ -105,7 +105,7 @@ void xts_decrypt(const void *datactx,
7711c0
     encfunc(tweakctx, XTS_BLOCK_SIZE, T, iv);
7711c0
 
7711c0
     for (i = 0; i < lim; i++) {
7711c0
-        xts_tweak_decrypt(datactx, decfunc, src, dst, T);
7711c0
+        xts_tweak_encdec(datactx, decfunc, src, dst, T);
7711c0
 
7711c0
         src += XTS_BLOCK_SIZE;
7711c0
         dst += XTS_BLOCK_SIZE;
7711c0
@@ -117,7 +117,7 @@ void xts_decrypt(const void *datactx,
7711c0
         xts_mult_x(CC);
7711c0
 
7711c0
         /* PP = tweak decrypt block m-1 */
7711c0
-        xts_tweak_decrypt(datactx, decfunc, src, PP, CC);
7711c0
+        xts_tweak_encdec(datactx, decfunc, src, PP, CC);
7711c0
 
7711c0
         /* Pm = first length % XTS_BLOCK_SIZE bytes of PP */
7711c0
         for (i = 0; i < mo; i++) {
7711c0
@@ -129,7 +129,7 @@ void xts_decrypt(const void *datactx,
7711c0
         }
7711c0
 
7711c0
         /* Pm-1 = Tweak uncrypt CC */
7711c0
-        xts_tweak_decrypt(datactx, decfunc, CC, dst, T);
7711c0
+        xts_tweak_encdec(datactx, decfunc, CC, dst, T);
7711c0
     }
7711c0
 
7711c0
     /* Decrypt the iv back */
7711c0
@@ -137,40 +137,6 @@ void xts_decrypt(const void *datactx,
7711c0
 }
7711c0
 
7711c0
 
7711c0
-/**
7711c0
- * xts_tweak_crypt:
7711c0
- * @param ctxt: the cipher context
7711c0
- * @param func: the cipher function
7711c0
- * @src: buffer providing the plain text of XTS_BLOCK_SIZE bytes
7711c0
- * @dst: buffer to output the cipher text of XTS_BLOCK_SIZE bytes
7711c0
- * @iv: the initialization vector tweak of XTS_BLOCK_SIZE bytes
7711c0
- *
7711c0
- * Encrypt data with a tweak
7711c0
- */
7711c0
-static void xts_tweak_encrypt(const void *ctx,
7711c0
-                              xts_cipher_func *func,
7711c0
-                              const uint8_t *src,
7711c0
-                              uint8_t *dst,
7711c0
-                              uint8_t *iv)
7711c0
-{
7711c0
-    unsigned long x;
7711c0
-
7711c0
-    /* tweak encrypt block i */
7711c0
-    for (x = 0; x < XTS_BLOCK_SIZE; x++) {
7711c0
-        dst[x] = src[x] ^ iv[x];
7711c0
-    }
7711c0
-
7711c0
-    func(ctx, XTS_BLOCK_SIZE, dst, dst);
7711c0
-
7711c0
-    for (x = 0; x < XTS_BLOCK_SIZE; x++) {
7711c0
-        dst[x] = dst[x] ^ iv[x];
7711c0
-    }
7711c0
-
7711c0
-    /* LFSR the tweak */
7711c0
-    xts_mult_x(iv);
7711c0
-}
7711c0
-
7711c0
-
7711c0
 void xts_encrypt(const void *datactx,
7711c0
                  const void *tweakctx,
7711c0
                  xts_cipher_func *encfunc,
7711c0
@@ -200,7 +166,7 @@ void xts_encrypt(const void *datactx,
7711c0
     encfunc(tweakctx, XTS_BLOCK_SIZE, T, iv);
7711c0
 
7711c0
     for (i = 0; i < lim; i++) {
7711c0
-        xts_tweak_encrypt(datactx, encfunc, src, dst, T);
7711c0
+        xts_tweak_encdec(datactx, encfunc, src, dst, T);
7711c0
 
7711c0
         dst += XTS_BLOCK_SIZE;
7711c0
         src += XTS_BLOCK_SIZE;
7711c0
@@ -209,7 +175,7 @@ void xts_encrypt(const void *datactx,
7711c0
     /* if length is not a multiple of XTS_BLOCK_SIZE then */
7711c0
     if (mo > 0) {
7711c0
         /* CC = tweak encrypt block m-1 */
7711c0
-        xts_tweak_encrypt(datactx, encfunc, src, CC, T);
7711c0
+        xts_tweak_encdec(datactx, encfunc, src, CC, T);
7711c0
 
7711c0
         /* Cm = first length % XTS_BLOCK_SIZE bytes of CC */
7711c0
         for (i = 0; i < mo; i++) {
7711c0
@@ -222,7 +188,7 @@ void xts_encrypt(const void *datactx,
7711c0
         }
7711c0
 
7711c0
         /* Cm-1 = Tweak encrypt PP */
7711c0
-        xts_tweak_encrypt(datactx, encfunc, PP, dst, T);
7711c0
+        xts_tweak_encdec(datactx, encfunc, PP, dst, T);
7711c0
     }
7711c0
 
7711c0
     /* Decrypt the iv back */
7711c0
-- 
7711c0
1.8.3.1
7711c0