Botan 3.14.1
Crypto and TLS for C&
x25519_scalarmult.cpp
Go to the documentation of this file.
1/*
2* (C) 2014,2018,2026 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/internal/x25519_internal.h>
8
9#include <botan/mem_ops.h>
10#include <botan/internal/ed25519_internal.h>
11#include <botan/internal/x25519_fe.h>
12#include <array>
13
14namespace Botan {
15
16namespace {
17
18// RFC 7748 Section 5 decodeScalar25519
19std::array<uint8_t, 32> x25519_clamp(std::span<const uint8_t, 32> scalar) {
20 std::array<uint8_t, 32> k{};
21 copy_mem(k, scalar);
22 k[0] &= 248;
23 k[31] &= 127;
24 k[31] |= 64;
25 return k;
26}
27
28} // namespace
29
30void x25519_scalarmult(std::span<uint8_t, 32> out,
31 std::span<const uint8_t, 32> scalar,
32 std::span<const uint8_t, 32> u) {
33 auto k = x25519_clamp(scalar);
34 x25519_ladder(out, k, u);
35 secure_scrub_memory(k.data(), k.size());
36}
37
38void x25519_basepoint(std::span<uint8_t, 32> out, std::span<const uint8_t, 32> scalar) {
39 /*
40 * Compute the public key as [k]B on the Ed25519 curve using the fixed-base
41 * tables, then map the result to Curve25519. This gives the same result as
42 * running the Montgomery ladder on u = 9 since the base points correspond
43 * under the birational map and B has order l, but is several times faster.
44 */
45 auto k = x25519_clamp(scalar);
46 const auto s = Ed25519_Scalar::from_bytes(k);
48 secure_scrub_memory(k.data(), k.size());
49}
50
51} // namespace Botan
static Ed25519_Scalar from_bytes(std::span< const uint8_t, BYTES > b)
void ed25519_basepoint_mul_to_x25519(std::span< uint8_t, 32 > out, const Ed25519_Scalar &scalar)
void x25519_scalarmult(std::span< uint8_t, 32 > out, std::span< const uint8_t, 32 > scalar, std::span< const uint8_t, 32 > u)
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
void secure_scrub_memory(void *ptr, size_t n)
Definition mem_utils.cpp:24
void x25519_basepoint(std::span< uint8_t, 32 > out, std::span< const uint8_t, 32 > scalar)
BOTAN_FORCE_INLINE void x25519_ladder(std::span< uint8_t, 32 > out, std::span< const uint8_t, 32 > k, std::span< const uint8_t, 32 > u)
Definition x25519_fe.h:345