crypto/bn/bn_exp.c:1218: error: BUFFER_OVERRUN_L3
  Offset: [1, +oo] (⇐ [0, +oo] + [1, +oo]) Size: [0, 8388607] by call to `BN_to_montgomery`.
Showing all 16 steps of the trace


crypto/bn/bn_exp.c:1132:1: Parameter `in_mont->N.top`
1130.   }
1131.   
1132. > int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,
1133.                            const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)
1134.   {

crypto/bn/bn_exp.c:1218:22: Call
1216.           if ((next_w / w) != w) { /* overflow */
1217.               if (r_is_one) {
1218.                   if (!BN_TO_MONTGOMERY_WORD(r, w, mont))
                             ^
1219.                       goto err;
1220.                   r_is_one = 0;

crypto/bn/bn_lib.c:878:1: Parameter `mont->N.top`
  876.   }
  877.   
  878. > int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
  879.                        BN_CTX *ctx)
  880.   {

crypto/bn/bn_lib.c:881:12: Call
  879.                        BN_CTX *ctx)
  880.   {
  881.       return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
                    ^
  882.   }
  883.   

crypto/bn/bn_mont.c:26:1: Parameter `mont->N.top`
    24.   #endif
    25.   
    26. > int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
    27.                             BN_MONT_CTX *mont, BN_CTX *ctx)
    28.   {

crypto/bn/bn_mont.c:29:15: Call
    27.                             BN_MONT_CTX *mont, BN_CTX *ctx)
    28.   {
    29.       int ret = bn_mul_mont_fixed_top(r, a, b, mont, ctx);
                        ^
    30.   
    31.       bn_correct_top(r);

crypto/bn/bn_mont.c:37:1: Parameter `mont->N.top`
      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:75:10: Call
      73.       /* reduce from aRR to aR */
      74.   #ifdef MONT_WORD
      75.       if (!bn_from_montgomery_word(r, tmp, mont))
                     ^
      76.           goto err;
      77.   #else

crypto/bn/bn_mont.c:123:21: <Offset trace>
121.        * includes |carry| which is stored separately.
122.        */
123.       for (carry = 0, i = 0; i < nl; i++, rp++) {
                           ^
124.           v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
125.           v = (v + carry + rp[nl]) & BN_MASK2;

crypto/bn/bn_mont.c:123:21: Assignment
        121.        * includes |carry| which is stored separately.
        122.        */
        123.       for (carry = 0, i = 0; i < nl; i++, rp++) {
                                   ^
        124.           v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
        125.           v = (v + carry + rp[nl]) & BN_MASK2;

crypto/bn/bn_mont.c:88:1: <Length trace>
86.   
87.   #ifdef MONT_WORD
88. > static int bn_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
89.   {
90.       BIGNUM *n;

crypto/bn/bn_mont.c:88:1: Parameter `*r->d`
        86.   
        87.   #ifdef MONT_WORD
        88. > static int bn_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
        89.   {
        90.       BIGNUM *n;

crypto/bn/bn_mont.c:102:9: Call
        100.   
        101.       max = (2 * nl);             /* carry is stored separately */
        102.       if (bn_wexpand(r, max) == NULL)
                       ^
        103.           return 0;
        104.   

crypto/bn/bn_lib.c:949:1: Parameter `*a->d`
          947.   }
          948.   
          949. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
          950.   {
          951.       return (words <= a->dmax) ? a : bn_expand2(a, words);

crypto/bn/bn_mont.c:107:5: Assignment
        105.       r->neg ^= n->neg;
        106.       np = n->d;
        107.       rp = r->d;
                   ^
        108.   
        109.       /* clear the top words of T */

crypto/bn/bn_mont.c:125:26: Array access: Offset: [1, +oo] (⇐ [0, +oo] + [1, +oo]) Size: [0, 8388607] by call to `BN_to_montgomery` 
        123.       for (carry = 0, i = 0; i < nl; i++, rp++) {
        124.           v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
        125.           v = (v + carry + rp[nl]) & BN_MASK2;
                                        ^
        126.           carry |= (v != rp[nl]);
        127.           carry &= (v <= rp[nl]);

static int bn_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
{
    BIGNUM *n;
    BN_ULONG *ap, *np, *rp, n0, v, carry;
    int nl, max, i;
    n = &(mont->N);
    nl = n->top;
    if (nl == 0) {
        ret->top = 0;
        return 1;
    }
    max = (2 * nl);
    if (bn_wexpand(r, max) == NULL)
        return 0;
    r->neg ^= n->neg;
    np = n->d;
    rp = r->d;
    i = max - r->top;
    if (i)
        memset(&rp[r->top], 0, sizeof(*rp) * i);
    r->top = max;
    r->flags |= BN_FLG_FIXED_TOP;
    n0 = mont->n0[0];
    for (carry = 0, i = 0; i < nl; i++, rp++) {
        v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
        v = (v + carry + rp[nl]) & BN_MASK2;
        carry |= (v != rp[nl]);
        carry &= (v <= rp[nl]);
        rp[nl] = v;
    }
    if (bn_wexpand(ret, nl) == NULL)
        return 0;
    ret->top = nl;
    ret->flags |= BN_FLG_FIXED_TOP;
    ret->neg = r->neg;
    rp = ret->d;
    ap = &(r->d[nl]);
    carry -= bn_sub_words(rp, ap, np, nl);
    for (i = 0; i < nl; i++) {
        rp[i] = (carry & ap[i]) | (~carry & rp[i]);
        ap[i] = 0;
    }
    return 1;
}
['int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,\n                         const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n    BN_MONT_CTX *mont = NULL;\n    int b, bits, ret = 0;\n    int r_is_one;\n    BN_ULONG w, next_w;\n    BIGNUM *r, *t;\n    BIGNUM *swap_tmp;\n#define BN_MOD_MUL_WORD(r, w, m) \\\n                (BN_mul_word(r, (w)) && \\\n                (   \\\n                        (BN_mod(t, r, m, ctx) && (swap_tmp = r, r = t, t = swap_tmp, 1))))\n#define BN_TO_MONTGOMERY_WORD(r, w, mont) \\\n                (BN_set_word(r, (w)) && BN_to_montgomery(r, r, (mont), ctx))\n    if (BN_get_flags(p, BN_FLG_CONSTTIME) != 0\n            || BN_get_flags(m, BN_FLG_CONSTTIME) != 0) {\n        BNerr(BN_F_BN_MOD_EXP_MONT_WORD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);\n        return 0;\n    }\n    bn_check_top(p);\n    bn_check_top(m);\n    if (!BN_is_odd(m)) {\n        BNerr(BN_F_BN_MOD_EXP_MONT_WORD, BN_R_CALLED_WITH_EVEN_MODULUS);\n        return 0;\n    }\n    if (m->top == 1)\n        a %= m->d[0];\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    if (a == 0) {\n        BN_zero(rr);\n        ret = 1;\n        return ret;\n    }\n    BN_CTX_start(ctx);\n    r = BN_CTX_get(ctx);\n    t = BN_CTX_get(ctx);\n    if (t == 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    r_is_one = 1;\n    w = a;\n    for (b = bits - 2; b >= 0; b--) {\n        next_w = w * w;\n        if ((next_w / w) != w) {\n            if (r_is_one) {\n                if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n                    goto err;\n                r_is_one = 0;\n            } else {\n                if (!BN_MOD_MUL_WORD(r, w, m))\n                    goto err;\n            }\n            next_w = 1;\n        }\n        w = next_w;\n        if (!r_is_one) {\n            if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n                goto err;\n        }\n        if (BN_is_bit_set(p, b)) {\n            next_w = w * a;\n            if ((next_w / a) != w) {\n                if (r_is_one) {\n                    if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n                        goto err;\n                    r_is_one = 0;\n                } else {\n                    if (!BN_MOD_MUL_WORD(r, w, m))\n                        goto err;\n                }\n                next_w = a;\n            }\n            w = next_w;\n        }\n    }\n    if (w != 1) {\n        if (r_is_one) {\n            if (!BN_TO_MONTGOMERY_WORD(r, w, mont))\n                goto err;\n            r_is_one = 0;\n        } else {\n            if (!BN_MOD_MUL_WORD(r, w, m))\n                goto err;\n        }\n    }\n    if (r_is_one) {\n        if (!BN_one(rr))\n            goto err;\n    } else {\n        if (!BN_from_montgomery(rr, r, mont, ctx))\n            goto err;\n    }\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_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,\n                     BN_CTX *ctx)\n{\n    return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);\n}', 'int BN_mod_mul_montgomery(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,\n                          BN_MONT_CTX *mont, BN_CTX *ctx)\n{\n    int ret = bn_mul_mont_fixed_top(r, a, b, mont, ctx);\n    bn_correct_top(r);\n    bn_check_top(r);\n    return ret;\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(tmp, a, ctx))\n            goto err;\n    } else {\n        if (!BN_mul(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}', 'static int bn_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)\n{\n    BIGNUM *n;\n    BN_ULONG *ap, *np, *rp, n0, v, carry;\n    int nl, max, i;\n    n = &(mont->N);\n    nl = n->top;\n    if (nl == 0) {\n        ret->top = 0;\n        return 1;\n    }\n    max = (2 * nl);\n    if (bn_wexpand(r, max) == NULL)\n        return 0;\n    r->neg ^= n->neg;\n    np = n->d;\n    rp = r->d;\n    i = max - r->top;\n    if (i)\n        memset(&rp[r->top], 0, sizeof(*rp) * i);\n    r->top = max;\n    r->flags |= BN_FLG_FIXED_TOP;\n    n0 = mont->n0[0];\n    for (carry = 0, i = 0; i < nl; i++, rp++) {\n        v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);\n        v = (v + carry + rp[nl]) & BN_MASK2;\n        carry |= (v != rp[nl]);\n        carry &= (v <= rp[nl]);\n        rp[nl] = v;\n    }\n    if (bn_wexpand(ret, nl) == NULL)\n        return 0;\n    ret->top = nl;\n    ret->flags |= BN_FLG_FIXED_TOP;\n    ret->neg = r->neg;\n    rp = ret->d;\n    ap = &(r->d[nl]);\n    carry -= bn_sub_words(rp, ap, np, nl);\n    for (i = 0; i < nl; i++) {\n        rp[i] = (carry & ap[i]) | (~carry & rp[i]);\n        ap[i] = 0;\n    }\n    return 1;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n    return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']