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_verify_key_manager.h"
18
19 #include <memory>
20
21 #include "absl/memory/memory.h"
22 #include "absl/strings/str_cat.h"
23 #include "absl/strings/string_view.h"
24 #include "tink/experimental/pqcrypto/signature/subtle/sphincs_subtle_utils.h"
25 #include "tink/experimental/pqcrypto/signature/subtle/sphincs_verify.h"
26 #include "tink/experimental/pqcrypto/signature/util/enums.h"
27 #include "tink/public_key_verify.h"
28 #include "tink/util/errors.h"
29 #include "tink/util/input_stream_util.h"
30 #include "tink/util/protobuf_helper.h"
31 #include "tink/util/secret_data.h"
32 #include "tink/util/status.h"
33 #include "tink/util/statusor.h"
34 #include "tink/util/validation.h"
35
36 namespace crypto {
37 namespace tink {
38
39 using ::crypto::tink::subtle::SphincsParamsPqclean;
40 using ::crypto::tink::subtle::SphincsPublicKeyPqclean;
41 using ::crypto::tink::util::EnumsPqcrypto;
42 using ::crypto::tink::util::Status;
43 using ::crypto::tink::util::StatusOr;
44 using ::google::crypto::tink::SphincsParams;
45 using ::google::crypto::tink::SphincsPublicKey;
46
47 StatusOr<std::unique_ptr<PublicKeyVerify>>
Create(const SphincsPublicKey & public_key) const48 SphincsVerifyKeyManager::PublicKeyVerifyFactory::Create(
49 const SphincsPublicKey& public_key) const {
50 SphincsParamsPqclean sphincs_params_pqclean = {
51 .hash_type =
52 EnumsPqcrypto::ProtoToSubtle(public_key.params().hash_type()),
53 .variant = EnumsPqcrypto::ProtoToSubtle(public_key.params().variant()),
54 .sig_length_type =
55 EnumsPqcrypto::ProtoToSubtle(public_key.params().sig_length_type()),
56 .private_key_size = public_key.params().key_size()};
57
58 SphincsPublicKeyPqclean sphincs_public_key_pqclean(public_key.key_value(),
59 sphincs_params_pqclean);
60
61 return subtle::SphincsVerify::New(sphincs_public_key_pqclean);
62 }
63
ValidateKey(const SphincsPublicKey & key) const64 Status SphincsVerifyKeyManager::ValidateKey(const SphincsPublicKey& key) const {
65 Status status = ValidateVersion(key.version(), get_version());
66 if (!status.ok()) {
67 return status;
68 }
69
70 status = subtle::ValidatePublicKeySize(key.key_value().length());
71 if (!status.ok()) {
72 return status;
73 }
74
75 return util::OkStatus();
76 }
77
ValidateParams(const SphincsParams & params) const78 Status SphincsVerifyKeyManager::ValidateParams(
79 const SphincsParams& params) const {
80 SphincsParamsPqclean sphincs_params_pqclean = {
81 .hash_type = EnumsPqcrypto::ProtoToSubtle(params.hash_type()),
82 .variant = EnumsPqcrypto::ProtoToSubtle(params.variant()),
83 .sig_length_type = EnumsPqcrypto::ProtoToSubtle(params.sig_length_type()),
84 .private_key_size = params.key_size()};
85
86 Status status = subtle::ValidateParams(sphincs_params_pqclean);
87 if (!status.ok()) {
88 return status;
89 }
90
91 return util::OkStatus();
92 }
93
94 } // namespace tink
95 } // namespace crypto
96