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> x = GetBIGNUM(t, "X");
65   bssl::UniquePtr<BIGNUM> y = GetBIGNUM(t, "Y");
66   bssl::UniquePtr<BIGNUM> r = GetBIGNUM(t, "R");
67   bssl::UniquePtr<BIGNUM> s = GetBIGNUM(t, "S");
68   std::vector<uint8_t> digest;
69   if (!group || !x || !y || !r || !s ||
70       !t->GetBytes(&digest, "Digest")) {
71     return false;
72   }
73 
74   bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
75   bssl::UniquePtr<EC_POINT> pub_key(EC_POINT_new(group.get()));
76   bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
77   if (!key || !pub_key || !sig ||
78       !EC_KEY_set_group(key.get(), group.get()) ||
79       !EC_POINT_set_affine_coordinates_GFp(group.get(), pub_key.get(), x.get(),
80                                            y.get(), nullptr) ||
81       !EC_KEY_set_public_key(key.get(), pub_key.get()) ||
82       !BN_copy(sig->r, r.get()) ||
83       !BN_copy(sig->s, s.get())) {
84     return false;
85   }
86 
87   int ok = ECDSA_do_verify(digest.data(), digest.size(), sig.get(), key.get());
88   if (t->HasAttribute("Invalid")) {
89     if (ok) {
90       t->PrintLine("Signature was incorrectly accepted.");
91       return false;
92     }
93   } else if (!ok) {
94     t->PrintLine("Signature was incorrectly rejected.");
95     return false;
96   }
97 
98   return true;
99 }
100 
main(int argc,char * argv[])101 int main(int argc, char *argv[]) {
102   CRYPTO_library_init();
103 
104   if (argc != 2) {
105     fprintf(stderr, "%s <test file.txt>\n", argv[0]);
106     return 1;
107   }
108 
109   return FileTestMain(TestECDSASign, nullptr, argv[1]);
110 }
111