yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone

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

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