crypto/rsa/rsa_sp800_56b_gen.c:319: error: BUFFER_OVERRUN_L3
  Offset added: [8, +oo] Size: [0, 536870848] by call to `rsa_sp800_56b_derive_params_from_pq`.
Showing all 20 steps of the trace


crypto/rsa/rsa_sp800_56b_gen.c:314:14: Call
312.       for (;;) {
313.           /* (Step 2) Generate prime factors */
314.           if (!rsa_fips186_4_gen_prob_primes(rsa, NULL, NULL, NULL, NULL, NULL,
                    ^
315.                                              NULL, NULL, NULL, NULL, NULL, NULL,
316.                                              NULL, nbits, e, ctx, cb))

crypto/rsa/rsa_sp800_56b_gen.c:51:1: Parameter `rsa->p->top`
  49.    *     (Required for CAVS testing).
  50.    */
  51. > int rsa_fips186_4_gen_prob_primes(RSA *rsa, BIGNUM *p1, BIGNUM *p2,
  52.                                     BIGNUM *Xpout, const BIGNUM *Xp,
  53.                                     const BIGNUM *Xp1, const BIGNUM *Xp2,

crypto/rsa/rsa_sp800_56b_gen.c:319:14: Call
317.               goto err;
318.           /* (Steps 3-5) Compute params d, n, dP, dQ, qInv */
319.           ok = rsa_sp800_56b_derive_params_from_pq(rsa, nbits, e, ctx);
                    ^
320.           if (ok < 0)
321.               goto err;

crypto/rsa/rsa_sp800_56b_gen.c:180:1: Parameter `e->top`
  178.    *           1 = success.
  179.    */
  180. > int rsa_sp800_56b_derive_params_from_pq(RSA *rsa, int nbits,
  181.                                           const BIGNUM *e, BN_CTX *ctx)
  182.   {

crypto/rsa/rsa_sp800_56b_gen.c:201:14: Call
  199.       /* copy e */
  200.       BN_free(rsa->e);
  201.       rsa->e = BN_dup(e);
                      ^
  202.       if (rsa->e == NULL)
  203.           goto err;

crypto/bn/bn_lib.c:262:1: Parameter `a->top`
    260.   }
    261.   
    262. > BIGNUM *BN_dup(const BIGNUM *a)
    263.   {
    264.       BIGNUM *t;

crypto/rsa/rsa_sp800_56b_gen.c:208:27: Call
  206.       /* (Step 3) d = (e^-1) mod (LCM(p-1, q-1)) */
  207.       rsa->d = BN_secure_new();
  208.       if (rsa->d == NULL || BN_mod_inverse(rsa->d, e, lcm, ctx) == NULL)
                                   ^
  209.           goto err;
  210.   

crypto/bn/bn_gcd.c:124:1: Parameter `a->top`
    122.                                           BN_CTX *ctx);
    123.   
    124. > BIGNUM *BN_mod_inverse(BIGNUM *in,
    125.                          const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)
    126.   {

crypto/bn/bn_gcd.c:129:10: Call
    127.       BIGNUM *rv;
    128.       int noinv;
    129.       rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);
                    ^
    130.       if (noinv)
    131.           BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);

crypto/bn/bn_gcd.c:135:1: Parameter `a->top`
      133.   }
      134.   
      135. > BIGNUM *int_bn_mod_inverse(BIGNUM *in,
      136.                              const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,
      137.                              int *pnoinv)

crypto/bn/bn_gcd.c:155:16: Call
      153.       if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)
      154.           || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {
      155.           return BN_mod_inverse_no_branch(in, a, n, ctx);
                            ^
      156.       }
      157.   

crypto/bn/bn_gcd.c:458:1: Parameter `a->top`
        456.    * not contain branches that may leak sensitive information.
        457.    */
        458. > static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,
        459.                                           const BIGNUM *a, const BIGNUM *n,
        460.                                           BN_CTX *ctx)

