From 3629f844a6bab0fa1e18534aefe1a25cbe016bdb Mon Sep 17 00:00:00 2001 From: Rudolf Polzer Date: Sat, 24 Apr 2010 21:54:31 +0200 Subject: [PATCH] visibility; proper random seed --- Makefile.am | 1 + d0.h | 1 + d0_bignum-gmp.c | 27 ++++++++++++++++++++------ d0_blind_id.h | 50 ++++++++++++++++++++++++------------------------- 4 files changed, 48 insertions(+), 31 deletions(-) diff --git a/Makefile.am b/Makefile.am index 503c946..3d12334 100644 --- a/Makefile.am +++ b/Makefile.am @@ -5,6 +5,7 @@ blind_id_LDADD = libblind_id.la lib_LTLIBRARIES = libblind_id.la libblind_id_la_SOURCES = d0_bignum-gmp.c d0_blind_id.c d0.c d0_iobuf.c sha1.c libblind_id_la_LDFLAGS = -versioninfo 0:0:0 +libblind_id_la_CFLAGS = -fvisibility=hidden # versioninfo: # - compatible interface change: c:r:a -> c+1:0:a+1 # - incompatible interface change: c:r:a -> c+1:0:0 diff --git a/d0.h b/d0.h index c792430..270cc8b 100644 --- a/d0.h +++ b/d0.h @@ -3,6 +3,7 @@ #include // size_t +#define EXPORT __attribute__((__visibility__("default"))) #define WARN_UNUSED_RESULT __attribute__((warn_unused_result)) #define BOOL int diff --git a/d0_bignum-gmp.c b/d0_bignum-gmp.c index b556b81..87aa0b8 100644 --- a/d0_bignum-gmp.c +++ b/d0_bignum-gmp.c @@ -27,17 +27,33 @@ struct d0_bignum_s }; static gmp_randstate_t RANDSTATE; +static d0_bignum_t temp; #include +#include void d0_bignum_INITIALIZE() { - gmp_randinit_default(RANDSTATE); - gmp_randseed_ui(RANDSTATE, time(NULL)); // TODO seed + FILE *f; + d0_bignum_init(&temp); + gmp_randinit_mt(RANDSTATE); + gmp_randseed_ui(RANDSTATE, time(NULL)); + f = fopen("/dev/random", "rb"); + if(f) + { + unsigned char buf[256]; + if(fread(buf, sizeof(buf), 1, f) == 1) + { + mpz_import(temp.z, sizeof(buf), 1, 1, 0, 0, buf); + gmp_randseed(RANDSTATE, temp.z); + } + fclose(f); + } } void d0_bignum_SHUTDOWN() { - // free RANDSTATE + d0_bignum_clear(&temp); + gmp_randclear(RANDSTATE); } BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum) @@ -112,10 +128,9 @@ int d0_bignum_cmp(const d0_bignum_t *a, const d0_bignum_t *b) d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max) { - static d0_bignum_t *temp = NULL; if(!temp) temp = d0_bignum_new(); if(!r) r = d0_bignum_new(); if(!r) return NULL; - mpz_sub(temp->z, max->z, min->z); - mpz_urandomm(r->z, RANDSTATE, temp->z); + mpz_sub(temp.z, max->z, min->z); + mpz_urandomm(r->z, RANDSTATE, temp.z); mpz_add(r->z, r->z, min->z); return r; } diff --git a/d0_blind_id.h b/d0_blind_id.h index ed5bcda..485d1a9 100644 --- a/d0_blind_id.h +++ b/d0_blind_id.h @@ -5,30 +5,30 @@ typedef struct d0_blind_id_s d0_blind_id_t; -WARN_UNUSED_RESULT d0_blind_id_t *d0_blind_id_new(); -void d0_blind_id_free(d0_blind_id_t *a); -void d0_blind_id_clear(d0_blind_id_t *ctx); -void d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src); -WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_keys(d0_blind_id_t *ctx, int k); -WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_keys(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen); -WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_keys(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen); -WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_keys(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen); -WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_keys(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen); -WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx); -WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen); -WARN_UNUSED_RESULT BOOL d0_blind_id_answer_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen); -WARN_UNUSED_RESULT BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen); -WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen); -WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen); -WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen); -WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen); -WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_start(d0_blind_id_t *ctx, int is_first, char *message, size_t msglen, char *outbuf, size_t *outbuflen); -WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_challenge(d0_blind_id_t *ctx, int is_first, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen); -WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_response(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen); -WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_verify(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *msg, ssize_t *msglen); -WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen); - -void d0_blind_id_INITIALIZE(); -void d0_blind_id_SHUTDOWN(); +EXPORT WARN_UNUSED_RESULT d0_blind_id_t *d0_blind_id_new(); +EXPORT void d0_blind_id_free(d0_blind_id_t *a); +EXPORT void d0_blind_id_clear(d0_blind_id_t *ctx); +EXPORT void d0_blind_id_copy(d0_blind_id_t *ctx, const d0_blind_id_t *src); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_keys(d0_blind_id_t *ctx, int k); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_keys(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_keys(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_keys(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_keys(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_start(d0_blind_id_t *ctx); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_generate_private_id_request(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_answer_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_finish_private_id_request(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_read_private_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_read_public_id(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_write_private_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_write_public_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_start(d0_blind_id_t *ctx, int is_first, char *message, size_t msglen, char *outbuf, size_t *outbuflen); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_challenge(d0_blind_id_t *ctx, int is_first, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_response(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *outbuf, size_t *outbuflen); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_authenticate_with_private_id_verify(d0_blind_id_t *ctx, const char *inbuf, size_t inbuflen, char *msg, ssize_t *msglen); +EXPORT WARN_UNUSED_RESULT BOOL d0_blind_id_fingerprint64_public_id(d0_blind_id_t *ctx, char *outbuf, size_t *outbuflen); +EXPORT +EXPORT void d0_blind_id_INITIALIZE(); +EXPORT void d0_blind_id_SHUTDOWN(); #endif -- 2.39.2