test/bntest.c:1305: error: BUFFER_OVERRUN_L3
  Offset: [1, +oo] (⇐ [0, +oo] + [1, +oo]) Size: [0, 8388607] by call to `BN_to_montgomery`.
Showing all 15 steps of the trace


test/bntest.c:1302:21: Call
1300.           BIGNUM *b_tmp = BN_new();
1301.           if (mont == NULL || a_tmp == NULL || b_tmp == NULL
1302.                   || !BN_MONT_CTX_set(mont, m, ctx)
                            ^
1303.                   || !BN_nnmod(a_tmp, a, m, ctx)
1304.                   || !BN_nnmod(b_tmp, b, m, ctx)

crypto/bn/bn_mont.c:247:1: Parameter `mont->N.top`
  245.   }
  246.   
  247. > int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)
  248.   {
  249.       int ret = 0;

test/bntest.c:1305:21: Call
1303.                   || !BN_nnmod(a_tmp, a, m, ctx)
1304.                   || !BN_nnmod(b_tmp, b, m, ctx)
1305.                   || !BN_to_montgomery(a_tmp, a_tmp, mont, ctx)
                            ^
1306.                   || !BN_to_montgomery(b_tmp, b_tmp, mont, ctx)
1307.                   || !BN_mod_mul_montgomery(ret, a_tmp, b_tmp, mont, ctx)

crypto/bn/bn_lib.c:945:1: Parameter `mont->N.top`
  943.   }
  944.   
  945. > int BN_to_montgomery(BIGNUM *r, const BIGNUM *a, BN_MONT_CTX *mont,
  946.                        BN_CTX *ctx)
  947.   {

crypto/bn/bn_lib.c:948:12: Call
  946.                        BN_CTX *ctx)
  947.   {
  948.       return BN_mod_mul_montgomery(r, a, &(mont->RR), mont, ctx);
                    ^
  949.   }
  950.   

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:61:10: Call
    59.       /* reduce from aRR to aR */
    60.   #ifdef MONT_WORD
    61.       if (!BN_from_montgomery_word(r, tmp, mont))
                   ^
    62.           goto err;
    63.   #else

