| 1 |
/* |
| 2 |
* Ed448 Signature Algorithm (RFC 8032) |
| 3 |
* (C) 2024 Jack Lloyd |
| 4 |
* 2024 Fabian Albert - Rohde & Schwarz Cybersecurity |
| 5 |
* |
| 6 |
* Botan is released under the Simplified BSD License (see license.txt) |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <botan/ed448.h> |
| 10 |
|
| 11 |
#include <botan/ber_dec.h> |
| 12 |
#include <botan/der_enc.h> |
| 13 |
#include <botan/hash.h> |
| 14 |
#include <botan/rng.h> |
| 15 |
#include <botan/internal/ct_utils.h> |
| 16 |
#include <botan/internal/ed448_internal.h> |
| 17 |
#include <botan/internal/pk_ops_impl.h> |
| 18 |
|
| 19 |
#include <utility> |
| 20 |
|
| 21 |
namespace Botan { |
| 22 |
|
| 23 |
AlgorithmIdentifier Ed448_PublicKey::algorithm_identifier() const { |
| 24 |
return AlgorithmIdentifier(object_identifier(), AlgorithmIdentifier::USE_EMPTY_PARAM); |
| 25 |
} |
| 26 |
|
| 27 |
bool Ed448_PublicKey::check_key(RandomNumberGenerator& /*rng*/, bool /*strong*/) const { |
| 28 |
try { |
| 29 |
Ed448Point::decode(m_public); |
| 30 |
} catch(Decoding_Error&) { |
| 31 |
return false; |
| 32 |
} |
| 33 |
return true; |
| 34 |
} |
| 35 |
|
| 36 |
Ed448_PublicKey::Ed448_PublicKey(const AlgorithmIdentifier& /* unused */, std::span<const uint8_t> key_bits) : |
| 37 |
Ed448_PublicKey(key_bits) {} |
| 38 |
|
| 39 |
Ed448_PublicKey::Ed448_PublicKey(std::span<const uint8_t> key_bits) { |
| 40 |
if(key_bits.size() != ED448_LEN) { |
| 41 |
throw Decoding_Error("Invalid length for Ed448 public key"); |
| 42 |
} |
| 43 |
copy_mem(m_public, key_bits.first<ED448_LEN>()); |
| 44 |
} |
| 45 |
|
| 46 |
std::vector<uint8_t> Ed448_PublicKey::raw_public_key_bits() const { |
| 47 |
return {m_public.begin(), m_public.end()}; |
| 48 |
} |
| 49 |
|
| 50 |
std::vector<uint8_t> Ed448_PublicKey::public_key_bits() const { |
| 51 |
return raw_public_key_bits(); |
| 52 |
} |
| 53 |
|
| 54 |
std::unique_ptr<Private_Key> Ed448_PublicKey::generate_another(RandomNumberGenerator& rng) const { |
| 55 |
return std::make_unique<Ed448_PrivateKey>(rng); |
| 56 |
} |
| 57 |
|
| 58 |
Ed448_PrivateKey::Ed448_PrivateKey(const AlgorithmIdentifier& /*unused*/, std::span<const uint8_t> key_bits) { |
| 59 |
secure_vector<uint8_t> bits; |
| 60 |
BER_Decoder(key_bits, BER_Decoder::Limits::DER()).decode(bits, ASN1_Type::OctetString).verify_end(); |
| 61 |
|
| 62 |
if(bits.size() != ED448_LEN) { |
| 63 |
throw Decoding_Error("Invalid size for Ed448 private key"); |
| 64 |
} |
| 65 |
m_private = std::move(bits); |
| 66 |
m_public = create_pk_from_sk(std::span(m_private).first<ED448_LEN>()); |
| 67 |
} |
| 68 |
|
| 69 |
Ed448_PrivateKey::Ed448_PrivateKey(RandomNumberGenerator& rng) : Ed448_PrivateKey(rng.random_vec(ED448_LEN)) {} |
| 70 |
|
| 71 |
Ed448_PrivateKey::Ed448_PrivateKey(std::span<const uint8_t> key_bits) { |
| 72 |
if(key_bits.size() != ED448_LEN) { |
| 73 |
throw Decoding_Error("Invalid size for Ed448 private key"); |
| 74 |
} |
| 75 |
m_private.assign(key_bits.begin(), key_bits.end()); |
| 76 |
auto scope = CT::scoped_poison(m_private); |
| 77 |
m_public = create_pk_from_sk(std::span(m_private).first<ED448_LEN>()); |
| 78 |
CT::unpoison(m_public); |
| 79 |
} |
| 80 |
|
| 81 |
std::unique_ptr<Public_Key> Ed448_PrivateKey::public_key() const { |
| 82 |
return std::make_unique<Ed448_PublicKey>(m_public); |
| 83 |
} |
| 84 |
|
| 85 |
secure_vector<uint8_t> Ed448_PrivateKey::private_key_bits() const { |
| 86 |
BOTAN_ASSERT_NOMSG(m_private.size() == ED448_LEN); |
| 87 |
return DER_Encoder().encode(m_private, ASN1_Type::OctetString).get_contents(); |
| 88 |
} |
| 89 |
|
| 90 |
bool Ed448_PrivateKey::check_key(RandomNumberGenerator& /*rng*/, bool /*strong*/) const { |
| 91 |
BOTAN_ASSERT_NOMSG(m_private.size() == ED448_LEN); |
| 92 |
auto scope = CT::scoped_poison(m_private); |
| 93 |
const auto public_point = create_pk_from_sk(std::span(m_private).first<ED448_LEN>()); |
| 94 |
CT::unpoison(public_point); |
| 95 |
return public_point == m_public; |
| 96 |
} |
| 97 |
|
| 98 |
namespace { |
| 99 |
|
| 100 |
/// Interface to abstract either a pure message or a prehashed message |
| 101 |
class Ed448_Message { |
| 102 |
public: |
| 103 |
virtual void update(std::span<const uint8_t> msg) = 0; |
| 104 |
virtual std::vector<uint8_t> get_and_clear() = 0; |
| 105 |
|
| 106 |
Ed448_Message() = default; |
| 107 |
virtual ~Ed448_Message() = default; |
| 108 |
Ed448_Message(const Ed448_Message&) = delete; |
| 109 |
Ed448_Message& operator=(const Ed448_Message&) = delete; |
| 110 |
Ed448_Message(Ed448_Message&&) = delete; |
| 111 |
Ed448_Message& operator=(Ed448_Message&&) = delete; |
| 112 |
}; |
| 113 |
|
| 114 |
class Prehashed_Ed448_Message final : public Ed448_Message { |
| 115 |
public: |
| 116 |
void update(std::span<const uint8_t> msg) override { m_hash->update(msg); } |
| 117 |
|
| 118 |
std::vector<uint8_t> get_and_clear() override { return m_hash->final_stdvec(); } |
| 119 |
|
| 120 |
explicit Prehashed_Ed448_Message(std::string_view hash) : m_hash(HashFunction::create_or_throw(hash)) {} |
| 121 |
|
| 122 |
private: |
| 123 |
std::unique_ptr<HashFunction> m_hash; |
| 124 |
}; |
| 125 |
|
| 126 |
class Pure_Ed448_Message final : public Ed448_Message { |
| 127 |
public: |
| 128 |
void update(std::span<const uint8_t> msg) override { m_msg.insert(m_msg.end(), msg.begin(), msg.end()); } |
| 129 |
|
| 130 |
std::vector<uint8_t> get_and_clear() override { return std::exchange(m_msg, {}); } |
| 131 |
|
| 132 |
private: |
| 133 |
std::vector<uint8_t> m_msg; |
| 134 |
}; |
| 135 |
|
| 136 |
/** |
| 137 |
* Ed448 verifying operation |
| 138 |
*/ |
| 139 |
class Ed448_Verify_Operation final : public PK_Ops::Verification { |
| 140 |
public: |
| 141 |
explicit Ed448_Verify_Operation(const Ed448_PublicKey& key, |
| 142 |
std::optional<std::string> prehash_function = std::nullopt) : |
| 143 |
m_pk(key.raw_public_key_bits()), m_prehash_function(std::move(prehash_function)) { |
| 144 |
if(m_prehash_function) { |
| 145 |
m_message = std::make_unique<Prehashed_Ed448_Message>(*m_prehash_function); |
| 146 |
} else { |
| 147 |
m_message = std::make_unique<Pure_Ed448_Message>(); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
void update(std::span<const uint8_t> input) override { m_message->update(input); } |
| 152 |
|
| 153 |
bool is_valid_signature(std::span<const uint8_t> sig) override { |
| 154 |
const auto msg = m_message->get_and_clear(); |
| 155 |
try { |
| 156 |
return verify_signature(std::span(m_pk).first<ED448_LEN>(), m_prehash_function.has_value(), {}, sig, msg); |
| 157 |
} catch(Decoding_Error&) { |
| 158 |
return false; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
std::string hash_function() const override { return m_prehash_function.value_or("SHAKE-256(912)"); } |
| 163 |
|
| 164 |
private: |
| 165 |
std::vector<uint8_t> m_pk; |
| 166 |
std::unique_ptr<Ed448_Message> m_message; |
| 167 |
std::optional<std::string> m_prehash_function; |
| 168 |
}; |
| 169 |
|
| 170 |
/** |
| 171 |
* Ed448 signing operation |
| 172 |
*/ |
| 173 |
class Ed448_Sign_Operation final : public PK_Ops::Signature { |
| 174 |
public: |
| 175 |
explicit Ed448_Sign_Operation(const Ed448_PrivateKey& key, |
| 176 |
std::optional<std::string> prehash_function = std::nullopt) : |
| 177 |
m_pk(key.raw_public_key_bits()), |
| 178 |
m_sk(key.raw_private_key_bits()), |
| 179 |
m_prehash_function(std::move(prehash_function)) { |
| 180 |
if(m_prehash_function) { |
| 181 |
m_message = std::make_unique<Prehashed_Ed448_Message>(*m_prehash_function); |
| 182 |
} else { |
| 183 |
m_message = std::make_unique<Pure_Ed448_Message>(); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
void update(std::span<const uint8_t> input) override { m_message->update(input); } |
| 188 |
|
| 189 |
std::vector<uint8_t> sign(RandomNumberGenerator& /*rng*/) override { |
| 190 |
BOTAN_ASSERT_NOMSG(m_sk.size() == ED448_LEN); |
| 191 |
auto scope = CT::scoped_poison(m_sk); |
| 192 |
const auto sig = sign_message(std::span(m_sk).first<ED448_LEN>(), |
| 193 |
std::span(m_pk).first<ED448_LEN>(), |
| 194 |
m_prehash_function.has_value(), |
| 195 |
{}, |
| 196 |
m_message->get_and_clear()); |
| 197 |
CT::unpoison(sig); |
| 198 |
return {sig.begin(), sig.end()}; |
| 199 |
} |
| 200 |
|
| 201 |
size_t signature_length() const override { return 2 * ED448_LEN; } |
| 202 |
|
| 203 |
AlgorithmIdentifier algorithm_identifier() const override; |
| 204 |
|
| 205 |
std::string hash_function() const override { return m_prehash_function.value_or("SHAKE-256(912)"); } |
| 206 |
|
| 207 |
private: |
| 208 |
std::vector<uint8_t> m_pk; |
| 209 |
secure_vector<uint8_t> m_sk; |
| 210 |
std::unique_ptr<Ed448_Message> m_message; |
| 211 |
std::optional<std::string> m_prehash_function; |
| 212 |
}; |
| 213 |
|
| 214 |
AlgorithmIdentifier Ed448_Sign_Operation::algorithm_identifier() const { |
| 215 |
return AlgorithmIdentifier(OID::from_string("Ed448"), AlgorithmIdentifier::USE_EMPTY_PARAM); |
| 216 |
} |
| 217 |
|
| 218 |
} // namespace |
| 219 |
|
| 220 |
std::unique_ptr<PK_Ops::Verification> Ed448_PublicKey::create_verification_op(std::string_view params, |
| 221 |
std::string_view provider) const { |
| 222 |
if(provider == "base" || provider.empty()) { |
| 223 |
if(params.empty() || params == "Identity" || params == "Pure" || params == "Ed448") { |
| 224 |
return std::make_unique<Ed448_Verify_Operation>(*this); |
| 225 |
} else if(params == "Ed448ph") { |
| 226 |
return std::make_unique<Ed448_Verify_Operation>(*this, "SHAKE-256(512)"); |
| 227 |
} else { |
| 228 |
return std::make_unique<Ed448_Verify_Operation>(*this, std::string(params)); |
| 229 |
} |
| 230 |
} |
| 231 |
throw Provider_Not_Found(algo_name(), provider); |
| 232 |
} |
| 233 |
|
| 234 |
std::unique_ptr<PK_Ops::Verification> Ed448_PublicKey::create_x509_verification_op(const AlgorithmIdentifier& alg_id, |
| 235 |
std::string_view provider) const { |
| 236 |
if(provider == "base" || provider.empty()) { |
| 237 |
if(alg_id != this->algorithm_identifier()) { |
| 238 |
throw Decoding_Error("Unexpected AlgorithmIdentifier for Ed448 X509 signature"); |
| 239 |
} |
| 240 |
|
| 241 |
return std::make_unique<Ed448_Verify_Operation>(*this); |
| 242 |
} |
| 243 |
throw Provider_Not_Found(algo_name(), provider); |
| 244 |
} |
| 245 |
|
| 246 |
std::unique_ptr<PK_Ops::Signature> Ed448_PrivateKey::create_signature_op(RandomNumberGenerator& /*rng*/, |
| 247 |
std::string_view params, |
| 248 |
std::string_view provider) const { |
| 249 |
if(provider == "base" || provider.empty()) { |
| 250 |
if(params.empty() || params == "Identity" || params == "Pure" || params == "Ed448") { |
| 251 |
return std::make_unique<Ed448_Sign_Operation>(*this); |
| 252 |
} else if(params == "Ed448ph") { |
| 253 |
return std::make_unique<Ed448_Sign_Operation>(*this, "SHAKE-256(512)"); |
| 254 |
} else { |
| 255 |
return std::make_unique<Ed448_Sign_Operation>(*this, std::string(params)); |
| 256 |
} |
| 257 |
} |
| 258 |
throw Provider_Not_Found(algo_name(), provider); |
| 259 |
} |
| 260 |
|
| 261 |
} // namespace Botan |