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/ecdsa.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>(
40         EC_GROUP_new_by_curve_name(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 
TestECDSASign(FileTest * t,void * arg)62 static bool TestECDSASign(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> k = GetBIGNUM(t, "K");
68   bssl::UniquePtr<BIGNUM> r = GetBIGNUM(t, "R");
69   bssl::UniquePtr<BIGNUM> s = GetBIGNUM(t, "S");
70   std::vector<uint8_t> digest;
71   if (!group || !priv_key || !x || !y || !k || !r || !s ||
72       !t->GetBytes(&digest, "Digest")) {
73     return false;
74   }
75 
76   bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
77   bssl::UniquePtr<EC_POINT> pub_key(EC_POINT_new(group.get()));
78   if (!key || !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_KEY_set_public_key(key.get(), pub_key.get()) ||
84       !EC_KEY_check_key(key.get())) {
85     return false;
86   }
87 
88   // |ECDSA_do_sign_ex| expects |k| to already be inverted.
89   bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
90   if (!ctx ||
91       !BN_mod_inverse(k.get(), k.get(), EC_GROUP_get0_order(group.get()),
92                       ctx.get())) {
93     return false;
94   }
95 
96   bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_do_sign_ex(digest.data(), digest.size(), k.get(),
97                                  r.get(), key.get()));
98   if (!sig) {
99     return false;
100   }
101 
102   if (BN_cmp(r.get(), sig->r) != 0 ||
103       BN_cmp(s.get(), sig->s) != 0) {
104     t->PrintLine("Signature mismatch.");
105     return false;
106   }
107 
108   return true;
109 }
110 
main(int argc,char * argv[])111 int main(int argc, char *argv[]) {
112   CRYPTO_library_init();
113 
114   if (argc != 2) {
115     fprintf(stderr, "%s <test file.txt>\n", argv[0]);
116     return 1;
117   }
118 
119   return FileTestMain(TestECDSASign, nullptr, argv[1]);
120 }
121