blob: 87399777319ba926d8ce7e063f90029f0853d1a1 [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/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
11namespace quic {
12namespace test {
13
14class CryptoSecretBoxerTest : public QuicTest {};
15
16TEST_F(CryptoSecretBoxerTest, BoxAndUnbox) {
17 QuicStringPiece message("hello world");
18
19 CryptoSecretBoxer boxer;
vasilvvc48c8712019-03-11 13:38:16 -070020 boxer.SetKeys({std::string(CryptoSecretBoxer::GetKeySize(), 0x11)});
QUICHE teama6ef0a62019-03-07 20:34:33 -050021
vasilvvc48c8712019-03-11 13:38:16 -070022 const std::string box = boxer.Box(QuicRandom::GetInstance(), message);
QUICHE teama6ef0a62019-03-07 20:34:33 -050023
vasilvvc48c8712019-03-11 13:38:16 -070024 std::string storage;
QUICHE teama6ef0a62019-03-07 20:34:33 -050025 QuicStringPiece result;
26 EXPECT_TRUE(boxer.Unbox(box, &storage, &result));
27 EXPECT_EQ(result, message);
28
vasilvvc48c8712019-03-11 13:38:16 -070029 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 teama6ef0a62019-03-07 20:34:33 -050033 EXPECT_FALSE(boxer.Unbox(
vasilvvc48c8712019-03-11 13:38:16 -070034 std::string(1, box[0] ^ 0x80) + box.substr(1, std::string::npos),
35 &storage, &result));
QUICHE teama6ef0a62019-03-07 20:34:33 -050036}
37
38// Helper function to test whether one boxer can decode the output of another.
39static bool CanDecode(const CryptoSecretBoxer& decoder,
40 const CryptoSecretBoxer& encoder) {
41 QuicStringPiece message("hello world");
vasilvvc48c8712019-03-11 13:38:16 -070042 const std::string boxed = encoder.Box(QuicRandom::GetInstance(), message);
43 std::string storage;
QUICHE teama6ef0a62019-03-07 20:34:33 -050044 QuicStringPiece result;
45 bool ok = decoder.Unbox(boxed, &storage, &result);
46 if (ok) {
47 EXPECT_EQ(result, message);
48 }
49 return ok;
50}
51
52TEST_F(CryptoSecretBoxerTest, MultipleKeys) {
vasilvvc48c8712019-03-11 13:38:16 -070053 std::string key_11(CryptoSecretBoxer::GetKeySize(), 0x11);
54 std::string key_12(CryptoSecretBoxer::GetKeySize(), 0x12);
QUICHE teama6ef0a62019-03-07 20:34:33 -050055
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