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/crypto_secret_boxer.h" |
| 6 | |
| 7 | #include "net/third_party/quiche/src/quic/core/crypto/quic_random.h" |
| 8 | #include "net/third_party/quiche/src/quic/platform/api/quic_string.h" |
| 9 | #include "net/third_party/quiche/src/quic/platform/api/quic_test.h" |
| 10 | |
| 11 | namespace quic { |
| 12 | namespace test { |
| 13 | |
| 14 | class CryptoSecretBoxerTest : public QuicTest {}; |
| 15 | |
| 16 | TEST_F(CryptoSecretBoxerTest, BoxAndUnbox) { |
| 17 | QuicStringPiece message("hello world"); |
| 18 | |
| 19 | CryptoSecretBoxer boxer; |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 20 | boxer.SetKeys({std::string(CryptoSecretBoxer::GetKeySize(), 0x11)}); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 21 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 22 | const std::string box = boxer.Box(QuicRandom::GetInstance(), message); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 23 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 24 | std::string storage; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 25 | QuicStringPiece result; |
| 26 | EXPECT_TRUE(boxer.Unbox(box, &storage, &result)); |
| 27 | EXPECT_EQ(result, message); |
| 28 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 29 | EXPECT_FALSE(boxer.Unbox(std::string(1, 'X') + box, &storage, &result)); |
| 30 | EXPECT_FALSE( |
| 31 | boxer.Unbox(box.substr(1, std::string::npos), &storage, &result)); |
| 32 | EXPECT_FALSE(boxer.Unbox(std::string(), &storage, &result)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 33 | EXPECT_FALSE(boxer.Unbox( |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 34 | std::string(1, box[0] ^ 0x80) + box.substr(1, std::string::npos), |
| 35 | &storage, &result)); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | // Helper function to test whether one boxer can decode the output of another. |
| 39 | static bool CanDecode(const CryptoSecretBoxer& decoder, |
| 40 | const CryptoSecretBoxer& encoder) { |
| 41 | QuicStringPiece message("hello world"); |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 42 | const std::string boxed = encoder.Box(QuicRandom::GetInstance(), message); |
| 43 | std::string storage; |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 44 | QuicStringPiece result; |
| 45 | bool ok = decoder.Unbox(boxed, &storage, &result); |
| 46 | if (ok) { |
| 47 | EXPECT_EQ(result, message); |
| 48 | } |
| 49 | return ok; |
| 50 | } |
| 51 | |
| 52 | TEST_F(CryptoSecretBoxerTest, MultipleKeys) { |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 53 | std::string key_11(CryptoSecretBoxer::GetKeySize(), 0x11); |
| 54 | std::string key_12(CryptoSecretBoxer::GetKeySize(), 0x12); |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 55 | |
| 56 | CryptoSecretBoxer boxer_11, boxer_12, boxer; |
| 57 | boxer_11.SetKeys({key_11}); |
| 58 | boxer_12.SetKeys({key_12}); |
| 59 | boxer.SetKeys({key_12, key_11}); |
| 60 | |
| 61 | // Neither single-key boxer can decode the other's tokens. |
| 62 | EXPECT_FALSE(CanDecode(boxer_11, boxer_12)); |
| 63 | EXPECT_FALSE(CanDecode(boxer_12, boxer_11)); |
| 64 | |
| 65 | // |boxer| encodes with the first key, which is key_12. |
| 66 | EXPECT_TRUE(CanDecode(boxer_12, boxer)); |
| 67 | EXPECT_FALSE(CanDecode(boxer_11, boxer)); |
| 68 | |
| 69 | // The boxer with both keys can decode tokens from either single-key boxer. |
| 70 | EXPECT_TRUE(CanDecode(boxer, boxer_11)); |
| 71 | EXPECT_TRUE(CanDecode(boxer, boxer_12)); |
| 72 | |
| 73 | // After we flush key_11 from |boxer|, it can no longer decode tokens from |
| 74 | // |boxer_11|. |
| 75 | boxer.SetKeys({key_12}); |
| 76 | EXPECT_FALSE(CanDecode(boxer, boxer_11)); |
| 77 | } |
| 78 | |
| 79 | } // namespace test |
| 80 | } // namespace quic |