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