aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_rsa.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/test_rsa.cpp')
-rw-r--r--src/tests/test_rsa.cpp193
1 files changed, 87 insertions, 106 deletions
diff --git a/src/tests/test_rsa.cpp b/src/tests/test_rsa.cpp
index dcc741bd2..5a9a14945 100644
--- a/src/tests/test_rsa.cpp
+++ b/src/tests/test_rsa.cpp
@@ -7,122 +7,103 @@
#include "tests.h"
#if defined(BOTAN_HAS_RSA)
+ #include <botan/rsa.h>
+ #include "test_pubkey.h"
+#endif
-#include "test_pubkey.h"
-
-#include <botan/pubkey.h>
-#include <botan/rsa.h>
-#include <botan/hex.h>
-#include <iostream>
-#include <fstream>
-
-using namespace Botan;
+namespace Botan_Tests {
namespace {
-size_t rsaes_kat(const std::string& e,
- const std::string& p,
- const std::string& q,
- const std::string& msg,
- std::string padding,
- const std::string& nonce,
- const std::string& output)
- {
- auto& rng = test_rng();
-
- RSA_PrivateKey privkey(rng, BigInt(p), BigInt(q), BigInt(e));
-
- RSA_PublicKey pubkey = privkey;
-
- if(padding == "")
- padding = "Raw";
-
- PK_Encryptor_EME enc(pubkey, padding, "base");
- PK_Decryptor_EME dec(privkey, padding);
-
- return validate_encryption(enc, dec, "RSAES/" + padding, msg, nonce, output);
- }
+#if defined(BOTAN_HAS_RSA)
-size_t rsa_sig_kat(const std::string& e,
- const std::string& p,
- const std::string& q,
- const std::string& msg,
- std::string padding,
- const std::string& nonce,
- const std::string& output)
+class RSA_ES_KAT_Tests : public PK_Encryption_Decryption_Test
{
- auto& rng = test_rng();
-
- RSA_PrivateKey privkey(rng, BigInt(p), BigInt(q), BigInt(e));
-
- RSA_PublicKey pubkey = privkey;
-
- if(padding == "")
- padding = "Raw";
-
- PK_Verifier verify(pubkey, padding);
- PK_Signer sign(privkey, padding, IEEE_1363, "base");
-
- return validate_signature(verify, sign, "RSA/" + padding, msg, rng, nonce, output);
- }
-
-size_t rsa_sig_verify(const std::string& e,
- const std::string& n,
- const std::string& msg,
- std::string padding,
- const std::string& signature)
+ public:
+ RSA_ES_KAT_Tests() : PK_Encryption_Decryption_Test(
+ "RSA",
+ Test::data_file("pubkey/rsaes.vec"),
+ {"E", "P", "Q", "Msg", "Ciphertext"},
+ {"Padding", "Nonce"})
+ {}
+
+ std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override
+ {
+ const BigInt p = get_req_bn(vars, "P");
+ const BigInt q = get_req_bn(vars, "Q");
+ const BigInt e = get_req_bn(vars, "E");
+
+ std::unique_ptr<Botan::Private_Key> key(new Botan::RSA_PrivateKey(Test::rng(), p, q, e));
+ return key;
+ }
+ };
+
+class RSA_Signature_KAT_Tests : public PK_Signature_Generation_Test
{
- BigInt e_bn(e);
- BigInt n_bn(n);
-
- RSA_PublicKey key(n_bn, e_bn);
-
- if(padding == "")
- padding = "Raw";
-
- PK_Verifier verify(key, padding);
-
- if(!verify.verify_message(hex_decode(msg), hex_decode(signature)))
- return 1;
- return 0;
- }
-
-}
-
-size_t test_rsa()
+ public:
+ RSA_Signature_KAT_Tests() : PK_Signature_Generation_Test(
+ "RSA",
+ Test::data_file("pubkey/rsa_sig.vec"),
+ {"E", "P", "Q", "Msg", "Signature"},
+ {"Padding", "Nonce"})
+ {}
+
+ std::string default_padding(const VarMap&) const override { return "Raw"; }
+
+ std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override
+ {
+ const BigInt p = get_req_bn(vars, "P");
+ const BigInt q = get_req_bn(vars, "Q");
+ const BigInt e = get_req_bn(vars, "E");
+
+ std::unique_ptr<Botan::Private_Key> key(new Botan::RSA_PrivateKey(Test::rng(), p, q, e));
+ return key;
+ }
+ };
+
+class RSA_Signature_Verify_Tests : public PK_Signature_Verification_Test
{
- std::ifstream rsa_enc(TEST_DATA_DIR_PK "/rsaes.vec");
- std::ifstream rsa_sig(TEST_DATA_DIR_PK "/rsa_sig.vec");
- std::ifstream rsa_verify(TEST_DATA_DIR_PK "/rsa_verify.vec");
-
- size_t fails = 0;
-
- fails += run_tests_bb(rsa_enc, "RSA Encryption", "Ciphertext", true,
- [](std::map<std::string, std::string> m) -> size_t
- {
- return rsaes_kat(m["E"], m["P"], m["Q"], m["Msg"],
- m["Padding"], m["Nonce"], m["Ciphertext"]);
- });
-
- fails += run_tests_bb(rsa_sig, "RSA Signature", "Signature", true,
- [](std::map<std::string, std::string> m) -> size_t
- {
- return rsa_sig_kat(m["E"], m["P"], m["Q"], m["Msg"],
- m["Padding"], m["Nonce"], m["Signature"]);
- });
+ public:
+ RSA_Signature_Verify_Tests() : PK_Signature_Verification_Test(
+ "RSA",
+ Test::data_file("pubkey/rsa_verify.vec"),
+ {"E", "N", "Msg", "Signature"},
+ {"Padding"})
+ {}
+
+ std::string default_padding(const VarMap&) const override { return "Raw"; }
+
+ std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) override
+ {
+ const BigInt n = get_req_bn(vars, "N");
+ const BigInt e = get_req_bn(vars, "E");
+
+ std::unique_ptr<Botan::Public_Key> key(new Botan::RSA_PublicKey(n, e));
+ return key;
+ }
+ };
+
+class RSA_Keygen_Tests : public PK_Key_Generation_Test
+ {
+ public:
+ std::vector<std::string> keygen_params() const override { return { "1024", "1280" }; }
- fails += run_tests_bb(rsa_verify, "RSA Verify", "Signature", true,
- [](std::map<std::string, std::string> m) -> size_t
- {
- return rsa_sig_verify(m["E"], m["N"], m["Msg"],
- m["Padding"], m["Signature"]);
- });
+ std::unique_ptr<Botan::Private_Key> make_key(Botan::RandomNumberGenerator& rng,
+ const std::string& param) const override
+ {
+ size_t bits = Botan::to_u32bit(param);
+ std::unique_ptr<Botan::Private_Key> key(new Botan::RSA_PrivateKey(rng, bits));
+ return key;
+ }
+ };
- return fails;
- }
+BOTAN_REGISTER_TEST("rsa_encrypt", RSA_ES_KAT_Tests);
+BOTAN_REGISTER_TEST("rsa_sign", RSA_Signature_KAT_Tests);
+BOTAN_REGISTER_TEST("rsa_verify", RSA_Signature_Verify_Tests);
+BOTAN_REGISTER_TEST("rsa_keygen", RSA_Keygen_Tests);
-#else
+#endif
-SKIP_TEST(rsa);
+}
-#endif // BOTAN_HAS_RSA
+}