121 lines
3.0 KiB
C
121 lines
3.0 KiB
C
/*
|
|
* VAS_EBOOT -- GRand Unified Bootloader
|
|
* Copyright (C) 2016 Free Software Foundation, Inc.
|
|
*
|
|
* VAS_EBOOT 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.
|
|
*
|
|
* VAS_EBOOT 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 VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <VasEBoot/random.h>
|
|
#include <VasEBoot/dl.h>
|
|
#include <VasEBoot/lib/hexdump.h>
|
|
#include <VasEBoot/command.h>
|
|
#include <VasEBoot/mm.h>
|
|
|
|
VAS_EBOOT_MOD_LICENSE ("GPLv3+");
|
|
|
|
VasEBoot_err_t
|
|
VasEBoot_crypto_get_random (void *buffer, VasEBoot_size_t sz)
|
|
{
|
|
/* This is an arbitrer between different methods.
|
|
TODO: Add more methods in the future. */
|
|
/* TODO: Add some PRNG smartness to reduce damage from bad entropy. */
|
|
if (VasEBoot_crypto_arch_get_random (buffer, sz))
|
|
return VAS_EBOOT_ERR_NONE;
|
|
return VasEBoot_error (VAS_EBOOT_ERR_IO, "no random sources found");
|
|
}
|
|
|
|
static int
|
|
get_num_digits (int val)
|
|
{
|
|
int ret = 0;
|
|
while (val != 0)
|
|
{
|
|
ret++;
|
|
val /= 10;
|
|
}
|
|
if (ret == 0)
|
|
return 1;
|
|
return ret;
|
|
}
|
|
|
|
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
|
|
|
|
static VasEBoot_err_t
|
|
VasEBoot_cmd_hexdump_random (VasEBoot_command_t cmd __attribute__ ((unused)), int argc, char **args)
|
|
{
|
|
VasEBoot_size_t length = 64;
|
|
VasEBoot_err_t err;
|
|
void *buffer;
|
|
VasEBoot_uint8_t *ptr;
|
|
int stats[256];
|
|
int i, digits = 2;
|
|
char template[10];
|
|
|
|
if (argc >= 1)
|
|
length = VasEBoot_strtoull (args[0], 0, 0);
|
|
|
|
if (length == 0)
|
|
return VasEBoot_error (VAS_EBOOT_ERR_BAD_ARGUMENT, "length pust be positive");
|
|
|
|
buffer = VasEBoot_malloc (length);
|
|
if (!buffer)
|
|
return VasEBoot_errno;
|
|
|
|
err = VasEBoot_crypto_get_random (buffer, length);
|
|
if (err)
|
|
{
|
|
VasEBoot_free (buffer);
|
|
return err;
|
|
}
|
|
|
|
hexdump (0, buffer, length);
|
|
VasEBoot_memset(stats, 0, sizeof(stats));
|
|
for (ptr = buffer; ptr < (VasEBoot_uint8_t *) buffer + length; ptr++)
|
|
stats[*ptr]++;
|
|
VasEBoot_printf ("Statistics:\n");
|
|
for (i = 0; i < 256; i++)
|
|
{
|
|
int z = get_num_digits (stats[i]);
|
|
if (z > digits)
|
|
digits = z;
|
|
}
|
|
|
|
VasEBoot_snprintf (template, sizeof (template), "%%0%dd ", digits);
|
|
|
|
for (i = 0; i < 256; i++)
|
|
{
|
|
VasEBoot_printf ("%s", template);//, stats[i]);
|
|
if ((i & 0xf) == 0xf)
|
|
VasEBoot_printf ("\n");
|
|
}
|
|
|
|
VasEBoot_free (buffer);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static VasEBoot_command_t cmd;
|
|
|
|
VAS_EBOOT_MOD_INIT (random)
|
|
{
|
|
cmd = VasEBoot_register_command ("hexdump_random", VasEBoot_cmd_hexdump_random,
|
|
N_("[LENGTH]"),
|
|
N_("Hexdump random data."));
|
|
}
|
|
|
|
VAS_EBOOT_MOD_FINI (random)
|
|
{
|
|
VasEBoot_unregister_command (cmd);
|
|
}
|