blob: 94018540546e32917906cfc1e7322e8e4fe78100 [file] [log] [blame]
Robert Sloan2424d842017-05-01 07:46:28 -07001/* 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_sigver_test processes a NIST CAVP ECDSA2 SigVer test vector
16// request file and emits the corresponding response. An optional sample vector
17// file can be passed to verify the result.
18
19#include <vector>
20
21#include <openssl/bn.h>
22#include <openssl/crypto.h>
23#include <openssl/digest.h>
24#include <openssl/ec_key.h>
25#include <openssl/ecdsa.h>
26#include <openssl/err.h>
27#include <openssl/nid.h>
28
29#include "../crypto/test/file_test.h"
30#include "cavp_test_util.h"
31
32
33static bool TestECDSA2SigVer(FileTest *t, void *arg) {
34 int nid = GetECGroupNIDFromInstruction(t);
35 const EVP_MD *md = GetDigestFromInstruction(t);
36 if (nid == NID_undef || md == nullptr) {
37 return false;
38 }
39 bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
40 bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid));
41 bssl::UniquePtr<BIGNUM> qx = GetBIGNUM(t, "Qx");
42 bssl::UniquePtr<BIGNUM> qy = GetBIGNUM(t, "Qy");
43 bssl::UniquePtr<BIGNUM> r = GetBIGNUM(t, "R");
44 bssl::UniquePtr<BIGNUM> s = GetBIGNUM(t, "S");
45 std::vector<uint8_t> msg;
46 uint8_t digest[EVP_MAX_MD_SIZE];
47 unsigned digest_len;
48 if (!sig || !key || !qx || !qy || !r || !s ||
49 !EC_KEY_set_public_key_affine_coordinates(key.get(), qx.get(),
50 qy.get()) ||
51 !t->GetBytes(&msg, "Msg") ||
52 !EVP_Digest(msg.data(), msg.size(), digest, &digest_len, md, nullptr)) {
53 return false;
54 }
55
56 BN_free(sig->r);
57 sig->r = r.release();
58 BN_free(sig->s);
59 sig->s = s.release();
60
61 if (ECDSA_do_verify(digest, digest_len, sig.get(), key.get())) {
62 printf("%sResult = P\r\n\r\n", t->CurrentTestToString().c_str());
63 } else {
64 char buf[256];
65 ERR_error_string_n(ERR_get_error(), buf, sizeof(buf));
66 printf("%sResult = F (%s)\r\n\r\n", t->CurrentTestToString().c_str(), buf);
67 }
68 ERR_clear_error();
69 return true;
70}
71
72int main(int argc, char **argv) {
73 CRYPTO_library_init();
74
75 if (argc != 2) {
76 fprintf(stderr, "usage: %s <test file>\n",
77 argv[0]);
78 return 1;
79 }
80
81 printf("# Generated by");
82 for (int i = 0; i < argc; i++) {
83 printf(" %s", argv[i]);
84 }
85 printf("\r\n\r\n");
86
87 return FileTestMainSilent(TestECDSA2SigVer, nullptr, argv[1]);
88}