From 9a8d32a24fe7d0a1865be8ae83308cc001d1d091 Mon Sep 17 00:00:00 2001 From: Szymon Janc Date: Sat, 20 Aug 2011 18:24:54 +0200 Subject: [PATCH] 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. --- ChangeLog.lzo | 8 ++ Makefile.util.def | 1 + grub-core/Makefile.core.def | 5 ++ grub-core/lib/adler32.c | 152 ++++++++++++++++++++++++++++++++++++ util/import_gcry.py | 2 + 5 files changed, 168 insertions(+) create mode 100644 grub-core/lib/adler32.c diff --git a/ChangeLog.lzo b/ChangeLog.lzo index 2ff940e21..d24d913d8 100644 --- a/ChangeLog.lzo +++ b/ChangeLog.lzo @@ -1,3 +1,11 @@ +2011-08-20 Szymon Janc + + 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 More work on LZO for btrfs support. Some fixes and code refactoring. diff --git a/Makefile.util.def b/Makefile.util.def index 3628e48f6..365f718a1 100644 --- a/Makefile.util.def +++ b/Makefile.util.def @@ -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; diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def index fc017b780..77dc7ed4b 100644 --- a/grub-core/Makefile.core.def +++ b/grub-core/Makefile.core.def @@ -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 index 000000000..cc40735cb --- /dev/null +++ b/grub-core/lib/adler32.c @@ -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 . + */ + +#include +#include +#include + +/* 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); +} diff --git a/util/import_gcry.py b/util/import_gcry.py index 727492f10..ee75b1b8e 100644 --- a/util/import_gcry.py +++ b/util/import_gcry.py @@ -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)