Use BN_rand_range().
[openssl.git] / crypto / rsa / rsa_lib.c
index 6ebb0b552a54b629f3535712cbedf1576e1d8032..d3a602f0a5be91bb9944b03c03e6b77760850f9d 100644 (file)
@@ -191,13 +191,13 @@ RSA *RSA_new_method(ENGINE *engine)
        ret->blinding=NULL;
        ret->bignum_data=NULL;
        ret->flags=meth->flags;
        ret->blinding=NULL;
        ret->bignum_data=NULL;
        ret->flags=meth->flags;
+       CRYPTO_new_ex_data(rsa_meth,ret,&ret->ex_data);
        if ((meth->init != NULL) && !meth->init(ret))
                {
        if ((meth->init != NULL) && !meth->init(ret))
                {
+               CRYPTO_free_ex_data(rsa_meth, ret, &ret->ex_data);
                OPENSSL_free(ret);
                ret=NULL;
                }
                OPENSSL_free(ret);
                ret=NULL;
                }
-       else
-               CRYPTO_new_ex_data(rsa_meth,ret,&ret->ex_data);
        return(ret);
        }
 
        return(ret);
        }
 
@@ -221,13 +221,13 @@ void RSA_free(RSA *r)
                }
 #endif
 
                }
 #endif
 
-       CRYPTO_free_ex_data(rsa_meth,r,&r->ex_data);
-
        meth = ENGINE_get_RSA(r->engine);
        if (meth->finish != NULL)
                meth->finish(r);
        ENGINE_finish(r->engine);
 
        meth = ENGINE_get_RSA(r->engine);
        if (meth->finish != NULL)
                meth->finish(r);
        ENGINE_finish(r->engine);
 
+       CRYPTO_free_ex_data(rsa_meth,r,&r->ex_data);
+
        if (r->n != NULL) BN_clear_free(r->n);
        if (r->e != NULL) BN_clear_free(r->e);
        if (r->d != NULL) BN_clear_free(r->d);
        if (r->n != NULL) BN_clear_free(r->n);
        if (r->e != NULL) BN_clear_free(r->e);
        if (r->d != NULL) BN_clear_free(r->d);
@@ -325,7 +325,7 @@ int RSA_blinding_on(RSA *rsa, BN_CTX *p_ctx)
 
        BN_CTX_start(ctx);
        A = BN_CTX_get(ctx);
 
        BN_CTX_start(ctx);
        A = BN_CTX_get(ctx);
-       if (!BN_rand(A,BN_num_bits(rsa->n)-1,1,0)) goto err;
+       if (!BN_rand_range(A,rsa->n)) goto err;
        if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err;
 
        if (!ENGINE_get_RSA(rsa->engine)->bn_mod_exp(A,A,
        if ((Ai=BN_mod_inverse(NULL,A,rsa->n,ctx)) == NULL) goto err;
 
        if (!ENGINE_get_RSA(rsa->engine)->bn_mod_exp(A,A,