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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 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

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

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

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

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

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

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

69
      if(rng_name == nullptr) {
1✔
70
         return BOTAN_FFI_ERROR_NULL_POINTER;
71
      }
72

73
      if(get_cb == nullptr) {
1✔
74
         return BOTAN_FFI_ERROR_NULL_POINTER;
75
      }
76

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

91
            ~Custom_RNG() override {
2✔
92
               if(m_destroy_cb) {
1✔
93
                  m_destroy_cb(m_context);
1✔
94
               }
95
            }
5✔
96

97
            Custom_RNG(const Custom_RNG& other) = delete;
98
            Custom_RNG(Custom_RNG&& other) = delete;
99
            Custom_RNG& operator=(const Custom_RNG& other) = delete;
100
            Custom_RNG& operator=(Custom_RNG&& other) = delete;
101

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

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

119
         public:
120
            bool accepts_input() const override { return m_add_entropy_cb != nullptr; }
4✔
121

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

124
            void clear() override {}
×
125

126
            bool is_seeded() const override { return true; }
×
127

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

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

138
      *rng_out = new botan_rng_struct(std::move(rng));
1✔
139
      return BOTAN_FFI_SUCCESS;
1✔
140
   });
2✔
141
}
142

143
int botan_rng_destroy(botan_rng_t rng) { return BOTAN_FFI_CHECKED_DELETE(rng); }
63✔
144

145
int botan_rng_get(botan_rng_t rng, uint8_t* out, size_t out_len) {
34✔
146
   return BOTAN_FFI_VISIT(rng, [=](auto& r) { r.randomize(out, out_len); });
68✔
147
}
148

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

156
int botan_rng_reseed(botan_rng_t rng, size_t bits) {
3✔
157
   return BOTAN_FFI_VISIT(rng, [=](auto& r) { r.reseed_from_rng(Botan::system_rng(), bits); });
6✔
158
}
159

160
int botan_rng_add_entropy(botan_rng_t rng, const uint8_t* input, size_t len) {
3✔
161
   return BOTAN_FFI_VISIT(rng, [=](auto& r) { r.add_entropy(input, len); });
6✔
162
}
163

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