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