cavp_rsa2_keygen_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_rsa2_keygen_test processes NIST CAVP RSA2 KeyGen test vector request
16 // files and emits the corresponding response.
17 
18 #include <vector>
19 
20 #include <openssl/bn.h>
21 #include <openssl/crypto.h>
22 #include <openssl/rsa.h>
23 
24 #include "../crypto/internal.h"
25 #include "../crypto/test/file_test.h"
26 #include "../crypto/test/test_util.h"
27 #include "cavp_test_util.h"
28 
29 
30 static bool TestRSA2KeyGen(FileTest *t, void *arg) {
31  std::string mod_str, table, count_str;
32  if (!t->GetInstruction(&mod_str, "mod") ||
33  !t->GetInstruction(&table, "Table for M-R Test") ||
34  table != "C.2" ||
35  !t->GetAttribute(&count_str, "N")) {
36  return false;
37  }
38 
39  printf("[mod = %s]\r\n", mod_str.c_str());
40  printf("[Table for M-R Test = %s]\r\n\r\n", table.c_str());
41 
42  size_t bits = strtoul(mod_str.c_str(), nullptr, 0);
43  size_t count = strtoul(count_str.c_str(), nullptr, 0);
44  for (size_t i = 0; i < count; i++) {
45  bssl::UniquePtr<RSA> key(RSA_new());
46  if (key == nullptr ||
47  bits == 0 ||
48  !RSA_generate_key_fips(key.get(), bits, nullptr)) {
49  return 0;
50  }
51 
52  const BIGNUM *n, *e, *d, *p, *q;
53  RSA_get0_key(key.get(), &n, &e, &d);
54  RSA_get0_factors(key.get(), &p, &q);
55  std::vector<uint8_t> n_bytes(BN_num_bytes(n)), e_bytes(BN_num_bytes(e)),
56  d_bytes((bits + 7) / 8), p_bytes(BN_num_bytes(p)),
57  q_bytes(BN_num_bytes(q));
58  if (n == NULL ||
59  BN_bn2bin(n, n_bytes.data()) != n_bytes.size() ||
60  e == NULL ||
61  BN_bn2bin(e, e_bytes.data()) != e_bytes.size() ||
62  d == NULL ||
63  !BN_bn2bin_padded(d_bytes.data(), d_bytes.size(), d) ||
64  p == NULL ||
65  BN_bn2bin(p, p_bytes.data()) != p_bytes.size() ||
66  q == NULL ||
67  BN_bn2bin(q, q_bytes.data()) != q_bytes.size()) {
68  return false;
69  }
70 
71  printf("e = %s\r\np = %s\r\nq = %s\r\nn = %s\r\nd = %s\r\n\r\n",
72  EncodeHex(e_bytes).c_str(), EncodeHex(p_bytes).c_str(),
73  EncodeHex(q_bytes).c_str(), EncodeHex(n_bytes).c_str(),
74  EncodeHex(d_bytes).c_str());
75  }
76 
77  return true;
78 }
79 
80 int cavp_rsa2_keygen_test_main(int argc, char **argv) {
81  if (argc != 2) {
82  fprintf(stderr, "usage: %s <test file>\n",
83  argv[0]);
84  return 1;
85  }
86 
88  opts.path = argv[1];
89  opts.callback = TestRSA2KeyGen;
90  opts.silent = true;
91  opts.comment_callback = EchoComment;
92  return FileTestMain(opts);
93 }
RSA_get0_key
#define RSA_get0_key
Definition: boringssl_prefix_symbols.h:2100
bn.h
FileTestMain
int FileTestMain(FileTestFunc run_test, void *arg, const char *path)
Definition: file_test.cc:399
RSA_get0_factors
#define RSA_get0_factors
Definition: boringssl_prefix_symbols.h:2098
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
cstest_report.opts
opts
Definition: cstest_report.py:81
xds_manager.p
p
Definition: xds_manager.py:60
BN_num_bytes
#define BN_num_bytes
Definition: boringssl_prefix_symbols.h:976
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
cavp_rsa2_keygen_test_main
int cavp_rsa2_keygen_test_main(int argc, char **argv)
Definition: cavp_rsa2_keygen_test.cc:80
RSA_generate_key_fips
#define RSA_generate_key_fips
Definition: boringssl_prefix_symbols.h:2092
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
cavp_test_util.h
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
bits
OPENSSL_EXPORT ASN1_BIT_STRING * bits
Definition: x509v3.h:482
crypto.h
rsa.h
arg
Definition: cmdline.cc:40
EchoComment
void EchoComment(const std::string &comment)
Definition: cavp_test_util.cc:218
BN_bn2bin
#define BN_bn2bin
Definition: boringssl_prefix_symbols.h:901
RSA_new
#define RSA_new
Definition: boringssl_prefix_symbols.h:2110
d
static const fe d
Definition: curve25519_tables.h:19
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
TestRSA2KeyGen
static bool TestRSA2KeyGen(FileTest *t, void *arg)
Definition: cavp_rsa2_keygen_test.cc:30
key
const char * key
Definition: hpack_parser_table.cc:164
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
bignum_st
Definition: bn.h:957
FileTest::Options
Definition: file_test.h:104
BN_bn2bin_padded
#define BN_bn2bin_padded
Definition: boringssl_prefix_symbols.h:902
table
uint8_t table[256]
Definition: hpack_parser.cc:456
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


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