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 
TestRSA2KeyGen(FileTest * t,void * arg)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 
cavp_rsa2_keygen_test_main(int argc,char ** argv)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 
87   FileTest::Options opts;
88   opts.path = argv[1];
89   opts.callback = TestRSA2KeyGen;
90   opts.silent = true;
91   opts.comment_callback = EchoComment;
92   return FileTestMain(opts);
93 }
94