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/experimental/pqcrypto/signature/sphincs_sign_key_manager.h"
18
19 #include <memory>
20
21 #include "absl/memory/memory.h"
22 #include "absl/status/status.h"
23 #include "absl/strings/str_cat.h"
24 #include "absl/strings/string_view.h"
25 #include "tink/experimental/pqcrypto/signature/sphincs_verify_key_manager.h"
26 #include "tink/experimental/pqcrypto/signature/subtle/sphincs_sign.h"
27 #include "tink/experimental/pqcrypto/signature/subtle/sphincs_subtle_utils.h"
28 #include "tink/experimental/pqcrypto/signature/util/enums.h"
29 #include "tink/public_key_sign.h"
30 #include "tink/util/errors.h"
31 #include "tink/util/input_stream_util.h"
32 #include "tink/util/protobuf_helper.h"
33 #include "tink/util/secret_data.h"
34 #include "tink/util/status.h"
35 #include "tink/util/statusor.h"
36 #include "tink/util/validation.h"
37
38 namespace crypto {
39 namespace tink {
40
41 using ::crypto::tink::subtle::SphincsKeyPair;
42 using ::crypto::tink::subtle::SphincsParamsPqclean;
43 using ::crypto::tink::subtle::SphincsPrivateKeyPqclean;
44 using ::crypto::tink::util::EnumsPqcrypto;
45 using ::crypto::tink::util::Status;
46 using ::crypto::tink::util::StatusOr;
47 using ::google::crypto::tink::SphincsKeyFormat;
48 using ::google::crypto::tink::SphincsPrivateKey;
49 using ::google::crypto::tink::SphincsPublicKey;
50
CreateKey(const SphincsKeyFormat & key_format) const51 StatusOr<SphincsPrivateKey> SphincsSignKeyManager::CreateKey(
52 const SphincsKeyFormat& key_format) const {
53 SphincsParamsPqclean sphincs_params_pqclean = {
54 .hash_type =
55 EnumsPqcrypto::ProtoToSubtle(key_format.params().hash_type()),
56 .variant = EnumsPqcrypto::ProtoToSubtle(key_format.params().variant()),
57 .sig_length_type =
58 EnumsPqcrypto::ProtoToSubtle(key_format.params().sig_length_type()),
59 .private_key_size = key_format.params().key_size()};
60
61 util::StatusOr<SphincsKeyPair> key_pair =
62 GenerateSphincsKeyPair(sphincs_params_pqclean);
63
64 if (!key_pair.status().ok()) {
65 return key_pair.status();
66 }
67
68 SphincsPrivateKey sphincs_private_key;
69 sphincs_private_key.set_version(get_version());
70 sphincs_private_key.set_key_value(
71 util::SecretDataAsStringView(key_pair->GetPrivateKey().GetKey()));
72
73 SphincsPublicKey* sphincs_public_key =
74 sphincs_private_key.mutable_public_key();
75 sphincs_public_key->set_version(get_version());
76 sphincs_public_key->set_key_value(key_pair->GetPublicKey().GetKey());
77 *(sphincs_public_key->mutable_params()) = key_format.params();
78
79 return sphincs_private_key;
80 }
81
82 StatusOr<std::unique_ptr<PublicKeySign>>
Create(const SphincsPrivateKey & private_key) const83 SphincsSignKeyManager::PublicKeySignFactory::Create(
84 const SphincsPrivateKey& private_key) const {
85 util::SecretData sk_data =
86 util::SecretDataFromStringView(private_key.key_value());
87 SphincsParamsPqclean sphincs_params_pqclean = {
88 .hash_type = EnumsPqcrypto::ProtoToSubtle(
89 private_key.public_key().params().hash_type()),
90 .variant = EnumsPqcrypto::ProtoToSubtle(
91 private_key.public_key().params().variant()),
92 .sig_length_type = EnumsPqcrypto::ProtoToSubtle(
93 private_key.public_key().params().sig_length_type()),
94 .private_key_size = private_key.public_key().params().key_size()};
95
96 SphincsPrivateKeyPqclean sphincs_private_key_pqclean(sk_data,
97 sphincs_params_pqclean);
98
99 return subtle::SphincsSign::New(sphincs_private_key_pqclean);
100 }
101
ValidateKey(const SphincsPrivateKey & key) const102 Status SphincsSignKeyManager::ValidateKey(const SphincsPrivateKey& key) const {
103 Status status = ValidateVersion(key.version(), get_version());
104 if (!status.ok()) {
105 return status;
106 }
107
108 status = subtle::ValidatePrivateKeySize(key.key_value().length());
109 if (!status.ok()) {
110 return status;
111 }
112
113 return SphincsVerifyKeyManager().ValidateKey(key.public_key());
114 }
115
ValidateKeyFormat(const SphincsKeyFormat & key_format) const116 Status SphincsSignKeyManager::ValidateKeyFormat(
117 const SphincsKeyFormat& key_format) const {
118 if (!key_format.has_params()) {
119 return Status(absl::StatusCode::kInvalidArgument, "Missing params.");
120 }
121
122 return SphincsVerifyKeyManager().ValidateParams(key_format.params());
123 }
124
125 } // namespace tink
126 } // namespace crypto
127