aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_rng.cpp
diff options
context:
space:
mode:
authorTomasz Frydrych <[email protected]>2017-05-01 15:03:52 +0200
committerTomasz Frydrych <[email protected]>2017-05-01 15:03:52 +0200
commit87c0fae706140b0a2a7a3fda9dd813474172fa2a (patch)
tree28930efab4d290dcebb89666d46050edd06703fd /src/tests/test_rng.cpp
parent29cc6bebe132a34f882d450b35a69bf71bb3e27b (diff)
Reformat code with astyle + fix code style
Diffstat (limited to 'src/tests/test_rng.cpp')
-rw-r--r--src/tests/test_rng.cpp98
1 files changed, 60 insertions, 38 deletions
diff --git a/src/tests/test_rng.cpp b/src/tests/test_rng.cpp
index d057f0e55..ed432944a 100644
--- a/src/tests/test_rng.cpp
+++ b/src/tests/test_rng.cpp
@@ -9,20 +9,20 @@
#include "test_rng.h"
#if defined(BOTAN_HAS_HMAC_DRBG)
- #include <botan/hmac_drbg.h>
+ #include <botan/hmac_drbg.h>
#endif
#if defined(BOTAN_HAS_AUTO_RNG)
- #include <botan/auto_rng.h>
+ #include <botan/auto_rng.h>
#endif
#if defined(BOTAN_HAS_ENTROPY_SOURCE)
- #include <botan/entropy_src.h>
+ #include <botan/entropy_src.h>
#endif
#if defined(BOTAN_TARGET_OS_TYPE_IS_UNIX)
- #include <unistd.h>
- #include <sys/wait.h>
+ #include <unistd.h>
+ #include <sys/wait.h>
#endif
#include <iostream>
@@ -36,9 +36,10 @@ namespace {
class HMAC_DRBG_Tests : public Text_Based_Test
{
public:
- HMAC_DRBG_Tests() : Text_Based_Test("hmac_drbg.vec",
- "EntropyInput,EntropyInputReseed,Out",
- "AdditionalInput1,AdditionalInput2") {}
+ HMAC_DRBG_Tests()
+ : Text_Based_Test("hmac_drbg.vec",
+ "EntropyInput,EntropyInputReseed,Out",
+ "AdditionalInput1,AdditionalInput2") {}
Test::Result run_one_test(const std::string& algo, const VarMap& vars) override
{
@@ -83,7 +84,10 @@ class HMAC_DRBG_Unit_Tests : public Test
class Broken_Entropy_Source : public Botan::Entropy_Source
{
public:
- std::string name() const override { return "Broken Entropy Source"; }
+ std::string name() const override
+ {
+ return "Broken Entropy Source";
+ }
size_t poll(Botan::RandomNumberGenerator&) override
{
@@ -94,7 +98,10 @@ class HMAC_DRBG_Unit_Tests : public Test
class Insufficient_Entropy_Source : public Botan::Entropy_Source
{
public:
- std::string name() const override { return "Insufficient Entropy Source"; }
+ std::string name() const override
+ {
+ return "Insufficient Entropy Source";
+ }
size_t poll(Botan::RandomNumberGenerator&) override
{
@@ -107,7 +114,10 @@ class HMAC_DRBG_Unit_Tests : public Test
public:
Request_Counting_RNG() : m_randomize_count(0) {}
- bool is_seeded() const override { return true; }
+ bool is_seeded() const override
+ {
+ return true;
+ }
void clear() override
{
@@ -121,9 +131,15 @@ class HMAC_DRBG_Unit_Tests : public Test
void add_entropy(const uint8_t[], size_t) override {}
- std::string name() const override { return "Request_Counting_RNG"; }
+ std::string name() const override
+ {
+ return "Request_Counting_RNG";
+ }
- size_t randomize_count() { return m_randomize_count; }
+ size_t randomize_count()
+ {
+ return m_randomize_count;
+ }
private:
size_t m_randomize_count;
@@ -144,14 +160,18 @@ class HMAC_DRBG_Unit_Tests : public Test
Request_Counting_RNG counting_rng;
Botan::HMAC_DRBG rng(std::move(mac), counting_rng, Botan::Entropy_Sources::global_sources(), 2);
Botan::secure_vector<uint8_t> seed_input(
- {0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF,
- 0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF});
+ {
+ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
+ 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
+ });
Botan::secure_vector<uint8_t> output_after_initialization(
- {0x48,0xD3,0xB4,0x5A,0xAB,0x65,0xEF,0x92,0xCC,0xFC,0xB9,0x42,0x7E,0xF2,0x0C,0x90,
- 0x29,0x70,0x65,0xEC,0xC1,0xB8,0xA5,0x25,0xBF,0xE4,0xDC,0x6F,0xF3,0x6D,0x0E,0x38});
+ {
+ 0x48, 0xD3, 0xB4, 0x5A, 0xAB, 0x65, 0xEF, 0x92, 0xCC, 0xFC, 0xB9, 0x42, 0x7E, 0xF2, 0x0C, 0x90,
+ 0x29, 0x70, 0x65, 0xEC, 0xC1, 0xB8, 0xA5, 0x25, 0xBF, 0xE4, 0xDC, 0x6F, 0xF3, 0x6D, 0x0E, 0x38
+ });
Botan::secure_vector<uint8_t> output_without_reseed(
- {0xC4,0x90,0x04,0x5B,0x35,0x4F,0x50,0x09,0x68,0x45,0xF0,0x4B,0x11,0x03,0x58,0xF0});
- result.test_eq("is_seeded",rng.is_seeded(),false);
+ {0xC4, 0x90, 0x04, 0x5B, 0x35, 0x4F, 0x50, 0x09, 0x68, 0x45, 0xF0, 0x4B, 0x11, 0x03, 0x58, 0xF0});
+ result.test_eq("is_seeded", rng.is_seeded(), false);
rng.initialize_with(seed_input.data(), seed_input.size());
@@ -200,10 +220,10 @@ class HMAC_DRBG_Unit_Tests : public Test
result.test_eq("still second reseed", counting_rng.randomize_count(), 3);
// request > max_number_of_bits_per_request, do reseeds occur?
- rng.random_vec(64*1024 + 1);
+ rng.random_vec(64 * 1024 + 1);
result.test_eq("request exceeds output limit", counting_rng.randomize_count(), 4);
- rng.random_vec(9*64*1024 + 1);
+ rng.random_vec(9 * 64 * 1024 + 1);
result.test_eq("request exceeds output limit", counting_rng.randomize_count(), 9);
return result;
@@ -283,7 +303,7 @@ class HMAC_DRBG_Unit_Tests : public Test
Botan::Null_RNG broken_entropy_input_rng;
Botan::HMAC_DRBG rng_with_broken_rng(std::move(mac), broken_entropy_input_rng, reseed_interval);
- result.test_throws("broken underlying rng", [&rng_with_broken_rng] () { rng_with_broken_rng.random_vec(16); });
+ result.test_throws("broken underlying rng", [&rng_with_broken_rng]() { rng_with_broken_rng.random_vec(16); });
// entropy_sources throw exception
std::unique_ptr<Broken_Entropy_Source> broken_entropy_source_1(new Broken_Entropy_Source());
@@ -295,7 +315,7 @@ class HMAC_DRBG_Unit_Tests : public Test
mac = Botan::MessageAuthenticationCode::create("HMAC(SHA-256)");
Botan::HMAC_DRBG rng_with_broken_es(std::move(mac), broken_entropy_sources, reseed_interval);
- result.test_throws("broken entropy sources", [&rng_with_broken_es] () { rng_with_broken_es.random_vec(16); });
+ result.test_throws("broken entropy sources", [&rng_with_broken_es]() { rng_with_broken_es.random_vec(16); });
// entropy source returns insufficient entropy
Botan::Entropy_Sources insufficient_entropy_sources;
@@ -304,24 +324,25 @@ class HMAC_DRBG_Unit_Tests : public Test
mac = Botan::MessageAuthenticationCode::create("HMAC(SHA-256)");
Botan::HMAC_DRBG rng_with_insufficient_es(std::move(mac), insufficient_entropy_sources, reseed_interval);
- result.test_throws("insufficient entropy source", [&rng_with_insufficient_es] () { rng_with_insufficient_es.random_vec(16); });
+ result.test_throws("insufficient entropy source", [&rng_with_insufficient_es]() { rng_with_insufficient_es.random_vec(16); });
// one of or both underlying_rng and entropy_sources throw exception
mac = Botan::MessageAuthenticationCode::create("HMAC(SHA-256)");
Botan::HMAC_DRBG rng_with_broken_rng_and_es(std::move(mac), broken_entropy_input_rng,
Botan::Entropy_Sources::global_sources(), reseed_interval);
- result.test_throws("broken underlying rng but good entropy sources", [&rng_with_broken_rng_and_es] ()
- { rng_with_broken_rng_and_es.random_vec(16); });
+ result.test_throws("broken underlying rng but good entropy sources", [&rng_with_broken_rng_and_es]()
+ { rng_with_broken_rng_and_es.random_vec(16); });
mac = Botan::MessageAuthenticationCode::create("HMAC(SHA-256)");
Botan::HMAC_DRBG rng_with_rng_and_broken_es(std::move(mac), Test::rng(), broken_entropy_sources, reseed_interval);
- result.test_throws("good underlying rng but broken entropy sources", [&rng_with_rng_and_broken_es] ()
- { rng_with_rng_and_broken_es.random_vec(16); });
+ result.test_throws("good underlying rng but broken entropy sources", [&rng_with_rng_and_broken_es]()
+ { rng_with_rng_and_broken_es.random_vec(16); });
mac = Botan::MessageAuthenticationCode::create("HMAC(SHA-256)");
- Botan::HMAC_DRBG rng_with_broken_rng_and_broken_es(std::move(mac), broken_entropy_input_rng, broken_entropy_sources, reseed_interval);
- result.test_throws("underlying rng and entropy sources broken", [&rng_with_broken_rng_and_broken_es] ()
- { rng_with_broken_rng_and_broken_es.random_vec(16); });
+ Botan::HMAC_DRBG rng_with_broken_rng_and_broken_es(std::move(mac), broken_entropy_input_rng, broken_entropy_sources,
+ reseed_interval);
+ result.test_throws("underlying rng and entropy sources broken", [&rng_with_broken_rng_and_broken_es]()
+ { rng_with_broken_rng_and_broken_es.random_vec(16); });
return result;
}
@@ -340,7 +361,7 @@ class HMAC_DRBG_Unit_Tests : public Test
// make sure the nonce has at least 1/2*security_strength bits
// SHA-256 -> 256 bits security strength
- for( auto nonce_size : { 0, 4, 8, 16, 31, 32, 34 } )
+ for(auto nonce_size : { 0, 4, 8, 16, 31, 32, 34 })
{
if(!mac)
{
@@ -355,7 +376,7 @@ class HMAC_DRBG_Unit_Tests : public Test
if(nonce_size < 32)
{
result.test_eq("not seeded", rng.is_seeded(), false);
- result.test_throws("invalid nonce size", [&rng, &nonce] () { rng.random_vec(32); });
+ result.test_throws("invalid nonce size", [&rng, &nonce]() { rng.random_vec(32); });
}
else
{
@@ -426,12 +447,12 @@ class HMAC_DRBG_Unit_Tests : public Test
}
pid_t pid = ::fork();
- if ( pid == -1 )
+ if(pid == -1)
{
result.test_failure("failed to fork process");
return result;
}
- else if ( pid != 0 )
+ else if(pid != 0)
{
// parent process, wait for randomize_count from child's rng
::close(fd[1]); // close write end in parent
@@ -473,9 +494,10 @@ class HMAC_DRBG_Unit_Tests : public Test
rng.randomize(&child_bytes[0], child_bytes.size());
count = counting_rng.randomize_count();
ssize_t written = ::write(fd[1], &count, sizeof(count));
- try {
+ try
+ {
rng.randomize(&child_bytes[0], child_bytes.size());
- }
+ }
catch(std::exception& e)
{
fprintf(stderr, "%s", e.what());
@@ -496,7 +518,7 @@ class HMAC_DRBG_Unit_Tests : public Test
std::vector<std::string> approved_hash_fns { "SHA-160", "SHA-224", "SHA-256", "SHA-512/256", "SHA-384", "SHA-512" };
std::vector<uint32_t> security_strengths { 128, 192, 256, 256, 256, 256 };
- for( size_t i = 0; i < approved_hash_fns.size(); ++i )
+ for(size_t i = 0; i < approved_hash_fns.size(); ++i)
{
std::string hash_fn = approved_hash_fns[i];
std::string mac_name = "HMAC(" + hash_fn + ")";