]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/secmemtest.c
Add support for parameterized SipHash
[thirdparty/openssl.git] / test / secmemtest.c
CommitLineData
440e5d80
RS
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 */
74924dcb
RS
9
10#include <openssl/crypto.h>
11
e8408681
TS
12#define perror_line() perror_line1(__LINE__)
13#define perror_line1(l) perror_line2(l)
14#define perror_line2(l) perror("failed " #l)
15
74924dcb
RS
16int main(int argc, char **argv)
17{
18#if defined(OPENSSL_SYS_LINUX) || defined(OPENSSL_SYS_UNIX)
e8408681 19 char *p = NULL, *q = NULL, *r = NULL, *s = NULL;
74924dcb 20
e8408681
TS
21 r = OPENSSL_secure_malloc(20);
22 /* r = non-secure 20 */
23 if (r == NULL) {
24 perror_line();
25 return 1;
26 }
74924dcb 27 if (!CRYPTO_secure_malloc_init(4096, 32)) {
e8408681
TS
28 perror_line();
29 return 1;
30 }
31 if (CRYPTO_secure_allocated(r)) {
32 perror_line();
74924dcb
RS
33 return 1;
34 }
35 p = OPENSSL_secure_malloc(20);
e8408681 36 /* r = non-secure 20, p = secure 20 */
74924dcb 37 if (!CRYPTO_secure_allocated(p)) {
e8408681
TS
38 perror_line();
39 return 1;
40 }
41 /* 20 secure -> 32-byte minimum allocaton unit */
42 if (CRYPTO_secure_used() != 32) {
43 perror_line();
74924dcb
RS
44 return 1;
45 }
46 q = OPENSSL_malloc(20);
e8408681 47 /* r = non-secure 20, p = secure 20, q = non-secure 20 */
74924dcb 48 if (CRYPTO_secure_allocated(q)) {
e8408681
TS
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();
74924dcb
RS
61 return 1;
62 }
d6b55fac 63 OPENSSL_secure_free(p);
e8408681
TS
64 /* 20 secure -> 32 bytes allocated */
65 if (CRYPTO_secure_used() != 32) {
66 perror_line();
67 return 1;
68 }
d6b55fac 69 OPENSSL_free(q);
e8408681
TS
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);
74924dcb
RS
95#else
96 /* Should fail. */
97 if (CRYPTO_secure_malloc_init(4096, 32)) {
e8408681 98 perror_line();
74924dcb
RS
99 return 1;
100 }
101#endif
102 return 0;
103}