Botan  2.6.0
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  /**
42  * Create a Ed25519 Public Key.
43  * @param pub 32-byte raw public key
44  */
45  explicit Ed25519_PublicKey(const std::vector<uint8_t>& pub) : m_public(pub) {}
46 
47  /**
48  * Create a Ed25519 Public Key.
49  * @param pub 32-byte raw public key
50  */
52  m_public(pub.begin(), pub.end()) {}
53 
54  std::unique_ptr<PK_Ops::Verification>
55  create_verification_op(const std::string& params,
56  const std::string& provider) const override;
57 
58  const std::vector<uint8_t>& get_public_key() const { return m_public; }
59 
60  protected:
61  Ed25519_PublicKey() = default;
62  std::vector<uint8_t> m_public;
63  };
64 
66  public virtual Private_Key
67  {
68  public:
69  /**
70  * Construct a private key from the specified parameters.
71  * @param alg_id the X.509 algorithm identifier
72  * @param key_bits PKCS #8 structure
73  */
75  const secure_vector<uint8_t>& key_bits);
76 
77  /**
78  * Generate a private key.
79  * @param rng the RNG to use
80  */
82 
83  /**
84  * Construct a private key from the specified parameters.
85  * @param secret_key the private key
86  */
87  explicit Ed25519_PrivateKey(const secure_vector<uint8_t>& secret_key);
88 
89  const secure_vector<uint8_t>& get_private_key() const { return m_private; }
90 
91  secure_vector<uint8_t> private_key_bits() const override;
92 
93  bool check_key(RandomNumberGenerator& rng, bool strong) const override;
94 
95  std::unique_ptr<PK_Ops::Signature>
96  create_signature_op(RandomNumberGenerator& rng,
97  const std::string& params,
98  const std::string& provider) const override;
99 
100  private:
101  secure_vector<uint8_t> m_private;
102  };
103 
104 void ed25519_gen_keypair(uint8_t pk[32], uint8_t sk[64], const uint8_t seed[32]);
105 
106 void ed25519_sign(uint8_t sig[64],
107  const uint8_t msg[],
108  size_t msg_len,
109  const uint8_t sk[64]);
110 
111 bool ed25519_verify(const uint8_t msg[],
112  size_t msg_len,
113  const uint8_t sig[64],
114  const uint8_t pk[32]);
115 
116 }
117 
118 #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:62
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:58
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
size_t key_length() const override
Definition: ed25519.h:25
Ed25519_PublicKey(const secure_vector< uint8_t > &pub)
Definition: ed25519.h:51
void ed25519_sign(uint8_t sig[64], const uint8_t *m, size_t mlen, const uint8_t *sk)
Definition: ed25519.cpp:36
const secure_vector< uint8_t > & get_private_key() const
Definition: ed25519.h:89
Definition: alg_id.cpp:13
bool ed25519_verify(const uint8_t *m, size_t mlen, const uint8_t sig[64], const uint8_t *pk)
Definition: ed25519.cpp:68
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
Ed25519_PublicKey(const std::vector< uint8_t > &pub)
Definition: ed25519.h:45