crypto/rsa/rsa_gen.c:178: error: BUFFER_OVERRUN_L3
  Offset: [8, +oo] (⇐ [8, +oo] + [0, +oo]) Size: [0, 8388607] by call to `BN_mul`.
Showing all 15 steps of the trace


crypto/rsa/rsa_gen.c:127:5: Call
125.           goto err;
126.   
127.       BN_copy(rsa->e, e_value);
           ^
128.   
129.       /* generate p and q */

crypto/bn/bn_lib.c:429:1: Parameter `*a->d`
  427.   }
  428.   
  429. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
  430.   {
  431.       int i;

crypto/rsa/rsa_gen.c:178:10: Call
176.   
177.       /* calculate n */
178.       if (!BN_mul(rsa->n, rsa->p, rsa->q, ctx))
                ^
179.           goto err;
180.   

crypto/bn/bn_mul.c:883:1: Parameter `*b->d`
  881.   #endif                          /* BN_RECURSION */
  882.   
  883. > int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx)
  884.   {
  885.       int ret = 0;

crypto/bn/bn_mul.c:964:17: Call
  962.                   if (bn_wexpand(rr, k * 4) == NULL)
  963.                       goto err;
  964.                   bn_mul_part_recursive(rr->d, a->d, b->d,
                         ^
  965.                                         j, al - j, bl - j, t->d);
  966.               } else {            /* al <= j || bl <= j */

crypto/bn/bn_mul.c:534:1: Parameter `n`
    532.    */
    533.   /* tnX may not be negative but less than n */
    534. > void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,
    535.                              int tna, int tnb, BN_ULONG *t)
    536.   {

crypto/bn/bn_mul.c:548:10: Call
    546.       /* r=(a[0]-a[1])*(b[1]-b[0]) */
    547.       c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);
    548.       c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);
                    ^
    549.       neg = 0;
    550.       switch (c1 * 3 + c2) {

crypto/bn/bn_lib.c:782:1: Parameter `cl`
      780.    */
      781.   
      782. > int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)
      783.   {
      784.       int n, i;

crypto/bn/bn_lib.c:799:12: Call
      797.           }
      798.       }
      799.       return bn_cmp_words(a, b, cl);
                        ^
      800.   }
      801.   

