crypto/dsa/dsa_ossl.c:405: error: BUFFER_OVERRUN_L3
  Offset: [1, +oo] Size: [0, 8388607] by call to `BN_mod_exp2_mont`.
Showing all 15 steps of the trace


crypto/dsa/dsa_ossl.c:387:9: Call
385.            */
386.           dgst_len = (i >> 3);
387.       if (BN_bin2bn(dgst, dgst_len, u1) == NULL)
               ^
388.           goto err;
389.   

crypto/bn/bn_lib.c:542:1: Parameter `*ret->d`
  540.   }
  541.   
  542. > BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)
  543.   {
  544.       unsigned int i, m;

crypto/dsa/dsa_ossl.c:405:5: Call
403.       }
404.   
405.       DSA_MOD_EXP(goto err, dsa, t1, dsa->g, u1, dsa->pub_key, u2, dsa->p, ctx,
           ^
406.                   mont);
407.       /* BN_copy(&u1,&t1); */

crypto/bn/bn_exp2.c:118:1: Parameter `*rr->d`
  116.   #define TABLE_SIZE      32
  117.   
  118. > int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,
  119.                        const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,
  120.                        BN_CTX *ctx, BN_MONT_CTX *in_mont)

crypto/bn/bn_exp2.c:294:10: Call
  292.           }
  293.       }
  294.       if (!BN_from_montgomery(rr, r, mont, ctx))
                  ^
  295.           goto err;
  296.       ret = 1;

