test/rsa_sp800_56b_test.c:551: error: BUFFER_OVERRUN_L3
  Offset: [16, +oo] (⇐ 1 + [15, +oo]) Size: [0, 8388607] by call to `rsa_sp800_56b_check_keypair`.
Showing all 28 steps of the trace


test/rsa_sp800_56b_test.c:548:14: Call
546.   
547.       ret = TEST_ptr(key = RSA_new())
548.             && TEST_true(rsa_sp800_56b_generate_key(key, sz, NULL, NULL))
                    ^
549.             && TEST_true(rsa_sp800_56b_check_public(key))
550.             && TEST_true(rsa_sp800_56b_check_private(key))

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,

test/rsa_sp800_56b_test.c:551:14: Call
549.             && TEST_true(rsa_sp800_56b_check_public(key))
550.             && TEST_true(rsa_sp800_56b_check_private(key))
551.             && TEST_true(rsa_sp800_56b_check_keypair(key, NULL, -1, sz));
                    ^
552.   
553.       RSA_free(key);

crypto/rsa/rsa_sp800_56b_check.c:316:1: Parameter `rsa->dmp1->top`
  314.    *     6.4.1.3.3 "rsakpv2 - crt"
  315.    */
  316. > int rsa_sp800_56b_check_keypair(const RSA *rsa, const BIGNUM *efixed,
  317.                                   int strength, int nbits)
  318.   {

crypto/rsa/rsa_sp800_56b_check.c:377:14: Call
  375.             && rsa_check_private_exponent(rsa, nbits, ctx)
  376.             /* 6.4.1.2.3 (Step 7): Check the CRT components */
  377.             && rsa_check_crt_components(rsa, ctx);
                      ^
  378.       if (ret != 1)
  379.           RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_KEYPAIR);

