From 3b95766a2dda5aadd6cb49facb6d36cd6b4b7025 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Thu, 10 May 2012 14:40:15 +0200 Subject: [PATCH] some clean up --- Makefile.am | 4 +- gentoo/ChangeLog | 8 ++ gentoo/metadata.xml | 5 ++ gentoo/mod_entropy-9999.ebuild | 22 ++++++ mod_entropy.c | 129 ++++++--------------------------- mod_entropy_add_entropy.c | 102 ++++++++++++++++++++++++++ mod_entropy_get_entropy_bits.c | 77 ++++++++++++++++++++ 7 files changed, 240 insertions(+), 107 deletions(-) create mode 100644 gentoo/ChangeLog create mode 100644 gentoo/metadata.xml create mode 100644 gentoo/mod_entropy-9999.ebuild create mode 100644 mod_entropy_add_entropy.c create mode 100644 mod_entropy_get_entropy_bits.c diff --git a/Makefile.am b/Makefile.am index dbec51f..325f65e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,7 +2,9 @@ ACLOCAL_AMFLAGS = -I m4 lib_LTLIBRARIES = libmodentropy.la -libmodentropy_la_SOURCES = mod_entropy.c +libmodentropy_la_SOURCES = mod_entropy.c \ + mod_entropy_get_entropy_bits.c \ + mod_entropy_add_entropy.c libmodentropy_la_LDFLAGS = -lrt -lm install: libmodentropy.la diff --git a/gentoo/ChangeLog b/gentoo/ChangeLog new file mode 100644 index 0000000..4d2f3c4 --- /dev/null +++ b/gentoo/ChangeLog @@ -0,0 +1,8 @@ +# ChangeLog for www-apache/mod_tidy +# Copyright 1999-2009 Gentoo Foundation; Distributed under the GPL v2 + +*mod_entropy-0.1.0 (10 May 2012) + + 10 May 2012; Georg Hopp +metadata.xml, +mod_entropy-9999.ebuild: + initial version + diff --git a/gentoo/metadata.xml b/gentoo/metadata.xml new file mode 100644 index 0000000..64a719e --- /dev/null +++ b/gentoo/metadata.xml @@ -0,0 +1,5 @@ + + + + apache + diff --git a/gentoo/mod_entropy-9999.ebuild b/gentoo/mod_entropy-9999.ebuild new file mode 100644 index 0000000..5df8898 --- /dev/null +++ b/gentoo/mod_entropy-9999.ebuild @@ -0,0 +1,22 @@ +EAPI=4 + +inherit eutils git autotools apache-module + +DESCRIPTION="a module to greate random data from incoming requests." +SRC_URI="" +EGIT_REPO_URI="git://redminie.weird-web-workers.org/var/lib/git/mod_entropy" + +HOMEPAGE="http://redmine.weird-web-workers.org/mod_entropy/" + +LICENSE="GPL-3" +SLOT="0" +KEYWORDS="~amd64 ~x86" +IUSE="" + +need_apache2 + +DOCFILES="COPYING README NEWS ChangeLog" + +src_prepare() { + eautoreconf +} diff --git a/mod_entropy.c b/mod_entropy.c index 48b66cd..c1c973b 100644 --- a/mod_entropy.c +++ b/mod_entropy.c @@ -1,4 +1,6 @@ /** + * \file + * * this filter generates a sha1 from the current microtime and request * useses this to fill the linux random source. * @@ -9,8 +11,22 @@ * * Most time was spend in figuring out how to write apache modules. * - * \author Georg Hopp + * \author Georg Hopp . */ + #define _POSIX_C_SOURCE 199309L #include "httpd.h" @@ -24,67 +40,16 @@ #include "apr_sha1.h" #include -#include -#include -#include -#include -#include -#include -#include -#define min(x, y) ((x)<(y)?(x):(y)) +int add_entropy(unsigned char *, size_t); module AP_MODULE_DECLARE_DATA entropy_module; -char * getData(const char *, size_t); /** - * This is taken from timer_entropyd and modified so - * that the constant 1/log(2.0) is not calculated but - * set directly. - * - * As far as i can say this correlates to the shannon - * entropy algorithm with equal probabilities - * for entropy where the entropy units are bits. - * - * But actually i am no mathemacian and my analysis capabilities - * are limited. Additionally i have not analysed the linux random - * character device code, so i trusted the code in timer_entropyd. + * add header values to sha1 */ -static -int -get_entropy(const unsigned char * data, size_t ndata) -{ - size_t byte_count[256]; - size_t iterator; - static double log2inv = 1.442695; //!< 1 / log(2.0): the entropy unit size - double entropy = 0.0; - - memset(byte_count, 0, sizeof(byte_count)); - - /** - * first get the amount each byte occurs in the array - */ - for (iterator = 0; iterator < ndata; iterator++) { - byte_count[data[iterator]]++; - } - - for (iterator = 0; iterator < 256; iterator++) { - double probability = (double)byte_count[iterator] / (double)ndata; - - if (0.0 < probability) { - entropy += probability * log2inv * (log(1.0 / probability)); - } - } - - entropy *= (double)ndata; - entropy = (entropy < 0.0)? 0.0 : entropy; - entropy = min((double)(ndata * 8), entropy); - - return entropy; -} - static int header_do_print(void * rec, const char * key, const char * value) @@ -183,59 +148,11 @@ entropy_filter_in( /** * fill /dev/random with sha1 from current request */ - { - int i; - int entropy = get_entropy(digest, APR_SHA1_DIGESTSIZE); - int fd = open("/dev/random", O_WRONLY|O_NONBLOCK); - struct rand_pool_info * output; - - output = (struct rand_pool_info *)malloc( - sizeof(struct rand_pool_info) + APR_SHA1_DIGESTSIZE); - - output->entropy_count = entropy; - output->buf_size = APR_SHA1_DIGESTSIZE; - memcpy(output->buf, digest, APR_SHA1_DIGESTSIZE); - - fprintf(stderr, "sha1 so far: "); - for (i=0; i. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +int get_entropy_bits(unsigned char *, size_t); + +/** + * fill /dev/random with sha1 from current request + * + * \todo add error handling... + */ +void +add_entropy(const unsigned char * data, size_t ndata) +{ + int i; + int fd; + int entropy = get_entropy_bits(data, ndata); + struct rand_pool_info * output; + + output = (struct rand_pool_info *)malloc( + sizeof(struct rand_pool_info) + ndata); + + output->entropy_count = entropy; + output->buf_size = ndata; + memcpy(output->buf, data, ndata); + + fd = open("/dev/random", O_WRONLY|O_NONBLOCK); + + if (ioctl(fd, RNDADDENTROPY, output) == -1) { + switch(errno) { + case EBADF: + fprintf(stderr, + "ioctl failed: no valid file descriptor %d\n", + fd); + break; + + case EFAULT: + fprintf(stderr, + "ioctl failed: invalid argument: %p\n", + output); + break; + + case EINVAL: + fprintf(stderr, + "ioctl failed: invalid request\n", + errno); + break; + + case ENOTTY: + fprintf(stderr, + "ioctl failed: discriptor not associated to " + "character device\n", + errno); + break; + + case EPERM: + fprintf(stderr, + "ioctl failed: invalid permissions\n", + errno); + break; + + default: + fprintf(stderr, + "ioctl(RNDADDENTROPY) failed: %d\n", + errno); + break; + } + } + + fflush(stderr); + free(output); + close(fd); +} + +// vim: set ts=4 sw=4: diff --git a/mod_entropy_get_entropy_bits.c b/mod_entropy_get_entropy_bits.c new file mode 100644 index 0000000..54e73bf --- /dev/null +++ b/mod_entropy_get_entropy_bits.c @@ -0,0 +1,77 @@ +/** + * \file + * + * calculate the available entropy. This is taken from timed_entropyd. + * + * \author Georg Hopp . + */ + +#include + +#define min(x, y) ((x)<(y)?(x):(y)) + +/** + * This is taken from timer_entropyd and modified so + * that the constant 1/log(2.0) is not calculated but + * set directly. + * + * As far as i can say this correlates to the shannon + * entropy algorithm with equal probabilities + * for entropy where the entropy units are bits. + * + * But actually i am no mathemacian and my analysis capabilities + * are limited. Additionally i have not analysed the linux random + * character device code, so i trusted the code in timer_entropyd. + */ +int +get_entropy_bits(const unsigned char * data, size_t ndata) +{ + size_t byte_count[256]; + size_t iterator; + static double log2inv = 1.442695; //!< 1 / log(2.0): the entropy unit size + double entropy = 0.0; + + memset(byte_count, 0, sizeof(byte_count)); + + /** + * first get the amount each byte occurs in the array + */ + for (iterator = 0; iterator < ndata; iterator++) { + byte_count[data[iterator]]++; + } + + /** + * calculate the entropy value + */ + for (iterator = 0; iterator < 256; iterator++) { + double probability = (double)byte_count[iterator] / (double)ndata; + + if (0.0 < probability) { + entropy += probability * log2inv * (log(1.0 / probability)); + } + } + + /** + * prepare for use with linux kernel ioctl RNDADDENTROPY + */ + entropy *= (double)ndata; + entropy = (entropy < 0.0)? 0.0 : entropy; + entropy = min((double)(ndata * 8), entropy); + + return entropy; +} + +// vim: set ts=4 sw=4: