blob: 11d7bd8f1a9c14060fa7fff0a86589785573972f [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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 "net/third_party/quiche/src/quic/core/crypto/p256_key_exchange.h"
6
7#include <memory>
vasilvv872e7a32019-03-12 16:42:44 -07008#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -05009
10#include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050011#include "net/third_party/quiche/src/quic/platform/api/quic_test.h"
12
13namespace quic {
14namespace test {
15
16class P256KeyExchangeTest : public QuicTest {
17 public:
18 // Holds the result of a key exchange callback.
19 class TestCallbackResult {
20 public:
21 void set_ok(bool ok) { ok_ = ok; }
22 bool ok() { return ok_; }
23
24 private:
25 bool ok_ = false;
26 };
27
28 // Key exchange callback which sets the result into the specified
29 // TestCallbackResult.
30 class TestCallback : public KeyExchange::Callback {
31 public:
32 TestCallback(TestCallbackResult* result) : result_(result) {}
33 virtual ~TestCallback() = default;
34
35 void Run(bool ok) { result_->set_ok(ok); }
36
37 private:
38 TestCallbackResult* result_;
39 };
40};
41
42// SharedKeyAsync just tests that the basic asynchronouse key exchange identity
43// holds: that both parties end up with the same key.
44TEST_F(P256KeyExchangeTest, SharedKey) {
45 for (int i = 0; i < 5; i++) {
vasilvvc48c8712019-03-11 13:38:16 -070046 std::string alice_private(P256KeyExchange::NewPrivateKey());
47 std::string bob_private(P256KeyExchange::NewPrivateKey());
QUICHE teama6ef0a62019-03-07 20:34:33 -050048
49 ASSERT_FALSE(alice_private.empty());
50 ASSERT_FALSE(bob_private.empty());
51 ASSERT_NE(alice_private, bob_private);
52
53 std::unique_ptr<P256KeyExchange> alice(P256KeyExchange::New(alice_private));
54 std::unique_ptr<P256KeyExchange> bob(P256KeyExchange::New(bob_private));
55
56 ASSERT_TRUE(alice != nullptr);
57 ASSERT_TRUE(bob != nullptr);
58
59 const QuicStringPiece alice_public(alice->public_value());
60 const QuicStringPiece bob_public(bob->public_value());
61
vasilvvc48c8712019-03-11 13:38:16 -070062 std::string alice_shared, bob_shared;
QUICHE teama6ef0a62019-03-07 20:34:33 -050063 ASSERT_TRUE(alice->CalculateSharedKey(bob_public, &alice_shared));
64 ASSERT_TRUE(bob->CalculateSharedKey(alice_public, &bob_shared));
65 ASSERT_EQ(alice_shared, bob_shared);
66 }
67}
68
69// SharedKey just tests that the basic key exchange identity holds: that both
70// parties end up with the same key.
71TEST_F(P256KeyExchangeTest, AsyncSharedKey) {
72 for (int i = 0; i < 5; i++) {
vasilvvc48c8712019-03-11 13:38:16 -070073 std::string alice_private(P256KeyExchange::NewPrivateKey());
74 std::string bob_private(P256KeyExchange::NewPrivateKey());
QUICHE teama6ef0a62019-03-07 20:34:33 -050075
76 ASSERT_FALSE(alice_private.empty());
77 ASSERT_FALSE(bob_private.empty());
78 ASSERT_NE(alice_private, bob_private);
79
80 std::unique_ptr<P256KeyExchange> alice(P256KeyExchange::New(alice_private));
81 std::unique_ptr<P256KeyExchange> bob(P256KeyExchange::New(bob_private));
82
83 ASSERT_TRUE(alice != nullptr);
84 ASSERT_TRUE(bob != nullptr);
85
86 const QuicStringPiece alice_public(alice->public_value());
87 const QuicStringPiece bob_public(bob->public_value());
88
vasilvvc48c8712019-03-11 13:38:16 -070089 std::string alice_shared, bob_shared;
QUICHE teama6ef0a62019-03-07 20:34:33 -050090 TestCallbackResult alice_result;
91 ASSERT_FALSE(alice_result.ok());
92 alice->CalculateSharedKey(bob_public, &alice_shared,
93 QuicMakeUnique<TestCallback>(&alice_result));
94 ASSERT_TRUE(alice_result.ok());
95 TestCallbackResult bob_result;
96 ASSERT_FALSE(bob_result.ok());
97 bob->CalculateSharedKey(alice_public, &bob_shared,
98 QuicMakeUnique<TestCallback>(&bob_result));
99 ASSERT_TRUE(bob_result.ok());
100 ASSERT_EQ(alice_shared, bob_shared);
101 ASSERT_NE(0u, alice_shared.length());
102 ASSERT_NE(0u, bob_shared.length());
103 }
104}
105
106} // namespace test
107} // namespace quic