diff options
author | Jack Lloyd <[email protected]> | 2018-09-12 14:05:08 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-09-12 14:05:08 -0400 |
commit | 101513906ad8729603b8b78bb7488d1ffb029b0d (patch) | |
tree | eee7adf9151c8cf6f5f16e651a87bd59b2e89e00 /src/tests/test_cryptobox.cpp | |
parent | bc7e11343eed29cf2ff123f6e49b7dc038886cda (diff) |
Create proper KATs for cryptobox function
Diffstat (limited to 'src/tests/test_cryptobox.cpp')
-rw-r--r-- | src/tests/test_cryptobox.cpp | 84 |
1 files changed, 42 insertions, 42 deletions
diff --git a/src/tests/test_cryptobox.cpp b/src/tests/test_cryptobox.cpp index 5e7fcf08a..d3b011eb4 100644 --- a/src/tests/test_cryptobox.cpp +++ b/src/tests/test_cryptobox.cpp @@ -1,14 +1,15 @@ /* -* (C) 2014,2015 Jack Lloyd +* (C) 2014,2015,2018 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ #include "tests.h" +#include "test_rng.h" #if defined(BOTAN_HAS_CRYPTO_BOX) #include <botan/cryptobox.h> - #include <botan/hex.h> + #include <botan/pem.h> #endif namespace Botan_Tests { @@ -17,56 +18,55 @@ namespace { #if defined(BOTAN_HAS_CRYPTO_BOX) -class Cryptobox_Tests final : public Test +class Cryptobox_KAT final : public Text_Based_Test { public: - std::vector<Test::Result> run() override + Cryptobox_KAT() : Text_Based_Test("cryptobox.vec", "Input,Passphrase,Salt,Output") {} + + Test::Result run_one_test(const std::string&, const VarMap& vars) override { - Test::Result result("cryptobox"); + Test::Result result("Cryptobox"); + + const std::string password = vars.get_req_str("Passphrase"); + const std::vector<uint8_t> input = vars.get_req_bin("Input"); + const std::vector<uint8_t> salt = vars.get_req_bin("Salt"); + const std::vector<uint8_t> expected = vars.get_req_bin("Output"); + + const std::string expected_pem = Botan::PEM_Code::encode(expected, "BOTAN CRYPTOBOX MESSAGE"); + + Fixed_Output_RNG salt_rng(salt); + + const std::string ciphertext = + Botan::CryptoBox::encrypt(input.data(), input.size(), password, salt_rng); + + result.test_eq("encryption is expected value", ciphertext, expected_pem); + + result.test_eq("decryption works", Botan::CryptoBox::decrypt_bin(ciphertext, password), input); - for(size_t i = 0; i <= 128; i += 7) + // Now corrupt a bit and ensure it fails + try { - const std::string password = Test::random_password(); - const std::vector<uint8_t> input = unlock(Test::rng().random_vec(i)); - - const std::string ciphertext = - Botan::CryptoBox::encrypt(input.data(), input.size(), password, Test::rng()); - - // First verify decryption works - try - { - const Botan::secure_vector<uint8_t> decrypted = - Botan::CryptoBox::decrypt_bin(ciphertext, password); - result.test_eq("decrypt", decrypted, input); - } - catch(std::exception& e) - { - result.test_failure("cryptobox decrypt", e.what()); - } - - // Now corrupt a bit and ensure it fails - try - { - std::string corrupted = ciphertext; - corrupted[corrupted.size()/2]++; - Botan::CryptoBox::decrypt(corrupted, password); - result.test_failure("Decrypted corrupted cryptobox message"); - } - catch(Botan::Decoding_Error&) - { - result.test_success("Rejected corrupted cryptobox message"); - } - catch(Botan::Invalid_Argument&) - { - result.test_success("Rejected corrupted cryptobox message"); - } + const std::vector<uint8_t> corrupted = Test::mutate_vec(expected); + const std::string corrupted_pem = Botan::PEM_Code::encode(corrupted, "BOTAN CRYPTOBOX MESSAGE"); + + Botan::CryptoBox::decrypt(corrupted_pem, password); + result.test_failure("Decrypted corrupted cryptobox message", corrupted); + } + catch(Botan::Decoding_Error&) + { + result.test_success("Rejected corrupted cryptobox message"); + } + catch(Botan::Invalid_Argument&) + { + result.test_success("Rejected corrupted cryptobox message"); } - return {result}; + return result; } + }; -BOTAN_REGISTER_TEST("cryptobox", Cryptobox_Tests); +BOTAN_REGISTER_TEST("cryptobox", Cryptobox_KAT); #endif |