crypto/bn/bn_mont.c:104:21: <Offset trace>
102.       n0 = mont->n0[0];
103.   
104.       for (carry = 0, i = 0; i < nl; i++, rp++) {
                           ^
105.           v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
106.           v = (v + carry + rp[nl]) & BN_MASK2;

crypto/bn/bn_mont.c:104:21: Assignment
      102.       n0 = mont->n0[0];
      103.   
      104.       for (carry = 0, i = 0; i < nl; i++, rp++) {
                                 ^
      105.           v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
      106.           v = (v + carry + rp[nl]) & BN_MASK2;

crypto/bn/bn_mont.c:75:1: <Length trace>
73.   
74.   #ifdef MONT_WORD
75. > static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
76.   {
77.       BIGNUM *n;

crypto/bn/bn_mont.c:75:1: Parameter `*r->d`
      73.   
      74.   #ifdef MONT_WORD
      75. > static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
      76.   {
      77.       BIGNUM *n;

crypto/bn/bn_mont.c:89:9: Call
      87.   
      88.       max = (2 * nl);             /* carry is stored separately */
      89.       if (bn_wexpand(r, max) == NULL)
                    ^
      90.           return (0);
      91.   

crypto/bn/bn_lib.c:1016:1: Parameter `*a->d`
        1014.   }
        1015.   
        1016. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
        1017.   {
        1018.       return (words <= a->dmax) ? a : bn_expand2(a, words);

crypto/bn/bn_mont.c:94:5: Assignment
      92.       r->neg ^= n->neg;
      93.       np = n->d;
      94.       rp = r->d;
                ^
      95.   
      96.       /* clear the top words of T */

crypto/bn/bn_mont.c:106:26: Array access: Offset: [1, +oo] (⇐ [0, +oo] + [1, +oo]) Size: [0, 8388607] by call to `BN_to_montgomery` 
      104.       for (carry = 0, i = 0; i < nl; i++, rp++) {
      105.           v = bn_mul_add_words(rp, np, nl, (rp[0] * n0) & BN_MASK2);
      106.           v = (v + carry + rp[nl]) & BN_MASK2;
                                      ^
      107.           carry |= (v != rp[nl]);
      108.           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;
    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->neg = r->neg;
    rp = ret->d;
    ap = &(r->d[nl]);
# define BRANCH_FREE 1
# if BRANCH_FREE
    {
        BN_ULONG *nrp;
        size_t m;
        v = bn_sub_words(rp, ap, np, nl) - carry;
        m = (0 - (size_t)v);
        nrp =
            (BN_ULONG *)(((PTR_SIZE_INT) rp & ~m) | ((PTR_SIZE_INT) ap & m));
        for (i = 0, nl -= 4; i < nl; i += 4) {
            BN_ULONG t1, t2, t3, t4;
            t1 = nrp[i + 0];
            t2 = nrp[i + 1];
            t3 = nrp[i + 2];
            ap[i + 0] = 0;
            t4 = nrp[i + 3];
            ap[i + 1] = 0;
            rp[i + 0] = t1;
            ap[i + 2] = 0;
            rp[i + 1] = t2;
            ap[i + 3] = 0;
            rp[i + 2] = t3;
            rp[i + 3] = t4;
        }
        for (nl += 4; i < nl; i++)
            rp[i] = nrp[i], ap[i] = 0;
    }
# else
    if (bn_sub_words(rp, ap, np, nl) - carry)
        memcpy(rp, ap, nl * sizeof(BN_ULONG));
# endif
    bn_correct_top(r);
    bn_correct_top(ret);
    bn_check_top(ret);
    return (1);
}
['static int file_modmul(STANZA *s)\n{\n    BIGNUM *a = getBN(s, "A");\n    BIGNUM *b = getBN(s, "B");\n    BIGNUM *m = getBN(s, "M");\n    BIGNUM *mod_mul = getBN(s, "ModMul");\n    BIGNUM *ret = BN_new();\n    int st = 0;\n    if (a == NULL || b == NULL || m == NULL || mod_mul == NULL || ret == NULL)\n        goto err;\n    if (!BN_mod_mul(ret, a, b, m, ctx)\n            || !equalBN("A * B (mod M)", mod_mul, ret))\n        goto err;\n    if (BN_is_odd(m)) {\n        BN_MONT_CTX *mont = BN_MONT_CTX_new();\n        BIGNUM *a_tmp = BN_new();\n        BIGNUM *b_tmp = BN_new();\n        if (mont == NULL || a_tmp == NULL || b_tmp == NULL\n                || !BN_MONT_CTX_set(mont, m, ctx)\n                || !BN_nnmod(a_tmp, a, m, ctx)\n                || !BN_nnmod(b_tmp, b, m, ctx)\n                || !BN_to_montgomery(a_tmp, a_tmp, mont, ctx)\n                || !BN_to_montgomery(b_tmp, b_tmp, mont, ctx)\n                || !BN_mod_mul_montgomery(ret, a_tmp, b_tmp, mont, ctx)\n                || !BN_from_montgomery(ret, ret, mont, ctx)\n                || !equalBN("A * B (mod M) (mont)", mod_mul, ret)) {\n            st = 0;\n        } else {\n            st = 1;\n        }\n        BN_MONT_CTX_free(mont);\n        BN_free(a_tmp);\n        BN_free(b_tmp);\n        if (st == 0)\n            goto err;\n    }\n    st = 1;\nerr:\n    BN_free(a);\n    BN_free(b);\n    BN_free(m);\n    BN_free(mod_mul);\n    BN_free(ret);\n    return st;\n}', 'int BN_MONT_CTX_set(BN_MONT_CTX *mont, const BIGNUM *mod, BN_CTX *ctx)\n{\n    int ret = 0;\n    BIGNUM *Ri, *R;\n    if (BN_is_zero(mod))\n        return 0;\n    BN_CTX_start(ctx);\n    if ((Ri = BN_CTX_get(ctx)) == NULL)\n        goto err;\n    R = &(mont->RR);\n    if (!BN_copy(&(mont->N), mod))\n        goto err;\n    mont->N.neg = 0;\n#ifdef MONT_WORD\n    {\n        BIGNUM tmod;\n        BN_ULONG buf[2];\n        bn_init(&tmod);\n        tmod.d = buf;\n        tmod.dmax = 2;\n        tmod.neg = 0;\n        mont->ri = (BN_num_bits(mod) + (BN_BITS2 - 1)) / BN_BITS2 * BN_BITS2;\n# if defined(OPENSSL_BN_ASM_MONT) && (BN_BITS2<=32)\n        BN_zero(R);\n        if (!(BN_set_bit(R, 2 * BN_BITS2)))\n            goto err;\n        tmod.top = 0;\n        if ((buf[0] = mod->d[0]))\n            tmod.top = 1;\n        if ((buf[1] = mod->top > 1 ? mod->d[1] : 0))\n            tmod.top = 2;\n        if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n            goto err;\n        if (!BN_lshift(Ri, Ri, 2 * BN_BITS2))\n            goto err;\n        if (!BN_is_zero(Ri)) {\n            if (!BN_sub_word(Ri, 1))\n                goto err;\n        } else {\n            if (bn_expand(Ri, (int)sizeof(BN_ULONG) * 2) == NULL)\n                goto err;\n            Ri->neg = 0;\n            Ri->d[0] = BN_MASK2;\n            Ri->d[1] = BN_MASK2;\n            Ri->top = 2;\n        }\n        if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n            goto err;\n        mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n        mont->n0[1] = (Ri->top > 1) ? Ri->d[1] : 0;\n# else\n        BN_zero(R);\n        if (!(BN_set_bit(R, BN_BITS2)))\n            goto err;\n        buf[0] = mod->d[0];\n        buf[1] = 0;\n        tmod.top = buf[0] != 0 ? 1 : 0;\n        if ((BN_mod_inverse(Ri, R, &tmod, ctx)) == NULL)\n            goto err;\n        if (!BN_lshift(Ri, Ri, BN_BITS2))\n            goto err;\n        if (!BN_is_zero(Ri)) {\n            if (!BN_sub_word(Ri, 1))\n                goto err;\n        } else {\n            if (!BN_set_word(Ri, BN_MASK2))\n                goto err;\n        }\n        if (!BN_div(Ri, NULL, Ri, &tmod, ctx))\n            goto err;\n        mont->n0[0] = (Ri->top > 0) ? Ri->d[0] : 0;\n        mont->n0[1] = 0;\n# endif\n    }\n#else\n    {\n        mont->ri = BN_num_bits(&mont->N);\n        BN_zero(R);\n        if (!BN_set_bit(R, mont->ri))\n            goto err;\n        if ((BN_mod_inverse(Ri, R, &mont->N, ctx)) == NULL)\n            goto err;\n        if (!BN_lshift(Ri, Ri, mont->ri))\n            goto err;\n        if (!BN_sub_word(Ri, 1))\n            goto err;\n        if (!BN_div(&(mont->Ni), NULL, Ri, &mont->N, ctx))\n            goto err;\n    }\n#endif\n    BN_zero(&(mont->RR));\n    if (!BN_set_bit(&(mont->RR), mont->ri * 2))\n        goto err;\n    if (!BN_mod(&(mont->RR), &(mont->RR), &(mont->N), ctx))\n        goto err;\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\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    BIGNUM *tmp;\n    int ret = 0;\n#if defined(OPENSSL_BN_ASM_MONT) && defined(MONT_WORD)\n    int num = mont->N.top;\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            bn_correct_top(r);\n            return (1);\n        }\n    }\n#endif\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    bn_check_top(r);\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    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->neg = r->neg;\n    rp = ret->d;\n    ap = &(r->d[nl]);\n# define BRANCH_FREE 1\n# if BRANCH_FREE\n    {\n        BN_ULONG *nrp;\n        size_t m;\n        v = bn_sub_words(rp, ap, np, nl) - carry;\n        m = (0 - (size_t)v);\n        nrp =\n            (BN_ULONG *)(((PTR_SIZE_INT) rp & ~m) | ((PTR_SIZE_INT) ap & m));\n        for (i = 0, nl -= 4; i < nl; i += 4) {\n            BN_ULONG t1, t2, t3, t4;\n            t1 = nrp[i + 0];\n            t2 = nrp[i + 1];\n            t3 = nrp[i + 2];\n            ap[i + 0] = 0;\n            t4 = nrp[i + 3];\n            ap[i + 1] = 0;\n            rp[i + 0] = t1;\n            ap[i + 2] = 0;\n            rp[i + 1] = t2;\n            ap[i + 3] = 0;\n            rp[i + 2] = t3;\n            rp[i + 3] = t4;\n        }\n        for (nl += 4; i < nl; i++)\n            rp[i] = nrp[i], ap[i] = 0;\n    }\n# else\n    if (bn_sub_words(rp, ap, np, nl) - carry)\n        memcpy(rp, ap, nl * sizeof(BN_ULONG));\n# endif\n    bn_correct_top(r);\n    bn_correct_top(ret);\n    bn_check_top(ret);\n    return (1);\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n    return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']