2 * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/crypto.h>
14 static int test_sec_mem(void)
16 #if defined(OPENSSL_SYS_LINUX) || defined(OPENSSL_SYS_UNIX)
18 char *p = NULL, *q = NULL, *r = NULL, *s = NULL;
20 r = OPENSSL_secure_malloc(20);
21 /* r = non-secure 20 */
23 || !TEST_true(CRYPTO_secure_malloc_init(4096, 32))
24 || !TEST_false(CRYPTO_secure_allocated(r)))
26 p = OPENSSL_secure_malloc(20);
28 /* r = non-secure 20, p = secure 20 */
29 || !TEST_true(CRYPTO_secure_allocated(p))
30 /* 20 secure -> 32-byte minimum allocaton unit */
31 || !TEST_size_t_eq(CRYPTO_secure_used(), 32))
33 q = OPENSSL_malloc(20);
36 /* r = non-secure 20, p = secure 20, q = non-secure 20 */
37 if (!TEST_false(CRYPTO_secure_allocated(q)))
39 s = OPENSSL_secure_malloc(20);
41 /* r = non-secure 20, p = secure 20, q = non-secure 20, s = secure 20 */
42 || !TEST_true(CRYPTO_secure_allocated(s))
43 /* 2 * 20 secure -> 64 bytes allocated */
44 || !TEST_size_t_eq(CRYPTO_secure_used(), 64))
46 OPENSSL_secure_free(p);
48 /* 20 secure -> 32 bytes allocated */
49 if (!TEST_size_t_eq(CRYPTO_secure_used(), 32))
53 /* should not complete, as secure memory is still allocated */
54 if (!TEST_false(CRYPTO_secure_malloc_done())
55 || !TEST_true(CRYPTO_secure_malloc_initialized()))
57 OPENSSL_secure_free(s);
59 /* secure memory should now be 0, so done should complete */
60 if (!TEST_size_t_eq(CRYPTO_secure_used(), 0)
61 || !TEST_true(CRYPTO_secure_malloc_done())
62 || !TEST_false(CRYPTO_secure_malloc_initialized()))
64 /* this can complete - it was not really secure */
67 OPENSSL_secure_free(p);
69 OPENSSL_secure_free(r);
70 OPENSSL_secure_free(s);
74 return TEST_false(CRYPTO_secure_malloc_init(4096, 32));
78 void register_tests(void)
80 ADD_TEST(test_sec_mem);