diff options
Diffstat (limited to 'src')
-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_octetstring.cpp | 168 | ||||
-rw-r--r-- | src/tests/tests.cpp | 17 | ||||
-rw-r--r-- | src/tests/tests.h | 5 |
8 files changed, 205 insertions, 7 deletions
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_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/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) { |