1 /* Copyright (c) 2016, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <stdio.h>
16 
17 #include <vector>
18 
19 #include <openssl/bn.h>
20 #include <openssl/crypto.h>
21 #include <openssl/ec.h>
22 #include <openssl/ec_key.h>
23 #include <openssl/ecdh.h>
24 #include <openssl/nid.h>
25 
26 #include "../test/file_test.h"
27 
28 
GetCurve(FileTest * t,const char * key)29 static bssl::UniquePtr<EC_GROUP> GetCurve(FileTest *t, const char *key) {
30   std::string curve_name;
31   if (!t->GetAttribute(&curve_name, key)) {
32     return nullptr;
33   }
34 
35   if (curve_name == "P-224") {
36     return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp224r1));
37   }
38   if (curve_name == "P-256") {
39     return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(
40         NID_X9_62_prime256v1));
41   }
42   if (curve_name == "P-384") {
43     return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp384r1));
44   }
45   if (curve_name == "P-521") {
46     return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp521r1));
47   }
48 
49   t->PrintLine("Unknown curve '%s'", curve_name.c_str());
50   return nullptr;
51 }
52 
GetBIGNUM(FileTest * t,const char * key)53 static bssl::UniquePtr<BIGNUM> GetBIGNUM(FileTest *t, const char *key) {
54   std::vector<uint8_t> bytes;
55   if (!t->GetBytes(&bytes, key)) {
56     return nullptr;
57   }
58 
59   return bssl::UniquePtr<BIGNUM>(BN_bin2bn(bytes.data(), bytes.size(), nullptr));
60 }
61 
TestECDH(FileTest * t,void * arg)62 static bool TestECDH(FileTest *t, void *arg) {
63   bssl::UniquePtr<EC_GROUP> group = GetCurve(t, "Curve");
64   bssl::UniquePtr<BIGNUM> priv_key = GetBIGNUM(t, "Private");
65   bssl::UniquePtr<BIGNUM> x = GetBIGNUM(t, "X");
66   bssl::UniquePtr<BIGNUM> y = GetBIGNUM(t, "Y");
67   bssl::UniquePtr<BIGNUM> peer_x = GetBIGNUM(t, "PeerX");
68   bssl::UniquePtr<BIGNUM> peer_y = GetBIGNUM(t, "PeerY");
69   std::vector<uint8_t> z;
70   if (!group || !priv_key || !x || !y || !peer_x || !peer_y ||
71       !t->GetBytes(&z, "Z")) {
72     return false;
73   }
74 
75   bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
76   bssl::UniquePtr<EC_POINT> pub_key(EC_POINT_new(group.get()));
77   bssl::UniquePtr<EC_POINT> peer_pub_key(EC_POINT_new(group.get()));
78   if (!key || !pub_key || !peer_pub_key ||
79       !EC_KEY_set_group(key.get(), group.get()) ||
80       !EC_KEY_set_private_key(key.get(), priv_key.get()) ||
81       !EC_POINT_set_affine_coordinates_GFp(group.get(), pub_key.get(), x.get(),
82                                            y.get(), nullptr) ||
83       !EC_POINT_set_affine_coordinates_GFp(group.get(), peer_pub_key.get(),
84                                            peer_x.get(), peer_y.get(),
85                                            nullptr) ||
86       !EC_KEY_set_public_key(key.get(), pub_key.get()) ||
87       !EC_KEY_check_key(key.get())) {
88     return false;
89   }
90 
91   std::vector<uint8_t> actual_z;
92   // Make |actual_z| larger than expected to ensure |ECDH_compute_key| returns
93   // the right amount of data.
94   actual_z.resize(z.size() + 1);
95   int ret = ECDH_compute_key(actual_z.data(), actual_z.size(),
96                              peer_pub_key.get(), key.get(), nullptr);
97   if (ret < 0 ||
98       !t->ExpectBytesEqual(z.data(), z.size(), actual_z.data(),
99                            static_cast<size_t>(ret))) {
100     return false;
101   }
102 
103   // Test |ECDH_compute_key| truncates.
104   actual_z.resize(z.size() - 1);
105   ret = ECDH_compute_key(actual_z.data(), actual_z.size(), peer_pub_key.get(),
106                          key.get(), nullptr);
107   if (ret < 0 ||
108       !t->ExpectBytesEqual(z.data(), z.size() - 1, actual_z.data(),
109                            static_cast<size_t>(ret))) {
110     return false;
111   }
112 
113   return true;
114 }
115 
main(int argc,char * argv[])116 int main(int argc, char *argv[]) {
117   CRYPTO_library_init();
118 
119   if (argc != 2) {
120     fprintf(stderr, "%s <test file.txt>\n", argv[0]);
121     return 1;
122   }
123 
124   return FileTestMain(TestECDH, nullptr, argv[1]);
125 }
126