xref: /aosp_15_r20/external/tink/cc/experimental/pqcrypto/signature/subtle/dilithium_key.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_DILITHIUM_KEY_H_
18 #define TINK_EXPERIMENTAL_PQCRYPTO_SIGNATURE_SUBTLE_DILITHIUM_KEY_H_
19 
20 #include <memory>
21 #include <string>
22 #include <string_view>
23 #include <utility>
24 
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 enum class DilithiumSeedExpansion {
33   SEED_EXPANSION_UNKNOWN = 0,
34   SEED_EXPANSION_SHAKE = 1,
35   SEED_EXPANSION_AES = 2,
36 };
37 
38 // Dilithium public key representation.
39 class DilithiumPublicKeyPqclean {
40  public:
41   // Creates a new DilithiumPublicKeyPqclean from key_data. Should only be
42   // called with the result of a previous call to GetKeyData().
43   static util::StatusOr<DilithiumPublicKeyPqclean> NewPublicKey(
44       absl::string_view key_data, DilithiumSeedExpansion seed_expansion);
45 
46   DilithiumPublicKeyPqclean(const DilithiumPublicKeyPqclean& other) = default;
47   DilithiumPublicKeyPqclean& operator=(const DilithiumPublicKeyPqclean& other) =
48       default;
49 
50   const std::string& GetKeyData() const;
51   const DilithiumSeedExpansion& GetSeedExpansion() const;
52 
53  private:
DilithiumPublicKeyPqclean(absl::string_view key_data,DilithiumSeedExpansion seed_expansion)54   DilithiumPublicKeyPqclean(absl::string_view key_data,
55                             DilithiumSeedExpansion seed_expansion)
56       : key_data_(key_data), seed_expansion_(seed_expansion) {}
57 
58   const std::string key_data_;
59   const DilithiumSeedExpansion seed_expansion_;
60 };
61 
62 // Dilithium private key representation.
63 class DilithiumPrivateKeyPqclean {
64  public:
65   // Creates a new DilithiumPrivateKeyPqclean from key_data. Should only be
66   // called with the result of a previous call to GetKeyData().
67   static util::StatusOr<DilithiumPrivateKeyPqclean> NewPrivateKey(
68       util::SecretData key_data, DilithiumSeedExpansion seed_expansion);
69 
70   DilithiumPrivateKeyPqclean(const DilithiumPrivateKeyPqclean& other) = default;
71   DilithiumPrivateKeyPqclean& operator=(
72       const DilithiumPrivateKeyPqclean& other) = default;
73 
74   // Generates a new dilithium key pair (different key sizes based on version).
75   // Possible values for the private key size are:
76   // 2528 - Dilithium2
77   // 4000 - Dilithium3
78   // 4864 - Dilithium5
79   static util::StatusOr<
80       std::pair<DilithiumPrivateKeyPqclean, DilithiumPublicKeyPqclean>>
81   GenerateKeyPair(int32_t key_size, DilithiumSeedExpansion seed_expansion);
82 
83   const util::SecretData& GetKeyData() const;
84   const DilithiumSeedExpansion& GetSeedExpansion() const;
85 
86  private:
DilithiumPrivateKeyPqclean(util::SecretData key_data,DilithiumSeedExpansion seed_expansion)87   DilithiumPrivateKeyPqclean(util::SecretData key_data,
88                              DilithiumSeedExpansion seed_expansion)
89       : key_data_(std::move(key_data)), seed_expansion_(seed_expansion) {}
90 
91   const util::SecretData key_data_;
92   const DilithiumSeedExpansion seed_expansion_;
93 };
94 
95 }  // namespace subtle
96 }  // namespace tink
97 }  // namespace crypto
98 
99 #endif  // TINK_EXPERIMENTAL_PQCRYPTO_SIGNATURE_SUBTLE_DILITHIUM_KEY_H_
100