Merge issue-36-ed25519: Ed25519 sign/verify (#36)
This commit is contained in:
commit
f146df97f4
3 files changed
+163
No files matched your search
@@ -166,6 +166,35 @@ namespace Void::Crypto
|
|||||||
const Ed25519PublicKey& public_key,
|
const Ed25519PublicKey& public_key,
|
||||||
const ProviderOptions& provider = {});
|
const ProviderOptions& provider = {});
|
||||||
|
|
||||||
|
/// Convenience key pair returned by ed25519_keypair(). The secret seed is
|
||||||
|
/// a move-only, self-wiping handle; prefer the typed Ed25519PrivateKey APIs
|
||||||
|
/// when the key can stay inside an owning handle.
|
||||||
|
struct Ed25519KeyPair {
|
||||||
|
Ed25519Seed secret_key;
|
||||||
|
Ed25519PublicKey public_key;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Generate a fresh Ed25519 key pair. Requires the OpenSSL backend.
|
||||||
|
[[nodiscard]] Void::Result::Result<Ed25519KeyPair>
|
||||||
|
ed25519_keypair(const ProviderOptions& provider = {});
|
||||||
|
|
||||||
|
/// Sign exact message bytes with a 32-byte Ed25519 seed/secret key. The
|
||||||
|
/// temporary private-key handle is wiped when it goes out of scope; prefer
|
||||||
|
/// sign_ed25519() when the caller already owns an Ed25519PrivateKey.
|
||||||
|
[[nodiscard]] Void::Result::Result<Ed25519Signature>
|
||||||
|
ed25519_sign(std::span<const std::byte> secret_key,
|
||||||
|
std::span<const std::byte> message,
|
||||||
|
const ProviderOptions& provider = {});
|
||||||
|
|
||||||
|
/// Verify a detached Ed25519 signature over exact message bytes.
|
||||||
|
/// Malformed key/signature sizes are errors; a well-formed but invalid
|
||||||
|
/// signature returns false.
|
||||||
|
[[nodiscard]] Void::Result::Result<bool>
|
||||||
|
ed25519_verify(std::span<const std::byte> public_key,
|
||||||
|
std::span<const std::byte> message,
|
||||||
|
std::span<const std::byte> signature,
|
||||||
|
const ProviderOptions& provider = {});
|
||||||
|
|
||||||
/// Compute HMAC-SHA-256 into a caller-owned 32-byte buffer.
|
/// Compute HMAC-SHA-256 into a caller-owned 32-byte buffer.
|
||||||
/// Wipes output on backend failure and rejects overlapping buffers.
|
/// Wipes output on backend failure and rejects overlapping buffers.
|
||||||
[[nodiscard]] Void::Result::Result<void>
|
[[nodiscard]] Void::Result::Result<void>
|
||||||
|
|||||||
@@ -891,6 +891,54 @@ namespace Void::Crypto
|
|||||||
provider);
|
provider);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<Ed25519KeyPair>
|
||||||
|
ed25519_keypair(const ProviderOptions& provider)
|
||||||
|
{
|
||||||
|
auto private_key = Ed25519PrivateKey::generate(provider);
|
||||||
|
if (!private_key) {
|
||||||
|
return std::unexpected{std::move(private_key).error()};
|
||||||
|
}
|
||||||
|
auto public_key = private_key->public_key();
|
||||||
|
if (!public_key) {
|
||||||
|
return std::unexpected{std::move(public_key).error()};
|
||||||
|
}
|
||||||
|
auto secret_key = private_key->export_raw_seed();
|
||||||
|
if (!secret_key) {
|
||||||
|
return std::unexpected{std::move(secret_key).error()};
|
||||||
|
}
|
||||||
|
return Ed25519KeyPair{std::move(*secret_key), std::move(*public_key)};
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<Ed25519Signature>
|
||||||
|
ed25519_sign(std::span<const std::byte> secret_key,
|
||||||
|
std::span<const std::byte> message,
|
||||||
|
const ProviderOptions& provider)
|
||||||
|
{
|
||||||
|
auto private_key = Ed25519PrivateKey::from_raw_seed(secret_key, provider);
|
||||||
|
if (!private_key) {
|
||||||
|
return std::unexpected{std::move(private_key).error()};
|
||||||
|
}
|
||||||
|
return sign_ed25519(message, *private_key, provider);
|
||||||
|
}
|
||||||
|
|
||||||
|
Void::Result::Result<bool>
|
||||||
|
ed25519_verify(std::span<const std::byte> public_key,
|
||||||
|
std::span<const std::byte> message,
|
||||||
|
std::span<const std::byte> signature,
|
||||||
|
const ProviderOptions& provider)
|
||||||
|
{
|
||||||
|
auto parsed_public_key = Ed25519PublicKey::from_bytes(public_key);
|
||||||
|
if (!parsed_public_key) {
|
||||||
|
return std::unexpected{std::move(parsed_public_key).error()};
|
||||||
|
}
|
||||||
|
auto parsed_signature = Ed25519Signature::from_bytes(signature);
|
||||||
|
if (!parsed_signature) {
|
||||||
|
return std::unexpected{std::move(parsed_signature).error()};
|
||||||
|
}
|
||||||
|
return verify_ed25519(message, *parsed_signature, *parsed_public_key,
|
||||||
|
provider);
|
||||||
|
}
|
||||||
|
|
||||||
Void::Result::Result<void>
|
Void::Result::Result<void>
|
||||||
hmac_sha256_into(std::span<const std::byte> secret_key,
|
hmac_sha256_into(std::span<const std::byte> secret_key,
|
||||||
std::span<const std::byte> message,
|
std::span<const std::byte> message,
|
||||||
|
|||||||
@@ -273,6 +273,92 @@ Void::Testing::Test crypto_ed25519_signing_vectors_and_boundaries()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test crypto_ed25519_convenience_api()
|
||||||
|
{
|
||||||
|
using namespace Void::Crypto;
|
||||||
|
|
||||||
|
// RFC 8032 TEST 1: seed, public key and empty-message signature.
|
||||||
|
const auto seed = from_hex(
|
||||||
|
"9d61b19deffd5a60ba844af492ec2cc44449c5697b326919703bac031cae7f60");
|
||||||
|
const auto public_key = from_hex(
|
||||||
|
"d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a");
|
||||||
|
const auto expected_signature = from_hex(
|
||||||
|
"e5564300c360ac729086e2cc806e828a84877f1eb8e5d974d873e06522490155"
|
||||||
|
"5fb8821590a33bacc61e39701cf9b46bd25bf5f0595bbe24655141438e7a100b");
|
||||||
|
|
||||||
|
if (!has_openssl_backend()) {
|
||||||
|
auto keypair = ed25519_keypair();
|
||||||
|
Void::Testing::require(!keypair.has_value());
|
||||||
|
Void::Testing::expect_eq(
|
||||||
|
keypair.error().code().value,
|
||||||
|
static_cast<std::int32_t>(CryptoErrorCode::unsupported_algorithm));
|
||||||
|
auto signature = ed25519_sign(seed, {});
|
||||||
|
Void::Testing::require(!signature.has_value());
|
||||||
|
auto verified = ed25519_verify(public_key, {}, expected_signature);
|
||||||
|
Void::Testing::require(!verified.has_value());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto signature = ed25519_sign(seed, {});
|
||||||
|
Void::Testing::require(signature.has_value());
|
||||||
|
Void::Testing::expect_eq(to_hex(signature->view()),
|
||||||
|
to_hex(expected_signature));
|
||||||
|
|
||||||
|
auto verified = ed25519_verify(public_key, {}, signature->view());
|
||||||
|
Void::Testing::require(verified.has_value());
|
||||||
|
Void::Testing::expect(*verified);
|
||||||
|
|
||||||
|
// Tampered signature and tampered message return false, not an error.
|
||||||
|
auto tampered_signature = *signature;
|
||||||
|
tampered_signature.bytes[0] ^= std::byte{0x01};
|
||||||
|
auto bad_signature =
|
||||||
|
ed25519_verify(public_key, {}, tampered_signature.view());
|
||||||
|
Void::Testing::require(bad_signature.has_value());
|
||||||
|
Void::Testing::expect(!*bad_signature);
|
||||||
|
|
||||||
|
auto bad_message =
|
||||||
|
ed25519_verify(public_key, as_bytes("not empty"), signature->view());
|
||||||
|
Void::Testing::require(bad_message.has_value());
|
||||||
|
Void::Testing::expect(!*bad_message);
|
||||||
|
|
||||||
|
// Generated key pair round-trip.
|
||||||
|
auto keypair = ed25519_keypair();
|
||||||
|
Void::Testing::require(keypair.has_value());
|
||||||
|
Void::Testing::expect(keypair->secret_key.valid());
|
||||||
|
const auto message = as_bytes("VoidBuildSystem provenance");
|
||||||
|
auto round_signature = ed25519_sign(keypair->secret_key.view(), message);
|
||||||
|
Void::Testing::require(round_signature.has_value());
|
||||||
|
auto round_verified = ed25519_verify(keypair->public_key.view(), message,
|
||||||
|
round_signature->view());
|
||||||
|
Void::Testing::require(round_verified.has_value());
|
||||||
|
Void::Testing::expect(*round_verified);
|
||||||
|
|
||||||
|
// Wrong-length secret keys and public keys are errors.
|
||||||
|
for (const std::size_t bad_len : {0U, 1U, 31U, 33U, 64U}) {
|
||||||
|
std::vector<std::byte> bad_key(bad_len, std::byte{0x11});
|
||||||
|
auto r = ed25519_sign(bad_key, {});
|
||||||
|
Void::Testing::require(!r.has_value());
|
||||||
|
Void::Testing::expect_eq(
|
||||||
|
r.error().code().value,
|
||||||
|
static_cast<std::int32_t>(CryptoErrorCode::invalid_key_size));
|
||||||
|
|
||||||
|
auto v = ed25519_verify(bad_key, {}, signature->view());
|
||||||
|
Void::Testing::require(!v.has_value());
|
||||||
|
Void::Testing::expect_eq(
|
||||||
|
v.error().code().value,
|
||||||
|
static_cast<std::int32_t>(CryptoErrorCode::invalid_key_size));
|
||||||
|
}
|
||||||
|
for (const std::size_t bad_len : {0U, 1U, 63U, 65U}) {
|
||||||
|
std::vector<std::byte> bad_signature(bad_len, std::byte{0x22});
|
||||||
|
auto v = ed25519_verify(public_key, {}, bad_signature);
|
||||||
|
Void::Testing::require(!v.has_value());
|
||||||
|
Void::Testing::expect_eq(
|
||||||
|
v.error().code().value,
|
||||||
|
static_cast<std::int32_t>(
|
||||||
|
CryptoErrorCode::invalid_signature_size));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Void::Testing::Test crypto_hmac_sha256_and_hkdf_sha256_vectors_and_guards()
|
Void::Testing::Test crypto_hmac_sha256_and_hkdf_sha256_vectors_and_guards()
|
||||||
{
|
{
|
||||||
using namespace Void::Crypto;
|
using namespace Void::Crypto;
|
||||||
|
|||||||
Reference in new issue
Block a user