Commit 1051ca08 authored by Niels Möller's avatar Niels Möller
Browse files

(update_zlib): New function.

(get_dev_mem): Implemented reading of /dev/mem.

Rev: src/lsh-make-seed.c:1.4
parent 74504dbb
......@@ -43,6 +43,29 @@
#include <unistd.h>
#endif
#if WITH_ZLIB
#if HAVE_ZLIB_H
#include <zlib.h>
#endif
/* FIXME: Duplicated in zlib.c */
/* zlib memory functions */
static void *
zlib_alloc(void *opaque UNUSED, unsigned int items, unsigned int size)
{
return lsh_space_alloc(items * size);
}
static void
zlib_free(void *opaque UNUSED, void *address)
{
lsh_space_free(address);
}
#endif /* WITH_ZLIB */
#include "lsh-make-seed.c.x"
/* Option parsing */
......@@ -187,7 +210,7 @@ get_dev_random(struct yarrow256_ctx *ctx, enum source_type source)
if (fd < 0)
return;
verbose("Reading %z\n", names[i]);
verbose("Reading %z...\n", names[i]);
do
{ res = read(fd, buffer, DEVRANDOM_SIZE); }
......@@ -200,7 +223,8 @@ get_dev_random(struct yarrow256_ctx *ctx, enum source_type source)
else if (res > 0)
{
/* Count 4 bits of entropy for each byte. */
verbose("Read %i bytes from /dev/random.\n",
res);
yarrow256_update(ctx, source, res * 4, res, buffer);
}
else
......@@ -210,9 +234,132 @@ get_dev_random(struct yarrow256_ctx *ctx, enum source_type source)
close(fd);
}
#define BUF_SIZE 16384
#if WITH_ZLIB
static int
update_zlib(struct yarrow256_ctx *ctx, enum source_type source,
z_stream *z, int flush,
unsigned length, uint8_t *buf)
{
/* Maximum expansion is 0.1% + 12 bytes. We use 1% + 12, to be
* conservative.
*
* FIXME: These figures are documented for the entire stream,
* does they really apply to all segments? */
uint8_t out[BUF_SIZE + BUF_SIZE / 100 + 12];
unsigned compressed;
int rc;
z->next_in = buf;
z->avail_in = length;
z->next_out = out;
z->avail_out = sizeof(out);
if ((rc = deflate(z, flush)) != Z_OK)
{
werror("deflate failed: %z\n",
z->msg ? z->msg : "No error(?)");
werror("rc = %i, flush = %i, avail_in = %i, avail_out = %i\n",
rc, flush, z->avail_in, z->avail_out);
return 0;
}
compressed = z->next_out - out;
yarrow256_update(ctx, source, compressed / 1024, compressed, out);
return 1;
}
#endif /* WITH_ZLIB */
static void
get_dev_mem(struct yarrow256_ctx *ctx, enum source_type source)
{
/* /dev/mem provides access to physical memory, if we
* have enough privileges.
*
* Count 1 bit of entropy compressed KB, or per 10 KB uncompressed.
*/
int fd = open("/dev/mem", O_RDONLY);
if (fd < 0)
{
if (!getuid() || (errno != EPERM))
werror("Couldn't open /dev/mem (errno = %i): %z\n",
errno, STRERROR(errno));
}
else
{
char buf[BUF_SIZE];
size_t total_read = 0;
#if WITH_ZLIB
z_stream z;
z.zalloc = zlib_alloc;
z.zfree = zlib_free;
if (deflateInit(&z, Z_DEFAULT_COMPRESSION) != Z_OK)
{
werror("deflateInit failed: %z\n",
z.msg ? z.msg : "No error(?)");
close(fd);
return;
}
#endif /* WITH_ZLIB */
werror("Reading /dev/mem...\n");
for (;;)
{
int res;
do
res = read(fd, buf, BUF_SIZE);
while ( (res < 0) && (errno != EINTR));
if (!res)
break;
if (res < 0)
{
werror("Reading /dev/mem failed (errno = %i): %z\n",
errno, STRERROR(errno));
break;
}
total_read += res;
#if WITH_ZLIB
if (!update_zlib(ctx, source,
&z, 0,
res, buf))
break;
#else /* !WITH_ZLIB */
yarrow256_update(ctx, source, res / 10240, res, buf);
#endif /* !WITH_ZLIB */
}
#if WITH_ZLIB
update_zlib(ctx, source,
&z, Z_FINISH,
0, buf);
verbose("Read %i MB from /dev/mem, %i MB after compression.\n",
total_read >> 20, z.total_out >> 20);
deflateEnd(&z);
#else /* !WITH_ZLIB */
verbose("Read %i MB from /dev/mem.\n",
total_read >> 20);
#endif /* !WITH_ZLIB */
close(fd);
}
}
static void
......@@ -222,6 +369,7 @@ get_system(struct yarrow256_ctx *ctx, enum source_type source)
* misc/rndunix.c. <URL:
* http://www.cs.auckland.ac.nz/~pgut001/cryptlib/> */
werror("Reading system state...\n");
#if 0
static struct RI {
const char *path; /* Path to check for existence of source */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment