cavp_rsa2_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_rsa2_siggen_test processes NIST CAVP RSA2 SigGen 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/digest.h>
23 #include <openssl/rsa.h>
24 
25 #include "../crypto/internal.h"
26 #include "../crypto/test/file_test.h"
27 #include "../crypto/test/test_util.h"
28 #include "cavp_test_util.h"
29 
30 namespace {
31 
32 struct TestCtx {
33  bssl::UniquePtr<RSA> key;
34  bool is_pss;
35 };
36 
37 }
38 
39 static bool TestRSA2SigGen(FileTest *t, void *arg) {
40  TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
41 
42  std::string mod_str, hash;
43  std::vector<uint8_t> msg;
44  if (!t->GetInstruction(&mod_str, "mod") ||
45  !t->GetAttribute(&hash, "SHAAlg") ||
46  !t->GetBytes(&msg, "Msg")) {
47  return false;
48  }
49 
50  std::string test = t->CurrentTestToString();
51  if (t->IsAtNewInstructionBlock()) {
52  int mod_bits = strtoul(mod_str.c_str(), nullptr, 0);
53  ctx->key = bssl::UniquePtr<RSA>(RSA_new());
54  if (ctx->key == nullptr ||
55  mod_bits == 0 ||
56  !RSA_generate_key_fips(ctx->key.get(), mod_bits, nullptr)) {
57  return false;
58  }
59 
60  const BIGNUM *n, *e;
61  RSA_get0_key(ctx->key.get(), &n, &e, nullptr);
62 
63  std::vector<uint8_t> n_bytes(BN_num_bytes(n));
64  std::vector<uint8_t> e_bytes(BN_num_bytes(e));
65  if (!BN_bn2bin_padded(n_bytes.data(), n_bytes.size(), n) ||
66  !BN_bn2bin_padded(e_bytes.data(), e_bytes.size(), e)) {
67  return false;
68  }
69 
70  printf("[mod = %s]\r\n\r\nn = %s\r\n\r\ne = %s", mod_str.c_str(),
71  EncodeHex(n_bytes).c_str(), EncodeHex(e_bytes).c_str());
72  test = test.substr(test.find("]") + 3);
73  }
74 
75  const EVP_MD *md = EVP_get_digestbyname(hash.c_str());
76  uint8_t digest_buf[EVP_MAX_MD_SIZE];
77  std::vector<uint8_t> sig(RSA_size(ctx->key.get()));
78  unsigned digest_len;
79  size_t sig_len;
80  if (md == NULL ||
81  !EVP_Digest(msg.data(), msg.size(), digest_buf, &digest_len, md, NULL)) {
82  return false;
83  }
84 
85  if (ctx->is_pss) {
86  if (!RSA_sign_pss_mgf1(ctx->key.get(), &sig_len, sig.data(), sig.size(),
87  digest_buf, digest_len, md, md, -1)) {
88  return false;
89  }
90  } else {
91  unsigned sig_len_u;
92  if (!RSA_sign(EVP_MD_type(md), digest_buf, digest_len, sig.data(),
93  &sig_len_u, ctx->key.get())) {
94  return false;
95  }
96  sig_len = sig_len_u;
97  }
98 
99  sig.resize(sig_len);
100  printf("%sS = %s\r\n\r\n", test.c_str(), EncodeHex(sig).c_str());
101  return true;
102 }
103 
104 int cavp_rsa2_siggen_test_main(int argc, char **argv) {
105  if (argc != 3) {
106  fprintf(stderr, "usage: %s (pkcs15|pss) <test file>\n",
107  argv[0]);
108  return 1;
109  }
110 
111  TestCtx ctx;
112  if (strcmp(argv[1], "pkcs15") == 0) {
113  ctx = {nullptr, false};
114  } else if (strcmp(argv[1], "pss") == 0) {
115  ctx = {nullptr, true};
116  } else {
117  fprintf(stderr, "Unknown test type: %s\n", argv[1]);
118  return 1;
119  }
120 
122  opts.path = argv[2];
123  opts.callback = TestRSA2SigGen;
124  opts.arg = &ctx;
125  opts.silent = true;
126  opts.comment_callback = EchoComment;
127  return FileTestMain(opts);
128 }
RSA_get0_key
#define RSA_get0_key
Definition: boringssl_prefix_symbols.h:2100
bn.h
EVP_MD_type
#define EVP_MD_type
Definition: boringssl_prefix_symbols.h:1580
ctx
Definition: benchmark-async.c:30
EVP_get_digestbyname
#define EVP_get_digestbyname
Definition: boringssl_prefix_symbols.h:1726
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
RSA_size
#define RSA_size
Definition: boringssl_prefix_symbols.h:2139
test
Definition: spinlock_test.cc:36
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
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
BN_num_bytes
#define BN_num_bytes
Definition: boringssl_prefix_symbols.h:976
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
hash
uint64_t hash
Definition: ring_hash.cc:284
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
cavp_rsa2_siggen_test_main
int cavp_rsa2_siggen_test_main(int argc, char **argv)
Definition: cavp_rsa2_siggen_test.cc:104
crypto.h
rsa.h
arg
Definition: cmdline.cc:40
EchoComment
void EchoComment(const std::string &comment)
Definition: cavp_test_util.cc:218
RSA_new
#define RSA_new
Definition: boringssl_prefix_symbols.h:2110
TestRSA2SigGen
static bool TestRSA2SigGen(FileTest *t, void *arg)
Definition: cavp_rsa2_siggen_test.cc:39
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
msg
std::string msg
Definition: client_interceptors_end2end_test.cc:372
benchmark.md
md
Definition: benchmark.py:86
digest.h
key
const char * key
Definition: hpack_parser_table.cc:164
EVP_Digest
#define EVP_Digest
Definition: boringssl_prefix_symbols.h:1506
bignum_st
Definition: bn.h:957
EVP_MAX_MD_SIZE
#define EVP_MAX_MD_SIZE
Definition: digest.h:156
arg
struct arg arg
FileTest::Options
Definition: file_test.h:104
BN_bn2bin_padded
#define BN_bn2bin_padded
Definition: boringssl_prefix_symbols.h:902
RSA_sign
#define RSA_sign
Definition: boringssl_prefix_symbols.h:2136
RSA_sign_pss_mgf1
#define RSA_sign_pss_mgf1
Definition: boringssl_prefix_symbols.h:2137


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