proxygen
KeyDerivationTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #include <gtest/gtest.h>
10 
12 #include <folly/String.h>
13 #include <folly/io/IOBuf.h>
14 
15 using namespace folly;
16 
17 namespace fizz {
18 namespace testing {
19 
20 struct KdfParams {
25 };
26 
27 class KeyDerivationTest : public ::testing::TestWithParam<KdfParams> {};
28 
29 TEST_P(KeyDerivationTest, ExpandLabel) {
30  auto prk = unhexlify(GetParam().secret);
31  auto hashValue = unhexlify(GetParam().hashValue);
32 
33  auto hash = IOBuf::wrapBuffer(range(hashValue));
34 
35  auto secret = std::vector<uint8_t>(prk.begin(), prk.end());
36 
38  auto out = deriver.expandLabel(
39  range(secret),
40  GetParam().label,
41  hash->clone(),
42  GetParam().result.size() / 2);
43  std::string hexOut = hexlify(out->coalesce());
44  EXPECT_EQ(GetParam().result, hexOut);
45 }
46 
47 TEST(KeyDerivation, DeriveSecret) {
48  // dummy prk
49  std::vector<uint8_t> secret(
51  std::vector<uint8_t> messageHash(
54  deriver.deriveSecret(range(secret), "hey", range(messageHash));
55 }
56 
57 TEST(KeyDerivation, Sha256BlankHash) {
58  std::vector<uint8_t> computed(
60  folly::IOBuf blankBuf;
61  Sha256::hash(blankBuf, MutableByteRange(computed.data(), computed.size()));
62  EXPECT_EQ(
65  StringPiece(folly::range(computed)));
66 }
67 
68 // These are taken by dumping mint's internal state
72  ::testing::Values(
73  KdfParams{
74  "09cf566b0a8cb6910fd56947fd030027f8195fc6c5b18ded76d758b436f5db2b",
75  "handshake key expansion, client write iv",
76  "b89ff73312c94b89fadc3320689fd2316a9fd04116b6742e5920a35aaed072a5",
77  "0788c228aca5f4b6873b7b57"},
78  KdfParams{
79  "6174b457f0282fcad096afd50eb92d172638b6fa9baaf8e23ef6b23fe551df73",
80  "server finished",
81  "",
82  "e1ad4d2331428327317047fbf73f3545a8c46fc17b1db445586e9e9b4249c00d"},
83  KdfParams{
84  "09cf566b0a8cb6910fd56947fd030027f8195fc6c5b18ded76d758b436f5db2b",
85  "handshake key expansion, client write key",
86  "b89ff73312c94b89fadc3320689fd2316a9fd04116b6742e5920a35aaed072a5",
87  "93f640530d77b1d386e1d1089ec86382"}));
88 } // namespace testing
89 } // namespace fizz
bool unhexlify(const InputString &input, OutputString &output)
Definition: String-inl.h:616
std::string str() const
Definition: Range.h:591
static std::unique_ptr< IOBuf > wrapBuffer(const void *buf, std::size_t capacity)
Definition: IOBuf.cpp:353
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr size_type size() const
Definition: Range.h:431
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
Range< unsigned char * > MutableByteRange
Definition: Range.h:1164
TEST_P(KeyDerivationTest, ExpandLabel)
constexpr Range< Iter > range(Iter first, Iter last)
Definition: Range.h:1114
Definition: Actions.h:16
INSTANTIATE_TEST_CASE_P(KeyDerivation, KeyDerivationTest,::testing::Values(KdfParams{"09cf566b0a8cb6910fd56947fd030027f8195fc6c5b18ded76d758b436f5db2b","handshake key expansion, client write iv","b89ff73312c94b89fadc3320689fd2316a9fd04116b6742e5920a35aaed072a5","0788c228aca5f4b6873b7b57"}, KdfParams{"6174b457f0282fcad096afd50eb92d172638b6fa9baaf8e23ef6b23fe551df73","server finished","","e1ad4d2331428327317047fbf73f3545a8c46fc17b1db445586e9e9b4249c00d"}, KdfParams{"09cf566b0a8cb6910fd56947fd030027f8195fc6c5b18ded76d758b436f5db2b","handshake key expansion, client write key","b89ff73312c94b89fadc3320689fd2316a9fd04116b6742e5920a35aaed072a5","93f640530d77b1d386e1d1089ec86382"}))
const char * string
Definition: Conv.cpp:212
static constexpr StringPiece secret
Range< const char * > StringPiece
bool hexlify(const InputString &input, OutputString &output, bool append_output)
Definition: String-inl.h:596
constexpr folly::StringPiece kHkdfLabelPrefix
Definition: Types.h:20
TEST(SequencedExecutor, CPUThreadPoolExecutor)
StringPiece label