fipsoracle: Add MCT mode for TDES.

Change-Id: I0dafd669a6d4e435d7597c0db26ef467e4beef0d
Reviewed-on: https://boringssl-review.googlesource.com/15805
Reviewed-by: Adam Langley <agl@google.com>
This commit is contained in:
Martin Kreichgauer 2017-05-01 15:31:43 -07:00 committed by Adam Langley
parent 5eb75e211e
commit 6dd055d2eb
4 changed files with 259 additions and 45 deletions

View File

@ -367,12 +367,16 @@ bool FileTest::IsAtNewInstructionBlock() const {
return is_at_new_instruction_block_; return is_at_new_instruction_block_;
} }
void FileTest::InjectInstruction(const std::string &key,
const std::string &value) {
instructions_[key] = value;
}
void FileTest::SetIgnoreUnusedAttributes(bool ignore) { void FileTest::SetIgnoreUnusedAttributes(bool ignore) {
ignore_unused_attributes_ = ignore; ignore_unused_attributes_ = ignore;
} }
int FileTestMainSilent(bool (*run_test)(FileTest *t, void *arg), void *arg, int FileTestMainSilent(FileTestFunc run_test, void *arg, const char *path) {
const char *path) {
FileTest t(path); FileTest t(path);
if (!t.is_open()) { if (!t.is_open()) {
return 1; return 1;
@ -417,8 +421,7 @@ int FileTestMainSilent(bool (*run_test)(FileTest *t, void *arg), void *arg,
return failed ? 1 : 0; return failed ? 1 : 0;
} }
int FileTestMain(bool (*run_test)(FileTest *t, void *arg), void *arg, int FileTestMain(FileTestFunc run_test, void *arg, const char *path) {
const char *path) {
int result = FileTestMainSilent(run_test, arg, path); int result = FileTestMainSilent(run_test, arg, path);
if (!result) { if (!result) {
printf("PASS\n"); printf("PASS\n");

View File

@ -158,6 +158,10 @@ class FileTest {
// other blank or comment lines are omitted. // other blank or comment lines are omitted.
const std::string &CurrentTestToString() const; const std::string &CurrentTestToString() const;
// InjectInstruction adds a key value pair to the most recently parsed set of
// instructions.
void InjectInstruction(const std::string &key, const std::string &value);
void SetIgnoreUnusedAttributes(bool ignore); void SetIgnoreUnusedAttributes(bool ignore);
private: private:
@ -197,6 +201,8 @@ class FileTest {
FileTest &operator=(const FileTest &) = delete; FileTest &operator=(const FileTest &) = delete;
}; };
typedef bool (*FileTestFunc)(FileTest *t, void *arg);
// FileTestMain runs a file-based test out of |path| and returns an exit code // FileTestMain runs a file-based test out of |path| and returns an exit code
// suitable to return out of |main|. |run_test| should return true on pass and // suitable to return out of |main|. |run_test| should return true on pass and
// false on failure. FileTestMain also implements common handling of the 'Error' // false on failure. FileTestMain also implements common handling of the 'Error'
@ -208,12 +214,10 @@ class FileTest {
// list of keys. This may be used to initialize a shared set of keys for many // list of keys. This may be used to initialize a shared set of keys for many
// tests. However, if one test fails, the framework will continue to run // tests. However, if one test fails, the framework will continue to run
// subsequent tests. // subsequent tests.
int FileTestMain(bool (*run_test)(FileTest *t, void *arg), void *arg, int FileTestMain(FileTestFunc run_test, void *arg, const char *path);
const char *path);
// FileTestMainSilent behaves like FileTestMain but does not print a final // FileTestMainSilent behaves like FileTestMain but does not print a final
// FAIL/PASS message to stdout. // FAIL/PASS message to stdout.
int FileTestMainSilent(bool (*run_test)(FileTest *t, void *arg), void *arg, int FileTestMainSilent(FileTestFunc run_test, void *arg, const char *path);
const char *path);
#endif /* OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H */ #endif /* OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H */

View File

@ -32,7 +32,6 @@ struct TestCtx {
enum Mode { enum Mode {
kKAT, // Known Answer Test kKAT, // Known Answer Test
kMCT, // Monte Carlo Test kMCT, // Monte Carlo Test
kMMT, // Multi Message Test
}; };
bool has_iv; bool has_iv;
Mode mode; Mode mode;
@ -50,41 +49,99 @@ static bool TestKAT(FileTest *t, void *arg) {
kDecrypt, kDecrypt,
} operation = t->HasInstruction("ENCRYPT") ? kEncrypt : kDecrypt; } operation = t->HasInstruction("ENCRYPT") ? kEncrypt : kDecrypt;
if (t->HasAttribute("NumKeys")) {
// Another file format quirk: NumKeys is a single attribute line immediately
// following an instruction and should probably have been an instruction
// instead. If it is present, the file has separate attributes "KEY{1,2,3}".
// If it is not, the keys are concatenated in a single attribute "KEYs".
std::string num_keys;
t->GetAttribute(&num_keys, "NumKeys");
t->InjectInstruction("NumKeys", num_keys);
std::string header = operation == kEncrypt ? "[ENCRYPT]" : "[DECRYPT]";
printf("%s\r\n\r\n", header.c_str());
return true;
}
enum {
kNotPresent,
kTwo,
kThree,
} num_keys = kNotPresent;
if (t->HasInstruction("NumKeys")) {
std::string num_keys_str;
t->GetInstruction(&num_keys_str, "NumKeys");
const int n = strtoul(num_keys_str.c_str(), nullptr, 0);
if (n == 2) {
num_keys = kTwo;
} else if (n == 3) {
num_keys = kThree;
} else {
t->PrintLine("invalid NumKeys value");
return false;
}
}
std::string count; std::string count;
std::vector<uint8_t> key, iv, in, result; std::vector<uint8_t> keys, key1, key2, key3, iv, in, result;
const std::string op_label = operation == kEncrypt ? "PLAINTEXT" : "CIPHERTEXT"; const std::string in_label =
operation == kEncrypt ? "PLAINTEXT" : "CIPHERTEXT";
// clang-format off
if (!t->GetAttribute(&count, "COUNT") || if (!t->GetAttribute(&count, "COUNT") ||
!t->GetBytes(&key, "KEYs") || (num_keys == 0 && !t->GetBytes(&keys, "KEYs")) ||
(num_keys > 0 &&
(!t->GetBytes(&key1, "KEY1") ||
!t->GetBytes(&key2, "KEY2") ||
!t->GetBytes(&key3, "KEY3"))) ||
(ctx->has_iv && !t->GetBytes(&iv, "IV")) || (ctx->has_iv && !t->GetBytes(&iv, "IV")) ||
!t->GetBytes(&in, op_label)) { !t->GetBytes(&in, in_label)) {
return false; return false;
} }
std::vector<uint8_t> triple_key(key); // clang-format on
triple_key.insert(triple_key.end(), key.begin(), key.end()); std::vector<uint8_t> key;
triple_key.insert(triple_key.end(), key.begin(), key.end()); if (num_keys != kNotPresent) {
key.insert(key.end(), key1.begin(), key1.end());
key.insert(key.end(), key2.begin(), key2.end());
if (num_keys == kThree) {
key.insert(key.end(), key3.begin(), key3.end());
}
} else {
key.insert(key.end(), keys.begin(), keys.end());
key.insert(key.end(), keys.begin(), keys.end());
key.insert(key.end(), keys.begin(), keys.end());
}
const EVP_CIPHER *cipher = ctx->cipher; if (!CipherOperation(ctx->cipher, &result, operation == kEncrypt, key, iv,
if (!CipherOperation(cipher, &result, operation == kEncrypt, triple_key, iv,
in)) { in)) {
return false; return false;
} }
const std::string result_label =
operation == kEncrypt ? "CIPHERTEXT" : "PLAINTEXT";
// TDES fax files output format differs from its input format, so we // TDES fax files output format differs from file to file, and the input
// construct it manually rather than printing CurrentTestToString(). // format is inconsistent with the output, so we construct the output manually
if (t->IsAtNewInstructionBlock()) { // rather than printing CurrentTestToString().
if (t->IsAtNewInstructionBlock() && num_keys == kNotPresent) {
// If NumKeys is present, header is printed when parsing NumKeys.
std::string header = operation == kEncrypt ? "[ENCRYPT]" : "[DECRYPT]"; std::string header = operation == kEncrypt ? "[ENCRYPT]" : "[DECRYPT]";
printf("%s\r\n", header.c_str()); printf("%s\r\n", header.c_str());
} }
printf("COUNT = %s\r\nKEYs = %s\r\n", count.c_str(), const std::string result_label =
EncodeHex(key.data(), key.size()).c_str()); operation == kEncrypt ? "CIPHERTEXT" : "PLAINTEXT";
printf("COUNT = %s\r\n", count.c_str());
if (num_keys == kNotPresent) {
printf("KEYs = %s\r\n", EncodeHex(keys.data(), keys.size()).c_str());
} else {
printf("KEY1 = %s\r\nKEY2 = %s\r\nKEY3 = %s\r\n",
EncodeHex(key1.data(), key1.size()).c_str(),
EncodeHex(key2.data(), key2.size()).c_str(),
EncodeHex(key3.data(), key3.size()).c_str());
}
if (ctx->has_iv) { if (ctx->has_iv) {
printf("IV = %s\r\n", EncodeHex(iv.data(), iv.size()).c_str()); printf("IV = %s\r\n", EncodeHex(iv.data(), iv.size()).c_str());
} }
printf("%s = %s\r\n%s = %s\r\n\r\n", op_label.c_str(), printf("%s = %s\r\n", in_label.c_str(),
EncodeHex(in.data(), in.size()).c_str(), result_label.c_str(), EncodeHex(in.data(), in.size()).c_str());
printf("%s = %s\r\n\r\n", result_label.c_str(),
EncodeHex(result.data(), result.size()).c_str()); EncodeHex(result.data(), result.size()).c_str());
// Check if sample response file matches. // Check if sample response file matches.
@ -108,11 +165,163 @@ static bool TestKAT(FileTest *t, void *arg) {
return true; return true;
} }
// XORKeyWithOddParityLSB sets |*key| to |key| XOR |value| and then writes
// the LSB of each byte to establish odd parity for that byte. This parity-based
// embedded of a DES key into 64 bits is an old tradition and something that
// NIST's tests require.
static void XORKeyWithOddParityLSB(std::vector<uint8_t> *key,
const std::vector<uint8_t> &value) {
for (size_t i = 0; i < key->size(); i++) {
uint8_t v = (*key)[i] ^ value[i];
// Use LSB to establish odd parity.
v |= 0x01;
for (uint8_t j = 1; j < 8; j++) {
v ^= ((v >> j) & 0x01);
}
(*key)[i] = v;
}
}
static bool TestMCT(FileTest *t, void *arg) {
TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
if (t->HasInstruction("ENCRYPT") == t->HasInstruction("DECRYPT")) {
t->PrintLine("Want either ENCRYPT or DECRYPT");
return false;
}
enum {
kEncrypt,
kDecrypt,
} operation = t->HasInstruction("ENCRYPT") ? kEncrypt : kDecrypt;
if (t->HasAttribute("NumKeys")) {
// Another file format quirk: NumKeys is a single attribute line immediately
// following an instruction and should probably have been an instruction
// instead.
std::string num_keys;
t->GetAttribute(&num_keys, "NumKeys");
t->InjectInstruction("NumKeys", num_keys);
return true;
}
enum {
kTwo,
kThree,
} num_keys;
std::string num_keys_str;
if (!t->GetInstruction(&num_keys_str, "NumKeys")) {
return false;
} else {
const int n = strtoul(num_keys_str.c_str(), nullptr, 0);
if (n == 2) {
num_keys = kTwo;
} else if (n == 3) {
num_keys = kThree;
} else {
t->PrintLine("invalid NumKeys value");
return false;
}
}
std::string count;
std::vector<uint8_t> key1, key2, key3, iv, in, result;
const std::string in_label =
operation == kEncrypt ? "PLAINTEXT" : "CIPHERTEXT";
// clang-format off
if (!t->GetBytes(&key1, "KEY1") ||
!t->GetBytes(&key2, "KEY2") ||
!t->GetBytes(&key3, "KEY3") ||
(ctx->has_iv && !t->GetBytes(&iv, "IV")) ||
!t->GetBytes(&in, in_label)) {
return false;
}
// clang-format on
for (int i = 0; i < 400; i++) {
std::vector<uint8_t> current_iv = iv, current_in = in, prev_result,
prev_prev_result;
std::vector<uint8_t> key(key1);
key.insert(key.end(), key2.begin(), key2.end());
key.insert(key.end(), key3.begin(), key3.end());
for (int j = 0; j < 10000; j++) {
prev_prev_result = prev_result;
prev_result = result;
const EVP_CIPHER *cipher = ctx->cipher;
if (!CipherOperation(cipher, &result, operation == kEncrypt, key,
current_iv, current_in)) {
t->PrintLine("CipherOperation failed");
return false;
}
if (ctx->has_iv) {
if (operation == kEncrypt) {
if (j == 0) {
current_in = current_iv;
} else {
current_in = prev_result;
}
current_iv = result;
} else { // operation == kDecrypt
current_iv = current_in;
current_in = result;
}
} else {
current_in = result;
}
}
// Output result for COUNT = i.
const std::string result_label =
operation == kEncrypt ? "CIPHERTEXT" : "PLAINTEXT";
if (i == 0) {
const std::string op_label =
operation == kEncrypt ? "ENCRYPT" : "DECRYPT";
printf("[%s]\n\n", op_label.c_str());
}
printf("COUNT = %d\r\nKEY1 = %s\r\nKEY2 = %s\r\nKEY3 = %s\r\n", i,
EncodeHex(key1.data(), key1.size()).c_str(),
EncodeHex(key2.data(), key2.size()).c_str(),
EncodeHex(key3.data(), key3.size()).c_str());
if (ctx->has_iv) {
printf("IV = %s\r\n", EncodeHex(iv.data(), iv.size()).c_str());
}
printf("%s = %s\r\n", in_label.c_str(),
EncodeHex(in.data(), in.size()).c_str());
printf("%s = %s\r\n\r\n", result_label.c_str(),
EncodeHex(result.data(), result.size()).c_str());
XORKeyWithOddParityLSB(&key1, result);
XORKeyWithOddParityLSB(&key2, prev_result);
if (num_keys == kThree) {
XORKeyWithOddParityLSB(&key3, prev_prev_result);
} else {
XORKeyWithOddParityLSB(&key3, result);
}
if (ctx->has_iv) {
if (operation == kEncrypt) {
in = prev_result;
iv = result;
} else {
iv = current_iv;
in = current_in;
}
} else {
in = result;
}
}
return true;
}
static int usage(char *arg) { static int usage(char *arg) {
fprintf( fprintf(stderr,
stderr, "usage: %s (kat|mct) <cipher> <test file> [<sample response "
"usage: %s (kat|mct|mmt) <cipher> <test file> [<sample response file>]\n", "file>]\n",
arg); arg);
return 1; return 1;
} }
@ -127,8 +336,6 @@ int main(int argc, char **argv) {
enum TestCtx::Mode test_mode; enum TestCtx::Mode test_mode;
if (tm == "kat") { if (tm == "kat") {
test_mode = TestCtx::kKAT; test_mode = TestCtx::kKAT;
} else if (tm == "mmt") {
test_mode = TestCtx::kMMT;
} else if (tm == "mct") { } else if (tm == "mct") {
test_mode = TestCtx::kMCT; test_mode = TestCtx::kMCT;
} else { } else {
@ -142,7 +349,7 @@ int main(int argc, char **argv) {
fprintf(stderr, "invalid cipher: %s\n", argv[2]); fprintf(stderr, "invalid cipher: %s\n", argv[2]);
return 1; return 1;
} }
bool has_iv = cipher_name != "des-ede3"; bool has_iv = cipher_name != "des-ede" && cipher_name != "des-ede3";
TestCtx ctx = {cipher, nullptr, has_iv, test_mode}; TestCtx ctx = {cipher, nullptr, has_iv, test_mode};
if (argc == 5) { if (argc == 5) {
@ -159,6 +366,6 @@ int main(int argc, char **argv) {
} }
printf("\r\n\r\n"); printf("\r\n\r\n");
// TODO(martinkr): Add MMT, MCT. FileTestFunc test_fn = test_mode == TestCtx::kKAT ? &TestKAT : &TestMCT;
return FileTestMainSilent(TestKAT, &ctx, argv[3]); return FileTestMainSilent(test_fn, &ctx, argv[3]);
} }

View File

@ -227,19 +227,19 @@ var tdesTests = testSuite{
"cavp_tdes_test", "cavp_tdes_test",
nil, nil,
[]test{ []test{
// {"TCBCMMT2", []string{"mmt"}, false}, {"TCBCMMT2", []string{"kat", "des-ede-cbc"}, false},
// {"TCBCMMT3", []string{"mmt"}, false}, {"TCBCMMT3", []string{"kat", "des-ede3-cbc"}, false},
// {"TCBCMonte2", []string{"mct"}, false}, {"TCBCMonte2", []string{"mct", "des-ede3-cbc"}, false},
// {"TCBCMonte3", []string{"mct"}, false}, {"TCBCMonte3", []string{"mct", "des-ede3-cbc"}, false},
{"TCBCinvperm", []string{"kat", "des-ede3-cbc"}, false}, {"TCBCinvperm", []string{"kat", "des-ede3-cbc"}, false},
{"TCBCpermop", []string{"kat", "des-ede3-cbc"}, false}, {"TCBCpermop", []string{"kat", "des-ede3-cbc"}, false},
{"TCBCsubtab", []string{"kat", "des-ede3-cbc"}, false}, {"TCBCsubtab", []string{"kat", "des-ede3-cbc"}, false},
{"TCBCvarkey", []string{"kat", "des-ede3-cbc"}, false}, {"TCBCvarkey", []string{"kat", "des-ede3-cbc"}, false},
{"TCBCvartext", []string{"kat", "des-ede3-cbc"}, false}, {"TCBCvartext", []string{"kat", "des-ede3-cbc"}, false},
// {"TECBMMT2", []string{"mmt"}, false}, {"TECBMMT2", []string{"kat", "des-ede"}, false},
// {"TECBMMT3", []string{"mmt"}, false}, {"TECBMMT3", []string{"kat", "des-ede3"}, false},
// {"TECBMonte2", []string{"mct"}, false}, {"TECBMonte2", []string{"mct", "des-ede3"}, false},
// {"TECBMonte3", []string{"mct"}, false}, {"TECBMonte3", []string{"mct", "des-ede3"}, false},
{"TECBinvperm", []string{"kat", "des-ede3"}, false}, {"TECBinvperm", []string{"kat", "des-ede3"}, false},
{"TECBpermop", []string{"kat", "des-ede3"}, false}, {"TECBpermop", []string{"kat", "des-ede3"}, false},
{"TECBsubtab", []string{"kat", "des-ede3"}, false}, {"TECBsubtab", []string{"kat", "des-ede3"}, false},