1 /*
2 * ECDH tests
3 *
4 * (C) 2007 Manuel Hartl (hartl@flexsecure.de)
5 *     2008 Jack Lloyd
6 *
7 * Botan is released under the Simplified BSD License (see license.txt)
8 */
9 
10 #include "tests.h"
11 
12 #if defined(BOTAN_HAS_ECDH)
13    #include <botan/pubkey.h>
14    #include <botan/ecdh.h>
15 #endif
16 
17 namespace Botan_Tests {
18 
19 namespace {
20 
21 #if defined(BOTAN_HAS_ECDH)
22 class ECDH_Unit_Tests final : public Test
23    {
24    public:
run()25       std::vector<Test::Result> run() override
26          {
27          std::vector<Test::Result> results;
28 
29          results.push_back(test_ecdh_normal_derivation());
30 
31          return results;
32          }
33    private:
34 
test_ecdh_normal_derivation()35       Test::Result test_ecdh_normal_derivation()
36          {
37          Test::Result result("ECDH key exchange");
38 
39          std::vector<std::string> params = { "secp256r1", "secp384r1", "secp521r1", "brainpool256r1" };
40 
41          for(auto const& param : params)
42             {
43             try
44                {
45                Botan::EC_Group dom_pars(param);
46                Botan::ECDH_PrivateKey private_a(Test::rng(), dom_pars);
47                Botan::ECDH_PrivateKey private_b(Test::rng(), dom_pars);
48 
49                Botan::PK_Key_Agreement ka(private_a, Test::rng(), "KDF2(SHA-512)");
50                Botan::PK_Key_Agreement kb(private_b, Test::rng(), "KDF2(SHA-512)");
51 
52                Botan::SymmetricKey alice_key = ka.derive_key(32, private_b.public_value());
53                Botan::SymmetricKey bob_key = kb.derive_key(32, private_a.public_value());
54 
55                if(!result.test_eq("same derived key", alice_key.bits_of(), bob_key.bits_of()))
56                   {
57                   result.test_note("Keys where " + alice_key.to_string() + " and " + bob_key.to_string());
58                   }
59                }
60             catch(Botan::Lookup_Error& e)
61                {
62                result.test_note("Skipping because ", e.what());
63                }
64             }
65 
66          return result;
67          }
68 
69    };
70 
71 BOTAN_REGISTER_TEST("pubkey", "ecdh_unit", ECDH_Unit_Tests);
72 
73 #endif
74 
75 }
76 
77 }
78