aboutsummaryrefslogtreecommitdiffstats
path: root/src/init_def.cpp
blob: 6faa5d86fb62ec810cdfc5566d054f6a3916a89e (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*************************************************
* Default Initialization Function Source File    *
* (C) 1999-2006 The Botan Project                *
*************************************************/

#include <botan/init.h>
#include <botan/libstate.h>
#include <botan/modules.h>
#include <botan/config.h>
#include <botan/defalloc.h>
#include <botan/fips140.h>
#include <botan/x931_rng.h>
#include <botan/def_char.h>

namespace Botan {

/*************************************************
* Library Initialization                         *
*************************************************/
void LibraryInitializer::initialize(const std::string& arg_string)
   {
   InitializerOptions args(arg_string);
   initialize(args);
   }

/*************************************************
* Library Initialization                         *
*************************************************/
void LibraryInitializer::initialize(const InitializerOptions& args)
   {
   try
      {
      Builtin_Modules modules(args);
      initialize(args, modules);
      }
   catch(...)
      {
      deinitialize();
      throw;
      }
   }

/*************************************************
* Library Initialization                         *
*************************************************/
void LibraryInitializer::initialize(const InitializerOptions& args,
                                    Modules& modules)
   {
   try
      {
      set_global_state(
         new Library_State(
            args.thread_safe() ?
               modules.mutex_factory() :
               new Default_Mutex_Factory
            )
         );

      global_state().config().load_defaults();
      if(args.config_file() != "")
         global_config().load_inifile(args.config_file());

      global_state().load(modules);
      global_state().set_prng(new ANSI_X931_RNG);

      if(args.seed_rng())
         {
         const u32bit min_entropy =
            global_config().option_as_u32bit("rng/min_entropy");

         if(min_entropy != 0)
            {
            u32bit bits_so_far = 0;

            for(u32bit j = 0; j != 4; ++j)
               {
               u32bit to_get = min_entropy - bits_so_far;

               bits_so_far += global_state().seed_prng(true, to_get);

               if(bits_so_far >= min_entropy)
                  break;
               }

            if(bits_so_far < min_entropy)
               throw PRNG_Unseeded("Unable to collect sufficient entropy");
            }
         }

      if(args.fips_mode() || args.self_test())
         {
         if(!FIPS140::passes_self_tests())
            throw Self_Test_Failure("FIPS-140 startup tests");
         }
      }
   catch(...)
      {
      deinitialize();
      throw;
      }
   }

/*************************************************
* Library Shutdown                               *
*************************************************/
void LibraryInitializer::deinitialize()
   {
   set_global_state(0);
   }

}