Botan 3.7.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/numthry.h>
11#include <botan/reducer.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
18extern "C" {
19
20using namespace Botan_FFI;
21
23 return ffi_guard_thunk(__func__, [=]() -> int {
24 if(mp_out == nullptr) {
26 }
27
28 auto mp = std::make_unique<Botan::BigInt>();
29 *mp_out = new botan_mp_struct(std::move(mp));
30 return BOTAN_FFI_SUCCESS;
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
57 const uint8_t* bytes = Botan::cast_char_ptr_to_uint8(str);
58 const size_t len = strlen(str);
59
60 bn = Botan::BigInt(bytes, len, base);
61 return BOTAN_FFI_SUCCESS;
62 });
63}
64
66 return BOTAN_FFI_VISIT(dest, [=](auto& bn) { bn = safe_get(source); });
67}
68
70 return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.is_negative() ? 1 : 0; });
71}
72
74 return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.is_positive() ? 1 : 0; });
75}
76
78 return BOTAN_FFI_VISIT(mp, [](auto& bn) { bn.flip_sign(); });
79}
80
81int botan_mp_from_bin(botan_mp_t mp, const uint8_t bin[], size_t bin_len) {
82 return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn._assign_from_bytes({bin, bin_len}); });
83}
84
85int botan_mp_to_hex(const botan_mp_t mp, char* out) {
86 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) {
87 const std::string hex = bn.to_hex_string();
88 std::memcpy(out, hex.c_str(), 1 + hex.size());
89 });
90}
91
92int botan_mp_to_str(const botan_mp_t mp, uint8_t digit_base, char* out, size_t* out_len) {
93 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
94 if(digit_base == 0 || digit_base == 10) {
95 return write_str_output(out, out_len, bn.to_dec_string());
96 } else if(digit_base == 16) {
97 return write_str_output(out, out_len, bn.to_hex_string());
98 } else {
100 }
101 });
102}
103
104int botan_mp_to_bin(const botan_mp_t mp, uint8_t vec[]) {
105 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { bn.serialize_to(std::span{vec, bn.bytes()}); });
106}
107
108int botan_mp_to_uint32(const botan_mp_t mp, uint32_t* val) {
109 if(val == nullptr) {
111 }
112 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { *val = bn.to_u32bit(); });
113}
114
118
119int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
120 return BOTAN_FFI_VISIT(result, [=](auto& res) {
121 if(result == x) {
122 res += safe_get(y);
123 } else {
124 res = safe_get(x) + safe_get(y);
125 }
126 });
127}
128
129int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
130 return BOTAN_FFI_VISIT(result, [=](auto& res) {
131 if(result == x) {
132 res -= safe_get(y);
133 } else {
134 res = safe_get(x) - safe_get(y);
135 }
136 });
137}
138
139int botan_mp_add_u32(botan_mp_t result, const botan_mp_t x, uint32_t y) {
140 return BOTAN_FFI_VISIT(result, [=](auto& res) {
141 if(result == x) {
142 res += static_cast<Botan::word>(y);
143 } else {
144 res = safe_get(x) + static_cast<Botan::word>(y);
145 }
146 });
147}
148
149int botan_mp_sub_u32(botan_mp_t result, const botan_mp_t x, uint32_t y) {
150 return BOTAN_FFI_VISIT(result, [=](auto& res) {
151 if(result == x) {
152 res -= static_cast<Botan::word>(y);
153 } else {
154 res = safe_get(x) - static_cast<Botan::word>(y);
155 }
156 });
157}
158
159int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y) {
160 return BOTAN_FFI_VISIT(result, [=](auto& res) {
161 if(result == x) {
162 res *= safe_get(y);
163 } else {
164 res = safe_get(x) * safe_get(y);
165 }
166 });
167}
168
169int botan_mp_div(botan_mp_t quotient, botan_mp_t remainder, const botan_mp_t x, const botan_mp_t y) {
170 return BOTAN_FFI_VISIT(quotient, [=](auto& q) {
173 safe_get(remainder) = r;
174 });
175}
176
177int botan_mp_equal(const botan_mp_t x_w, const botan_mp_t y_w) {
178 return BOTAN_FFI_VISIT(x_w, [=](const auto& x) -> int { return x == safe_get(y_w); });
179}
180
182 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_zero(); });
183}
184
186 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_odd(); });
187}
188
190 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_even(); });
191}
192
193int botan_mp_cmp(int* result, const botan_mp_t x_w, const botan_mp_t y_w) {
194 return BOTAN_FFI_VISIT(x_w, [=](auto& x) { *result = x.cmp(safe_get(y_w)); });
195}
196
198 return BOTAN_FFI_VISIT(x_w, [=](auto& x) { x.swap(safe_get(y_w)); });
199}
200
201// Return (base^exponent) % modulus
202int botan_mp_powmod(botan_mp_t out, const botan_mp_t base, const botan_mp_t exponent, const botan_mp_t modulus) {
203 return BOTAN_FFI_VISIT(
204 out, [=](auto& o) { o = Botan::power_mod(safe_get(base), safe_get(exponent), safe_get(modulus)); });
205}
206
207int botan_mp_lshift(botan_mp_t out, const botan_mp_t in, size_t shift) {
208 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) << shift; });
209}
210
211int botan_mp_rshift(botan_mp_t out, const botan_mp_t in, size_t shift) {
212 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) >> shift; });
213}
214
215int botan_mp_mod_inverse(botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus) {
216 return BOTAN_FFI_VISIT(out, [=](auto& o) {
218 });
219}
220
221int botan_mp_mod_mul(botan_mp_t out, const botan_mp_t x, const botan_mp_t y, const botan_mp_t modulus) {
222 return BOTAN_FFI_VISIT(out, [=](auto& o) {
224 o = reducer.multiply(safe_get(x), safe_get(y));
225 });
226}
227
228int botan_mp_rand_bits(botan_mp_t rand_out, botan_rng_t rng, size_t bits) {
229 return BOTAN_FFI_VISIT(rng, [=](auto& r) { safe_get(rand_out).randomize(r, bits); });
230}
231
232int botan_mp_rand_range(botan_mp_t rand_out, botan_rng_t rng, const botan_mp_t lower, const botan_mp_t upper) {
233 return BOTAN_FFI_VISIT(
234 rng, [=](auto& r) { safe_get(rand_out) = Botan::BigInt::random_integer(r, safe_get(lower), safe_get(upper)); });
235}
236
237int botan_mp_gcd(botan_mp_t out, const botan_mp_t x, const botan_mp_t y) {
238 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = Botan::gcd(safe_get(x), safe_get(y)); });
239}
240
241int botan_mp_is_prime(const botan_mp_t mp, botan_rng_t rng, size_t test_prob) {
242 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { return (Botan::is_prime(n, safe_get(rng), test_prob)) ? 1 : 0; });
243}
244
245int botan_mp_get_bit(const botan_mp_t mp, size_t bit) {
246 return BOTAN_FFI_VISIT(mp, [=](const auto& n) -> int { return n.get_bit(bit); });
247}
248
249int botan_mp_set_bit(botan_mp_t mp, size_t bit) {
250 return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.set_bit(bit); });
251}
252
253int botan_mp_clear_bit(botan_mp_t mp, size_t bit) {
254 return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.clear_bit(bit); });
255}
256
257int botan_mp_num_bits(const botan_mp_t mp, size_t* bits) {
258 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bits = n.bits(); });
259}
260
261int botan_mp_num_bytes(const botan_mp_t mp, size_t* bytes) {
262 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bytes = n.bytes(); });
263}
264}
static BigInt zero()
Definition bigint.h:50
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:49
static Modular_Reducer for_secret_modulus(const BigInt &m)
Definition reducer.cpp:32
struct botan_mp_struct * botan_mp_t
Definition ffi.h:902
struct botan_rng_struct * botan_rng_t
Definition ffi.h:272
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:135
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:129
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:130
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:65
int botan_mp_to_bin(const botan_mp_t mp, uint8_t vec[])
Definition ffi_mp.cpp:104
int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
Definition ffi_mp.cpp:129
int botan_mp_gcd(botan_mp_t out, const botan_mp_t x, const botan_mp_t y)
Definition ffi_mp.cpp:237
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:232
int botan_mp_add_u32(botan_mp_t result, const botan_mp_t x, uint32_t y)
Definition ffi_mp.cpp:139
int botan_mp_num_bits(const botan_mp_t mp, size_t *bits)
Definition ffi_mp.cpp:257
int botan_mp_set_bit(botan_mp_t mp, size_t bit)
Definition ffi_mp.cpp:249
int botan_mp_num_bytes(const botan_mp_t mp, size_t *bytes)
Definition ffi_mp.cpp:261
int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
Definition ffi_mp.cpp:159
int botan_mp_is_zero(const botan_mp_t mp)
Definition ffi_mp.cpp:181
int botan_mp_sub_u32(botan_mp_t result, const botan_mp_t x, uint32_t y)
Definition ffi_mp.cpp:149
int botan_mp_is_even(const botan_mp_t mp)
Definition ffi_mp.cpp:189
int botan_mp_destroy(botan_mp_t mp)
Definition ffi_mp.cpp:115
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:221
int botan_mp_is_prime(const botan_mp_t mp, botan_rng_t rng, size_t test_prob)
Definition ffi_mp.cpp:241
int botan_mp_is_positive(const botan_mp_t mp)
Definition ffi_mp.cpp:73
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:22
int botan_mp_rand_bits(botan_mp_t rand_out, botan_rng_t rng, size_t bits)
Definition ffi_mp.cpp:228
int botan_mp_clear_bit(botan_mp_t mp, size_t bit)
Definition ffi_mp.cpp:253
int botan_mp_flip_sign(botan_mp_t mp)
Definition ffi_mp.cpp:77
int botan_mp_mod_inverse(botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus)
Definition ffi_mp.cpp:215
int botan_mp_clear(botan_mp_t mp)
Definition ffi_mp.cpp:34
int botan_mp_to_str(const botan_mp_t mp, uint8_t digit_base, char *out, size_t *out_len)
Definition ffi_mp.cpp:92
int botan_mp_is_odd(const botan_mp_t mp)
Definition ffi_mp.cpp:185
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:169
int botan_mp_rshift(botan_mp_t out, const botan_mp_t in, size_t shift)
Definition ffi_mp.cpp:211
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:202
int botan_mp_to_uint32(const botan_mp_t mp, uint32_t *val)
Definition ffi_mp.cpp:108
int botan_mp_cmp(int *result, const botan_mp_t x_w, const botan_mp_t y_w)
Definition ffi_mp.cpp:193
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:69
int botan_mp_equal(const botan_mp_t x_w, const botan_mp_t y_w)
Definition ffi_mp.cpp:177
int botan_mp_to_hex(const botan_mp_t mp, char *out)
Definition ffi_mp.cpp:85
int botan_mp_swap(botan_mp_t x_w, botan_mp_t y_w)
Definition ffi_mp.cpp:197
int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
Definition ffi_mp.cpp:119
int botan_mp_from_bin(botan_mp_t mp, const uint8_t bin[], size_t bin_len)
Definition ffi_mp.cpp:81
int botan_mp_lshift(botan_mp_t out, const botan_mp_t in, size_t shift)
Definition ffi_mp.cpp:207
int botan_mp_get_bit(const botan_mp_t mp, size_t bit)
Definition ffi_mp.cpp:245
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:124
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:143
int write_str_output(uint8_t out[], size_t *out_len, std::string_view str)
Definition ffi_util.h:205
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:63
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition ffi.cpp:128
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
Definition numthry.cpp:284
void vartime_divide(const BigInt &x, const BigInt &y_arg, BigInt &q_out, BigInt &r_out)
Definition divide.cpp:219
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
Definition numthry.cpp:355
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:193
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition mem_ops.h:274