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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

92.54
/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
extern "C" {
23

24
using namespace Botan_FFI;
25

26
int botan_rng_init(botan_rng_t* rng_out, const char* rng_type) {
63✔
27
   return ffi_guard_thunk(__func__, [=]() -> int {
63✔
28
      if(rng_out == nullptr)
125✔
29
         return BOTAN_FFI_ERROR_NULL_POINTER;
30

31
      const std::string rng_type_s(rng_type ? rng_type : "system");
126✔
32

33
      std::unique_ptr<Botan::RandomNumberGenerator> rng;
63✔
34

35
      if(rng_type_s == "system") {
63✔
36
         rng = std::make_unique<Botan::System_RNG>();
55✔
37
      } else if(rng_type_s == "user" || rng_type_s == "user-threadsafe") {
8✔
38
         rng = std::make_unique<Botan::AutoSeeded_RNG>();
5✔
39
      } else if(rng_type_s == "null") {
3✔
40
         rng = std::make_unique<Botan::Null_RNG>();
1✔
41
      }
42
#if defined(BOTAN_HAS_PROCESSOR_RNG)
43
      else if((rng_type_s == "rdrand" || rng_type_s == "hwrng") && Botan::Processor_RNG::available()) {
2✔
44
         rng = std::make_unique<Botan::Processor_RNG>();
1✔
45
      }
46
#endif
47

48
      if(!rng) {
63✔
49
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
50
      }
51

52
      *rng_out = new botan_rng_struct(std::move(rng));
62✔
53
      return BOTAN_FFI_SUCCESS;
62✔
54
   });
189✔
55
}
56

57
int botan_rng_init_custom(botan_rng_t* rng_out,
1✔
58
                          const char* rng_name,
59
                          void* context,
60
                          int (*get_cb)(void* context, uint8_t* out, size_t out_len),
61
                          int (*add_entropy_cb)(void* context, const uint8_t input[], size_t length),
62
                          void (*destroy_cb)(void* context)) {
63
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
64
      if(rng_out == nullptr)
2✔
65
         return BOTAN_FFI_ERROR_NULL_POINTER;
66

67
      if(rng_name == nullptr)
1✔
68
         return BOTAN_FFI_ERROR_NULL_POINTER;
69

70
      if(get_cb == nullptr)
1✔
71
         return BOTAN_FFI_ERROR_NULL_POINTER;
72

73
      class Custom_RNG : public Botan::RandomNumberGenerator {
1✔
74
         public:
75
            Custom_RNG(std::string_view name,
1✔
76
                       void* context,
77
                       int (*get_cb)(void* context, uint8_t* out, size_t out_len),
78
                       int (*add_entropy_cb)(void* context, const uint8_t input[], size_t length),
79
                       void (*destroy_cb)(void* context)) :
1✔
80
                  m_name(name) {
1✔
81
               m_context = context;
1✔
82
               m_get_cb = get_cb;
1✔
83
               m_add_entropy_cb = add_entropy_cb;
1✔
84
               m_destroy_cb = destroy_cb;
1✔
85
            }
1✔
86

87
            ~Custom_RNG() override {
2✔
88
               if(m_destroy_cb) {
1✔
89
                  m_destroy_cb(m_context);
1✔
90
               }
91
            }
5✔
92

93
            Custom_RNG(const Custom_RNG& other) = delete;
94
            Custom_RNG(Custom_RNG&& other) = delete;
95
            Custom_RNG& operator=(const Custom_RNG& other) = delete;
96
            Custom_RNG& operator=(Custom_RNG&& other) = delete;
97

98
         protected:
99
            void fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> input) override {
4✔
100
               if(accepts_input() && !input.empty()) {
4✔
101
                  int rc = m_add_entropy_cb(m_context, input.data(), input.size());
3✔
102
                  if(rc) {
3✔
103
                     throw Botan::Invalid_State("Failed to add entropy via C callback, rc=" + std::to_string(rc));
×
104
                  }
105
               }
106

107
               if(!output.empty()) {
4✔
108
                  int rc = m_get_cb(m_context, output.data(), output.size());
1✔
109
                  if(rc) {
1✔
110
                     throw Botan::Invalid_State("Failed to get random from C callback, rc=" + std::to_string(rc));
×
111
                  }
112
               }
113
            }
4✔
114

115
         public:
116
            bool accepts_input() const override { return m_add_entropy_cb != nullptr; }
4✔
117

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

120
            void clear() override {}
×
121

122
            bool is_seeded() const override { return true; }
×
123

124
         private:
125
            std::string m_name;
126
            void* m_context;
127
            std::function<int(void* context, uint8_t* out, size_t out_len)> m_get_cb;
128
            std::function<int(void* context, const uint8_t input[], size_t length)> m_add_entropy_cb;
129
            std::function<void(void* context)> m_destroy_cb;
130
      };
131

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

134
      *rng_out = new botan_rng_struct(std::move(rng));
1✔
135
      return BOTAN_FFI_SUCCESS;
1✔
136
   });
2✔
137
}
138

139
int botan_rng_destroy(botan_rng_t rng) { return BOTAN_FFI_CHECKED_DELETE(rng); }
63✔
140

141
int botan_rng_get(botan_rng_t rng, uint8_t* out, size_t out_len) {
34✔
142
   return BOTAN_FFI_VISIT(rng, [=](auto& r) { r.randomize(out, out_len); });
68✔
143
}
144

145
int botan_system_rng_get(uint8_t* out, size_t out_len) {
1✔
146
   return ffi_guard_thunk(__func__, [=]() -> int {
1✔
147
      Botan::system_rng().randomize(out, out_len);
1✔
148
      return BOTAN_FFI_SUCCESS;
1✔
149
   });
1✔
150
}
151

152
int botan_rng_reseed(botan_rng_t rng, size_t bits) {
3✔
153
   return BOTAN_FFI_VISIT(rng, [=](auto& r) { r.reseed_from_rng(Botan::system_rng(), bits); });
6✔
154
}
155

156
int botan_rng_add_entropy(botan_rng_t rng, const uint8_t* input, size_t len) {
3✔
157
   return BOTAN_FFI_VISIT(rng, [=](auto& r) { r.add_entropy(input, len); });
6✔
158
}
159

160
int botan_rng_reseed_from_rng(botan_rng_t rng, botan_rng_t source_rng, size_t bits) {
5✔
161
   return BOTAN_FFI_VISIT(rng, [=](auto& r) { r.reseed_from_rng(safe_get(source_rng), bits); });
10✔
162
}
163
}
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

© 2026 Coveralls, Inc