aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_ecies.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tests/test_ecies.cpp')
-rw-r--r--src/tests/test_ecies.cpp208
1 files changed, 208 insertions, 0 deletions
diff --git a/src/tests/test_ecies.cpp b/src/tests/test_ecies.cpp
index 40fb7a2dc..dea9b6266 100644
--- a/src/tests/test_ecies.cpp
+++ b/src/tests/test_ecies.cpp
@@ -1,5 +1,6 @@
/*
* (C) 2016 Philipp Weber
+* (C) 2016 Daniel Neus
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
@@ -248,6 +249,213 @@ class ECIES_Tests : public Text_Based_Test
BOTAN_REGISTER_TEST("ecies", ECIES_Tests);
+#if defined(BOTAN_HAS_KDF1_18033) && defined(BOTAN_HAS_HMAC) && defined(BOTAN_HAS_AES)
+
+Test::Result test_other_key_not_set()
+ {
+ Test::Result result("ECIES other key not set");
+
+ const Flags flags = ecies_flags(false, false, false, true);
+ const Botan::EC_Group domain("secp521r1");
+ const Botan::BigInt private_key_value("405029866705438137604064977397053031159826489755682166267763407"
+ "5002761777100287880684822948852132235484464537021197213998300006"
+ "547176718172344447619746779823");
+
+ const Botan::ECDH_PrivateKey private_key(Test::rng(), domain, private_key_value);
+ const Botan::ECIES_System_Params ecies_params(private_key.domain(), "KDF1-18033(SHA-512)", "AES-256/CBC", 32,
+ "HMAC(SHA-512)", 20, Botan::PointGFp::Compression_Type::COMPRESSED,
+ flags);
+
+ Botan::ECIES_Encryptor ecies_enc(private_key, ecies_params);
+
+ result.test_throws("encrypt not possible without setting other public key", [ &ecies_enc ]()
+ {
+ ecies_enc.encrypt(std::vector<byte>(8), Test::rng());
+ });
+
+ return result;
+ }
+
+Test::Result test_kdf_not_found()
+ {
+ Test::Result result("ECIES kdf not found");
+
+ const Flags flags = ecies_flags(false, false, false, true);
+ const Botan::EC_Group domain("secp521r1");
+ const Botan::BigInt private_key_value("405029866705438137604064977397053031159826489755682166267763407"
+ "5002761777100287880684822948852132235484464537021197213998300006"
+ "547176718172344447619746779823");
+
+ const Botan::ECDH_PrivateKey private_key(Test::rng(), domain, private_key_value);
+ const Botan::ECIES_System_Params ecies_params(private_key.domain(), "KDF-XYZ(SHA-512)", "AES-256/CBC", 32,
+ "HMAC(SHA-512)", 20, Botan::PointGFp::Compression_Type::COMPRESSED,
+ flags);
+
+ Botan::ECIES_Encryptor ecies_enc(private_key, ecies_params);
+
+ result.test_throws("kdf not found", [ &ecies_enc ]()
+ {
+ ecies_enc.encrypt(std::vector<byte>(8), Test::rng());
+ });
+
+ return result;
+ }
+
+Test::Result test_mac_not_found()
+ {
+ Test::Result result("ECIES mac not found");
+
+ const Flags flags = ecies_flags(false, false, false, true);
+ const Botan::EC_Group domain("secp521r1");
+ const Botan::BigInt private_key_value("405029866705438137604064977397053031159826489755682166267763407"
+ "5002761777100287880684822948852132235484464537021197213998300006"
+ "547176718172344447619746779823");
+
+ const Botan::ECDH_PrivateKey private_key(Test::rng(), domain, private_key_value);
+ const Botan::ECIES_System_Params ecies_params(private_key.domain(), "KDF1-18033(SHA-512)", "AES-256/CBC", 32,
+ "XYZMAC(SHA-512)", 20, Botan::PointGFp::Compression_Type::COMPRESSED,
+ flags);
+
+ Botan::ECIES_Encryptor ecies_enc(private_key, ecies_params);
+
+ result.test_throws("mac not found", [ &ecies_enc ]()
+ {
+ ecies_enc.encrypt(std::vector<byte>(8), Test::rng());
+ });
+
+ return result;
+ }
+
+Test::Result test_cipher_not_found()
+ {
+ Test::Result result("ECIES cipher not found");
+
+ const Flags flags = ecies_flags(false, false, false, true);
+ const Botan::EC_Group domain("secp521r1");
+ const Botan::BigInt private_key_value("405029866705438137604064977397053031159826489755682166267763407"
+ "5002761777100287880684822948852132235484464537021197213998300006"
+ "547176718172344447619746779823");
+
+ const Botan::ECDH_PrivateKey private_key(Test::rng(), domain, private_key_value);
+ const Botan::ECIES_System_Params ecies_params(private_key.domain(), "KDF1-18033(SHA-512)", "AES-XYZ-256/CBC", 32,
+ "HMAC(SHA-512)", 20, Botan::PointGFp::Compression_Type::COMPRESSED,
+ flags);
+
+ Botan::ECIES_Encryptor ecies_enc(private_key, ecies_params);
+
+ result.test_throws("cipher not found", [ &ecies_enc ]()
+ {
+ ecies_enc.encrypt(std::vector<byte>(8), Test::rng());
+ });
+
+ return result;
+ }
+
+Test::Result test_system_params_short_ctor()
+ {
+ Test::Result result("ECIES short system params ctor");
+
+ const Botan::EC_Group domain("secp521r1");
+ const Botan::BigInt private_key_value("405029866705438137604064977397053031159826489755682166267763407"
+ "5002761777100287880684822948852132235484464537021197213998300006"
+ "547176718172344447619746779823");
+
+ const Botan::BigInt other_private_key_value("2294226772740614508941417891614236736606752960073669253551166842"
+ "5866095315090327914760325168219669828915074071456176066304457448"
+ "25404691681749451640151380153");
+
+ const Botan::ECDH_PrivateKey private_key(Test::rng(), domain, private_key_value);
+ const Botan::ECDH_PrivateKey other_private_key(Test::rng(), domain, other_private_key_value);
+
+ const Botan::ECIES_System_Params ecies_params(private_key.domain(), "KDF1-18033(SHA-512)", "AES-256/CBC", 32,
+ "HMAC(SHA-512)", 16);
+
+ const Botan::InitializationVector iv("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
+ const std::string label = "Test";
+
+ const std::vector<byte> plaintext = Botan::hex_decode("000102030405060708090A0B0C0D0E0F");
+
+ // generated with botan
+ const std::vector<byte> ciphertext = Botan::hex_decode("0401519EAA0489FF9D51E98E4C22349463E2001CD06F8CE47D81D4007A"
+ "79ACF98E92C814686477CEA666EFC277DC84E15FC95E38AFF8E16D478A"
+ "44CD5C5F1517F8B1F300000591317F261C3D04A7207F01EAE3EC70F2360"
+ "0F82C53CC0B85BE7AC9F6CE79EF2AB416E5934D61BA9D346385D7545C57F"
+ "77C7EA7C58E18C70CBFB0A24AE1B9943EC5A8D0657522CCDF30BA95674D81"
+ "B397635D215178CD13BD9504AE957A9888F4128FFC0F0D3F1CEC646AEC8CE"
+ "3F2463D233B22A7A12B679F4C06501F584D4DEFF6D26592A8D873398BD892"
+ "B477B3468813C053DA43C4F3D49009F7A12D6EF7");
+
+ check_encrypt_decrypt(result, private_key, other_private_key, ecies_params, iv, label, plaintext, ciphertext);
+
+ return result;
+ }
+
+Test::Result test_ciphertext_too_short()
+ {
+ Test::Result result("ECIES ciphertext too short");
+
+ const Botan::EC_Group domain("secp521r1");
+ const Botan::BigInt private_key_value("405029866705438137604064977397053031159826489755682166267763407"
+ "5002761777100287880684822948852132235484464537021197213998300006"
+ "547176718172344447619746779823");
+
+ const Botan::BigInt other_private_key_value("2294226772740614508941417891614236736606752960073669253551166842"
+ "5866095315090327914760325168219669828915074071456176066304457448"
+ "25404691681749451640151380153");
+
+ const Botan::ECDH_PrivateKey private_key(Test::rng(), domain, private_key_value);
+ const Botan::ECDH_PrivateKey other_private_key(Test::rng(), domain, other_private_key_value);
+
+ const Botan::ECIES_System_Params ecies_params(private_key.domain(), "KDF1-18033(SHA-512)", "AES-256/CBC", 32,
+ "HMAC(SHA-512)", 16);
+
+ Botan::ECIES_Decryptor ecies_dec(other_private_key, ecies_params);
+
+ result.test_throws("ciphertext too short", [ &ecies_dec ]()
+ {
+ ecies_dec.decrypt(Botan::hex_decode("0401519EAA0489FF9D51E98E4C22349A"));
+ });
+
+ return result;
+ }
+
+class ECIES_Unit_Tests : public Test
+ {
+ public:
+ std::vector<Test::Result> run() override
+ {
+ std::vector<Test::Result> results;
+
+ std::vector<std::function<Test::Result()>> fns =
+ {
+ test_other_key_not_set,
+ test_kdf_not_found,
+ test_mac_not_found,
+ test_cipher_not_found,
+ test_system_params_short_ctor,
+ test_ciphertext_too_short
+ };
+
+ 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("ECIES unit tests " + std::to_string(i), e.what()));
+ }
+ }
+
+ return results;
+ }
+ };
+
+BOTAN_REGISTER_TEST("ecies-unit", ECIES_Unit_Tests);
+
+#endif
+
#endif
}