crypto/rsa/rsa_ossl.c:889: error: BUFFER_OVERRUN_L3
  Offset: 0 (⇐ 1 + -1) Size: [0, 8388607] by call to `BN_mod_exp_mont`.
Showing all 18 steps of the trace


crypto/rsa/rsa_ossl.c:688:17: Call
686.                                         ctx)
687.               /* r0 = r1 * q + m1 */
688.               || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)
                       ^
689.               || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))
690.               goto err;

crypto/bn/bn_mul.c:507:1: Parameter `*r->d`
  505.   }
  506.   
  507. > int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  508.   {
  509.       int ret = 0;

crypto/rsa/rsa_ossl.c:689:17: Call
687.               /* r0 = r1 * q + m1 */
688.               || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)
689.               || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))
                       ^
690.               goto err;
691.   

crypto/bn/bn_mod.c:48:1: Parameter `*a->d`
  46.    * move depending on whether or not subtraction borrowed.
  47.    */
  48. > int bn_mod_add_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
  49.                            const BIGNUM *m)
  50.   {

crypto/rsa/rsa_ossl.c:889:18: Call
887.       if (rsa->e && rsa->n) {
888.           if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {
889.               if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,
                        ^
890.                                    rsa->_method_mod_n))
891.                   goto err;

crypto/bn/bn_exp.c:296:1: Parameter `*a->d`
  294.   }
  295.   
  296. > int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
  297.                       const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
  298.   {

crypto/bn/bn_exp.c:359:10: Call
  357.       } else
  358.           aa = a;
  359.       if (!bn_to_mont_fixed_top(val[0], aa, mont, ctx))
                  ^
  360.           goto err;               /* 1 */
  361.   

crypto/bn/bn_mont.c:222:1: Parameter `*a->d`
    220.   }
    221.   
    222. > int bn_to_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
    223.                            BN_CTX *ctx)
    224.   {

crypto/bn/bn_mont.c:225:12: Call
    223.                            BN_CTX *ctx)
    224.   {
    225.       return bn_mul_mont_fixed_top(r, a, &(mont->RR), mont, ctx);
                      ^
    226.   }
    227.   

