cavp_ecdsa2_sigver_test.cc
Go to the documentation of this file.
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_sigver_test processes a NIST CAVP ECDSA2 SigVer test vector
16 // request file and emits the corresponding response.
17 
18 #include <vector>
19 
20 #include <openssl/bn.h>
21 #include <openssl/crypto.h>
22 #include <openssl/digest.h>
23 #include <openssl/ec_key.h>
24 #include <openssl/ecdsa.h>
25 #include <openssl/err.h>
26 #include <openssl/nid.h>
27 
28 #include "../crypto/test/file_test.h"
29 #include "cavp_test_util.h"
30 
31 
32 static bool TestECDSA2SigVer(FileTest *t, void *arg) {
35  if (nid == NID_undef || md == nullptr) {
36  return false;
37  }
38  bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
39  bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid));
40  bssl::UniquePtr<BIGNUM> qx = GetBIGNUM(t, "Qx");
41  bssl::UniquePtr<BIGNUM> qy = GetBIGNUM(t, "Qy");
42  bssl::UniquePtr<BIGNUM> r = GetBIGNUM(t, "R");
43  bssl::UniquePtr<BIGNUM> s = GetBIGNUM(t, "S");
44  std::vector<uint8_t> msg;
45  uint8_t digest[EVP_MAX_MD_SIZE];
46  unsigned digest_len;
47  if (!sig || !key || !qx || !qy || !r || !s ||
49  qy.get()) ||
50  !t->GetBytes(&msg, "Msg") ||
51  !EVP_Digest(msg.data(), msg.size(), digest, &digest_len, md, nullptr)) {
52  return false;
53  }
54 
55  BN_free(sig->r);
56  sig->r = r.release();
57  BN_free(sig->s);
58  sig->s = s.release();
59 
60  if (ECDSA_do_verify(digest, digest_len, sig.get(), key.get())) {
61  printf("%sResult = P\r\n\r\n", t->CurrentTestToString().c_str());
62  } else {
63  char buf[256];
65  printf("%sResult = F (%s)\r\n\r\n", t->CurrentTestToString().c_str(), buf);
66  }
68  return true;
69 }
70 
71 int cavp_ecdsa2_sigver_test_main(int argc, char **argv) {
72  if (argc != 2) {
73  fprintf(stderr, "usage: %s <test file>\n",
74  argv[0]);
75  return 1;
76  }
77 
79  opts.path = argv[1];
80  opts.callback = TestECDSA2SigVer;
81  opts.silent = true;
82  opts.comment_callback = EchoComment;
83  return FileTestMain(opts);
84 }
bn.h
EC_KEY_new_by_curve_name
#define EC_KEY_new_by_curve_name
Definition: boringssl_prefix_symbols.h:1356
GetDigestFromInstruction
const EVP_MD * GetDigestFromInstruction(FileTest *t)
Definition: cavp_test_util.cc:198
FileTestMain
int FileTestMain(FileTestFunc run_test, void *arg, const char *path)
Definition: file_test.cc:399
env_md_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/internal.h:67
ecdsa.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
cstest_report.opts
opts
Definition: cstest_report.py:81
ECDSA_SIG_new
#define ECDSA_SIG_new
Definition: boringssl_prefix_symbols.h:1305
BN_free
#define BN_free
Definition: boringssl_prefix_symbols.h:923
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
ERR_get_error
#define ERR_get_error
Definition: boringssl_prefix_symbols.h:1419
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
cavp_test_util.h
GetECGroupNIDFromInstruction
int GetECGroupNIDFromInstruction(FileTest *t, const char **out_str)
Definition: cavp_test_util.cc:172
FileTest
Definition: file_test.h:90
err.h
crypto.h
arg
Definition: cmdline.cc:40
ec_key.h
NID_undef
#define NID_undef
Definition: nid.h:85
EchoComment
void EchoComment(const std::string &comment)
Definition: cavp_test_util.cc:218
ERR_error_string_n
#define ERR_error_string_n
Definition: boringssl_prefix_symbols.h:1416
TestECDSA2SigVer
static bool TestECDSA2SigVer(FileTest *t, void *arg)
Definition: cavp_ecdsa2_sigver_test.cc:32
ECDSA_do_verify
#define ECDSA_do_verify
Definition: boringssl_prefix_symbols.h:1310
EC_KEY_set_public_key_affine_coordinates
#define EC_KEY_set_public_key_affine_coordinates
Definition: boringssl_prefix_symbols.h:1368
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
GetBIGNUM
static bssl::UniquePtr< BIGNUM > GetBIGNUM(FileTest *t, const char *key)
Definition: ecdh_test.cc:62
nid
int nid
Definition: cipher_extra.c:71
benchmark.md
md
Definition: benchmark.py:86
nid.h
digest.h
key
const char * key
Definition: hpack_parser_table.cc:164
EVP_Digest
#define EVP_Digest
Definition: boringssl_prefix_symbols.h:1506
fix_build_deps.r
r
Definition: fix_build_deps.py:491
EVP_MAX_MD_SIZE
#define EVP_MAX_MD_SIZE
Definition: digest.h:156
cavp_ecdsa2_sigver_test_main
int cavp_ecdsa2_sigver_test_main(int argc, char **argv)
Definition: cavp_ecdsa2_sigver_test.cc:71
ERR_clear_error
#define ERR_clear_error
Definition: boringssl_prefix_symbols.h:1413
FileTest::Options
Definition: file_test.h:104


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:52