xref: /aosp_15_r20/external/tink/cc/hybrid/internal/hpke_test_util.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16 
17 #ifndef TINK_HYBRID_INTERNAL_HPKE_TEST_UTIL_H_
18 #define TINK_HYBRID_INTERNAL_HPKE_TEST_UTIL_H_
19 
20 #include <string>
21 #include <vector>
22 
23 #include "absl/strings/escaping.h"
24 #include "tink/hybrid/internal/hpke_util.h"
25 #include "tink/util/statusor.h"
26 #include "proto/hpke.pb.h"
27 
28 namespace crypto {
29 namespace tink {
30 namespace internal {
31 
32 struct HpkeTestParams {
33   std::string recipient_public_key;   // pkRm
34   std::string seed_for_testing;       // skEm
35   std::string application_info;       // info
36   std::string plaintext;              // pt
37   std::string associated_data;        // aad
38   std::string ciphertext;             // ct
39   std::string recipient_private_key;  // skRm
40   std::string encapsulated_key;       // enc
41   std::vector<std::string> exported_contexts;
42   std::vector<std::string> exported_values;
43 
HpkeTestParamsHpkeTestParams44   explicit HpkeTestParams(const absl::string_view* test_vector)
45       : recipient_public_key(absl::HexStringToBytes(test_vector[0])),
46         seed_for_testing(absl::HexStringToBytes(test_vector[1])),
47         application_info(absl::HexStringToBytes(test_vector[2])),
48         plaintext(absl::HexStringToBytes(test_vector[3])),
49         associated_data(absl::HexStringToBytes(test_vector[4])),
50         ciphertext(absl::HexStringToBytes(test_vector[5])),
51         recipient_private_key(absl::HexStringToBytes(test_vector[6])),
52         encapsulated_key(absl::HexStringToBytes(test_vector[7])),
53         exported_contexts({absl::HexStringToBytes(test_vector[8]),
54                            absl::HexStringToBytes(test_vector[9]),
55                            absl::HexStringToBytes(test_vector[10])}),
56         exported_values({absl::HexStringToBytes(test_vector[11]),
57                          absl::HexStringToBytes(test_vector[12]),
58                          absl::HexStringToBytes(test_vector[13])}) {}
59 };
60 
61 // Returns an HpkeTestParams struct for the following HPKE parameters:
62 // DHKEM(X25519, HKDF-SHA256), HKDF-SHA256, AES-128-GCM.
63 HpkeTestParams DefaultHpkeTestParams();
64 
65 // Creates an HpkeTestParams struct for the specified HpkeParams protobuf.
66 util::StatusOr<HpkeTestParams> CreateHpkeTestParams(
67     const google::crypto::tink::HpkeParams& params);
68 
69 // Creates an HpkeTestParams struct for the specified HpkeParams struct.
70 util::StatusOr<HpkeTestParams> CreateHpkeTestParams(
71     const HpkeParams& params);
72 
73 // Creates an HpkeParams protobuf from `kem`, `kdf`, and `aead`.
74 google::crypto::tink::HpkeParams CreateHpkeParams(
75     const google::crypto::tink::HpkeKem& kem,
76     const google::crypto::tink::HpkeKdf& kdf,
77     const google::crypto::tink::HpkeAead& aead);
78 
79 // Creates an HpkePublicKey proto from the specified HpkeParams protobuf and
80 // the `raw_key_bytes`.
81 google::crypto::tink::HpkePublicKey CreateHpkePublicKey(
82     const google::crypto::tink::HpkeParams& params,
83     const std::string& raw_key_bytes);
84 
85 // Creates an HpkePrivateKey proto from the specified HpkeParams protobuf and
86 // the `raw_key_bytes`.  Note that the key material for the embedded
87 // HpkePublicKey `public_key` field will be empty.
88 google::crypto::tink::HpkePrivateKey CreateHpkePrivateKey(
89     const google::crypto::tink::HpkeParams& params,
90     const std::string& raw_key_bytes);
91 
92 }  // namespace internal
93 }  // namespace tink
94 }  // namespace crypto
95 
96 #endif  // TINK_HYBRID_INTERNAL_HPKE_TEST_UTIL_H_
97