|
| 1 | +// Copyright 2024 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/slh_dsa_private_key.h" |
| 18 | + |
| 19 | +#include <cstdint> |
| 20 | +#include <string> |
| 21 | + |
| 22 | +#include "absl/status/status.h" |
| 23 | +#include "absl/strings/string_view.h" |
| 24 | +#include "openssl/boringssl/src/include/openssl/mem.h" |
| 25 | +#define OPENSSL_UNSTABLE_EXPERIMENTAL_SPX |
| 26 | +#include "openssl/experimental/spx.h" |
| 27 | +#undef OPENSSL_UNSTABLE_EXPERIMENTAL_SPX |
| 28 | +#include "tink/experimental/pqcrypto/signature/slh_dsa_public_key.h" |
| 29 | +#include "tink/insecure_secret_key_access.h" |
| 30 | +#include "tink/key.h" |
| 31 | +#include "tink/partial_key_access_token.h" |
| 32 | +#include "tink/restricted_data.h" |
| 33 | +#include "tink/util/status.h" |
| 34 | +#include "tink/util/statusor.h" |
| 35 | + |
| 36 | +namespace crypto { |
| 37 | +namespace tink { |
| 38 | + |
| 39 | +util::StatusOr<SlhDsaPrivateKey> SlhDsaPrivateKey::Create( |
| 40 | + const SlhDsaPublicKey& public_key, const RestrictedData& private_key_bytes, |
| 41 | + PartialKeyAccessToken token) { |
| 42 | + // Only 64-byte private keys are currently supported. |
| 43 | + if (private_key_bytes.size() != SPX_SECRET_KEY_BYTES) { |
| 44 | + return util::Status(absl::StatusCode::kInvalidArgument, |
| 45 | + "SLH-DSA private key length must be 64 bytes."); |
| 46 | + } |
| 47 | + |
| 48 | + if (public_key.GetParameters().GetPrivateKeySizeInBytes() != |
| 49 | + private_key_bytes.size()) { |
| 50 | + return util::Status(absl::StatusCode::kInvalidArgument, |
| 51 | + "Private key size does not match parameters"); |
| 52 | + } |
| 53 | + // Confirm that the private key and public key are a valid SLH-DSA key pair. |
| 54 | + std::string public_key_bytes_regen; |
| 55 | + public_key_bytes_regen.resize(SPX_PUBLIC_KEY_BYTES); |
| 56 | + std::string private_key_bytes_regen; |
| 57 | + private_key_bytes_regen.resize(SPX_SECRET_KEY_BYTES); |
| 58 | + |
| 59 | + absl::string_view expected_private_key_bytes = |
| 60 | + private_key_bytes.GetSecret(InsecureSecretKeyAccess::Get()); |
| 61 | + SPX_generate_key_from_seed( |
| 62 | + reinterpret_cast<uint8_t*>(public_key_bytes_regen.data()), |
| 63 | + reinterpret_cast<uint8_t*>(private_key_bytes_regen.data()), |
| 64 | + // Uses the first 48 bytes of the private key as seed. |
| 65 | + reinterpret_cast<const uint8_t*>(expected_private_key_bytes.data())); |
| 66 | + |
| 67 | + absl::string_view expected_public_key_bytes = |
| 68 | + public_key.GetPublicKeyBytes(token); |
| 69 | + |
| 70 | + if (CRYPTO_memcmp(expected_public_key_bytes.data(), |
| 71 | + public_key_bytes_regen.data(), SPX_PUBLIC_KEY_BYTES) != 0 || |
| 72 | + CRYPTO_memcmp(expected_private_key_bytes.data(), |
| 73 | + private_key_bytes_regen.data(), |
| 74 | + SPX_SECRET_KEY_BYTES) != 0) { |
| 75 | + return util::Status(absl::StatusCode::kInvalidArgument, |
| 76 | + "Invalid SLH-DSA key pair"); |
| 77 | + } |
| 78 | + |
| 79 | + return SlhDsaPrivateKey(public_key, private_key_bytes); |
| 80 | +} |
| 81 | + |
| 82 | +bool SlhDsaPrivateKey::operator==(const Key& other) const { |
| 83 | + const SlhDsaPrivateKey* that = dynamic_cast<const SlhDsaPrivateKey*>(&other); |
| 84 | + if (that == nullptr) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + return public_key_ == that->public_key_ && |
| 88 | + private_key_bytes_ == that->private_key_bytes_; |
| 89 | +} |
| 90 | + |
| 91 | +} // namespace tink |
| 92 | +} // namespace crypto |
0 commit comments