Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
+#ifdef WIN32
+#include <windows.h>
+#include <wincrypt.h>
+#endif
+
#include "d0_bignum.h"
#include <gmp.h>
#include <time.h>
#include <stdio.h>
+
void d0_bignum_INITIALIZE(void)
{
FILE *f;
+ unsigned char buf[256];
d0_bignum_init(&temp);
gmp_randinit_mt(RANDSTATE);
gmp_randseed_ui(RANDSTATE, time(NULL));
+ * (time_t *) (&buf[0]) = time(0); // if everything else fails, we use the current time + uninitialized data
+#ifdef WIN32
+ {
+ HCRYPTPROV hCryptProv;
+ if(CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
+ {
+ if(!CryptGenRandom(hCryptProv, sizeof(buf), (PBYTE) &buf[0]))
+ fprintf(stderr, "WARNING: could not initialize random number generator (CryptGenRandom failed)\n");
+ }
+ else
+ fprintf(stderr, "WARNING: could not initialize random number generator (CryptAcquireContext failed)\n");
+ }
+#else
f = fopen("/dev/urandom", "rb");
if(!f)
f = fopen("/dev/random", "rb");
if(f)
{
- unsigned char buf[256];
setbuf(f, NULL);
- if(fread(buf, sizeof(buf), 1, f) == 1)
- {
- mpz_import(temp.z, sizeof(buf), 1, 1, 0, 0, buf);
- gmp_randseed(RANDSTATE, temp.z);
- }
+ if(fread(buf, sizeof(buf), 1, f) != 1)
+ fprintf(stderr, "WARNING: could not initialize random number generator (read from random device failed)\n");
fclose(f);
}
+ else
+ fprintf(stderr, "WARNING: could not initialize random number generator (no random device found)\n");
+#endif
+
+ mpz_import(temp.z, sizeof(buf), 1, 1, 0, 0, buf);
+ gmp_randseed(RANDSTATE, temp.z);
}
void d0_bignum_SHUTDOWN(void)
#define CHECK(x) do { if(!(x)) goto fail; } while(0)
#define CHECK_ASSIGN(var, value) do { d0_bignum_t *val; val = value; if(!val) goto fail; var = val; } while(0)
+#define MPCHECK(x) do { if(!failed) if(!(x)) failed = 1; } while(0)
+#define MPCHECK_ASSIGN(var, value) do { if(!failed) { d0_bignum_t *val; val = value; if(val) var = val; else failed = 1; } } while(0)
#define USING(x) if(!(ctx->x)) return 0
#define REPLACING(x)
static unsigned char convbuf[1024];
d0_iobuf_t *conv = NULL;
size_t sz = 0;
+ BOOL failed = 0;
// temps: temp0 order, temp0 4^r
if(is_first)
// generate random number r; x = g^r; send hash of x, remember r, forget x
CHECK(d0_dl_get_order(temp0, ctx->schnorr_G));
CHECK_ASSIGN(ctx->r, d0_bignum_rand_range(ctx->r, zero, temp0));
- CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
+ //CHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
// initialize Signed Diffie Hellmann
- CHECK(d0_dl_get_order(temp1, ctx->schnorr_G));
- CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp1));
- CHECK_ASSIGN(ctx->g_to_t, d0_bignum_mod_pow(ctx->g_to_t, four, ctx->t, ctx->schnorr_G));
+ // we already have the group order in temp1
+ CHECK_ASSIGN(ctx->t, d0_bignum_rand_range(ctx->t, zero, temp0));
// can we SOMEHOW do this with just one mod_pow?
+#pragma omp parallel default(shared) reduction(||:failed)
+#pragma omp sections
+ {
+#pragma omp section
+ {
+ MPCHECK(d0_bignum_mod_pow(temp0, four, ctx->r, ctx->schnorr_G));
+ }
+#pragma omp section
+ {
+ MPCHECK_ASSIGN(ctx->g_to_t, d0_bignum_mod_pow(ctx->g_to_t, four, ctx->t, ctx->schnorr_G));
+ }
+ }
+ CHECK(!failed);
+
// hash it, hash it, everybody hash it
conv = d0_iobuf_open_write(convbuf, sizeof(convbuf));
CHECK(d0_iobuf_write_bignum(conv, temp0));