aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_ed25519.cpp
blob: 85e1fbdbaa5c5ff762b979efcb1ab3aff92e9bb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* (C) 2014,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "tests.h"

#if defined(BOTAN_HAS_ED25519)
   #include "test_pubkey.h"
   #include <botan/ed25519.h>
   #include <botan/pkcs8.h>
   #include <botan/x509_key.h>
   #include <botan/data_src.h>
#endif

namespace Botan_Tests {

namespace {

#if defined(BOTAN_HAS_ED25519)

class Ed25519_Key_Validity_Tests : public PK_Key_Validity_Test
   {
   public:
      Ed25519_Key_Validity_Tests() : PK_Key_Validity_Test(
         "Ed25519",
         "pubkey/ed25519_key_valid.vec",
         "Pubkey") {}

      std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) override
         {
         const std::vector<uint8_t> pubkey = vars.get_req_bin("Pubkey");
         return std::make_unique<Botan::Ed25519_PublicKey>(pubkey);
         }
   };

class Ed25519_Verification_Tests : public PK_Signature_Verification_Test
   {
   public:
      Ed25519_Verification_Tests() : PK_Signature_Verification_Test(
         "Ed25519",
         "pubkey/ed25519_verify.vec",
         "Pubkey,Msg,Signature", "Valid") {}

      bool clear_between_callbacks() const override
         {
         return false;
         }

      std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) override
         {
         const std::vector<uint8_t> pubkey = vars.get_req_bin("Pubkey");
         return std::make_unique<Botan::Ed25519_PublicKey>(pubkey);
         }
   };

class Ed25519_Signature_Tests final : public PK_Signature_Generation_Test
   {
   public:
      Ed25519_Signature_Tests() : PK_Signature_Generation_Test(
            "Ed25519",
            "pubkey/ed25519.vec",
            "Privkey,Pubkey,Msg,Signature") {}

      bool clear_between_callbacks() const override
         {
         return false;
         }

      std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override
         {
         const std::vector<uint8_t> privkey = vars.get_req_bin("Privkey");
         const std::vector<uint8_t> pubkey = vars.get_req_bin("Pubkey");

         Botan::secure_vector<uint8_t> seed(privkey.begin(), privkey.end());

         auto key = std::make_unique<Botan::Ed25519_PrivateKey>(seed);

         if(key->get_public_key() != pubkey)
            throw Test_Error("Invalid Ed25519 key in test data");

         return key;
         }
   };

class Ed25519_Curdle_Format_Tests final : public Test
   {
   public:
      std::vector<Test::Result> run() override
         {
         // Keys from draft-ietf-curdle-pkix-04.txt
         const std::string priv_key_str =
            "-----BEGIN PRIVATE KEY-----\n"
            "MC4CAQAwBQYDK2VwBCIEINTuctv5E1hK1bbY8fdp+K06/nwoy/HU++CXqI9EdVhC\n"
            "-----END PRIVATE KEY-----\n";

         const std::string pub_key_str =
            "-----BEGIN PUBLIC KEY-----\n"
            "MCowBQYDK2VwAyEAGb9ECWmEzf6FQbrBZ9w7lshQhqowtrbLDFw4rXAxZuE=\n"
            "-----END PUBLIC KEY-----\n";

         Test::Result result("Ed25519 CURDLE format");

         Botan::DataSource_Memory priv_data(priv_key_str);
         std::unique_ptr<Botan::Private_Key> priv_key = Botan::PKCS8::load_key(priv_data);
         result.confirm("Private key loaded", priv_key != nullptr);

         Botan::DataSource_Memory pub_data(pub_key_str);
         std::unique_ptr<Botan::Public_Key> pub_key(Botan::X509::load_key(pub_data));
         result.confirm("Public key loaded", pub_key != nullptr);

         Botan::PK_Signer signer(*priv_key, Test::rng(), "Pure");
         signer.update("message");
         std::vector<uint8_t> sig = signer.signature(Test::rng());

         Botan::PK_Verifier verifier(*pub_key, "Pure");
         verifier.update("message");
         result.confirm("Signature valid", verifier.check_signature(sig));

         return std::vector<Test::Result>{result};
         }
   };

BOTAN_REGISTER_TEST("pubkey", "ed25519_key_valid", Ed25519_Key_Validity_Tests);
BOTAN_REGISTER_TEST("pubkey", "ed25519_verify", Ed25519_Verification_Tests);
BOTAN_REGISTER_TEST("pubkey", "ed25519_sign", Ed25519_Signature_Tests);
BOTAN_REGISTER_TEST("pubkey", "ed25519_curdle", Ed25519_Curdle_Format_Tests);

#endif

}

}