Skip to content

Commit

Permalink
test/param_build_test.c: test zero BIGNUM
Browse files Browse the repository at this point in the history
We also add tests where the zero bignum is the only parameter, to test what
that does with the allocated blocks that the OSSL_PARAM_BLD functionality
handles.

Reviewed-by: Paul Dale <pauli@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #20013)
  • Loading branch information
levitte committed Jan 11, 2023
1 parent 174d166 commit b49cf27
Showing 1 changed file with 94 additions and 1 deletion.
95 changes: 94 additions & 1 deletion test/param_build_test.c
Expand Up @@ -16,10 +16,77 @@

static const OSSL_PARAM params_empty[] = { OSSL_PARAM_END };

static int template_public_single_zero_test(void)
{
OSSL_PARAM_BLD *bld = NULL;
OSSL_PARAM *params = NULL, *params_blt = NULL, *p;
BIGNUM *zbn = NULL, *zbn_res = NULL;
int res = 0;

if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
|| !TEST_ptr(zbn = BN_new())
|| !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", zbn))
|| !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld)))
goto err;

params = params_blt;
/* Check BN (zero BN becomes unsigned integer) */
if (!TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber"))
|| !TEST_str_eq(p->key, "zeronumber")
|| !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
|| !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res))
|| !TEST_BN_eq(zbn_res, zbn))
goto err;
res = 1;
err:
if (params != params_blt)
OPENSSL_free(params);
OSSL_PARAM_free(params_blt);
OSSL_PARAM_BLD_free(bld);
BN_free(zbn);
BN_free(zbn_res);
return res;
}

static int template_private_single_zero_test(void)
{
OSSL_PARAM_BLD *bld = NULL;
OSSL_PARAM *params = NULL, *params_blt = NULL, *p;
BIGNUM *zbn = NULL, *zbn_res = NULL;
int res = 0;

if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())
|| !TEST_ptr(zbn = BN_secure_new())
|| !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", zbn))
|| !TEST_ptr(params_blt = OSSL_PARAM_BLD_to_param(bld)))
goto err;

params = params_blt;
/* Check BN (zero BN becomes unsigned integer) */
if (!TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber"))
|| !TEST_true(CRYPTO_secure_allocated(p->data))
|| !TEST_str_eq(p->key, "zeronumber")
|| !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
|| !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res))
|| !TEST_int_eq(BN_get_flags(zbn, BN_FLG_SECURE), BN_FLG_SECURE)
|| !TEST_BN_eq(zbn_res, zbn))
goto err;
res = 1;
err:
if (params != params_blt)
OPENSSL_free(params);
OSSL_PARAM_free(params_blt);
OSSL_PARAM_BLD_free(bld);
BN_free(zbn);
BN_free(zbn_res);
return res;
}

