diff options
author | Jack Lloyd <[email protected]> | 2018-02-20 09:32:43 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-02-20 09:32:43 -0500 |
commit | 061182a46f8b9e42808d57ec7bbafc13db7cf809 (patch) | |
tree | eb9ded78eb28e19432db65743e3a4cd27d004eb7 /src/tests/test_dl_group.cpp | |
parent | ee756051a251900708c9ed21890e99dba4d96e6d (diff) |
Improve DL_Group tests
Diffstat (limited to 'src/tests/test_dl_group.cpp')
-rw-r--r-- | src/tests/test_dl_group.cpp | 49 |
1 files changed, 47 insertions, 2 deletions
diff --git a/src/tests/test_dl_group.cpp b/src/tests/test_dl_group.cpp index 94ddf7cc2..36ae02531 100644 --- a/src/tests/test_dl_group.cpp +++ b/src/tests/test_dl_group.cpp @@ -4,10 +4,13 @@ * Botan is released under the Simplified BSD License (see license.txt) */ +#define BOTAN_NO_DEPRECATED_WARNINGS + #include "tests.h" #if defined(BOTAN_HAS_DL_GROUP) #include <botan/dl_group.h> + #include <botan/workfactor.h> #endif namespace Botan_Tests { @@ -28,11 +31,29 @@ class DL_Group_Tests final : public Test results.push_back(test_dl_encoding()); results.push_back(test_dl_named(rng)); results.push_back(test_dl_generate(rng)); + results.push_back(test_dl_errors()); return results; } private: + Test::Result test_dl_errors() + { + Test::Result result("DL_Group errors"); + result.test_throws("Uninitialized", + "DL_Group uninitialized", + []() { Botan::DL_Group dl; dl.get_p(); }); + + result.test_throws("Bad generator param", + "Invalid argument DL_Group unknown PrimeType", + []() { + auto invalid_type = static_cast<Botan::DL_Group::PrimeType>(666); + Botan::DL_Group dl(Test::rng(), invalid_type, 1024); + }); + + return result; + } + Test::Result test_dl_encoding() { Test::Result result("DL_Group encoding"); @@ -49,6 +70,10 @@ class DL_Group_Tests final : public Test result.test_eq("Same q in X9.42 decoding", group1.get_q(), orig.get_q()); result.test_eq("Same g in X9.42 decoding", group1.get_g(), orig.get_g()); + result.test_eq("PEM encodings match", + group1.PEM_encode(Botan::DL_Group::ANSI_X9_42), + Botan::DL_Group::PEM_for_named_group("modp/ietf/1024")); + Botan::DL_Group group2(pem2); result.test_eq("Same p in X9.57 decoding", group2.get_p(), orig.get_p()); @@ -76,15 +101,35 @@ class DL_Group_Tests final : public Test result.test_lte("DH g size", dh1050.get_g().bits(), 1050); result.test_eq("DH group verifies", dh1050.verify_group(rng, true), true); + Botan::DL_Group dh_implicit_q(rng, Botan::DL_Group::Prime_Subgroup, 1040); + result.test_eq("DH p size", dh_implicit_q.get_p().bits(), 1040); + result.test_eq("DH q size", dh_implicit_q.get_q().bits(), Botan::dl_exponent_size(1040)); + result.test_eq("DH group verifies", dh_implicit_q.verify_group(rng, true), true); + + Botan::DL_Group dh_strong(rng, Botan::DL_Group::Strong, 1025); + result.test_eq("DH p size", dh_strong.get_p().bits(), 1025); + result.test_eq("DH q size", dh_strong.get_q().bits(), 1024); + result.test_eq("DH group verifies", dh_strong.verify_group(rng, true), true); + #if defined(BOTAN_HAS_SHA1) Botan::DL_Group dsa1024(rng, Botan::DL_Group::DSA_Kosherizer, 1024); result.test_eq("DSA p size", dsa1024.get_p().bits(), 1024); result.test_eq("DSA q size", dsa1024.get_q().bits(), 160); result.test_lte("DSA g size", dsa1024.get_g().bits(), 1024); result.test_eq("DSA group verifies", dsa1024.verify_group(rng, true), true); -#endif -#if defined(BOTAN_HAS_SHA1) + const std::vector<uint8_t> short_seed(16); + const std::vector<uint8_t> invalid_seed(20); + const std::vector<uint8_t> working_seed = Botan::hex_decode("0000000000000000000000000000000000000021"); + + result.test_throws("DSA seed does not generate group", + "Invalid argument DL_Group: The seed given does not generate a DSA group", + [&rng,&invalid_seed]() { Botan::DL_Group dsa(rng, invalid_seed, 1024, 160); }); + + result.test_throws("DSA seed is too short", + "Invalid argument Generating a DSA parameter set with a 160 bit long q requires a seed at least as many bits long", + [&rng,&short_seed]() { Botan::DL_Group dsa(rng, short_seed, 1024, 160); }); + // From FIPS 186-3 test data const std::vector<uint8_t> seed = Botan::hex_decode("1F5DA0AF598EEADEE6E6665BF880E63D8B609BA2"); |