1 /* Copyright (c) 2017, 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 // cavp_ecdsa2_pkv_test processes a NIST CAVP ECDSA2 PKV test vector request file
16 // and emits the corresponding response.
17
18 #include <vector>
19
20 #include <openssl/bn.h>
21 #include <openssl/crypto.h>
22 #include <openssl/ec_key.h>
23 #include <openssl/err.h>
24 #include <openssl/nid.h>
25
26 #include "../crypto/test/file_test.h"
27 #include "cavp_test_util.h"
28
29
TestECDSA2PKV(FileTest * t,void * arg)30 static bool TestECDSA2PKV(FileTest *t, void *arg) {
31 int nid = GetECGroupNIDFromInstruction(t);
32 if (nid == NID_undef) {
33 return false;
34 }
35 bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid));
36 bssl::UniquePtr<BIGNUM> qx = GetBIGNUM(t, "Qx");
37 bssl::UniquePtr<BIGNUM> qy = GetBIGNUM(t, "Qy");
38 if (!key || !qx || !qy) {
39 return false;
40 }
41
42 if (EC_KEY_set_public_key_affine_coordinates(key.get(), qx.get(), qy.get())) {
43 printf("%sResult = P\r\n\r\n", t->CurrentTestToString().c_str());
44 } else {
45 char buf[256];
46 ERR_error_string_n(ERR_get_error(), buf, sizeof(buf));
47 printf("%sResult = F (%s)\r\n\r\n", t->CurrentTestToString().c_str(), buf);
48 }
49 ERR_clear_error();
50 return true;
51 }
52
cavp_ecdsa2_pkv_test_main(int argc,char ** argv)53 int cavp_ecdsa2_pkv_test_main(int argc, char **argv) {
54 if (argc != 2) {
55 fprintf(stderr, "usage: %s <test file>\n",
56 argv[0]);
57 return 1;
58 }
59
60 FileTest::Options opts;
61 opts.path = argv[1];
62 opts.callback = TestECDSA2PKV;
63 opts.silent = true;
64 opts.comment_callback = EchoComment;
65 return FileTestMain(opts);
66 }
67