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_FALCON_SUBTLE_UTILS_H_ 18 #define TINK_EXPERIMENTAL_PQCRYPTO_SIGNATURE_SUBTLE_FALCON_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 "absl/strings/string_view.h" 26 #include "tink/util/secret_data.h" 27 #include "tink/util/statusor.h" 28 29 namespace crypto { 30 namespace tink { 31 namespace subtle { 32 33 // The two possible falcon private key sizes, as defined at 34 // https://falcon-sign.info/. 35 36 const int kFalcon512PrivateKeySize = 1281; 37 const int kFalcon1024PrivateKeySize = 2305; 38 39 // The two possible falcon public key sizes as defined at 40 // https://falcon-sign.info/. 41 const int kFalcon512PublicKeySize = 897; 42 const int kFalcon1024PublicKeySize = 1793; 43 44 // Representation of the Falcon private key. 45 class FalconPrivateKeyPqclean { 46 public: 47 // Creates a new FalconPrivateKeyPqclean from key_data. 48 static util::StatusOr<FalconPrivateKeyPqclean> NewPrivateKey( 49 const util::SecretData& key_data); 50 51 FalconPrivateKeyPqclean(const FalconPrivateKeyPqclean& other) = default; 52 FalconPrivateKeyPqclean& operator=(const FalconPrivateKeyPqclean& other) = 53 default; 54 GetKey()55 const util::SecretData& GetKey() const { return key_data_; } 56 57 private: FalconPrivateKeyPqclean(const util::SecretData & key_data)58 explicit FalconPrivateKeyPqclean(const util::SecretData& key_data) 59 : key_data_(key_data) {} 60 61 const util::SecretData key_data_; 62 }; 63 64 // Representation of the Falcon public key. 65 class FalconPublicKeyPqclean { 66 public: 67 // Creates a new FalconPublicKeyPqclean from key_data. 68 static util::StatusOr<FalconPublicKeyPqclean> NewPublicKey( 69 absl::string_view key_data); 70 71 FalconPublicKeyPqclean(const FalconPublicKeyPqclean& other) = default; 72 FalconPublicKeyPqclean& operator=(const FalconPublicKeyPqclean& other) = 73 default; 74 GetKey()75 const std::string& GetKey() const { return key_data_; } 76 77 private: FalconPublicKeyPqclean(absl::string_view key_data)78 explicit FalconPublicKeyPqclean(absl::string_view key_data) 79 : key_data_(std::move(key_data)) {} 80 81 const std::string key_data_; 82 }; 83 84 class FalconKeyPair { 85 public: FalconKeyPair(FalconPrivateKeyPqclean private_key,FalconPublicKeyPqclean public_key)86 FalconKeyPair(FalconPrivateKeyPqclean private_key, 87 FalconPublicKeyPqclean public_key) 88 : private_key_(std::move(private_key)), 89 public_key_(std::move(public_key)) {} 90 91 FalconKeyPair(const FalconKeyPair& other) = default; 92 FalconKeyPair& operator=(const FalconKeyPair& other) = default; 93 GetPrivateKey()94 const FalconPrivateKeyPqclean& GetPrivateKey() const { return private_key_; } GetPublicKey()95 const FalconPublicKeyPqclean& GetPublicKey() const { return public_key_; } 96 97 private: 98 const FalconPrivateKeyPqclean private_key_; 99 const FalconPublicKeyPqclean public_key_; 100 }; 101 102 // This is an utility function that generates a new Falcon key pair. 103 // This function is expected to be called from a key manager class. 104 crypto::tink::util::StatusOr<FalconKeyPair> GenerateFalconKeyPair( 105 int32_t private_key_size); 106 107 // Validates whether the private key size is safe to use for falcon signature. 108 crypto::tink::util::Status ValidateFalconPrivateKeySize(int32_t key_size); 109 110 // Validates whether the public key size is safe to use for falcon signature. 111 crypto::tink::util::Status ValidateFalconPublicKeySize(int32_t key_size); 112 113 } // namespace subtle 114 } // namespace tink 115 } // namespace crypto 116 117 #endif // TINK_EXPERIMENTAL_PQCRYPTO_SIGNATURE_SUBTLE_FALCON_SUBTLE_UTILS_H_ 118