Botan  2.12.1
Crypto and TLS for C++11
ed25519.h
Go to the documentation of this file.
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 #ifndef BOTAN_ED25519_H_
12 #define BOTAN_ED25519_H_
13 
14 #include <botan/pk_keys.h>
15 
16 namespace Botan {
17 
18 class BOTAN_PUBLIC_API(2,2) Ed25519_PublicKey : public virtual Public_Key
19  {
20  public:
21  std::string algo_name() const override { return "Ed25519"; }
22 
23  size_t estimated_strength() const override { return 128; }
24 
25  size_t key_length() const override { return 255; }
26 
27  bool check_key(RandomNumberGenerator& rng, bool strong) const override;
28 
29  AlgorithmIdentifier algorithm_identifier() const override;
30 
31  std::vector<uint8_t> public_key_bits() const override;
32 
33  /**
34  * Create a Ed25519 Public Key.
35  * @param alg_id the X.509 algorithm identifier
36  * @param key_bits DER encoded public key bits
37  */
39  const std::vector<uint8_t>& key_bits);
40 
41  template<typename Alloc>
42  Ed25519_PublicKey(const std::vector<uint8_t, Alloc>& pub) :
43  Ed25519_PublicKey(pub.data(), pub.size()) {}
44 
45  Ed25519_PublicKey(const uint8_t pub_key[], size_t len);
46 
47  std::unique_ptr<PK_Ops::Verification>
48  create_verification_op(const std::string& params,
49  const std::string& provider) const override;
50 
51  const std::vector<uint8_t>& get_public_key() const { return m_public; }
52 
53  protected:
54  Ed25519_PublicKey() = default;
55  std::vector<uint8_t> m_public;
56  };
57 
59  public virtual Private_Key
60  {
61  public:
62  /**
63  * Construct a private key from the specified parameters.
64  * @param alg_id the X.509 algorithm identifier
65  * @param key_bits PKCS #8 structure
66  */
68  const secure_vector<uint8_t>& key_bits);
69 
70  /**
71  * Generate a private key.
72  * @param rng the RNG to use
73  */
75 
76  /**
77  * Construct a private key from the specified parameters.
78  * @param secret_key the private key
79  */
80  explicit Ed25519_PrivateKey(const secure_vector<uint8_t>& secret_key);
81 
82  const secure_vector<uint8_t>& get_private_key() const { return m_private; }
83 
84  secure_vector<uint8_t> private_key_bits() const override;
85 
86  bool check_key(RandomNumberGenerator& rng, bool strong) const override;
87 
88  std::unique_ptr<PK_Ops::Signature>
89  create_signature_op(RandomNumberGenerator& rng,
90  const std::string& params,
91  const std::string& provider) const override;
92 
93  private:
94  secure_vector<uint8_t> m_private;
95  };
96 
97 void ed25519_gen_keypair(uint8_t pk[32], uint8_t sk[64], const uint8_t seed[32]);
98 
99 void ed25519_sign(uint8_t sig[64],
100  const uint8_t msg[],
101  size_t msg_len,
102  const uint8_t sk[64],
103  const uint8_t domain_sep[], size_t domain_sep_len);
104 
105 bool ed25519_verify(const uint8_t msg[],
106  size_t msg_len,
107  const uint8_t sig[64],
108  const uint8_t pk[32],
109  const uint8_t domain_sep[], size_t domain_sep_len);
110 
111 }
112 
113 #endif
void ed25519_gen_keypair(uint8_t *pk, uint8_t *sk, const uint8_t seed[32])
Definition: ed25519.cpp:18
std::vector< uint8_t > m_public
Definition: ed25519.h:55
std::string algo_name() const override
Definition: ed25519.h:21
size_t estimated_strength() const override
Definition: ed25519.h:23
const std::vector< uint8_t > & get_public_key() const
Definition: ed25519.h:51
int(* final)(unsigned char *, CTX *)
Ed25519_PublicKey(const std::vector< uint8_t, Alloc > &pub)
Definition: ed25519.h:42
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
size_t key_length() const override
Definition: ed25519.h:25
const secure_vector< uint8_t > & get_private_key() const
Definition: ed25519.h:82
Definition: alg_id.cpp:13
bool ed25519_verify(const uint8_t *m, size_t mlen, const uint8_t sig[64], const uint8_t *pk, const uint8_t domain_sep[], size_t domain_sep_len)
Definition: ed25519.cpp:71
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65
void ed25519_sign(uint8_t sig[64], const uint8_t m[], size_t mlen, const uint8_t sk[64], const uint8_t domain_sep[], size_t domain_sep_len)
Definition: ed25519.cpp:36