• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 12292176937

12 Dec 2024 08:02AM UTC coverage: 91.254% (-0.007%) from 91.261%
12292176937

Pull #4309

github

web-flow
Merge f3d19f78d into 281c77941
Pull Request #4309: Add Entropy Source and DRNG Manager (ESDM) RNG support

93435 of 102390 relevant lines covered (91.25%)

11397234.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.24
/src/lib/ffi/ffi_rng.cpp
1
/*
2
* (C) 2015,2017 Jack Lloyd
3
* (C) 2021 René Fischer
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/ffi.h>
9

10
#include <botan/auto_rng.h>
11
#include <botan/system_rng.h>
12
#include <botan/internal/ffi_rng.h>
13
#include <botan/internal/ffi_util.h>
14

15
#include <functional>
16
#include <memory>
17

18
#if defined(BOTAN_HAS_PROCESSOR_RNG)
19
   #include <botan/processor_rng.h>
20
#endif
21

22
#if defined(BOTAN_HAS_JITTER_RNG)
23
   #include <botan/jitter_rng.h>
24
#endif
25
#if defined(BOTAN_HAS_ESDM_RNG)
26
   #include <botan/esdm_rng.h>
27
#endif
28

29
extern "C" {
30

31
using namespace Botan_FFI;
32

33
int botan_rng_init(botan_rng_t* rng_out, const char* rng_type) {
86✔
34
   return ffi_guard_thunk(__func__, [=]() -> int {
86✔
35
      if(rng_out == nullptr) {
86✔
36
         return BOTAN_FFI_ERROR_NULL_POINTER;
37
      }
38

39
      const std::string rng_type_s(rng_type ? rng_type : "system");
172✔
40

41
      std::unique_ptr<Botan::RandomNumberGenerator> rng;
86✔
42

43
      if(rng_type_s == "system") {
86✔
44
         rng = std::make_unique<Botan::System_RNG>();
69✔
45
      } else if(rng_type_s == "user" || rng_type_s == "user-threadsafe") {
17✔
46
         rng = std::make_unique<Botan::AutoSeeded_RNG>();
11✔
47
      } else if(rng_type_s == "null") {
6✔
48
         rng = std::make_unique<Botan::Null_RNG>();
1✔
49
      }
50
#if defined(BOTAN_HAS_PROCESSOR_RNG)
51
      else if((rng_type_s == "rdrand" || rng_type_s == "hwrng") && Botan::Processor_RNG::available()) {
5✔
52
         rng = std::make_unique<Botan::Processor_RNG>();
1✔
53
      }
54
#endif
55
#if defined(BOTAN_HAS_JITTER_RNG)
56
      else if(rng_type_s == "jitter") {
4✔
57
         rng = std::make_unique<Botan::Jitter_RNG>();
1✔
58
      }
59
#endif
60
#if defined(BOTAN_HAS_ESDM_RNG)
61
      else if(rng_type_s == "esdm-full") {
3✔
62
         rng = std::make_unique<Botan::ESDM_RNG>(false);
1✔
63
      } else if(rng_type_s == "esdm-pr") {
2✔
64
         rng = std::make_unique<Botan::ESDM_RNG>(true);
1✔
65
      }
66
#endif
67

68
      if(!rng) {
86✔
69
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
70
      }
71

72
      *rng_out = new botan_rng_struct(std::move(rng));
85✔
73
      return BOTAN_FFI_SUCCESS;
85✔
74
   });
172✔
75
}
76

77
int botan_rng_init_custom(botan_rng_t* rng_out,
1✔
78
                          const char* rng_name,
79
                          void* context,
80
                          int (*get_cb)(void* context, uint8_t* out, size_t out_len),
81
                          int (*add_entropy_cb)(void* context, const uint8_t input[], size_t length),
82
                          void (*destroy_cb)(void* context)) {
83
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
84
      if(rng_out == nullptr) {
1✔
85
         return BOTAN_FFI_ERROR_NULL_POINTER;
86
      }
87

88
      if(rng_name == nullptr) {
1✔
89
         return BOTAN_FFI_ERROR_NULL_POINTER;
90
      }
91

92
      if(get_cb == nullptr) {
1✔
93
         return BOTAN_FFI_ERROR_NULL_POINTER;
94
      }
95

96
      class Custom_RNG : public Botan::RandomNumberGenerator {
1✔
97
         public:
98
            Custom_RNG(std::string_view name,
1✔
99
                       void* context,
100
                       int (*get_cb)(void* context, uint8_t* out, size_t out_len),
101
                       int (*add_entropy_cb)(void* context, const uint8_t input[], size_t length),
102
                       void (*destroy_cb)(void* context)) :
1✔
103
                  m_name(name) {
1✔
104
               m_context = context;
1✔
105
               m_get_cb = get_cb;
1✔
106
               m_add_entropy_cb = add_entropy_cb;
1✔
107
               m_destroy_cb = destroy_cb;
1✔
108
            }
1✔
109

110
            ~Custom_RNG() override {
2✔
111
               if(m_destroy_cb) {
1✔
112
                  m_destroy_cb(m_context);
1✔
113
               }
114
            }
5✔
115

116
            Custom_RNG(const Custom_RNG& other) = delete;
117
            Custom_RNG(Custom_RNG&& other) = delete;
118
            Custom_RNG& operator=(const Custom_RNG& other) = delete;
119
            Custom_RNG& operator=(Custom_RNG&& other) = delete;
120

121
         protected:
122
            void fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> input) override {
4✔
123
               if(accepts_input() && !input.empty()) {
4✔
124
                  int rc = m_add_entropy_cb(m_context, input.data(), input.size());
3✔
125
                  if(rc) {
3✔
126
                     throw Botan::Invalid_State("Failed to add entropy via C callback, rc=" + std::to_string(rc));
×
127
                  }
128
               }
129

130
               if(!output.empty()) {
4✔
131
                  int rc = m_get_cb(m_context, output.data(), output.size());
1✔
132
                  if(rc) {
1✔
133
                     throw Botan::Invalid_State("Failed to get random from C callback, rc=" + std::to_string(rc));
×
134
                  }
135
               }
136
            }
4✔
137

138
         public:
139
            bool accepts_input() const override { return m_add_entropy_cb != nullptr; }
4✔
140

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

143
            void clear() override {}
×
144

145
            bool is_seeded() const override { return true; }
×
146

147
         private:
148
            std::string m_name;
149
            void* m_context;
150
            std::function<int(void* context, uint8_t* out, size_t out_len)> m_get_cb;
151
            std::function<int(void* context, const uint8_t input[], size_t length)> m_add_entropy_cb;
152
            std::function<void(void* context)> m_destroy_cb;
153
      };
154

155
      auto rng = std::make_unique<Custom_RNG>(rng_name, context, get_cb, add_entropy_cb, destroy_cb);
1✔
156

157
      *rng_out = new botan_rng_struct(std::move(rng));
1✔
158
      return BOTAN_FFI_SUCCESS;
1✔
159
   });
2✔
160
}
161

162
int botan_rng_destroy(botan_rng_t rng) {
97✔
163
   return BOTAN_FFI_CHECKED_DELETE(rng);
97✔
164
}
165

166
int botan_rng_get(botan_rng_t rng, uint8_t* out, size_t out_len) {
66✔
167
   return BOTAN_FFI_VISIT(rng, [=](auto& r) { r.randomize(out, out_len); });
132✔
168
}
169

170
int botan_system_rng_get(uint8_t* out, size_t out_len) {
1✔
171
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
172
      Botan::system_rng().randomize(out, out_len);
1✔
173
      return BOTAN_FFI_SUCCESS;
1✔
174
   });
1✔
175
}
176

177
int botan_rng_reseed(botan_rng_t rng, size_t bits) {
7✔
178
   return BOTAN_FFI_VISIT(rng, [=](auto& r) { r.reseed_from_rng(Botan::system_rng(), bits); });
14✔
179
}
180

181
int botan_rng_add_entropy(botan_rng_t rng, const uint8_t* input, size_t len) {
8✔
182
   return BOTAN_FFI_VISIT(rng, [=](auto& r) { r.add_entropy(input, len); });
16✔
183
}
184

185
int botan_rng_reseed_from_rng(botan_rng_t rng, botan_rng_t source_rng, size_t bits) {
9✔
186
   return BOTAN_FFI_VISIT(rng, [=](auto& r) { r.reseed_from_rng(safe_get(source_rng), bits); });
18✔
187
}
188
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc