Botan 3.14.1
Crypto and TLS for C&
ec_key_data.cpp
Go to the documentation of this file.
1/*
2* (C) 2024 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/ec_key_data.h>
8
9#include <botan/exceptn.h>
10#include <botan/mem_ops.h>
11#include <botan/rng.h>
12#include <botan/internal/ct_utils.h>
13
14namespace Botan {
15
16namespace {
17
18EC_AffinePoint decode_ec_public_key_point(const EC_Group& group, std::span<const uint8_t> bytes) {
19 /*
20 * RFC 5480 section 2.2:
21 * The first octet of the OCTET STRING indicates whether the key is
22 * compressed or uncompressed. The uncompressed form is indicated
23 * by 0x04 and the compressed form is indicated by either 0x02 or
24 * 0x03 (see 2.3.3 in [SEC1]). The public key MUST be rejected if
25 * any other value is included in the first octet.
26 */
27 if(auto pt_uncompressed = EC_AffinePoint::deserialize_uncompressed(group, bytes)) {
28 return std::move(pt_uncompressed).value();
29 } else if(auto pt_compressed = EC_AffinePoint::deserialize_compressed(group, bytes)) {
30 return std::move(pt_compressed).value();
31 } else {
32 throw Decoding_Error("Failed to deserialize elliptic curve point");
33 }
34}
35
36} // namespace
37
38EC_PublicKey_Data::EC_PublicKey_Data(const EC_Group& group, std::span<const uint8_t> bytes) :
39 EC_PublicKey_Data(group, decode_ec_public_key_point(group, bytes)) {}
40
42 m_group(std::move(group)), m_point(std::move(pt)) {
43 // Checking that the point lies on the curve is done in the deserialization
44 // of EC_AffinePoint.
45 if(m_point.is_identity()) {
46 throw Decoding_Error("ECC public key cannot be point at infinity");
47 }
48
49 if(m_point._group() != m_group._data()) {
50 if(m_point.group() != m_group) {
51 throw Decoding_Error("ECC public key point is not an element of the specified group");
52 }
53
54 // Same curve but a distinct EC_Group_Data (eg an unregistered explicit
55 // group decoded twice); point arithmetic requires a shared representation
56 if(auto rebound = EC_AffinePoint::deserialize_uncompressed(m_group, m_point.serialize_uncompressed())) {
57 m_point = std::move(rebound).value();
58 } else {
59 throw Decoding_Error("ECC public key point is not an element of the specified group");
60 }
61 }
62
63#if defined(BOTAN_HAS_LEGACY_EC_POINT)
64 m_legacy_point = m_point.to_legacy_point();
65#endif
66}
67
69 m_group(std::move(group)), m_scalar(std::move(x)), m_legacy_x(m_scalar.to_bigint()) {
70 if(m_scalar.group()._data() != m_group._data()) {
71 if(m_scalar.group() != m_group) {
72 throw Decoding_Error("ECC private key scalar is not an element of the specified group");
73 }
74
75 if(auto rebound = EC_Scalar::deserialize(m_group, m_scalar.serialize<secure_vector<uint8_t>>())) {
76 m_scalar = std::move(rebound).value();
77 } else {
78 throw Decoding_Error("ECC private key scalar is not an element of the specified group");
79 }
80 }
81
82 // Checking that the scalar is lower than the group order is ensured in the
83 // deserialization of the EC_Scalar or during the random generation respectively.
84 if(m_scalar.is_zero()) {
85 throw Decoding_Error("ECC private key cannot be zero");
86 }
87}
88
89namespace {
90
91EC_Scalar decode_ec_secret_key_scalar(const EC_Group& group, std::span<const uint8_t> bytes) {
92 const size_t order_bytes = group.get_order_bytes();
93
94 if(bytes.size() < order_bytes) {
95 /*
96 * Older versions had a bug which caused secret keys to not be encoded to
97 * the full byte length of the order if there were leading zero bytes. This
98 * was particularly a problem for P-521, where on average half of keys do
99 * not have their high bit set and so can be encoded in 65 bytes, vs 66
100 * bytes for the full order.
101 *
102 * To accommodate this, zero prefix the key if we see such a short input
103 */
104 secure_vector<uint8_t> padded_sk(order_bytes);
105 copy_mem(std::span{padded_sk}.last(bytes.size()), bytes);
106 return decode_ec_secret_key_scalar(group, padded_sk);
107 }
108
109 if(auto s = EC_Scalar::deserialize(group, bytes)) {
110 return s.value();
111 } else {
112 throw Decoding_Error("EC private key is invalid for this group");
113 }
114}
115
116} // namespace
117
118EC_PrivateKey_Data::EC_PrivateKey_Data(const EC_Group& group, std::span<const uint8_t> bytes) :
119 Botan::EC_PrivateKey_Data(group, decode_ec_secret_key_scalar(group, bytes)) {}
120
122 m_scalar.zeroize();
123}
124
125std::shared_ptr<EC_PublicKey_Data> EC_PrivateKey_Data::public_key(RandomNumberGenerator& rng,
126 bool with_modular_inverse) const {
127 const auto public_point = [&] {
128 const auto scope = CT::scoped_poison(m_scalar);
129 auto pt =
130 with_modular_inverse ? EC_AffinePoint::g_mul(m_scalar.invert(), rng) : EC_AffinePoint::g_mul(m_scalar, rng);
131 CT::unpoison(pt);
132 return pt;
133 };
134
135 return std::make_shared<EC_PublicKey_Data>(m_group, public_point());
136}
137
138std::shared_ptr<EC_PublicKey_Data> EC_PrivateKey_Data::public_key(bool with_modular_inverse) const {
139 Null_RNG null_rng;
140 return this->public_key(null_rng, with_modular_inverse);
141}
142
143void EC_PrivateKey_Data::serialize_to(std::span<uint8_t> output) const {
144 m_scalar.serialize_to(output);
145}
146
147} // namespace Botan
static std::optional< EC_AffinePoint > deserialize_uncompressed(const EC_Group &group, std::span< const uint8_t > bytes)
static EC_AffinePoint g_mul(const EC_Scalar &scalar, RandomNumberGenerator &rng)
Multiply by the group generator returning a complete point.
static std::optional< EC_AffinePoint > deserialize_compressed(const EC_Group &group, std::span< const uint8_t > bytes)
const EC_Group & group() const
Definition ec_key_data.h:71
void serialize_to(std::span< uint8_t > output) const
std::shared_ptr< EC_PublicKey_Data > public_key(RandomNumberGenerator &rng, bool with_modular_inverse) const
EC_PrivateKey_Data(EC_Group group, EC_Scalar x)
const EC_Group & group() const
Definition ec_key_data.h:30
EC_PublicKey_Data(EC_Group group, EC_AffinePoint pt)
static std::optional< EC_Scalar > deserialize(const EC_Group &group, std::span< const uint8_t > bytes)
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:222
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:67
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128