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

randombit / botan / 16248620128

13 Jul 2025 11:27AM UTC coverage: 90.565% (-0.01%) from 90.575%
16248620128

push

github

web-flow
Merge pull request #4983 from randombit/jack/clang-tidy-cppcoreguidelines-owning-memory

Enable and fix clang-tidy warning cppcoreguidelines-owning-memory

99026 of 109342 relevant lines covered (90.57%)

12444574.04 hits per line

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

95.4
/src/lib/ffi/ffi_mp.cpp
1
/*
2
* (C) 2015,2017 Jack Lloyd
3
* (C) 2017 Ribose Inc
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include <botan/ffi.h>
9

10
#include <botan/numthry.h>
11
#include <botan/internal/barrett.h>
12
#include <botan/internal/divide.h>
13
#include <botan/internal/ffi_mp.h>
14
#include <botan/internal/ffi_rng.h>
15
#include <botan/internal/ffi_util.h>
16
#include <botan/internal/mod_inv.h>
17

18
extern "C" {
19

20
using namespace Botan_FFI;
21

22
int botan_mp_init(botan_mp_t* mp_out) {
151✔
23
   return ffi_guard_thunk(__func__, [=]() -> int {
151✔
24
      if(mp_out == nullptr) {
151✔
25
         return BOTAN_FFI_ERROR_NULL_POINTER;
26
      }
27

28
      auto mp = std::make_unique<Botan::BigInt>();
151✔
29
      return ffi_new_object(mp_out, std::move(mp));
151✔
30
   });
151✔
31
}
32

33
int botan_mp_clear(botan_mp_t mp) {
×
34
   return BOTAN_FFI_VISIT(mp, [](auto& bn) { bn.clear(); });
×
35
}
36

37
int botan_mp_set_from_int(botan_mp_t mp, int initial_value) {
2✔
38
   return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn = Botan::BigInt::from_s32(initial_value); });
4✔
39
}
40

41
int botan_mp_set_from_str(botan_mp_t mp, const char* str) {
35✔
42
   return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn = Botan::BigInt(str); });
70✔
43
}
44

45
int botan_mp_set_from_radix_str(botan_mp_t mp, const char* str, size_t radix) {
10✔
46
   return BOTAN_FFI_VISIT(mp, [=](auto& bn) {
20✔
47
      Botan::BigInt::Base base;
48
      if(radix == 10) {
49
         base = Botan::BigInt::Decimal;
50
      } else if(radix == 16) {
51
         base = Botan::BigInt::Hexadecimal;
52
      } else {
53
         return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
54
      }
55

56
      const uint8_t* bytes = Botan::cast_char_ptr_to_uint8(str);
57
      const size_t len = strlen(str);
58

59
      bn = Botan::BigInt(bytes, len, base);
60
      return BOTAN_FFI_SUCCESS;
61
   });
62
}
63

64
int botan_mp_set_from_mp(botan_mp_t dest, const botan_mp_t source) {
4✔
65
   return BOTAN_FFI_VISIT(dest, [=](auto& bn) { bn = safe_get(source); });
8✔
66
}
67

68
int botan_mp_is_negative(const botan_mp_t mp) {
10✔
69
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.is_negative() ? 1 : 0; });
20✔
70
}
71

72
int botan_mp_is_positive(const botan_mp_t mp) {
6✔
73
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.is_positive() ? 1 : 0; });
12✔
74
}
75

76
int botan_mp_flip_sign(botan_mp_t mp) {
5✔
77
   return BOTAN_FFI_VISIT(mp, [](auto& bn) { bn.flip_sign(); });
15✔
78
}
79

80
int botan_mp_from_bin(botan_mp_t mp, const uint8_t bin[], size_t bin_len) {
1✔
81
   return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn._assign_from_bytes({bin, bin_len}); });
2✔
82
}
83

84
int botan_mp_to_hex(const botan_mp_t mp, char* out) {
38✔
85
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) {
76✔
86
      const std::string hex = bn.to_hex_string();
87
      std::memcpy(out, hex.c_str(), 1 + hex.size());
88
   });
89
}
90

91
int botan_mp_to_str(const botan_mp_t mp, uint8_t digit_base, char* out, size_t* out_len) {
14✔
92
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
28✔
93
      if(digit_base == 0 || digit_base == 10) {
94
         return write_str_output(out, out_len, bn.to_dec_string());
95
      } else if(digit_base == 16) {
96
         return write_str_output(out, out_len, bn.to_hex_string());
97
      } else {
98
         return BOTAN_FFI_ERROR_BAD_PARAMETER;
99
      }
100
   });
101
}
102

103
int botan_mp_to_bin(const botan_mp_t mp, uint8_t vec[]) {
3✔
104
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { bn.serialize_to(std::span{vec, bn.bytes()}); });
6✔
105
}
106

107
int botan_mp_to_uint32(const botan_mp_t mp, uint32_t* val) {
5✔
108
   if(val == nullptr) {
5✔
109
      return BOTAN_FFI_ERROR_NULL_POINTER;
110
   }
111
   return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { *val = bn.to_u32bit(); });
10✔
112
}
113

114
int botan_mp_destroy(botan_mp_t mp) {
171✔
115
   return BOTAN_FFI_CHECKED_DELETE(mp);
171✔
116
}
117

118
int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
5✔
119
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
10✔
120
      if(result == x) {
121
         res += safe_get(y);
122
      } else {
123
         res = safe_get(x) + safe_get(y);
124
      }
125
   });
126
}
127

128
int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
1✔
129
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
2✔
130
      if(result == x) {
131
         res -= safe_get(y);
132
      } else {
133
         res = safe_get(x) - safe_get(y);
134
      }
135
   });
136
}
137

138
int botan_mp_add_u32(botan_mp_t result, const botan_mp_t x, uint32_t y) {
2✔
139
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
4✔
140
      if(result == x) {
141
         res += static_cast<Botan::word>(y);
142
      } else {
143
         res = safe_get(x) + static_cast<Botan::word>(y);
144
      }
145
   });
146
}
147

148
int botan_mp_sub_u32(botan_mp_t result, const botan_mp_t x, uint32_t y) {
1✔
149
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
2✔
150
      if(result == x) {
151
         res -= static_cast<Botan::word>(y);
152
      } else {
153
         res = safe_get(x) - static_cast<Botan::word>(y);
154
      }
155
   });
156
}
157

158
int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
6✔
159
   return BOTAN_FFI_VISIT(result, [=](auto& res) {
12✔
160
      if(result == x) {
161
         res *= safe_get(y);
162
      } else {
163
         res = safe_get(x) * safe_get(y);
164
      }
165
   });
166
}
167

168
int botan_mp_div(botan_mp_t quotient, botan_mp_t remainder, const botan_mp_t x, const botan_mp_t y) {
2✔
169
   return BOTAN_FFI_VISIT(quotient, [=](auto& q) {
4✔
170
      Botan::BigInt r;
171
      Botan::vartime_divide(safe_get(x), safe_get(y), q, r);
172
      safe_get(remainder) = r;
173
   });
174
}
175

176
int botan_mp_equal(const botan_mp_t x_w, const botan_mp_t y_w) {
23✔
177
   return BOTAN_FFI_VISIT(x_w, [=](const auto& x) -> int { return x == safe_get(y_w); });
46✔
178
}
179

180
int botan_mp_is_zero(const botan_mp_t mp) {
3✔
181
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_zero(); });
6✔
182
}
183

184
int botan_mp_is_odd(const botan_mp_t mp) {
2✔
185
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_odd(); });
5✔
186
}
187

188
int botan_mp_is_even(const botan_mp_t mp) {
2✔
189
   return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_even(); });
5✔
190
}
191

192
int botan_mp_cmp(int* result, const botan_mp_t x_w, const botan_mp_t y_w) {
44✔
193
   return BOTAN_FFI_VISIT(x_w, [=](auto& x) { *result = x.cmp(safe_get(y_w)); });
88✔
194
}
195

196
int botan_mp_swap(botan_mp_t x_w, botan_mp_t y_w) {
×
197
   return BOTAN_FFI_VISIT(x_w, [=](auto& x) { x.swap(safe_get(y_w)); });
×
198
}
199

200
// Return (base^exponent) % modulus
201
int botan_mp_powmod(botan_mp_t out, const botan_mp_t base, const botan_mp_t exponent, const botan_mp_t modulus) {
2✔
202
   return BOTAN_FFI_VISIT(
4✔
203
      out, [=](auto& o) { o = Botan::power_mod(safe_get(base), safe_get(exponent), safe_get(modulus)); });
204
}
205

206
int botan_mp_lshift(botan_mp_t out, const botan_mp_t in, size_t shift) {
2✔
207
   return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) << shift; });
4✔
208
}
209

210
int botan_mp_rshift(botan_mp_t out, const botan_mp_t in, size_t shift) {
4✔
211
   return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) >> shift; });
8✔
212
}
213

214
int botan_mp_mod_inverse(botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus) {
2✔
215
   return BOTAN_FFI_VISIT(out, [=](auto& o) {
4✔
216
      o = Botan::inverse_mod_general(safe_get(in), safe_get(modulus)).value_or(Botan::BigInt::zero());
217
   });
218
}
219

220
int botan_mp_mod_mul(botan_mp_t out, const botan_mp_t x, const botan_mp_t y, const botan_mp_t modulus) {
2✔
221
   return BOTAN_FFI_VISIT(out, [=](auto& o) {
6✔
222
      auto reducer = Botan::Barrett_Reduction::for_secret_modulus(safe_get(modulus));
223
      o = reducer.multiply(safe_get(x), safe_get(y));
224
   });
225
}
226

227
int botan_mp_rand_bits(botan_mp_t rand_out, botan_rng_t rng, size_t bits) {
2✔
228
   return BOTAN_FFI_VISIT(rng, [=](auto& r) { safe_get(rand_out).randomize(r, bits); });
4✔
229
}
230

231
int botan_mp_rand_range(botan_mp_t rand_out, botan_rng_t rng, const botan_mp_t lower, const botan_mp_t upper) {
10✔
232
   return BOTAN_FFI_VISIT(
30✔
233
      rng, [=](auto& r) { safe_get(rand_out) = Botan::BigInt::random_integer(r, safe_get(lower), safe_get(upper)); });
234
}
235

236
int botan_mp_gcd(botan_mp_t out, const botan_mp_t x, const botan_mp_t y) {
4✔
237
   return BOTAN_FFI_VISIT(out, [=](auto& o) { o = Botan::gcd(safe_get(x), safe_get(y)); });
8✔
238
}
239

240
int botan_mp_is_prime(const botan_mp_t mp, botan_rng_t rng, size_t test_prob) {
5✔
241
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) { return (Botan::is_prime(n, safe_get(rng), test_prob)) ? 1 : 0; });
10✔
242
}
243

244
int botan_mp_get_bit(const botan_mp_t mp, size_t bit) {
6✔
245
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) -> int { return n.get_bit(bit); });
12✔
246
}
247

248
int botan_mp_set_bit(botan_mp_t mp, size_t bit) {
2✔
249
   return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.set_bit(bit); });
4✔
250
}
251

252
int botan_mp_clear_bit(botan_mp_t mp, size_t bit) {
2✔
253
   return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.clear_bit(bit); });
4✔
254
}
255

256
int botan_mp_num_bits(const botan_mp_t mp, size_t* bits) {
7✔
257
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bits = n.bits(); });
14✔
258
}
259

260
int botan_mp_num_bytes(const botan_mp_t mp, size_t* bytes) {
45✔
261
   return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bytes = n.bytes(); });
90✔
262
}
263
}
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