crypto/rsa/rsa_gen.c:193: error: BUFFER_OVERRUN_L3
  Offset added: [8, +oo] Size: [0, 536870848] by call to `BN_mod_inverse`.
Showing all 17 steps of the trace


crypto/rsa/rsa_gen.c:189:18: Call
187.                   }
188.               }
189.               if (!BN_sub(r2, prime, BN_value_one()))
                        ^
190.                   goto err;
191.               ERR_set_mark();

crypto/bn/bn_add.c:59:19: Call
  57.           if (cmp_res > 0) {
  58.               r_neg = a->neg;
  59.               ret = BN_usub(r, a, b);
                          ^
  60.           } else if (cmp_res < 0) {
  61.               r_neg = !b->neg;

crypto/bn/bn_add.c:125:1: Parameter `r->top`
    123.   
    124.   /* unsigned subtraction of b from a, a must be larger than b. */
    125. > int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)
    126.   {
    127.       int max, min, dif;

crypto/rsa/rsa_gen.c:193:17: Call
191.               ERR_set_mark();
192.               BN_set_flags(r2, BN_FLG_CONSTTIME);
193.               if (BN_mod_inverse(r1, r2, rsa->e, ctx) != NULL) {
                       ^
194.                  /* GCD == 1 since inverse exists */
195.                   break;

crypto/bn/bn_gcd.c:124:1: Parameter `a->top`
  122.                                           BN_CTX *ctx);
  123.   
  124. > BIGNUM *BN_mod_inverse(BIGNUM *in,
  125.                          const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
  126.   {

crypto/bn/bn_gcd.c:129:10: Call
  127.       BIGNUM *rv;
  128.       int noinv;
  129.       rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);
                  ^
  130.       if (noinv)
  131.           BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);

crypto/bn/bn_gcd.c:135:1: Parameter `a->top`
    133.   }
    134.   
    135. > BIGNUM *int_bn_mod_inverse(BIGNUM *in,
    136.                              const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,
    137.                              int *pnoinv)

crypto/bn/bn_gcd.c:155:16: Call
    153.       if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)
    154.           || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {
    155.           return BN_mod_inverse_no_branch(in, a, n, ctx);
                          ^
    156.       }
    157.   

crypto/bn/bn_gcd.c:458:1: Parameter `a->top`
      456.    * not contain branches that may leak sensitive information.
      457.    */
      458. > static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,
      459.                                           const BIGNUM *a, const BIGNUM *n,
      460.                                           BN_CTX *ctx)

crypto/bn/bn_gcd.c:489:9: Call
      487.       BN_one(X);
      488.       BN_zero(Y);
      489.       if (BN_copy(B, a) == NULL)
                     ^
      490.           goto err;
      491.       if (BN_copy(A, n) == NULL)

