]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
Unify and improve RAID and crypto xor.
authorVladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Tue, 13 Dec 2011 00:26:53 +0000 (01:26 +0100)
committerVladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Tue, 13 Dec 2011 00:26:53 +0000 (01:26 +0100)
* grub-core/disk/raid.c (grub_raid_block_xor): Removed. All users
changed to grub_crypto_xor
* grub-core/lib/crypto.c (grub_crypto_xor): Moved from here ...
* include/grub/crypto.h (grub_crypto_xor): ... here. Inlined.
Use bigger types when possible.

ChangeLog
grub-core/disk/raid.c
grub-core/disk/raid5_recover.c
grub-core/disk/raid6_recover.c
grub-core/lib/crypto.c
include/grub/crypto.h

index b157a8e40d6763d2edda77421c08f9ce4462dc49..801681ef5ec5de42afb795fb4d476e15e341d6cf 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2011-12-13  Vladimir Serbinenko  <phcoder@gmail.com>
+
+       Unify and improve RAID and crypto xor.
+
+       * grub-core/disk/raid.c (grub_raid_block_xor): Removed. All users
+       changed to grub_crypto_xor
+       * grub-core/lib/crypto.c (grub_crypto_xor): Moved from here ...
+       * include/grub/crypto.h (grub_crypto_xor): ... here. Inlined.
+       Use bigger types when possible.
+
 2011-12-13  Vladimir Serbinenko  <phcoder@gmail.com>
 
        * grub-core/disk/raid.c (scan_devices): Fix condition.
index aeba2a22f934ab97d63fadee6810a4fe195010fa..db1e969b31ff1926f851f550b677b53968d772e3 100644 (file)
@@ -332,23 +332,6 @@ grub_raid_close (grub_disk_t disk __attribute ((unused)))
   return;
 }
 
-void
-grub_raid_block_xor (char *buf1, const char *buf2, int size)
-{
-  grub_size_t *p1;
-  const grub_size_t *p2;
-
-  p1 = (grub_size_t *) buf1;
-  p2 = (const grub_size_t *) buf2;
-  size /= GRUB_CPU_SIZEOF_VOID_P;
-
-  while (size)
-    {
-      *(p1++) ^= *(p2++);
-      size--;
-    }
-}
-
 static grub_err_t
 grub_raid_read (grub_disk_t disk, grub_disk_addr_t sector,
                grub_size_t size, char *buf)
index c26d05e94f273135e3d3d1629449dd6f0611fc23..dfc89a71d55c942267531f76132141f8a2d4fdbe 100644 (file)
@@ -23,6 +23,7 @@
 #include <grub/err.h>
 #include <grub/misc.h>
 #include <grub/raid.h>
+#include <grub/crypto.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -57,7 +58,7 @@ grub_raid5_recover (struct grub_raid_array *array, int disknr,
           return err;
         }
 
-      grub_raid_block_xor (buf, buf2, size);
+      grub_crypto_xor (buf, buf2, buf2, size);
     }
 
   grub_free (buf2);
index e91992547fbd468791996450a5e3325dda1d7145..05d8511c55bbe8a470e3e2f90d081b2d150e43f2 100644 (file)
@@ -23,6 +23,7 @@
 #include <grub/err.h>
 #include <grub/misc.h>
 #include <grub/raid.h>
+#include <grub/crypto.h>
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
@@ -98,9 +99,9 @@ grub_raid6_recover (struct grub_raid_array *array, int disknr, int p,
                                 array->members[pos].start_sector + sector,
                                 0, size, buf)))
             {
-              grub_raid_block_xor (pbuf, buf, size);
+              grub_crypto_xor (pbuf, pbuf, buf, size);
               grub_raid_block_mulx (i, buf, size);
-              grub_raid_block_xor (qbuf, buf, size);
+              grub_crypto_xor (qbuf, qbuf, buf, size);
             }
           else
             {
@@ -130,7 +131,7 @@ grub_raid6_recover (struct grub_raid_array *array, int disknr, int p,
                             array->members[p].start_sector + sector,
                             0, size, buf)))
         {
-          grub_raid_block_xor (buf, pbuf, size);
+          grub_crypto_xor (buf, buf, pbuf, size);
           goto quit;
         }
 
@@ -145,7 +146,7 @@ grub_raid6_recover (struct grub_raid_array *array, int disknr, int p,
                          array->members[q].start_sector + sector, 0, size, buf))
         goto quit;
 
