Only use fake rand once per operation. This stops the EC
[openssl.git] / crypto / ecdsa / ecdsatest.c
index aa4e1481a8f3439427bb237982c72e6e4db533f5..c5b6ed2b87f3695fc1d9eafb9325d6a068e4861b 100644 (file)
@@ -137,7 +137,7 @@ int restore_rand(void)
                return 1;
        }
 
-static int fbytes_counter = 0;
+static int fbytes_counter = 0, use_fake = 0;
 static const char *numbers[8] = {
        "651056770906015076056810763456358567190100156695615665659",
        "6140507067065001063065065565667405560006161556565665656654",
@@ -157,6 +157,11 @@ int fbytes(unsigned char *buf, int num)
        int     ret;
        BIGNUM  *tmp = NULL;
 
+       if (use_fake == 0)
+               return old_rand->bytes(buf, num);
+
+       use_fake = 0;
+
        if (fbytes_counter >= 8)
                return 0;
        tmp = BN_new();
@@ -168,10 +173,9 @@ int fbytes(unsigned char *buf, int num)
                return 0;
                }
        fbytes_counter ++;
-       ret = BN_bn2bin(tmp, buf);      
-       if (ret == 0 || ret != num)
+       if (num != BN_num_bytes(tmp) || !BN_bn2bin(tmp, buf))
                ret = 0;
-       else
+       else 
                ret = 1;
        if (tmp)
                BN_free(tmp);
@@ -200,11 +204,13 @@ int x9_62_test_internal(BIO *out, int nid, const char *r_in, const char *s_in)
        /* create the key */
        if ((key = EC_KEY_new_by_curve_name(nid)) == NULL)
                goto x962_int_err;
+       use_fake = 1;
        if (!EC_KEY_generate_key(key))
                goto x962_int_err;
        BIO_printf(out, ".");
        (void)BIO_flush(out);
        /* create the signature */
+       use_fake = 1;
        signature = ECDSA_do_sign(digest, 20, key);
        if (signature == NULL)
                goto x962_int_err;
@@ -263,6 +269,7 @@ int x9_62_tests(BIO *out)
                "3238135532097973577080787768312505059318910517550078427819"
                "78505179448783"))
                goto x962_err;
+#ifndef OPENSSL_NO_EC2M
        if (!x9_62_test_internal(out, NID_X9_62_c2tnb191v1,
                "87194383164871543355722284926904419997237591535066528048",
                "308992691965804947361541664549085895292153777025772063598"))
@@ -273,7 +280,7 @@ int x9_62_tests(BIO *out)
                "1970303740007316867383349976549972270528498040721988191026"
                "49413465737174"))
                goto x962_err;
-
+#endif
        ret = 1;
 x962_err:
        if (!restore_rand())