diff options
author | Jack Lloyd <[email protected]> | 2018-05-29 14:53:26 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-05-29 14:53:26 -0400 |
commit | 5f26125dc579d0ee5297a0233305496365c69e63 (patch) | |
tree | b1357b3ad81717a0827af35b22ed797c2a3704d0 /src | |
parent | 48fb47c2aad3d39c51d971685b0be3f6a9292e15 (diff) |
Dedup some test code
Possible now that VarMap doesn't require access to protected functions
of Text_Based_Test
Diffstat (limited to 'src')
-rw-r--r-- | src/tests/test_rsa.cpp | 70 | ||||
-rw-r--r-- | src/tests/test_sm2.cpp | 50 |
2 files changed, 45 insertions, 75 deletions
diff --git a/src/tests/test_rsa.cpp b/src/tests/test_rsa.cpp index 8d5d34893..32e4ab9e6 100644 --- a/src/tests/test_rsa.cpp +++ b/src/tests/test_rsa.cpp @@ -18,6 +18,23 @@ namespace { #if defined(BOTAN_HAS_RSA) +std::unique_ptr<Botan::Private_Key> load_rsa_private_key(const VarMap& vars) + { + const BigInt p = vars.get_req_bn("P"); + const BigInt q = vars.get_req_bn("Q"); + const BigInt e = vars.get_req_bn("E"); + + return std::unique_ptr<Botan::Private_Key>(new Botan::RSA_PrivateKey(p, q, e)); + } + +std::unique_ptr<Botan::Public_Key> load_rsa_public_key(const VarMap& vars) + { + const BigInt n = vars.get_req_bn("N"); + const BigInt e = vars.get_req_bn("E"); + + return std::unique_ptr<Botan::Public_Key>(new Botan::RSA_PublicKey(n, e)); + } + class RSA_ES_KAT_Tests final : public PK_Encryption_Decryption_Test { public: @@ -30,12 +47,7 @@ class RSA_ES_KAT_Tests final : public PK_Encryption_Decryption_Test std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override { - const BigInt p = vars.get_req_bn("P"); - const BigInt q = vars.get_req_bn("Q"); - const BigInt e = vars.get_req_bn("E"); - - std::unique_ptr<Botan::Private_Key> key(new Botan::RSA_PrivateKey(p, q, e)); - return key; + return load_rsa_private_key(vars); } }; @@ -54,12 +66,7 @@ class RSA_Decryption_KAT_Tests final : public PK_Decryption_Test std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override { - const BigInt p = vars.get_req_bn("P"); - const BigInt q = vars.get_req_bn("Q"); - const BigInt e = vars.get_req_bn("E"); - - std::unique_ptr<Botan::Private_Key> key(new Botan::RSA_PrivateKey(p, q, e)); - return key; + return load_rsa_private_key(vars); } }; @@ -74,12 +81,7 @@ class RSA_KEM_Tests final : public PK_KEM_Test std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override { - const BigInt p = vars.get_req_bn("P"); - const BigInt q = vars.get_req_bn("Q"); - const BigInt e = vars.get_req_bn("E"); - - std::unique_ptr<Botan::Private_Key> key(new Botan::RSA_PrivateKey(p, q, e)); - return key; + return load_rsa_private_key(vars); } }; @@ -101,12 +103,7 @@ class RSA_Signature_KAT_Tests final : public PK_Signature_Generation_Test std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override { - const BigInt p = vars.get_req_bn("P"); - const BigInt q = vars.get_req_bn("Q"); - const BigInt e = vars.get_req_bn("E"); - - std::unique_ptr<Botan::Private_Key> key(new Botan::RSA_PrivateKey(p, q, e)); - return key; + return load_rsa_private_key(vars); } }; @@ -134,12 +131,7 @@ class RSA_PSS_KAT_Tests final : public PK_Signature_Generation_Test std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override { - const BigInt p = vars.get_req_bn("P"); - const BigInt q = vars.get_req_bn("Q"); - const BigInt e = vars.get_req_bn("E"); - - std::unique_ptr<Botan::Private_Key> key(new Botan::RSA_PrivateKey(p, q, e)); - return key; + return load_rsa_private_key(vars); } }; @@ -167,12 +159,7 @@ class RSA_PSS_Raw_KAT_Tests final : public PK_Signature_Generation_Test std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override { - const BigInt p = vars.get_req_bn("P"); - const BigInt q = vars.get_req_bn("Q"); - const BigInt e = vars.get_req_bn("E"); - - std::unique_ptr<Botan::Private_Key> key(new Botan::RSA_PrivateKey(p, q, e)); - return key; + return load_rsa_private_key(vars); } }; @@ -192,11 +179,7 @@ class RSA_Signature_Verify_Tests final : public PK_Signature_Verification_Test std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) override { - const BigInt n = vars.get_req_bn("N"); - const BigInt e = vars.get_req_bn("E"); - - std::unique_ptr<Botan::Public_Key> key(new Botan::RSA_PublicKey(n, e)); - return key; + return load_rsa_public_key(vars); } }; @@ -216,10 +199,7 @@ class RSA_Signature_Verify_Invalid_Tests final : public PK_Signature_NonVerifica std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) override { - const BigInt n = vars.get_req_bn("N"); - const BigInt e = vars.get_req_bn("E"); - std::unique_ptr<Botan::Public_Key> key(new Botan::RSA_PublicKey(n, e)); - return key; + return load_rsa_public_key(vars); } }; diff --git a/src/tests/test_sm2.cpp b/src/tests/test_sm2.cpp index 3a2318715..333e198b7 100644 --- a/src/tests/test_sm2.cpp +++ b/src/tests/test_sm2.cpp @@ -19,6 +19,24 @@ namespace Botan_Tests { namespace { +std::unique_ptr<Botan::Private_Key> load_sm2_private_key(const VarMap& vars) + { + // group params + const BigInt p = vars.get_req_bn("P"); + const BigInt a = vars.get_req_bn("A"); + const BigInt b = vars.get_req_bn("B"); + const BigInt xG = vars.get_req_bn("xG"); + const BigInt yG = vars.get_req_bn("yG"); + const BigInt order = vars.get_req_bn("Order"); + const BigInt cofactor = vars.get_req_bn("Cofactor"); + const BigInt x = vars.get_req_bn("x"); + + Botan::EC_Group domain(p, a, b, xG, yG, order, cofactor); + + Botan::Null_RNG null_rng; + return std::unique_ptr<Botan::Private_Key>(new Botan::SM2_Signature_PrivateKey(null_rng, domain, x)); + } + class SM2_Signature_KAT_Tests final : public PK_Signature_Generation_Test { public: @@ -41,21 +59,7 @@ class SM2_Signature_KAT_Tests final : public PK_Signature_Generation_Test std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override { - // group params - const BigInt p = vars.get_req_bn("P"); - const BigInt a = vars.get_req_bn("A"); - const BigInt b = vars.get_req_bn("B"); - const BigInt xG = vars.get_req_bn("xG"); - const BigInt yG = vars.get_req_bn("yG"); - const BigInt order = vars.get_req_bn("Order"); - const BigInt cofactor = vars.get_req_bn("Cofactor"); - const BigInt x = vars.get_req_bn("x"); - - Botan::EC_Group domain(p, a, b, xG, yG, order, cofactor); - - Botan::Null_RNG null_rng; - std::unique_ptr<Botan::Private_Key> key(new Botan::SM2_Signature_PrivateKey(null_rng, domain, x)); - return key; + return load_sm2_private_key(vars); } }; @@ -85,21 +89,7 @@ class SM2_Encryption_KAT_Tests final : public PK_Encryption_Decryption_Test std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override { - // group params - const BigInt p = vars.get_req_bn("P"); - const BigInt a = vars.get_req_bn("A"); - const BigInt b = vars.get_req_bn("B"); - const BigInt xG = vars.get_req_bn("xG"); - const BigInt yG = vars.get_req_bn("yG"); - const BigInt order = vars.get_req_bn("Order"); - const BigInt cofactor = vars.get_req_bn("Cofactor"); - const BigInt x = vars.get_req_bn("x"); - - Botan::EC_Group domain(p, a, b, xG, yG, order, cofactor); - - Botan::Null_RNG null_rng; - std::unique_ptr<Botan::Private_Key> key(new Botan::SM2_Encryption_PrivateKey(null_rng, domain, x)); - return key; + return load_sm2_private_key(vars); } }; |