crypto/bn/bn_lib.c:285:1: <Offset trace>
283.   }
284.   
285. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
286.   {
287.       bn_check_top(b);

crypto/bn/bn_lib.c:285:1: Parameter `b->top`
        283.   }
        284.   
        285. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
        286.   {
        287.       bn_check_top(b);

crypto/bn/bn_lib.c:285:1: <Length trace>
283.   }
284.   
285. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
286.   {
287.       bn_check_top(b);

crypto/bn/bn_lib.c:285:1: Parameter `*a->d`
        283.   }
        284.   
        285. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
        286.   {
        287.       bn_check_top(b);

crypto/bn/bn_lib.c:291:9: Call
        289.       if (a == b)
        290.           return a;
        291.       if (bn_wexpand(a, b->top) == NULL)
                       ^
        292.           return NULL;
        293.   

crypto/bn/bn_lib.c:941:1: Parameter `*a->d`
          939.   }
          940.   
          941. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
          942.   {
          943.       return (words <= a->dmax) ? a : bn_expand2(a, words);

crypto/bn/bn_lib.c:295:9: Array access: Offset added: [8, +oo] Size: [0, 536870848] by call to `BN_mod_inverse` 
        293.   
        294.       if (b->top > 0)
        295.           memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
                       ^
        296.   
        297.       a->top = b->top;

BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
    bn_check_top(b);
    if (a == b)
        return a;
    if (bn_wexpand(a, b->top) == NULL)
        return NULL;
    if (b->top > 0)
        memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
    a->top = b->top;
    a->neg = b->neg;
    bn_check_top(a);
    return a;
}
['static int rsa_builtin_keygen(RSA *rsa, int bits, int primes, BIGNUM *e_value,\n                              BN_GENCB *cb)\n{\n    BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *tmp, *prime;\n    int ok = -1, n = 0, bitsr[RSA_MAX_PRIME_NUM], bitse = 0;\n    int i = 0, quo = 0, rmd = 0, adj = 0, retries = 0;\n    RSA_PRIME_INFO *pinfo = NULL;\n    STACK_OF(RSA_PRIME_INFO) *prime_infos = NULL;\n    BN_CTX *ctx = NULL;\n    BN_ULONG bitst = 0;\n    unsigned long error = 0;\n    if (bits < RSA_MIN_MODULUS_BITS) {\n        ok = 0;\n        RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, RSA_R_KEY_SIZE_TOO_SMALL);\n        goto err;\n    }\n    if (primes < RSA_DEFAULT_PRIME_NUM || primes > rsa_multip_cap(bits)) {\n        ok = 0;\n        RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, RSA_R_KEY_PRIME_NUM_INVALID);\n        goto err;\n    }\n    ctx = BN_CTX_new();\n    if (ctx == NULL)\n        goto err;\n    BN_CTX_start(ctx);\n    r0 = BN_CTX_get(ctx);\n    r1 = BN_CTX_get(ctx);\n    r2 = BN_CTX_get(ctx);\n    if (r2 == NULL)\n        goto err;\n    quo = bits / primes;\n    rmd = bits % primes;\n    for (i = 0; i < primes; i++)\n        bitsr[i] = (i < rmd) ? quo + 1 : quo;\n    if (!rsa->n && ((rsa->n = BN_new()) == NULL))\n        goto err;\n    if (!rsa->d && ((rsa->d = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->e && ((rsa->e = BN_new()) == NULL))\n        goto err;\n    if (!rsa->p && ((rsa->p = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->q && ((rsa->q = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->dmp1 && ((rsa->dmp1 = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->dmq1 && ((rsa->dmq1 = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->iqmp && ((rsa->iqmp = BN_secure_new()) == NULL))\n        goto err;\n    if (primes > RSA_DEFAULT_PRIME_NUM) {\n        rsa->version = RSA_ASN1_VERSION_MULTI;\n        prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, primes - 2);\n        if (prime_infos == NULL)\n            goto err;\n        if (rsa->prime_infos != NULL) {\n            sk_RSA_PRIME_INFO_pop_free(rsa->prime_infos, rsa_multip_info_free);\n        }\n        rsa->prime_infos = prime_infos;\n        for (i = 2; i < primes; i++) {\n            pinfo = rsa_multip_info_new();\n            if (pinfo == NULL)\n                goto err;\n            (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);\n        }\n    }\n    if (BN_copy(rsa->e, e_value) == NULL)\n        goto err;\n    for (i = 0; i < primes; i++) {\n        adj = 0;\n        retries = 0;\n        if (i == 0) {\n            prime = rsa->p;\n        } else if (i == 1) {\n            prime = rsa->q;\n        } else {\n            pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);\n            prime = pinfo->r;\n        }\n        BN_set_flags(prime, BN_FLG_CONSTTIME);\n        for (;;) {\n redo:\n            if (!BN_generate_prime_ex(prime, bitsr[i] + adj, 0, NULL, NULL, cb))\n                goto err;\n            {\n                int j;\n                for (j = 0; j < i; j++) {\n                    BIGNUM *prev_prime;\n                    if (j == 0)\n                        prev_prime = rsa->p;\n                    else if (j == 1)\n                        prev_prime = rsa->q;\n                    else\n                        prev_prime = sk_RSA_PRIME_INFO_value(prime_infos,\n                                                             j - 2)->r;\n                    if (!BN_cmp(prime, prev_prime)) {\n                        goto redo;\n                    }\n                }\n            }\n            if (!BN_sub(r2, prime, BN_value_one()))\n                goto err;\n            ERR_set_mark();\n            BN_set_flags(r2, BN_FLG_CONSTTIME);\n            if (BN_mod_inverse(r1, r2, rsa->e, ctx) != NULL) {\n                break;\n            }\n            error = ERR_peek_last_error();\n            if (ERR_GET_LIB(error) == ERR_LIB_BN\n                && ERR_GET_REASON(error) == BN_R_NO_INVERSE) {\n                ERR_pop_to_mark();\n            } else {\n                goto err;\n            }\n            if (!BN_GENCB_call(cb, 2, n++))\n                goto err;\n        }\n        bitse += bitsr[i];\n        if (i == 1) {\n            if (!BN_mul(r1, rsa->p, rsa->q, ctx))\n                goto err;\n        } else if (i != 0) {\n            if (!BN_mul(r1, rsa->n, prime, ctx))\n                goto err;\n        } else {\n            if (!BN_GENCB_call(cb, 3, i))\n                goto err;\n            continue;\n        }\n        if (!BN_rshift(r2, r1, bitse - 4))\n            goto err;\n        bitst = BN_get_word(r2);\n        if (bitst < 0x9 || bitst > 0xF) {\n            bitse -= bitsr[i];\n            if (!BN_GENCB_call(cb, 2, n++))\n                goto err;\n            if (primes > 4) {\n                if (bitst < 0x9)\n                    adj++;\n                else\n                    adj--;\n            } else if (retries == 4) {\n                i = -1;\n                bitse = 0;\n                continue;\n            }\n            retries++;\n            goto redo;\n        }\n        if (i > 1 && BN_copy(pinfo->pp, rsa->n) == NULL)\n            goto err;\n        if (BN_copy(rsa->n, r1) == NULL)\n            goto err;\n        if (!BN_GENCB_call(cb, 3, i))\n            goto err;\n    }\n    if (BN_cmp(rsa->p, rsa->q) < 0) {\n        tmp = rsa->p;\n        rsa->p = rsa->q;\n        rsa->q = tmp;\n    }\n    if (!BN_sub(r1, rsa->p, BN_value_one()))\n        goto err;\n    if (!BN_sub(r2, rsa->q, BN_value_one()))\n        goto err;\n    if (!BN_mul(r0, r1, r2, ctx))\n        goto err;\n    for (i = 2; i < primes; i++) {\n        pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);\n        if (!BN_sub(pinfo->d, pinfo->r, BN_value_one()))\n            goto err;\n        if (!BN_mul(r0, r0, pinfo->d, ctx))\n            goto err;\n    }\n    {\n        BIGNUM *pr0 = BN_new();\n        if (pr0 == NULL)\n            goto err;\n        BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);\n        if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) {\n            BN_free(pr0);\n            goto err;\n        }\n        BN_free(pr0);\n    }\n    {\n        BIGNUM *d = BN_new();\n        if (d == NULL)\n            goto err;\n        BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);\n        if (!BN_mod(rsa->dmp1, d, r1, ctx)\n            || !BN_mod(rsa->dmq1, d, r2, ctx)) {\n            BN_free(d);\n            goto err;\n        }\n        for (i = 2; i < primes; i++) {\n            pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);\n            if (!BN_mod(pinfo->d, d, pinfo->d, ctx)) {\n                BN_free(d);\n                goto err;\n            }\n        }\n        BN_free(d);\n    }\n    {\n        BIGNUM *p = BN_new();\n        if (p == NULL)\n            goto err;\n        BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);\n        if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) {\n            BN_free(p);\n            goto err;\n        }\n        for (i = 2; i < primes; i++) {\n            pinfo = sk_RSA_PRIME_INFO_value(prime_infos, i - 2);\n            BN_with_flags(p, pinfo->r, BN_FLG_CONSTTIME);\n            if (!BN_mod_inverse(pinfo->t, pinfo->pp, p, ctx)) {\n                BN_free(p);\n                goto err;\n            }\n        }\n        BN_free(p);\n    }\n    ok = 1;\n err:\n    if (ok == -1) {\n        RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, ERR_LIB_BN);\n        ok = 0;\n    }\n    if (ctx != NULL)\n        BN_CTX_end(ctx);\n    BN_CTX_free(ctx);\n    return ok;\n}', 'int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n    int ret, r_neg, cmp_res;\n    bn_check_top(a);\n    bn_check_top(b);\n    if (a->neg != b->neg) {\n        r_neg = a->neg;\n        ret = BN_uadd(r, a, b);\n    } else {\n        cmp_res = BN_ucmp(a, b);\n        if (cmp_res > 0) {\n            r_neg = a->neg;\n            ret = BN_usub(r, a, b);\n        } else if (cmp_res < 0) {\n            r_neg = !b->neg;\n            ret = BN_usub(r, b, a);\n        } else {\n            r_neg = 0;\n            BN_zero(r);\n            ret = 1;\n        }\n    }\n    r->neg = r_neg;\n    bn_check_top(r);\n    return ret;\n}', 'int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b)\n{\n    int max, min, dif;\n    BN_ULONG t1, t2, borrow, *rp;\n    const BN_ULONG *ap, *bp;\n    bn_check_top(a);\n    bn_check_top(b);\n    max = a->top;\n    min = b->top;\n    dif = max - min;\n    if (dif < 0) {\n        BNerr(BN_F_BN_USUB, BN_R_ARG2_LT_ARG3);\n        return 0;\n    }\n    if (bn_wexpand(r, max) == NULL)\n        return 0;\n    ap = a->d;\n    bp = b->d;\n    rp = r->d;\n    borrow = bn_sub_words(rp, ap, bp, min);\n    ap += min;\n    rp += min;\n    while (dif) {\n        dif--;\n        t1 = *(ap++);\n        t2 = (t1 - borrow) & BN_MASK2;\n        *(rp++) = t2;\n        borrow &= (t1 == 0);\n    }\n    while (max && *--rp == 0)\n        max--;\n    r->top = max;\n    r->neg = 0;\n    bn_pollute(r);\n    return 1;\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n                       const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n    BIGNUM *rv;\n    int noinv;\n    rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n    if (noinv)\n        BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n    return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n                           const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n                           int *pnoinv)\n{\n    BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n    BIGNUM *ret = NULL;\n    int sign;\n    if (BN_abs_is_word(n, 1) || BN_is_zero(n)) {\n        if (pnoinv != NULL)\n            *pnoinv = 1;\n        return NULL;\n    }\n    if (pnoinv != NULL)\n        *pnoinv = 0;\n    if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n        || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n        return BN_mod_inverse_no_branch(in, a, n, ctx);\n    }\n    bn_check_top(a);\n    bn_check_top(n);\n    BN_CTX_start(ctx);\n    A = BN_CTX_get(ctx);\n    B = BN_CTX_get(ctx);\n    X = BN_CTX_get(ctx);\n    D = BN_CTX_get(ctx);\n    M = BN_CTX_get(ctx);\n    Y = BN_CTX_get(ctx);\n    T = BN_CTX_get(ctx);\n    if (T == NULL)\n        goto err;\n    if (in == NULL)\n        R = BN_new();\n    else\n        R = in;\n    if (R == NULL)\n        goto err;\n    BN_one(X);\n    BN_zero(Y);\n    if (BN_copy(B, a) == NULL)\n        goto err;\n    if (BN_copy(A, n) == NULL)\n        goto err;\n    A->neg = 0;\n    if (B->neg || (BN_ucmp(B, A) >= 0)) {\n        if (!BN_nnmod(B, B, A, ctx))\n            goto err;\n    }\n    sign = -1;\n    if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n        int shift;\n        while (!BN_is_zero(B)) {\n            shift = 0;\n            while (!BN_is_bit_set(B, shift)) {\n                shift++;\n                if (BN_is_odd(X)) {\n                    if (!BN_uadd(X, X, n))\n                        goto err;\n                }\n                if (!BN_rshift1(X, X))\n                    goto err;\n            }\n            if (shift > 0) {\n                if (!BN_rshift(B, B, shift))\n                    goto err;\n            }\n            shift = 0;\n            while (!BN_is_bit_set(A, shift)) {\n                shift++;\n                if (BN_is_odd(Y)) {\n                    if (!BN_uadd(Y, Y, n))\n                        goto err;\n                }\n                if (!BN_rshift1(Y, Y))\n                    goto err;\n            }\n            if (shift > 0) {\n                if (!BN_rshift(A, A, shift))\n                    goto err;\n            }\n            if (BN_ucmp(B, A) >= 0) {\n                if (!BN_uadd(X, X, Y))\n                    goto err;\n                if (!BN_usub(B, B, A))\n                    goto err;\n            } else {\n                if (!BN_uadd(Y, Y, X))\n                    goto err;\n                if (!BN_usub(A, A, B))\n                    goto err;\n            }\n        }\n    } else {\n        while (!BN_is_zero(B)) {\n            BIGNUM *tmp;\n            if (BN_num_bits(A) == BN_num_bits(B)) {\n                if (!BN_one(D))\n                    goto err;\n                if (!BN_sub(M, A, B))\n                    goto err;\n            } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n                if (!BN_lshift1(T, B))\n                    goto err;\n                if (BN_ucmp(A, T) < 0) {\n                    if (!BN_one(D))\n                        goto err;\n                    if (!BN_sub(M, A, B))\n                        goto err;\n                } else {\n                    if (!BN_sub(M, A, T))\n                        goto err;\n                    if (!BN_add(D, T, B))\n                        goto err;\n                    if (BN_ucmp(A, D) < 0) {\n                        if (!BN_set_word(D, 2))\n                            goto err;\n                    } else {\n                        if (!BN_set_word(D, 3))\n                            goto err;\n                        if (!BN_sub(M, M, B))\n                            goto err;\n                    }\n                }\n            } else {\n                if (!BN_div(D, M, A, B, ctx))\n                    goto err;\n            }\n            tmp = A;\n            A = B;\n            B = M;\n            if (BN_is_one(D)) {\n                if (!BN_add(tmp, X, Y))\n                    goto err;\n            } else {\n                if (BN_is_word(D, 2)) {\n                    if (!BN_lshift1(tmp, X))\n                        goto err;\n                } else if (BN_is_word(D, 4)) {\n                    if (!BN_lshift(tmp, X, 2))\n                        goto err;\n                } else if (D->top == 1) {\n                    if (!BN_copy(tmp, X))\n                        goto err;\n                    if (!BN_mul_word(tmp, D->d[0]))\n                        goto err;\n                } else {\n                    if (!BN_mul(tmp, D, X, ctx))\n                        goto err;\n                }\n                if (!BN_add(tmp, tmp, Y))\n                    goto err;\n            }\n            M = Y;\n            Y = X;\n            X = tmp;\n            sign = -sign;\n        }\n    }\n    if (sign < 0) {\n        if (!BN_sub(Y, n, Y))\n            goto err;\n    }\n    if (BN_is_one(A)) {\n        if (!Y->neg && BN_ucmp(Y, n) < 0) {\n            if (!BN_copy(R, Y))\n                goto err;\n        } else {\n            if (!BN_nnmod(R, Y, n, ctx))\n                goto err;\n        }\n    } else {\n        if (pnoinv)\n            *pnoinv = 1;\n        goto err;\n    }\n    ret = R;\n err:\n    if ((ret == NULL) && (in == NULL))\n        BN_free(R);\n    BN_CTX_end(ctx);\n    bn_check_top(ret);\n    return ret;\n}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n                                        const BIGNUM *a, const BIGNUM *n,\n                                        BN_CTX *ctx)\n{\n    BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n    BIGNUM *ret = NULL;\n    int sign;\n    bn_check_top(a);\n    bn_check_top(n);\n    BN_CTX_start(ctx);\n    A = BN_CTX_get(ctx);\n    B = BN_CTX_get(ctx);\n    X = BN_CTX_get(ctx);\n    D = BN_CTX_get(ctx);\n    M = BN_CTX_get(ctx);\n    Y = BN_CTX_get(ctx);\n    T = BN_CTX_get(ctx);\n    if (T == NULL)\n        goto err;\n    if (in == NULL)\n        R = BN_new();\n    else\n        R = in;\n    if (R == NULL)\n        goto err;\n    BN_one(X);\n    BN_zero(Y);\n    if (BN_copy(B, a) == NULL)\n        goto err;\n    if (BN_copy(A, n) == NULL)\n        goto err;\n    A->neg = 0;\n    if (B->neg || (BN_ucmp(B, A) >= 0)) {\n         {\n            BIGNUM local_B;\n            bn_init(&local_B);\n            BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);\n            if (!BN_nnmod(B, &local_B, A, ctx))\n                goto err;\n        }\n    }\n    sign = -1;\n    while (!BN_is_zero(B)) {\n        BIGNUM *tmp;\n        {\n            BIGNUM local_A;\n            bn_init(&local_A);\n            BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);\n            if (!BN_div(D, M, &local_A, B, ctx))\n                goto err;\n        }\n        tmp = A;\n        A = B;\n        B = M;\n        if (!BN_mul(tmp, D, X, ctx))\n            goto err;\n        if (!BN_add(tmp, tmp, Y))\n            goto err;\n        M = Y;\n        Y = X;\n        X = tmp;\n        sign = -sign;\n    }\n    if (sign < 0) {\n        if (!BN_sub(Y, n, Y))\n            goto err;\n    }\n    if (BN_is_one(A)) {\n        if (!Y->neg && BN_ucmp(Y, n) < 0) {\n            if (!BN_copy(R, Y))\n                goto err;\n        } else {\n            if (!BN_nnmod(R, Y, n, ctx))\n                goto err;\n        }\n    } else {\n        BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n        goto err;\n    }\n    ret = R;\n err:\n    if ((ret == NULL) && (in == NULL))\n        BN_free(R);\n    BN_CTX_end(ctx);\n    bn_check_top(ret);\n    return ret;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n    bn_check_top(b);\n    if (a == b)\n        return a;\n    if (bn_wexpand(a, b->top) == NULL)\n        return NULL;\n    if (b->top > 0)\n        memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n    a->top = b->top;\n    a->neg = b->neg;\n    bn_check_top(a);\n    return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n    return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']