cavp_ecdsa2_siggen_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_siggen_test processes NIST CAVP ECDSA2 SigGen and
16 // SigGenComponent test vector request files and emits the corresponding
17 // response.
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/internal.h"
30 #include "../crypto/test/file_test.h"
31 #include "../crypto/test/test_util.h"
32 #include "cavp_test_util.h"
33 
34 
35 static bool TestECDSA2SigGenImpl(FileTest *t, bool is_component) {
38  if (nid == NID_undef || md == nullptr) {
39  return false;
40  }
41  bssl::UniquePtr<BIGNUM> qx(BN_new()), qy(BN_new());
42  bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid));
43  std::vector<uint8_t> msg;
44  if (!qx || !qy || !key ||
45  !EC_KEY_generate_key_fips(key.get()) ||
48  qx.get(), qy.get(), nullptr) ||
49  !t->GetBytes(&msg, "Msg")) {
50  return false;
51  }
52 
53  uint8_t digest[EVP_MAX_MD_SIZE];
54  unsigned digest_len;
55  if (is_component) {
56  if (msg.size() != EVP_MD_size(md)) {
57  t->PrintLine("Bad input length.");
58  return false;
59  }
60  digest_len = EVP_MD_size(md);
61  OPENSSL_memcpy(digest, msg.data(), msg.size());
62  } else if (!EVP_Digest(msg.data(), msg.size(), digest, &digest_len, md,
63  nullptr)) {
64  return false;
65  }
66 
67  bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_do_sign(digest, digest_len, key.get()));
68  if (!sig) {
69  return false;
70  }
71 
72  size_t degree_len =
73  (EC_GROUP_get_degree(EC_KEY_get0_group(key.get())) + 7) / 8;
74  size_t order_len =
76  std::vector<uint8_t> qx_bytes(degree_len), qy_bytes(degree_len);
77  std::vector<uint8_t> r_bytes(order_len), s_bytes(order_len);
78  if (!BN_bn2bin_padded(qx_bytes.data(), qx_bytes.size(), qx.get()) ||
79  !BN_bn2bin_padded(qy_bytes.data(), qy_bytes.size(), qy.get()) ||
80  !BN_bn2bin_padded(r_bytes.data(), r_bytes.size(), sig->r) ||
81  !BN_bn2bin_padded(s_bytes.data(), s_bytes.size(), sig->s)) {
82  return false;
83  }
84 
85  printf("%sQx = %s\r\nQy = %s\r\nR = %s\r\nS = %s\r\n\r\n",
86  t->CurrentTestToString().c_str(), EncodeHex(qx_bytes).c_str(),
87  EncodeHex(qy_bytes).c_str(), EncodeHex(r_bytes).c_str(),
88  EncodeHex(s_bytes).c_str());
89  return true;
90 }
91 
92 static bool TestECDSA2SigGen(FileTest *t, void *arg) {
93  return TestECDSA2SigGenImpl(t, false);
94 }
95 
96 static bool TestECDSA2SigGenComponent(FileTest *t, void *arg) {
97  return TestECDSA2SigGenImpl(t, true);
98 }
99 
100 int cavp_ecdsa2_siggen_test_main(int argc, char **argv) {
101  if (argc != 3) {
102  fprintf(stderr, "usage: %s (SigGen|SigGenComponent) <test file>\n",
103  argv[0]);
104  return 1;
105  }
106 
107  static bool (*test_func)(FileTest *, void *);
108  if (strcmp(argv[1], "SigGen") == 0) {
109  test_func = TestECDSA2SigGen;
110  } else if (strcmp(argv[1], "SigGenComponent") == 0) {
111  test_func = TestECDSA2SigGenComponent;
112  } else {
113  fprintf(stderr, "Unknown test type: %s\n", argv[1]);
114  return 1;
115  }
116 
118  opts.path = argv[2];
119  opts.callback = test_func;
120  opts.silent = true;
121  opts.comment_callback = EchoComment;
122  return FileTestMain(opts);
123 }
EC_GROUP_get0_order
#define EC_GROUP_get0_order
Definition: boringssl_prefix_symbols.h:1323
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
bool
bool
Definition: setup_once.h:312
FileTestMain
int FileTestMain(FileTestFunc run_test, void *arg, const char *path)
Definition: file_test.cc:399
EC_KEY_generate_key_fips
#define EC_KEY_generate_key_fips
Definition: boringssl_prefix_symbols.h:1343
env_md_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/digest/internal.h:67
ecdsa.h
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_do_sign
#define ECDSA_do_sign
Definition: boringssl_prefix_symbols.h:1309
BN_num_bytes
#define BN_num_bytes
Definition: boringssl_prefix_symbols.h:976
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
EC_KEY_get0_group
#define EC_KEY_get0_group
Definition: boringssl_prefix_symbols.h:1344
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
TestECDSA2SigGenComponent
static bool TestECDSA2SigGenComponent(FileTest *t, void *arg)
Definition: cavp_ecdsa2_siggen_test.cc:96
EC_POINT_get_affine_coordinates_GFp
#define EC_POINT_get_affine_coordinates_GFp
Definition: boringssl_prefix_symbols.h:1379
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
EVP_MD_size
#define EVP_MD_size
Definition: boringssl_prefix_symbols.h:1579
cavp_test_util.h
GetECGroupNIDFromInstruction
int GetECGroupNIDFromInstruction(FileTest *t, const char **out_str)
Definition: cavp_test_util.cc:172
EncodeHex
std::string EncodeHex(bssl::Span< const uint8_t > in)
Definition: boringssl-with-bazel/src/crypto/test/test_util.cc:75
FileTest
Definition: file_test.h:90
OPENSSL_memcpy
static void * OPENSSL_memcpy(void *dst, const void *src, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:819
err.h
crypto.h
arg
Definition: cmdline.cc:40
EC_KEY_get0_public_key
#define EC_KEY_get0_public_key
Definition: boringssl_prefix_symbols.h:1346
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
cavp_ecdsa2_siggen_test_main
int cavp_ecdsa2_siggen_test_main(int argc, char **argv)
Definition: cavp_ecdsa2_siggen_test.cc:100
EC_GROUP_get_degree
#define EC_GROUP_get_degree
Definition: boringssl_prefix_symbols.h:1328
TestECDSA2SigGenImpl
static bool TestECDSA2SigGenImpl(FileTest *t, bool is_component)
Definition: cavp_ecdsa2_siggen_test.cc:35
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
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
EVP_MAX_MD_SIZE
#define EVP_MAX_MD_SIZE
Definition: digest.h:156
FileTest::Options
Definition: file_test.h:104
BN_bn2bin_padded
#define BN_bn2bin_padded
Definition: boringssl_prefix_symbols.h:902
TestECDSA2SigGen
static bool TestECDSA2SigGen(FileTest *t, void *arg)
Definition: cavp_ecdsa2_siggen_test.cc:92
BN_new
#define BN_new
Definition: boringssl_prefix_symbols.h:971


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