]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/secmemtest.c
Remove BIO_seek/BIO_tell from evp_test.c
[thirdparty/openssl.git] / test / secmemtest.c
1 /*
2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #include <openssl/crypto.h>
11
12 #define perror_line() perror_line1(__LINE__)
13 #define perror_line1(l) perror_line2(l)
14 #define perror_line2(l) perror("failed " #l)
15
16 int main(int argc, char **argv)
17 {
18 #if defined(OPENSSL_SYS_LINUX) || defined(OPENSSL_SYS_UNIX)
19 char *p = NULL, *q = NULL, *r = NULL, *s = NULL;
20
21 r = OPENSSL_secure_malloc(20);
22 /* r = non-secure 20 */
23 if (r == NULL) {
24 perror_line();
25 return 1;
26 }
27 if (!CRYPTO_secure_malloc_init(4096, 32)) {
28 perror_line();
29 return 1;
30 }
31 if (CRYPTO_secure_allocated(r)) {
32 perror_line();
33 return 1;
34 }
35 p = OPENSSL_secure_malloc(20);
36 /* r = non-secure 20, p = secure 20 */
37 if (!CRYPTO_secure_allocated(p)) {
38 perror_line();
39 return 1;
40 }
41 /* 20 secure -> 32-byte minimum allocaton unit */
42 if (CRYPTO_secure_used() != 32) {
43 perror_line();
44 return 1;
45 }
46 q = OPENSSL_malloc(20);
47 /* r = non-secure 20, p = secure 20, q = non-secure 20 */
48 if (CRYPTO_secure_allocated(q)) {
49 perror_line();
50 return 1;
51 }
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)) {
55 perror_line();
56 return 1;
57 }
58 /* 2 * 20 secure -> 64 bytes allocated */
59 if (CRYPTO_secure_used() != 64) {
60 perror_line();
61 return 1;
62 }
63 OPENSSL_secure_free(p);
64 /* 20 secure -> 32 bytes allocated */
65 if (CRYPTO_secure_used() != 32) {
66 perror_line();
67 return 1;
68 }
69 OPENSSL_free(q);
70 /* should not complete, as secure memory is still allocated */
71 if (CRYPTO_secure_malloc_done()) {
72 perror_line();
73 return 1;
74 }
75 if (!CRYPTO_secure_malloc_initialized()) {
76 perror_line();
77 return 1;
78 }
79 OPENSSL_secure_free(s);
80 /* secure memory should now be 0, so done should complete */
81 if (CRYPTO_secure_used() != 0) {
82 perror_line();
83 return 1;
84 }
85 if (!CRYPTO_secure_malloc_done()) {
86 perror_line();
87 return 1;
88 }
89 if (CRYPTO_secure_malloc_initialized()) {
90 perror_line();
91 return 1;
92 }
93 /* this can complete - it was not really secure */
94 OPENSSL_secure_free(r);
95 #else
96 /* Should fail. */
97 if (CRYPTO_secure_malloc_init(4096, 32)) {
98 perror_line();
99 return 1;
100 }
101 #endif
102 return 0;
103 }