From 3d5ada60c3d11b68cc74b2ae3a001694ac2cc17e Mon Sep 17 00:00:00 2001 From: Giovanni Bajo Date: Tue, 27 Sep 2011 01:57:28 +0200 Subject: [PATCH] Avoid writing directly into fp_digits through char*. There are potential issues with weird platforms were not all bits of a fp_digit are used (padding bits), plus it makes it harder to do reproducible testing of algorithms. --- src/numtheory/fp_prime_random_ex.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/numtheory/fp_prime_random_ex.c b/src/numtheory/fp_prime_random_ex.c index 0c4b479..8ade165 100644 --- a/src/numtheory/fp_prime_random_ex.c +++ b/src/numtheory/fp_prime_random_ex.c @@ -19,7 +19,8 @@ int fp_prime_random_ex(fp_int *a, int size, int flags, tfm_prime_callback cb, void *dat) { fp_digit maskAND_msb, maskOR_lsb; - int res, err, bsize, dsize; + int res, bsize, dsize; + unsigned char buf[FP_SIZE * sizeof(fp_digit)]; /* sanity check the input */ if (size <= 1) { @@ -33,6 +34,7 @@ int fp_prime_random_ex(fp_int *a, int size, int flags, tfm_prime_callback cb, vo /* calc the digit size */ dsize = (size + DIGIT_BIT - 1) >> DIGIT_SHIFT; + bsize = (size + 7) >> 3; /* calc the maskAND value for the MSbyte */ maskAND_msb = FP_MASK >> ((DIGIT_BIT - (size & (DIGIT_BIT-1))) & (DIGIT_BIT-1)); @@ -45,10 +47,10 @@ int fp_prime_random_ex(fp_int *a, int size, int flags, tfm_prime_callback cb, vo do { /* read the bytes */ - if (cb((unsigned char*)&a->dp[0], dsize*DIGIT_BIT, dat) != dsize*DIGIT_BIT) { + if (cb(buf, bsize, dat) != bsize) { return FP_VAL; } - a->used = dsize; + fp_read_unsigned_bin(a, buf, bsize); /* make sure the MSbyte has the required number of bits */ a->dp[dsize-1] &= maskAND_msb;