crypto/bn/bn_mont.c:37:1: Parameter `*a->d`
      35.   }
      36.   
      37. > int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
      38.                             BN_MONT_CTX *mont, BN_CTX *ctx)
      39.   {

crypto/bn/bn_mont.c:67:14: Call
      65.       bn_check_top(tmp);
      66.       if (a == b) {
      67.           if (!bn_sqr_fixed_top(tmp, a, ctx))
                         ^
      68.               goto err;
      69.       } else {

crypto/bn/bn_sqr.c:27:1: Parameter `*a->d`
        25.   }
        26.   
        27. > int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
        28.   {
        29.       int max, al;

crypto/bn/bn_sqr.c:70:13: Call
        68.           if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {
        69.               BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];
        70.               bn_sqr_normal(rr->d, a->d, al, t);
                          ^
        71.           } else {
        72.               int j, k;

crypto/bn/bn_sqr.c:109:1: <Length trace>
107.   
108.   /* tmp must have 2*n words */
109. > void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
110.   {
111.       int i, j, max;

crypto/bn/bn_sqr.c:109:1: Parameter `*a`
          107.   
          108.   /* tmp must have 2*n words */
          109. > void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
          110.   {
          111.       int i, j, max;

crypto/bn/bn_sqr.c:116:5: Assignment
          114.   
          115.       max = n * 2;
          116.       ap = a;
                     ^
          117.       rp = r;
          118.       rp[0] = rp[max - 1] = 0;

crypto/bn/bn_sqr.c:123:9: Assignment
          121.   
          122.       if (--j > 0) {
          123.           ap++;
                         ^
          124.           rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
          125.           rp += 2;

crypto/bn/bn_sqr.c:124:41: Array access: Offset: 0 (⇐ 1 + -1) Size: [0, 8388607] by call to `BN_mod_exp_mont` 
          122.       if (--j > 0) {
          123.           ap++;
          124.           rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
                                                         ^
          125.           rp += 2;
          126.       }

void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
{
    int i, j, max;
    const BN_ULONG *ap;
    BN_ULONG *rp;
    max = n * 2;
    ap = a;
    rp = r;
    rp[0] = rp[max - 1] = 0;
    rp++;
    j = n;
    if (--j > 0) {
        ap++;
        rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
        rp += 2;
    }
    for (i = n - 2; i > 0; i--) {
        j--;
        ap++;
        rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
        rp += 2;
    }
    bn_add_words(r, r, r, max);
    bn_sqr_words(tmp, a, n);
    bn_add_words(r, r, tmp, max);
}
['static int rsa_ossl_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)\n{\n    BIGNUM *r1, *m1, *vrfy, *r2, *m[RSA_MAX_PRIME_NUM - 2];\n    int ret = 0, i, ex_primes = 0, smooth = 0;\n    RSA_PRIME_INFO *pinfo;\n    BN_CTX_start(ctx);\n    r1 = BN_CTX_get(ctx);\n    r2 = BN_CTX_get(ctx);\n    m1 = BN_CTX_get(ctx);\n    vrfy = BN_CTX_get(ctx);\n    if (vrfy == NULL)\n        goto err;\n    if (rsa->version == RSA_ASN1_VERSION_MULTI\n        && ((ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos)) <= 0\n             || ex_primes > RSA_MAX_PRIME_NUM - 2))\n        goto err;\n    if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {\n        BIGNUM *factor = BN_new();\n        if (factor == NULL)\n            goto err;\n        if (!(BN_with_flags(factor, rsa->p, BN_FLG_CONSTTIME),\n              BN_MONT_CTX_set_locked(&rsa->_method_mod_p, rsa->lock,\n                                     factor, ctx))\n            || !(BN_with_flags(factor, rsa->q, BN_FLG_CONSTTIME),\n                 BN_MONT_CTX_set_locked(&rsa->_method_mod_q, rsa->lock,\n                                        factor, ctx))) {\n            BN_free(factor);\n            goto err;\n        }\n        for (i = 0; i < ex_primes; i++) {\n            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n            BN_with_flags(factor, pinfo->r, BN_FLG_CONSTTIME);\n            if (!BN_MONT_CTX_set_locked(&pinfo->m, rsa->lock, factor, ctx)) {\n                BN_free(factor);\n                goto err;\n            }\n        }\n        BN_free(factor);\n        smooth = (ex_primes == 0)\n                 && (rsa->meth->bn_mod_exp == BN_mod_exp_mont)\n                 && (BN_num_bits(rsa->q) == BN_num_bits(rsa->p));\n    }\n    if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)\n        if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, rsa->lock,\n                                    rsa->n, ctx))\n            goto err;\n    if (smooth) {\n        if (\n            !bn_from_mont_fixed_top(m1, I, rsa->_method_mod_q, ctx)\n            || !bn_to_mont_fixed_top(m1, m1, rsa->_method_mod_q, ctx)\n            || !BN_mod_exp_mont_consttime(m1, m1, rsa->dmq1, rsa->q, ctx,\n                                          rsa->_method_mod_q)\n            || !bn_from_mont_fixed_top(r1, I, rsa->_method_mod_p, ctx)\n            || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)\n            || !BN_mod_exp_mont_consttime(r1, r1, rsa->dmp1, rsa->p, ctx,\n                                          rsa->_method_mod_p)\n            || !bn_mod_sub_fixed_top(r1, r1, m1, rsa->p)\n            || !bn_to_mont_fixed_top(r1, r1, rsa->_method_mod_p, ctx)\n            || !bn_mul_mont_fixed_top(r1, r1, rsa->iqmp, rsa->_method_mod_p,\n                                      ctx)\n            || !bn_mul_fixed_top(r0, r1, rsa->q, ctx)\n            || !bn_mod_add_fixed_top(r0, r0, m1, rsa->n))\n            goto err;\n        goto tail;\n    }\n    {\n        BIGNUM *c = BN_new();\n        if (c == NULL)\n            goto err;\n        BN_with_flags(c, I, BN_FLG_CONSTTIME);\n        if (!BN_mod(r1, c, rsa->q, ctx)) {\n            BN_free(c);\n            goto err;\n        }\n        {\n            BIGNUM *dmq1 = BN_new();\n            if (dmq1 == NULL) {\n                BN_free(c);\n                goto err;\n            }\n            BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);\n            if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx,\n                                       rsa->_method_mod_q)) {\n                BN_free(c);\n                BN_free(dmq1);\n                goto err;\n            }\n            BN_free(dmq1);\n        }\n        if (!BN_mod(r1, c, rsa->p, ctx)) {\n            BN_free(c);\n            goto err;\n        }\n        BN_free(c);\n    }\n    {\n        BIGNUM *dmp1 = BN_new();\n        if (dmp1 == NULL)\n            goto err;\n        BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);\n        if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx,\n                                   rsa->_method_mod_p)) {\n            BN_free(dmp1);\n            goto err;\n        }\n        BN_free(dmp1);\n    }\n    if (ex_primes > 0) {\n        BIGNUM *di = BN_new(), *cc = BN_new();\n        if (cc == NULL || di == NULL) {\n            BN_free(cc);\n            BN_free(di);\n            goto err;\n        }\n        for (i = 0; i < ex_primes; i++) {\n            if ((m[i] = BN_CTX_get(ctx)) == NULL) {\n                BN_free(cc);\n                BN_free(di);\n                goto err;\n            }\n            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n            BN_with_flags(cc, I, BN_FLG_CONSTTIME);\n            BN_with_flags(di, pinfo->d, BN_FLG_CONSTTIME);\n            if (!BN_mod(r1, cc, pinfo->r, ctx)) {\n                BN_free(cc);\n                BN_free(di);\n                goto err;\n            }\n            if (!rsa->meth->bn_mod_exp(m[i], r1, di, pinfo->r, ctx, pinfo->m)) {\n                BN_free(cc);\n                BN_free(di);\n                goto err;\n            }\n        }\n        BN_free(cc);\n        BN_free(di);\n    }\n    if (!BN_sub(r0, r0, m1))\n        goto err;\n    if (BN_is_negative(r0))\n        if (!BN_add(r0, r0, rsa->p))\n            goto err;\n    if (!BN_mul(r1, r0, rsa->iqmp, ctx))\n        goto err;\n    {\n        BIGNUM *pr1 = BN_new();\n        if (pr1 == NULL)\n            goto err;\n        BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);\n        if (!BN_mod(r0, pr1, rsa->p, ctx)) {\n            BN_free(pr1);\n            goto err;\n        }\n        BN_free(pr1);\n    }\n    if (BN_is_negative(r0))\n        if (!BN_add(r0, r0, rsa->p))\n            goto err;\n    if (!BN_mul(r1, r0, rsa->q, ctx))\n        goto err;\n    if (!BN_add(r0, r1, m1))\n        goto err;\n    if (ex_primes > 0) {\n        BIGNUM *pr2 = BN_new();\n        if (pr2 == NULL)\n            goto err;\n        for (i = 0; i < ex_primes; i++) {\n            pinfo = sk_RSA_PRIME_INFO_value(rsa->prime_infos, i);\n            if (!BN_sub(r1, m[i], r0)) {\n                BN_free(pr2);\n                goto err;\n            }\n            if (!BN_mul(r2, r1, pinfo->t, ctx)) {\n                BN_free(pr2);\n                goto err;\n            }\n            BN_with_flags(pr2, r2, BN_FLG_CONSTTIME);\n            if (!BN_mod(r1, pr2, pinfo->r, ctx)) {\n                BN_free(pr2);\n                goto err;\n            }\n            if (BN_is_negative(r1))\n                if (!BN_add(r1, r1, pinfo->r)) {\n                    BN_free(pr2);\n                    goto err;\n                }\n            if (!BN_mul(r1, r1, pinfo->pp, ctx)) {\n                BN_free(pr2);\n                goto err;\n            }\n            if (!BN_add(r0, r0, r1)) {\n                BN_free(pr2);\n                goto err;\n            }\n        }\n        BN_free(pr2);\n    }\n tail:\n    if (rsa->e && rsa->n) {\n        if (rsa->meth->bn_mod_exp == BN_mod_exp_mont) {\n            if (!BN_mod_exp_mont(vrfy, r0, rsa->e, rsa->n, ctx,\n                                 rsa->_method_mod_n))\n                goto err;\n        } else {\n            bn_correct_top(r0);\n            if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,\n                                       rsa->_method_mod_n))\n                goto err;\n        }\n        if (!BN_sub(vrfy, vrfy, I))\n            goto err;\n        if (BN_is_zero(vrfy)) {\n            bn_correct_top(r0);\n            ret = 1;\n            goto err;\n        }\n        if (!BN_mod(vrfy, vrfy, rsa->n, ctx))\n            goto err;\n        if (BN_is_negative(vrfy))\n            if (!BN_add(vrfy, vrfy, rsa->n))\n                goto err;\n        if (!BN_is_zero(vrfy)) {\n            BIGNUM *d = BN_new();\n            if (d == NULL)\n                goto err;\n            BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);\n            if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,\n                                       rsa->_method_mod_n)) {\n                BN_free(d);\n                goto err;\n            }\n            BN_free(d);\n        }\n    }\n    bn_correct_top(r0);\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    return ret;\n}', 'int bn_mul_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)\n{\n    int ret = 0;\n    int top, al, bl;\n    BIGNUM *rr;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n    int i;\n#endif\n#ifdef BN_RECURSION\n    BIGNUM *t = NULL;\n    int j = 0, k;\n#endif\n    bn_check_top(a);\n    bn_check_top(b);\n    bn_check_top(r);\n    al = a->top;\n    bl = b->top;\n    if ((al == 0) || (bl == 0)) {\n        BN_zero(r);\n        return 1;\n    }\n    top = al + bl;\n    BN_CTX_start(ctx);\n    if ((r == a) || (r == b)) {\n        if ((rr = BN_CTX_get(ctx)) == NULL)\n            goto err;\n    } else\n        rr = r;\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n    i = al - bl;\n#endif\n#ifdef BN_MUL_COMBA\n    if (i == 0) {\n# if 0\n        if (al == 4) {\n            if (bn_wexpand(rr, 8) == NULL)\n                goto err;\n            rr->top = 8;\n            bn_mul_comba4(rr->d, a->d, b->d);\n            goto end;\n        }\n# endif\n        if (al == 8) {\n            if (bn_wexpand(rr, 16) == NULL)\n                goto err;\n            rr->top = 16;\n            bn_mul_comba8(rr->d, a->d, b->d);\n            goto end;\n        }\n    }\n#endif\n#ifdef BN_RECURSION\n    if ((al >= BN_MULL_SIZE_NORMAL) && (bl >= BN_MULL_SIZE_NORMAL)) {\n        if (i >= -1 && i <= 1) {\n            if (i >= 0) {\n                j = BN_num_bits_word((BN_ULONG)al);\n            }\n            if (i == -1) {\n                j = BN_num_bits_word((BN_ULONG)bl);\n            }\n            j = 1 << (j - 1);\n            assert(j <= al || j <= bl);\n            k = j + j;\n            t = BN_CTX_get(ctx);\n            if (t == NULL)\n                goto err;\n            if (al > j || bl > j) {\n                if (bn_wexpand(t, k * 4) == NULL)\n                    goto err;\n                if (bn_wexpand(rr, k * 4) == NULL)\n                    goto err;\n                bn_mul_part_recursive(rr->d, a->d, b->d,\n                                      j, al - j, bl - j, t->d);\n            } else {\n                if (bn_wexpand(t, k * 2) == NULL)\n                    goto err;\n                if (bn_wexpand(rr, k * 2) == NULL)\n                    goto err;\n                bn_mul_recursive(rr->d, a->d, b->d, j, al - j, bl - j, t->d);\n            }\n            rr->top = top;\n            goto end;\n        }\n    }\n#endif\n    if (bn_wexpand(rr, top) == NULL)\n        goto err;\n    rr->top = top;\n    bn_mul_normal(rr->d, a->d, al, b->d, bl);\n#if defined(BN_MUL_COMBA) || defined(BN_RECURSION)\n end:\n#endif\n    rr->neg = a->neg ^ b->neg;\n    rr->flags |= BN_FLG_FIXED_TOP;\n    if (r != rr && BN_copy(r, rr) == NULL)\n        goto err;\n    ret = 1;\n err:\n    bn_check_top(r);\n    BN_CTX_end(ctx);\n    return ret;\n}', 'int bn_mod_add_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n                         const BIGNUM *m)\n{\n    size_t i, ai, bi, mtop = m->top;\n    BN_ULONG storage[1024 / BN_BITS2];\n    BN_ULONG carry, temp, mask, *rp, *tp = storage;\n    const BN_ULONG *ap, *bp;\n    if (bn_wexpand(r, mtop) == NULL)\n        return 0;\n    if (mtop > sizeof(storage) / sizeof(storage[0])\n        && (tp = OPENSSL_malloc(mtop * sizeof(BN_ULONG))) == NULL)\n        return 0;\n    ap = a->d != NULL ? a->d : tp;\n    bp = b->d != NULL ? b->d : tp;\n    for (i = 0, ai = 0, bi = 0, carry = 0; i < mtop;) {\n        mask = (BN_ULONG)0 - ((i - a->top) >> (8 * sizeof(i) - 1));\n        temp = ((ap[ai] & mask) + carry) & BN_MASK2;\n        carry = (temp < carry);\n        mask = (BN_ULONG)0 - ((i - b->top) >> (8 * sizeof(i) - 1));\n        tp[i] = ((bp[bi] & mask) + temp) & BN_MASK2;\n        carry += (tp[i] < temp);\n        i++;\n        ai += (i - a->dmax) >> (8 * sizeof(i) - 1);\n        bi += (i - b->dmax) >> (8 * sizeof(i) - 1);\n    }\n    rp = r->d;\n    carry -= bn_sub_words(rp, tp, m->d, mtop);\n    for (i = 0; i < mtop; i++) {\n        rp[i] = (carry & tp[i]) | (~carry & rp[i]);\n        ((volatile BN_ULONG *)tp)[i] = 0;\n    }\n    r->top = mtop;\n    r->flags |= BN_FLG_FIXED_TOP;\n    r->neg = 0;\n    if (tp != storage)\n        OPENSSL_free(tp);\n    return 1;\n}', 'int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,\n                    const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n    int i, j, bits, ret = 0, wstart, wend, window, wvalue;\n    int start = 1;\n    BIGNUM *d, *r;\n    const BIGNUM *aa;\n    BIGNUM *val[TABLE_SIZE];\n    BN_MONT_CTX *mont = NULL;\n    if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n            || BN_get_flags(a, BN_FLG_CONSTTIME) != 0\n            || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n        return BN_mod_exp_mont_consttime(rr, a, p, m, ctx, in_mont);\n    }\n    bn_check_top(a);\n    bn_check_top(p);\n    bn_check_top(m);\n    if (!BN_is_odd(m)) {\n        BNerr(BN_F_BN_MOD_EXP_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n        return 0;\n    }\n    bits = BN_num_bits(p);\n    if (bits == 0) {\n        if (BN_abs_is_word(m, 1)) {\n            ret = 1;\n            BN_zero(rr);\n        } else {\n            ret = BN_one(rr);\n        }\n        return ret;\n    }\n    BN_CTX_start(ctx);\n    d = BN_CTX_get(ctx);\n    r = BN_CTX_get(ctx);\n    val[0] = BN_CTX_get(ctx);\n    if (val[0] == NULL)\n        goto err;\n    if (in_mont != NULL)\n        mont = in_mont;\n    else {\n        if ((mont = BN_MONT_CTX_new()) == NULL)\n            goto err;\n        if (!BN_MONT_CTX_set(mont, m, ctx))\n            goto err;\n    }\n    if (a->neg || BN_ucmp(a, m) >= 0) {\n        if (!BN_nnmod(val[0], a, m, ctx))\n            goto err;\n        aa = val[0];\n    } else\n        aa = a;\n    if (!bn_to_mont_fixed_top(val[0], aa, mont, ctx))\n        goto err;\n    window = BN_window_bits_for_exponent_size(bits);\n    if (window > 1) {\n        if (!bn_mul_mont_fixed_top(d, val[0], val[0], mont, ctx))\n            goto err;\n        j = 1 << (window - 1);\n        for (i = 1; i < j; i++) {\n            if (((val[i] = BN_CTX_get(ctx)) == NULL) ||\n                !bn_mul_mont_fixed_top(val[i], val[i - 1], d, mont, ctx))\n                goto err;\n        }\n    }\n    start = 1;\n    wvalue = 0;\n    wstart = bits - 1;\n    wend = 0;\n#if 1\n    j = m->top;\n    if (m->d[j - 1] & (((BN_ULONG)1) << (BN_BITS2 - 1))) {\n        if (bn_wexpand(r, j) == NULL)\n            goto err;\n        r->d[0] = (0 - m->d[0]) & BN_MASK2;\n        for (i = 1; i < j; i++)\n            r->d[i] = (~m->d[i]) & BN_MASK2;\n        r->top = j;\n        r->flags |= BN_FLG_FIXED_TOP;\n    } else\n#endif\n    if (!bn_to_mont_fixed_top(r, BN_value_one(), mont, ctx))\n        goto err;\n    for (;;) {\n        if (BN_is_bit_set(p, wstart) == 0) {\n            if (!start) {\n                if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))\n                    goto err;\n            }\n            if (wstart == 0)\n                break;\n            wstart--;\n            continue;\n        }\n        j = wstart;\n        wvalue = 1;\n        wend = 0;\n        for (i = 1; i < window; i++) {\n            if (wstart - i < 0)\n                break;\n            if (BN_is_bit_set(p, wstart - i)) {\n                wvalue <<= (i - wend);\n                wvalue |= 1;\n                wend = i;\n            }\n        }\n        j = wend + 1;\n        if (!start)\n            for (i = 0; i < j; i++) {\n                if (!bn_mul_mont_fixed_top(r, r, r, mont, ctx))\n                    goto err;\n            }\n        if (!bn_mul_mont_fixed_top(r, r, val[wvalue >> 1], mont, ctx))\n            goto err;\n        wstart -= wend + 1;\n        wvalue = 0;\n        start = 0;\n        if (wstart < 0)\n            break;\n    }\n#if defined(SPARC_T4_MONT)\n    if (OPENSSL_sparcv9cap_P[0] & (SPARCV9_VIS3 | SPARCV9_PREFER_FPU)) {\n        j = mont->N.top;\n        val[0]->d[0] = 1;\n        for (i = 1; i < j; i++)\n            val[0]->d[i] = 0;\n        val[0]->top = j;\n        if (!BN_mod_mul_montgomery(rr, r, val[0], mont, ctx))\n            goto err;\n    } else\n#endif\n    if (!BN_from_montgomery(rr, r, mont, ctx))\n        goto err;\n    ret = 1;\n err:\n    if (in_mont == NULL)\n        BN_MONT_CTX_free(mont);\n    BN_CTX_end(ctx);\n    bn_check_top(rr);\n    return ret;\n}', 'int bn_to_mont_fixed_top(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n                         BN_CTX *ctx)\n{\n    return bn_mul_mont_fixed_top(r, a, &(mont->RR), mont, ctx);\n}', 'int bn_mul_mont_fixed_top(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n                          BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n    BIGNUM *tmp;\n    int ret = 0;\n    int num = mont->N.top;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n    if (num > 1 && a->top == num && b->top == num) {\n        if (bn_wexpand(r, num) == NULL)\n            return 0;\n        if (bn_mul_mont(r->d, a->d, b->d, mont->N.d, mont->n0, num)) {\n            r->neg = a->neg ^ b->neg;\n            r->top = num;\n            r->flags |= BN_FLG_FIXED_TOP;\n            return 1;\n        }\n    }\n#endif\n    if ((a->top + b->top) > 2 * num)\n        return 0;\n    BN_CTX_start(ctx);\n    tmp = BN_CTX_get(ctx);\n    if (tmp == NULL)\n        goto err;\n    bn_check_top(tmp);\n    if (a == b) {\n        if (!bn_sqr_fixed_top(tmp, a, ctx))\n            goto err;\n    } else {\n        if (!bn_mul_fixed_top(tmp, a, b, ctx))\n            goto err;\n    }\n#ifdef MONT_WORD\n    if (!bn_from_montgomery_word(r, tmp, mont))\n        goto err;\n#else\n    if (!BN_from_montgomery(r, tmp, mont, ctx))\n        goto err;\n#endif\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    return ret;\n}', 'int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n    int max, al;\n    int ret = 0;\n    BIGNUM *tmp, *rr;\n    bn_check_top(a);\n    al = a->top;\n    if (al <= 0) {\n        r->top = 0;\n        r->neg = 0;\n        return 1;\n    }\n    BN_CTX_start(ctx);\n    rr = (a != r) ? r : BN_CTX_get(ctx);\n    tmp = BN_CTX_get(ctx);\n    if (rr == NULL || tmp == NULL)\n        goto err;\n    max = 2 * al;\n    if (bn_wexpand(rr, max) == NULL)\n        goto err;\n    if (al == 4) {\n#ifndef BN_SQR_COMBA\n        BN_ULONG t[8];\n        bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n        bn_sqr_comba4(rr->d, a->d);\n#endif\n    } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n        BN_ULONG t[16];\n        bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n        bn_sqr_comba8(rr->d, a->d);\n#endif\n    } else {\n#if defined(BN_RECURSION)\n        if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n            BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n            bn_sqr_normal(rr->d, a->d, al, t);\n        } else {\n            int j, k;\n            j = BN_num_bits_word((BN_ULONG)al);\n            j = 1 << (j - 1);\n            k = j + j;\n            if (al == j) {\n                if (bn_wexpand(tmp, k * 2) == NULL)\n                    goto err;\n                bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n            } else {\n                if (bn_wexpand(tmp, max) == NULL)\n                    goto err;\n                bn_sqr_normal(rr->d, a->d, al, tmp->d);\n            }\n        }\n#else\n        if (bn_wexpand(tmp, max) == NULL)\n            goto err;\n        bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n    }\n    rr->neg = 0;\n    rr->top = max;\n    rr->flags |= BN_FLG_FIXED_TOP;\n    if (r != rr && BN_copy(r, rr) == NULL)\n        goto err;\n    ret = 1;\n err:\n    bn_check_top(rr);\n    bn_check_top(tmp);\n    BN_CTX_end(ctx);\n    return ret;\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n    int i, j, max;\n    const BN_ULONG *ap;\n    BN_ULONG *rp;\n    max = n * 2;\n    ap = a;\n    rp = r;\n    rp[0] = rp[max - 1] = 0;\n    rp++;\n    j = n;\n    if (--j > 0) {\n        ap++;\n        rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n        rp += 2;\n    }\n    for (i = n - 2; i > 0; i--) {\n        j--;\n        ap++;\n        rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n        rp += 2;\n    }\n    bn_add_words(r, r, r, max);\n    bn_sqr_words(tmp, a, n);\n    bn_add_words(r, r, tmp, max);\n}']