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 "cavp_test_util.h"
28
29 namespace {
30
31 struct TestCtx {
32 bssl::UniquePtr<RSA> key;
33 bool is_pss;
34 };
35
36 }
37
TestRSA2SigGen(FileTest * t,void * arg)38 static bool TestRSA2SigGen(FileTest *t, void *arg) {
39 TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
40
41 std::string mod_str, hash;
42 std::vector<uint8_t> msg;
43 if (!t->GetInstruction(&mod_str, "mod") ||
44 !t->GetAttribute(&hash, "SHAAlg") ||
45 !t->GetBytes(&msg, "Msg")) {
46 return false;
47 }
48
49 std::string test = t->CurrentTestToString();
50 if (t->IsAtNewInstructionBlock()) {
51 int mod_bits = strtoul(mod_str.c_str(), nullptr, 0);
52 ctx->key = bssl::UniquePtr<RSA>(RSA_new());
53 if (ctx->key == nullptr ||
54 mod_bits == 0 ||
55 !RSA_generate_key_fips(ctx->key.get(), mod_bits, nullptr)) {
56 return false;
57 }
58
59 const BIGNUM *n, *e;
60 RSA_get0_key(ctx->key.get(), &n, &e, nullptr);
61
62 std::vector<uint8_t> n_bytes(BN_num_bytes(n));
63 std::vector<uint8_t> e_bytes(BN_num_bytes(e));
64 if (!BN_bn2bin_padded(n_bytes.data(), n_bytes.size(), n) ||
65 !BN_bn2bin_padded(e_bytes.data(), e_bytes.size(), e)) {
66 return false;
67 }
68
69 printf("[mod = %s]\r\n\r\nn = %s\r\n\r\ne = %s",
70 mod_str.c_str(),
71 EncodeHex(n_bytes.data(), n_bytes.size()).c_str(),
72 EncodeHex(e_bytes.data(), e_bytes.size()).c_str());
73 test = test.substr(test.find("]") + 3);
74 }
75
76 const EVP_MD *md = EVP_get_digestbyname(hash.c_str());
77 uint8_t digest_buf[EVP_MAX_MD_SIZE];
78 std::vector<uint8_t> sig(RSA_size(ctx->key.get()));
79 unsigned digest_len;
80 size_t sig_len;
81 if (md == NULL ||
82 !EVP_Digest(msg.data(), msg.size(), digest_buf, &digest_len, md, NULL)) {
83 return false;
84 }
85
86 if (ctx->is_pss) {
87 if (!RSA_sign_pss_mgf1(ctx->key.get(), &sig_len, sig.data(), sig.size(),
88 digest_buf, digest_len, md, md, -1)) {
89 return false;
90 }
91 } else {
92 unsigned sig_len_u;
93 if (!RSA_sign(EVP_MD_type(md), digest_buf, digest_len, sig.data(),
94 &sig_len_u, ctx->key.get())) {
95 return false;
96 }
97 sig_len = sig_len_u;
98 }
99
100 printf("%sS = %s\r\n\r\n", test.c_str(),
101 EncodeHex(sig.data(), sig_len).c_str());
102 return true;
103 }
104
cavp_rsa2_siggen_test_main(int argc,char ** argv)105 int cavp_rsa2_siggen_test_main(int argc, char **argv) {
106 if (argc != 3) {
107 fprintf(stderr, "usage: %s (pkcs15|pss) <test file>\n",
108 argv[0]);
109 return 1;
110 }
111
112 TestCtx ctx;
113 if (strcmp(argv[1], "pkcs15") == 0) {
114 ctx = {nullptr, false};
115 } else if (strcmp(argv[1], "pss") == 0) {
116 ctx = {nullptr, true};
117 } else {
118 fprintf(stderr, "Unknown test type: %s\n", argv[1]);
119 return 1;
120 }
121
122 FileTest::Options opts;
123 opts.path = argv[2];
124 opts.callback = TestRSA2SigGen;
125 opts.arg = &ctx;
126 opts.silent = true;
127 opts.comment_callback = EchoComment;
128 return FileTestMain(opts);
129 }
130