crypto/rsa/rsa_sp800_56b_check.c:24:1: Parameter `rsa->dmp1->top`
    22.    *   6.4.1.3.3: rsakpv2-crt Step 7
    23.    */
    24. > int rsa_check_crt_components(const RSA *rsa, BN_CTX *ctx)
    25.   {
    26.       int ret = 0;

crypto/rsa/rsa_sp800_56b_check.c:48:15: Call
    46.             && BN_sub_word(q1, 1)
    47.             /* (a) 1 < dP < (p – 1). */
    48.             && (BN_cmp(rsa->dmp1, BN_value_one()) > 0)
                        ^
    49.             && (BN_cmp(rsa->dmp1, p1) < 0)
    50.             /* (b) 1 < dQ < (q - 1). */

crypto/bn/bn_lib.c:579:1: Parameter `a->top`
      577.   }
      578.   
      579. > int BN_cmp(const BIGNUM *a, const BIGNUM *b)
      580.   {
      581.       int i;

crypto/rsa/rsa_sp800_56b_check.c:49:15: Call
    47.             /* (a) 1 < dP < (p – 1). */
    48.             && (BN_cmp(rsa->dmp1, BN_value_one()) > 0)
    49.             && (BN_cmp(rsa->dmp1, p1) < 0)
                        ^
    50.             /* (b) 1 < dQ < (q - 1). */
    51.             && (BN_cmp(rsa->dmq1, BN_value_one()) > 0)

crypto/bn/bn_lib.c:579:1: Parameter `a->top`
      577.   }
      578.   
      579. > int BN_cmp(const BIGNUM *a, const BIGNUM *b)
      580.   {
      581.       int i;

crypto/rsa/rsa_sp800_56b_check.c:57:14: Call
    55.             && (BN_cmp(rsa->iqmp, rsa->p) < 0)
    56.             /* (d) 1 = (dP . e) mod (p - 1)*/
    57.             && BN_mod_mul(r, rsa->dmp1, rsa->e, p1, ctx)
                       ^
    58.             && BN_is_one(r)
    59.             /* (e) 1 = (dQ . e) mod (q - 1) */

crypto/bn/bn_mod.c:193:1: Parameter `a->top`
      191.   
      192.   /* slow but works */
      193. > int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,
      194.                  BN_CTX *ctx)
      195.   {

crypto/bn/bn_mod.c:207:14: Call
      205.           goto err;
      206.       if (a == b) {
      207.           if (!BN_sqr(t, a, ctx))
                          ^
      208.               goto err;
      209.       } else {

crypto/bn/bn_sqr.c:17:1: Parameter `*r->d`
        15.    * I've just gone over this and it is now %20 faster on x86 - eay - 27 Jun 96
        16.    */
        17. > int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
        18.   {
        19.       int ret = bn_sqr_fixed_top(r, a, ctx);

crypto/bn/bn_sqr.c:19:15: Call
        17.   int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
        18.   {
        19.       int ret = bn_sqr_fixed_top(r, a, ctx);
                            ^
        20.   
        21.       bn_correct_top(r);

crypto/bn/bn_sqr.c:27:1: Parameter `a->top`
          25.   }
          26.   
          27. > int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)
          28.   {
          29.       int max, al;

crypto/bn/bn_sqr.c:35:5: Assignment
          33.       bn_check_top(a);
          34.   
          35.       al = a->top;
                    ^
          36.       if (al <= 0) {
          37.           r->top = 0;

crypto/bn/bn_sqr.c:84:17: Call
          82.                   if (bn_wexpand(tmp, max) == NULL)
          83.                       goto err;
          84.                   bn_sqr_normal(rr->d, a->d, al, tmp->d);
                                ^
          85.               }
          86.           }

crypto/bn/bn_sqr.c:109:1: <Offset trace>
107.   
108.   /* tmp must have 2*n words */
109. > void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
110.   {
111.       int i, j, max;

crypto/bn/bn_sqr.c:109:1: Parameter `n`
            107.   
            108.   /* tmp must have 2*n words */
            109. > void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
            110.   {
            111.       int i, j, max;

crypto/bn/bn_sqr.c:120:5: Assignment
            118.       rp[0] = rp[max - 1] = 0;
            119.       rp++;
            120.       j = n;
                       ^
            121.   
            122.       if (--j > 0) {

crypto/bn/bn_sqr.c:122:9: Assignment
            120.       j = n;
            121.   
            122.       if (--j > 0) {
                           ^
            123.           ap++;
            124.           rp[j] = bn_mul_words(rp, ap, j, ap[-1]);

crypto/bn/bn_sqr.c:109:1: <Length trace>
107.   
108.   /* tmp must have 2*n words */
109. > void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
110.   {
111.       int i, j, max;

crypto/bn/bn_sqr.c:109:1: Parameter `*r`
            107.   
            108.   /* tmp must have 2*n words */
            109. > void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
            110.   {
            111.       int i, j, max;

crypto/bn/bn_sqr.c:117:5: Assignment
            115.       max = n * 2;
            116.       ap = a;
            117.       rp = r;
                       ^
            118.       rp[0] = rp[max - 1] = 0;
            119.       rp++;

crypto/bn/bn_sqr.c:119:5: Assignment
            117.       rp = r;
            118.       rp[0] = rp[max - 1] = 0;
            119.       rp++;
                       ^
            120.       j = n;
            121.   

crypto/bn/bn_sqr.c:124:9: Array access: Offset: [16, +oo] (⇐ 1 + [15, +oo]) Size: [0, 8388607] by call to `rsa_sp800_56b_check_keypair` 
            122.       if (--j > 0) {
            123.           ap++;
            124.           rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
                           ^
            125.           rp += 2;
            126.       }

void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)
{
    int i, j, max;
    const BN_ULONG *ap;
    BN_ULONG *rp;
    max = n * 2;
    ap = a;
    rp = r;
    rp[0] = rp[max - 1] = 0;
    rp++;
    j = n;
    if (--j > 0) {
        ap++;
        rp[j] = bn_mul_words(rp, ap, j, ap[-1]);
        rp += 2;
    }
    for (i = n - 2; i > 0; i--) {
        j--;
        ap++;
        rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);
        rp += 2;
    }
    bn_add_words(r, r, r, max);
    bn_sqr_words(tmp, a, n);
    bn_add_words(r, r, tmp, max);
}
['static int test_sp80056b_keygen(int id)\n{\n    RSA *key = NULL;\n    int ret;\n    int sz = keygen_size[id];\n    ret = TEST_ptr(key = RSA_new())\n          && TEST_true(rsa_sp800_56b_generate_key(key, sz, NULL, NULL))\n          && TEST_true(rsa_sp800_56b_check_public(key))\n          && TEST_true(rsa_sp800_56b_check_private(key))\n          && TEST_true(rsa_sp800_56b_check_keypair(key, NULL, -1, sz));\n    RSA_free(key);\n    return ret;\n}', '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_check_keypair(const RSA *rsa, const BIGNUM *efixed,\n                                int strength, int nbits)\n{\n    int ret = 0;\n    BN_CTX *ctx = NULL;\n    BIGNUM *r = NULL;\n    if (rsa->p == NULL\n            || rsa->q == NULL\n            || rsa->e == NULL\n            || rsa->d == NULL\n            || rsa->n == NULL) {\n        RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_REQUEST);\n        return 0;\n    }\n    if (!rsa_sp800_56b_validate_strength(nbits, strength))\n        return 0;\n    if (efixed != NULL) {\n        if (BN_cmp(efixed, rsa->e) != 0) {\n            RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_REQUEST);\n            return 0;\n        }\n    }\n    if (!rsa_check_public_exponent(rsa->e)) {\n        RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR,\n               RSA_R_PUB_EXPONENT_OUT_OF_RANGE);\n        return 0;\n    }\n    if (nbits != BN_num_bits(rsa->n)) {\n        RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_KEYPAIR);\n        return 0;\n    }\n    ctx = BN_CTX_new();\n    if (ctx == NULL)\n        return 0;\n    BN_CTX_start(ctx);\n    r = BN_CTX_get(ctx);\n    if (r == NULL || !BN_mul(r, rsa->p, rsa->q, ctx))\n        goto err;\n    if (BN_cmp(rsa->n, r) != 0) {\n        RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_REQUEST);\n        goto err;\n    }\n    ret = rsa_check_prime_factor(rsa->p, rsa->e, nbits, ctx)\n          && rsa_check_prime_factor(rsa->q, rsa->e, nbits, ctx)\n          && (rsa_check_pminusq_diff(r, rsa->p, rsa->q, nbits) > 0)\n          && rsa_check_private_exponent(rsa, nbits, ctx)\n          && rsa_check_crt_components(rsa, ctx);\n    if (ret != 1)\n        RSAerr(RSA_F_RSA_SP800_56B_CHECK_KEYPAIR, RSA_R_INVALID_KEYPAIR);\nerr:\n    BN_clear(r);\n    BN_CTX_end(ctx);\n    BN_CTX_free(ctx);\n    return ret;\n}', 'int rsa_check_crt_components(const RSA *rsa, BN_CTX *ctx)\n{\n    int ret = 0;\n    BIGNUM *r = NULL, *p1 = NULL, *q1 = NULL;\n    if (rsa->dmp1 == NULL || rsa->dmq1 == NULL || rsa->iqmp == NULL) {\n        if (rsa->dmp1 != NULL || rsa->dmq1 != NULL || rsa->iqmp != NULL)\n            return 0;\n        return 1;\n    }\n    BN_CTX_start(ctx);\n    r = BN_CTX_get(ctx);\n    p1 = BN_CTX_get(ctx);\n    q1 = BN_CTX_get(ctx);\n    ret = (q1 != NULL)\n          && (BN_copy(p1, rsa->p) != NULL)\n          && BN_sub_word(p1, 1)\n          && (BN_copy(q1, rsa->q) != NULL)\n          && BN_sub_word(q1, 1)\n          && (BN_cmp(rsa->dmp1, BN_value_one()) > 0)\n          && (BN_cmp(rsa->dmp1, p1) < 0)\n          && (BN_cmp(rsa->dmq1, BN_value_one()) > 0)\n          && (BN_cmp(rsa->dmq1, q1) < 0)\n          && (BN_cmp(rsa->iqmp, BN_value_one()) > 0)\n          && (BN_cmp(rsa->iqmp, rsa->p) < 0)\n          && BN_mod_mul(r, rsa->dmp1, rsa->e, p1, ctx)\n          && BN_is_one(r)\n          && BN_mod_mul(r, rsa->dmq1, rsa->e, q1, ctx)\n          && BN_is_one(r)\n          && BN_mod_mul(r, rsa->iqmp, rsa->q, rsa->p, ctx)\n          && BN_is_one(r);\n    BN_clear(p1);\n    BN_clear(q1);\n    BN_CTX_end(ctx);\n    return ret;\n}', 'int BN_cmp(const BIGNUM *a, const BIGNUM *b)\n{\n    int i;\n    int gt, lt;\n    BN_ULONG t1, t2;\n    if ((a == NULL) || (b == NULL)) {\n        if (a != NULL)\n            return -1;\n        else if (b != NULL)\n            return 1;\n        else\n            return 0;\n    }\n    bn_check_top(a);\n    bn_check_top(b);\n    if (a->neg != b->neg) {\n        if (a->neg)\n            return -1;\n        else\n            return 1;\n    }\n    if (a->neg == 0) {\n        gt = 1;\n        lt = -1;\n    } else {\n        gt = -1;\n        lt = 1;\n    }\n    if (a->top > b->top)\n        return gt;\n    if (a->top < b->top)\n        return lt;\n    for (i = a->top - 1; i >= 0; i--) {\n        t1 = a->d[i];\n        t2 = b->d[i];\n        if (t1 > t2)\n            return gt;\n        if (t1 < t2)\n            return lt;\n    }\n    return 0;\n}', 'int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m,\n               BN_CTX *ctx)\n{\n    BIGNUM *t;\n    int ret = 0;\n    bn_check_top(a);\n    bn_check_top(b);\n    bn_check_top(m);\n    BN_CTX_start(ctx);\n    if ((t = BN_CTX_get(ctx)) == NULL)\n        goto err;\n    if (a == b) {\n        if (!BN_sqr(t, a, ctx))\n            goto err;\n    } else {\n        if (!BN_mul(t, a, b, ctx))\n            goto err;\n    }\n    if (!BN_nnmod(r, t, m, ctx))\n        goto err;\n    bn_check_top(r);\n    ret = 1;\n err:\n    BN_CTX_end(ctx);\n    return ret;\n}', 'int BN_sqr(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n    int ret = bn_sqr_fixed_top(r, a, ctx);\n    bn_correct_top(r);\n    bn_check_top(r);\n    return ret;\n}', 'int bn_sqr_fixed_top(BIGNUM *r, const BIGNUM *a, BN_CTX *ctx)\n{\n    int max, al;\n    int ret = 0;\n    BIGNUM *tmp, *rr;\n    bn_check_top(a);\n    al = a->top;\n    if (al <= 0) {\n        r->top = 0;\n        r->neg = 0;\n        return 1;\n    }\n    BN_CTX_start(ctx);\n    rr = (a != r) ? r : BN_CTX_get(ctx);\n    tmp = BN_CTX_get(ctx);\n    if (rr == NULL || tmp == NULL)\n        goto err;\n    max = 2 * al;\n    if (bn_wexpand(rr, max) == NULL)\n        goto err;\n    if (al == 4) {\n#ifndef BN_SQR_COMBA\n        BN_ULONG t[8];\n        bn_sqr_normal(rr->d, a->d, 4, t);\n#else\n        bn_sqr_comba4(rr->d, a->d);\n#endif\n    } else if (al == 8) {\n#ifndef BN_SQR_COMBA\n        BN_ULONG t[16];\n        bn_sqr_normal(rr->d, a->d, 8, t);\n#else\n        bn_sqr_comba8(rr->d, a->d);\n#endif\n    } else {\n#if defined(BN_RECURSION)\n        if (al < BN_SQR_RECURSIVE_SIZE_NORMAL) {\n            BN_ULONG t[BN_SQR_RECURSIVE_SIZE_NORMAL * 2];\n            bn_sqr_normal(rr->d, a->d, al, t);\n        } else {\n            int j, k;\n            j = BN_num_bits_word((BN_ULONG)al);\n            j = 1 << (j - 1);\n            k = j + j;\n            if (al == j) {\n                if (bn_wexpand(tmp, k * 2) == NULL)\n                    goto err;\n                bn_sqr_recursive(rr->d, a->d, al, tmp->d);\n            } else {\n                if (bn_wexpand(tmp, max) == NULL)\n                    goto err;\n                bn_sqr_normal(rr->d, a->d, al, tmp->d);\n            }\n        }\n#else\n        if (bn_wexpand(tmp, max) == NULL)\n            goto err;\n        bn_sqr_normal(rr->d, a->d, al, tmp->d);\n#endif\n    }\n    rr->neg = 0;\n    rr->top = max;\n    rr->flags |= BN_FLG_FIXED_TOP;\n    if (r != rr && BN_copy(r, rr) == NULL)\n        goto err;\n    ret = 1;\n err:\n    bn_check_top(rr);\n    bn_check_top(tmp);\n    BN_CTX_end(ctx);\n    return ret;\n}', 'void bn_sqr_normal(BN_ULONG *r, const BN_ULONG *a, int n, BN_ULONG *tmp)\n{\n    int i, j, max;\n    const BN_ULONG *ap;\n    BN_ULONG *rp;\n    max = n * 2;\n    ap = a;\n    rp = r;\n    rp[0] = rp[max - 1] = 0;\n    rp++;\n    j = n;\n    if (--j > 0) {\n        ap++;\n        rp[j] = bn_mul_words(rp, ap, j, ap[-1]);\n        rp += 2;\n    }\n    for (i = n - 2; i > 0; i--) {\n        j--;\n        ap++;\n        rp[j] = bn_mul_add_words(rp, ap, j, ap[-1]);\n        rp += 2;\n    }\n    bn_add_words(r, r, r, max);\n    bn_sqr_words(tmp, a, n);\n    bn_add_words(r, r, tmp, max);\n}']