crypto/bn/bn_mont.c:268:1: Parameter `*ret->d`
    266.   #endif                          /* MONT_WORD */
    267.   
    268. > int BN_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,
    269.                          BN_CTX *ctx)
    270.   {

crypto/bn/bn_mont.c:277:16: Call
    275.       BN_CTX_start(ctx);
    276.       if ((t = BN_CTX_get(ctx)) && BN_copy(t, a))
    277.           retn = BN_from_montgomery_word(ret, t, mont);
                          ^
    278.       BN_CTX_end(ctx);
    279.   #else                           /* !MONT_WORD */

crypto/bn/bn_mont.c:237:14: <Offset trace>
235.               (BN_ULONG *)(((PTR_SIZE_INT) rp & ~m) | ((PTR_SIZE_INT) ap & m));
236.   
237.           for (i = 0, nl -= 4; i < nl; i += 4) {
                    ^
238.               BN_ULONG t1, t2, t3, t4;
239.   

crypto/bn/bn_mont.c:237:14: Assignment
      235.               (BN_ULONG *)(((PTR_SIZE_INT) rp & ~m) | ((PTR_SIZE_INT) ap & m));
      236.   
      237.           for (i = 0, nl -= 4; i < nl; i += 4) {
                          ^
      238.               BN_ULONG t1, t2, t3, t4;
      239.   

crypto/bn/bn_mont.c:177:1: <Length trace>
175.   
176.   #ifdef MONT_WORD
177. > static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
178.   {
179.       BIGNUM *n;

crypto/bn/bn_mont.c:177:1: Parameter `*ret->d`
      175.   
      176.   #ifdef MONT_WORD
      177. > static int BN_from_montgomery_word(BIGNUM *ret, BIGNUM *r, BN_MONT_CTX *mont)
      178.   {
      179.       BIGNUM *n;

crypto/bn/bn_mont.c:214:9: Call
      212.       }
      213.   
      214.       if (bn_wexpand(ret, nl) == NULL)
                     ^
      215.           return (0);
      216.       ret->top = nl;

crypto/bn/bn_lib.c:995:1: Parameter `*a->d`
        993.   }
        994.   
        995. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
        996.   {
        997.       return (words <= a->dmax) ? a : bn_expand2(a, words);

crypto/bn/bn_mont.c:219:5: Assignment
      217.       ret->neg = r->neg;
      218.   
      219.       rp = ret->d;
                 ^
      220.       ap = &(r->d[nl]);
      221.   

crypto/bn/bn_mont.c:248:13: Array access: Offset: [1, +oo] Size: [0, 8388607] by call to `BN_mod_exp2_mont` 
      246.               rp[i + 0] = t1;
      247.               ap[i + 2] = 0;
      248.               rp[i + 1] = t2;
                         ^
      249.               ap[i + 3] = 0;
      250.               rp[i + 2] = t3;

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 dsa_do_verify(const unsigned char *dgst, int dgst_len,\n                         DSA_SIG *sig, DSA *dsa)\n{\n    BN_CTX *ctx;\n    BIGNUM *u1, *u2, *t1;\n    BN_MONT_CTX *mont = NULL;\n    int ret = -1, i;\n    if (!dsa->p || !dsa->q || !dsa->g) {\n        DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MISSING_PARAMETERS);\n        return -1;\n    }\n    i = BN_num_bits(dsa->q);\n    if (i != 160 && i != 224 && i != 256) {\n        DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_BAD_Q_VALUE);\n        return -1;\n    }\n    if (BN_num_bits(dsa->p) > OPENSSL_DSA_MAX_MODULUS_BITS) {\n        DSAerr(DSA_F_DSA_DO_VERIFY, DSA_R_MODULUS_TOO_LARGE);\n        return -1;\n    }\n    u1 = BN_new();\n    u2 = BN_new();\n    t1 = BN_new();\n    ctx = BN_CTX_new();\n    if (u1 == NULL || u2 == NULL || t1 == NULL || ctx == NULL)\n        goto err;\n    if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||\n        BN_ucmp(sig->r, dsa->q) >= 0) {\n        ret = 0;\n        goto err;\n    }\n    if (BN_is_zero(sig->s) || BN_is_negative(sig->s) ||\n        BN_ucmp(sig->s, dsa->q) >= 0) {\n        ret = 0;\n        goto err;\n    }\n    if ((BN_mod_inverse(u2, sig->s, dsa->q, ctx)) == NULL)\n        goto err;\n    if (dgst_len > (i >> 3))\n        dgst_len = (i >> 3);\n    if (BN_bin2bn(dgst, dgst_len, u1) == NULL)\n        goto err;\n    if (!BN_mod_mul(u1, u1, u2, dsa->q, ctx))\n        goto err;\n    if (!BN_mod_mul(u2, sig->r, u2, dsa->q, ctx))\n        goto err;\n    if (dsa->flags & DSA_FLAG_CACHE_MONT_P) {\n        mont = BN_MONT_CTX_set_locked(&dsa->method_mont_p,\n                                      CRYPTO_LOCK_DSA, dsa->p, ctx);\n        if (!mont)\n            goto err;\n    }\n    DSA_MOD_EXP(goto err, dsa, t1, dsa->g, u1, dsa->pub_key, u2, dsa->p, ctx,\n                mont);\n    if (!BN_mod(u1, t1, dsa->q, ctx))\n        goto err;\n    ret = (BN_ucmp(u1, sig->r) == 0);\n err:\n    if (ret < 0)\n        DSAerr(DSA_F_DSA_DO_VERIFY, ERR_R_BN_LIB);\n    BN_CTX_free(ctx);\n    BN_free(u1);\n    BN_free(u2);\n    BN_free(t1);\n    return (ret);\n}', 'BIGNUM *BN_bin2bn(const unsigned char *s, int len, BIGNUM *ret)\n{\n    unsigned int i, m;\n    unsigned int n;\n    BN_ULONG l;\n    BIGNUM *bn = NULL;\n    if (ret == NULL)\n        ret = bn = BN_new();\n    if (ret == NULL)\n        return (NULL);\n    bn_check_top(ret);\n    for ( ; len > 0 && *s == 0; s++, len--)\n        continue;\n    n = len;\n    if (n == 0) {\n        ret->top = 0;\n        return (ret);\n    }\n    i = ((n - 1) / BN_BYTES) + 1;\n    m = ((n - 1) % (BN_BYTES));\n    if (bn_wexpand(ret, (int)i) == NULL) {\n        BN_free(bn);\n        return NULL;\n    }\n    ret->top = i;\n    ret->neg = 0;\n    l = 0;\n    while (n--) {\n        l = (l << 8L) | *(s++);\n        if (m-- == 0) {\n            ret->d[--i] = l;\n            l = 0;\n            m = BN_BYTES - 1;\n        }\n    }\n    bn_correct_top(ret);\n    return (ret);\n}', 'int BN_mod_exp2_mont(BIGNUM *rr, const BIGNUM *a1, const BIGNUM *p1,\n                     const BIGNUM *a2, const BIGNUM *p2, const BIGNUM *m,\n                     BN_CTX *ctx, BN_MONT_CTX *in_mont)\n{\n    int i, j, bits, b, bits1, bits2, ret =\n        0, wpos1, wpos2, window1, window2, wvalue1, wvalue2;\n    int r_is_one = 1;\n    BIGNUM *d, *r;\n    const BIGNUM *a_mod_m;\n    BIGNUM *val1[TABLE_SIZE], *val2[TABLE_SIZE];\n    BN_MONT_CTX *mont = NULL;\n    bn_check_top(a1);\n    bn_check_top(p1);\n    bn_check_top(a2);\n    bn_check_top(p2);\n    bn_check_top(m);\n    if (!(m->d[0] & 1)) {\n        BNerr(BN_F_BN_MOD_EXP2_MONT, BN_R_CALLED_WITH_EVEN_MODULUS);\n        return (0);\n    }\n    bits1 = BN_num_bits(p1);\n    bits2 = BN_num_bits(p2);\n    if ((bits1 == 0) && (bits2 == 0)) {\n        ret = BN_one(rr);\n        return ret;\n    }\n    bits = (bits1 > bits2) ? bits1 : bits2;\n    BN_CTX_start(ctx);\n    d = BN_CTX_get(ctx);\n    r = BN_CTX_get(ctx);\n    val1[0] = BN_CTX_get(ctx);\n    val2[0] = BN_CTX_get(ctx);\n    if (!d || !r || !val1[0] || !val2[0])\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    window1 = BN_window_bits_for_exponent_size(bits1);\n    window2 = BN_window_bits_for_exponent_size(bits2);\n    if (a1->neg || BN_ucmp(a1, m) >= 0) {\n        if (!BN_mod(val1[0], a1, m, ctx))\n            goto err;\n        a_mod_m = val1[0];\n    } else\n        a_mod_m = a1;\n    if (BN_is_zero(a_mod_m)) {\n        BN_zero(rr);\n        ret = 1;\n        goto err;\n    }\n    if (!BN_to_montgomery(val1[0], a_mod_m, mont, ctx))\n        goto err;\n    if (window1 > 1) {\n        if (!BN_mod_mul_montgomery(d, val1[0], val1[0], mont, ctx))\n            goto err;\n        j = 1 << (window1 - 1);\n        for (i = 1; i < j; i++) {\n            if (((val1[i] = BN_CTX_get(ctx)) == NULL) ||\n                !BN_mod_mul_montgomery(val1[i], val1[i - 1], d, mont, ctx))\n                goto err;\n        }\n    }\n    if (a2->neg || BN_ucmp(a2, m) >= 0) {\n        if (!BN_mod(val2[0], a2, m, ctx))\n            goto err;\n        a_mod_m = val2[0];\n    } else\n        a_mod_m = a2;\n    if (BN_is_zero(a_mod_m)) {\n        BN_zero(rr);\n        ret = 1;\n        goto err;\n    }\n    if (!BN_to_montgomery(val2[0], a_mod_m, mont, ctx))\n        goto err;\n    if (window2 > 1) {\n        if (!BN_mod_mul_montgomery(d, val2[0], val2[0], mont, ctx))\n            goto err;\n        j = 1 << (window2 - 1);\n        for (i = 1; i < j; i++) {\n            if (((val2[i] = BN_CTX_get(ctx)) == NULL) ||\n                !BN_mod_mul_montgomery(val2[i], val2[i - 1], d, mont, ctx))\n                goto err;\n        }\n    }\n    r_is_one = 1;\n    wvalue1 = 0;\n    wvalue2 = 0;\n    wpos1 = 0;\n    wpos2 = 0;\n    if (!BN_to_montgomery(r, BN_value_one(), mont, ctx))\n        goto err;\n    for (b = bits - 1; b >= 0; b--) {\n        if (!r_is_one) {\n            if (!BN_mod_mul_montgomery(r, r, r, mont, ctx))\n                goto err;\n        }\n        if (!wvalue1)\n            if (BN_is_bit_set(p1, b)) {\n                i = b - window1 + 1;\n                while (!BN_is_bit_set(p1, i))\n                    i++;\n                wpos1 = i;\n                wvalue1 = 1;\n                for (i = b - 1; i >= wpos1; i--) {\n                    wvalue1 <<= 1;\n                    if (BN_is_bit_set(p1, i))\n                        wvalue1++;\n                }\n            }\n        if (!wvalue2)\n            if (BN_is_bit_set(p2, b)) {\n                i = b - window2 + 1;\n                while (!BN_is_bit_set(p2, i))\n                    i++;\n                wpos2 = i;\n                wvalue2 = 1;\n                for (i = b - 1; i >= wpos2; i--) {\n                    wvalue2 <<= 1;\n                    if (BN_is_bit_set(p2, i))\n                        wvalue2++;\n                }\n            }\n        if (wvalue1 && b == wpos1) {\n            if (!BN_mod_mul_montgomery(r, r, val1[wvalue1 >> 1], mont, ctx))\n                goto err;\n            wvalue1 = 0;\n            r_is_one = 0;\n        }\n        if (wvalue2 && b == wpos2) {\n            if (!BN_mod_mul_montgomery(r, r, val2[wvalue2 >> 1], mont, ctx))\n                goto err;\n            wvalue2 = 0;\n            r_is_one = 0;\n        }\n    }\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_from_montgomery(BIGNUM *ret, const BIGNUM *a, BN_MONT_CTX *mont,\n                       BN_CTX *ctx)\n{\n    int retn = 0;\n#ifdef MONT_WORD\n    BIGNUM *t;\n    BN_CTX_start(ctx);\n    if ((t = BN_CTX_get(ctx)) && BN_copy(t, a))\n        retn = BN_from_montgomery_word(ret, t, mont);\n    BN_CTX_end(ctx);\n#else\n    BIGNUM *t1, *t2;\n    BN_CTX_start(ctx);\n    t1 = BN_CTX_get(ctx);\n    t2 = BN_CTX_get(ctx);\n    if (t1 == NULL || t2 == NULL)\n        goto err;\n    if (!BN_copy(t1, a))\n        goto err;\n    BN_mask_bits(t1, mont->ri);\n    if (!BN_mul(t2, t1, &mont->Ni, ctx))\n        goto err;\n    BN_mask_bits(t2, mont->ri);\n    if (!BN_mul(t1, t2, &mont->N, ctx))\n        goto err;\n    if (!BN_add(t2, a, t1))\n        goto err;\n    if (!BN_rshift(ret, t2, mont->ri))\n        goto err;\n    if (BN_ucmp(ret, &(mont->N)) >= 0) {\n        if (!BN_usub(ret, ret, &(mont->N)))\n            goto err;\n    }\n    retn = 1;\n    bn_check_top(ret);\n err:\n    BN_CTX_end(ctx);\n#endif\n    return (retn);\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}']