diff options
author | Simon Cogliani <[email protected]> | 2016-07-30 14:43:55 +0200 |
---|---|---|
committer | Simon Cogliani <[email protected]> | 2016-07-30 14:43:55 +0200 |
commit | 8c305d9969ed0690d5603b75fd151cb7a33138f0 (patch) | |
tree | cb040976f029793af05f4bde8ca40eaa89252fc0 /src/tests | |
parent | c006e97219dc93dd2d8dcaca5c325296e1ce3d55 (diff) | |
parent | 17677f9981005b68201653b45de42c5958f32b70 (diff) |
Merge branch 'master' into frp256v1
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/data/charset.vec | 41 | ||||
-rw-r--r-- | src/tests/test_bigint.cpp | 9 | ||||
-rw-r--r-- | src/tests/test_block.cpp | 6 | ||||
-rw-r--r-- | src/tests/test_dh.cpp | 34 | ||||
-rw-r--r-- | src/tests/test_pubkey.cpp | 25 | ||||
-rw-r--r-- | src/tests/test_utils.cpp | 125 |
6 files changed, 233 insertions, 7 deletions
diff --git a/src/tests/data/charset.vec b/src/tests/data/charset.vec new file mode 100644 index 000000000..dd64ac6e3 --- /dev/null +++ b/src/tests/data/charset.vec @@ -0,0 +1,41 @@ +[UTF16-LATIN1] + +# Botan +In = 0042006F00740061006E +Out = 426F74616E + +# Heizölrückstoßabdämpfung +In = 004800650069007A00F6006C007200FC0063006B00730074006F00DF00610062006400E4006D007000660075006E0067 +Out = 4865697AF66C72FC636B73746FDF616264E46D7066756E67 + +# ÿ@Ðé¿ã!ð +In = 00FF004000D000E900BF00E3002100F0 +Out = FF40D0E9BFE321F0 + +[UTF8-LATIN1] + +# Botan +In = 426F74616E +Out = 426F74616E + +# Heizölrückstoßabdämpfung +In = 4865697AC3B66C72C3BC636B73746FC39F616264C3A46D7066756E67 +Out = 4865697AF66C72FC636B73746FDF616264E46D7066756E67 + +# ÿ@Ðé¿ã!ð +In = C3BF40C390C3A9C2BFC3A321C3B0 +Out = FF40D0E9BFE321F0 + +[LATIN1-UTF8] + +# Botan +In = 426F74616E +Out = 426F74616E + +# Heizölrückstoßabdämpfung +In = 4865697AF66C72FC636B73746FDF616264E46D7066756E67 +Out = 4865697AC3B66C72C3BC636B73746FC39F616264C3A46D7066756E67 + +# ÿ@Ðé¿ã!ð +In = FF40D0E9BFE321F0 +Out = C3BF40C390C3A9C2BFC3A321C3B0
\ No newline at end of file diff --git a/src/tests/test_bigint.cpp b/src/tests/test_bigint.cpp index a7e00498f..6f3d603db 100644 --- a/src/tests/test_bigint.cpp +++ b/src/tests/test_bigint.cpp @@ -303,6 +303,15 @@ class BigInt_Mod_Test : public Text_Based_Test e %= b; result.test_eq("a %= b", e, c); + // if b fits into a Botan::word test %= operator for words + if(b.bytes() <= sizeof(Botan::word)) + { + Botan::word b_word = b.word_at( 0 ); + e = a; + e %= b_word; + result.test_eq("a %= b (as word)", e, c); + } + return result; } }; diff --git a/src/tests/test_block.cpp b/src/tests/test_block.cpp index 75a460bc7..0863cb891 100644 --- a/src/tests/test_block.cpp +++ b/src/tests/test_block.cpp @@ -45,6 +45,12 @@ class Block_Cipher_Tests : public Text_Based_Test result.test_gte(provider, cipher->block_size(), 8); result.test_gte(provider, cipher->parallel_bytes(), cipher->block_size() * cipher->parallelism()); + // Test to make sure clear() resets what we need it to + cipher->set_key(Test::rng().random_vec(cipher->key_spec().minimum_keylength())); + Botan::secure_vector<byte> garbage = Test::rng().random_vec(cipher->block_size()); + cipher->encrypt(garbage); + cipher->clear(); + cipher->set_key(key); std::vector<uint8_t> buf = input; diff --git a/src/tests/test_dh.cpp b/src/tests/test_dh.cpp index 4414d2c75..e82ce522a 100644 --- a/src/tests/test_dh.cpp +++ b/src/tests/test_dh.cpp @@ -52,8 +52,41 @@ class Diffie_Hellman_KAT_Tests : public PK_Key_Agreement_Test Botan::DH_PublicKey key(grp, y); return key.public_value(); } + + std::vector<Test::Result> run_final_tests() override + { + using namespace Botan; + + Test::Result result("DH negative tests"); + + const BigInt g("2"); + const BigInt p("58458002095536094658683755258523362961421200751439456159756164191494576279467"); + const DL_Group grp(p, g); + + const Botan::BigInt x("46205663093589612668746163860870963912226379131190812163519349848291472898748"); + std::unique_ptr<Private_Key> privkey(new DH_PrivateKey(Test::rng(), grp, x)); + + std::unique_ptr<PK_Key_Agreement> kas(new PK_Key_Agreement(*privkey, "Raw")); + + result.test_throws("agreement input too big", [&kas]() + { + const BigInt too_big("584580020955360946586837552585233629614212007514394561597561641914945762794672"); + kas->derive_key(16, BigInt::encode(too_big)); + }); + + result.test_throws("agreement input too small", [&kas]() + { + const BigInt too_small("1"); + kas->derive_key(16, BigInt::encode(too_small)); + }); + + return{result}; + } + }; +BOTAN_REGISTER_TEST("dh_kat", Diffie_Hellman_KAT_Tests); + class Diffie_Hellman_Keygen_Tests : public PK_Key_Generation_Test { public: @@ -69,7 +102,6 @@ class Diffie_Hellman_Keygen_Tests : public PK_Key_Generation_Test }; -BOTAN_REGISTER_TEST("dh_kat", Diffie_Hellman_KAT_Tests); BOTAN_REGISTER_TEST("dh_keygen", Diffie_Hellman_Keygen_Tests); #endif diff --git a/src/tests/test_pubkey.cpp b/src/tests/test_pubkey.cpp index cdd0c99b0..c7bd8f932 100644 --- a/src/tests/test_pubkey.cpp +++ b/src/tests/test_pubkey.cpp @@ -197,6 +197,19 @@ PK_Encryption_Decryption_Test::run_one_test(const std::string&, const VarMap& va //std::unique_ptr<Botan::Public_Key> pubkey(Botan::X509::load_key(Botan::X509::BER_encode(*privkey))); Botan::Public_Key* pubkey = privkey.get(); + // test EME::maximum_input_size() + std::unique_ptr<Botan::EME> eme(Botan::get_eme(padding)); + + if(eme) + { + size_t max_input_size = eme->maximum_input_size(1); + result.test_eq("maximum input size( 1 ) should always return 0", max_input_size, 0); + + size_t keybits = pubkey->max_input_bits(); + max_input_size = eme->maximum_input_size(keybits); + result.test_gte("maximum input size( keybits ) > 0", max_input_size, 1); + } + for(auto&& enc_provider : possible_pk_providers()) { std::unique_ptr<Botan::PK_Encryptor> encryptor; @@ -370,7 +383,7 @@ PK_Key_Generation_Test::test_key(const std::string& algo, const Botan::Private_K Botan::DataSource_Memory data_src(Botan::X509::PEM_encode(key)); std::unique_ptr<Botan::Public_Key> loaded(Botan::X509::load_key(data_src)); - result.test_eq("recovered public key from private", loaded.get(), true); + result.confirm("recovered public key from private", loaded.get() != nullptr); result.test_eq("public key has same type", loaded->algo_name(), key.algo_name()); result.test_eq("public key passes checks", loaded->check_key(Test::rng(), false), true); } @@ -384,7 +397,7 @@ PK_Key_Generation_Test::test_key(const std::string& algo, const Botan::Private_K Botan::DataSource_Memory data_src(Botan::X509::BER_encode(key)); std::unique_ptr<Botan::Public_Key> loaded(Botan::X509::load_key(data_src)); - result.test_eq("recovered public key from private", loaded.get(), true); + result.confirm("recovered public key from private", loaded.get() != nullptr); result.test_eq("public key has same type", loaded->algo_name(), key.algo_name()); result.test_eq("public key passes checks", loaded->check_key(Test::rng(), false), true); } @@ -399,7 +412,7 @@ PK_Key_Generation_Test::test_key(const std::string& algo, const Botan::Private_K std::unique_ptr<Botan::Private_Key> loaded( Botan::PKCS8::load_key(data_src, Test::rng())); - result.test_eq("recovered private key from PEM blob", loaded.get(), true); + result.confirm("recovered private key from PEM blob", loaded.get() != nullptr); result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name()); result.test_eq("private key passes checks", loaded->check_key(Test::rng(), false), true); } @@ -413,7 +426,7 @@ PK_Key_Generation_Test::test_key(const std::string& algo, const Botan::Private_K Botan::DataSource_Memory data_src(Botan::PKCS8::BER_encode(key)); std::unique_ptr<Botan::Public_Key> loaded(Botan::PKCS8::load_key(data_src, Test::rng())); - result.test_eq("recovered public key from private", loaded.get(), true); + result.confirm("recovered public key from private", loaded.get() != nullptr); result.test_eq("public key has same type", loaded->algo_name(), key.algo_name()); result.test_eq("public key passes checks", loaded->check_key(Test::rng(), false), true); } @@ -433,7 +446,7 @@ PK_Key_Generation_Test::test_key(const std::string& algo, const Botan::Private_K std::unique_ptr<Botan::Private_Key> loaded( Botan::PKCS8::load_key(data_src, Test::rng(), passphrase)); - result.test_eq("recovered private key from encrypted blob", loaded.get(), true); + result.confirm("recovered private key from encrypted blob", loaded.get() != nullptr); result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name()); result.test_eq("private key passes checks", loaded->check_key(Test::rng(), false), true); } @@ -451,7 +464,7 @@ PK_Key_Generation_Test::test_key(const std::string& algo, const Botan::Private_K std::unique_ptr<Botan::Private_Key> loaded( Botan::PKCS8::load_key(data_src, Test::rng(), passphrase)); - result.test_eq("recovered private key from BER blob", loaded.get(), true); + result.confirm("recovered private key from BER blob", loaded.get() != nullptr); result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name()); result.test_eq("private key passes checks", loaded->check_key(Test::rng(), false), true); } diff --git a/src/tests/test_utils.cpp b/src/tests/test_utils.cpp index 47f740a17..04d1092d8 100644 --- a/src/tests/test_utils.cpp +++ b/src/tests/test_utils.cpp @@ -1,5 +1,6 @@ /* * (C) 2015 Jack Lloyd +* (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -9,6 +10,7 @@ #include <botan/loadstor.h> #include <botan/calendar.h> #include <botan/internal/rounding.h> +#include <botan/charset.h> #if defined(BOTAN_HAS_BASE64_CODEC) #include <botan/base64.h> @@ -326,6 +328,129 @@ BOTAN_REGISTER_TEST("base64", Base64_Tests); #endif +class Charset_Tests : public Text_Based_Test + { + public: + Charset_Tests() : Text_Based_Test("charset.vec", + { "In","Out" }) + {} + + Test::Result run_one_test(const std::string& type, const VarMap& vars) override + { + using namespace Botan; + + Test::Result result("Charset"); + + const std::vector<byte> in = get_req_bin(vars, "In"); + const std::vector<byte> expected = get_req_bin(vars, "Out"); + + std::string converted; + if(type == "UTF16-LATIN1") + { + converted = Charset::transcode(std::string(in.begin(), in.end()), + Character_Set::LATIN1_CHARSET, Character_Set::UCS2_CHARSET); + } + else if(type == "UTF8-LATIN1") + { + converted = Charset::transcode(std::string(in.begin(), in.end()), + Character_Set::LATIN1_CHARSET, Character_Set::UTF8_CHARSET); + } + else if(type == "LATIN1-UTF8") + { + converted = Charset::transcode(std::string(in.begin(), in.end()), + Character_Set::UTF8_CHARSET, Character_Set::LATIN1_CHARSET); + } + else + { + throw Test_Error("Unexpected header '" + type + "' in charset tests"); + } + + result.test_eq("string converted successfully", std::vector<byte>(converted.begin(), converted.end()), expected); + + return result; + } + + Test::Result utf16_to_latin1_negative_tests() + { + using namespace Botan; + + Test::Result result("Charset negative tests"); + + result.test_throws("conversion fails for non-Latin1 characters", []() + { + // "abcdef�abcdef" + std::vector<byte> input = { 0x00, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00, 0x64, 0x00, 0x65, 0x00, 0x66, 0x01, + 0x78, 0x00, 0x61, 0x00, 0x62, 0x00, 0x63, 0x00, 0x64, 0x00, 0x65, 0x00, 0x66 + }; + + Charset::transcode(std::string(input.begin(), input.end()), + Character_Set::LATIN1_CHARSET, Character_Set::UCS2_CHARSET); + }); + + result.test_throws("conversion fails for UTF16 string with odd number of bytes", []() + { + std::vector<byte> input = { 0x00, 0x61, 0x00 }; + + Charset::transcode(std::string(input.begin(), input.end()), + Character_Set::LATIN1_CHARSET, Character_Set::UCS2_CHARSET); + }); + + return result; + } + + Test::Result utf8_to_latin1_negative_tests() + { + using namespace Botan; + + Test::Result result("Charset negative tests"); + + result.test_throws("conversion fails for non-Latin1 characters", []() + { + // "abcdef�abcdef" + std::vector<byte> input = { 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0xC5, + 0xB8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66 + }; + + Charset::transcode(std::string(input.begin(), input.end()), + Character_Set::LATIN1_CHARSET, Character_Set::UTF8_CHARSET); + }); + + result.test_throws("invalid utf-8 string", []() + { + // sequence truncated + std::vector<byte> input = { 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0xC5 }; + + Charset::transcode(std::string(input.begin(), input.end()), + Character_Set::LATIN1_CHARSET, Character_Set::UTF8_CHARSET); + }); + + result.test_throws("invalid utf-8 string", []() + { + std::vector<byte> input = { 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0xC8, 0xB8, 0x61 }; + + Charset::transcode(std::string(input.begin(), input.end()), + Character_Set::LATIN1_CHARSET, Character_Set::UTF8_CHARSET); + }); + + return result; + } + + std::vector<Test::Result> run_final_tests() override + { + using namespace Botan; + + Test::Result result("Charset negative tests"); + + result.merge(utf16_to_latin1_negative_tests()); + result.merge(utf8_to_latin1_negative_tests()); + + return{ result }; + } + + }; + +BOTAN_REGISTER_TEST("charset", Charset_Tests); + } } |