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> |
bnc | 463f235 | 2019-10-10 04:49:34 -0700 | [diff] [blame] | 9 | #include <utility> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 10 | |
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" |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 12 | #include "net/third_party/quiche/src/common/platform/api/quiche_string_piece.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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: |
| 22 | void set_ok(bool 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. |
QUICHE team | fe1aca6 | 2019-03-14 13:39:01 -0700 | [diff] [blame] | 31 | class TestCallback : public AsynchronousKeyExchange::Callback { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 32 | public: |
| 33 | TestCallback(TestCallbackResult* result) : result_(result) {} |
| 34 | virtual ~TestCallback() = default; |
| 35 | |
| 36 | void Run(bool ok) { result_->set_ok(ok); } |
| 37 | |
| 38 | private: |
| 39 | TestCallbackResult* result_; |
| 40 | }; |
| 41 | }; |
| 42 | |
QUICHE team | fe1aca6 | 2019-03-14 13:39:01 -0700 | [diff] [blame] | 43 | // SharedKeyAsync just tests that the basic asynchronous key exchange identity |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 44 | // holds: that both parties end up with the same key. |
| 45 | TEST_F(P256KeyExchangeTest, SharedKey) { |
| 46 | for (int i = 0; i < 5; i++) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 47 | std::string alice_private(P256KeyExchange::NewPrivateKey()); |
| 48 | std::string bob_private(P256KeyExchange::NewPrivateKey()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 | |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 60 | const quiche::QuicheStringPiece alice_public(alice->public_value()); |
| 61 | const quiche::QuicheStringPiece bob_public(bob->public_value()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 62 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 63 | std::string alice_shared, bob_shared; |
QUICHE team | fe1aca6 | 2019-03-14 13:39:01 -0700 | [diff] [blame] | 64 | ASSERT_TRUE(alice->CalculateSharedKeySync(bob_public, &alice_shared)); |
| 65 | ASSERT_TRUE(bob->CalculateSharedKeySync(alice_public, &bob_shared)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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. |
| 72 | TEST_F(P256KeyExchangeTest, AsyncSharedKey) { |
| 73 | for (int i = 0; i < 5; i++) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 74 | std::string alice_private(P256KeyExchange::NewPrivateKey()); |
| 75 | std::string bob_private(P256KeyExchange::NewPrivateKey()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 | |
dmcardle | 904ef18 | 2019-12-13 08:34:33 -0800 | [diff] [blame^] | 87 | const quiche::QuicheStringPiece alice_public(alice->public_value()); |
| 88 | const quiche::QuicheStringPiece bob_public(bob->public_value()); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 89 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 90 | std::string alice_shared, bob_shared; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 91 | TestCallbackResult alice_result; |
| 92 | ASSERT_FALSE(alice_result.ok()); |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 93 | alice->CalculateSharedKeyAsync( |
| 94 | bob_public, &alice_shared, |
| 95 | std::make_unique<TestCallback>(&alice_result)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 96 | ASSERT_TRUE(alice_result.ok()); |
| 97 | TestCallbackResult bob_result; |
| 98 | ASSERT_FALSE(bob_result.ok()); |
QUICHE team | fe1aca6 | 2019-03-14 13:39:01 -0700 | [diff] [blame] | 99 | bob->CalculateSharedKeyAsync(alice_public, &bob_shared, |
vasilvv | 0fc587f | 2019-09-06 13:33:08 -0700 | [diff] [blame] | 100 | std::make_unique<TestCallback>(&bob_result)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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 |