Coverage Report

Created: 2026-09-28 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/botan/src/lib/pubkey/ed25519/ed25519.cpp
Line
Count
Source
1
/*
2
* Ed25519
3
* (C) 2017 Ribose Inc
4
*
5
* Based on the public domain code from SUPERCOP ref10 by
6
* Peter Schwabe, Daniel J. Bernstein, Niels Duif, Tanja Lange, Bo-Yin Yang
7
*
8
* Botan is released under the Simplified BSD License (see license.txt)
9
*/
10
11
#include <botan/ed25519.h>
12
13
#include <botan/hash.h>
14
#include <botan/rng.h>
15
#include <botan/internal/ct_utils.h>
16
#include <botan/internal/ed25519_internal.h>
17
18
namespace Botan {
19
20
0
void ed25519_gen_keypair(uint8_t pk[32], uint8_t sk[64], const uint8_t seed[32]) {
21
0
   uint8_t az[64];
22
23
0
   auto sha512 = HashFunction::create_or_throw("SHA-512");
24
0
   sha512->update(seed, 32);
25
0
   sha512->final(az);
26
0
   az[0] &= 248;
27
0
   az[31] &= 63;
28
0
   az[31] |= 64;
29
30
0
   ed25519_basepoint_mul(std::span<uint8_t, 32>{pk, 32}, az);
31
32
0
   copy_mem(sk, seed, 32);
33
0
   copy_mem(sk + 32, pk, 32);
34
0
}
35
36
void ed25519_sign(uint8_t sig[64],
37
                  const uint8_t m[],
38
                  size_t mlen,
39
                  const uint8_t sk[64],
40
                  const uint8_t domain_sep[],
41
0
                  size_t domain_sep_len) {
42
0
   uint8_t az[64];
43
0
   uint8_t nonce[64];
44
0
   uint8_t hram[64];
45
46
0
   auto sha512 = HashFunction::create_or_throw("SHA-512");
47
48
0
   sha512->update(sk, 32);
49
0
   sha512->final(az);
50
0
   az[0] &= 248;
51
0
   az[31] &= 63;
52
0
   az[31] |= 64;
53
54
0
   sha512->update(domain_sep, domain_sep_len);
55
0
   sha512->update(az + 32, 32);
56
0
   sha512->update(m, mlen);
57
0
   sha512->final(nonce);
58
59
0
   sc_reduce(nonce);
60
0
   ed25519_basepoint_mul(std::span<uint8_t, 32>{sig, 32}, nonce);
61
62
0
   sha512->update(domain_sep, domain_sep_len);
63
0
   sha512->update(sig, 32);
64
0
   sha512->update(sk + 32, 32);
65
0
   sha512->update(m, mlen);
66
0
   sha512->final(hram);
67
68
0
   sc_reduce(hram);
69
0
   sc_muladd(sig + 32, hram, az, nonce);
70
0
}
71
72
bool ed25519_verify(const uint8_t* m,
73
                    size_t mlen,
74
                    const uint8_t sig[64],
75
                    const uint8_t* pk,
76
                    const uint8_t domain_sep[],
77
0
                    size_t domain_sep_len) {
78
0
   if((sig[63] & 0xE0) != 0x00) {
79
0
      return false;
80
0
   }
81
82
0
   const uint64_t CURVE25519_ORDER[4] = {
83
0
      0x1000000000000000,
84
0
      0x0000000000000000,
85
0
      0x14def9dea2f79cd6,
86
0
      0x5812631a5cf5d3ed,
87
0
   };
88
89
0
   const uint64_t s[4] = {load_le<uint64_t>(sig + 32, 3),
90
0
                          load_le<uint64_t>(sig + 32, 2),
91
0
                          load_le<uint64_t>(sig + 32, 1),
92
0
                          load_le<uint64_t>(sig + 32, 0)};
93
94
   // RFC 8032 adds the requirement that we verify that s < order in
95
   // the signature; this did not exist in the original Ed25519 spec.
96
0
   for(size_t i = 0; i != 4; ++i) {
97
0
      if(s[i] > CURVE25519_ORDER[i]) {
98
0
         return false;
99
0
      }
100
0
      if(s[i] < CURVE25519_ORDER[i]) {
101
0
         break;
102
0
      }
103
0
      if(i == 3) {  // here s == order
104
0
         return false;
105
0
      }
106
0
   }
107
108
0
   uint8_t h[64];
109
0
   auto sha512 = HashFunction::create_or_throw("SHA-512");
110
111
0
   sha512->update(domain_sep, domain_sep_len);
112
0
   sha512->update(sig, 32);
113
0
   sha512->update(pk, 32);
114
0
   sha512->update(m, mlen);
115
0
   sha512->final(h);
116
0
   sc_reduce(h);
117
118
0
   return signature_check(std::span<const uint8_t, 32>{pk, 32}, h, sig, sig + 32);
119
0
}
120
121
}  // namespace Botan