xref: /aosp_15_r20/external/tink/cc/experimental/pqcrypto/signature/subtle/sphincs_subtle_utils.h (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 #ifndef TINK_EXPERIMENTAL_PQCRYPTO_SIGNATURE_SUBTLE_SPHINCS_SUBTLE_UTILS_H_
18 #define TINK_EXPERIMENTAL_PQCRYPTO_SIGNATURE_SUBTLE_SPHINCS_SUBTLE_UTILS_H_
19 
20 #include <string>
21 #include <utility>
22 
23 #include "absl/memory/memory.h"
24 #include "absl/strings/str_format.h"
25 #include "tink/util/secret_data.h"
26 #include "tink/util/statusor.h"
27 
28 namespace crypto {
29 namespace tink {
30 namespace subtle {
31 
32 // The three possible sphincs private key sizes.
33 const int kSphincsPrivateKeySize64 = 64;
34 const int kSphincsPrivateKeySize96 = 96;
35 const int kSphincsPrivateKeySize128 = 128;
36 
37 // The three possible sphincs public key sizes.
38 const int kSphincsPublicKeySize32 = 32;
39 const int kSphincsPublicKeySize48 = 48;
40 const int kSphincsPublicKeySize64 = 64;
41 
42 enum SphincsHashType {
43   HASH_TYPE_UNSPECIFIED = 0,
44   HARAKA = 1,
45   SHA256 = 2,
46   SHAKE256 = 3,
47 };
48 
49 enum SphincsVariant {
50   VARIANT_UNSPECIFIED = 0,
51   ROBUST = 1,
52   SIMPLE = 2,
53 };
54 
55 enum SphincsSignatureType {
56   SIG_TYPE_UNSPECIFIED = 0,
57   FAST_SIGNING = 1,
58   SMALL_SIGNATURE = 2,
59 };
60 
61 struct SphincsParamsPqclean {
62   SphincsHashType hash_type;
63   SphincsVariant variant;
64   SphincsSignatureType sig_length_type;
65   int32_t private_key_size;
66 };
67 
68 // Representation of the Sphincs private key.
69 class SphincsPrivateKeyPqclean {
70  public:
SphincsPrivateKeyPqclean(util::SecretData key_data,SphincsParamsPqclean params)71   explicit SphincsPrivateKeyPqclean(util::SecretData key_data,
72                                     SphincsParamsPqclean params)
73       : private_key_data_(std::move(key_data)), params_(std::move(params)) {}
74 
75   SphincsPrivateKeyPqclean(const SphincsPrivateKeyPqclean& other) = default;
76   SphincsPrivateKeyPqclean& operator=(const SphincsPrivateKeyPqclean& other) =
77       default;
78 
GetKey()79   const util::SecretData& GetKey() const { return private_key_data_; }
GetParams()80   const SphincsParamsPqclean& GetParams() const { return params_; }
81 
82  private:
83   const util::SecretData private_key_data_;
84   const SphincsParamsPqclean params_;
85 };
86 
87 // Representation of the Sphincs public key.
88 class SphincsPublicKeyPqclean {
89  public:
SphincsPublicKeyPqclean(std::string key_data,SphincsParamsPqclean params)90   SphincsPublicKeyPqclean(std::string key_data, SphincsParamsPqclean params)
91       : public_key_data_(std::move(key_data)), params_(std::move(params)) {}
92 
93   SphincsPublicKeyPqclean(const SphincsPublicKeyPqclean& other) = default;
94   SphincsPublicKeyPqclean& operator=(const SphincsPublicKeyPqclean& other) =
95       default;
96 
GetKey()97   const std::string& GetKey() const { return public_key_data_; }
GetParams()98   const SphincsParamsPqclean& GetParams() const { return params_; }
99 
100  private:
101   const std::string public_key_data_;
102   const SphincsParamsPqclean params_;
103 };
104 
105 class SphincsKeyPair {
106  public:
SphincsKeyPair(SphincsPrivateKeyPqclean private_key,SphincsPublicKeyPqclean public_key)107   SphincsKeyPair(SphincsPrivateKeyPqclean private_key,
108                  SphincsPublicKeyPqclean public_key)
109       : private_key_(std::move(private_key)),
110         public_key_(std::move(public_key)) {}
111 
112   SphincsKeyPair(const SphincsKeyPair& other) = default;
113   SphincsKeyPair& operator=(const SphincsKeyPair& other) = default;
114 
GetPrivateKey()115   const SphincsPrivateKeyPqclean& GetPrivateKey() const { return private_key_; }
GetPublicKey()116   const SphincsPublicKeyPqclean& GetPublicKey() const { return public_key_; }
117 
118  private:
119   const SphincsPrivateKeyPqclean private_key_;
120   const SphincsPublicKeyPqclean public_key_;
121 };
122 
123 // This is an utility function that generates a new Sphincs key pair based on
124 // Sphincs specific parameters. This function is expected to be called from
125 // a key manager class.
126 crypto::tink::util::StatusOr<SphincsKeyPair> GenerateSphincsKeyPair(
127     SphincsParamsPqclean params);
128 
129 // Validates whether the private key size is safe to use for sphincs signature.
130 crypto::tink::util::Status ValidatePrivateKeySize(int32_t key_size);
131 
132 // Validates whether the public key size is safe to use for sphincs signature.
133 crypto::tink::util::Status ValidatePublicKeySize(int32_t key_size);
134 
135 // Validates whether the parameters are safe to use for sphincs signature.
136 crypto::tink::util::Status ValidateParams(SphincsParamsPqclean params);
137 
138 
139 // Convert the sphincs private key size to the appropiate index in the
140 // pqclean functions array.
141 crypto::tink::util::StatusOr<int32_t> SphincsKeySizeToIndex(int32_t key_size);
142 
143 }  // namespace subtle
144 }  // namespace tink
145 }  // namespace crypto
146 
147 #endif  // TINK_EXPERIMENTAL_PQCRYPTO_SIGNATURE_SUBTLE_SPHINCS_SUBTLE_UTILS_H_
148