X-Git-Url: http://git.xonotic.org/?a=blobdiff_plain;f=d0_bignum-tommath.c;h=75392a48ec7529f0d671d0863b6dbb74fa2c1119;hb=754d6430131e6845d7d0a6130b31305bb5f895f1;hp=d28a56fc0cb31765b08678de60755cb869828645;hpb=17862d3aa240f8e2203dad321db501e78dec016a;p=xonotic%2Fd0_blind_id.git diff --git a/d0_bignum-tommath.c b/d0_bignum-tommath.c index d28a56f..75392a4 100644 --- a/d0_bignum-tommath.c +++ b/d0_bignum-tommath.c @@ -51,6 +51,8 @@ struct d0_bignum_s }; static d0_bignum_t temp; +static unsigned char numbuf[65536]; +static void *tempmutex = NULL; // hold this mutex when using temp or numbuf #include @@ -75,6 +77,10 @@ D0_WARN_UNUSED_RESULT D0_BOOL d0_bignum_INITIALIZE(void) { D0_BOOL ret = 1; unsigned char buf[256]; + + tempmutex = d0_createmutex(); + d0_lockmutex(tempmutex); + d0_bignum_init(&temp); #ifdef WIN32 { @@ -106,11 +112,15 @@ D0_WARN_UNUSED_RESULT D0_BOOL d0_bignum_INITIALIZE(void) } #endif + d0_unlockmutex(tempmutex); + return ret; } void d0_bignum_SHUTDOWN(void) { + d0_lockmutex(tempmutex); + d0_bignum_clear(&temp); #ifdef WIN32 if(hCryptProv) @@ -119,32 +129,55 @@ void d0_bignum_SHUTDOWN(void) hCryptProv = NULL; } #endif + + d0_unlockmutex(tempmutex); + d0_destroymutex(tempmutex); + tempmutex = NULL; } D0_BOOL d0_iobuf_write_bignum(d0_iobuf_t *buf, const d0_bignum_t *bignum) { - static unsigned char numbuf[65536]; + D0_BOOL ret; size_t count = 0; + + d0_lockmutex(tempmutex); numbuf[0] = (mp_iszero(&bignum->z) ? 0 : (bignum->z.sign == MP_ZPOS) ? 1 : 3); if((numbuf[0] & 3) != 0) // nonzero { count = mp_unsigned_bin_size((mp_int *) &bignum->z); if(count > sizeof(numbuf) - 1) + { + d0_unlockmutex(tempmutex); return 0; + } mp_to_unsigned_bin((mp_int *) &bignum->z, numbuf+1); } - return d0_iobuf_write_packet(buf, numbuf, count + 1); + ret = d0_iobuf_write_packet(buf, numbuf, count + 1); + d0_unlockmutex(tempmutex); + return ret; } d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum) { - static unsigned char numbuf[65536]; size_t count = sizeof(numbuf); + d0_lockmutex(tempmutex); if(!d0_iobuf_read_packet(buf, numbuf, &count)) + { + d0_unlockmutex(tempmutex); return NULL; + } if(count < 1) + { + d0_unlockmutex(tempmutex); return NULL; - if(!bignum) bignum = d0_bignum_new(); if(!bignum) return NULL; + } + if(!bignum) + bignum = d0_bignum_new(); + if(!bignum) + { + d0_unlockmutex(tempmutex); + return NULL; + } if(numbuf[0] & 3) // nonzero { mp_read_unsigned_bin(&bignum->z, numbuf+1, count-1); @@ -155,6 +188,7 @@ d0_bignum_t *d0_iobuf_read_bignum(d0_iobuf_t *buf, d0_bignum_t *bignum) { mp_zero(&bignum->z); } + d0_unlockmutex(tempmutex); return bignum; } @@ -246,45 +280,69 @@ static d0_bignum_t *d0_bignum_rand_0_to_limit(d0_bignum_t *r, const d0_bignum_t size_t n = d0_bignum_size(limit); size_t b = (n + 7) / 8; unsigned char mask = "\xFF\x7F\x3F\x1F\x0F\x07\x03\x01"[8*b - n]; - unsigned char numbuf[65536]; assert(b <= sizeof(numbuf)); + d0_lockmutex(tempmutex); for(;;) { rand_bytes(numbuf, b); numbuf[0] &= mask; r = d0_bignum_import_unsigned(r, numbuf, b); if(d0_bignum_cmp(r, limit) < 0) + { + d0_unlockmutex(tempmutex); return r; + } } } d0_bignum_t *d0_bignum_rand_range(d0_bignum_t *r, const d0_bignum_t *min, const d0_bignum_t *max) { + d0_lockmutex(tempmutex); mp_sub((mp_int *) &max->z, (mp_int *) &min->z, &temp.z); r = d0_bignum_rand_0_to_limit(r, &temp); + d0_unlockmutex(tempmutex); mp_add((mp_int *) &r->z, (mp_int *) &min->z, &r->z); return r; } d0_bignum_t *d0_bignum_rand_bit_atmost(d0_bignum_t *r, size_t n) { + d0_lockmutex(tempmutex); if(!d0_bignum_one(&temp)) + { + d0_unlockmutex(tempmutex); return NULL; + } if(!d0_bignum_shl(&temp, &temp, n)) + { + d0_unlockmutex(tempmutex); return NULL; + } r = d0_bignum_rand_0_to_limit(r, &temp); + d0_unlockmutex(tempmutex); return r; } d0_bignum_t *d0_bignum_rand_bit_exact(d0_bignum_t *r, size_t n) { + d0_lockmutex(tempmutex); if(!d0_bignum_one(&temp)) + { + d0_unlockmutex(tempmutex); return NULL; + } if(!d0_bignum_shl(&temp, &temp, n-1)) + { + d0_unlockmutex(tempmutex); return NULL; + } r = d0_bignum_rand_0_to_limit(r, &temp); if(!d0_bignum_add(r, r, &temp)) + { + d0_unlockmutex(tempmutex); return NULL; + } + d0_unlockmutex(tempmutex); return r; } @@ -425,7 +483,10 @@ d0_bignum_t *d0_bignum_gcd(d0_bignum_t *r, d0_bignum_t *s, d0_bignum_t *t, const char *d0_bignum_tostring(const d0_bignum_t *x, unsigned int base) { - static char str[65536]; - mp_toradix_n((mp_int *) &x->z, str, base, sizeof(str)); + char *str; + int sz = 0; + mp_radix_size((mp_int *) &x->z, base, &sz); + str = d0_malloc(sz + 1); + mp_toradix_n((mp_int *) &x->z, str, base, sz + 1); return str; }