2 #include <openssl/crypto.h>
4 #define perror_line() perror_line1(__LINE__)
5 #define perror_line1(l) perror_line2(l)
6 #define perror_line2(l) perror("failed " #l)
8 int main(int argc, char **argv)
10 #if defined(OPENSSL_SYS_LINUX) || defined(OPENSSL_SYS_UNIX)
11 char *p = NULL, *q = NULL, *r = NULL, *s = NULL;
13 r = OPENSSL_secure_malloc(20);
14 /* r = non-secure 20 */
19 if (!CRYPTO_secure_malloc_init(4096, 32)) {
23 if (CRYPTO_secure_allocated(r)) {
27 p = OPENSSL_secure_malloc(20);
28 /* r = non-secure 20, p = secure 20 */
29 if (!CRYPTO_secure_allocated(p)) {
33 /* 20 secure -> 32-byte minimum allocaton unit */
34 if (CRYPTO_secure_used() != 32) {
38 q = OPENSSL_malloc(20);
39 /* r = non-secure 20, p = secure 20, q = non-secure 20 */
40 if (CRYPTO_secure_allocated(q)) {
44 s = OPENSSL_secure_malloc(20);
45 /* r = non-secure 20, p = secure 20, q = non-secure 20, s = secure 20 */
46 if (!CRYPTO_secure_allocated(s)) {
50 /* 2 * 20 secure -> 64 bytes allocated */
51 if (CRYPTO_secure_used() != 64) {
55 OPENSSL_secure_free(p);
56 /* 20 secure -> 32 bytes allocated */
57 if (CRYPTO_secure_used() != 32) {
62 /* should not complete, as secure memory is still allocated */
63 if (CRYPTO_secure_malloc_done()) {
67 if (!CRYPTO_secure_malloc_initialized()) {
71 OPENSSL_secure_free(s);
72 /* secure memory should now be 0, so done should complete */
73 if (CRYPTO_secure_used() != 0) {
77 if (!CRYPTO_secure_malloc_done()) {
81 if (CRYPTO_secure_malloc_initialized()) {
85 /* this can complete - it was not really secure */
86 OPENSSL_secure_free(r);
89 if (CRYPTO_secure_malloc_init(4096, 32)) {