Debian Codegraph @

src/lib/pubkey/curve448/ed448/ed448_internal.h

raw
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
16 namespace Botan {
17
18 constexpr 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 */
26 class BOTAN_TEST_API Ed448Point final {
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
41 static Ed448Point identity() { return Ed448Point(Gf448Elem::zero(), Gf448Elem::one()); }
42
43 /// Encode the point to its 57-byte representation (RFC 8032 5.2.2)
44 std::array<uint8_t, ED448_LEN> encode() const;
45
46 /// Add two points (RFC 8032 5.2.4)
47 Ed448Point operator+(const Ed448Point& other) const;
48
49 /// Double a point (RFC 8032 5.2.4)
50 Ed448Point double_point() const;
51
52 /// Scalar multiplication
53 Ed448Point scalar_mul(const Scalar448& scalar) const;
54
55 /// Fixed base point scalar multiplication (precomputed table, no doublings)
56 static Ed448Point base_point_mul(const Scalar448& scalar);
57
58 /// Variable-time double scalar multiplication using Shamir's trick: [s1]P + [s2]Q
59 static Ed448Point double_scalar_mul_vartime(const Scalar448& s1,
60 const Ed448Point& p1,
61 const Scalar448& s2,
62 const Ed448Point& p2);
63
64 /// Negate the point
65 Ed448Point negate() const { return Ed448Point(-m_x, m_y, m_z); }
66
67 /// Getter for projective coordinate X
68 Gf448Elem x_proj() const { return m_x; }
69
70 /// Getter for projective coordinate Y
71 Gf448Elem y_proj() const { return m_y; }
72
73 /// Getter for projective coordinate Z
74 Gf448Elem z_proj() const { return m_z; }
75
76 /// Getter for point coordinate x
77 Gf448Elem x() const { return m_x / m_z; }
78
79 /// Getter for point coordinate y
80 Gf448Elem y() const { return m_y / m_z; }
81
82 /// Check if two points are equal (constant time)
83 bool operator==(const Ed448Point& other) const;
84
85 /// Assign other to this if @p mask is set (constant time)
86 void ct_conditional_assign(CT::Mask<uint64_t> mask, const Ed448Point& other);
87
88 private:
89 Gf448Elem m_x;
90 Gf448Elem m_y;
91 Gf448Elem m_z;
92 };
93
94 /// Syntax sugar for scalar multiplication
95 Ed448Point operator*(const Scalar448& lhs, const Ed448Point& rhs);
96
97 /**
98 * @brief Create a public key point from a secret key (RFC 8032 5.2.5)
99 */
100 BOTAN_TEST_API std::array<uint8_t, ED448_LEN> create_pk_from_sk(std::span<const uint8_t, ED448_LEN> sk);
101
102 /**
103 * @brief Sign a message using a keypair (RFC 8032 5.2.6)
104 *
105 * @param sk the secret key
106 * @param pk the public key
107 * @param f the prehash flag (true iff using Ed448ph)
108 * @param context the context string
109 * @param msg the message to sign
110 * @return the signature
111 */
112 std::array<uint8_t, 114> sign_message(std::span<const uint8_t, ED448_LEN> sk,
113 std::span<const uint8_t, ED448_LEN> pk,
114 bool f,
115 std::span<const uint8_t> context,
116 std::span<const uint8_t> msg);
117
118 /**
119 * @brief Verify a signature(RFC 8032 5.2.7)
120 *
121 * @param pk the public key
122 * @param phflag the prehash flag (true iff using Ed448ph)
123 * @param context the context string
124 * @param sig the signature
125 * @param msg the message to verify
126 *
127 * @throw Decoding_Error if the public key or signature is malformed
128 * @return true if the signature is valid
129 */
130 bool verify_signature(std::span<const uint8_t, ED448_LEN> pk,
131 bool phflag,
132 std::span<const uint8_t> context,
133 std::span<const uint8_t> sig,
134 std::span<const uint8_t> msg);
135
136 } // namespace Botan
137
138 #endif // BOTAN_ED448_INTERNAL_H_