Botan 3.10.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/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/mem_utils.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 return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn = Botan::BigInt(str); });
44}
45
46int botan_mp_set_from_radix_str(botan_mp_t mp, const char* str, size_t radix) {
47 return BOTAN_FFI_VISIT(mp, [=](auto& bn) {
49 if(radix == 10) {
51 } else if(radix == 16) {
53 } else {
55 }
56
58 return BOTAN_FFI_SUCCESS;
59 });
60}
61
62// NOLINTBEGIN(misc-misplaced-const)
63
65 return BOTAN_FFI_VISIT(dest, [=](auto& bn) { bn = safe_get(source); });
66}
67
69 return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.is_negative() ? 1 : 0; });
70}
71
73 return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.is_positive() ? 1 : 0; });
74}
75
77 return BOTAN_FFI_VISIT(mp, [](auto& bn) { bn.flip_sign(); });
78}
79
80int botan_mp_from_bin(botan_mp_t mp, const uint8_t bin[], size_t bin_len) {
81 return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn._assign_from_bytes({bin, bin_len}); });
82}
83
84int botan_mp_to_hex(const botan_mp_t mp, char* out) {
85 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) {
86 const std::string hex = bn.to_hex_string();
87
88 // Check that we are about to write no more than the documented upper bound
89 const size_t upper_bound = 2 * bn.bytes() + 5;
90 BOTAN_ASSERT_NOMSG(hex.size() + 1 <= upper_bound);
91 std::memcpy(out, hex.c_str(), 1 + hex.size());
92 });
93}
94
96 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
97 const std::string hex = bn.to_hex_string();
98 return invoke_view_callback(view, ctx, hex);
99 });
100}
101
102int botan_mp_to_str(const botan_mp_t mp, uint8_t radix, char* out, size_t* out_len) {
103 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
104 if(radix == 0 || radix == 10) {
105 return write_str_output(out, out_len, bn.to_dec_string());
106 } else if(radix == 16) {
107 return write_str_output(out, out_len, bn.to_hex_string());
108 } else {
110 }
111 });
112}
113
114int botan_mp_view_str(const botan_mp_t mp, uint8_t radix, botan_view_ctx ctx, botan_view_str_fn view) {
115 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
116 if(radix == 10) {
117 return invoke_view_callback(view, ctx, bn.to_dec_string());
118 } else if(radix == 16) {
119 return invoke_view_callback(view, ctx, bn.to_hex_string());
120 } else {
122 }
123 });
124}
125
126int botan_mp_to_bin(const botan_mp_t mp, uint8_t vec[]) {
127 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { bn.serialize_to(std::span{vec, bn.bytes()}); });
128}
129
131 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) {
132 const auto bytes = bn.serialize();
133 return invoke_view_callback(view, ctx, bytes);
134 });
135}
136
137int botan_mp_to_uint32(const botan_mp_t mp, uint32_t* val) {
138 if(val == nullptr) {
140 }
141 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { *val = bn.to_u32bit(); });
142}
143
147
148int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
149 return BOTAN_FFI_VISIT(result, [=](auto& res) {
150 if(result == x) {
151 res += safe_get(y);
152 } else {
153 res = safe_get(x) + safe_get(y);
154 }
155 });
156}
157
158int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
159 return BOTAN_FFI_VISIT(result, [=](auto& res) {
160 if(result == x) {
161 res -= safe_get(y);
162 } else {
163 res = safe_get(x) - safe_get(y);
164 }
165 });
166}
167
168int botan_mp_add_u32(botan_mp_t result, const botan_mp_t x, uint32_t y) {
169 return BOTAN_FFI_VISIT(result, [=](auto& res) {
170 if(result == x) {
171 res += static_cast<Botan::word>(y);
172 } else {
173 res = safe_get(x) + static_cast<Botan::word>(y);
174 }
175 });
176}
177
178int botan_mp_sub_u32(botan_mp_t result, const botan_mp_t x, uint32_t y) {
179 return BOTAN_FFI_VISIT(result, [=](auto& res) {
180 if(result == x) {
181 res -= static_cast<Botan::word>(y);
182 } else {
183 res = safe_get(x) - static_cast<Botan::word>(y);
184 }
185 });
186}
187
188int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
189 return BOTAN_FFI_VISIT(result, [=](auto& res) {
190 if(result == x) {
191 res *= safe_get(y);
192 } else {
193 res = safe_get(x) * safe_get(y);
194 }
195 });
196}
197
198int botan_mp_div(botan_mp_t quotient, botan_mp_t remainder, const botan_mp_t x, const botan_mp_t y) {
199 return BOTAN_FFI_VISIT(quotient, [=](auto& q) {
202 safe_get(remainder) = r;
203 });
204}
205
206int botan_mp_equal(const botan_mp_t x_w, const botan_mp_t y_w) {
207 return BOTAN_FFI_VISIT(x_w, [=](const auto& x) -> int { return x == safe_get(y_w); });
208}
209
211 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_zero(); });
212}
213
215 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_odd(); });
216}
217
219 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_even(); });
220}
221
222int botan_mp_cmp(int* result, const botan_mp_t x_w, const botan_mp_t y_w) {
223 return BOTAN_FFI_VISIT(x_w, [=](auto& x) { *result = x.cmp(safe_get(y_w)); });
224}
225
227 return BOTAN_FFI_VISIT(x_w, [=](auto& x) { x.swap(safe_get(y_w)); });
228}
229
230// Return (base^exponent) % modulus
231int botan_mp_powmod(botan_mp_t out, const botan_mp_t base, const botan_mp_t exponent, const botan_mp_t modulus) {
232 return BOTAN_FFI_VISIT(
233 out, [=](auto& o) { o = Botan::power_mod(safe_get(base), safe_get(exponent), safe_get(modulus)); });
234}
235
236int botan_mp_lshift(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_rshift(botan_mp_t out, const botan_mp_t in, size_t shift) {
241 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) >> shift; });
242}
243
244int botan_mp_mod_inverse(botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus) {
245 return BOTAN_FFI_VISIT(out, [=](auto& o) {
247 });
248}
249
250int botan_mp_mod_mul(botan_mp_t out, const botan_mp_t x, const botan_mp_t y, const botan_mp_t modulus) {
251 return BOTAN_FFI_VISIT(out, [=](auto& o) {
253 o = reducer.multiply(safe_get(x), safe_get(y));
254 });
255}
256
257int botan_mp_rand_bits(botan_mp_t rand_out, botan_rng_t rng, size_t bits) {
258 return BOTAN_FFI_VISIT(rng, [=](auto& r) { safe_get(rand_out).randomize(r, bits); });
259}
260
261int botan_mp_rand_range(botan_mp_t rand_out, botan_rng_t rng, const botan_mp_t lower, const botan_mp_t upper) {
262 return BOTAN_FFI_VISIT(
263 rng, [=](auto& r) { safe_get(rand_out) = Botan::BigInt::random_integer(r, safe_get(lower), safe_get(upper)); });
264}
265
266int botan_mp_gcd(botan_mp_t out, const botan_mp_t x, const botan_mp_t y) {
267 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = Botan::gcd(safe_get(x), safe_get(y)); });
268}
269
270int botan_mp_is_prime(const botan_mp_t mp, botan_rng_t rng, size_t test_prob) {
271 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { return (Botan::is_prime(n, safe_get(rng), test_prob)) ? 1 : 0; });
272}
273
274int botan_mp_get_bit(const botan_mp_t mp, size_t bit) {
275 return BOTAN_FFI_VISIT(mp, [=](const auto& n) -> int { return n.get_bit(bit); });
276}
277
278int botan_mp_set_bit(botan_mp_t mp, size_t bit) {
279 return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.set_bit(bit); });
280}
281
282int botan_mp_clear_bit(botan_mp_t mp, size_t bit) {
283 return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.clear_bit(bit); });
284}
285
286int botan_mp_num_bits(const botan_mp_t mp, size_t* bits) {
287 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bits = n.bits(); });
288}
289
290int botan_mp_num_bytes(const botan_mp_t mp, size_t* bytes) {
291 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bytes = n.bytes(); });
292}
293
294// NOLINTEND(misc-misplaced-const)
295}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
static Barrett_Reduction for_secret_modulus(const BigInt &m)
Definition barrett.cpp:22
static BigInt zero()
Definition bigint.h:49
static BigInt decode(const uint8_t buf[], size_t length)
Definition bigint.h:857
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition big_rand.cpp:43
static BigInt from_s32(int32_t n)
Definition bigint.cpp:41
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:921
void * botan_view_ctx
Definition ffi.h:152
struct botan_rng_struct * botan_rng_t
Definition ffi.h:289
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:132
@ BOTAN_FFI_SUCCESS
Definition ffi.h:115
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:133
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:64
int botan_mp_to_bin(const botan_mp_t mp, uint8_t vec[])
Definition ffi_mp.cpp:126
int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
Definition ffi_mp.cpp:158
int botan_mp_gcd(botan_mp_t out, const botan_mp_t x, const botan_mp_t y)
Definition ffi_mp.cpp:266
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:261
int botan_mp_add_u32(botan_mp_t result, const botan_mp_t x, uint32_t y)
Definition ffi_mp.cpp:168
int botan_mp_num_bits(const botan_mp_t mp, size_t *bits)
Definition ffi_mp.cpp:286
int botan_mp_set_bit(botan_mp_t mp, size_t bit)
Definition ffi_mp.cpp:278
int botan_mp_num_bytes(const botan_mp_t mp, size_t *bytes)
Definition ffi_mp.cpp:290
int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
Definition ffi_mp.cpp:188
int botan_mp_is_zero(const botan_mp_t mp)
Definition ffi_mp.cpp:210
int botan_mp_to_str(const botan_mp_t mp, uint8_t radix, char *out, size_t *out_len)
Definition ffi_mp.cpp:102
int botan_mp_sub_u32(botan_mp_t result, const botan_mp_t x, uint32_t y)
Definition ffi_mp.cpp:178
int botan_mp_is_even(const botan_mp_t mp)
Definition ffi_mp.cpp:218
int botan_mp_view_bin(const botan_mp_t mp, botan_view_ctx ctx, botan_view_bin_fn view)
Definition ffi_mp.cpp:130
int botan_mp_destroy(botan_mp_t mp)
Definition ffi_mp.cpp:144
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:250
int botan_mp_is_prime(const botan_mp_t mp, botan_rng_t rng, size_t test_prob)
Definition ffi_mp.cpp:270
int botan_mp_is_positive(const botan_mp_t mp)
Definition ffi_mp.cpp:72
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:257
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:114
int botan_mp_clear_bit(botan_mp_t mp, size_t bit)
Definition ffi_mp.cpp:282
int botan_mp_flip_sign(botan_mp_t mp)
Definition ffi_mp.cpp:76
int botan_mp_mod_inverse(botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus)
Definition ffi_mp.cpp:244
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:214
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:198
int botan_mp_rshift(botan_mp_t out, const botan_mp_t in, size_t shift)
Definition ffi_mp.cpp:240
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:231
int botan_mp_to_uint32(const botan_mp_t mp, uint32_t *val)
Definition ffi_mp.cpp:137
int botan_mp_cmp(int *result, const botan_mp_t x_w, const botan_mp_t y_w)
Definition ffi_mp.cpp:222
int botan_mp_set_from_radix_str(botan_mp_t mp, const char *str, size_t radix)
Definition ffi_mp.cpp:46
int botan_mp_is_negative(const botan_mp_t mp)
Definition ffi_mp.cpp:68
int botan_mp_equal(const botan_mp_t x_w, const botan_mp_t y_w)
Definition ffi_mp.cpp:206
int botan_mp_to_hex(const botan_mp_t mp, char *out)
Definition ffi_mp.cpp:84
int botan_mp_swap(botan_mp_t x_w, botan_mp_t y_w)
Definition ffi_mp.cpp:226
int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
Definition ffi_mp.cpp:148
int botan_mp_view_hex(const botan_mp_t mp, botan_view_ctx ctx, botan_view_str_fn view)
Definition ffi_mp.cpp:95
int botan_mp_from_bin(botan_mp_t mp, const uint8_t bin[], size_t bin_len)
Definition ffi_mp.cpp:80
int botan_mp_lshift(botan_mp_t out, const botan_mp_t in, size_t shift)
Definition ffi_mp.cpp:236
int botan_mp_get_bit(const botan_mp_t mp, size_t bit)
Definition ffi_mp.cpp:274
#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:309
void vartime_divide(const BigInt &x, const BigInt &y_arg, BigInt &q_out, BigInt &r_out)
Definition divide.cpp:324
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
Definition numthry.cpp:380
std::span< const uint8_t > cstr_as_span_of_bytes(const char *s)
Definition mem_utils.h:41
std::optional< BigInt > inverse_mod_general(const BigInt &x, const BigInt &mod)
Definition mod_inv.cpp:177
BigInt gcd(const BigInt &a, const BigInt &b)
Definition numthry.cpp:219
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
Definition types.h:119