*
*/
+#define OPENSSL_FIPSAPI
+
#include <stdio.h>
#include <time.h>
#include "cryptlib.h"
#include "bn_lcl.h"
#include <openssl/rand.h>
-#define OPENSSL_FIPSAPI
-#ifdef OPENSSL_FIPS
-#include <openssl/fips.h>
-#endif
-
static int bnrand(int pseudorand, BIGNUM *rnd, int bits, int top, int bottom)
{
unsigned char *buf=NULL;
if (n == 1)
BN_zero(r);
+#ifdef OPENSSL_FIPS
+ /* FIPS 186-3 is picky about how random numbers for keys etc are
+ * generated. So we just use the second case which is equivalent to
+ * "Generation by Testing Candidates" mentioned in B.1.2 et al.
+ */
+ else if (!FIPS_module_mode() && !BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3))
+#else
else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3))
+#endif
{
/* range = 100..._2,
* so 3*range (= 11..._2) is exactly one bit longer than range */