2016-05-17 18:20:24 +00:00
|
|
|
/*
|
2017-04-10 00:05:55 +00:00
|
|
|
* Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
|
2016-05-17 18:20:24 +00:00
|
|
|
*
|
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
2015-04-24 20:39:40 +00:00
|
|
|
|
|
|
|
#include <openssl/crypto.h>
|
|
|
|
|
2017-04-10 00:05:55 +00:00
|
|
|
#include "test_main.h"
|
|
|
|
#include "testutil.h"
|
2016-04-11 20:03:42 +00:00
|
|
|
|
2017-04-10 00:05:55 +00:00
|
|
|
static int test_sec_mem(void)
|
2015-04-24 20:39:40 +00:00
|
|
|
{
|
|
|
|
#if defined(OPENSSL_SYS_LINUX) || defined(OPENSSL_SYS_UNIX)
|
2017-04-10 00:05:55 +00:00
|
|
|
int testresult = 0;
|
2016-04-11 20:03:42 +00:00
|
|
|
char *p = NULL, *q = NULL, *r = NULL, *s = NULL;
|
2015-04-24 20:39:40 +00:00
|
|
|
|
2016-04-11 20:03:42 +00:00
|
|
|
r = OPENSSL_secure_malloc(20);
|
|
|
|
/* r = non-secure 20 */
|
2017-04-10 00:05:55 +00:00
|
|
|
if (!TEST_ptr(r)
|
|
|
|
|| !TEST_true(CRYPTO_secure_malloc_init(4096, 32))
|
|
|
|
|| !TEST_false(CRYPTO_secure_allocated(r)))
|
|
|
|
goto end;
|
2015-04-24 20:39:40 +00:00
|
|
|
p = OPENSSL_secure_malloc(20);
|
2017-04-10 00:05:55 +00:00
|
|
|
if (!TEST_ptr(p)
|
|
|
|
/* r = non-secure 20, p = secure 20 */
|
|
|
|
|| !TEST_true(CRYPTO_secure_allocated(p))
|
|
|
|
/* 20 secure -> 32-byte minimum allocaton unit */
|
|
|
|
|| !TEST_size_t_eq(CRYPTO_secure_used(), 32))
|
|
|
|
goto end;
|
2015-04-24 20:39:40 +00:00
|
|
|
q = OPENSSL_malloc(20);
|
2017-04-10 00:05:55 +00:00
|
|
|
if (!TEST_ptr(q))
|
|
|
|
goto end;
|
2016-04-11 20:03:42 +00:00
|
|
|
/* r = non-secure 20, p = secure 20, q = non-secure 20 */
|
2017-04-10 00:05:55 +00:00
|
|
|
if (!TEST_false(CRYPTO_secure_allocated(q)))
|
|
|
|
goto end;
|
2016-04-11 20:03:42 +00:00
|
|
|
s = OPENSSL_secure_malloc(20);
|
2017-04-10 00:05:55 +00:00
|
|
|
if (!TEST_ptr(s)
|
|
|
|
/* r = non-secure 20, p = secure 20, q = non-secure 20, s = secure 20 */
|
|
|
|
|| !TEST_true(CRYPTO_secure_allocated(s))
|
|
|
|
/* 2 * 20 secure -> 64 bytes allocated */
|
|
|
|
|| !TEST_size_t_eq(CRYPTO_secure_used(), 64))
|
|
|
|
goto end;
|
2016-02-17 10:03:55 +00:00
|
|
|
OPENSSL_secure_free(p);
|
2017-04-10 00:05:55 +00:00
|
|
|
p = NULL;
|
2016-04-11 20:03:42 +00:00
|
|
|
/* 20 secure -> 32 bytes allocated */
|
2017-04-10 00:05:55 +00:00
|
|
|
if (!TEST_size_t_eq(CRYPTO_secure_used(), 32))
|
|
|
|
goto end;
|
2016-02-17 10:03:55 +00:00
|
|
|
OPENSSL_free(q);
|
2017-04-10 00:05:55 +00:00
|
|
|
q = NULL;
|
2016-04-11 20:03:42 +00:00
|
|
|
/* should not complete, as secure memory is still allocated */
|
2017-04-10 00:05:55 +00:00
|
|
|
if (!TEST_false(CRYPTO_secure_malloc_done())
|
|
|
|
|| !TEST_true(CRYPTO_secure_malloc_initialized()))
|
|
|
|
goto end;
|
2016-04-11 20:03:42 +00:00
|
|
|
OPENSSL_secure_free(s);
|
2017-04-10 00:05:55 +00:00
|
|
|
s = NULL;
|
2016-04-11 20:03:42 +00:00
|
|
|
/* secure memory should now be 0, so done should complete */
|
2017-04-10 00:05:55 +00:00
|
|
|
if (!TEST_size_t_eq(CRYPTO_secure_used(), 0)
|
|
|
|
|| !TEST_true(CRYPTO_secure_malloc_done())
|
|
|
|
|| !TEST_false(CRYPTO_secure_malloc_initialized()))
|
|
|
|
goto end;
|
2016-04-11 20:03:42 +00:00
|
|
|
/* this can complete - it was not really secure */
|
2017-04-10 00:05:55 +00:00
|
|
|
testresult = 1;
|
|
|
|
end:
|
|
|
|
OPENSSL_secure_free(p);
|
|
|
|
OPENSSL_free(q);
|
2016-04-11 20:03:42 +00:00
|
|
|
OPENSSL_secure_free(r);
|
2017-04-10 00:05:55 +00:00
|
|
|
OPENSSL_secure_free(s);
|
|
|
|
return testresult;
|
2015-04-24 20:39:40 +00:00
|
|
|
#else
|
|
|
|
/* Should fail. */
|
2017-04-10 00:05:55 +00:00
|
|
|
return TEST_false(CRYPTO_secure_malloc_init(4096, 32));
|
2015-04-24 20:39:40 +00:00
|
|
|
#endif
|
2017-04-10 00:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void register_tests(void)
|
|
|
|
{
|
|
|
|
ADD_TEST(test_sec_mem);
|
2015-04-24 20:39:40 +00:00
|
|
|
}
|