boringssl/crypto/fipsmodule/rand/ctrdrbg_vector_test.cc
Adam Langley 7784104dd8 Move much of rand/ into the FIPS module.
Support for platforms that we don't support FIPS on doesn't need to be
in the module. Also, functions for dealing with whether fork-unsafe
buffering is enabled are left out because they aren't implementing any
cryptography and they use global r/w state, making their inclusion
painful.

Change-Id: I71a0123db6f5449e9dfc7ec7dea0944428e661aa
Reviewed-on: https://boringssl-review.googlesource.com/15084
Reviewed-by: Adam Langley <agl@google.com>
2017-04-21 22:03:18 +00:00

74 lines
2.5 KiB
C++

/* 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. */
#include <openssl/rand.h>
#include <openssl/crypto.h>
#include "internal.h"
#include "../../test/test_util.h"
#include "../../test/file_test.h"
static bool TestCTRDRBG(FileTest *t, void *arg) {
std::vector<uint8_t> seed, personalisation, reseed, ai_reseed, ai1, ai2,
expected;
if (!t->GetBytes(&seed, "EntropyInput") ||
!t->GetBytes(&personalisation, "PersonalizationString") ||
!t->GetBytes(&reseed, "EntropyInputReseed") ||
!t->GetBytes(&ai_reseed, "AdditionalInputReseed") ||
!t->GetBytes(&ai1, "AdditionalInput1") ||
!t->GetBytes(&ai2, "AdditionalInput2") ||
!t->GetBytes(&expected, "ReturnedBits")) {
t->PrintLine("missing value");
return false;
}
if (seed.size() != CTR_DRBG_ENTROPY_LEN ||
reseed.size() != CTR_DRBG_ENTROPY_LEN) {
t->PrintLine("bad seed length");
return false;
}
CTR_DRBG_STATE drbg;
CTR_DRBG_init(&drbg, seed.data(),
personalisation.size() > 0 ? personalisation.data() : nullptr,
personalisation.size());
CTR_DRBG_reseed(&drbg, reseed.data(),
ai_reseed.size() > 0 ? ai_reseed.data() : nullptr,
ai_reseed.size());
std::vector<uint8_t> out;
out.resize(expected.size());
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());
return t->ExpectBytesEqual(expected.data(), expected.size(), out.data(),
out.size());
}
int main(int argc, char **argv) {
CRYPTO_library_init();
if (argc != 2) {
fprintf(stderr, "%s <test file>\n", argv[0]);
return 1;
}
return FileTestMain(TestCTRDRBG, nullptr, argv[1]);
}