]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
Add support for adler32 checksuming.
authorSzymon Janc <szymon@janc.net.pl>
Sat, 20 Aug 2011 16:24:54 +0000 (18:24 +0200)
committerSzymon Janc <szymon@janc.net.pl>
Sat, 20 Aug 2011 16:24:54 +0000 (18:24 +0200)
* grub-core/lib/adler32.c: New file.
* Makefile.util.def (library): Add grub-core/lib/adler32.c to common.
* util/import_gcry.py (cryptolist): Add adler32.

ChangeLog.lzo
Makefile.util.def
grub-core/Makefile.core.def
grub-core/lib/adler32.c [new file with mode: 0644]
util/import_gcry.py

index 2ff940e21b64a45017f8c1c1a2d2d9bc38ccbe17..d24d913d83928049b724e8bf3ef16b3dfc4496f3 100644 (file)
@@ -1,3 +1,11 @@
+2011-08-20  Szymon Janc <szymon@janc.net.pl>
+
+       Add support for adler32 checksuming.
+
+       * grub-core/lib/adler32.c: New file.
+       * Makefile.util.def (library): Add grub-core/lib/adler32.c to common.
+       * util/import_gcry.py (cryptolist): Add adler32.
+
 2011-08-20  Szymon Janc <szymon@janc.net.pl>
 
        More work on LZO for btrfs support. Some fixes and code refactoring.
index 3628e48f65b75314c16c00354d4f976a7f2f30b7..365f718a1a9cf2fe9f61ac8a40142f4e4c3b5f59 100644 (file)
@@ -91,6 +91,7 @@ library = {
   common = grub-core/lib/LzFind.c;
   common = grub-core/lib/LzmaEnc.c;
   common = grub-core/lib/crc.c;
+  common = grub-core/lib/adler32.c;
   common = grub-core/normal/datetime.c;
   common = grub-core/normal/misc.c;
   common = grub-core/partmap/acorn.c;
index fc017b7808d8855f472a6e459ec501ca290126b9..77dc7ed4b2b28433aa87bbc1e815f85d23b2d14c 100644 (file)
@@ -1687,3 +1687,8 @@ module = {
   common = commands/cacheinfo.c;
   condition = COND_ENABLE_CACHE_STATS;
 };
+
+module = {
+  name = adler32;
+  common = lib/adler32.c;
+};
diff --git a/grub-core/lib/adler32.c b/grub-core/lib/adler32.c
new file mode 100644 (file)
index 0000000..cc40735
--- /dev/null
@@ -0,0 +1,152 @@
+/* adler32.c - adler32 check.  */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2011  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/types.h>
+#include <grub/dl.h>
+#include <grub/crypto.h>
+
+/* Based on adler32() from adler32.c of zlib-1.2.5 library. */
+
+#define BASE 65521UL
+#define NMAX 5552
+
+#define DO1(buf,i)  {adler += (buf)[i]; sum2 += adler;}
+#define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
+#define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
+#define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
+#define DO16(buf)   DO8(buf,0); DO8(buf,8);
+
+static grub_uint32_t
+update_adler32 (grub_uint32_t adler, const grub_uint8_t *buf, grub_size_t len)
+{
+  unsigned long sum2;
+  unsigned int n;
+
+  sum2 = (adler >> 16) & 0xffff;
+  adler &= 0xffff;
+
+  if (len == 1)
+    {
+      adler += buf[0];
+      if (adler >= BASE)
+       adler -= BASE;
+      sum2 += adler;
+      if (sum2 >= BASE)
+       sum2 -= BASE;
+      return adler | (sum2 << 16);
+    }
+
+  if (len < 16)
+    {
+      while (len--)
+       {
+         adler += *buf++;
+         sum2 += adler;
+       }
+      if (adler >= BASE)
+       adler -= BASE;
+      sum2 %= BASE;
+      return adler | (sum2 << 16);
+    }
+
+  while (len >= NMAX)
+    {
+      len -= NMAX;
+      n = NMAX / 16;
+      do
+       {
+         DO16 (buf);
+         buf += 16;
+       }
+      while (--n);
+      adler %= BASE;
+      sum2 %= BASE;
+    }
+
+  if (len)
+    {
+      while (len >= 16)
+       {
+         len -= 16;
+         DO16 (buf);
+         buf += 16;
+       }
+      while (len--)
+       {
+         adler += *buf++;
+         sum2 += adler;
+       }
+      adler %= BASE;
+      sum2 %= BASE;
+    }
+
+  return adler | (sum2 << 16);
+}
+
+typedef struct
+{
+  grub_uint32_t adler;
+  grub_uint8_t buf[4];
+}
+adler32_context;
+
+static void
+adler32_init (void *context)
+{
+  adler32_context *ctx = (adler32_context *) context;
+  ctx->adler = 1;
+}
+
+static void
+adler32_write (void *context, const void *inbuf, grub_size_t inlen)
+{
+  adler32_context *ctx = (adler32_context *) context;
+  if (!inbuf)
+    return;
+  ctx->adler = update_adler32 (ctx->adler, inbuf, inlen);
+}
+
+static grub_uint8_t *
+adler32_read (void *context)
+{
+  adler32_context *ctx = (adler32_context *) context;
+  return ctx->buf;
+}
+
+static void
+adler32_final (void *context __attribute__ ((unused)))
+{
+}
+
+gcry_md_spec_t _gcry_digest_spec_adler32 = {
+  "ADLER32",0 , 0, 0 , 4,
+  adler32_init, adler32_write, adler32_final, adler32_read,
+  sizeof (adler32_context),
+  .blocksize = 64
+};
+
+GRUB_MOD_INIT(adler32)
+{
+  grub_md_register (&_gcry_digest_spec_adler32);
+}
+
+GRUB_MOD_FINI(adler32)
+{
+  grub_md_unregister (&_gcry_digest_spec_adler32);
+}
index 727492f101c52390b25ec6c7c3943315c42b0c79..ee75b1b8eaba759c7b318e889621a458fbded65d 100644 (file)
@@ -81,6 +81,8 @@ cryptolist.write ("AES-128: gcry_rijndael\n");
 cryptolist.write ("AES-192: gcry_rijndael\n");
 cryptolist.write ("AES-256: gcry_rijndael\n");
 
+cryptolist.write ("ADLER32: adler32\n");
+
 for cipher_file in cipher_files:
     infile = os.path.join (cipher_dir_in, cipher_file)
     outfile = os.path.join (cipher_dir_out, cipher_file)