24 #include "../crypto/test/file_test.h"
25 #include "../crypto/test/test_util.h"
44 TestCtx *
ctx =
reinterpret_cast<TestCtx *
>(
arg);
46 if (t->HasInstruction(
"ENCRYPT") == t->HasInstruction(
"DECRYPT")) {
47 t->PrintLine(
"Want either ENCRYPT or DECRYPT");
53 } operation = t->HasInstruction(
"ENCRYPT") ? kEncrypt : kDecrypt;
55 if (t->HasAttribute(
"NumKeys")) {
61 t->GetAttribute(&num_keys,
"NumKeys");
62 t->InjectInstruction(
"NumKeys", num_keys);
74 } num_keys = kNotPresent;
75 if (t->HasInstruction(
"NumKeys")) {
77 t->GetInstruction(&num_keys_str,
"NumKeys");
78 const int n = strtoul(num_keys_str.c_str(),
nullptr, 0);
84 t->PrintLine(
"invalid NumKeys value");
92 operation == kEncrypt ?
"PLAINTEXT" :
"CIPHERTEXT";
94 if (!t->GetAttribute(&
count,
"COUNT") ||
95 (num_keys == 0 && !t->GetBytes(&
keys,
"KEYs")) ||
97 (!t->GetBytes(&
key1,
"KEY1") ||
98 !t->GetBytes(&
key2,
"KEY2") ||
99 !t->GetBytes(&key3,
"KEY3"))) ||
100 (
ctx->has_iv && !t->GetBytes(&iv,
"IV")) ||
101 !t->GetBytes(&
in, in_label)) {
105 std::vector<uint8_t>
key;
106 if (num_keys != kNotPresent) {
109 if (num_keys == kThree) {
110 key.insert(
key.end(), key3.begin(), key3.end());
126 if (t->IsAtNewInstructionBlock() && num_keys == kNotPresent) {
132 operation == kEncrypt ?
"CIPHERTEXT" :
"PLAINTEXT";
134 if (num_keys == kNotPresent) {
154 const std::vector<uint8_t> &
value) {
155 for (
size_t i = 0;
i <
key->size();
i++) {
160 for (
uint8_t j = 1; j < 8; j++) {
161 v ^= ((
v >> j) & 0x01);
168 TestCtx *
ctx =
reinterpret_cast<TestCtx *
>(
arg);
170 if (t->HasInstruction(
"ENCRYPT") == t->HasInstruction(
"DECRYPT")) {
171 t->PrintLine(
"Want either ENCRYPT or DECRYPT");
177 } operation = t->HasInstruction(
"ENCRYPT") ? kEncrypt : kDecrypt;
179 if (t->HasAttribute(
"NumKeys")) {
184 t->GetAttribute(&num_keys,
"NumKeys");
185 t->InjectInstruction(
"NumKeys", num_keys);
194 if (!t->GetInstruction(&num_keys_str,
"NumKeys")) {
197 const int n = strtoul(num_keys_str.c_str(),
nullptr, 0);
203 t->PrintLine(
"invalid NumKeys value");
211 operation == kEncrypt ?
"PLAINTEXT" :
"CIPHERTEXT";
213 if (!t->GetBytes(&
key1,
"KEY1") ||
214 !t->GetBytes(&
key2,
"KEY2") ||
215 !t->GetBytes(&key3,
"KEY3") ||
216 (
ctx->has_iv && !t->GetBytes(&iv,
"IV")) ||
217 !t->GetBytes(&
in, in_label)) {
222 for (
int i = 0;
i < 400;
i++) {
223 std::vector<uint8_t> current_iv = iv, current_in =
in, prev_result,
226 std::vector<uint8_t>
key(
key1);
228 key.insert(
key.end(), key3.begin(), key3.end());
230 for (
int j = 0; j < 10000; j++) {
231 prev_prev_result = prev_result;
235 current_iv, current_in)) {
236 t->PrintLine(
"CipherOperation failed");
240 if (operation == kEncrypt) {
242 current_in = current_iv;
244 current_in = prev_result;
248 current_iv = current_in;
258 operation == kEncrypt ?
"CIPHERTEXT" :
"PLAINTEXT";
261 operation == kEncrypt ?
"ENCRYPT" :
"DECRYPT";
262 printf(
"[%s]\n\n", op_label.c_str());
264 printf(
"COUNT = %d\r\nKEY1 = %s\r\nKEY2 = %s\r\nKEY3 = %s\r\n",
i,
276 if (num_keys == kThree) {
283 if (operation == kEncrypt) {
299 fprintf(
stderr,
"usage: %s (kat|mct) <cipher> <test file>\n",
arg);
305 return usage(argv[0]);
312 }
else if (
tm ==
"mct") {
315 fprintf(
stderr,
"invalid test_mode: %s\n",
tm.c_str());
316 return usage(argv[0]);
321 if (cipher ==
nullptr) {
322 fprintf(
stderr,
"invalid cipher: %s\n", argv[2]);
325 bool has_iv = cipher_name !=
"des-ede" && cipher_name !=
"des-ede3";
331 opts.callback = test_fn;