aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_ocb.cpp
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2015-11-11 05:43:01 -0500
committerJack Lloyd <[email protected]>2015-11-11 05:43:01 -0500
commitcf05aea092fad448c2f4a8e8b66159237096ba8e (patch)
tree00631bcc84809a1eeac5dd32dd92c62143ef831b /src/tests/test_ocb.cpp
parent6bb38ae2fa0e1be46b3a3256ac03f435b16a57ea (diff)
Update and consolidate the test framework.
The tests previously had used 4 to 6 different schemes internally (the vec file reader framework, Catch, the old InSiTo Boost.Test tests, the PK/BigInt tests which escaped the rewrite in 1.11.7, plus a number of one-offs). Converge on a design that works everywhere, and update all the things. Fix also a few bugs found by the test changes: SHA-512-256 name incorrect, OpenSSL RC4 name incorrect, signature of FFI function botan_pubkey_destroy was wrong.
Diffstat (limited to 'src/tests/test_ocb.cpp')
-rw-r--r--src/tests/test_ocb.cpp178
1 files changed, 80 insertions, 98 deletions
diff --git a/src/tests/test_ocb.cpp b/src/tests/test_ocb.cpp
index 891ecb54d..314fa31df 100644
--- a/src/tests/test_ocb.cpp
+++ b/src/tests/test_ocb.cpp
@@ -7,133 +7,115 @@
#include "tests.h"
#if defined(BOTAN_HAS_AEAD_OCB)
+ #include <botan/ocb.h>
+ #include <botan/loadstor.h>
+#endif
-#if defined(BOTAN_HAS_AES)
-
-#include <iostream>
-#include <botan/ocb.h>
-#include <botan/hex.h>
-#include <botan/sha2_32.h>
-#include <botan/aes.h>
-#include <botan/loadstor.h>
-
-using namespace Botan;
+namespace Botan_Tests {
namespace {
-std::vector<byte> ocb_encrypt(OCB_Encryption& enc,
- OCB_Decryption& dec,
- const std::vector<byte>& nonce,
- const std::vector<byte>& pt,
- const std::vector<byte>& ad)
- {
- enc.set_associated_data(ad.data(), ad.size());
-
- enc.start(nonce.data(), nonce.size());
-
- secure_vector<byte> buf(pt.begin(), pt.end());
- enc.finish(buf, 0);
-
- try
- {
- secure_vector<byte> ct = buf;
+#if defined(BOTAN_HAS_AEAD_OCB)
- dec.set_associated_data(ad.data(), ad.size());
+class OCB_Long_KAT_Tests : public Text_Based_Test
+ {
+ public:
+ OCB_Long_KAT_Tests() : Text_Based_Test(Test::data_file("ocb_long.vec"),
+ {"Keylen", "Taglen", "Output"}) {}
- dec.start(nonce.data(), nonce.size());
+ Test::Result run_one_test(const std::string&, const VarMap& vars)
+ {
+ const size_t keylen = get_req_sz(vars, "Keylen");
+ const size_t taglen = get_req_sz(vars, "Taglen");
+ const std::vector<byte> expected = get_req_bin(vars, "Output");
- dec.finish(ct, 0);
+ // Test from RFC 7253 Appendix A
- if(ct != pt)
- std::cout << "OCB failed to decrypt correctly" << std::endl;
- }
- catch(std::exception& e)
- {
- std::cout << "OCB round trip error - " << e.what() << std::endl;
- }
+ const std::string algo = "AES-" + std::to_string(keylen);
- return unlock(buf);
- }
+ Test::Result result("OCB long");
-size_t test_ocb_long(size_t keylen, size_t taglen,
- const std::string &expected)
- {
- // Test from RFC 7253 Appendix A
+ std::unique_ptr<Botan::BlockCipher> aes(Botan::BlockCipher::create(algo));
+ if(!aes)
+ {
+ result.note_missing(algo);
+ return result;
+ }
- const std::string algo = "AES-" + std::to_string(keylen);
+ Botan::OCB_Encryption enc(aes->clone(), taglen / 8);
+ Botan::OCB_Decryption dec(aes->clone(), taglen / 8);
- std::unique_ptr<BlockCipher> aes(BlockCipher::create(algo));
- if(!aes)
- throw Algorithm_Not_Found(algo);
+ std::vector<byte> key(keylen/8);
+ key[keylen/8-1] = taglen;
- OCB_Encryption enc(aes->clone(), taglen / 8);
- OCB_Decryption dec(aes->clone(), taglen / 8);
+ enc.set_key(key);
+ dec.set_key(key);
- std::vector<byte> key(keylen/8);
- key[keylen/8-1] = taglen;
+ const std::vector<byte> empty;
+ std::vector<byte> N(12);
+ std::vector<byte> C;
- enc.set_key(key);
- dec.set_key(key);
+ for(size_t i = 0; i != 128; ++i)
+ {
+ const std::vector<byte> S(i);
- const std::vector<byte> empty;
- std::vector<byte> N(12);
- std::vector<byte> C;
+ Botan::store_be(static_cast<uint32_t>(3*i+1), &N[8]);
- for(size_t i = 0; i != 128; ++i)
- {
- const std::vector<byte> S(i);
+ ocb_encrypt(result, C, enc, dec, N, S, S);
+ Botan::store_be(static_cast<uint32_t>(3*i+2), &N[8]);
+ ocb_encrypt(result, C, enc, dec, N, S, empty);
+ Botan::store_be(static_cast<uint32_t>(3*i+3), &N[8]);
+ ocb_encrypt(result, C, enc, dec, N, empty, S);
+ }
- store_be(static_cast<u32bit>(3*i+1), &N[8]);
- C += ocb_encrypt(enc, dec, N, S, S);
- store_be(static_cast<u32bit>(3*i+2), &N[8]);
- C += ocb_encrypt(enc, dec, N, S, empty);
- store_be(static_cast<u32bit>(3*i+3), &N[8]);
- C += ocb_encrypt(enc, dec, N, empty, S);
- }
+ Botan::store_be(static_cast<uint32_t>(385), &N[8]);
+ std::vector<byte> final_result;
+ ocb_encrypt(result, final_result, enc, dec, N, empty, C);
- store_be(static_cast<u32bit>(385), &N[8]);
- const std::vector<byte> cipher = ocb_encrypt(enc, dec, N, empty, C);
+ result.test_eq("correct value", final_result, expected);
- const std::string cipher_hex = hex_encode(cipher);
+ return result;
+ }
+ private:
+ void ocb_encrypt(Test::Result& result,
+ std::vector<byte>& output_to,
+ Botan::OCB_Encryption& enc,
+ Botan::OCB_Decryption& dec,
+ const std::vector<byte>& nonce,
+ const std::vector<byte>& pt,
+ const std::vector<byte>& ad)
+ {
+ enc.set_associated_data(ad.data(), ad.size());
- if(cipher_hex != expected)
- {
- std::cout << "OCB " << algo << " long test mistmatch "
- << cipher_hex << " != " << expected << std::endl;
- return 1;
- }
+ enc.start(nonce.data(), nonce.size());
- return 0;
- }
+ Botan::secure_vector<byte> buf(pt.begin(), pt.end());
+ enc.finish(buf, 0);
+ output_to.insert(output_to.end(), buf.begin(), buf.end());
-}
+ try
+ {
+ dec.set_associated_data(ad.data(), ad.size());
-size_t test_ocb()
- {
- size_t fails = 0;
+ dec.start(nonce.data(), nonce.size());
- fails += test_ocb_long(128, 128, "67E944D23256C5E0B6C61FA22FDF1EA2");
- fails += test_ocb_long(192, 128, "F673F2C3E7174AAE7BAE986CA9F29E17");
- fails += test_ocb_long(256, 128, "D90EB8E9C977C88B79DD793D7FFA161C");
- fails += test_ocb_long(128, 96, "77A3D8E73589158D25D01209");
- fails += test_ocb_long(192, 96, "05D56EAD2752C86BE6932C5E");
- fails += test_ocb_long(256, 96, "5458359AC23B0CBA9E6330DD");
- fails += test_ocb_long(128, 64, "192C9B7BD90BA06A");
- fails += test_ocb_long(192, 64, "0066BC6E0EF34E24");
- fails += test_ocb_long(256, 64, "7D4EA5D445501CBE");
- test_report("OCB long", 9, fails);
+ dec.finish(buf, 0);
- return fails;
- }
+ result.test_eq("OCB round tripped", buf, pt);
+ }
+ catch(std::exception& e)
+ {
+ result.test_failure("OCB round trip error", e.what());
+ }
-#else
+ }
+ };
-UNTESTED_WARNING(ocb);
+BOTAN_REGISTER_TEST("ocb_long", OCB_Long_KAT_Tests);
-#endif // BOTAN_HAS_AES
+#endif
-#else
+}
-SKIP_TEST(ocb);
+}
-#endif // BOTAN_HAS_AEAD_OCB