Add CTR-DRBG CAVP test driver.
Change-Id: I14c554eaf1e431271c5e981e2337b937c6cdf012
Reviewed-on: https://boringssl-review.googlesource.com/15645
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/fipsoracle/cavp_ctr_drbg_test.cc b/fipsoracle/cavp_ctr_drbg_test.cc
new file mode 100644
index 0000000..7a6124a
--- /dev/null
+++ b/fipsoracle/cavp_ctr_drbg_test.cc
@@ -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]);
+}