aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_rng.cpp
blob: 99b2fbf8f19758d3710ec2711380960528cd689e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "tests.h"
#include "test_rng.h"

#include <botan/libstate.h>
#include <botan/x931_rng.h>
#include <botan/aes.h>
#include <botan/des.h>
#include <botan/hex.h>
#include <iostream>
#include <fstream>
#include <deque>

using namespace Botan;

namespace {

RandomNumberGenerator* get_x931(const std::string& algo, const std::string& ikm_hex)
   {
   const auto ikm = hex_decode(ikm_hex);

   if(algo == "X9.31-RNG(TripleDES)")
      return new ANSI_X931_RNG(new TripleDES, new Fixed_Output_RNG(ikm));
   else if(algo == "X9.31-RNG(AES-128)")
      return new ANSI_X931_RNG(new AES_128, new Fixed_Output_RNG(ikm));
   else if(algo == "X9.31-RNG(AES-192)")
      return new ANSI_X931_RNG(new AES_192, new Fixed_Output_RNG(ikm));
   else if(algo == "X9.31-RNG(AES-256)")
      return new ANSI_X931_RNG(new AES_256, new Fixed_Output_RNG(ikm));

   return nullptr;
   }

bool x931_test(const std::string& algo,
               const std::string& ikm,
               const std::string& out,
               size_t L)
   {
   std::unique_ptr<RandomNumberGenerator> x931(get_x931(algo, ikm));
   x931->reseed(0);

   const std::string got = hex_encode(x931->random_vec(L));

   if(got != out)
      {
      std::cout << "X9.31 " << got << " != " << out << "\n";
      return false;
      }

   return true;
   }

}

size_t test_rngs()
   {
   std::ifstream vec(CHECKS_DIR "/x931.vec");

   return run_tests_bb(vec, "RNG", "Out", true,
             [](std::map<std::string, std::string> m) -> bool
             {
             return x931_test(m["RNG"], m["IKM"], m["Out"], to_u32bit(m["L"]));
             });
   }