QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 "net/third_party/quiche/src/quic/core/crypto/p256_key_exchange.h" |
| 6 | |
| 7 | #include <memory> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame^] | 8 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 9 | |
| 10 | #include "net/third_party/quiche/src/quic/platform/api/quic_ptr_util.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 12 | |
| 13 | namespace quic { |
| 14 | namespace test { |
| 15 | |
| 16 | class 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. |
| 44 | TEST_F(P256KeyExchangeTest, SharedKey) { |
| 45 | for (int i = 0; i < 5; i++) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 46 | std::string alice_private(P256KeyExchange::NewPrivateKey()); |
| 47 | std::string bob_private(P256KeyExchange::NewPrivateKey()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 48 | |
| 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 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 62 | std::string alice_shared, bob_shared; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 63 | 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. |
| 71 | TEST_F(P256KeyExchangeTest, AsyncSharedKey) { |
| 72 | for (int i = 0; i < 5; i++) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 73 | std::string alice_private(P256KeyExchange::NewPrivateKey()); |
| 74 | std::string bob_private(P256KeyExchange::NewPrivateKey()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 75 | |
| 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 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 89 | std::string alice_shared, bob_shared; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 90 | 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 |