aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/test_rng.cpp
blob: 84e291c6a784752392bfed4e25a8866e152c4aef (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
64
65
66
67
68
69
70
71
#include "tests.h"
#include "test_rng.h"

#include <botan/libstate.h>
#include <botan/hex.h>
#include <iostream>
#include <fstream>

#if defined(BOTAN_HAS_X931_RNG)
  #include <botan/x931_rng.h>
  #include <botan/aes.h>
  #include <botan/des.h>
#endif

using namespace Botan;

namespace {

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

#if defined(BOTAN_HAS_X931_RNG)
   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));
#endif

   return nullptr;
   }

size_t x931_test(const std::string& algo,
                 const std::string& ikm,
                 const std::string& out,
                 size_t L)
   {
   std::unique_ptr<RandomNumberGenerator> rng(get_rng(algo, ikm));

   if(!rng)
      throw std::runtime_error("Unknown RNG " + algo);

   rng->reseed(0);

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

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

   return 0;
   }

}

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

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