1 /* Copyright (c) 2020, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #include <stdio.h>
16 #include <string.h>
17
18 #include <string>
19
20 #include <openssl/base.h>
21 #include <openssl/crypto.h>
22 #include <openssl/cipher.h>
23 #include <openssl/mem.h>
24
25 #include <gtest/gtest.h>
26
27 // Test that OPENSSL_VERSION_NUMBER and OPENSSL_VERSION_TEXT are consistent.
28 // Node.js parses the version out of OPENSSL_VERSION_TEXT instead of using
29 // OPENSSL_VERSION_NUMBER.
TEST(CryptoTest,Version)30 TEST(CryptoTest, Version) {
31 char expected[512];
32 snprintf(expected, sizeof(expected), "OpenSSL %d.%d.%d ",
33 OPENSSL_VERSION_NUMBER >> 28, (OPENSSL_VERSION_NUMBER >> 20) & 0xff,
34 (OPENSSL_VERSION_NUMBER >> 12) & 0xff);
35 EXPECT_EQ(expected,
36 std::string(OPENSSL_VERSION_TEXT).substr(0, strlen(expected)));
37 }
38
TEST(CryptoTest,Strndup)39 TEST(CryptoTest, Strndup) {
40 bssl::UniquePtr<char> str(OPENSSL_strndup(nullptr, 0));
41 EXPECT_TRUE(str);
42 EXPECT_STREQ("", str.get());
43 }
44
45 #if defined(BORINGSSL_FIPS_COUNTERS)
TEST(CryptoTest,FIPSCountersEVP)46 TEST(CryptoTest, FIPSCountersEVP) {
47 constexpr struct {
48 const EVP_CIPHER *(*cipher)();
49 fips_counter_t counter;
50 } kTests[] = {
51 {
52 EVP_aes_128_gcm,
53 fips_counter_evp_aes_128_gcm,
54 },
55 {
56 EVP_aes_256_gcm,
57 fips_counter_evp_aes_256_gcm,
58 },
59 {
60 EVP_aes_128_ctr,
61 fips_counter_evp_aes_128_ctr,
62 },
63 {
64 EVP_aes_256_ctr,
65 fips_counter_evp_aes_256_ctr,
66 },
67 };
68
69 uint8_t key[EVP_MAX_KEY_LENGTH] = {0};
70 uint8_t iv[EVP_MAX_IV_LENGTH] = {1};
71
72 for (const auto& test : kTests) {
73 const size_t before = FIPS_read_counter(test.counter);
74
75 bssl::ScopedEVP_CIPHER_CTX ctx;
76 ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), test.cipher(), /*engine=*/nullptr,
77 key, iv));
78 ASSERT_GT(FIPS_read_counter(test.counter), before);
79 }
80 }
81 #endif // BORINGSSL_FIPS_COUNTERS
82