]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/secmemtest.c
2 * Copyright 2015-2016 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>
12 #define perror_line() perror_line1(__LINE__)
13 #define perror_line1(l) perror_line2(l)
14 #define perror_line2(l) perror("failed " #l)
16 int main(int argc
, char **argv
)
18 #if defined(OPENSSL_SYS_LINUX) || defined(OPENSSL_SYS_UNIX)
19 char *p
= NULL
, *q
= NULL
, *r
= NULL
, *s
= NULL
;
21 r
= OPENSSL_secure_malloc(20);
22 /* r = non-secure 20 */
27 if (!CRYPTO_secure_malloc_init(4096, 32)) {
31 if (CRYPTO_secure_allocated(r
)) {
35 p
= OPENSSL_secure_malloc(20);
36 /* r = non-secure 20, p = secure 20 */
37 if (!CRYPTO_secure_allocated(p
)) {
41 /* 20 secure -> 32-byte minimum allocaton unit */
42 if (CRYPTO_secure_used() != 32) {
46 q
= OPENSSL_malloc(20);
47 /* r = non-secure 20, p = secure 20, q = non-secure 20 */
48 if (CRYPTO_secure_allocated(q
)) {
52 s
= OPENSSL_secure_malloc(20);
53 /* r = non-secure 20, p = secure 20, q = non-secure 20, s = secure 20 */
54 if (!CRYPTO_secure_allocated(s
)) {
58 /* 2 * 20 secure -> 64 bytes allocated */
59 if (CRYPTO_secure_used() != 64) {
63 OPENSSL_secure_free(p
);
64 /* 20 secure -> 32 bytes allocated */
65 if (CRYPTO_secure_used() != 32) {
70 /* should not complete, as secure memory is still allocated */
71 if (CRYPTO_secure_malloc_done()) {
75 if (!CRYPTO_secure_malloc_initialized()) {
79 OPENSSL_secure_free(s
);
80 /* secure memory should now be 0, so done should complete */
81 if (CRYPTO_secure_used() != 0) {
85 if (!CRYPTO_secure_malloc_done()) {
89 if (CRYPTO_secure_malloc_initialized()) {
93 /* this can complete - it was not really secure */
94 OPENSSL_secure_free(r
);
97 if (CRYPTO_secure_malloc_init(4096, 32)) {