yeahuh / rpms / qemu-kvm

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