thebeanogamer / rpms / qemu-kvm

Forked from rpms/qemu-kvm 5 months ago
Clone

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

b38b0f
From cfa8288b73c3a2856a56bef220b7468b402905c3 Mon Sep 17 00:00:00 2001
b38b0f
From: "Daniel P. Berrange" <berrange@redhat.com>
b38b0f
Date: Wed, 24 Apr 2019 09:56:37 +0100
b38b0f
Subject: [PATCH 3/9] crypto: remove code duplication in tweak encrypt/decrypt
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-4-berrange@redhat.com>
b38b0f
Patchwork-id: 85881
b38b0f
O-Subject: [RHEL-8.1.0 qemu-kvm PATCH 3/9] crypto: remove code duplication in tweak encrypt/decrypt
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 tweak encrypt/decrypt functions are identical except for the
b38b0f
comments, so can be merged. Profiling data shows that the compiler is
b38b0f
in fact already merging the two merges in the object files.
b38b0f
b38b0f
Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com>
b38b0f
Reviewed-by: Alberto Garcia <berto@igalia.com>
b38b0f
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
b38b0f
(cherry picked from commit 299ec87838babdf38be618cf2d81aef2500758bd)
b38b0f
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
b38b0f
---
b38b0f
 crypto/xts.c | 64 ++++++++++++++----------------------------------------------
b38b0f
 1 file changed, 15 insertions(+), 49 deletions(-)
b38b0f
b38b0f
diff --git a/crypto/xts.c b/crypto/xts.c
b38b0f
index 9521234..3c1a92f 100644
b38b0f
--- a/crypto/xts.c
b38b0f
+++ b/crypto/xts.c
b38b0f
@@ -43,20 +43,20 @@ static void xts_mult_x(uint8_t *I)
b38b0f
 
b38b0f
 
b38b0f
 /**
b38b0f
- * xts_tweak_uncrypt:
b38b0f
+ * xts_tweak_encdec:
b38b0f
  * @param ctxt: the cipher context
b38b0f
  * @param func: the cipher function
b38b0f
- * @src: buffer providing the cipher text of XTS_BLOCK_SIZE bytes
b38b0f
- * @dst: buffer to output the plain text of XTS_BLOCK_SIZE bytes
b38b0f
+ * @src: buffer providing the input text of XTS_BLOCK_SIZE bytes
b38b0f
+ * @dst: buffer to output the output text of XTS_BLOCK_SIZE bytes
b38b0f
  * @iv: the initialization vector tweak of XTS_BLOCK_SIZE bytes
b38b0f
  *
b38b0f
- * Decrypt data with a tweak
b38b0f
+ * Encrypt/decrypt data with a tweak
b38b0f
  */
