diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/mac/siphash/siphash.cpp | 7 | ||||
-rw-r--r-- | src/lib/prov/openssl/openssl_rc4.cpp | 6 | ||||
-rw-r--r-- | src/lib/stream/rc4/rc4.cpp | 6 | ||||
-rw-r--r-- | src/lib/stream/rc4/rc4.h | 2 | ||||
-rw-r--r-- | src/lib/stream/stream_cipher.cpp | 6 | ||||
-rw-r--r-- | src/lib/stream/stream_cipher.h | 2 | ||||
-rw-r--r-- | src/tests/test_mac.cpp | 20 | ||||
-rw-r--r-- | src/tests/test_octetstring.cpp | 168 | ||||
-rw-r--r-- | src/tests/test_oid.cpp | 115 | ||||
-rw-r--r-- | src/tests/tests.cpp | 17 | ||||
-rw-r--r-- | src/tests/tests.h | 5 |
11 files changed, 343 insertions, 11 deletions
diff --git a/src/lib/mac/siphash/siphash.cpp b/src/lib/mac/siphash/siphash.cpp index 4a9ffe8ea..cb72f771c 100644 --- a/src/lib/mac/siphash/siphash.cpp +++ b/src/lib/mac/siphash/siphash.cpp @@ -85,9 +85,7 @@ void SipHash::final_result(byte mac[]) store_le(X, mac); - m_mbuf = 0; - m_mbuf_pos = 0; - m_words = 0; + clear(); } void SipHash::key_schedule(const byte key[], size_t) @@ -105,6 +103,9 @@ void SipHash::key_schedule(const byte key[], size_t) void SipHash::clear() { m_V.clear(); + m_mbuf = 0; + m_mbuf_pos = 0; + m_words = 0; } std::string SipHash::name() const diff --git a/src/lib/prov/openssl/openssl_rc4.cpp b/src/lib/prov/openssl/openssl_rc4.cpp index 070cdb14d..d6246e4ab 100644 --- a/src/lib/prov/openssl/openssl_rc4.cpp +++ b/src/lib/prov/openssl/openssl_rc4.cpp @@ -12,6 +12,7 @@ #include <botan/internal/algo_registry.h> #include <botan/internal/openssl.h> #include <botan/parsing.h> +#include <botan/exceptn.h> #include <openssl/rc4.h> namespace Botan { @@ -46,6 +47,11 @@ class OpenSSL_RC4 : public StreamCipher explicit OpenSSL_RC4(size_t skip = 0) : m_skip(skip) { clear(); } ~OpenSSL_RC4() { clear(); } + void set_iv(const byte*, size_t) override + { + throw Exception("RC4 does not support an IV"); + } + void seek(u64bit) override { throw Exception("RC4 does not support seeking"); diff --git a/src/lib/stream/rc4/rc4.cpp b/src/lib/stream/rc4/rc4.cpp index a4dea9e2b..e5ea2e2b8 100644 --- a/src/lib/stream/rc4/rc4.cpp +++ b/src/lib/stream/rc4/rc4.cpp @@ -6,6 +6,7 @@ */ #include <botan/rc4.h> +#include <botan/exceptn.h> namespace Botan { @@ -35,6 +36,11 @@ void RC4::cipher(const byte in[], byte out[], size_t length) m_position += length; } +void RC4::set_iv(const byte*, size_t) + { + throw Exception("RC4 does not support an IV"); + } + /* * Generate cipher stream */ diff --git a/src/lib/stream/rc4/rc4.h b/src/lib/stream/rc4/rc4.h index 88798fae6..82dd6097b 100644 --- a/src/lib/stream/rc4/rc4.h +++ b/src/lib/stream/rc4/rc4.h @@ -21,6 +21,8 @@ class BOTAN_DLL RC4 final : public StreamCipher public: void cipher(const byte in[], byte out[], size_t length) override; + void set_iv(const byte iv[], size_t iv_len) override; + void clear() override; std::string name() const override; diff --git a/src/lib/stream/stream_cipher.cpp b/src/lib/stream/stream_cipher.cpp index 6f98df1fb..cd6400d8f 100644 --- a/src/lib/stream/stream_cipher.cpp +++ b/src/lib/stream/stream_cipher.cpp @@ -44,12 +44,6 @@ std::vector<std::string> StreamCipher::providers(const std::string& algo_spec) StreamCipher::StreamCipher() {} StreamCipher::~StreamCipher() {} -void StreamCipher::set_iv(const byte[], size_t iv_len) - { - if(!valid_iv_length(iv_len)) - throw Invalid_IV_Length(name(), iv_len); - } - #if defined(BOTAN_HAS_CHACHA) BOTAN_REGISTER_T_1LEN(StreamCipher, ChaCha, 20); #endif diff --git a/src/lib/stream/stream_cipher.h b/src/lib/stream/stream_cipher.h index 56bd2d5d9..e08bee0ce 100644 --- a/src/lib/stream/stream_cipher.h +++ b/src/lib/stream/stream_cipher.h @@ -67,7 +67,7 @@ class BOTAN_DLL StreamCipher : public SymmetricAlgorithm * @param iv the initialization vector * @param iv_len the length of the IV in bytes */ - virtual void set_iv(const byte[], size_t iv_len); + virtual void set_iv(const byte[], size_t iv_len) = 0; /** * @param iv_len the length of the IV in bytes diff --git a/src/tests/test_mac.cpp b/src/tests/test_mac.cpp index 8bc58e0e1..c7efb7f08 100644 --- a/src/tests/test_mac.cpp +++ b/src/tests/test_mac.cpp @@ -51,11 +51,21 @@ class Message_Auth_Tests : public Text_Based_Test result.test_eq(provider, mac->name(), algo); mac->set_key(key); - mac->update(input); result.test_eq(provider, "correct mac", mac->final(), expected); + // Test to make sure clear() resets what we need it to + mac->set_key( key ); + mac->update( "some discarded input"); + mac->clear(); + + // do the same to test verify_mac() + mac->set_key(key); + mac->update(input); + + result.test_eq(provider + " correct mac", mac->verify_mac(expected.data(), expected.size()), true); + if(input.size() > 2) { mac->set_key(key); // Poly1305 requires the re-key @@ -64,6 +74,14 @@ class Message_Auth_Tests : public Text_Based_Test mac->update(input[input.size()-1]); result.test_eq(provider, "split mac", mac->final(), expected); + + // do the same to test verify_mac() + mac->set_key(key); + mac->update(input[ 0 ]); + mac->update(&input[ 1 ], input.size() - 2); + mac->update(input[ input.size() - 1 ]); + + result.test_eq(provider + " split mac", mac->verify_mac(expected.data(), expected.size()), true); } } diff --git a/src/tests/test_octetstring.cpp b/src/tests/test_octetstring.cpp new file mode 100644 index 000000000..ff689518e --- /dev/null +++ b/src/tests/test_octetstring.cpp @@ -0,0 +1,168 @@ +/* +* (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include "tests.h" + +#include <botan/symkey.h> + +namespace Botan_Tests { + +namespace { + +using Botan::OctetString; + +Test::Result test_from_rng() + { + Test::Result result("OctetString"); + + OctetString os(Test::rng(), 32); + result.test_eq("length is 32 bytes", os.size(), 32); + + return result; + } + +Test::Result test_from_hex() + { + Test::Result result("OctetString"); + + OctetString os("0123456789ABCDEF"); + result.test_eq("length is 8 bytes", os.size(), 8); + + return result; + } + +Test::Result test_from_byte() + { + Test::Result result("OctetString"); + + auto rand_bytes = Test::rng().random_vec(8); + OctetString os(rand_bytes.data(), rand_bytes.size()); + result.test_eq("length is 8 bytes", os.size(), 8); + + return result; + } + +Test::Result test_odd_parity() + { + Test::Result result("OctetString"); + + OctetString os("FFFFFFFFFFFFFFFF"); + os.set_odd_parity(); + OctetString expected("FEFEFEFEFEFEFEFE"); + result.test_eq("odd parity set correctly", os, expected); + + OctetString os2("EFCBDA4FAA997F63"); + os2.set_odd_parity(); + OctetString expected2("EFCBDA4FAB987F62"); + result.test_eq("odd parity set correctly", os2, expected2); + + return result; + } + +Test::Result test_as_string() + { + Test::Result result("OctetString"); + + OctetString os("0123456789ABCDEF"); + result.test_eq("OctetString::as_string() returns correct string", os.as_string(), "0123456789ABCDEF"); + + return result; + } + +Test::Result test_xor() + { + Test::Result result("OctetString"); + + OctetString os1("0000000000000000"); + OctetString os2("FFFFFFFFFFFFFFFF"); + + OctetString xor_result = os1 ^ os2; + result.test_eq("OctetString XOR operations works as expected", xor_result, os2); + + xor_result = os1; + xor_result ^= os2; + result.test_eq("OctetString XOR operations works as expected", xor_result, os2); + + xor_result = os2 ^ os2; + result.test_eq("OctetString XOR operations works as expected", xor_result, os1); + + OctetString os3("0123456789ABCDEF"); + xor_result = os3 ^ os2; + OctetString expected("FEDCBA9876543210"); + result.test_eq("OctetString XOR operations works as expected", xor_result, expected); + + return result; + } + +Test::Result test_equality() + { + Test::Result result("OctetString"); + + OctetString os1("0000000000000000"); + OctetString os2("FFFFFFFFFFFFFFFF"); + + result.confirm("OctetString equality operations works as expected", os1 == os1); + result.confirm("OctetString equality operations works as expected", os2 == os2); + result.confirm("OctetString equality operations works as expected", os1 != os2); + + return result; + } + +Test::Result test_append() + { + Test::Result result("OctetString"); + + OctetString os1("0000"); + OctetString os2("FFFF"); + OctetString expected("0000FFFF"); + + OctetString append_result = os1 + os2; + + result.test_eq("OctetString append operations works as expected", append_result, expected); + + return result; + } + +class OctetString_Tests : public Test + { + public: + std::vector<Test::Result> run() override + { + std::vector<Test::Result> results; + + std::vector<std::function<Test::Result()>> fns = + { + test_from_rng, + test_from_hex, + test_from_byte, + test_odd_parity, + test_as_string, + test_xor, + test_equality, + test_append + }; + + for(size_t i = 0; i != fns.size(); ++i) + { + try + { + results.push_back(fns[ i ]()); + } + catch(std::exception& e) + { + results.push_back(Test::Result::Failure("OctetString tests " + std::to_string(i), e.what())); + } + } + + return results; + } + }; + +BOTAN_REGISTER_TEST("octetstring", OctetString_Tests); + +} + +} diff --git a/src/tests/test_oid.cpp b/src/tests/test_oid.cpp new file mode 100644 index 000000000..087fdc64f --- /dev/null +++ b/src/tests/test_oid.cpp @@ -0,0 +1,115 @@ +/* +* (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include "tests.h" + +#if defined(BOTAN_HAS_OID_LOOKUP) + #include <botan/oids.h> +#endif + +namespace Botan_Tests { + +namespace { + +#if defined(BOTAN_HAS_OID_LOOKUP) + +Test::Result test_add_have_OID() + { + Test::Result result("OID add"); + + result.test_eq("there is no OID 'botan-test-oid1'", Botan::OIDS::have_oid("botan-test-oid1"), false); + + Botan::OIDS::add_oid(Botan::OID("1.2.345.6.666"), "botan-test-oid1"); + + result.test_eq("OID 'botan-test-oid1' added successfully", Botan::OIDS::have_oid("botan-test-oid1"), true); + + result.test_eq("name of OID '1.2.345.6.666' is 'botan-test-oid1'", Botan::OIDS::name_of(Botan::OID("1.2.345.6.666"), + "botan-test-oid1"), true); + + return result; + } + +Test::Result test_add_have_OID_str() + { + Test::Result result("OID add string"); + + result.test_eq("there is no OID 'botan-test-oid2'", Botan::OIDS::have_oid("botan-test-oid2"), false); + + Botan::OIDS::add_oidstr("1.2.345.6.777", "botan-test-oid2"); + + result.test_eq("OID 'botan-test-oid2' added successfully", Botan::OIDS::have_oid("botan-test-oid2"), true); + + result.test_eq("name of OID '1.2.345.6.777' is 'botan-test-oid2'", Botan::OIDS::name_of(Botan::OID("1.2.345.6.777"), + "botan-test-oid2"), true); + return result; + } + +Test::Result test_add_and_lookup() + { + Test::Result result("OID add and lookup"); + + result.test_eq("OIDS::lookup returns empty string for non-existent OID object", + Botan::OIDS::lookup(Botan::OID("1.2.345.6.888")), std::string()); + + result.test_throws("OIDS::lookup thows for non-existent OID name", []() + { + Botan::OIDS::lookup("botan-test-oid3"); + }); + + // add oid -> string mapping + Botan::OIDS::add_oid2str(Botan::OID("1.2.345.6.888"), "botan-test-oid3"); + result.test_eq("", Botan::OIDS::lookup(Botan::OID("1.2.345.6.888")), "botan-test-oid3"); + + // still throws + result.test_throws("OIDS::lookup still throws without adding name mapping", []() + { + Botan::OIDS::lookup("botan-test-oid3"); + }); + + // add string -> oid mapping + Botan::OIDS::add_str2oid(Botan::OID("1.2.345.6.888"), "botan-test-oid3"); + Botan::OIDS::lookup("botan-test-oid3"); + + return result; + } + +class OID_Tests : public Test + { + public: + std::vector<Test::Result> run() override + { + std::vector<Test::Result> results; + + std::vector<std::function<Test::Result()>> fns = + { + test_add_have_OID, + test_add_have_OID_str, + test_add_and_lookup, + }; + + for(size_t i = 0; i != fns.size(); ++i) + { + try + { + results.push_back(fns[ i ]()); + } + catch(std::exception& e) + { + results.push_back(Test::Result::Failure("OID tests " + std::to_string(i), e.what())); + } + } + + return results; + } + }; + +BOTAN_REGISTER_TEST("oid", OID_Tests); + +#endif + +} + +} diff --git a/src/tests/tests.cpp b/src/tests/tests.cpp index aae4174b9..578418ffb 100644 --- a/src/tests/tests.cpp +++ b/src/tests/tests.cpp @@ -190,6 +190,23 @@ bool Test::Result::test_eq(const std::string& what, size_t produced, size_t expe return test_is_eq(what, produced, expected); } +bool Test::Result::test_eq(const std::string& what, OctetString produced, OctetString expected) + { + std::ostringstream out; + out << m_who << " " << what; + + if(produced == expected) + { + out << " produced expected result " << produced.as_string(); + return test_success(out.str()); + } + else + { + out << " produced unexpected result " << produced.as_string() << " expected " << expected.as_string(); + return test_failure(out.str()); + } + } + bool Test::Result::test_lt(const std::string& what, size_t produced, size_t expected) { if(produced >= expected) diff --git a/src/tests/tests.h b/src/tests/tests.h index 0016a4d90..b33b2837f 100644 --- a/src/tests/tests.h +++ b/src/tests/tests.h @@ -11,6 +11,7 @@ #include <botan/build.h> #include <botan/rng.h> #include <botan/hex.h> +#include <botan/symkey.h> #if defined(BOTAN_HAS_BIGINT) #include <botan/bigint.h> @@ -38,6 +39,8 @@ using Botan::byte; using Botan::BigInt; #endif +using Botan::OctetString; + class Test_Error : public Botan::Exception { public: @@ -175,6 +178,8 @@ class Test bool test_eq(const std::string& what, size_t produced, size_t expected); + bool test_eq(const std::string& what, OctetString produced, OctetString expected); + template<typename I1, typename I2> bool test_int_eq(I1 x, I2 y, const char* what) { |