Botan 3.14.1
Crypto and TLS for C&
ed448_internal.h
Go to the documentation of this file.
1/*
2 * Ed448 Internals
3 * (C) 2024 Jack Lloyd
4 * 2024 René Meusel, Fabian Albert - Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9#ifndef BOTAN_ED448_INTERNAL_H_
10#define BOTAN_ED448_INTERNAL_H_
11
12#include <botan/internal/ct_utils.h>
13#include <botan/internal/curve448_gf.h>
14#include <botan/internal/curve448_scalar.h>
15
16namespace Botan {
17
18constexpr size_t ED448_LEN = 57;
19
20/**
21 * @brief Representation of a point on the Ed448 curve.
22 *
23 * The point is represented in projective coordinates (X, Y, Z).
24 * All operations are constant time.
25 */
27 public:
28 /// Decode a point from its 57-byte encoding (RFC 8032 5.2.3)
29 static Ed448Point decode(std::span<const uint8_t, ED448_LEN> enc);
30
31 /// Create the curve's base point ('B' in RFC 8032 5.2)
32 static Ed448Point base_point();
33
34 /// Create a point from its projective coordinates X, Y, Z
35 Ed448Point(const Gf448Elem& x, const Gf448Elem& y, const Gf448Elem& z) : m_x(x), m_y(y), m_z(z) {}
36
37 /// Create a point from its coordinates x, y
38 Ed448Point(const Gf448Elem& x, const Gf448Elem& y) : m_x(x), m_y(y), m_z(1) {}
39
40 /// Return the identity element
42
43 /// Return true if this is the identity element
44 bool is_identity() const { return m_x.is_zero() && m_y == m_z; }
45
46 /// Encode the point to its 57-byte representation (RFC 8032 5.2.2)
47 std::array<uint8_t, ED448_LEN> encode() const;
48
49 /// Add two points (RFC 8032 5.2.4)
50 Ed448Point operator+(const Ed448Point& other) const;
51
52 /// Double a point (RFC 8032 5.2.4)
53 Ed448Point double_point() const;
54
55 /// Scalar multiplication
56 Ed448Point scalar_mul(const Scalar448& scalar) const;
57
58 /// Fixed base point scalar multiplication (precomputed table, no doublings)
59 static Ed448Point base_point_mul(const Scalar448& scalar);
60
61 /// Variable-time double scalar multiplication using Shamir's trick: [s1]P + [s2]Q
62 static Ed448Point double_scalar_mul_vartime(const Scalar448& s1,
63 const Ed448Point& p1,
64 const Scalar448& s2,
65 const Ed448Point& p2);
66
67 /// Negate the point
68 Ed448Point negate() const { return Ed448Point(-m_x, m_y, m_z); }
69
70 /// Getter for projective coordinate X
71 Gf448Elem x_proj() const { return m_x; }
72
73 /// Getter for projective coordinate Y
74 Gf448Elem y_proj() const { return m_y; }
75
76 /// Getter for projective coordinate Z
77 Gf448Elem z_proj() const { return m_z; }
78
79 /// Getter for point coordinate x
80 Gf448Elem x() const { return m_x / m_z; }
81
82 /// Getter for point coordinate y
83 Gf448Elem y() const { return m_y / m_z; }
84
85 /// Check if two points are equal (constant time)
86 bool operator==(const Ed448Point& other) const;
87
88 /// Assign other to this if @p mask is set (constant time)
89 void ct_conditional_assign(CT::Mask<uint64_t> mask, const Ed448Point& other);
90
91 private:
92 Gf448Elem m_x;
93 Gf448Elem m_y;
94 Gf448Elem m_z;
95};
96
97/// Syntax sugar for scalar multiplication
98Ed448Point operator*(const Scalar448& lhs, const Ed448Point& rhs);
99
100/**
101 * @brief Create a public key point from a secret key (RFC 8032 5.2.5)
102 */
103BOTAN_TEST_API std::array<uint8_t, ED448_LEN> create_pk_from_sk(std::span<const uint8_t, ED448_LEN> sk);
104
105/**
106 * @brief Sign a message using a keypair (RFC 8032 5.2.6)
107 *
108 * @param sk the secret key
109 * @param pk the public key
110 * @param f the prehash flag (true iff using Ed448ph)
111 * @param context the context string
112 * @param msg the message to sign
113 * @return the signature
114 */
115std::array<uint8_t, 114> sign_message(std::span<const uint8_t, ED448_LEN> sk,
116 std::span<const uint8_t, ED448_LEN> pk,
117 bool f,
118 std::span<const uint8_t> context,
119 std::span<const uint8_t> msg);
120
121/**
122 * @brief Verify a signature(RFC 8032 5.2.7)
123 *
124 * @param pk the public key
125 * @param phflag the prehash flag (true iff using Ed448ph)
126 * @param context the context string
127 * @param sig the signature
128 * @param msg the message to verify
129 *
130 * @throw Decoding_Error if the public key or signature is malformed
131 * @return true if the signature is valid
132 */
133bool verify_signature(std::span<const uint8_t, ED448_LEN> pk,
134 bool phflag,
135 std::span<const uint8_t> context,
136 std::span<const uint8_t> sig,
137 std::span<const uint8_t> msg);
138
139/**
140* Check that this is a valid public key point encoding: it must decode,
141* must not be the identity or another point of small order, and must lie
142* within the prime order subgroup
143*
144* @throw Decoding_Error if any of these checks fail
145*/
146void ed448_validate_public_key_point(std::span<const uint8_t, ED448_LEN> pk);
147
148} // namespace Botan
149
150#endif // BOTAN_ED448_INTERNAL_H_
#define BOTAN_TEST_API
Definition api.h:41
Representation of a point on the Ed448 curve.
Ed448Point negate() const
Negate the point.
Gf448Elem z_proj() const
Getter for projective coordinate Z.
Gf448Elem y() const
Getter for point coordinate y.
Gf448Elem x_proj() const
Getter for projective coordinate X.
Ed448Point(const Gf448Elem &x, const Gf448Elem &y)
Create a point from its coordinates x, y.
static Ed448Point identity()
Return the identity element.
Ed448Point(const Gf448Elem &x, const Gf448Elem &y, const Gf448Elem &z)
Create a point from its projective coordinates X, Y, Z.
bool is_identity() const
Return true if this is the identity element.
Gf448Elem y_proj() const
Getter for projective coordinate Y.
static Ed448Point decode(std::span< const uint8_t, ED448_LEN > enc)
Decode a point from its 57-byte encoding (RFC 8032 5.2.3).
static Ed448Point base_point()
Create the curve's base point ('B' in RFC 8032 5.2).
Gf448Elem x() const
Getter for point coordinate x.
static Gf448Elem zero()
Definition curve448_gf.h:59
static Gf448Elem one()
Definition curve448_gf.h:64
Representation of a scalar for X448.
BigInt operator*(const BigInt &x, const BigInt &y)
Definition big_ops3.cpp:57
void ed448_validate_public_key_point(std::span< const uint8_t, ED448_LEN > pk)
std::array< uint8_t, ED448_LEN > create_pk_from_sk(std::span< const uint8_t, ED448_LEN > sk)
Create a public key point from a secret key (RFC 8032 5.2.5).
OctetString operator+(const OctetString &k1, const OctetString &k2)
Definition symkey.cpp:99
constexpr size_t ED448_LEN
bool verify_signature(std::span< const uint8_t, ED448_LEN > pk, bool phflag, std::span< const uint8_t > context, std::span< const uint8_t > sig, std::span< const uint8_t > msg)
Verify a signature(RFC 8032 5.2.7).
bool operator==(const AlgorithmIdentifier &x, const AlgorithmIdentifier &y)
Definition alg_id.cpp:54
std::array< uint8_t, 2 *ED448_LEN > sign_message(std::span< const uint8_t, ED448_LEN > sk, std::span< const uint8_t, ED448_LEN > pk, bool pgflag, std::span< const uint8_t > context, std::span< const uint8_t > msg)
Sign a message using a keypair (RFC 8032 5.2.6).