crypto/bn/bn_lib.c:755:1: <Offset trace>
753.   }
754.   
755. > int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
756.   {
757.       int i;

crypto/bn/bn_lib.c:755:1: Parameter `n`
        753.   }
        754.   
        755. > int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
        756.   {
        757.       int i;

crypto/bn/bn_lib.c:764:10: Assignment
        762.       if (aa != bb)
        763.           return ((aa > bb) ? 1 : -1);
        764.       for (i = n - 2; i >= 0; i--) {
                        ^
        765.           aa = a[i];
        766.           bb = b[i];

crypto/bn/bn_lib.c:755:1: <Length trace>
753.   }
754.   
755. > int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
756.   {
757.       int i;

crypto/bn/bn_lib.c:755:1: Parameter `*a`
        753.   }
        754.   
        755. > int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
        756.   {
        757.       int i;

crypto/bn/bn_lib.c:765:14: Array access: Offset: [8, +oo] (⇐ [8, +oo] + [0, +oo]) Size: [0, 8388607] by call to `BN_mul` 
        763.           return ((aa > bb) ? 1 : -1);
        764.       for (i = n - 2; i >= 0; i--) {
        765.           aa = a[i];
                            ^
        766.           bb = b[i];
        767.           if (aa != bb)

int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)
{
    int i;
    BN_ULONG aa, bb;
    aa = a[n - 1];
    bb = b[n - 1];
    if (aa != bb)
        return ((aa > bb) ? 1 : -1);
    for (i = n - 2; i >= 0; i--) {
        aa = a[i];
        bb = b[i];
        if (aa != bb)
            return ((aa > bb) ? 1 : -1);
    }
    return (0);
}
['static int rsa_builtin_keygen(RSA *rsa, int bits, BIGNUM *e_value,\n                              BN_GENCB *cb)\n{\n    BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp;\n    int bitsp, bitsq, ok = -1, n = 0;\n    BN_CTX *ctx = NULL;\n    ctx = BN_CTX_new();\n    if (ctx == NULL)\n        goto err;\n    BN_CTX_start(ctx);\n    r0 = BN_CTX_get(ctx);\n    r1 = BN_CTX_get(ctx);\n    r2 = BN_CTX_get(ctx);\n    r3 = BN_CTX_get(ctx);\n    if (r3 == NULL)\n        goto err;\n    bitsp = (bits + 1) / 2;\n    bitsq = bits - bitsp;\n    if (!rsa->n && ((rsa->n = BN_new()) == NULL))\n        goto err;\n    if (!rsa->d && ((rsa->d = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->e && ((rsa->e = BN_new()) == NULL))\n        goto err;\n    if (!rsa->p && ((rsa->p = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->q && ((rsa->q = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->dmp1 && ((rsa->dmp1 = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->dmq1 && ((rsa->dmq1 = BN_secure_new()) == NULL))\n        goto err;\n    if (!rsa->iqmp && ((rsa->iqmp = BN_secure_new()) == NULL))\n        goto err;\n    BN_copy(rsa->e, e_value);\n    for (;;) {\n        if (!BN_generate_prime_ex(rsa->p, bitsp, 0, NULL, NULL, cb))\n            goto err;\n        if (!BN_sub(r2, rsa->p, BN_value_one()))\n            goto err;\n        if (!BN_gcd(r1, r2, rsa->e, ctx))\n            goto err;\n        if (BN_is_one(r1))\n            break;\n        if (!BN_GENCB_call(cb, 2, n++))\n            goto err;\n    }\n    if (!BN_GENCB_call(cb, 3, 0))\n        goto err;\n    for (;;) {\n        unsigned int degenerate = 0;\n        do {\n            if (!BN_generate_prime_ex(rsa->q, bitsq, 0, NULL, NULL, cb))\n                goto err;\n        } while ((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3));\n        if (degenerate == 3) {\n            ok = 0;\n            RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, RSA_R_KEY_SIZE_TOO_SMALL);\n            goto err;\n        }\n        if (!BN_sub(r2, rsa->q, BN_value_one()))\n            goto err;\n        if (!BN_gcd(r1, r2, rsa->e, ctx))\n            goto err;\n        if (BN_is_one(r1))\n            break;\n        if (!BN_GENCB_call(cb, 2, n++))\n            goto err;\n    }\n    if (!BN_GENCB_call(cb, 3, 1))\n        goto err;\n    if (BN_cmp(rsa->p, rsa->q) < 0) {\n        tmp = rsa->p;\n        rsa->p = rsa->q;\n        rsa->q = tmp;\n    }\n    if (!BN_mul(rsa->n, rsa->p, rsa->q, ctx))\n        goto err;\n    if (!BN_sub(r1, rsa->p, BN_value_one()))\n        goto err;\n    if (!BN_sub(r2, rsa->q, BN_value_one()))\n        goto err;\n    if (!BN_mul(r0, r1, r2, ctx))\n        goto err;\n    {\n        BIGNUM *local_r0 = NULL, *pr0;\n        if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {\n            pr0 = local_r0 = BN_new();\n            if (local_r0 == NULL)\n                goto err;\n            BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);\n        } else {\n            pr0 = r0;\n        }\n        if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) {\n            BN_free(local_r0);\n            goto err;\n        }\n        BN_free(local_r0);\n    }\n    {\n        BIGNUM *local_d = NULL, *d;\n        if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {\n            d = local_d = BN_new();\n            if (local_d == NULL)\n                goto err;\n            BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);\n        } else {\n            d = rsa->d;\n        }\n        if (\n               !BN_mod(rsa->dmp1, d, r1, ctx)\n            || !BN_mod(rsa->dmq1, d, r2, ctx)) {\n            BN_free(local_d);\n            goto err;\n        }\n        BN_free(local_d);\n    }\n    {\n        BIGNUM *local_p = NULL, *p;\n        if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {\n            p = local_p = BN_new();\n            if (local_p == NULL)\n                goto err;\n            BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);\n        } else {\n            p = rsa->p;\n        }\n        if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) {\n            BN_free(local_p);\n            goto err;\n        }\n        BN_free(local_p);\n    }\n    ok = 1;\n err:\n    if (ok == -1) {\n        RSAerr(RSA_F_RSA_BUILTIN_KEYGEN, ERR_LIB_BN);\n        ok = 0;\n    }\n    if (ctx != NULL)\n        BN_CTX_end(ctx);\n    BN_CTX_free(ctx);\n    return ok;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\n    int i;\n    BN_ULONG *A;\n    const BN_ULONG *B;\n    bn_check_top(b);\n    if (a == b)\n        return (a);\n    if (bn_wexpand(a, b->top) == NULL)\n        return (NULL);\n#if 1\n    A = a->d;\n    B = b->d;\n    for (i = b->top >> 2; i > 0; i--, A += 4, B += 4) {\n        BN_ULONG a0, a1, a2, a3;\n        a0 = B[0];\n        a1 = B[1];\n        a2 = B[2];\n        a3 = B[3];\n        A[0] = a0;\n        A[1] = a1;\n        A[2] = a2;\n        A[3] = a3;\n    }\n    switch (b->top & 3) {\n    case 3:\n        A[2] = B[2];\n    case 2:\n        A[1] = B[1];\n    case 1:\n        A[0] = B[0];\n    case 0:;\n    }\n#else\n    memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n#endif\n    a->top = b->top;\n    a->neg = b->neg;\n    bn_check_top(a);\n    return (a);\n}', 'int BN_mul(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    rr->neg = a->neg ^ b->neg;\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# if 0\n        if (i == 1 && !BN_get_flags(b, BN_FLG_STATIC_DATA)) {\n            BIGNUM *tmp_bn = (BIGNUM *)b;\n            if (bn_wexpand(tmp_bn, al) == NULL)\n                goto err;\n            tmp_bn->d[bl] = 0;\n            bl++;\n            i--;\n        } else if (i == -1 && !BN_get_flags(a, BN_FLG_STATIC_DATA)) {\n            BIGNUM *tmp_bn = (BIGNUM *)a;\n            if (bn_wexpand(tmp_bn, bl) == NULL)\n                goto err;\n            tmp_bn->d[al] = 0;\n            al++;\n            i++;\n        }\n        if (i == 0) {\n            j = BN_num_bits_word((BN_ULONG)al);\n            j = 1 << (j - 1);\n            k = j + j;\n            t = BN_CTX_get(ctx);\n            if (al == j) {\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, al, t->d);\n            } else {\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, al - j, j, t->d);\n            }\n            rr->top = top;\n            goto end;\n        }\n# endif\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    bn_correct_top(rr);\n    if (r != rr)\n        BN_copy(r, rr);\n    ret = 1;\n err:\n    bn_check_top(r);\n    BN_CTX_end(ctx);\n    return (ret);\n}', 'void bn_mul_part_recursive(BN_ULONG *r, BN_ULONG *a, BN_ULONG *b, int n,\n                           int tna, int tnb, BN_ULONG *t)\n{\n    int i, j, n2 = n * 2;\n    int c1, c2, neg;\n    BN_ULONG ln, lo, *p;\n    if (n < 8) {\n        bn_mul_normal(r, a, n + tna, b, n + tnb);\n        return;\n    }\n    c1 = bn_cmp_part_words(a, &(a[n]), tna, n - tna);\n    c2 = bn_cmp_part_words(&(b[n]), b, tnb, tnb - n);\n    neg = 0;\n    switch (c1 * 3 + c2) {\n    case -4:\n        bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n        bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n        break;\n    case -3:\n    case -2:\n        bn_sub_part_words(t, &(a[n]), a, tna, tna - n);\n        bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n        neg = 1;\n        break;\n    case -1:\n    case 0:\n    case 1:\n    case 2:\n        bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n        bn_sub_part_words(&(t[n]), b, &(b[n]), tnb, n - tnb);\n        neg = 1;\n        break;\n    case 3:\n    case 4:\n        bn_sub_part_words(t, a, &(a[n]), tna, n - tna);\n        bn_sub_part_words(&(t[n]), &(b[n]), b, tnb, tnb - n);\n        break;\n    }\n# if 0\n    if (n == 4) {\n        bn_mul_comba4(&(t[n2]), t, &(t[n]));\n        bn_mul_comba4(r, a, b);\n        bn_mul_normal(&(r[n2]), &(a[n]), tn, &(b[n]), tn);\n        memset(&r[n2 + tn * 2], 0, sizeof(*r) * (n2 - tn * 2));\n    } else\n# endif\n    if (n == 8) {\n        bn_mul_comba8(&(t[n2]), t, &(t[n]));\n        bn_mul_comba8(r, a, b);\n        bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n        memset(&r[n2 + tna + tnb], 0, sizeof(*r) * (n2 - tna - tnb));\n    } else {\n        p = &(t[n2 * 2]);\n        bn_mul_recursive(&(t[n2]), t, &(t[n]), n, 0, 0, p);\n        bn_mul_recursive(r, a, b, n, 0, 0, p);\n        i = n / 2;\n        if (tna > tnb)\n            j = tna - i;\n        else\n            j = tnb - i;\n        if (j == 0) {\n            bn_mul_recursive(&(r[n2]), &(a[n]), &(b[n]),\n                             i, tna - i, tnb - i, p);\n            memset(&r[n2 + i * 2], 0, sizeof(*r) * (n2 - i * 2));\n        } else if (j > 0) {\n            bn_mul_part_recursive(&(r[n2]), &(a[n]), &(b[n]),\n                                  i, tna - i, tnb - i, p);\n            memset(&(r[n2 + tna + tnb]), 0,\n                   sizeof(BN_ULONG) * (n2 - tna - tnb));\n        } else {\n            memset(&r[n2], 0, sizeof(*r) * n2);\n            if (tna < BN_MUL_RECURSIVE_SIZE_NORMAL\n                && tnb < BN_MUL_RECURSIVE_SIZE_NORMAL) {\n                bn_mul_normal(&(r[n2]), &(a[n]), tna, &(b[n]), tnb);\n            } else {\n                for (;;) {\n                    i /= 2;\n                    if (i < tna || i < tnb) {\n                        bn_mul_part_recursive(&(r[n2]),\n                                              &(a[n]), &(b[n]),\n                                              i, tna - i, tnb - i, p);\n                        break;\n                    } else if (i == tna || i == tnb) {\n                        bn_mul_recursive(&(r[n2]),\n                                         &(a[n]), &(b[n]),\n                                         i, tna - i, tnb - i, p);\n                        break;\n                    }\n                }\n            }\n        }\n    }\n    c1 = (int)(bn_add_words(t, r, &(r[n2]), n2));\n    if (neg) {\n        c1 -= (int)(bn_sub_words(&(t[n2]), t, &(t[n2]), n2));\n    } else {\n        c1 += (int)(bn_add_words(&(t[n2]), &(t[n2]), t, n2));\n    }\n    c1 += (int)(bn_add_words(&(r[n]), &(r[n]), &(t[n2]), n2));\n    if (c1) {\n        p = &(r[n + n2]);\n        lo = *p;\n        ln = (lo + c1) & BN_MASK2;\n        *p = ln;\n        if (ln < (BN_ULONG)c1) {\n            do {\n                p++;\n                lo = *p;\n                ln = (lo + 1) & BN_MASK2;\n                *p = ln;\n            } while (ln == 0);\n        }\n    }\n}', 'int bn_cmp_part_words(const BN_ULONG *a, const BN_ULONG *b, int cl, int dl)\n{\n    int n, i;\n    n = cl - 1;\n    if (dl < 0) {\n        for (i = dl; i < 0; i++) {\n            if (b[n - i] != 0)\n                return -1;\n        }\n    }\n    if (dl > 0) {\n        for (i = dl; i > 0; i--) {\n            if (a[n + i] != 0)\n                return 1;\n        }\n    }\n    return bn_cmp_words(a, b, cl);\n}', 'int bn_cmp_words(const BN_ULONG *a, const BN_ULONG *b, int n)\n{\n    int i;\n    BN_ULONG aa, bb;\n    aa = a[n - 1];\n    bb = b[n - 1];\n    if (aa != bb)\n        return ((aa > bb) ? 1 : -1);\n    for (i = n - 2; i >= 0; i--) {\n        aa = a[i];\n        bb = b[i];\n        if (aa != bb)\n            return ((aa > bb) ? 1 : -1);\n    }\n    return (0);\n}']