b38b0f
-static void xts_tweak_decrypt(const void *ctx,
b38b0f
-                              xts_cipher_func *func,
b38b0f
-                              const uint8_t *src,
b38b0f
-                              uint8_t *dst,
b38b0f
-                              uint8_t *iv)
b38b0f
+static void xts_tweak_encdec(const void *ctx,
b38b0f
+                             xts_cipher_func *func,
b38b0f
+                             const uint8_t *src,
b38b0f
+                             uint8_t *dst,
b38b0f
+                             uint8_t *iv)
b38b0f
 {
b38b0f
     unsigned long x;
b38b0f
 
b38b0f
@@ -105,7 +105,7 @@ void xts_decrypt(const void *datactx,
b38b0f
     encfunc(tweakctx, XTS_BLOCK_SIZE, T, iv);
b38b0f
 
b38b0f
     for (i = 0; i < lim; i++) {
b38b0f
-        xts_tweak_decrypt(datactx, decfunc, src, dst, T);
b38b0f
+        xts_tweak_encdec(datactx, decfunc, src, dst, T);
b38b0f
 
b38b0f
         src += XTS_BLOCK_SIZE;
b38b0f
         dst += XTS_BLOCK_SIZE;
b38b0f
@@ -117,7 +117,7 @@ void xts_decrypt(const void *datactx,
b38b0f
         xts_mult_x(CC);
b38b0f
 
b38b0f
         /* PP = tweak decrypt block m-1 */
b38b0f
-        xts_tweak_decrypt(datactx, decfunc, src, PP, CC);
b38b0f
+        xts_tweak_encdec(datactx, decfunc, src, PP, CC);
b38b0f
 
b38b0f
         /* Pm = first length % XTS_BLOCK_SIZE bytes of PP */
b38b0f
         for (i = 0; i < mo; i++) {
b38b0f
@@ -129,7 +129,7 @@ void xts_decrypt(const void *datactx,
b38b0f
         }
b38b0f
 
b38b0f
         /* Pm-1 = Tweak uncrypt CC */
b38b0f
-        xts_tweak_decrypt(datactx, decfunc, CC, dst, T);
b38b0f
+        xts_tweak_encdec(datactx, decfunc, CC, dst, T);
b38b0f
     }
b38b0f
 
b38b0f
     /* Decrypt the iv back */
b38b0f
@@ -137,40 +137,6 @@ void xts_decrypt(const void *datactx,
b38b0f
 }
b38b0f
 
b38b0f
 
b38b0f
-/**
b38b0f
- * xts_tweak_crypt:
b38b0f
- * @param ctxt: the cipher context
b38b0f
- * @param func: the cipher function
b38b0f
- * @src: buffer providing the plain text of XTS_BLOCK_SIZE bytes
b38b0f
- * @dst: buffer to output the cipher text of XTS_BLOCK_SIZE bytes
b38b0f
- * @iv: the initialization vector tweak of XTS_BLOCK_SIZE bytes
b38b0f
- *
b38b0f
- * Encrypt data with a tweak
b38b0f
- */
b38b0f
-static void xts_tweak_encrypt(const void *ctx,
b38b0f
-                              xts_cipher_func *func,
b38b0f
-                              const uint8_t *src,
b38b0f
-                              uint8_t *dst,
b38b0f
-                              uint8_t *iv)
b38b0f
-{
b38b0f
-    unsigned long x;
b38b0f
-
b38b0f
-    /* tweak encrypt block i */
b38b0f
-    for (x = 0; x < XTS_BLOCK_SIZE; x++) {
b38b0f
-        dst[x] = src[x] ^ iv[x];
b38b0f
-    }
b38b0f
-
b38b0f
-    func(ctx, XTS_BLOCK_SIZE, dst, dst);
b38b0f
-
b38b0f
-    for (x = 0; x < XTS_BLOCK_SIZE; x++) {
b38b0f
-        dst[x] = dst[x] ^ iv[x];
b38b0f
-    }
b38b0f
-
b38b0f
-    /* LFSR the tweak */
b38b0f
-    xts_mult_x(iv);
b38b0f
-}
b38b0f
-
b38b0f
-
b38b0f
 void xts_encrypt(const void *datactx,
b38b0f
                  const void *tweakctx,
b38b0f
                  xts_cipher_func *encfunc,
b38b0f
@@ -200,7 +166,7 @@ void xts_encrypt(const void *datactx,
b38b0f
     encfunc(tweakctx, XTS_BLOCK_SIZE, T, iv);
b38b0f
 
b38b0f
     for (i = 0; i < lim; i++) {
b38b0f
-        xts_tweak_encrypt(datactx, encfunc, src, dst, T);
b38b0f
+        xts_tweak_encdec(datactx, encfunc, src, dst, T);
b38b0f
 
b38b0f
         dst += XTS_BLOCK_SIZE;
b38b0f
         src += XTS_BLOCK_SIZE;
b38b0f
@@ -209,7 +175,7 @@ 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_encrypt(datactx, encfunc, src, CC, T);
b38b0f
+        xts_tweak_encdec(datactx, encfunc, src, CC, T);
b38b0f
 
b38b0f
         /* Cm = first length % XTS_BLOCK_SIZE bytes of CC */
b38b0f
         for (i = 0; i < mo; i++) {
b38b0f
@@ -222,7 +188,7 @@ void xts_encrypt(const void *datactx,
b38b0f
         }
b38b0f
 
b38b0f
         /* Cm-1 = Tweak encrypt PP */
b38b0f
-        xts_tweak_encrypt(datactx, encfunc, PP, dst, T);
b38b0f
+        xts_tweak_encdec(datactx, encfunc, PP, dst, T);
b38b0f
     }
b38b0f
 
b38b0f
     /* Decrypt the iv back */
b38b0f
-- 
b38b0f
1.8.3.1
b38b0f