1 /* Copyright (c) 2018, 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 <openssl/base.h>
16 
17 #if !defined(NDEBUG) && !defined(BORINGSSL_FIPS) && \
18     !defined(BORINGSSL_SHARED_LIBRARY)
19 
20 #include <functional>
21 #include <utility>
22 #include <vector>
23 
24 #include <openssl/aead.h>
25 #include <openssl/aes.h>
26 #include <openssl/cpu.h>
27 #include <openssl/mem.h>
28 
29 #include <gtest/gtest.h>
30 
31 #include "internal.h"
32 
33 
34 class ImplDispatchTest : public ::testing::Test {
35  public:
SetUp()36   void SetUp() override {
37 #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
38     aesni_ = OPENSSL_ia32cap_P[1] & (1 << (57 - 32));
39     avx_movbe_ = ((OPENSSL_ia32cap_P[1] >> 22) & 0x41) == 0x41;
40     ssse3_ = OPENSSL_ia32cap_P[1] & (1 << (41 - 32));
41     is_x86_64_ =
42 #if defined(OPENSSL_X86_64)
43         true;
44 #else
45         false;
46 #endif
47 #endif  // X86 || X86_64
48   }
49 
50  protected:
51   // AssertFunctionsHit takes a list of pairs (flag index, boolean), and a
52   // function to test. It runs the given function and asserts, for each flag
53   // index, that the boolean reflects whether that flag index was written or
54   // not, and that no other flagged functions were triggered.
AssertFunctionsHit(std::vector<std::pair<size_t,bool>> flags,std::function<void ()> f)55   void AssertFunctionsHit(std::vector<std::pair<size_t, bool>> flags,
56                           std::function<void()> f) {
57     OPENSSL_memset(BORINGSSL_function_hit, 0, sizeof(BORINGSSL_function_hit));
58 
59     f();
60 
61     for (const auto flag : flags) {
62       SCOPED_TRACE(flag.first);
63 
64       ASSERT_LT(flag.first, sizeof(BORINGSSL_function_hit));
65       EXPECT_EQ(flag.second, BORINGSSL_function_hit[flag.first] == 1);
66       BORINGSSL_function_hit[flag.first] = 0;
67     }
68 
69     for (size_t i = 0; i < sizeof(BORINGSSL_function_hit); i++) {
70       EXPECT_EQ(0u, BORINGSSL_function_hit[i])
71           << "Flag " << i << " unexpectedly hit";
72     }
73   }
74 
75 #if defined(OPENSSL_X86) || defined(OPENSSL_X86_64)
76   bool aesni_ = false;
77   bool avx_movbe_ = false;
78   bool ssse3_ = false;
79   bool is_x86_64_ = false;
80 #endif
81 };
82 
83 #if !defined(OPENSSL_NO_ASM) && \
84     (defined(OPENSSL_X86) || defined(OPENSSL_X86_64))
85 
86 constexpr size_t kFlag_aes_hw_ctr32_encrypt_blocks = 0;
87 constexpr size_t kFlag_aes_hw_encrypt = 1;
88 constexpr size_t kFlag_aesni_gcm_encrypt = 2;
89 constexpr size_t kFlag_aes_hw_set_encrypt_key = 3;
90 constexpr size_t kFlag_vpaes_encrypt = 4;
91 constexpr size_t kFlag_vpaes_set_encrypt_key = 5;
92 
TEST_F(ImplDispatchTest,AEAD_AES_GCM)93 TEST_F(ImplDispatchTest, AEAD_AES_GCM) {
94   AssertFunctionsHit(
95       {
96           {kFlag_aes_hw_ctr32_encrypt_blocks, aesni_},
97           {kFlag_aes_hw_encrypt, aesni_},
98           {kFlag_aes_hw_set_encrypt_key, aesni_},
99           {kFlag_aesni_gcm_encrypt, is_x86_64_ && aesni_ && avx_movbe_},
100           {kFlag_vpaes_encrypt, ssse3_ && !aesni_},
101           {kFlag_vpaes_set_encrypt_key, ssse3_ && !aesni_},
102       },
103       [] {
104         const uint8_t kZeros[16] = {0};
105         const uint8_t kPlaintext[40] = {1, 2, 3, 4, 0};
106         uint8_t ciphertext[sizeof(kPlaintext) + 16];
107         size_t ciphertext_len;
108         EVP_AEAD_CTX ctx;
109         ASSERT_TRUE(EVP_AEAD_CTX_init(&ctx, EVP_aead_aes_128_gcm(), kZeros,
110                                       sizeof(kZeros),
111                                       EVP_AEAD_DEFAULT_TAG_LENGTH, nullptr));
112         ASSERT_TRUE(EVP_AEAD_CTX_seal(
113             &ctx, ciphertext, &ciphertext_len, sizeof(ciphertext), kZeros,
114             EVP_AEAD_nonce_length(EVP_aead_aes_128_gcm()), kPlaintext,
115             sizeof(kPlaintext), nullptr, 0));
116       });
117 }
118 
TEST_F(ImplDispatchTest,AES_set_encrypt_key)119 TEST_F(ImplDispatchTest, AES_set_encrypt_key) {
120   AssertFunctionsHit(
121       {
122           {kFlag_aes_hw_set_encrypt_key, aesni_},
123           {kFlag_vpaes_set_encrypt_key, ssse3_ && !aesni_},
124       },
125       [] {
126         AES_KEY key;
127         static const uint8_t kZeros[16] = {0};
128         AES_set_encrypt_key(kZeros, sizeof(kZeros) * 8, &key);
129       });
130 }
131 
TEST_F(ImplDispatchTest,AES_single_block)132 TEST_F(ImplDispatchTest, AES_single_block) {
133   AES_KEY key;
134   static const uint8_t kZeros[16] = {0};
135   AES_set_encrypt_key(kZeros, sizeof(kZeros) * 8, &key);
136 
137   AssertFunctionsHit(
138       {
139           {kFlag_aes_hw_encrypt, aesni_},
140           {kFlag_vpaes_encrypt, ssse3_ && !aesni_},
141       },
142       [&key] {
143         uint8_t in[AES_BLOCK_SIZE] = {0};
144         uint8_t out[AES_BLOCK_SIZE];
145         AES_encrypt(in, out, &key);
146       });
147 }
148 
149 #endif  // X86 || X86_64
150 
151 #endif  // !NDEBUG && !FIPS && !SHARED_LIBRARY
152