xref: /aosp_15_r20/external/tink/cc/experimental/pqcrypto/signature/subtle/sphincs_verify.cc (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 #include "tink/experimental/pqcrypto/signature/subtle/sphincs_verify.h"
18 
19 #include <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "absl/memory/memory.h"
24 #include "absl/status/status.h"
25 #include "absl/strings/str_format.h"
26 #include "tink/experimental/pqcrypto/signature/subtle/sphincs_helper_pqclean.h"
27 #include "tink/experimental/pqcrypto/signature/subtle/sphincs_subtle_utils.h"
28 #include "tink/util/secret_data.h"
29 #include "tink/util/statusor.h"
30 
31 namespace crypto {
32 namespace tink {
33 namespace subtle {
34 
35 // static
New(SphincsPublicKeyPqclean public_key)36 util::StatusOr<std::unique_ptr<PublicKeyVerify>> SphincsVerify::New(
37     SphincsPublicKeyPqclean public_key) {
38   auto status = internal::CheckFipsCompatibility<SphincsVerify>();
39   if (!status.ok()) return status;
40 
41   util::Status key_size = ValidatePublicKeySize(public_key.GetKey().size());
42   if (!key_size.ok()) {
43     return key_size;
44   }
45 
46   util::Status valid_parameters = ValidateParams(public_key.GetParams());
47   if (!valid_parameters.ok()) {
48     return valid_parameters;
49   }
50 
51   return {absl::WrapUnique<SphincsVerify>(
52       new SphincsVerify(std::move(public_key)))};
53 }
54 
Verify(absl::string_view signature,absl::string_view data) const55 util::Status SphincsVerify::Verify(absl::string_view signature,
56                                    absl::string_view data) const {
57   SphincsParamsPqclean params = key_.GetParams();
58   util::StatusOr<int32_t> key_size_index =
59       SphincsKeySizeToIndex(params.private_key_size);
60   if (!key_size_index.ok()) {
61     return key_size_index.status();
62   }
63 
64   const SphincsHelperPqclean &sphincs_helper_pqclean =
65       GetSphincsHelperPqclean(params.hash_type, params.variant, *key_size_index,
66                               params.sig_length_type);
67 
68   if ((sphincs_helper_pqclean.Verify(
69           reinterpret_cast<const uint8_t *>(signature.data()), signature.size(),
70           reinterpret_cast<const uint8_t *>(data.data()), data.size(),
71           reinterpret_cast<const uint8_t *>(key_.GetKey().data()))) != 0) {
72     return util::Status(absl::StatusCode::kInvalidArgument,
73                         "Signature is not valid.");
74   }
75 
76   return util::OkStatus();
77 }
78 
79 }  // namespace subtle
80 }  // namespace tink
81 }  // namespace crypto
82