Debian Codegraph @

crt/s2n/tests/unit/s2n_libcrypto_test.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "crypto/s2n_libcrypto.h"
17
18 #include "s2n_test.h"
19 #include "utils/s2n_random.h"
20
21 int main(int argc, char** argv)
22 {
23 BEGIN_TEST();
24
25 const char* env_libcrypto = getenv("S2N_LIBCRYPTO");
26 if (env_libcrypto == NULL) {
27 END_TEST();
28 }
29
30 /* Confirm "S2N_LIBCRYPTO" env variable matches the linked libcrypto. */
31 if (strstr(env_libcrypto, "awslc") != NULL) {
32 EXPECT_TRUE(s2n_libcrypto_is_awslc());
33 EXPECT_FALSE(s2n_libcrypto_is_boringssl());
34 EXPECT_FALSE(s2n_libcrypto_is_libressl());
35 EXPECT_FALSE(s2n_libcrypto_is_openssl());
36 } else if (strcmp(env_libcrypto, "boringssl") == 0) {
37 EXPECT_FALSE(s2n_libcrypto_is_awslc());
38 EXPECT_TRUE(s2n_libcrypto_is_boringssl());
39 EXPECT_FALSE(s2n_libcrypto_is_libressl());
40 EXPECT_FALSE(s2n_libcrypto_is_openssl());
41 } else if (strcmp(env_libcrypto, "libressl") == 0) {
42 EXPECT_FALSE(s2n_libcrypto_is_awslc());
43 EXPECT_FALSE(s2n_libcrypto_is_boringssl());
44 EXPECT_TRUE(s2n_libcrypto_is_libressl());
45 EXPECT_FALSE(s2n_libcrypto_is_openssl());
46 } else if (strstr(env_libcrypto, "openssl") != NULL) {
47 EXPECT_FALSE(s2n_libcrypto_is_awslc());
48 EXPECT_FALSE(s2n_libcrypto_is_boringssl());
49 EXPECT_FALSE(s2n_libcrypto_is_libressl());
50 EXPECT_TRUE(s2n_libcrypto_is_openssl());
51 } else if (strcmp(env_libcrypto, "default") == 0) {
52 /* running with the default libcrypto on path */
53 } else {
54 FAIL_MSG("Testing with an unexpected libcrypto.");
55 }
56
57 /* We expect openssl-3.0 to support providers */
58 if (strstr(env_libcrypto, "openssl") && strstr(env_libcrypto, "3")) {
59 EXPECT_TRUE(s2n_libcrypto_supports_providers());
60 }
61
62 /* Verify randomness delegation assumptions.
63 *
64 * s2n delegates to libcrypto for AWS-LC or when it supports RAND_priv_bytes/
65 * RAND_public_bytes, and falls back to /dev/urandom otherwise.
66 */
67 {
68 /* These libcryptos support libcrypto randomness delegation */
69 if (strstr(env_libcrypto, "awslc") != NULL
70 || (strstr(env_libcrypto, "openssl") && strstr(env_libcrypto, "1.1.1"))
71 || (strstr(env_libcrypto, "openssl") && strstr(env_libcrypto, "3"))) {
72 EXPECT_TRUE(s2n_use_libcrypto_rand());
73 }
74
75 /* These libcryptos lack RAND_priv_bytes and RAND_public_bytes,
76 * so s2n falls back to system random (/dev/urandom).
77 */
78 if ((strstr(env_libcrypto, "openssl") && strstr(env_libcrypto, "1.0.2"))
79 || strcmp(env_libcrypto, "boringssl") == 0
80 || strcmp(env_libcrypto, "libressl") == 0) {
81 EXPECT_FALSE(s2n_use_libcrypto_rand());
82 }
83 };
84
85 END_TEST();
86 }