summaryrefslogtreecommitdiffstats
path: root/comm/third_party/botan/src/lib/ffi/ffi_rng.cpp
blob: 7bb365a248571a9f4a4312e5d6e3d24b95fd68a7 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* (C) 2015,2017 Jack Lloyd
* (C) 2021 René Fischer
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/ffi.h>
#include <botan/internal/ffi_util.h>
#include <botan/internal/ffi_rng.h>
#include <botan/system_rng.h>
#include <botan/auto_rng.h>

#include <functional>

#if defined(BOTAN_HAS_PROCESSOR_RNG)
   #include <botan/processor_rng.h>
#endif

extern "C" {

using namespace Botan_FFI;

int botan_rng_init(botan_rng_t* rng_out, const char* rng_type)
   {
   return ffi_guard_thunk(__func__, [=]() -> int {
      if(rng_out == nullptr)
         return BOTAN_FFI_ERROR_NULL_POINTER;

      const std::string rng_type_s(rng_type ? rng_type : "system");

      std::unique_ptr<Botan::RandomNumberGenerator> rng;

      if(rng_type_s == "system")
         {
         rng.reset(new Botan::System_RNG);
         }
      else if(rng_type_s == "user" || rng_type_s == "user-threadsafe")
         {
         rng.reset(new Botan::AutoSeeded_RNG);
         }
      else if(rng_type_s == "null")
         {
         rng.reset(new Botan::Null_RNG);
         }
#if defined(BOTAN_HAS_PROCESSOR_RNG)
      else if((rng_type_s == "rdrand" || rng_type_s == "hwrng") && Botan::Processor_RNG::available())
         {
         rng.reset(new Botan::Processor_RNG);
         }
#endif

      if(!rng)
         {
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
         }

      *rng_out = new botan_rng_struct(rng.release());
      return BOTAN_FFI_SUCCESS;
      });
   }

int botan_rng_init_custom(botan_rng_t* rng_out, const char* rng_name, void* context,
                          int(* get_cb)(void* context, uint8_t* out, size_t out_len),
                          int(* add_entropy_cb)(void* context, const uint8_t input[], size_t length),
                          void(* destroy_cb)(void* context))
{
return ffi_guard_thunk(__func__,[=]() -> int {
   if(rng_out == nullptr)
      return BOTAN_FFI_ERROR_NULL_POINTER;

   if(rng_name == nullptr)
      return BOTAN_FFI_ERROR_NULL_POINTER;

   if(get_cb == nullptr)
      return BOTAN_FFI_ERROR_NULL_POINTER;

   class Custom_RNG : public Botan::RandomNumberGenerator
      {
      public:
         Custom_RNG(const std::string& name, void* context,
                    int(* get_cb)(void* context, uint8_t* out, size_t out_len),
                    int(* add_entropy_cb)(void* context, const uint8_t input[], size_t length),
                    void(* destroy_cb)(void* context)) :
            m_name(name)
            {
               m_context = context;
               m_get_cb = get_cb;
               m_add_entropy_cb = add_entropy_cb;
               m_destroy_cb = destroy_cb;
            }

         ~Custom_RNG()
         {
            if(m_destroy_cb)
            {
               m_destroy_cb(m_context);
            }
         }

         void randomize(uint8_t output[], size_t length) override
         {
            int rc = m_get_cb(m_context, output, length);
            if(rc)
            {
               throw Botan::Invalid_State("Failed to get random from C callback, rc=" + std::to_string(rc));
            }
         }

         bool accepts_input() const override
         {
            return m_add_entropy_cb != nullptr;
         }

         void add_entropy(const uint8_t input[], size_t length) override
         {
            if(m_add_entropy_cb == nullptr)
            {
               return;
            }

            int rc = m_add_entropy_cb(m_context, input, length);
            if(rc)
            {
               throw Botan::Invalid_State("Failed to add entropy via C callback, rc=" + std::to_string(rc));
            }
         }

         std::string name() const override
         {
            return m_name;
         }

         void clear() override
         {
         }

         bool is_seeded() const override
         {
            return true;
         }

      private:
         std::string m_name;
         void* m_context;
         std::function<int(void* context, uint8_t* out, size_t out_len)> m_get_cb;
         std::function<int(void* context, const uint8_t input[], size_t length)> m_add_entropy_cb;
         std::function<void(void* context)> m_destroy_cb;
   };

   std::unique_ptr<Botan::RandomNumberGenerator> rng(new Custom_RNG(rng_name, context, get_cb, add_entropy_cb, destroy_cb));

   *rng_out = new botan_rng_struct(rng.release());
   return BOTAN_FFI_SUCCESS;
   });
}

int botan_rng_destroy(botan_rng_t rng)
   {
   return BOTAN_FFI_CHECKED_DELETE(rng);
   }

int botan_rng_get(botan_rng_t rng, uint8_t* out, size_t out_len)
   {
   return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.randomize(out, out_len); });
   }

int botan_rng_reseed(botan_rng_t rng, size_t bits)
   {
   return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.reseed_from_rng(Botan::system_rng(), bits); });
   }

int botan_rng_add_entropy(botan_rng_t rng, const uint8_t* input, size_t len)
   {
   return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.add_entropy(input, len); });
   }

int botan_rng_reseed_from_rng(botan_rng_t rng, botan_rng_t source_rng, size_t bits)
   {
   return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.reseed_from_rng(safe_get(source_rng), bits); });
   }

}