static int template_public_test(int tstid)
{
OSSL_PARAM_BLD *bld = OSSL_PARAM_BLD_new();
OSSL_PARAM *params = NULL, *params_blt = NULL, *p1 = NULL, *p;
BIGNUM *zbn = NULL, *zbn_res = NULL;
BIGNUM *pbn = NULL, *pbn_res = NULL;
BIGNUM *nbn = NULL, *nbn_res = NULL;
int i;
Expand All @@ -38,6 +105,8 @@ static int template_public_test(int tstid)
|| !TEST_true(OSSL_PARAM_BLD_push_int64(bld, "i64", -9999999))
|| !TEST_true(OSSL_PARAM_BLD_push_time_t(bld, "t", 11224))
|| !TEST_true(OSSL_PARAM_BLD_push_double(bld, "d", 1.61803398875))
|| !TEST_ptr(zbn = BN_new())
|| !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", zbn))
|| !TEST_ptr(pbn = BN_new())
|| !TEST_true(BN_set_word(pbn, 1729))
|| !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", pbn))
Expand Down Expand Up @@ -123,6 +192,12 @@ static int template_public_test(int tstid)
|| !TEST_ptr(p = OSSL_PARAM_locate(params, "utf8_p"))
|| !TEST_true(OSSL_PARAM_get_utf8_ptr(p, &cutf))
|| !TEST_str_eq(cutf, "bar-boom")
/* Check BN (zero BN becomes unsigned integer) */
|| !TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber"))
|| !TEST_str_eq(p->key, "zeronumber")
|| !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
|| !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res))
|| !TEST_BN_eq(zbn_res, zbn)
/* Check BN (positive BN becomes unsigned integer) */
|| !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber"))
|| !TEST_str_eq(p->key, "bignumber")
Expand All @@ -144,6 +219,8 @@ static int template_public_test(int tstid)
OSSL_PARAM_free(params_blt);
OSSL_PARAM_BLD_free(bld);
OPENSSL_free(utf);
BN_free(zbn);
BN_free(zbn_res);
BN_free(pbn);
BN_free(pbn_res);
BN_free(nbn);
Expand All @@ -165,6 +242,7 @@ static int template_private_test(int tstid)
uint32_t i32;
uint64_t i64;
size_t st;
BIGNUM *zbn = NULL, *zbn_res = NULL;
BIGNUM *pbn = NULL, *pbn_res = NULL;
BIGNUM *nbn = NULL, *nbn_res = NULL;
int res = 0;
Expand All @@ -184,6 +262,8 @@ static int template_private_test(int tstid)
|| !TEST_true(OSSL_PARAM_BLD_push_uint32(bld, "i32", 1532))
|| !TEST_true(OSSL_PARAM_BLD_push_uint64(bld, "i64", 9999999))
|| !TEST_true(OSSL_PARAM_BLD_push_size_t(bld, "st", 65537))
|| !TEST_ptr(zbn = BN_secure_new())
|| !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "zeronumber", zbn))
|| !TEST_ptr(pbn = BN_secure_new())
|| !TEST_true(BN_set_word(pbn, 1729))
|| !TEST_true(OSSL_PARAM_BLD_push_BN(bld, "bignumber", pbn))
Expand Down Expand Up @@ -269,6 +349,14 @@ static int template_private_test(int tstid)
|| !TEST_str_eq(p->key, "oct_p")
|| !TEST_uint_eq(p->data_type, OSSL_PARAM_OCTET_PTR)
|| !TEST_mem_eq(*(void **)p->data, p->data_size, data2, data2_size)
/* Check BN (zero BN becomes unsigned integer) */
|| !TEST_ptr(p = OSSL_PARAM_locate(params, "zeronumber"))
|| !TEST_true(CRYPTO_secure_allocated(p->data))
|| !TEST_str_eq(p->key, "zeronumber")
|| !TEST_uint_eq(p->data_type, OSSL_PARAM_UNSIGNED_INTEGER)
|| !TEST_true(OSSL_PARAM_get_BN(p, &zbn_res))
|| !TEST_int_eq(BN_get_flags(pbn, BN_FLG_SECURE), BN_FLG_SECURE)
|| !TEST_BN_eq(zbn_res, zbn)
/* Check BN (positive BN becomes unsigned integer) */
|| !TEST_ptr(p = OSSL_PARAM_locate(params, "bignumber"))
|| !TEST_true(CRYPTO_secure_allocated(p->data))
Expand All @@ -295,6 +383,8 @@ static int template_private_test(int tstid)
OSSL_PARAM_BLD_free(bld);
OPENSSL_secure_free(data1);
OPENSSL_secure_free(data2);
BN_free(zbn);
BN_free(zbn_res);
BN_free(pbn);
BN_free(pbn_res);
BN_free(nbn);
Expand Down Expand Up @@ -460,10 +550,13 @@ static int builder_merge_test(void)

int setup_tests(void)
{
ADD_TEST(template_public_single_zero_test);
ADD_ALL_TESTS(template_public_test, 5);
/* Only run the secure memory testing if we have secure memory available */
if (CRYPTO_secure_malloc_init(1<<16, 16))
if (CRYPTO_secure_malloc_init(1<<16, 16)) {
ADD_TEST(template_private_single_zero_test);
ADD_ALL_TESTS(template_private_test, 5);
}
ADD_TEST(builder_limit_test);
ADD_TEST(builder_merge_test);
return 1;
Expand Down

0 comments on commit b49cf27

Please sign in to comment.