2011-08-20 16:24:54 +00:00
|
|
|
/*
|
|
|
|
* GRUB -- GRand Unified Bootloader
|
2012-04-07 17:58:39 +00:00
|
|
|
* Copyright (C) 2012 Free Software Foundation, Inc.
|
2011-08-20 16:24:54 +00:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2012-04-07 17:58:39 +00:00
|
|
|
|
2011-08-20 16:24:54 +00:00
|
|
|
#include <grub/dl.h>
|
|
|
|
#include <grub/crypto.h>
|
|
|
|
|
2011-11-14 13:25:09 +00:00
|
|
|
GRUB_MOD_LICENSE ("GPLv3+");
|
|
|
|
|
2012-04-07 17:58:39 +00:00
|
|
|
struct adler32_context
|
2011-08-20 16:24:54 +00:00
|
|
|
{
|
2012-04-07 17:58:39 +00:00
|
|
|
grub_uint16_t a, b;
|
|
|
|
grub_uint32_t c;
|
|
|
|
};
|
2011-08-20 16:24:54 +00:00
|
|
|
|
2012-04-07 17:58:39 +00:00
|
|
|
static void
|
|
|
|
adler32_init (void *context)
|
|
|
|
{
|
|
|
|
struct adler32_context *ctx = context;
|
2011-08-20 16:24:54 +00:00
|
|
|
|
2012-04-07 17:58:39 +00:00
|
|
|
ctx->a = 1;
|
|
|
|
ctx->b = 0;
|
2011-08-20 16:24:54 +00:00
|
|
|
}
|
|
|
|
|
2012-04-07 17:58:39 +00:00
|
|
|
#define MOD 65521
|
|
|
|
|
|
|
|
static grub_uint16_t
|
|
|
|
mod_add (grub_uint16_t a, grub_uint16_t b)
|
2011-08-20 16:24:54 +00:00
|
|
|
{
|
2012-04-07 17:58:39 +00:00
|
|
|
if ((grub_uint32_t) a + (grub_uint32_t) b >= MOD)
|
|
|
|
return a + b - MOD;
|
|
|
|
return a + b;
|
2011-08-20 16:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-04-07 17:58:39 +00:00
|
|
|
adler32_write (void *context, const void *inbuf, grub_size_t inlen)
|
2011-08-20 16:24:54 +00:00
|
|
|
{
|
2012-04-07 17:58:39 +00:00
|
|
|
struct adler32_context *ctx = context;
|
|
|
|
const grub_uint8_t *ptr = inbuf;
|
|
|
|
|
|
|
|
while (inlen)
|
|
|
|
{
|
|
|
|
ctx->a = mod_add (ctx->a, *ptr);
|
|
|
|
ctx->b = mod_add (ctx->a, ctx->b);
|
|
|
|
inlen--;
|
|
|
|
ptr++;
|
|
|
|
}
|
2011-08-20 16:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-04-07 17:58:39 +00:00
|
|
|
adler32_final (void *context __attribute__ ((unused)))
|
2011-08-20 16:24:54 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static grub_uint8_t *
|
|
|
|
adler32_read (void *context)
|
|
|
|
{
|
2012-04-07 17:58:39 +00:00
|
|
|
struct adler32_context *ctx = context;
|
|
|
|
if (ctx->a > MOD)
|
|
|
|
ctx->a -= MOD;
|
|
|
|
if (ctx->b > MOD)
|
|
|
|
ctx->b -= MOD;
|
|
|
|
ctx->c = grub_cpu_to_be32 (ctx->a | (ctx->b << 16));
|
|
|
|
return (grub_uint8_t *) &ctx->c;
|
2011-08-20 16:24:54 +00:00
|
|
|
}
|
|
|
|
|
2012-04-07 17:58:39 +00:00
|
|
|
static gcry_md_spec_t spec_adler32 =
|
|
|
|
{
|
|
|
|
"ADLER32", 0, 0, 0, 4,
|
|
|
|
adler32_init, adler32_write, adler32_final, adler32_read,
|
|
|
|
sizeof (struct adler32_context),
|
|
|
|
#ifdef GRUB_UTIL
|
|
|
|
.modname = "adler32",
|
|
|
|
#endif
|
|
|
|
.blocksize = 64
|
|
|
|
};
|
2011-08-20 16:24:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
GRUB_MOD_INIT(adler32)
|
|
|
|
{
|
2012-04-07 17:58:39 +00:00
|
|
|
grub_md_register (&spec_adler32);
|
2011-08-20 16:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GRUB_MOD_FINI(adler32)
|
|
|
|
{
|
2012-04-07 17:58:39 +00:00
|
|
|
grub_md_unregister (&spec_adler32);
|
2011-08-20 16:24:54 +00:00
|
|
|
}
|