-      grub_raid_block_xor (buf, qbuf, size);
+      grub_crypto_xor (buf, buf, qbuf, size);
       grub_raid_block_mulx (255 - bad1, buf,
                            size);
     }
@@ -165,14 +166,14 @@ grub_raid6_recover (struct grub_raid_array *array, int disknr, int p,
                          0, size, buf))
         goto quit;
 
-      grub_raid_block_xor (pbuf, buf, size);
+      grub_crypto_xor (pbuf, pbuf, buf, size);
 
       if (grub_disk_read (array->members[q].device,
                          array->members[q].start_sector + sector,
                          0, size, buf))
         goto quit;
 
-      grub_raid_block_xor (qbuf, buf, size);
+      grub_crypto_xor (qbuf, qbuf, buf, size);
 
       c = (255 - bad1 + (255 - powx_inv[(powx[bad2 - bad1 + 255] ^ 1)])) % 255;
       grub_raid_block_mulx (c, qbuf, size);
@@ -180,7 +181,7 @@ grub_raid6_recover (struct grub_raid_array *array, int disknr, int p,
       c = (bad2 + c) % 255;
       grub_raid_block_mulx (c, pbuf, size);
 
-      grub_raid_block_xor (pbuf, qbuf, size);
+      grub_crypto_xor (pbuf, pbuf, qbuf, size);
       grub_memcpy (buf, pbuf, size);
     }
 
index 4dec5c6949255926c24f6301587f3d37a4031800..48d86033505b6c8d8b1594638153e4559976fc90 100644 (file)
@@ -169,20 +169,6 @@ grub_crypto_cipher_set_key (grub_crypto_cipher_handle_t cipher,
   return cipher->cipher->setkey (cipher->ctx, key, keylen);
 }
 
-void
-grub_crypto_xor (void *out, const void *in1, const void *in2, grub_size_t size)
-{
-  const grub_uint8_t *in1ptr = in1, *in2ptr = in2;
-  grub_uint8_t *outptr = out;
-  while (size--)
-    {
-      *outptr = *in1ptr ^ *in2ptr;
-      in1ptr++;
-      in2ptr++;
-      outptr++;
-    }
-}
-
 gcry_err_code_t
 grub_crypto_ecb_decrypt (grub_crypto_cipher_handle_t cipher,
                         void *out, const void *in, grub_size_t size)
index b8a5b3a225f0ed58bf8150db023023304fff1707..9a67c5e119648ae11fef637c7fb359fd11d37932 100644 (file)
@@ -198,8 +198,40 @@ grub_crypto_cipher_close (grub_crypto_cipher_handle_t cipher)
   grub_free (cipher);
 }
 
-void
-grub_crypto_xor (void *out, const void *in1, const void *in2, grub_size_t size);
+static inline void
+grub_crypto_xor (void *out, const void *in1, const void *in2, grub_size_t size)
+{
+  const grub_uint8_t *in1ptr = in1, *in2ptr = in2;
+  grub_uint8_t *outptr = out;
+  while (size && (((grub_addr_t) in1ptr & (sizeof (grub_uint64_t) - 1))
+                 || ((grub_addr_t) in2ptr & (sizeof (grub_uint64_t) - 1))
+                 || ((grub_addr_t) outptr & (sizeof (grub_uint64_t) - 1))))
+    {
+      *outptr = *in1ptr ^ *in2ptr;
+      in1ptr++;
+      in2ptr++;
+      outptr++;
+      size--;
+    }
+  while (size >= sizeof (grub_uint64_t))
+    {
+      *(grub_uint64_t *) (void *) outptr
+       = (*(grub_uint64_t *) (void *) in1ptr
+          ^ *(grub_uint64_t *) (void *) in2ptr);
+      in1ptr += sizeof (grub_uint64_t);
+      in2ptr += sizeof (grub_uint64_t);
+      outptr += sizeof (grub_uint64_t);
+      size -= sizeof (grub_uint64_t);
+    }
+  while (size)
+    {
+      *outptr = *in1ptr ^ *in2ptr;
+      in1ptr++;
+      in2ptr++;
+      outptr++;
+      size--;
+    }
+}
 
 gcry_err_code_t
 grub_crypto_ecb_decrypt (grub_crypto_cipher_handle_t cipher,