• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/falcon_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/falcon_subtle_utils.h"
27 #include "tink/util/secret_data.h"
28 #include "tink/util/statusor.h"
29 
30 extern "C" {
31 #include "third_party/pqclean/crypto_sign/falcon-1024/api.h"
32 #include "third_party/pqclean/crypto_sign/falcon-512/api.h"
33 }
34 
35 namespace crypto {
36 namespace tink {
37 namespace subtle {
38 
39 // static
New(const FalconPublicKeyPqclean & public_key)40 util::StatusOr<std::unique_ptr<PublicKeyVerify>> FalconVerify::New(
41     const FalconPublicKeyPqclean& public_key) {
42   auto status = internal::CheckFipsCompatibility<FalconVerify>();
43   if (!status.ok()) return status;
44 
45   return {
46       absl::WrapUnique<FalconVerify>(new FalconVerify(public_key))};
47 }
48 
Verify(absl::string_view signature,absl::string_view data) const49 util::Status FalconVerify::Verify(absl::string_view signature,
50                                   absl::string_view data) const {
51   int32_t key_size = public_key_.GetKey().size();
52   int result = 1;
53 
54   switch (key_size) {
55     case kFalcon512PublicKeySize: {
56       result = PQCLEAN_FALCON512_crypto_sign_verify(
57           reinterpret_cast<const uint8_t *>(signature.data()), signature.size(),
58           reinterpret_cast<const uint8_t *>(data.data()), data.size(),
59           reinterpret_cast<const uint8_t *>(public_key_.GetKey().data()));
60       break;
61     }
62     case kFalcon1024PublicKeySize: {
63       result = PQCLEAN_FALCON1024_crypto_sign_verify(
64           reinterpret_cast<const uint8_t *>(signature.data()), signature.size(),
65           reinterpret_cast<const uint8_t *>(data.data()), data.size(),
66           reinterpret_cast<const uint8_t *>(public_key_.GetKey().data()));
67       break;
68     }
69     default:
70       return util::Status(absl::StatusCode::kInvalidArgument,
71                           "Invalid keysize.");
72   }
73 
74   if (result != 0) {
75     return util::Status(absl::StatusCode::kInternal, "Signature is not valid.");
76   }
77 
78   return util::OkStatus();
79 }
80 
81 }  // namespace subtle
82 }  // namespace tink
83 }  // namespace crypto
84