#include <openssl/crypto.h>
#include "testutil.h"
#include "internal/e_os.h"
static int test_sec_mem(void)
{
#ifndef OPENSSL_NO_SECURE_MEMORY
int testresult = 0;
char *p = NULL, *q = NULL, *r = NULL, *s = NULL;
TEST_info("Secure memory is implemented.");
s = OPENSSL_secure_malloc(20);
if (!TEST_ptr(s)
|| !TEST_false(CRYPTO_secure_allocated(s)))
goto end;
r = OPENSSL_secure_malloc(20);
if (!TEST_ptr(r)
|| !TEST_true(CRYPTO_secure_malloc_init(4096, 32))
|| !TEST_false(CRYPTO_secure_allocated(r)))
goto end;
p = OPENSSL_secure_malloc(20);
if (!TEST_ptr(p)
|| !TEST_true(CRYPTO_secure_allocated(p))
|| !TEST_size_t_eq(CRYPTO_secure_used(), 32))
goto end;
q = OPENSSL_malloc(20);
if (!TEST_ptr(q))
goto end;
if (!TEST_false(CRYPTO_secure_allocated(q)))
goto end;
OPENSSL_secure_clear_free(s, 20);
s = OPENSSL_secure_malloc(20);
if (!TEST_ptr(s)
|| !TEST_true(CRYPTO_secure_allocated(s))
|| !TEST_size_t_eq(CRYPTO_secure_used(), 64))
goto end;
OPENSSL_secure_clear_free(p, 20);
p = NULL;
if (!TEST_size_t_eq(CRYPTO_secure_used(), 32))
goto end;
OPENSSL_free(q);
q = NULL;
if (!TEST_false(CRYPTO_secure_malloc_done())
|| !TEST_true(CRYPTO_secure_malloc_initialized()))
goto end;
OPENSSL_secure_free(s);
s = NULL;
if (!TEST_size_t_eq(CRYPTO_secure_used(), 0)
|| !TEST_true(CRYPTO_secure_malloc_done())
|| !TEST_false(CRYPTO_secure_malloc_initialized()))
goto end;
TEST_info("Possible infinite loop: allocate more than available");
if (!TEST_true(CRYPTO_secure_malloc_init(32768, 16)))
goto end;
TEST_ptr_null(OPENSSL_secure_malloc((size_t)-1));
TEST_true(CRYPTO_secure_malloc_done());
if (TEST_false(CRYPTO_secure_malloc_init(16, 16)) && !TEST_false(CRYPTO_secure_malloc_initialized())) {
TEST_true(CRYPTO_secure_malloc_done());
goto end;
}
#if 0
if (sizeof(size_t) > 4) {
TEST_info("Possible infinite loop: 1<<31 limit");
if (TEST_true(CRYPTO_secure_malloc_init((size_t)1<<34, 1<<4) != 0))
TEST_true(CRYPTO_secure_malloc_done());
}
#endif
testresult = 1;
end:
OPENSSL_secure_free(p);
OPENSSL_free(q);
OPENSSL_secure_free(r);
OPENSSL_secure_free(s);
return testresult;
#else
TEST_info("Secure memory is *not* implemented.");
return TEST_false(CRYPTO_secure_malloc_init(4096, 32));
#endif
}
static int test_sec_mem_clear(void)
{
#ifndef OPENSSL_NO_SECURE_MEMORY
const int size = 64;
unsigned char *p = NULL;
int i, res = 0;
if (!TEST_true(CRYPTO_secure_malloc_init(4096, 32))
|| !TEST_ptr(p = OPENSSL_secure_malloc(size)))
goto err;
for (i = 0; i < size; i++)
if (!TEST_uchar_eq(p[i], 0))
goto err;
for (i = 0; i < size; i++)
p[i] = (unsigned char)(i + ' ' + 1);
OPENSSL_secure_free(p);
for (i = sizeof(void *) * 2; i < size; i++)
if (!TEST_uchar_eq(p[i], 0))
return 0;
res = 1;
p = NULL;
err:
OPENSSL_secure_free(p);
CRYPTO_secure_malloc_done();
return res;
#else
return 1;
#endif
}
int setup_tests(void)
{
ADD_TEST(test_sec_mem);
ADD_TEST(test_sec_mem_clear);
return 1;
}