xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/crypto/p256_key_exchange_test.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "quiche/quic/core/crypto/p256_key_exchange.h"
6 
7 #include <memory>
8 #include <string>
9 #include <utility>
10 
11 #include "absl/strings/string_view.h"
12 #include "quiche/quic/platform/api/quic_test.h"
13 
14 namespace quic {
15 namespace test {
16 
17 class P256KeyExchangeTest : public QuicTest {
18  public:
19   // Holds the result of a key exchange callback.
20   class TestCallbackResult {
21    public:
set_ok(bool ok)22     void set_ok(bool ok) { ok_ = ok; }
ok()23     bool ok() { return ok_; }
24 
25    private:
26     bool ok_ = false;
27   };
28 
29   // Key exchange callback which sets the result into the specified
30   // TestCallbackResult.
31   class TestCallback : public AsynchronousKeyExchange::Callback {
32    public:
TestCallback(TestCallbackResult * result)33     TestCallback(TestCallbackResult* result) : result_(result) {}
34     virtual ~TestCallback() = default;
35 
Run(bool ok)36     void Run(bool ok) { result_->set_ok(ok); }
37 
38    private:
39     TestCallbackResult* result_;
40   };
41 };
42 
43 // SharedKeyAsync just tests that the basic asynchronous key exchange identity
44 // holds: that both parties end up with the same key.
TEST_F(P256KeyExchangeTest,SharedKey)45 TEST_F(P256KeyExchangeTest, SharedKey) {
46   for (int i = 0; i < 5; i++) {
47     std::string alice_private(P256KeyExchange::NewPrivateKey());
48     std::string bob_private(P256KeyExchange::NewPrivateKey());
49 
50     ASSERT_FALSE(alice_private.empty());
51     ASSERT_FALSE(bob_private.empty());
52     ASSERT_NE(alice_private, bob_private);
53 
54     std::unique_ptr<P256KeyExchange> alice(P256KeyExchange::New(alice_private));
55     std::unique_ptr<P256KeyExchange> bob(P256KeyExchange::New(bob_private));
56 
57     ASSERT_TRUE(alice != nullptr);
58     ASSERT_TRUE(bob != nullptr);
59 
60     const absl::string_view alice_public(alice->public_value());
61     const absl::string_view bob_public(bob->public_value());
62 
63     std::string alice_shared, bob_shared;
64     ASSERT_TRUE(alice->CalculateSharedKeySync(bob_public, &alice_shared));
65     ASSERT_TRUE(bob->CalculateSharedKeySync(alice_public, &bob_shared));
66     ASSERT_EQ(alice_shared, bob_shared);
67   }
68 }
69 
70 // SharedKey just tests that the basic key exchange identity holds: that both
71 // parties end up with the same key.
TEST_F(P256KeyExchangeTest,AsyncSharedKey)72 TEST_F(P256KeyExchangeTest, AsyncSharedKey) {
73   for (int i = 0; i < 5; i++) {
74     std::string alice_private(P256KeyExchange::NewPrivateKey());
75     std::string bob_private(P256KeyExchange::NewPrivateKey());
76 
77     ASSERT_FALSE(alice_private.empty());
78     ASSERT_FALSE(bob_private.empty());
79     ASSERT_NE(alice_private, bob_private);
80 
81     std::unique_ptr<P256KeyExchange> alice(P256KeyExchange::New(alice_private));
82     std::unique_ptr<P256KeyExchange> bob(P256KeyExchange::New(bob_private));
83 
84     ASSERT_TRUE(alice != nullptr);
85     ASSERT_TRUE(bob != nullptr);
86 
87     const absl::string_view alice_public(alice->public_value());
88     const absl::string_view bob_public(bob->public_value());
89 
90     std::string alice_shared, bob_shared;
91     TestCallbackResult alice_result;
92     ASSERT_FALSE(alice_result.ok());
93     alice->CalculateSharedKeyAsync(
94         bob_public, &alice_shared,
95         std::make_unique<TestCallback>(&alice_result));
96     ASSERT_TRUE(alice_result.ok());
97     TestCallbackResult bob_result;
98     ASSERT_FALSE(bob_result.ok());
99     bob->CalculateSharedKeyAsync(alice_public, &bob_shared,
100                                  std::make_unique<TestCallback>(&bob_result));
101     ASSERT_TRUE(bob_result.ok());
102     ASSERT_EQ(alice_shared, bob_shared);
103     ASSERT_NE(0u, alice_shared.length());
104     ASSERT_NE(0u, bob_shared.length());
105   }
106 }
107 
108 }  // namespace test
109 }  // namespace quic
110