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 "cavp_test_util.h" 27 28 29 static bool TestRSA2KeyGen(FileTest *t, void *arg) { 30 std::string mod_str, table, count_str; 31 if (!t->GetInstruction(&mod_str, "mod") || 32 !t->GetInstruction(&table, "Table for M-R Test") || 33 table != "C.2" || 34 !t->GetAttribute(&count_str, "N")) { 35 return false; 36 } 37 38 printf("[mod = %s]\r\n", mod_str.c_str()); 39 printf("[Table for M-R Test = %s]\r\n\r\n", table.c_str()); 40 41 size_t bits = strtoul(mod_str.c_str(), nullptr, 0); 42 size_t count = strtoul(count_str.c_str(), nullptr, 0); 43 for (size_t i = 0; i < count; i++) { 44 bssl::UniquePtr<RSA> key(RSA_new()); 45 if (key == nullptr || 46 bits == 0 || 47 !RSA_generate_key_fips(key.get(), bits, nullptr)) { 48 return 0; 49 } 50 51 const BIGNUM *n, *e, *d, *p, *q; 52 RSA_get0_key(key.get(), &n, &e, &d); 53 RSA_get0_factors(key.get(), &p, &q); 54 std::vector<uint8_t> n_bytes(BN_num_bytes(n)), e_bytes(BN_num_bytes(e)), 55 d_bytes((bits + 7) / 8), p_bytes(BN_num_bytes(p)), 56 q_bytes(BN_num_bytes(q)); 57 if (n == NULL || 58 BN_bn2bin(n, n_bytes.data()) != n_bytes.size() || 59 e == NULL || 60 BN_bn2bin(e, e_bytes.data()) != e_bytes.size() || 61 d == NULL || 62 !BN_bn2bin_padded(d_bytes.data(), d_bytes.size(), d) || 63 p == NULL || 64 BN_bn2bin(p, p_bytes.data()) != p_bytes.size() || 65 q == NULL || 66 BN_bn2bin(q, q_bytes.data()) != q_bytes.size()) { 67 return false; 68 } 69 70 printf("e = %s\r\np = %s\r\nq = %s\r\nn = %s\r\nd = %s\r\n\r\n", 71 EncodeHex(e_bytes.data(), e_bytes.size()).c_str(), 72 EncodeHex(p_bytes.data(), p_bytes.size()).c_str(), 73 EncodeHex(q_bytes.data(), q_bytes.size()).c_str(), 74 EncodeHex(n_bytes.data(), n_bytes.size()).c_str(), 75 EncodeHex(d_bytes.data(), d_bytes.size()).c_str()); 76 } 77 78 return true; 79 } 80 81 int cavp_rsa2_keygen_test_main(int argc, char **argv) { 82 if (argc != 2) { 83 fprintf(stderr, "usage: %s <test file>\n", 84 argv[0]); 85 return 1; 86 } 87 88 FileTest::Options opts; 89 opts.path = argv[1]; 90 opts.callback = TestRSA2KeyGen; 91 opts.silent = true; 92 opts.comment_callback = EchoComment; 93 return FileTestMain(opts); 94 } 95