| 1 |
/* |
| 2 |
* X448 |
| 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/x448.h> |
| 10 |
|
| 11 |
#include <botan/ber_dec.h> |
| 12 |
#include <botan/der_enc.h> |
| 13 |
#include <botan/mem_ops.h> |
| 14 |
#include <botan/rng.h> |
| 15 |
#include <botan/internal/ct_utils.h> |
| 16 |
#include <botan/internal/pk_ops_impl.h> |
| 17 |
#include <botan/internal/x448_internal.h> |
| 18 |
|
| 19 |
namespace Botan { |
| 20 |
|
| 21 |
namespace { |
| 22 |
void x448_basepoint_from_data(std::span<uint8_t, X448_LEN> mypublic, std::span<const uint8_t, X448_LEN> secret) { |
| 23 |
auto bp = x448_basepoint(decode_scalar(secret)); |
| 24 |
auto bp_bytes = encode_point(bp); |
| 25 |
copy_mem(mypublic, bp_bytes); |
| 26 |
} |
| 27 |
|
| 28 |
secure_vector<uint8_t> ber_decode_sk(std::span<const uint8_t> key_bits) { |
| 29 |
secure_vector<uint8_t> decoded_bits; |
| 30 |
BER_Decoder(key_bits, BER_Decoder::Limits::DER()).decode(decoded_bits, ASN1_Type::OctetString).verify_end(); |
| 31 |
if(decoded_bits.size() != X448_LEN) { |
| 32 |
throw Decoding_Error("Invalid size for X448 private key"); |
| 33 |
} |
| 34 |
return decoded_bits; |
| 35 |
} |
| 36 |
|
| 37 |
} // namespace |
| 38 |
|
| 39 |
AlgorithmIdentifier X448_PublicKey::algorithm_identifier() const { |
| 40 |
return AlgorithmIdentifier(object_identifier(), AlgorithmIdentifier::USE_EMPTY_PARAM); |
| 41 |
} |
| 42 |
|
| 43 |
bool X448_PublicKey::check_key(RandomNumberGenerator& /*rng*/, bool /*strong*/) const { |
| 44 |
return true; // no tests possible? |
| 45 |
} |
| 46 |
|
| 47 |
std::vector<uint8_t> X448_PublicKey::raw_public_key_bits() const { |
| 48 |
return {m_public.begin(), m_public.end()}; |
| 49 |
} |
| 50 |
|
| 51 |
std::vector<uint8_t> X448_PublicKey::public_key_bits() const { |
| 52 |
return raw_public_key_bits(); |
| 53 |
} |
| 54 |
|
| 55 |
std::unique_ptr<Private_Key> X448_PublicKey::generate_another(RandomNumberGenerator& rng) const { |
| 56 |
return std::make_unique<X448_PrivateKey>(rng); |
| 57 |
} |
| 58 |
|
| 59 |
X448_PublicKey::X448_PublicKey(const AlgorithmIdentifier& /*alg_id*/, std::span<const uint8_t> key_bits) : |
| 60 |
X448_PublicKey(key_bits) {} |
| 61 |
|
| 62 |
X448_PublicKey::X448_PublicKey(std::span<const uint8_t> pub) { |
| 63 |
BOTAN_ARG_CHECK(pub.size() == X448_LEN, "Invalid size for X448 public key"); |
| 64 |
copy_mem(m_public, pub); |
| 65 |
} |
| 66 |
|
| 67 |
X448_PrivateKey::X448_PrivateKey(const AlgorithmIdentifier& /*alg_id*/, std::span<const uint8_t> key_bits) : |
| 68 |
X448_PrivateKey(ber_decode_sk(key_bits)) {} |
| 69 |
|
| 70 |
X448_PrivateKey::X448_PrivateKey(std::span<const uint8_t> secret_key) { |
| 71 |
BOTAN_ARG_CHECK(secret_key.size() == X448_LEN, "Invalid size for X448 private key"); |
| 72 |
m_private.assign(secret_key.begin(), secret_key.end()); |
| 73 |
auto scope = CT::scoped_poison(m_private); |
| 74 |
x448_basepoint_from_data(m_public, std::span(m_private).first<X448_LEN>()); |
| 75 |
CT::unpoison(m_public); |
| 76 |
} |
| 77 |
|
| 78 |
X448_PrivateKey::X448_PrivateKey(RandomNumberGenerator& rng) : X448_PrivateKey(rng.random_vec(X448_LEN)) {} |
| 79 |
|
| 80 |
std::unique_ptr<Public_Key> X448_PrivateKey::public_key() const { |
| 81 |
return std::make_unique<X448_PublicKey>(public_value()); |
| 82 |
} |
| 83 |
|
| 84 |
secure_vector<uint8_t> X448_PrivateKey::private_key_bits() const { |
| 85 |
return DER_Encoder().encode(m_private, ASN1_Type::OctetString).get_contents(); |
| 86 |
} |
| 87 |
|
| 88 |
bool X448_PrivateKey::check_key(RandomNumberGenerator& /*rng*/, bool /*strong*/) const { |
| 89 |
std::array<uint8_t, X448_LEN> public_point{}; |
| 90 |
BOTAN_ASSERT_NOMSG(m_private.size() == X448_LEN); |
| 91 |
auto scope = CT::scoped_poison(m_private); |
| 92 |
x448_basepoint_from_data(public_point, std::span(m_private).first<X448_LEN>()); |
| 93 |
return CT::is_equal(public_point.data(), m_public.data(), m_public.size()).as_bool(); |
| 94 |
} |
| 95 |
|
| 96 |
namespace { |
| 97 |
|
| 98 |
/** |
| 99 |
* X448 operation |
| 100 |
*/ |
| 101 |
class X448_KA_Operation final : public PK_Ops::Key_Agreement_with_KDF { |
| 102 |
public: |
| 103 |
X448_KA_Operation(std::span<const uint8_t> sk, std::string_view kdf) : |
| 104 |
PK_Ops::Key_Agreement_with_KDF(kdf), m_sk(sk.begin(), sk.end()) { |
| 105 |
BOTAN_ARG_CHECK(sk.size() == X448_LEN, "Invalid size for X448 private key"); |
| 106 |
} |
| 107 |
|
| 108 |
size_t agreed_value_size() const override { return X448_LEN; } |
| 109 |
|
| 110 |
secure_vector<uint8_t> raw_agree(const uint8_t w_data[], size_t w_len) override { |
| 111 |
auto scope = CT::scoped_poison(m_sk); |
| 112 |
|
| 113 |
const std::span<const uint8_t> w(w_data, w_len); |
| 114 |
if(w.size() != X448_LEN) { |
| 115 |
throw Decoding_Error("Invalid size for X448 public key"); |
| 116 |
} |
| 117 |
const auto k = decode_scalar(m_sk); |
| 118 |
const auto u = decode_point(w); |
| 119 |
|
| 120 |
auto shared_secret = encode_point(x448(k, u)); |
| 121 |
CT::unpoison(shared_secret); |
| 122 |
|
| 123 |
// RFC 7748 Section 6.2 |
| 124 |
// As with X25519, both sides MAY check, without leaking extra |
| 125 |
// information about the value of K, whether the resulting shared K |
| 126 |
// is the all-zero value and abort if so. |
| 127 |
// |
| 128 |
// TODO: once the generic Key Agreement operation creation is equipped |
| 129 |
// with a more flexible parameterization, this check could be |
| 130 |
// made optional. |
| 131 |
// For instance: `sk->agree().with_optional_sanity_checks(true)`. |
| 132 |
// See also: https://github.com/randombit/botan/pull/4318 |
| 133 |
if(CT::all_zeros(shared_secret.data(), shared_secret.size()).as_bool()) { |
| 134 |
throw Invalid_Argument("X448 public point appears to be of low order"); |
| 135 |
} |
| 136 |
|
| 137 |
return shared_secret; |
| 138 |
} |
| 139 |
|
| 140 |
private: |
| 141 |
secure_vector<uint8_t> m_sk; |
| 142 |
}; |
| 143 |
|
| 144 |
} // namespace |
| 145 |
|
| 146 |
std::unique_ptr<PK_Ops::Key_Agreement> X448_PrivateKey::create_key_agreement_op(RandomNumberGenerator& /*rng*/, |
| 147 |
std::string_view params, |
| 148 |
std::string_view provider) const { |
| 149 |
if(provider == "base" || provider.empty()) { |
| 150 |
return std::make_unique<X448_KA_Operation>(m_private, params); |
| 151 |
} |
| 152 |
throw Provider_Not_Found(algo_name(), provider); |
| 153 |
} |
| 154 |
|
| 155 |
} // namespace Botan |