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 #include "tink/hybrid/internal/hpke_private_key_manager.h"
18
19 #include <memory>
20
21 #include "absl/status/status.h"
22 #include "tink/hybrid/internal/hpke_key_manager_util.h"
23 #include "tink/internal/ec_util.h"
24 #include "tink/util/status.h"
25 #include "tink/util/statusor.h"
26 #include "tink/util/validation.h"
27 #include "proto/hpke.pb.h"
28
29 namespace crypto {
30 namespace tink {
31 namespace internal {
32 namespace {
33
34 using ::google::crypto::tink::HpkeKem;
35 using ::google::crypto::tink::HpkeKeyFormat;
36 using ::google::crypto::tink::HpkePrivateKey;
37 using ::google::crypto::tink::HpkePublicKey;
38
GenerateX25519Key(HpkePublicKey & public_key,HpkePrivateKey & private_key)39 util::Status GenerateX25519Key(HpkePublicKey& public_key,
40 HpkePrivateKey& private_key) {
41 util::StatusOr<std::unique_ptr<internal::X25519Key>> key =
42 internal::NewX25519Key();
43 if (!key.ok()) {
44 return key.status();
45 }
46 public_key.set_public_key((*key)->public_value, X25519KeyPubKeySize());
47 private_key.set_private_key((*key)->private_key, X25519KeyPrivKeySize());
48 return util::OkStatus();
49 }
50
51 } // namespace
52
ValidateKeyFormat(const HpkeKeyFormat & key_format) const53 util::Status HpkePrivateKeyManager::ValidateKeyFormat(
54 const HpkeKeyFormat& key_format) const {
55 if (!key_format.has_params()) {
56 return util::Status(absl::StatusCode::kInvalidArgument, "Missing params.");
57 }
58 return ValidateParams(key_format.params());
59 }
60
CreateKey(const HpkeKeyFormat & key_format) const61 util::StatusOr<HpkePrivateKey> HpkePrivateKeyManager::CreateKey(
62 const HpkeKeyFormat& key_format) const {
63 // Set key metadata.
64 HpkePrivateKey private_key;
65 private_key.set_version(get_version());
66 HpkePublicKey* public_key = private_key.mutable_public_key();
67 public_key->set_version(get_version());
68 *(public_key->mutable_params()) = key_format.params();
69 // Generate key material.
70 switch (key_format.params().kem()) {
71 case HpkeKem::DHKEM_X25519_HKDF_SHA256: {
72 util::Status res = GenerateX25519Key(*public_key, private_key);
73 if (!res.ok()) {
74 return res;
75 }
76 break;
77 }
78 default:
79 return util::Status(
80 absl::StatusCode::kInvalidArgument,
81 absl::StrCat("Unsupported KEM type: ", key_format.params().kem()));
82 }
83 return private_key;
84 }
85
GetPublicKey(const HpkePrivateKey & private_key) const86 util::StatusOr<HpkePublicKey> HpkePrivateKeyManager::GetPublicKey(
87 const HpkePrivateKey& private_key) const {
88 return private_key.public_key();
89 }
90
ValidateKey(const HpkePrivateKey & key) const91 util::Status HpkePrivateKeyManager::ValidateKey(
92 const HpkePrivateKey& key) const {
93 util::Status status = ValidateVersion(key.version(), get_version());
94 if (!status.ok()) return status;
95 if (!key.has_public_key()) {
96 return util::Status(absl::StatusCode::kInvalidArgument,
97 "Missing HPKE public key.");
98 }
99 return ValidateKeyAndVersion(key.public_key(), get_version());
100 }
101
102 } // namespace internal
103 } // namespace tink
104 } // namespace crypto
105