1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
/*
* (C) 2014 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include "tests.h"
#include <botan/libstate.h>
#include <botan/block_cipher.h>
#include <botan/hex.h>
#include <iostream>
#include <fstream>
using namespace Botan;
namespace {
size_t block_test(const std::string& algo,
const std::string& key_hex,
const std::string& in_hex,
const std::string& out_hex)
{
const secure_vector<byte> key = hex_decode_locked(key_hex);
const secure_vector<byte> pt = hex_decode_locked(in_hex);
const secure_vector<byte> ct = hex_decode_locked(out_hex);
Algorithm_Factory& af = global_state().algorithm_factory();
const auto providers = af.providers_of(algo);
size_t fails = 0;
if(providers.empty())
throw std::runtime_error("Unknown block cipher " + algo);
for(auto provider: providers)
{
const BlockCipher* proto = af.prototype_block_cipher(algo, provider);
if(!proto)
{
std::cout << "Unable to get " << algo << " from " << provider << "\n";
++fails;
continue;
}
std::unique_ptr<BlockCipher> cipher(proto->clone());
cipher->set_key(key);
secure_vector<byte> buf = pt;
cipher->encrypt(buf);
if(buf != ct)
{
std::cout << algo << " " << provider << " enc " << hex_encode(buf) << " != " << out_hex << "\n";
++fails;
buf = ct;
}
cipher->decrypt(buf);
if(buf != pt)
{
std::cout << algo << " " << provider << " dec " << hex_encode(buf) << " != " << out_hex << "\n";
++fails;
}
}
return fails;
}
}
size_t test_block()
{
auto test_bc = [](const std::string& input)
{
std::ifstream vec(input);
return run_tests_bb(vec, "BlockCipher", "Out", true,
[](std::map<std::string, std::string> m) -> size_t
{
return block_test(m["BlockCipher"], m["Key"], m["In"], m["Out"]);
});
};
return run_tests_in_dir(TEST_DATA_DIR "block", test_bc);
}
|