test/bntest.c:1402: error: BUFFER_OVERRUN_L3
  Offset: [1, +oo] Size: [0, 8388607] by call to `BN_GF2m_mod_inv`.
Showing all 13 steps of the trace


test/bntest.c:1402:13: Call
1400.           BN_bntest_rand(a, 512, 0, 0);
1401.           for (j = 0; j < 2; j++) {
1402.               BN_GF2m_mod_inv(c, a, b[j], ctx);
                    ^
1403.               BN_GF2m_mod_mul(d, a, c, b[j], ctx);
1404.               /* Test that ((1/a)*a) = 1. */

crypto/bn/bn_gf2m.c:560:1: Parameter `r->top`
  558.    * Curve Cryptography Over Binary Fields".
  559.    */
  560. > int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
  561.   {
  562.       BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;

test/bntest.c:1402:13: Call
1400.           BN_bntest_rand(a, 512, 0, 0);
1401.           for (j = 0; j < 2; j++) {
1402.               BN_GF2m_mod_inv(c, a, b[j], ctx);
                    ^
1403.               BN_GF2m_mod_mul(d, a, c, b[j], ctx);
1404.               /* Test that ((1/a)*a) = 1. */

crypto/bn/bn_gf2m.c:639:14: <Offset trace>
637.           bdp = b->d;
638.           bdp[0] = 1;
639.           for (i = 1; i < top; i++)
                    ^
640.               bdp[i] = 0;
641.           b->top = top;

crypto/bn/bn_gf2m.c:639:14: Assignment
  637.           bdp = b->d;
  638.           bdp[0] = 1;
  639.           for (i = 1; i < top; i++)
                      ^
  640.               bdp[i] = 0;
  641.           b->top = top;

crypto/bn/bn_gf2m.c:570:14: <Length trace>
568.       BN_CTX_start(ctx);
569.   
570.       if ((b = BN_CTX_get(ctx)) == NULL)
                    ^
571.           goto err;
572.       if ((c = BN_CTX_get(ctx)) == NULL)

crypto/bn/bn_gf2m.c:570:14: Call
  568.       BN_CTX_start(ctx);
  569.   
  570.       if ((b = BN_CTX_get(ctx)) == NULL)
                      ^
  571.           goto err;
  572.       if ((c = BN_CTX_get(ctx)) == NULL)

crypto/bn/bn_ctx.c:229:5: Call
    227.       }
    228.       /* OK, make sure the returned bignum is "zero" */
    229.       BN_zero(ret);
               ^
    230.       ctx->used++;
    231.       CTXDBG_RET(ctx, ret);

crypto/bn/bn_lib.c:463:1: Parameter `*a->d`
      461.   }
      462.   
      463. > int BN_set_word(BIGNUM *a, BN_ULONG w)
      464.   {
      465.       bn_check_top(a);

crypto/bn/bn_lib.c:466:9: Call
      464.   {
      465.       bn_check_top(a);
      466.       if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
                     ^
      467.           return (0);
      468.       a->neg = 0;

crypto/bn/bn_lcl.h:676:1: Parameter `*a->d`
        674.   int bn_probable_prime_dh_coprime(BIGNUM *rnd, int bits, BN_CTX *ctx);
        675.   
        676. > static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)
        677.   {
        678.       if (bits > (INT_MAX - BN_BITS2 + 1))

crypto/bn/bn_gf2m.c:637:9: Assignment
  635.           if (!bn_wexpand(b, top))
  636.             goto err;
  637.           bdp = b->d;
                 ^
  638.           bdp[0] = 1;
  639.           for (i = 1; i < top; i++)

crypto/bn/bn_gf2m.c:640:13: Array access: Offset: [1, +oo] Size: [0, 8388607] by call to `BN_GF2m_mod_inv` 
  638.           bdp[0] = 1;
  639.           for (i = 1; i < top; i++)
  640.               bdp[i] = 0;
                     ^
  641.           b->top = top;
  642.           if (!bn_wexpand(c, top))

int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)
{
    BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;
    int ret = 0;
    bn_check_top(a);
    bn_check_top(p);
    BN_CTX_start(ctx);
    if ((b = BN_CTX_get(ctx)) == NULL)
        goto err;
    if ((c = BN_CTX_get(ctx)) == NULL)
        goto err;
    if ((u = BN_CTX_get(ctx)) == NULL)
        goto err;
    if ((v = BN_CTX_get(ctx)) == NULL)
        goto err;
    if (!BN_GF2m_mod(u, a, p))
        goto err;
    if (BN_is_zero(u))
        goto err;
    if (!BN_copy(v, p))
        goto err;
# if 0
    if (!BN_one(b))
        goto err;
    while (1) {
        while (!BN_is_odd(u)) {
            if (BN_is_zero(u))
                goto err;
            if (!BN_rshift1(u, u))
                goto err;
            if (BN_is_odd(b)) {
                if (!BN_GF2m_add(b, b, p))
                    goto err;
            }
            if (!BN_rshift1(b, b))
                goto err;
        }
        if (BN_abs_is_word(u, 1))
            break;
        if (BN_num_bits(u) < BN_num_bits(v)) {
            tmp = u;
            u = v;
            v = tmp;
            tmp = b;
            b = c;
            c = tmp;
        }
        if (!BN_GF2m_add(u, u, v))
            goto err;
        if (!BN_GF2m_add(b, b, c))
            goto err;
    }
# else
    {
        int i;
        int ubits = BN_num_bits(u);
        int vbits = BN_num_bits(v);
        int top = p->top;
        BN_ULONG *udp, *bdp, *vdp, *cdp;
        if (!bn_wexpand(u, top))
            goto err;
        udp = u->d;
        for (i = u->top; i < top; i++)
            udp[i] = 0;
        u->top = top;
        if (!bn_wexpand(b, top))
          goto err;
        bdp = b->d;
        bdp[0] = 1;
        for (i = 1; i < top; i++)
            bdp[i] = 0;
        b->top = top;
        if (!bn_wexpand(c, top))
          goto err;
        cdp = c->d;
        for (i = 0; i < top; i++)
            cdp[i] = 0;
        c->top = top;
        vdp = v->d;
        while (1) {
            while (ubits && !(udp[0] & 1)) {
                BN_ULONG u0, u1, b0, b1, mask;
                u0 = udp[0];
                b0 = bdp[0];
                mask = (BN_ULONG)0 - (b0 & 1);
                b0 ^= p->d[0] & mask;
                for (i = 0; i < top - 1; i++) {
                    u1 = udp[i + 1];
                    udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2;
                    u0 = u1;
                    b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);
                    bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2;
                    b0 = b1;
                }
                udp[i] = u0 >> 1;
                bdp[i] = b0 >> 1;
                ubits--;
            }
            if (ubits <= BN_BITS2) {
                if (udp[0] == 0)
                    goto err;
                if (udp[0] == 1)
                    break;
            }
            if (ubits < vbits) {
                i = ubits;
                ubits = vbits;
                vbits = i;
                tmp = u;
                u = v;
                v = tmp;
                tmp = b;
                b = c;
                c = tmp;
                udp = vdp;
                vdp = v->d;
                bdp = cdp;
                cdp = c->d;
            }
            for (i = 0; i < top; i++) {
                udp[i] ^= vdp[i];
                bdp[i] ^= cdp[i];
            }
            if (ubits == vbits) {
                BN_ULONG ul;
                int utop = (ubits - 1) / BN_BITS2;
                while ((ul = udp[utop]) == 0 && utop)
                    utop--;
                ubits = utop * BN_BITS2 + BN_num_bits_word(ul);
            }
        }
        bn_correct_top(b);
    }
# endif
    if (!BN_copy(r, b))
        goto err;
    bn_check_top(r);
    ret = 1;
 err:
# ifdef BN_DEBUG
    bn_correct_top(c);
    bn_correct_top(u);
    bn_correct_top(v);
# endif
    BN_CTX_end(ctx);
    return ret;
}
['int test_gf2m_mod_inv(BIO *bp, BN_CTX *ctx)\n{\n    BIGNUM *a, *b[2], *c, *d;\n    int i, j, ret = 0;\n    int p0[] = { 163, 7, 6, 3, 0, -1 };\n    int p1[] = { 193, 15, 0, -1 };\n    a = BN_new();\n    b[0] = BN_new();\n    b[1] = BN_new();\n    c = BN_new();\n    d = BN_new();\n    BN_GF2m_arr2poly(p0, b[0]);\n    BN_GF2m_arr2poly(p1, b[1]);\n    for (i = 0; i < num0; i++) {\n        BN_bntest_rand(a, 512, 0, 0);\n        for (j = 0; j < 2; j++) {\n            BN_GF2m_mod_inv(c, a, b[j], ctx);\n            BN_GF2m_mod_mul(d, a, c, b[j], ctx);\n            if (!BN_is_one(d)) {\n                fprintf(stderr, "GF(2^m) modular inversion test failed!\\n");\n                goto err;\n            }\n        }\n    }\n    ret = 1;\n err:\n    BN_free(a);\n    BN_free(b[0]);\n    BN_free(b[1]);\n    BN_free(c);\n    BN_free(d);\n    return ret;\n}', 'int BN_GF2m_mod_inv(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx)\n{\n    BIGNUM *b, *c = NULL, *u = NULL, *v = NULL, *tmp;\n    int ret = 0;\n    bn_check_top(a);\n    bn_check_top(p);\n    BN_CTX_start(ctx);\n    if ((b = BN_CTX_get(ctx)) == NULL)\n        goto err;\n    if ((c = BN_CTX_get(ctx)) == NULL)\n        goto err;\n    if ((u = BN_CTX_get(ctx)) == NULL)\n        goto err;\n    if ((v = BN_CTX_get(ctx)) == NULL)\n        goto err;\n    if (!BN_GF2m_mod(u, a, p))\n        goto err;\n    if (BN_is_zero(u))\n        goto err;\n    if (!BN_copy(v, p))\n        goto err;\n# if 0\n    if (!BN_one(b))\n        goto err;\n    while (1) {\n        while (!BN_is_odd(u)) {\n            if (BN_is_zero(u))\n                goto err;\n            if (!BN_rshift1(u, u))\n                goto err;\n            if (BN_is_odd(b)) {\n                if (!BN_GF2m_add(b, b, p))\n                    goto err;\n            }\n            if (!BN_rshift1(b, b))\n                goto err;\n        }\n        if (BN_abs_is_word(u, 1))\n            break;\n        if (BN_num_bits(u) < BN_num_bits(v)) {\n            tmp = u;\n            u = v;\n            v = tmp;\n            tmp = b;\n            b = c;\n            c = tmp;\n        }\n        if (!BN_GF2m_add(u, u, v))\n            goto err;\n        if (!BN_GF2m_add(b, b, c))\n            goto err;\n    }\n# else\n    {\n        int i;\n        int ubits = BN_num_bits(u);\n        int vbits = BN_num_bits(v);\n        int top = p->top;\n        BN_ULONG *udp, *bdp, *vdp, *cdp;\n        if (!bn_wexpand(u, top))\n            goto err;\n        udp = u->d;\n        for (i = u->top; i < top; i++)\n            udp[i] = 0;\n        u->top = top;\n        if (!bn_wexpand(b, top))\n          goto err;\n        bdp = b->d;\n        bdp[0] = 1;\n        for (i = 1; i < top; i++)\n            bdp[i] = 0;\n        b->top = top;\n        if (!bn_wexpand(c, top))\n          goto err;\n        cdp = c->d;\n        for (i = 0; i < top; i++)\n            cdp[i] = 0;\n        c->top = top;\n        vdp = v->d;\n        while (1) {\n            while (ubits && !(udp[0] & 1)) {\n                BN_ULONG u0, u1, b0, b1, mask;\n                u0 = udp[0];\n                b0 = bdp[0];\n                mask = (BN_ULONG)0 - (b0 & 1);\n                b0 ^= p->d[0] & mask;\n                for (i = 0; i < top - 1; i++) {\n                    u1 = udp[i + 1];\n                    udp[i] = ((u0 >> 1) | (u1 << (BN_BITS2 - 1))) & BN_MASK2;\n                    u0 = u1;\n                    b1 = bdp[i + 1] ^ (p->d[i + 1] & mask);\n                    bdp[i] = ((b0 >> 1) | (b1 << (BN_BITS2 - 1))) & BN_MASK2;\n                    b0 = b1;\n                }\n                udp[i] = u0 >> 1;\n                bdp[i] = b0 >> 1;\n                ubits--;\n            }\n            if (ubits <= BN_BITS2) {\n                if (udp[0] == 0)\n                    goto err;\n                if (udp[0] == 1)\n                    break;\n            }\n            if (ubits < vbits) {\n                i = ubits;\n                ubits = vbits;\n                vbits = i;\n                tmp = u;\n                u = v;\n                v = tmp;\n                tmp = b;\n                b = c;\n                c = tmp;\n                udp = vdp;\n                vdp = v->d;\n                bdp = cdp;\n                cdp = c->d;\n            }\n            for (i = 0; i < top; i++) {\n                udp[i] ^= vdp[i];\n                bdp[i] ^= cdp[i];\n            }\n            if (ubits == vbits) {\n                BN_ULONG ul;\n                int utop = (ubits - 1) / BN_BITS2;\n                while ((ul = udp[utop]) == 0 && utop)\n                    utop--;\n                ubits = utop * BN_BITS2 + BN_num_bits_word(ul);\n            }\n        }\n        bn_correct_top(b);\n    }\n# endif\n    if (!BN_copy(r, b))\n        goto err;\n    bn_check_top(r);\n    ret = 1;\n err:\n# ifdef BN_DEBUG\n    bn_correct_top(c);\n    bn_correct_top(u);\n    bn_correct_top(v);\n# endif\n    BN_CTX_end(ctx);\n    return ret;\n}', 'BIGNUM *BN_CTX_get(BN_CTX *ctx)\n{\n    BIGNUM *ret;\n    CTXDBG_ENTRY("BN_CTX_get", ctx);\n    if (ctx->err_stack || ctx->too_many)\n        return NULL;\n    if ((ret = BN_POOL_get(&ctx->pool, ctx->flags)) == NULL) {\n        ctx->too_many = 1;\n        BNerr(BN_F_BN_CTX_GET, BN_R_TOO_MANY_TEMPORARY_VARIABLES);\n        return NULL;\n    }\n    BN_zero(ret);\n    ctx->used++;\n    CTXDBG_RET(ctx, ret);\n    return ret;\n}', 'int BN_set_word(BIGNUM *a, BN_ULONG w)\n{\n    bn_check_top(a);\n    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)\n        return (0);\n    a->neg = 0;\n    a->d[0] = w;\n    a->top = (w ? 1 : 0);\n    bn_check_top(a);\n    return (1);\n}', 'static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)\n{\n    if (bits > (INT_MAX - BN_BITS2 + 1))\n        return NULL;\n    if(((bits+BN_BITS2-1)/BN_BITS2) <= (a)->dmax)\n        return a;\n    return bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2);\n}']