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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
/*
* (C) 2016 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include "tests.h"
#if defined(BOTAN_HAS_PK_PADDING)
#include <botan/emsa.h>
#endif
#if defined(BOTAN_HAS_EME_PKCS1)
#include <botan/eme_pkcs.h>
#endif
namespace Botan_Tests {
#if defined(BOTAN_HAS_EME_PKCS1)
class EME_PKCS1v15_Decoding_Tests final : public Text_Based_Test
{
public:
EME_PKCS1v15_Decoding_Tests()
: Text_Based_Test(
"pk_pad_eme/pkcs1.vec",
"RawCiphertext",
"Plaintext") {}
Test::Result run_one_test(const std::string& hdr, const VarMap& vars) override
{
const bool is_valid = (hdr == "valid");
Test::Result result("PKCSv15 Decoding");
Botan::EME_PKCS1v15 pkcs;
const std::vector<uint8_t> ciphertext = vars.get_req_bin("RawCiphertext");
const std::vector<uint8_t> plaintext = vars.get_opt_bin("Plaintext");
if(is_valid == false)
{
result.test_eq("Plaintext value should be empty for invalid EME inputs", plaintext.size(), 0);
}
uint8_t valid_mask = 0;
Botan::secure_vector<uint8_t> decoded =
pkcs.unpad(valid_mask, ciphertext.data(), ciphertext.size());
result.confirm("EME valid_mask has expected value", valid_mask == 0x00 || valid_mask == 0xFF);
result.test_eq("EME decoding valid/invalid matches", valid_mask == 0xFF, is_valid);
if(valid_mask == 0xFF)
{
result.test_eq("EME decoded plaintext correct", decoded, plaintext);
}
else
{
bool all_zeros = true;
for(size_t i = 0; i != decoded.size(); ++i)
if(decoded[i] != 0)
all_zeros = false;
result.confirm("On invalid padding output is all zero", all_zeros);
}
// TODO: also test that encoding is accepted
return result;
}
};
BOTAN_REGISTER_TEST("eme_pkcs1v15", EME_PKCS1v15_Decoding_Tests);
class EMSA_unit_tests final : public Test
{
public:
std::vector<Test::Result> run() override
{
Test::Result name_tests("EMSA_name_tests");
std::vector<std::string> pads_need_hash =
{
#if BOTAN_HAS_EMSA1
"EMSA1",
#endif
#if BOTAN_HAS_EMSA_X931
"EMSA2",
#endif
#if BOTAN_HAS_EMSA_PKCS1
"EMSA3",
#endif
#if BOTAN_HAS_EMSA_PSSR
"EMSA4",
"PSSR_Raw",
#endif
#if BOTAN_HAS_ISO_9796
"ISO_9796_DS2",
"ISO_9796_DS3",
#endif
};
std::vector<std::string> pads_no_hash =
{
#if BOTAN_HAS_EMSA_RAW
"Raw",
#endif
#if BOTAN_HAS_EMSA_PKCS1
"EMSA3(Raw)",
"EMSA3(Raw,SHA-512)",
#endif
};
for(auto pad : pads_need_hash)
{
try
{
const std::string hash_to_use = Botan::hash_for_emsa(pad);
std::unique_ptr<Botan::EMSA> emsa_1(
Botan::get_emsa(pad + "(" + hash_to_use + ")"));
std::unique_ptr<Botan::EMSA> emsa_2(Botan::get_emsa(emsa_1->name()));
name_tests.test_eq("EMSA_name_test for " + pad,
emsa_1->name(), emsa_2->name());
}
catch(Botan::Lookup_Error&)
{
name_tests.test_note("Skipping test due to missing hash");
}
catch(const std::exception& e)
{
name_tests.test_failure("EMSA_name_test for " + pad + ": " + e.what());
}
}
for(auto pad : pads_need_hash)
{
std::string algo_name = pad + "(YYZ)";
try
{
std::unique_ptr<Botan::EMSA> emsa(
Botan::get_emsa(algo_name));
name_tests.test_failure("EMSA_name_test for " + pad + ": " +
"Could create EMSA with fantasy hash YYZ");
}
catch(Botan::Lookup_Error&)
{
name_tests.test_note("Skipping test due to missing hash");
}
catch(const std::exception& e)
{
name_tests.test_eq("EMSA_name_test for " + pad,
e.what(),
"Could not find any algorithm named \"" + algo_name + "\"");
}
}
for(auto pad : pads_no_hash)
{
try
{
std::unique_ptr<Botan::EMSA> emsa_1(Botan::get_emsa(pad));
std::unique_ptr<Botan::EMSA> emsa_2(Botan::get_emsa(emsa_1->name()));
name_tests.test_eq("EMSA_name_test for " + pad,
emsa_1->name(), emsa_2->name());
}
catch(Botan::Lookup_Error&)
{
name_tests.test_note("Skipping test due to missing hash");
}
catch(const std::exception& e)
{
name_tests.test_failure("EMSA_name_test for " + pad + ": " + e.what());
}
}
return { name_tests };
}
};
BOTAN_REGISTER_TEST("pk_pad_emsa_unit", EMSA_unit_tests);
#endif
}
|