crypto/sm2/sm2_crypt.c:181: error: BUFFER_OVERRUN_L3
  Offset: 0 Size: [0, 8388607] by call to `BN_priv_rand_range`.
Showing all 15 steps of the trace


crypto/sm2/sm2_crypt.c:160:9: Call
158.   
159.       BN_CTX_start(ctx);
160.       k = BN_CTX_get(ctx);
               ^
161.       x1 = BN_CTX_get(ctx);
162.       x2 = BN_CTX_get(ctx);

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:359:1: Parameter `*a->d`
    357.   }
    358.   
    359. > int BN_set_word(BIGNUM *a, BN_ULONG w)
    360.   {
    361.       bn_check_top(a);

crypto/bn/bn_lib.c:362:9: Call
    360.   {
    361.       bn_check_top(a);
    362.       if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
                   ^
    363.           return 0;
    364.       a->neg = 0;

crypto/bn/bn_lcl.h:660:1: Parameter `*a->d`
      658.                            const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx);
      659.   
      660. > static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)
      661.   {
      662.       if (bits > (INT_MAX - BN_BITS2 + 1))

crypto/sm2/sm2_crypt.c:181:10: Call
179.       memset(ciphertext_buf, 0, *ciphertext_len);
180.   
181.       if (!BN_priv_rand_range(k, order)) {
                ^
182.           SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);
183.           goto done;

crypto/bn/bn_rand.c:182:1: Parameter `*r->d`
  180.   }
  181.   
  182. > int BN_priv_rand_range(BIGNUM *r, const BIGNUM *range)
  183.   {
  184.       return bnrand_range(PRIVATE, r, range);

crypto/bn/bn_rand.c:184:12: Call
  182.   int BN_priv_rand_range(BIGNUM *r, const BIGNUM *range)
  183.   {
  184.       return bnrand_range(PRIVATE, r, range);
                    ^
  185.   }
  186.   

crypto/bn/bn_rand.c:113:1: Parameter `*r->d`
    111.   
    112.   /* random number r:  0 <= r < range */
    113. > static int bnrand_range(BNRAND_FLAG flag, BIGNUM *r, const BIGNUM *range)
    114.   {
    115.       int n;

crypto/bn/bn_rand.c:128:9: Call
    126.   
    127.       if (n == 1)
    128.           BN_zero(r);
                   ^
    129.       else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {
    130.           /*

crypto/bn/bn_lib.c:359:1: <Length trace>
357.   }
358.   
359. > int BN_set_word(BIGNUM *a, BN_ULONG w)
360.   {
361.       bn_check_top(a);

crypto/bn/bn_lib.c:359:1: Parameter `*a->d`
      357.   }
      358.   
      359. > int BN_set_word(BIGNUM *a, BN_ULONG w)
      360.   {
      361.       bn_check_top(a);

crypto/bn/bn_lib.c:362:9: Call
      360.   {
      361.       bn_check_top(a);
      362.       if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
                     ^
      363.           return 0;
      364.       a->neg = 0;

crypto/bn/bn_lcl.h:660:1: Parameter `*a->d`
        658.                            const BIGNUM *add, const BIGNUM *rem, BN_CTX *ctx);
        659.   
        660. > static ossl_inline BIGNUM *bn_expand(BIGNUM *a, int bits)
        661.   {
        662.       if (bits > (INT_MAX - BN_BITS2 + 1))

crypto/bn/bn_lib.c:365:5: Array access: Offset: 0 Size: [0, 8388607] by call to `BN_priv_rand_range` 
      363.           return 0;
      364.       a->neg = 0;
      365.       a->d[0] = w;
                 ^
      366.       a->top = (w ? 1 : 0);
      367.       a->flags &= ~BN_FLG_FIXED_TOP;

int BN_set_word(BIGNUM *a, BN_ULONG w)
{
    bn_check_top(a);
    if (bn_expand(a, (int)sizeof(BN_ULONG) * 8) == NULL)
        return 0;
    a->neg = 0;
    a->d[0] = w;
    a->top = (w ? 1 : 0);
    a->flags &= ~BN_FLG_FIXED_TOP;
    bn_check_top(a);
    return 1;
}
['int sm2_encrypt(const EC_KEY *key,\n                const EVP_MD *digest,\n                const uint8_t *msg,\n                size_t msg_len, uint8_t *ciphertext_buf, size_t *ciphertext_len)\n{\n    int rc = 0, ciphertext_leni;\n    size_t i;\n    BN_CTX *ctx = NULL;\n    BIGNUM *k = NULL;\n    BIGNUM *x1 = NULL;\n    BIGNUM *y1 = NULL;\n    BIGNUM *x2 = NULL;\n    BIGNUM *y2 = NULL;\n    EVP_MD_CTX *hash = EVP_MD_CTX_new();\n    struct SM2_Ciphertext_st ctext_struct;\n    const EC_GROUP *group = EC_KEY_get0_group(key);\n    const BIGNUM *order = EC_GROUP_get0_order(group);\n    const EC_POINT *P = EC_KEY_get0_public_key(key);\n    EC_POINT *kG = NULL;\n    EC_POINT *kP = NULL;\n    uint8_t *msg_mask = NULL;\n    uint8_t *x2y2 = NULL;\n    uint8_t *C3 = NULL;\n    size_t field_size;\n    const int C3_size = EVP_MD_size(digest);\n    ctext_struct.C2 = NULL;\n    ctext_struct.C3 = NULL;\n    if (hash == NULL || C3_size <= 0) {\n        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);\n        goto done;\n    }\n    field_size = ec_field_size(group);\n    if (field_size == 0) {\n        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);\n        goto done;\n    }\n    kG = EC_POINT_new(group);\n    kP = EC_POINT_new(group);\n    ctx = BN_CTX_new();\n    if (kG == NULL || kP == NULL || ctx == NULL) {\n        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);\n        goto done;\n    }\n    BN_CTX_start(ctx);\n    k = BN_CTX_get(ctx);\n    x1 = BN_CTX_get(ctx);\n    x2 = BN_CTX_get(ctx);\n    y1 = BN_CTX_get(ctx);\n    y2 = BN_CTX_get(ctx);\n    if (y2 == NULL) {\n        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_BN_LIB);\n        goto done;\n    }\n    x2y2 = OPENSSL_zalloc(2 * field_size);\n    C3 = OPENSSL_zalloc(C3_size);\n    if (x2y2 == NULL || C3 == NULL) {\n        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);\n        goto done;\n    }\n    memset(ciphertext_buf, 0, *ciphertext_len);\n    if (!BN_priv_rand_range(k, order)) {\n        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);\n        goto done;\n    }\n    if (!EC_POINT_mul(group, kG, k, NULL, NULL, ctx)\n            || !EC_POINT_get_affine_coordinates(group, kG, x1, y1, ctx)\n            || !EC_POINT_mul(group, kP, NULL, P, k, ctx)\n            || !EC_POINT_get_affine_coordinates(group, kP, x2, y2, ctx)) {\n        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EC_LIB);\n        goto done;\n    }\n    if (BN_bn2binpad(x2, x2y2, field_size) < 0\n            || BN_bn2binpad(y2, x2y2 + field_size, field_size) < 0) {\n        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);\n        goto done;\n    }\n    msg_mask = OPENSSL_zalloc(msg_len);\n    if (msg_mask == NULL) {\n       SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);\n       goto done;\n   }\n    if (!ecdh_KDF_X9_63(msg_mask, msg_len, x2y2, 2 * field_size, NULL, 0,\n                        digest)) {\n        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EVP_LIB);\n        goto done;\n    }\n    for (i = 0; i != msg_len; ++i)\n        msg_mask[i] ^= msg[i];\n    if (EVP_DigestInit(hash, digest) == 0\n            || EVP_DigestUpdate(hash, x2y2, field_size) == 0\n            || EVP_DigestUpdate(hash, msg, msg_len) == 0\n            || EVP_DigestUpdate(hash, x2y2 + field_size, field_size) == 0\n            || EVP_DigestFinal(hash, C3, NULL) == 0) {\n        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_EVP_LIB);\n        goto done;\n    }\n    ctext_struct.C1x = x1;\n    ctext_struct.C1y = y1;\n    ctext_struct.C3 = ASN1_OCTET_STRING_new();\n    ctext_struct.C2 = ASN1_OCTET_STRING_new();\n    if (ctext_struct.C3 == NULL || ctext_struct.C2 == NULL) {\n       SM2err(SM2_F_SM2_ENCRYPT, ERR_R_MALLOC_FAILURE);\n       goto done;\n    }\n    if (!ASN1_OCTET_STRING_set(ctext_struct.C3, C3, C3_size)\n            || !ASN1_OCTET_STRING_set(ctext_struct.C2, msg_mask, msg_len)) {\n        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);\n        goto done;\n    }\n    ciphertext_leni = i2d_SM2_Ciphertext(&ctext_struct, &ciphertext_buf);\n    if (ciphertext_leni < 0) {\n        SM2err(SM2_F_SM2_ENCRYPT, ERR_R_INTERNAL_ERROR);\n        goto done;\n    }\n    *ciphertext_len = (size_t)ciphertext_leni;\n    rc = 1;\n done:\n    ASN1_OCTET_STRING_free(ctext_struct.C2);\n    ASN1_OCTET_STRING_free(ctext_struct.C3);\n    OPENSSL_free(msg_mask);\n    OPENSSL_free(x2y2);\n    OPENSSL_free(C3);\n    EVP_MD_CTX_free(hash);\n    BN_CTX_free(ctx);\n    EC_POINT_free(kG);\n    EC_POINT_free(kP);\n    return rc;\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    a->flags &= ~BN_FLG_FIXED_TOP;\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}', 'int BN_priv_rand_range(BIGNUM *r, const BIGNUM *range)\n{\n    return bnrand_range(PRIVATE, r, range);\n}', 'static int bnrand_range(BNRAND_FLAG flag, BIGNUM *r, const BIGNUM *range)\n{\n    int n;\n    int count = 100;\n    if (range->neg || BN_is_zero(range)) {\n        BNerr(BN_F_BNRAND_RANGE, BN_R_INVALID_RANGE);\n        return 0;\n    }\n    n = BN_num_bits(range);\n    if (n == 1)\n        BN_zero(r);\n    else if (!BN_is_bit_set(range, n - 2) && !BN_is_bit_set(range, n - 3)) {\n        do {\n            if (!bnrand(flag, r, n + 1, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))\n                return 0;\n            if (BN_cmp(r, range) >= 0) {\n                if (!BN_sub(r, r, range))\n                    return 0;\n                if (BN_cmp(r, range) >= 0)\n                    if (!BN_sub(r, r, range))\n                        return 0;\n            }\n            if (!--count) {\n                BNerr(BN_F_BNRAND_RANGE, BN_R_TOO_MANY_ITERATIONS);\n                return 0;\n            }\n        }\n        while (BN_cmp(r, range) >= 0);\n    } else {\n        do {\n            if (!bnrand(flag, r, n, BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))\n                return 0;\n            if (!--count) {\n                BNerr(BN_F_BNRAND_RANGE, BN_R_TOO_MANY_ITERATIONS);\n                return 0;\n            }\n        }\n        while (BN_cmp(r, range) >= 0);\n    }\n    bn_check_top(r);\n    return 1;\n}']