Botan 3.14.1
Crypto and TLS for C&
ec_scalar.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/ec_scalar.h>
8
9#include <botan/ec_group.h>
10#include <botan/internal/ec_inner_data.h>
11#include <algorithm>
12
13namespace Botan {
14
15EC_Scalar EC_Scalar::_from_inner(std::unique_ptr<EC_Scalar_Data> inner) {
16 return EC_Scalar(std::move(inner));
17}
18
20 m_scalar->_const_time_poison();
21}
22
24 m_scalar->_const_time_unpoison();
25}
26
27EC_Scalar::EC_Scalar(std::unique_ptr<EC_Scalar_Data> scalar) : m_scalar(std::move(scalar)) {
28 BOTAN_ASSERT_NONNULL(m_scalar);
29}
30
31EC_Scalar::EC_Scalar(const EC_Scalar& other) : m_scalar(other.inner().clone()) {}
32
33EC_Scalar::EC_Scalar(EC_Scalar&& other) noexcept : m_scalar(std::move(other.m_scalar)) {}
34
36 if(this != &other) {
37 if(m_scalar == nullptr || m_scalar->group() != other.inner().group()) {
38 m_scalar = other.inner().clone();
39 } else {
40 this->assign(other);
41 }
42 }
43 return (*this);
44}
45
47 if(this != &other) {
48 // Even a cross-curve swap is accepted here
49 std::swap(m_scalar, other.m_scalar);
50 }
51 return (*this);
52}
53
54EC_Scalar::~EC_Scalar() = default;
55
57 return EC_Group(inner().group());
58}
59
60size_t EC_Scalar::bytes() const {
61 return m_scalar->bytes();
62}
63
65 return EC_Scalar(group._data()->scalar_from_bytes_with_trunc(bytes));
66}
67
69 if(auto s = group._data()->scalar_from_bytes_mod_order(bytes)) {
70 return EC_Scalar(std::move(s));
71 } else {
72 throw Decoding_Error("EC_Scalar::from_bytes_mod_order input invalid");
73 }
74}
75
77 return EC_Scalar(group._data()->scalar_random(rng));
78}
79
81 return EC_Scalar(group._data()->scalar_one());
82}
83
85 if(auto data = group._data()->scalar_from_bigint(bn)) {
86 return EC_Scalar(std::move(data));
87 } else {
88 throw Invalid_Argument("EC_Scalar::from_bigint input out of range");
89 }
90}
91
93 secure_vector<uint8_t> bytes(m_scalar->bytes());
94 m_scalar->serialize_to(bytes);
96}
97
98//static
100 const auto& group = scalar._inner().group();
101 return EC_Scalar(group->gk_x_mod_order(scalar.inner(), rng));
102}
103
104void EC_Scalar::serialize_to(std::span<uint8_t> bytes) const {
105 inner().serialize_to(bytes);
106}
107
108void EC_Scalar::serialize_pair_to(std::span<uint8_t> bytes, const EC_Scalar& r, const EC_Scalar& s) {
109 BOTAN_ARG_CHECK(r._inner().group() == s._inner().group(), "Curve mismatch");
110 const size_t scalar_bytes = r.bytes();
111 BOTAN_ARG_CHECK(bytes.size() == 2 * scalar_bytes, "Invalid output length");
112 r.serialize_to(bytes.first(scalar_bytes));
113 s.serialize_to(bytes.last(scalar_bytes));
114}
115
116std::optional<std::pair<EC_Scalar, EC_Scalar>> EC_Scalar::deserialize_pair(const EC_Group& group,
117 std::span<const uint8_t> bytes) {
118 if(bytes.size() % 2 != 0) {
119 return {};
120 }
121
122 const size_t half = bytes.size() / 2;
123
124 auto r = EC_Scalar::deserialize(group, bytes.first(half));
125 auto s = EC_Scalar::deserialize(group, bytes.last(half));
126
127 if(r && s) {
128 return std::make_pair(r.value(), s.value());
129 } else {
130 return {};
131 }
132}
133
134std::optional<EC_Scalar> EC_Scalar::deserialize(const EC_Group& group, std::span<const uint8_t> bytes) {
135 if(auto v = group._data()->scalar_deserialize(bytes)) {
136 return EC_Scalar(std::move(v));
137 } else {
138 return {};
139 }
140}
141
142EC_Scalar::EC_Scalar(const EC_Group& group, std::span<const uint8_t> bytes) {
143 m_scalar = group._data()->scalar_deserialize(bytes);
144 if(!m_scalar) {
145 throw Decoding_Error("EC_Scalar::from_bytes is not a valid scalar value");
146 }
147}
148
149bool EC_Scalar::is_zero() const {
150 return inner().is_zero();
151}
152
154 return EC_Scalar(inner().invert());
155}
156
158 return EC_Scalar(inner().invert_vartime());
159}
160
162 return EC_Scalar(inner().negate());
163}
164
166 m_scalar->square_self();
167}
168
170 BOTAN_ARG_CHECK(inner().group() == x.inner().group(), "Curve mismatch");
171 return EC_Scalar(inner().add(x.inner()));
172}
173
175 BOTAN_ARG_CHECK(inner().group() == x.inner().group(), "Curve mismatch");
176 return EC_Scalar(inner().sub(x.inner()));
177}
178
180 BOTAN_ARG_CHECK(inner().group() == x.inner().group(), "Curve mismatch");
181 return EC_Scalar(inner().mul(x.inner()));
182}
183
185 if(this != &x) {
186 if(m_scalar == nullptr || m_scalar->group() != x.inner().group()) {
187 m_scalar = x.inner().clone();
188 } else {
189 m_scalar->assign(x.inner());
190 }
191 }
192}
193
195 m_scalar->zeroize();
196}
197
198bool EC_Scalar::is_eq(const EC_Scalar& x) const {
199 if(inner().group() != x.inner().group()) {
200 return false;
201 }
202
203 return inner().is_eq(x.inner());
204}
205
206//static
208 std::string_view hash_fn,
209 std::span<const uint8_t> input,
210 std::span<const uint8_t> domain_sep) {
211 /*
212 RFC 9380 Section 5.2
213 L = ceil((ceil(log2(p)) + k) / 8), where k is the security
214 parameter of the suite (e.g., k = 128)
215 */
216 const size_t scalar_bits = group.get_order_bits();
217 const size_t security_level = std::min<size_t>((scalar_bits + 1) / 2, 256);
218 secure_vector<uint8_t> uniform_bytes((scalar_bits + security_level + 7) / 8);
219
220 h2c_expand_message(hash_fn, scalar_bits, input, domain_sep)(uniform_bytes);
221
222 return EC_Scalar::from_bytes_mod_order(group, uniform_bytes);
223}
224
225} // namespace Botan
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:121
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
static BigInt from_bytes(std::span< const uint8_t > bytes)
Definition bigint.cpp:84
virtual const std::shared_ptr< const EC_Group_Data > & group() const =0
virtual std::unique_ptr< EC_Scalar_Data > clone() const =0
static EC_Scalar one(const EC_Group &group)
Definition ec_scalar.cpp:80
BigInt to_bigint() const
Definition ec_scalar.cpp:92
const EC_Scalar_Data & _inner() const
Definition ec_scalar.h:282
static EC_Scalar hash(const EC_Group &group, std::string_view hash_fn, std::span< const uint8_t > input, std::span< const uint8_t > domain_sep)
void assign(const EC_Scalar &x)
bool is_eq(const EC_Scalar &x) const
static std::optional< EC_Scalar > deserialize(const EC_Group &group, std::span< const uint8_t > bytes)
EC_Scalar mul(const EC_Scalar &x) const
EC_Scalar add(const EC_Scalar &x) const
static EC_Scalar gk_x_mod_order(const EC_Scalar &scalar, RandomNumberGenerator &rng)
Definition ec_scalar.cpp:99
void _const_time_unpoison() const
Definition ec_scalar.cpp:23
void serialize_to(std::span< uint8_t > bytes) const
void _const_time_poison() const
Definition ec_scalar.cpp:19
bool is_zero() const
size_t bytes() const
Definition ec_scalar.cpp:60
static EC_Scalar from_bigint(const EC_Group &group, const BigInt &bn)
Definition ec_scalar.cpp:84
EC_Scalar & operator=(const EC_Scalar &other)
Definition ec_scalar.cpp:35
EC_Scalar invert_vartime() const
EC_Scalar invert() const
static void serialize_pair_to(std::span< uint8_t > bytes, const EC_Scalar &r, const EC_Scalar &s)
EC_Scalar(const EC_Group &group, std::span< const uint8_t > bytes)
static EC_Scalar from_bytes_mod_order(const EC_Group &group, std::span< const uint8_t > bytes)
Definition ec_scalar.cpp:68
EC_Group group() const
Definition ec_scalar.cpp:56
static EC_Scalar random(const EC_Group &group, RandomNumberGenerator &rng)
Definition ec_scalar.cpp:76
static EC_Scalar _from_inner(std::unique_ptr< EC_Scalar_Data > inner)
Definition ec_scalar.cpp:15
EC_Scalar negate() const
EC_Scalar sub(const EC_Scalar &x) const
static std::optional< std::pair< EC_Scalar, EC_Scalar > > deserialize_pair(const EC_Group &group, std::span< const uint8_t > bytes)
static EC_Scalar from_bytes_with_trunc(const EC_Group &group, std::span< const uint8_t > bytes)
Definition ec_scalar.cpp:64
std::function< void(std::span< uint8_t >)> h2c_expand_message(std::string_view hash_fn, size_t order_bits, std::span< const uint8_t > input, std::span< const uint8_t > domain_sep)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128