Skip to content

Commit

Permalink
Add CTR-DRBG CAVP test driver.
Browse files Browse the repository at this point in the history
Change-Id: I14c554eaf1e431271c5e981e2337b937c6cdf012
Reviewed-on: https://boringssl-review.googlesource.com/15645
Reviewed-by: Adam Langley <agl@google.com>
  • Loading branch information
agl committed May 1, 2017
1 parent 0fcac4b commit b387e22
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 3 deletions.
15 changes: 12 additions & 3 deletions crypto/test/file_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,20 @@ FileTest::ReadResult FileTest::ReadNext() {
std::string key, value;
std::tie(key, value) = ParseKeyValue(buf.get(), len);

unused_attributes_.insert(key);
attributes_[key] = value;
// Duplicate keys are rewritten to have “/2”, “/3”, … suffixes.
std::string mapped_key = key;
for (unsigned i = 2; attributes_.count(mapped_key) != 0; i++) {
char suffix[32];
snprintf(suffix, sizeof(suffix), "/%u", i);
suffix[sizeof(suffix)-1] = 0;
mapped_key = key + suffix;
}

unused_attributes_.insert(mapped_key);
attributes_[mapped_key] = value;
if (start_line_ == 0) {
// This is the start of a test.
type_ = key;
type_ = mapped_key;
parameter_ = value;
start_line_ = line_;
for (const auto &kv : instructions_) {
Expand Down
11 changes: 11 additions & 0 deletions fipsoracle/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ if (FIPS)
$<TARGET_OBJECTS:test_support>
)

add_executable(
cavp_ctr_drbg_test

cavp_ctr_drbg_test.cc
cavp_test_util.h
cavp_test_util.cc
$<TARGET_OBJECTS:test_support>
)

target_link_libraries(cavp_aes_test crypto)
target_link_libraries(cavp_aes_gcm_test crypto)

Expand All @@ -59,4 +68,6 @@ if (FIPS)

target_link_libraries(cavp_sha_test crypto)
target_link_libraries(cavp_sha_monte_test crypto)

target_link_libraries(cavp_ctr_drbg_test crypto)
endif()
141 changes: 141 additions & 0 deletions fipsoracle/cavp_ctr_drbg_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/* 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_aes_gcm_test processes a NIST CAVP AES GCM test vector request file and
// emits the corresponding response. An optional sample vector file can be
// passed to verify the result.

#include <openssl/crypto.h>

#include <stdlib.h>

#include "cavp_test_util.h"
#include "../crypto/fipsmodule/rand/internal.h"
#include "../crypto/test/file_test.h"


struct TestCtx {
std::unique_ptr<FileTest> response_sample;
};

static bool TestCTRDRBG(FileTest *t, void *arg) {
TestCtx *ctx = reinterpret_cast<TestCtx *>(arg);

std::string test_type, prediction_resistance, entropy_input_len, nonce_len,
personalization_str_len, additional_input_len, returned_bits_len;
if (!t->GetInstruction(&test_type, "AES-256 no df") ||
!t->GetInstruction(&prediction_resistance, "PredictionResistance") ||
!t->GetInstruction(&entropy_input_len, "EntropyInputLen") ||
!t->GetInstruction(&nonce_len, "NonceLen") ||
!t->GetInstruction(&personalization_str_len,
"PersonalizationStringLen") ||
!t->GetInstruction(&additional_input_len, "AdditionalInputLen") ||
!t->GetInstruction(&returned_bits_len, "ReturnedBitsLen") ||
!test_type.empty() ||
prediction_resistance != "False" ||
strtoul(entropy_input_len.c_str(), nullptr, 0) !=
CTR_DRBG_ENTROPY_LEN * 8 ||
nonce_len != "0") {
return false;
}

std::string count;
std::vector<uint8_t> entropy, nonce, personalization_str, ai1, ai2;
if (!t->GetAttribute(&count, "COUNT") ||
!t->GetBytes(&entropy, "EntropyInput") ||
!t->GetBytes(&nonce, "Nonce") ||
!t->GetBytes(&personalization_str, "PersonalizationString") ||
!t->GetBytes(&ai1, "AdditionalInput") ||
!t->GetBytes(&ai2, "AdditionalInput/2") ||
entropy.size() * 8 != strtoul(entropy_input_len.c_str(), nullptr, 0) ||
nonce.size() != 0 ||
personalization_str.size() * 8 !=
strtoul(personalization_str_len.c_str(), nullptr, 0) ||
ai1.size() != ai2.size() ||
ai1.size() * 8 != strtoul(additional_input_len.c_str(), nullptr, 0)) {
return false;
}

CTR_DRBG_STATE drbg;
CTR_DRBG_init(&drbg, entropy.data(),
personalization_str.size() > 0 ? personalization_str.data()
: nullptr,
personalization_str.size());

uint64_t out_len = strtoul(returned_bits_len.c_str(), nullptr, 0);
if (out_len == 0 || (out_len & 7) != 0) {
return false;
}
out_len /= 8;

std::vector<uint8_t> out;
out.resize(out_len);

CTR_DRBG_generate(&drbg, out.data(), out.size(),
ai1.size() > 0 ? ai1.data() : nullptr, ai1.size());
CTR_DRBG_generate(&drbg, out.data(), out.size(),
ai2.size() > 0 ? ai2.data() : nullptr, ai2.size());

printf("%s", t->CurrentTestToString().c_str());
printf("ReturnedBits = %s\r\n\r\n",
EncodeHex(out.data(), out.size()).c_str());

// Check if sample response file matches.
if (ctx->response_sample) {
ctx->response_sample->ReadNext();
std::string expected_count;
std::vector<uint8_t> expected_bits;
if (!ctx->response_sample->GetAttribute(&expected_count, "COUNT") ||
count != expected_count ||
!ctx->response_sample->GetBytes(&expected_bits, "ReturnedBits") ||
!t->ExpectBytesEqual(expected_bits.data(), expected_bits.size(),
out.data(), out.size())) {
t->PrintLine("result doesn't match");
return false;
}
}

return true;
}

static int usage(char *arg) {
fprintf(stderr, "usage: %s <test file> [<sample response file>]\n", arg);
return 1;
}

int main(int argc, char **argv) {
CRYPTO_library_init();

if (argc != 2 && argc != 3) {
return usage(argv[0]);
}

TestCtx ctx = {nullptr};

if (argc == 3) {
ctx.response_sample.reset(new FileTest(argv[2]));
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(TestCTRDRBG, &ctx, argv[1]);
}
7 changes: 7 additions & 0 deletions fipsoracle/run_cavp.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,16 @@ var shaMonteTests = testSuite{
},
}

var ctrDRBGTests = testSuite{
"DRBG800-90A",
"cavp_ctr_drbg_test",
[]test{{"CTR_DRBG", nil, false}},
}

var allTestSuites = []*testSuite{
&aesGCMTests,
&aesTests,
&ctrDRBGTests,
&ecdsa2PKVTests,
&ecdsa2SigVerTests,
&shaTests,
Expand Down

0 comments on commit b387e22

Please sign in to comment.