| 1 |
|
| 2 |
/* |
| 3 |
* Ed448 Internals |
| 4 |
* (C) 2024 Jack Lloyd |
| 5 |
* 2024 Fabian Albert - Rohde & Schwarz Cybersecurity |
| 6 |
* |
| 7 |
* Botan is released under the Simplified BSD License (see license.txt) |
| 8 |
*/ |
| 9 |
#include <botan/internal/ed448_internal.h> |
| 10 |
|
| 11 |
#include <botan/exceptn.h> |
| 12 |
#include <botan/types.h> |
| 13 |
#include <botan/xof.h> |
| 14 |
#include <botan/internal/buffer_slicer.h> |
| 15 |
#include <botan/internal/buffer_stuffer.h> |
| 16 |
#include <botan/internal/concat_util.h> |
| 17 |
#include <botan/internal/ct_utils.h> |
| 18 |
#include <botan/internal/loadstor.h> |
| 19 |
|
| 20 |
namespace Botan { |
| 21 |
namespace { |
| 22 |
|
| 23 |
std::vector<uint8_t> dom4(uint8_t x, std::span<const uint8_t> y) { |
| 24 |
// RFC 8032 2. Notation and Conventions |
| 25 |
// dom4(x, y) The octet string "SigEd448" || octet(x) || |
| 26 |
// octet(OLEN(y)) || y, where x is in range 0-255 and y |
| 27 |
// is an octet string of at most 255 octets. "SigEd448" |
| 28 |
// is in ASCII (8 octets). |
| 29 |
BOTAN_ARG_CHECK(y.size() < 256, "y is too long"); |
| 30 |
return concat<std::vector<uint8_t>>(std::array<uint8_t, 8>{'S', 'i', 'g', 'E', 'd', '4', '4', '8'}, |
| 31 |
store_le(x), |
| 32 |
store_le(static_cast<uint8_t>(y.size())), |
| 33 |
y); |
| 34 |
} |
| 35 |
|
| 36 |
template <ranges::spanable_range... Ts> |
| 37 |
std::array<uint8_t, 2 * ED448_LEN> shake(bool f, std::span<const uint8_t> context, Ts... xs) { |
| 38 |
auto shake_xof = XOF::create_or_throw("SHAKE-256"); |
| 39 |
shake_xof->update(dom4(static_cast<uint8_t>(f), context)); |
| 40 |
(shake_xof->update(std::span(xs)), ...); |
| 41 |
std::array<uint8_t, 2 * ED448_LEN> res{}; |
| 42 |
shake_xof->output(res); |
| 43 |
return res; |
| 44 |
} |
| 45 |
|
| 46 |
std::pair<std::span<const uint8_t, 57>, std::span<const uint8_t, 57>> split(std::span<const uint8_t, 114> arr) { |
| 47 |
BufferSlicer bs(arr); |
| 48 |
auto lhs = bs.take<57>(); |
| 49 |
auto rhs = bs.take<57>(); |
| 50 |
return {lhs, rhs}; |
| 51 |
} |
| 52 |
|
| 53 |
Scalar448 scalar_from_xof(XOF& shake_xof) { |
| 54 |
// 5.2.5. Key Generation |
| 55 |
// The 57-byte public key is generated by the following steps: |
| 56 |
// 1. Hash the 57-byte private key using SHAKE256(x, 114), storing the |
| 57 |
// digest in a 114-octet large buffer, denoted h. Only the lower 57 |
| 58 |
// bytes are used for generating the public key. |
| 59 |
std::array<uint8_t, ED448_LEN> raw_s{}; |
| 60 |
shake_xof.output(raw_s); |
| 61 |
// 2. Prune the buffer: The two least significant bits of the first |
| 62 |
// octet are cleared, all eight bits the last octet are cleared, and |
| 63 |
// the highest bit of the second to last octet is set. |
| 64 |
raw_s[0] &= 0xFC; |
| 65 |
raw_s[55] |= 0x80; |
| 66 |
raw_s[56] = 0; |
| 67 |
|
| 68 |
return Scalar448(raw_s); |
| 69 |
} |
| 70 |
|
| 71 |
} // namespace |
| 72 |
|
| 73 |
Ed448Point Ed448Point::decode(std::span<const uint8_t, ED448_LEN> enc) { |
| 74 |
// RFC 8032 5.2.3 Decoding |
| 75 |
// 1. First, interpret the string as an integer in little-endian |
| 76 |
// representation. Bit 455 of this number is the least significant |
| 77 |
// bit of the x-coordinate, and denote this value x_0. The |
| 78 |
// y-coordinate is recovered simply by clearing this bit. If the |
| 79 |
// resulting value is >= p, decoding fails. |
| 80 |
if((enc.back() & 0x7F) != 0) { // last byte is either 0x00 or 0x80 |
| 81 |
throw Decoding_Error("Ed448 point has unacceptable x-distinguisher"); |
| 82 |
} |
| 83 |
const bool x_distinguisher = enc.back() != 0; |
| 84 |
const auto y_data = std::span(enc).first<56>(); |
| 85 |
if(!Gf448Elem::bytes_are_canonical_representation(y_data)) { |
| 86 |
throw Decoding_Error("Ed448 y-coordinate is not smaller than p"); |
| 87 |
} |
| 88 |
const auto y = Gf448Elem(y_data); |
| 89 |
|
| 90 |
// 2. To recover the x-coordinate, the curve equation implies |
| 91 |
// x^2 = (y^2 - 1) / (d y^2 - 1) (mod p). The denominator is always |
| 92 |
// non-zero mod p. Let u = y^2 - 1 and v = d y^2 - 1. To compute |
| 93 |
// the square root of (u/v), the first step is to compute the |
| 94 |
// candidate root x = (u/v)^((p+1)/4). This can be done using the |
| 95 |
// following trick, to use a single modular powering for both the |
| 96 |
// inversion of v and the square root: |
| 97 |
// (p+1)/4 3 (p-3)/4 |
| 98 |
// x = (u/v) = u v (u^5 v^3) (mod p) |
| 99 |
const auto u = square(Gf448Elem(y)) - Gf448Elem::one(); |
| 100 |
const auto v = -mul_a24(square(Gf448Elem(y))) - Gf448Elem::one(); |
| 101 |
const auto maybe_x = (u * square(u)) * v * root((square(square(u)) * u) * square(v) * v); |
| 102 |
|
| 103 |
// 3. If v * x^2 = u, the recovered x-coordinate is x. Otherwise, no |
| 104 |
// square root exists, and the decoding fails. |
| 105 |
if(v * square(maybe_x) != u) { |
| 106 |
throw Decoding_Error("Square root does not exist"); |
| 107 |
} |
| 108 |
// 4. Finally, use the x_0 bit to select the right square root. If |
| 109 |
// x = 0, and x_0 = 1, decoding fails. Otherwise, if x_0 != x mod |
| 110 |
// 2, set x <-- p - x. Return the decoded point (x,y). |
| 111 |
if(maybe_x.is_zero() && x_distinguisher) { |
| 112 |
throw Decoding_Error("Square root of zero cannot be odd"); |
| 113 |
} |
| 114 |
const bool maybe_x_parity = maybe_x.is_odd(); |
| 115 |
std::array<uint64_t, WORDS_448> x_data{}; |
| 116 |
CT::Mask<uint64_t>::expand_bool(maybe_x_parity == x_distinguisher) |
| 117 |
.select_n(x_data.data(), maybe_x.words().data(), (-maybe_x).words().data(), WORDS_448); |
| 118 |
|
| 119 |
return {Gf448Elem(x_data), y}; |
| 120 |
} |
| 121 |
|
| 122 |
Ed448Point Ed448Point::base_point() { |
| 123 |
constexpr std::array<const uint64_t, WORDS_448> x = {0x2626a82bc70cc05e, |
| 124 |
0x433b80e18b00938e, |
| 125 |
0x12ae1af72ab66511, |
| 126 |
0xea6de324a3d3a464, |
| 127 |
0x9e146570470f1767, |
| 128 |
0x221d15a622bf36da, |
| 129 |
0x4f1970c66bed0ded}; |
| 130 |
constexpr std::array<const uint64_t, WORDS_448> y = {0x9808795bf230fa14, |
| 131 |
0xfdbd132c4ed7c8ad, |
| 132 |
0x3ad3ff1ce67c39c4, |
| 133 |
0x87789c1e05a0c2d7, |
| 134 |
0x4bea73736ca39840, |
| 135 |
0x8876203756c9c762, |
| 136 |
0x693f46716eb6bc24}; |
| 137 |
return Ed448Point(Gf448Elem(x), Gf448Elem(y)); |
| 138 |
} |
| 139 |
|
| 140 |
std::array<uint8_t, ED448_LEN> Ed448Point::encode() const { |
| 141 |
std::array<uint8_t, ED448_LEN> res_buf = {0}; |
| 142 |
|
| 143 |
// RFC 8032 5.2.2 |
| 144 |
// All values are coded as octet strings, and integers are coded using |
| 145 |
// little-endian convention. [...] |
| 146 |
// First, encode the y-coordinate as a little-endian string of 57 octets. |
| 147 |
// The final octet is always zero. |
| 148 |
y().to_bytes(std::span(res_buf).first<56>()); |
| 149 |
|
| 150 |
// To form the encoding of the point, copy the least significant bit of |
| 151 |
// the x-coordinate to the most significant bit of the final octet. |
| 152 |
res_buf.back() = (static_cast<uint8_t>(x().is_odd()) << 7); |
| 153 |
|
| 154 |
return res_buf; |
| 155 |
} |
| 156 |
|
| 157 |
Ed448Point Ed448Point::operator+(const Ed448Point& other) const { |
| 158 |
// RFC 8032 5.2.4. - Point Addition (Add) |
| 159 |
const Gf448Elem A = m_z * other.m_z; |
| 160 |
const Gf448Elem B = square(A); |
| 161 |
const Gf448Elem C = m_x * other.m_x; |
| 162 |
const Gf448Elem D = m_y * other.m_y; |
| 163 |
const Gf448Elem E = -mul_a24(C * D); |
| 164 |
const Gf448Elem F = B - E; |
| 165 |
const Gf448Elem G = B + E; |
| 166 |
const Gf448Elem H = (m_x + m_y) * (other.m_x + other.m_y); |
| 167 |
const Gf448Elem X3 = A * F * (H - C - D); |
| 168 |
const Gf448Elem Y3 = A * G * (D - C); |
| 169 |
const Gf448Elem Z3 = F * G; |
| 170 |
|
| 171 |
return Ed448Point(X3, Y3, Z3); |
| 172 |
} |
| 173 |
|
| 174 |
Ed448Point Ed448Point::double_point() const { |
| 175 |
// RFC 8032 5.2.4. - Point Addition (Double) |
| 176 |
const Gf448Elem B = square(m_x + m_y); |
| 177 |
const Gf448Elem C = square(m_x); |
| 178 |
const Gf448Elem D = square(m_y); |
| 179 |
const Gf448Elem E = C + D; |
| 180 |
const Gf448Elem H = square(m_z); |
| 181 |
const Gf448Elem J = E - (H + H); |
| 182 |
const Gf448Elem X3 = (B - E) * J; |
| 183 |
const Gf448Elem Y3 = E * (C - D); |
| 184 |
const Gf448Elem Z3 = E * J; |
| 185 |
|
| 186 |
return Ed448Point(X3, Y3, Z3); |
| 187 |
} |
| 188 |
|
| 189 |
Ed448Point Ed448Point::scalar_mul(const Scalar448& s) const { |
| 190 |
// 4-bit windowed scalar multiplication. |
| 191 |
std::array<Ed448Point, 16> table = {Ed448Point::identity(), |
| 192 |
*this, |
| 193 |
Ed448Point::identity(), |
| 194 |
Ed448Point::identity(), |
| 195 |
Ed448Point::identity(), |
| 196 |
Ed448Point::identity(), |
| 197 |
Ed448Point::identity(), |
| 198 |
Ed448Point::identity(), |
| 199 |
Ed448Point::identity(), |
| 200 |
Ed448Point::identity(), |
| 201 |
Ed448Point::identity(), |
| 202 |
Ed448Point::identity(), |
| 203 |
Ed448Point::identity(), |
| 204 |
Ed448Point::identity(), |
| 205 |
Ed448Point::identity(), |
| 206 |
Ed448Point::identity()}; |
| 207 |
|
| 208 |
for(size_t i = 2; i < 16; ++i) { |
| 209 |
if(i % 2 == 0) { |
| 210 |
table[i] = table[i / 2].double_point(); |
| 211 |
} else { |
| 212 |
table[i] = table[i - 1] + *this; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
// Process 448 bits (446-bit scalar + 2 leading zero bits) in 112 4-bit windows |
| 217 |
auto res = Ed448Point::identity(); |
| 218 |
|
| 219 |
for(int window = 111; window >= 0; --window) { |
| 220 |
// Double 4 times |
| 221 |
res = res.double_point(); |
| 222 |
res = res.double_point(); |
| 223 |
res = res.double_point(); |
| 224 |
res = res.double_point(); |
| 225 |
|
| 226 |
// Extract 4-bit window value. Bits at position >= 446 are zero. |
| 227 |
const uint64_t w = s.get_window(static_cast<size_t>(window) * 4, 4); |
| 228 |
|
| 229 |
// Constant-time table lookup |
| 230 |
auto selected = Ed448Point::identity(); |
| 231 |
for(size_t i = 0; i < 16; ++i) { |
| 232 |
const auto correct_idx = CT::Mask<uint64_t>::is_equal(static_cast<uint64_t>(i), w); |
| 233 |
selected.ct_conditional_assign(correct_idx, table[i]); |
| 234 |
} |
| 235 |
|
| 236 |
res = res + selected; |
| 237 |
} |
| 238 |
|
| 239 |
return res; |
| 240 |
} |
| 241 |
|
| 242 |
Ed448Point Ed448Point::base_point_mul(const Scalar448& scalar) { |
| 243 |
/* |
| 244 |
Fixed base point multiplication |
| 245 |
|
| 246 |
Same idea as base point multiply used in pcurves |
| 247 |
*/ |
| 248 |
constexpr size_t W = 4; |
| 249 |
constexpr size_t WindowElements = (1 << W) - 1; // 15 |
| 250 |
constexpr size_t Windows = (448 + W - 1) / W; // 112 |
| 251 |
constexpr size_t TableSize = Windows * WindowElements; // 1680 |
| 252 |
|
| 253 |
static const auto table = []() { |
| 254 |
std::vector<Ed448Point> tbl(TableSize, Ed448Point::identity()); |
| 255 |
|
| 256 |
auto accum = Ed448Point::base_point(); |
| 257 |
|
| 258 |
for(size_t i = 0; i < TableSize; i += WindowElements) { |
| 259 |
tbl[i] = accum; |
| 260 |
|
| 261 |
for(size_t j = 1; j < WindowElements; ++j) { |
| 262 |
if(j % 2 == 1) { |
| 263 |
tbl[i + j] = tbl[i + j / 2].double_point(); |
| 264 |
} else { |
| 265 |
tbl[i + j] = tbl[i + j - 1] + tbl[i]; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
accum = tbl[i + (WindowElements / 2)].double_point(); |
| 270 |
} |
| 271 |
|
| 272 |
return tbl; |
| 273 |
}(); |
| 274 |
|
| 275 |
auto res = Ed448Point::identity(); |
| 276 |
|
| 277 |
for(size_t i = 0; i != Windows; ++i) { |
| 278 |
const uint8_t w = static_cast<uint8_t>(scalar.get_window(i * W, W)); |
| 279 |
|
| 280 |
// Constant-time table lookup from this window's 15-entry subtable |
| 281 |
auto selected = Ed448Point::identity(); |
| 282 |
for(size_t j = 0; j != WindowElements; ++j) { |
| 283 |
const auto assign = CT::Mask<uint64_t>::is_equal(j + 1, w); |
| 284 |
selected.ct_conditional_assign(assign, table[i * WindowElements + j]); |
| 285 |
} |
| 286 |
|
| 287 |
res = res + selected; |
| 288 |
} |
| 289 |
|
| 290 |
return res; |
| 291 |
} |
| 292 |
|
| 293 |
Ed448Point Ed448Point::double_scalar_mul_vartime(const Scalar448& s1, |
| 294 |
const Ed448Point& p1, |
| 295 |
const Scalar448& s2, |
| 296 |
const Ed448Point& p2) { |
| 297 |
// 2-bit 2-ary Shamir's trick (variable time) |
| 298 |
// Process 2 bits from each scalar per iteration, using a 16-entry table. |
| 299 |
// table[w1 | (w2 << 2)] = w1*p1 + w2*p2, for w1,w2 in 0..3. |
| 300 |
|
| 301 |
// Precompute small multiples of each point |
| 302 |
const auto p1x2 = p1.double_point(); |
| 303 |
const auto p1x3 = p1x2 + p1; |
| 304 |
const auto p2x2 = p2.double_point(); |
| 305 |
const auto p2x3 = p2x2 + p2; |
| 306 |
|
| 307 |
// Build table indexed by (w2 << 2) | w1, excluding identity at index 0 |
| 308 |
const std::array<Ed448Point, 15> table = { |
| 309 |
p1, // 1*p1 + 0*p2 |
| 310 |
p1x2, // 2*p1 + 0*p2 |
| 311 |
p1x3, // 3*p1 + 0*p2 |
| 312 |
p2, // 0*p1 + 1*p2 |
| 313 |
p1 + p2, // 1*p1 + 1*p2 |
| 314 |
p1x2 + p2, // 2*p1 + 1*p2 |
| 315 |
p1x3 + p2, // 3*p1 + 1*p2 |
| 316 |
p2x2, // 0*p1 + 2*p2 |
| 317 |
p1 + p2x2, // 1*p1 + 2*p2 |
| 318 |
p1x2 + p2x2, // 2*p1 + 2*p2 |
| 319 |
p1x3 + p2x2, // 3*p1 + 2*p2 |
| 320 |
p2x3, // 0*p1 + 3*p2 |
| 321 |
p1 + p2x3, // 1*p1 + 3*p2 |
| 322 |
p1x2 + p2x3, // 2*p1 + 3*p2 |
| 323 |
p1x3 + p2x3, // 3*p1 + 3*p2 |
| 324 |
}; |
| 325 |
|
| 326 |
auto res = Ed448Point::identity(); |
| 327 |
|
| 328 |
// 446 bits / 2 = 223 windows, covering bit positions 0..445 |
| 329 |
for(int window = 222; window >= 0; --window) { |
| 330 |
res = res.double_point(); |
| 331 |
res = res.double_point(); |
| 332 |
|
| 333 |
const size_t bit_pos = static_cast<size_t>(window) * 2; |
| 334 |
const size_t idx = s1.get_window(bit_pos, 2) | (s2.get_window(bit_pos, 2) << 2); |
| 335 |
|
| 336 |
if(idx > 0) { |
| 337 |
res = res + table[idx - 1]; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
return res; |
| 342 |
} |
| 343 |
|
| 344 |
bool Ed448Point::operator==(const Ed448Point& other) const { |
| 345 |
// Compare in projective coordinates: (X1:Y1:Z1) == (X2:Y2:Z2) |
| 346 |
// iff X1*Z2 == X2*Z1 && Y1*Z2 == Y2*Z1 |
| 347 |
// This avoids two field inversions that x() and y() would require. |
| 348 |
const auto lhs_x = m_x * other.m_z; |
| 349 |
const auto rhs_x = other.m_x * m_z; |
| 350 |
const auto lhs_y = m_y * other.m_z; |
| 351 |
const auto rhs_y = other.m_y * m_z; |
| 352 |
|
| 353 |
const auto mask_x = CT::Mask<uint8_t>::expand_bool(lhs_x == rhs_x); |
| 354 |
const auto mask_y = CT::Mask<uint8_t>::expand_bool(lhs_y == rhs_y); |
| 355 |
|
| 356 |
return (mask_x & mask_y).as_bool(); |
| 357 |
} |
| 358 |
|
| 359 |
void Ed448Point::ct_conditional_assign(CT::Mask<uint64_t> mask, const Ed448Point& other) { |
| 360 |
m_x.ct_cond_assign(mask, other.m_x); |
| 361 |
m_y.ct_cond_assign(mask, other.m_y); |
| 362 |
m_z.ct_cond_assign(mask, other.m_z); |
| 363 |
} |
| 364 |
|
| 365 |
Ed448Point operator*(const Scalar448& lhs, const Ed448Point& rhs) { |
| 366 |
return rhs.scalar_mul(lhs); |
| 367 |
} |
| 368 |
|
| 369 |
std::array<uint8_t, ED448_LEN> create_pk_from_sk(std::span<const uint8_t, ED448_LEN> sk) { |
| 370 |
// 5.2.5. Key Generation |
| 371 |
// The 57-byte public key is generated by the following steps: |
| 372 |
auto shake_xof = XOF::create_or_throw("SHAKE-256"); |
| 373 |
shake_xof->update(sk); |
| 374 |
|
| 375 |
const Scalar448 s = scalar_from_xof(*shake_xof); |
| 376 |
// 3. Interpret the buffer as the little-endian integer, forming a |
| 377 |
// secret scalar s. Perform a known-base-point scalar |
| 378 |
// multiplication [s]B. |
| 379 |
return Ed448Point::base_point_mul(s).encode(); |
| 380 |
} |
| 381 |
|
| 382 |
std::array<uint8_t, 2 * ED448_LEN> sign_message(std::span<const uint8_t, ED448_LEN> sk, |
| 383 |
std::span<const uint8_t, ED448_LEN> pk, |
| 384 |
bool pgflag, |
| 385 |
std::span<const uint8_t> context, |
| 386 |
std::span<const uint8_t> msg) { |
| 387 |
// 5.2.6. Signature Generation |
| 388 |
// The inputs to the signing procedure is the private key, a 57-octet |
| 389 |
// string, a flag F, which is 0 for Ed448, 1 for Ed448ph, context C of |
| 390 |
// at most 255 octets, and a message M of arbitrary size. |
| 391 |
// 1. Hash the private key, 57 octets, using SHAKE256(x, 114). Let h |
| 392 |
// denote the resulting digest. Construct the secret scalar s from |
| 393 |
// the first half of the digest, and the corresponding public key A, |
| 394 |
// as described in the previous section. Let prefix denote the |
| 395 |
// second half of the hash digest, h[57],...,h[113]. |
| 396 |
auto shake_xof = XOF::create_or_throw("SHAKE-256"); |
| 397 |
shake_xof->update(sk); |
| 398 |
const Scalar448 s = scalar_from_xof(*shake_xof); |
| 399 |
std::array<uint8_t, ED448_LEN> prefix{}; |
| 400 |
shake_xof->output(prefix); |
| 401 |
// 2. Compute SHAKE256(dom4(F, C) || prefix || PH(M), 114), where M is |
| 402 |
// the message to be signed, F is 1 for Ed448ph, 0 for Ed448, and C |
| 403 |
// is the context to use. Interpret the 114-octet digest as a |
| 404 |
// little-endian integer r. |
| 405 |
const Scalar448 r(shake(pgflag, context, prefix, msg)); |
| 406 |
// 3. Compute the point [r]B. For efficiency, do this by first |
| 407 |
// reducing r modulo L, the group order of B. Let the string R be |
| 408 |
// the encoding of this point. |
| 409 |
const auto big_r = Ed448Point::base_point_mul(r).encode(); |
| 410 |
// 4. Compute SHAKE256(dom4(F, C) || R || A || PH(M), 114), and |
| 411 |
// interpret the 114-octet digest as a little-endian integer k. |
| 412 |
const Scalar448 k(shake(pgflag, context, big_r, pk, msg)); |
| 413 |
// 5. Compute S = (r + k * s) mod L. For efficiency, again reduce k |
| 414 |
// modulo L first. |
| 415 |
const auto big_s = r + k * s; //r_plus_ks_mod_L(r, k, s); |
| 416 |
// 6. Form the signature of the concatenation of R (57 octets) and the |
| 417 |
// little-endian encoding of S (57 octets; the ten most significant |
| 418 |
// bits of the final octets are always zero). |
| 419 |
std::array<uint8_t, 2 * ED448_LEN> sig{}; |
| 420 |
BufferStuffer stuf(sig); |
| 421 |
stuf.append(big_r); |
| 422 |
stuf.append(big_s.to_bytes<ED448_LEN>()); |
| 423 |
BOTAN_ASSERT(stuf.full(), "Buffer is full"); |
| 424 |
|
| 425 |
return sig; |
| 426 |
} |
| 427 |
|
| 428 |
bool verify_signature(std::span<const uint8_t, ED448_LEN> pk, |
| 429 |
bool phflag, |
| 430 |
std::span<const uint8_t> context, |
| 431 |
std::span<const uint8_t> sig, |
| 432 |
std::span<const uint8_t> msg) { |
| 433 |
// RFC 8032 5.2.7. Verify |
| 434 |
// 1. To verify a signature on a message M using context C and public |
| 435 |
// key A, with F being 0 for Ed448 and 1 for Ed448ph, first split |
| 436 |
// the signature into two 57-octet halves. Decode the first half as |
| 437 |
// a point R, and the second half as an integer S, in the range 0 <= |
| 438 |
// s < L. Decode the public key A as point A’. If any of the |
| 439 |
// decodings fail (including S being out of range), the signature is |
| 440 |
// invalid. |
| 441 |
if(sig.size() != 2 * ED448_LEN) { |
| 442 |
// Wrong signature size |
| 443 |
throw Decoding_Error("Ed448 signature has wrong size"); |
| 444 |
} |
| 445 |
const auto [big_r_bytes, big_s_bytes] = split(sig.first<2 * ED448_LEN>()); |
| 446 |
const auto big_r = Ed448Point::decode(big_r_bytes); |
| 447 |
if(!Scalar448::bytes_are_reduced(big_s_bytes)) { |
| 448 |
// S not in range 0 <= s < L |
| 449 |
throw Decoding_Error("Ed448 signature has invalid S"); |
| 450 |
} |
| 451 |
const Scalar448 big_s(big_s_bytes); |
| 452 |
// 2. Compute SHAKE256(dom4(F, C) || R || A || PH(M), 114), and |
| 453 |
// interpret the 114-octet digest as a little-endian integer k. |
| 454 |
const Scalar448 k(shake(phflag, context, big_r_bytes, pk, msg)); |
| 455 |
// 3. Check the group equation [4][S]B = [4]R + [4][k]A’. It’s |
| 456 |
// sufficient, but not required, to instead check [S]B = R + [k]A’. |
| 457 |
// Rearranged as [S]B + [k](-A’) = R, computed via Shamir’s trick. |
| 458 |
const auto neg_A = Ed448Point::decode(pk).negate(); |
| 459 |
return Ed448Point::double_scalar_mul_vartime(big_s, Ed448Point::base_point(), k, neg_A) == big_r; |
| 460 |
} |
| 461 |
|
| 462 |
} // namespace Botan |