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 
TestRSA2SigGen(FileTest * t,void * arg)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 
cavp_rsa2_siggen_test_main(int argc,char ** argv)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 
121   FileTest::Options opts;
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 }
129