Browse Source

Fix returning too many or too few bits

original patch by @friedrichsenm

This closes #543
Steffen Jaeckel 5 years ago
parent
commit
696e781d41
1 changed files with 2 additions and 2 deletions
  1. 2 2
      src/math/rand_bn.c

+ 2 - 2
src/math/rand_bn.c

@@ -19,7 +19,7 @@ int rand_bn_bits(void *N, int bits, prng_state *prng, int wprng)
    if ((res = prng_is_valid(wprng)) != CRYPT_OK) return res;
    if ((res = prng_is_valid(wprng)) != CRYPT_OK) return res;
 
 
    bytes = (bits+7) >> 3;
    bytes = (bits+7) >> 3;
-   mask = 0xff << (8 - bits % 8);
+   mask = 0xff >> (bits % 8 == 0 ? 0 : 8 - bits % 8);
 
 
    /* allocate buffer */
    /* allocate buffer */
    if ((buf = XCALLOC(1, bytes)) == NULL) return CRYPT_MEM;
    if ((buf = XCALLOC(1, bytes)) == NULL) return CRYPT_MEM;
@@ -30,7 +30,7 @@ int rand_bn_bits(void *N, int bits, prng_state *prng, int wprng)
       goto cleanup;
       goto cleanup;
    }
    }
    /* mask bits */
    /* mask bits */
-   buf[0] &= ~mask;
+   buf[0] &= mask;
    /* load value */
    /* load value */
    if ((res = mp_read_unsigned_bin(N, buf, bytes)) != CRYPT_OK) goto cleanup;
    if ((res = mp_read_unsigned_bin(N, buf, bytes)) != CRYPT_OK) goto cleanup;