fipsoracle: Add AES KeyWrap test.

Change-Id: Ia67616674e4ccda66ff1a5627393b703a5f9cfe3
Reviewed-on: https://boringssl-review.googlesource.com/15848
Reviewed-by: Adam Langley <agl@google.com>
This commit is contained in:
Martin Kreichgauer 2017-05-03 11:17:50 -07:00 committed by Adam Langley
parent 9b7228c544
commit be5c67d4b1
3 changed files with 175 additions and 3 deletions

View File

@ -109,7 +109,14 @@ if (FIPS)
cavp_ctr_drbg_test
cavp_ctr_drbg_test.cc
cavp_test_util.h
cavp_test_util.cc
$<TARGET_OBJECTS:test_support>
)
add_executable(
cavp_keywrap_test
cavp_keywrap_test.cc
cavp_test_util.cc
$<TARGET_OBJECTS:test_support>
)
@ -121,10 +128,11 @@ if (FIPS)
target_link_libraries(cavp_ecdsa2_pkv_test crypto)
target_link_libraries(cavp_ecdsa2_siggen_test crypto)
target_link_libraries(cavp_ecdsa2_sigver_test crypto)
target_link_libraries(cavp_hmac_test crypto)
target_link_libraries(cavp_keywrap_test crypto)
target_link_libraries(cavp_rsa2_keygen_test crypto)
target_link_libraries(cavp_rsa2_siggen_test crypto)
target_link_libraries(cavp_rsa2_sigver_test crypto)
target_link_libraries(cavp_hmac_test crypto)
target_link_libraries(cavp_sha_test crypto)
target_link_libraries(cavp_sha_monte_test crypto)
target_link_libraries(cavp_tdes_test crypto)

View File

@ -0,0 +1,151 @@
/* Copyright (c) 2017, Google Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
// cavp_keywrap_test processes a NIST CAVP AES test vector request file and
// emits the corresponding response. An optional sample vector file can be
// passed to verify the result.
#include <stdlib.h>
#include <openssl/aes.h>
#include <openssl/crypto.h>
#include "../crypto/test/file_test.h"
#include "cavp_test_util.h"
struct TestCtx {
bool encrypt;
std::unique_ptr<FileTest> response_sample;
};
static bool AESKeyWrap(std::vector<uint8_t> *out, bool encrypt,
const std::vector<uint8_t> &key,
const std::vector<uint8_t> &in) {
size_t key_bits = key.size() * 8;
if (key_bits != 128 && key_bits != 256) {
return false;
}
AES_KEY aes_key;
if (encrypt) {
out->resize(in.size() + 8);
if (AES_set_encrypt_key(key.data(), key_bits, &aes_key) ||
AES_wrap_key(&aes_key, nullptr, out->data(), in.data(), in.size()) ==
-1) {
return false;
}
} else {
out->resize(in.size() - 8);
if (AES_set_decrypt_key(key.data(), key_bits, &aes_key) ||
AES_unwrap_key(&aes_key, nullptr, out->data(), in.data(), in.size()) ==
-1) {
return false;
}
}
return true;
}
static bool TestCipher(FileTest *t, void *arg) {
TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);
std::string count, unused, in_label = ctx->encrypt ? "P" : "C",
result_label = ctx->encrypt ? "C" : "P";
std::vector<uint8_t> key, in, result;
// clang-format off
if (!t->GetInstruction(&unused, "PLAINTEXT LENGTH") ||
!t->GetAttribute(&count, "COUNT") ||
!t->GetBytes(&key, "K") ||
!t->GetBytes(&in, in_label)) {
return false;
}
// clang-format on
printf("%s", t->CurrentTestToString().c_str());
if (!AESKeyWrap(&result, ctx->encrypt, key, in)) {
if (ctx->encrypt) {
return false;
} else {
printf("FAIL\r\n\r\n");
}
} else {
printf("%s = %s\r\n\r\n", result_label.c_str(),
EncodeHex(result.data(), result.size()).c_str());
}
// Check if sample response file matches.
if (ctx->response_sample) {
if (ctx->response_sample->ReadNext() != FileTest::kReadSuccess) {
t->PrintLine("invalid sample file");
return false;
}
std::string expected_count;
std::vector<uint8_t> expected_result;
if (!ctx->response_sample->GetAttribute(&expected_count, "COUNT") ||
count != expected_count ||
(!ctx->response_sample->GetBytes(&expected_result, result_label) ||
!t->ExpectBytesEqual(expected_result.data(), expected_result.size(),
result.data(), result.size()))) {
t->PrintLine("result doesn't match");
return false;
}
}
return true;
}
static int usage(char *arg) {
fprintf(
stderr,
"usage: %s (enc|dec) (128|256) <test file> [<sample response file>]\n",
arg);
return 1;
}
int main(int argc, char **argv) {
CRYPTO_library_init();
if (argc < 4 || argc > 5) {
return usage(argv[0]);
}
const std::string op(argv[1]);
bool encrypt;
if (op == "enc") {
encrypt = true;
} else if (op == "dec") {
encrypt = false;
} else {
return usage(argv[0]);
}
TestCtx ctx = {encrypt, nullptr};
if (argc == 5) {
ctx.response_sample.reset(new FileTest(argv[4]));
if (!ctx.response_sample->is_open()) {
return 1;
}
ctx.response_sample->SetIgnoreUnusedAttributes(true);
}
printf("# Generated by");
for (int i = 0; i < argc; i++) {
printf(" %s", argv[i]);
}
printf("\r\n\r\n");
return FileTestMainSilent(TestCipher, &ctx, argv[3]);
}

View File

@ -257,6 +257,18 @@ var tdesTests = testSuite{
},
}
var keyWrapTests = testSuite{
"KeyWrap38F",
"cavp_keywrap_test",
nil,
[]test{
{"KW_AD_128", []string{"dec", "128"}, false},
{"KW_AD_256", []string{"dec", "256"}, false},
{"KW_AE_128", []string{"enc", "128"}, false},
{"KW_AE_256", []string{"enc", "256"}, false},
},
}
var allTestSuites = []*testSuite{
&aesGCMTests,
&aesTests,
@ -265,10 +277,11 @@ var allTestSuites = []*testSuite{
&ecdsa2PKVTests,
&ecdsa2SigGenTests,
&ecdsa2SigVerTests,
&hmacTests,
&keyWrapTests,
&rsa2KeyGenTests,
&rsa2SigGenTests,
&rsa2SigVerTests,
&hmacTests,
&shaTests,
&shaMonteTests,
&tdesTests,