Botan 3.14.1
Crypto and TLS for C&
ffi_mp.cpp
Go to the documentation of this file.
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/assert.h>
11#include <botan/numthry.h>
12#include <botan/internal/barrett.h>
13#include <botan/internal/divide.h>
14#include <botan/internal/ffi_mp.h>
15#include <botan/internal/ffi_rng.h>
16#include <botan/internal/ffi_util.h>
17#include <botan/internal/mod_inv.h>
18
19extern "C" {
20
21using namespace Botan_FFI;
22
24 return ffi_guard_thunk(__func__, [=]() -> int {
25 if(mp_out == nullptr) {
27 }
28
29 auto mp = std::make_unique<Botan::BigInt>();
30 return ffi_new_object(mp_out, std::move(mp));
31 });
32}
33
35 return BOTAN_FFI_VISIT(mp, [](auto& bn) { bn.clear(); });
36}
37
38int botan_mp_set_from_int(botan_mp_t mp, int initial_value) {
39 return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn = Botan::BigInt::from_s32(initial_value); });
40}
41
42int botan_mp_set_from_str(botan_mp_t mp, const char* str) {
43 if(str == nullptr) {
45 }
46
47 return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn = Botan::BigInt(str); });
48}
49
50int botan_mp_set_from_radix_str(botan_mp_t mp, const char* str, size_t radix) {
51 if(str == nullptr) {
53 }
54
55 return BOTAN_FFI_VISIT(mp, [=](auto& bn) {
56 if(radix != 10 && radix != 16) {
58 }
59
60 bn = Botan::BigInt::from_radix_digits(std::string_view(str), radix);
61 return BOTAN_FFI_SUCCESS;
62 });
63}
64
65// NOLINTBEGIN(misc-misplaced-const)
66
68 return BOTAN_FFI_VISIT(dest, [=](auto& bn) { bn = safe_get(source); });
69}
70
72 return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.signum() < 0 ? 1 : 0; });
73}
74
76 return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.signum() >= 0 ? 1 : 0; });
77}
78
80 return BOTAN_FFI_VISIT(mp, [](auto& bn) { bn.flip_sign(); });
81}
82
83int botan_mp_from_bin(botan_mp_t mp, const uint8_t bin[], size_t bin_len) {
84 if(bin_len > 0 && bin == nullptr) {
86 }
87 return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn._assign_from_bytes({bin, bin_len}); });
88}
89
90int botan_mp_to_hex(const botan_mp_t mp, char* out) {
91 if(out == nullptr) {
93 }
94 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) {
95 const std::string hex = bn.to_hex_string();
96
97 // Check that we are about to write no more than the documented upper bound
98 const size_t upper_bound = 2 * bn.bytes() + 5;
99 BOTAN_ASSERT_NOMSG(hex.size() + 1 <= upper_bound);
100 std::memcpy(out, hex.c_str(), 1 + hex.size());
101 });
102}
103
105 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
106 const std::string hex = bn.to_hex_string();
107 return invoke_view_callback(view, ctx, hex);
108 });
109}
110
111int botan_mp_to_str(const botan_mp_t mp, uint8_t radix, char* out, size_t* out_len) {
112 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
113 if(radix == 0 || radix == 10) {
114 return write_str_output(out, out_len, bn.to_dec_string());
115 } else if(radix == 16) {
116 return write_str_output(out, out_len, bn.to_hex_string());
117 } else {
119 }
120 });
121}
122
123int botan_mp_view_str(const botan_mp_t mp, uint8_t radix, botan_view_ctx ctx, botan_view_str_fn view) {
124 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
125 if(radix == 10) {
126 return invoke_view_callback(view, ctx, bn.to_dec_string());
127 } else if(radix == 16) {
128 return invoke_view_callback(view, ctx, bn.to_hex_string());
129 } else {
131 }
132 });
133}
134
135int botan_mp_to_bin(const botan_mp_t mp, uint8_t vec[]) {
136 if(vec == nullptr) {
138 }
139 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { bn.serialize_to(std::span{vec, bn.bytes()}); });
140}
141
143 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) {
144 const auto bytes = bn.serialize();
145 return invoke_view_callback(view, ctx, bytes);
146 });
147}
148
149int botan_mp_to_uint32(const botan_mp_t mp, uint32_t* val) {
150 if(val == nullptr) {
152 }
153 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { *val = bn.to_u32bit(); });
154}
155
159
160int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
161 return BOTAN_FFI_VISIT(result, [=](auto& res) {
162 if(result == x) {
163 res += safe_get(y);
164 } else {
165 res = safe_get(x) + safe_get(y);
166 }
167 });
168}
169
170int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
171 return BOTAN_FFI_VISIT(result, [=](auto& res) {
172 if(result == x) {
173 res -= safe_get(y);
174 } else {
175 res = safe_get(x) - safe_get(y);
176 }
177 });
178}
179
180int botan_mp_add_u32(botan_mp_t result, const botan_mp_t x, uint32_t y) {
181 return BOTAN_FFI_VISIT(result, [=](auto& res) {
182 if(result == x) {
183 res += static_cast<Botan::word>(y);
184 } else {
185 res = safe_get(x) + static_cast<Botan::word>(y);
186 }
187 });
188}
189
190int botan_mp_sub_u32(botan_mp_t result, const botan_mp_t x, uint32_t y) {
191 return BOTAN_FFI_VISIT(result, [=](auto& res) {
192 if(result == x) {
193 res -= static_cast<Botan::word>(y);
194 } else {
195 res = safe_get(x) - static_cast<Botan::word>(y);
196 }
197 });
198}
199
200int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
201 return BOTAN_FFI_VISIT(result, [=](auto& res) {
202 if(result == x) {
203 res *= safe_get(y);
204 } else {
205 res = safe_get(x) * safe_get(y);
206 }
207 });
208}
209
210int botan_mp_div(botan_mp_t quotient, botan_mp_t remainder, const botan_mp_t x, const botan_mp_t y) {
211 return BOTAN_FFI_VISIT(quotient, [=](auto& q) {
213 Botan::ct_divide(safe_get(x), safe_get(y), q, r);
214 safe_get(remainder) = r;
215 });
216}
217
218int botan_mp_equal(const botan_mp_t x_w, const botan_mp_t y_w) {
219 return BOTAN_FFI_VISIT(x_w, [=](const auto& x) -> int { return x == safe_get(y_w); });
220}
221
223 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_zero(); });
224}
225
227 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_odd(); });
228}
229
231 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_even(); });
232}
233
234int botan_mp_cmp(int* result, const botan_mp_t x_w, const botan_mp_t y_w) {
235 if(result == nullptr) {
237 }
238 return BOTAN_FFI_VISIT(x_w, [=](auto& x) { *result = x.cmp(safe_get(y_w)); });
239}
240
242 return BOTAN_FFI_VISIT(x_w, [=](auto& x) { x.swap(safe_get(y_w)); });
243}
244
245// Return (base^exponent) % modulus
246int botan_mp_powmod(botan_mp_t out, const botan_mp_t base, const botan_mp_t exponent, const botan_mp_t modulus) {
247 return BOTAN_FFI_VISIT(
248 out, [=](auto& o) { o = Botan::power_mod(safe_get(base), safe_get(exponent), safe_get(modulus)); });
249}
250
251int botan_mp_lshift(botan_mp_t out, const botan_mp_t in, size_t shift) {
252 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) << shift; });
253}
254
255int botan_mp_rshift(botan_mp_t out, const botan_mp_t in, size_t shift) {
256 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) >> shift; });
257}
258
259int botan_mp_mod_inverse(botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus) {
260 return BOTAN_FFI_VISIT(out, [=](auto& o) {
262 });
263}
264
265int botan_mp_mod_mul(botan_mp_t out, const botan_mp_t x, const botan_mp_t y, const botan_mp_t modulus) {
266 return BOTAN_FFI_VISIT(out, [=](auto& o) {
267 const auto reducer = Botan::Barrett_Reduction::for_secret_modulus(safe_get(modulus));
268 o = reducer.multiply(safe_get(x), safe_get(y));
269 });
270}
271
272int botan_mp_rand_bits(botan_mp_t rand_out, botan_rng_t rng, size_t bits) {
273 return BOTAN_FFI_VISIT(rng, [=](auto& r) { safe_get(rand_out).randomize(r, bits); });
274}
275
276int botan_mp_rand_range(botan_mp_t rand_out, botan_rng_t rng, const botan_mp_t lower, const botan_mp_t upper) {
277 return BOTAN_FFI_VISIT(
278 rng, [=](auto& r) { safe_get(rand_out) = Botan::BigInt::random_integer(r, safe_get(lower), safe_get(upper)); });
279}
280
281int botan_mp_gcd(botan_mp_t out, const botan_mp_t x, const botan_mp_t y) {
282 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = Botan::gcd(safe_get(x), safe_get(y)); });
283}
284
285int botan_mp_is_prime(const botan_mp_t mp, botan_rng_t rng, size_t test_prob) {
286 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { return (Botan::is_prime(n, safe_get(rng), test_prob)) ? 1 : 0; });
287}
288
289int botan_mp_get_bit(const botan_mp_t mp, size_t bit) {
290 return BOTAN_FFI_VISIT(mp, [=](const auto& n) -> int { return n.get_bit(bit); });
291}
292
293int botan_mp_set_bit(botan_mp_t mp, size_t bit) {
294 return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.set_bit(bit); });
295}
296
297int botan_mp_clear_bit(botan_mp_t mp, size_t bit) {
298 return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.clear_bit(bit); });
299}
300
301int botan_mp_num_bits(const botan_mp_t mp, size_t* bits) {
302 if(bits == nullptr) {
304 }
305 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bits = n.bits(); });
306}
307
308int botan_mp_num_bytes(const botan_mp_t mp, size_t* bytes) {
309 if(bytes == nullptr) {
311 }
312 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bytes = n.bytes(); });
313}
314
315// NOLINTEND(misc-misplaced-const)
316}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
static Barrett_Reduction for_secret_modulus(const BigInt &m)
Definition barrett.cpp:23
static BigInt zero()
Definition bigint.h:50
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition big_rand.cpp:45
static BigInt from_s32(int32_t n)
Definition bigint.cpp:43
static BigInt from_radix_digits(std::string_view digits, size_t radix)
Definition big_code.cpp:126
int(* botan_view_bin_fn)(botan_view_ctx view_ctx, const uint8_t *data, size_t len)
Definition ffi.h:161
struct botan_mp_struct * botan_mp_t
Definition ffi.h:1109
void * botan_view_ctx
Definition ffi.h:152
struct botan_rng_struct * botan_rng_t
Definition ffi.h:343
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:131
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:132
int(* botan_view_str_fn)(botan_view_ctx view_ctx, const char *str, size_t len)
Definition ffi.h:170
int botan_mp_set_from_int(botan_mp_t mp, int initial_value)
Definition ffi_mp.cpp:38
int botan_mp_set_from_mp(botan_mp_t dest, const botan_mp_t source)
Definition ffi_mp.cpp:67
int botan_mp_to_bin(const botan_mp_t mp, uint8_t vec[])
Definition ffi_mp.cpp:135
int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
Definition ffi_mp.cpp:170
int botan_mp_gcd(botan_mp_t out, const botan_mp_t x, const botan_mp_t y)
Definition ffi_mp.cpp:281
int botan_mp_rand_range(botan_mp_t rand_out, botan_rng_t rng, const botan_mp_t lower, const botan_mp_t upper)
Definition ffi_mp.cpp:276
int botan_mp_add_u32(botan_mp_t result, const botan_mp_t x, uint32_t y)
Definition ffi_mp.cpp:180
int botan_mp_num_bits(const botan_mp_t mp, size_t *bits)
Definition ffi_mp.cpp:301
int botan_mp_set_bit(botan_mp_t mp, size_t bit)
Definition ffi_mp.cpp:293
int botan_mp_num_bytes(const botan_mp_t mp, size_t *bytes)
Definition ffi_mp.cpp:308
int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
Definition ffi_mp.cpp:200
int botan_mp_is_zero(const botan_mp_t mp)
Definition ffi_mp.cpp:222
int botan_mp_to_str(const botan_mp_t mp, uint8_t radix, char *out, size_t *out_len)
Definition ffi_mp.cpp:111
int botan_mp_sub_u32(botan_mp_t result, const botan_mp_t x, uint32_t y)
Definition ffi_mp.cpp:190
int botan_mp_is_even(const botan_mp_t mp)
Definition ffi_mp.cpp:230
int botan_mp_view_bin(const botan_mp_t mp, botan_view_ctx ctx, botan_view_bin_fn view)
Definition ffi_mp.cpp:142
int botan_mp_destroy(botan_mp_t mp)
Definition ffi_mp.cpp:156
int botan_mp_mod_mul(botan_mp_t out, const botan_mp_t x, const botan_mp_t y, const botan_mp_t modulus)
Definition ffi_mp.cpp:265
int botan_mp_is_prime(const botan_mp_t mp, botan_rng_t rng, size_t test_prob)
Definition ffi_mp.cpp:285
int botan_mp_is_positive(const botan_mp_t mp)
Definition ffi_mp.cpp:75
int botan_mp_set_from_str(botan_mp_t mp, const char *str)
Definition ffi_mp.cpp:42
int botan_mp_init(botan_mp_t *mp_out)
Definition ffi_mp.cpp:23
int botan_mp_rand_bits(botan_mp_t rand_out, botan_rng_t rng, size_t bits)
Definition ffi_mp.cpp:272
int botan_mp_view_str(const botan_mp_t mp, uint8_t radix, botan_view_ctx ctx, botan_view_str_fn view)
Definition ffi_mp.cpp:123
int botan_mp_clear_bit(botan_mp_t mp, size_t bit)
Definition ffi_mp.cpp:297
int botan_mp_flip_sign(botan_mp_t mp)
Definition ffi_mp.cpp:79
int botan_mp_mod_inverse(botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus)
Definition ffi_mp.cpp:259
int botan_mp_clear(botan_mp_t mp)
Definition ffi_mp.cpp:34
int botan_mp_is_odd(const botan_mp_t mp)
Definition ffi_mp.cpp:226
int botan_mp_div(botan_mp_t quotient, botan_mp_t remainder, const botan_mp_t x, const botan_mp_t y)
Definition ffi_mp.cpp:210
int botan_mp_rshift(botan_mp_t out, const botan_mp_t in, size_t shift)
Definition ffi_mp.cpp:255
int botan_mp_powmod(botan_mp_t out, const botan_mp_t base, const botan_mp_t exponent, const botan_mp_t modulus)
Definition ffi_mp.cpp:246
int botan_mp_to_uint32(const botan_mp_t mp, uint32_t *val)
Definition ffi_mp.cpp:149
int botan_mp_cmp(int *result, const botan_mp_t x_w, const botan_mp_t y_w)
Definition ffi_mp.cpp:234
int botan_mp_set_from_radix_str(botan_mp_t mp, const char *str, size_t radix)
Definition ffi_mp.cpp:50
int botan_mp_is_negative(const botan_mp_t mp)
Definition ffi_mp.cpp:71
int botan_mp_equal(const botan_mp_t x_w, const botan_mp_t y_w)
Definition ffi_mp.cpp:218
int botan_mp_to_hex(const botan_mp_t mp, char *out)
Definition ffi_mp.cpp:90
int botan_mp_swap(botan_mp_t x_w, botan_mp_t y_w)
Definition ffi_mp.cpp:241
int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
Definition ffi_mp.cpp:160
int botan_mp_view_hex(const botan_mp_t mp, botan_view_ctx ctx, botan_view_str_fn view)
Definition ffi_mp.cpp:104
int botan_mp_from_bin(botan_mp_t mp, const uint8_t bin[], size_t bin_len)
Definition ffi_mp.cpp:83
int botan_mp_lshift(botan_mp_t out, const botan_mp_t in, size_t shift)
Definition ffi_mp.cpp:251
int botan_mp_get_bit(const botan_mp_t mp, size_t bit)
Definition ffi_mp.cpp:289
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:327
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:357
int invoke_view_callback(botan_view_bin_fn view, botan_view_ctx ctx, std::span< const uint8_t > buf)
Definition ffi_util.h:359
auto safe_get(T *p) -> typename T::element_type &
Definition ffi_util.h:191
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:347
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:229
int write_str_output(char out[], size_t *out_len, const std::string &str)
Definition ffi_util.h:440
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
Definition numthry.cpp:310
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
Definition numthry.cpp:449
void ct_divide(const BigInt &x, const BigInt &y, BigInt &q_out, BigInt &r_out)
Definition divide.cpp:180
std::optional< BigInt > inverse_mod_general(const BigInt &x, const BigInt &mod)
Definition mod_inv.cpp:175
BigInt gcd(const BigInt &a, const BigInt &b)
Definition numthry.cpp:220
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
The native machine word, used as the limb type for multiprecision integers.
Definition types.h:131