crypto/bn/bn_gcd.c:489:9: Call
        487.       BN_one(X);
        488.       BN_zero(Y);
        489.       if (BN_copy(B, a) == NULL)
                       ^
        490.           goto err;
        491.       if (BN_copy(A, n) == NULL)

crypto/bn/bn_lib.c:281:1: <Offset trace>
279.   }
280.   
281. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
282.   {
283.       bn_check_top(b);

crypto/bn/bn_lib.c:281:1: Parameter `b->top`
          279.   }
          280.   
          281. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
          282.   {
          283.       bn_check_top(b);

crypto/bn/bn_lib.c:281:1: <Length trace>
279.   }
280.   
281. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
282.   {
283.       bn_check_top(b);

crypto/bn/bn_lib.c:281:1: Parameter `*a->d`
          279.   }
          280.   
          281. > BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
          282.   {
          283.       bn_check_top(b);

crypto/bn/bn_lib.c:287:9: Call
          285.       if (a == b)
          286.           return a;
          287.       if (bn_wexpand(a, b->top) == NULL)
                         ^
          288.           return NULL;
          289.   

crypto/bn/bn_lib.c:960:1: Parameter `*a->d`
            958.   }
            959.   
            960. > BIGNUM *bn_wexpand(BIGNUM *a, int words)
            961.   {
            962.       return (words <= a->dmax) ? a : bn_expand2(a, words);

crypto/bn/bn_lib.c:291:9: Array access: Offset added: [8, +oo] Size: [0, 536870848] by call to `rsa_sp800_56b_derive_params_from_pq` 
          289.   
          290.       if (b->top > 0)
          291.           memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
                         ^
          292.   
          293.       a->neg = b->neg;

BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)
{
    bn_check_top(b);
    if (a == b)
        return a;
    if (bn_wexpand(a, b->top) == NULL)
        return NULL;
    if (b->top > 0)
        memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);
    a->neg = b->neg;
    a->top = b->top;
    a->flags |= b->flags & BN_FLG_FIXED_TOP;
    bn_check_top(a);
    return a;
}
['int rsa_sp800_56b_generate_key(RSA *rsa, int nbits, const BIGNUM *efixed,\n                               BN_GENCB *cb)\n{\n    int ret = 0;\n    int ok;\n    BN_CTX *ctx = NULL;\n    BIGNUM *e = NULL;\n    if (!rsa_sp800_56b_validate_strength(nbits, -1))\n        return 0;\n    ctx = BN_CTX_new();\n    if (ctx == NULL)\n        return 0;\n    if (efixed == NULL) {\n        e = BN_new();\n        if (e == NULL || !BN_set_word(e, 65537))\n            goto err;\n    } else {\n        e = (BIGNUM *)efixed;\n    }\n    for (;;) {\n        if (!rsa_fips186_4_gen_prob_primes(rsa, NULL, NULL, NULL, NULL, NULL,\n                                           NULL, NULL, NULL, NULL, NULL, NULL,\n                                           NULL, nbits, e, ctx, cb))\n            goto err;\n        ok = rsa_sp800_56b_derive_params_from_pq(rsa, nbits, e, ctx);\n        if (ok < 0)\n            goto err;\n        if (ok > 0)\n            break;\n    }\n    ret = rsa_sp800_56b_pairwise_test(rsa, ctx);\nerr:\n    if (efixed == NULL)\n        BN_free(e);\n    BN_CTX_free(ctx);\n    return ret;\n}', 'int rsa_fips186_4_gen_prob_primes(RSA *rsa, BIGNUM *p1, BIGNUM *p2,\n                                  BIGNUM *Xpout, const BIGNUM *Xp,\n                                  const BIGNUM *Xp1, const BIGNUM *Xp2,\n                                  BIGNUM *q1, BIGNUM *q2, BIGNUM *Xqout,\n                                  const BIGNUM *Xq, const BIGNUM *Xq1,\n                                  const BIGNUM *Xq2, int nbits,\n                                  const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb)\n{\n    int ret = 0, ok;\n    BIGNUM *Xpo = NULL, *Xqo = NULL, *tmp = NULL;\n    if (nbits < RSA_FIPS1864_MIN_KEYGEN_KEYSIZE) {\n        RSAerr(RSA_F_RSA_FIPS186_4_GEN_PROB_PRIMES, RSA_R_INVALID_KEY_LENGTH);\n        return 0;\n    }\n    if (!rsa_check_public_exponent(e)) {\n        RSAerr(RSA_F_RSA_FIPS186_4_GEN_PROB_PRIMES,\n               RSA_R_PUB_EXPONENT_OUT_OF_RANGE);\n        goto err;\n    }\n    BN_CTX_start(ctx);\n    tmp = BN_CTX_get(ctx);\n    Xpo = (Xpout != NULL) ? Xpout : BN_CTX_get(ctx);\n    Xqo = (Xqout != NULL) ? Xqout : BN_CTX_get(ctx);\n    if (tmp == NULL || Xpo == NULL || Xqo == NULL)\n        goto err;\n    if (rsa->p == NULL)\n        rsa->p = BN_secure_new();\n    if (rsa->q == NULL)\n        rsa->q = BN_secure_new();\n    if (rsa->p == NULL || rsa->q == NULL)\n        goto err;\n    if (!bn_rsa_fips186_4_gen_prob_primes(rsa->p, Xpo, p1, p2, Xp, Xp1, Xp2,\n                                          nbits, e, ctx, cb))\n        goto err;\n    for(;;) {\n        if (!bn_rsa_fips186_4_gen_prob_primes(rsa->q, Xqo, q1, q2, Xq, Xq1,\n                                              Xq2, nbits, e, ctx, cb))\n            goto err;\n        ok = rsa_check_pminusq_diff(tmp, Xpo, Xqo, nbits);\n        if (ok < 0)\n            goto err;\n        if (ok == 0)\n            continue;\n        ok = rsa_check_pminusq_diff(tmp, rsa->p, rsa->q, nbits);\n        if (ok < 0)\n            goto err;\n        if (ok == 0)\n            continue;\n        break;\n    }\n    ret = 1;\nerr:\n    if (Xpo != Xpout)\n        BN_clear(Xpo);\n    if (Xqo != Xqout)\n        BN_clear(Xqo);\n    BN_clear(tmp);\n    BN_CTX_end(ctx);\n    return ret;\n}', 'int rsa_sp800_56b_derive_params_from_pq(RSA *rsa, int nbits,\n                                        const BIGNUM *e, BN_CTX *ctx)\n{\n    int ret = -1;\n    BIGNUM *p1, *q1, *lcm, *p1q1, *gcd;\n    BN_CTX_start(ctx);\n    p1 = BN_CTX_get(ctx);\n    q1 = BN_CTX_get(ctx);\n    lcm = BN_CTX_get(ctx);\n    p1q1 = BN_CTX_get(ctx);\n    gcd = BN_CTX_get(ctx);\n    if (gcd == NULL)\n        goto err;\n    if (rsa_get_lcm(ctx, rsa->p, rsa->q, lcm, gcd, p1, q1, p1q1) != 1)\n        goto err;\n    BN_free(rsa->e);\n    rsa->e = BN_dup(e);\n    if (rsa->e == NULL)\n        goto err;\n    BN_clear_free(rsa->d);\n    rsa->d = BN_secure_new();\n    if (rsa->d == NULL || BN_mod_inverse(rsa->d, e, lcm, ctx) == NULL)\n        goto err;\n    if (BN_num_bits(rsa->d) <= (nbits >> 1)) {\n        ret = 0;\n        goto err;\n    }\n    if (rsa->n == NULL)\n        rsa->n = BN_new();\n    if (rsa->n == NULL || !BN_mul(rsa->n, rsa->p, rsa->q, ctx))\n        goto err;\n    if (rsa->dmp1 == NULL)\n        rsa->dmp1 = BN_new();\n    if (rsa->dmp1 == NULL || !BN_mod(rsa->dmp1, rsa->d, p1, ctx))\n        goto err;\n    if (rsa->dmq1 == NULL)\n        rsa->dmq1 = BN_secure_new();\n    if (rsa->dmq1 == NULL || !BN_mod(rsa->dmq1, rsa->d, q1, ctx))\n        goto err;\n    BN_free(rsa->iqmp);\n    rsa->iqmp = BN_secure_new();\n    if (rsa->iqmp == NULL\n            || BN_mod_inverse(rsa->iqmp, rsa->q, rsa->p, ctx) == NULL)\n        goto err;\n    ret = 1;\nerr:\n    if (ret != 1) {\n        BN_free(rsa->e);\n        rsa->e = NULL;\n        BN_free(rsa->d);\n        rsa->d = NULL;\n        BN_free(rsa->n);\n        rsa->n = NULL;\n        BN_free(rsa->iqmp);\n        rsa->iqmp = NULL;\n        BN_free(rsa->dmq1);\n        rsa->dmq1 = NULL;\n        BN_free(rsa->dmp1);\n        rsa->dmp1 = NULL;\n    }\n    BN_clear(p1);\n    BN_clear(q1);\n    BN_clear(lcm);\n    BN_clear(p1q1);\n    BN_clear(gcd);\n    BN_CTX_end(ctx);\n    return ret;\n}', 'BIGNUM *BN_dup(const BIGNUM *a)\n{\n    BIGNUM *t;\n    if (a == NULL)\n        return NULL;\n    bn_check_top(a);\n    t = BN_get_flags(a, BN_FLG_SECURE) ? BN_secure_new() : BN_new();\n    if (t == NULL)\n        return NULL;\n    if (!BN_copy(t, a)) {\n        BN_free(t);\n        return NULL;\n    }\n    bn_check_top(t);\n    return t;\n}', 'BIGNUM *BN_mod_inverse(BIGNUM *in,\n                       const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx)\n{\n    BIGNUM *rv;\n    int noinv;\n    rv = int_bn_mod_inverse(in, a, n, ctx, &noinv);\n    if (noinv)\n        BNerr(BN_F_BN_MOD_INVERSE, BN_R_NO_INVERSE);\n    return rv;\n}', 'BIGNUM *int_bn_mod_inverse(BIGNUM *in,\n                           const BIGNUM *a, const BIGNUM *n, BN_CTX *ctx,\n                           int *pnoinv)\n{\n    BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n    BIGNUM *ret = NULL;\n    int sign;\n    if (BN_abs_is_word(n, 1) || BN_is_zero(n)) {\n        if (pnoinv != NULL)\n            *pnoinv = 1;\n        return NULL;\n    }\n    if (pnoinv != NULL)\n        *pnoinv = 0;\n    if ((BN_get_flags(a, BN_FLG_CONSTTIME) != 0)\n        || (BN_get_flags(n, BN_FLG_CONSTTIME) != 0)) {\n        return BN_mod_inverse_no_branch(in, a, n, ctx);\n    }\n    bn_check_top(a);\n    bn_check_top(n);\n    BN_CTX_start(ctx);\n    A = BN_CTX_get(ctx);\n    B = BN_CTX_get(ctx);\n    X = BN_CTX_get(ctx);\n    D = BN_CTX_get(ctx);\n    M = BN_CTX_get(ctx);\n    Y = BN_CTX_get(ctx);\n    T = BN_CTX_get(ctx);\n    if (T == NULL)\n        goto err;\n    if (in == NULL)\n        R = BN_new();\n    else\n        R = in;\n    if (R == NULL)\n        goto err;\n    BN_one(X);\n    BN_zero(Y);\n    if (BN_copy(B, a) == NULL)\n        goto err;\n    if (BN_copy(A, n) == NULL)\n        goto err;\n    A->neg = 0;\n    if (B->neg || (BN_ucmp(B, A) >= 0)) {\n        if (!BN_nnmod(B, B, A, ctx))\n            goto err;\n    }\n    sign = -1;\n    if (BN_is_odd(n) && (BN_num_bits(n) <= 2048)) {\n        int shift;\n        while (!BN_is_zero(B)) {\n            shift = 0;\n            while (!BN_is_bit_set(B, shift)) {\n                shift++;\n                if (BN_is_odd(X)) {\n                    if (!BN_uadd(X, X, n))\n                        goto err;\n                }\n                if (!BN_rshift1(X, X))\n                    goto err;\n            }\n            if (shift > 0) {\n                if (!BN_rshift(B, B, shift))\n                    goto err;\n            }\n            shift = 0;\n            while (!BN_is_bit_set(A, shift)) {\n                shift++;\n                if (BN_is_odd(Y)) {\n                    if (!BN_uadd(Y, Y, n))\n                        goto err;\n                }\n                if (!BN_rshift1(Y, Y))\n                    goto err;\n            }\n            if (shift > 0) {\n                if (!BN_rshift(A, A, shift))\n                    goto err;\n            }\n            if (BN_ucmp(B, A) >= 0) {\n                if (!BN_uadd(X, X, Y))\n                    goto err;\n                if (!BN_usub(B, B, A))\n                    goto err;\n            } else {\n                if (!BN_uadd(Y, Y, X))\n                    goto err;\n                if (!BN_usub(A, A, B))\n                    goto err;\n            }\n        }\n    } else {\n        while (!BN_is_zero(B)) {\n            BIGNUM *tmp;\n            if (BN_num_bits(A) == BN_num_bits(B)) {\n                if (!BN_one(D))\n                    goto err;\n                if (!BN_sub(M, A, B))\n                    goto err;\n            } else if (BN_num_bits(A) == BN_num_bits(B) + 1) {\n                if (!BN_lshift1(T, B))\n                    goto err;\n                if (BN_ucmp(A, T) < 0) {\n                    if (!BN_one(D))\n                        goto err;\n                    if (!BN_sub(M, A, B))\n                        goto err;\n                } else {\n                    if (!BN_sub(M, A, T))\n                        goto err;\n                    if (!BN_add(D, T, B))\n                        goto err;\n                    if (BN_ucmp(A, D) < 0) {\n                        if (!BN_set_word(D, 2))\n                            goto err;\n                    } else {\n                        if (!BN_set_word(D, 3))\n                            goto err;\n                        if (!BN_sub(M, M, B))\n                            goto err;\n                    }\n                }\n            } else {\n                if (!BN_div(D, M, A, B, ctx))\n                    goto err;\n            }\n            tmp = A;\n            A = B;\n            B = M;\n            if (BN_is_one(D)) {\n                if (!BN_add(tmp, X, Y))\n                    goto err;\n            } else {\n                if (BN_is_word(D, 2)) {\n                    if (!BN_lshift1(tmp, X))\n                        goto err;\n                } else if (BN_is_word(D, 4)) {\n                    if (!BN_lshift(tmp, X, 2))\n                        goto err;\n                } else if (D->top == 1) {\n                    if (!BN_copy(tmp, X))\n                        goto err;\n                    if (!BN_mul_word(tmp, D->d[0]))\n                        goto err;\n                } else {\n                    if (!BN_mul(tmp, D, X, ctx))\n                        goto err;\n                }\n                if (!BN_add(tmp, tmp, Y))\n                    goto err;\n            }\n            M = Y;\n            Y = X;\n            X = tmp;\n            sign = -sign;\n        }\n    }\n    if (sign < 0) {\n        if (!BN_sub(Y, n, Y))\n            goto err;\n    }\n    if (BN_is_one(A)) {\n        if (!Y->neg && BN_ucmp(Y, n) < 0) {\n            if (!BN_copy(R, Y))\n                goto err;\n        } else {\n            if (!BN_nnmod(R, Y, n, ctx))\n                goto err;\n        }\n    } else {\n        if (pnoinv)\n            *pnoinv = 1;\n        goto err;\n    }\n    ret = R;\n err:\n    if ((ret == NULL) && (in == NULL))\n        BN_free(R);\n    BN_CTX_end(ctx);\n    bn_check_top(ret);\n    return ret;\n}', 'static BIGNUM *BN_mod_inverse_no_branch(BIGNUM *in,\n                                        const BIGNUM *a, const BIGNUM *n,\n                                        BN_CTX *ctx)\n{\n    BIGNUM *A, *B, *X, *Y, *M, *D, *T, *R = NULL;\n    BIGNUM *ret = NULL;\n    int sign;\n    bn_check_top(a);\n    bn_check_top(n);\n    BN_CTX_start(ctx);\n    A = BN_CTX_get(ctx);\n    B = BN_CTX_get(ctx);\n    X = BN_CTX_get(ctx);\n    D = BN_CTX_get(ctx);\n    M = BN_CTX_get(ctx);\n    Y = BN_CTX_get(ctx);\n    T = BN_CTX_get(ctx);\n    if (T == NULL)\n        goto err;\n    if (in == NULL)\n        R = BN_new();\n    else\n        R = in;\n    if (R == NULL)\n        goto err;\n    BN_one(X);\n    BN_zero(Y);\n    if (BN_copy(B, a) == NULL)\n        goto err;\n    if (BN_copy(A, n) == NULL)\n        goto err;\n    A->neg = 0;\n    if (B->neg || (BN_ucmp(B, A) >= 0)) {\n         {\n            BIGNUM local_B;\n            bn_init(&local_B);\n            BN_with_flags(&local_B, B, BN_FLG_CONSTTIME);\n            if (!BN_nnmod(B, &local_B, A, ctx))\n                goto err;\n        }\n    }\n    sign = -1;\n    while (!BN_is_zero(B)) {\n        BIGNUM *tmp;\n        {\n            BIGNUM local_A;\n            bn_init(&local_A);\n            BN_with_flags(&local_A, A, BN_FLG_CONSTTIME);\n            if (!BN_div(D, M, &local_A, B, ctx))\n                goto err;\n        }\n        tmp = A;\n        A = B;\n        B = M;\n        if (!BN_mul(tmp, D, X, ctx))\n            goto err;\n        if (!BN_add(tmp, tmp, Y))\n            goto err;\n        M = Y;\n        Y = X;\n        X = tmp;\n        sign = -sign;\n    }\n    if (sign < 0) {\n        if (!BN_sub(Y, n, Y))\n            goto err;\n    }\n    if (BN_is_one(A)) {\n        if (!Y->neg && BN_ucmp(Y, n) < 0) {\n            if (!BN_copy(R, Y))\n                goto err;\n        } else {\n            if (!BN_nnmod(R, Y, n, ctx))\n                goto err;\n        }\n    } else {\n        BNerr(BN_F_BN_MOD_INVERSE_NO_BRANCH, BN_R_NO_INVERSE);\n        goto err;\n    }\n    ret = R;\n err:\n    if ((ret == NULL) && (in == NULL))\n        BN_free(R);\n    BN_CTX_end(ctx);\n    bn_check_top(ret);\n    return ret;\n}', 'BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b)\n{\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 (b->top > 0)\n        memcpy(a->d, b->d, sizeof(b->d[0]) * b->top);\n    a->neg = b->neg;\n    a->top = b->top;\n    a->flags |= b->flags & BN_FLG_FIXED_TOP;\n    bn_check_top(a);\n    return a;\n}', 'BIGNUM *bn_wexpand(BIGNUM *a, int words)\n{\n    return (words <= a->dmax) ? a : bn_expand2(a, words);\n}']