aboutsummaryrefslogtreecommitdiffstats
path: root/checks/ecdh.cpp
blob: c641796d65ac69f7bd08511d6fa2c3372ff6d0b2 (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
/*
* ECDH tests
*
* (C) 2007 Manuel Hartl (hartl@flexsecure.de)
*     2008 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/build.h>

#include "validate.h"
#include "common.h"

#if defined(BOTAN_HAS_ECDH)

#include <iostream>
#include <fstream>

#include <botan/pubkey.h>
#include <botan/ecdh.h>
#include <botan/x509self.h>
#include <botan/der_enc.h>

using namespace Botan;

#define CHECK_MESSAGE(expr, print) try { if(!(expr)) std::cout << print << "\n"; } catch(std::exception& e) { std::cout << __FUNCTION__ << ": " << e.what() << "\n"; }
#define CHECK(expr) try { if(!(expr)) std::cout << #expr << "\n"; } catch(std::exception& e) { std::cout << __FUNCTION__ << ": " << e.what() << "\n"; }

namespace {

void test_ecdh_normal_derivation(RandomNumberGenerator& rng)
   {
   std::cout << "." << std::flush;

   EC_Group dom_pars(OID("1.3.132.0.8"));

   ECDH_PrivateKey private_a(rng, dom_pars);

   ECDH_PrivateKey private_b(rng, dom_pars); //public_a.getCurve()

   PK_Key_Agreement ka(private_a, "KDF2(SHA-1)");
   PK_Key_Agreement kb(private_b, "KDF2(SHA-1)");

   SymmetricKey alice_key = ka.derive_key(32, private_b.public_value());
   SymmetricKey bob_key = kb.derive_key(32, private_a.public_value());

   if(alice_key != bob_key)
      {
      std::cout << "The two keys didn't match!\n";
      std::cout << "Alice's key was: " << alice_key.as_string() << "\n";
      std::cout << "Bob's key was: " << bob_key.as_string() << "\n";
      }
   }

void test_ecdh_some_dp(RandomNumberGenerator& rng)
   {
   std::vector<std::string> oids;
   oids.push_back("1.2.840.10045.3.1.7");
   oids.push_back("1.3.132.0.8");
   oids.push_back("1.2.840.10045.3.1.1");

   for(u32bit i = 0; i< oids.size(); i++)
      {
      std::cout << "." << std::flush;

      OID oid(oids[i]);
      EC_Group dom_pars(oid);

      ECDH_PrivateKey private_a(rng, dom_pars);
      ECDH_PrivateKey private_b(rng, dom_pars);

      PK_Key_Agreement ka(private_a, "KDF2(SHA-1)");
      PK_Key_Agreement kb(private_b, "KDF2(SHA-1)");

      SymmetricKey alice_key = ka.derive_key(32, private_b.public_value());
      SymmetricKey bob_key = kb.derive_key(32, private_a.public_value());

      CHECK_MESSAGE(alice_key == bob_key, "different keys - " << "Alice's key was: " << alice_key.as_string() << ", Bob's key was: " << bob_key.as_string());
      }

   }

void test_ecdh_der_derivation(RandomNumberGenerator& rng)
   {
   std::vector<std::string> oids;
   oids.push_back("1.2.840.10045.3.1.7");
   oids.push_back("1.3.132.0.8");
   oids.push_back("1.2.840.10045.3.1.1");

   for(u32bit i = 0; i< oids.size(); i++)
      {
      OID oid(oids[i]);
      EC_Group dom_pars(oid);

      ECDH_PrivateKey private_a(rng, dom_pars);
      ECDH_PrivateKey private_b(rng, dom_pars);

      MemoryVector<byte> key_a = private_a.public_value();
      MemoryVector<byte> key_b = private_b.public_value();

      PK_Key_Agreement ka(private_a, "KDF2(SHA-1)");
      PK_Key_Agreement kb(private_b, "KDF2(SHA-1)");

      SymmetricKey alice_key = ka.derive_key(32, key_b);
      SymmetricKey bob_key = kb.derive_key(32, key_a);

      CHECK_MESSAGE(alice_key == bob_key, "different keys - " << "Alice's key was: " << alice_key.as_string() << ", Bob's key was: " << bob_key.as_string());
      //cout << "key: " << alice_key.as_string() << endl;
      }
   }

}

u32bit do_ecdh_tests(RandomNumberGenerator& rng)
   {
   std::cout << "Testing ECDH (InSiTo unit tests): ";

   test_ecdh_normal_derivation(rng);
   test_ecdh_some_dp(rng);
   test_ecdh_der_derivation(rng);

   std::cout << std::endl;

   return 0;
   }

#else
u32bit do_ecdh_tests(RandomNumberGenerator&) { return 0; }
#endif