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.
This commit is contained in:
Giovanni Bajo 2011-09-27 01:57:28 +02:00
parent d1dee8c6e1
commit 3d5ada60c3

View File

@ -19,7 +19,8 @@
int fp_prime_random_ex(fp_int *a, int size, int flags, tfm_prime_callback cb, void *dat) int fp_prime_random_ex(fp_int *a, int size, int flags, tfm_prime_callback cb, void *dat)
{ {
fp_digit maskAND_msb, maskOR_lsb; 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 */ /* sanity check the input */
if (size <= 1) { 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 */ /* calc the digit size */
dsize = (size + DIGIT_BIT - 1) >> DIGIT_SHIFT; dsize = (size + DIGIT_BIT - 1) >> DIGIT_SHIFT;
bsize = (size + 7) >> 3;
/* calc the maskAND value for the MSbyte */ /* calc the maskAND value for the MSbyte */
maskAND_msb = FP_MASK >> ((DIGIT_BIT - (size & (DIGIT_BIT-1))) & (DIGIT_BIT-1)); 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 { do {
/* read the bytes */ /* 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; return FP_VAL;
} }
a->used = dsize; fp_read_unsigned_bin(a, buf, bsize);
/* make sure the MSbyte has the required number of bits */ /* make sure the MSbyte has the required number of bits */
a->dp[dsize-1] &= maskAND_msb; a->dp[dsize-1] &= maskAND_msb;