Internal version of BN_mod_inverse allowing checking of no-inverse without
[openssl.git] / crypto / bn / bn_gcd.c
index 4a352119ba8aafc0a7164f14337df2e09837de32..45b417bddc3d171988988e7dee908962feb2bb9f 100644 (file)
@@ -205,13 +205,28 @@ err:
 /* solves ax == 1 (mod n) */
 static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,
         const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx);
+
 BIGNUM *BN_mod_inverse(BIGNUM *in,
        const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
        {
+       BIGNUM *rv;
+       int noinv;
+       rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);
+       if (noinv)
+               BNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);
+       return rv;
+       }
+
+BIGNUM *int_bn_mod_inverse(BIGNUM *in,
+       const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx, int *pnoinv)
+       {
        BIGNUM *A,*B,*X,*Y,*M,*D,*T,*R=NULL;
        BIGNUM *ret=NULL;
        int sign;
 
+       if (pnoinv)
+               *pnoinv = 0;
+
        if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0) || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0))
                {
                return BN_mod_inverse_no_branch(in, a, n, ctx);
@@ -488,7 +503,8 @@ BIGNUM *BN_mod_inverse(BIGNUM *in,
                }
        else
                {
-               BNerr(BN_F_BN_MOD_INVERSE,BN_R_NO_INVERSE);
+               if (pnoinv)
+                       *pnoinv = 1;
                goto err;
                }
        ret=R;