Botan 3.7.1
Crypto and TLS for C&
ffi_mp.cpp File Reference
#include <botan/ffi.h>
#include <botan/numthry.h>
#include <botan/reducer.h>
#include <botan/internal/divide.h>
#include <botan/internal/ffi_mp.h>
#include <botan/internal/ffi_rng.h>
#include <botan/internal/ffi_util.h>
#include <botan/internal/mod_inv.h>

Go to the source code of this file.

Functions

int botan_mp_add (botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
 
int botan_mp_add_u32 (botan_mp_t result, const botan_mp_t x, uint32_t y)
 
int botan_mp_clear (botan_mp_t mp)
 
int botan_mp_clear_bit (botan_mp_t mp, size_t bit)
 
int botan_mp_cmp (int *result, const botan_mp_t x_w, const botan_mp_t y_w)
 
int botan_mp_destroy (botan_mp_t mp)
 
int botan_mp_div (botan_mp_t quotient, botan_mp_t remainder, const botan_mp_t x, const botan_mp_t y)
 
int botan_mp_equal (const botan_mp_t x_w, const botan_mp_t y_w)
 
int botan_mp_flip_sign (botan_mp_t mp)
 
int botan_mp_from_bin (botan_mp_t mp, const uint8_t bin[], size_t bin_len)
 
int botan_mp_gcd (botan_mp_t out, const botan_mp_t x, const botan_mp_t y)
 
int botan_mp_get_bit (const botan_mp_t mp, size_t bit)
 
int botan_mp_init (botan_mp_t *mp_out)
 
int botan_mp_is_even (const botan_mp_t mp)
 
int botan_mp_is_negative (const botan_mp_t mp)
 
int botan_mp_is_odd (const botan_mp_t mp)
 
int botan_mp_is_positive (const botan_mp_t mp)
 
int botan_mp_is_prime (const botan_mp_t mp, botan_rng_t rng, size_t test_prob)
 
int botan_mp_is_zero (const botan_mp_t mp)
 
int botan_mp_lshift (botan_mp_t out, const botan_mp_t in, size_t shift)
 
int botan_mp_mod_inverse (botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus)
 
int botan_mp_mod_mul (botan_mp_t out, const botan_mp_t x, const botan_mp_t y, const botan_mp_t modulus)
 
int botan_mp_mul (botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
 
int botan_mp_num_bits (const botan_mp_t mp, size_t *bits)
 
int botan_mp_num_bytes (const botan_mp_t mp, size_t *bytes)
 
int botan_mp_powmod (botan_mp_t out, const botan_mp_t base, const botan_mp_t exponent, const botan_mp_t modulus)
 
int botan_mp_rand_bits (botan_mp_t rand_out, botan_rng_t rng, size_t bits)
 
int botan_mp_rand_range (botan_mp_t rand_out, botan_rng_t rng, const botan_mp_t lower, const botan_mp_t upper)
 
int botan_mp_rshift (botan_mp_t out, const botan_mp_t in, size_t shift)
 
int botan_mp_set_bit (botan_mp_t mp, size_t bit)
 
int botan_mp_set_from_int (botan_mp_t mp, int initial_value)
 
int botan_mp_set_from_mp (botan_mp_t dest, const botan_mp_t source)
 
int botan_mp_set_from_radix_str (botan_mp_t mp, const char *str, size_t radix)
 
int botan_mp_set_from_str (botan_mp_t mp, const char *str)
 
int botan_mp_sub (botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
 
int botan_mp_sub_u32 (botan_mp_t result, const botan_mp_t x, uint32_t y)
 
int botan_mp_swap (botan_mp_t x_w, botan_mp_t y_w)
 
int botan_mp_to_bin (const botan_mp_t mp, uint8_t vec[])
 
int botan_mp_to_hex (const botan_mp_t mp, char *out)
 
int botan_mp_to_str (const botan_mp_t mp, uint8_t digit_base, char *out, size_t *out_len)
 
int botan_mp_to_uint32 (const botan_mp_t mp, uint32_t *val)
 

Function Documentation

◆ botan_mp_add()

int botan_mp_add ( botan_mp_t result,
const botan_mp_t x,
const botan_mp_t y )

Definition at line 119 of file ffi_mp.cpp.

119 {
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}
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:124
T & safe_get(botan_struct< T, M > *p)
Definition ffi_util.h:63

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_add_u32()

int botan_mp_add_u32 ( botan_mp_t result,
const botan_mp_t x,
uint32_t y )

Definition at line 139 of file ffi_mp.cpp.

139 {
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}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_clear()

int botan_mp_clear ( botan_mp_t mp)

Set the MPI to zero

Definition at line 34 of file ffi_mp.cpp.

34 {
35 return BOTAN_FFI_VISIT(mp, [](auto& bn) { bn.clear(); });
36}

References BOTAN_FFI_VISIT.

◆ botan_mp_clear_bit()

int botan_mp_clear_bit ( botan_mp_t n,
size_t bit )

Clear the specified bit

Definition at line 253 of file ffi_mp.cpp.

253 {
254 return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.clear_bit(bit); });
255}

References BOTAN_FFI_VISIT.

◆ botan_mp_cmp()

int botan_mp_cmp ( int * result,
const botan_mp_t x_w,
const botan_mp_t y_w )

Definition at line 193 of file ffi_mp.cpp.

193 {
194 return BOTAN_FFI_VISIT(x_w, [=](auto& x) { *result = x.cmp(safe_get(y_w)); });
195}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_destroy()

int botan_mp_destroy ( botan_mp_t mp)

Destroy (deallocate) an MPI

Returns
0 if success, error if invalid object handle

Definition at line 115 of file ffi_mp.cpp.

115 {
116 return BOTAN_FFI_CHECKED_DELETE(mp);
117}
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:143

References BOTAN_FFI_CHECKED_DELETE.

◆ botan_mp_div()

int botan_mp_div ( botan_mp_t quotient,
botan_mp_t remainder,
const botan_mp_t x,
const botan_mp_t y )

Definition at line 169 of file ffi_mp.cpp.

169 {
170 return BOTAN_FFI_VISIT(quotient, [=](auto& q) {
173 safe_get(remainder) = r;
174 });
175}
void vartime_divide(const BigInt &x, const BigInt &y_arg, BigInt &q_out, BigInt &r_out)
Definition divide.cpp:219

References BOTAN_FFI_VISIT, Botan_FFI::safe_get(), and Botan::vartime_divide().

◆ botan_mp_equal()

int botan_mp_equal ( const botan_mp_t x_w,
const botan_mp_t y_w )

Definition at line 177 of file ffi_mp.cpp.

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

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_flip_sign()

int botan_mp_flip_sign ( botan_mp_t mp)

Definition at line 77 of file ffi_mp.cpp.

77 {
78 return BOTAN_FFI_VISIT(mp, [](auto& bn) { bn.flip_sign(); });
79}

References BOTAN_FFI_VISIT.

◆ botan_mp_from_bin()

int botan_mp_from_bin ( botan_mp_t mp,
const uint8_t bin[],
size_t bin_len )

Definition at line 81 of file ffi_mp.cpp.

81 {
82 return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn._assign_from_bytes({bin, bin_len}); });
83}

References BOTAN_FFI_VISIT.

◆ botan_mp_gcd()

int botan_mp_gcd ( botan_mp_t out,
const botan_mp_t x,
const botan_mp_t y )

Definition at line 237 of file ffi_mp.cpp.

237 {
238 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = Botan::gcd(safe_get(x), safe_get(y)); });
239}
BigInt gcd(const BigInt &a, const BigInt &b)
Definition numthry.cpp:193

References BOTAN_FFI_VISIT, Botan::gcd(), and Botan_FFI::safe_get().

◆ botan_mp_get_bit()

int botan_mp_get_bit ( botan_mp_t n,
size_t bit )

Returns 0 if specified bit of n is not set Returns 1 if specified bit of n is set Returns negative number on error

Definition at line 245 of file ffi_mp.cpp.

245 {
246 return BOTAN_FFI_VISIT(mp, [=](const auto& n) -> int { return n.get_bit(bit); });
247}

References BOTAN_FFI_VISIT.

◆ botan_mp_init()

int botan_mp_init ( botan_mp_t * mp)

Initialize an MPI

Definition at line 22 of file ffi_mp.cpp.

22 {
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}
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:129
@ BOTAN_FFI_SUCCESS
Definition ffi.h:114
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
Definition ffi.cpp:128

References BOTAN_FFI_ERROR_NULL_POINTER, BOTAN_FFI_SUCCESS, and Botan_FFI::ffi_guard_thunk().

◆ botan_mp_is_even()

int botan_mp_is_even ( const botan_mp_t mp)

Definition at line 189 of file ffi_mp.cpp.

189 {
190 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_even(); });
191}

References BOTAN_FFI_VISIT.

◆ botan_mp_is_negative()

int botan_mp_is_negative ( botan_mp_t mp)

Return 1 iff mp is less than 0

Definition at line 69 of file ffi_mp.cpp.

69 {
70 return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.is_negative() ? 1 : 0; });
71}

References BOTAN_FFI_VISIT.

◆ botan_mp_is_odd()

int botan_mp_is_odd ( const botan_mp_t mp)

Definition at line 185 of file ffi_mp.cpp.

185 {
186 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_odd(); });
187}

References BOTAN_FFI_VISIT.

◆ botan_mp_is_positive()

int botan_mp_is_positive ( botan_mp_t mp)

This function should have been named mp_is_non_negative. Returns 1 iff mp is greater than or equal to zero. Use botan_mp_is_negative to detect negative numbers, botan_mp_is_zero to check for zero.

Definition at line 73 of file ffi_mp.cpp.

73 {
74 return BOTAN_FFI_VISIT(mp, [](const auto& bn) { return bn.is_positive() ? 1 : 0; });
75}

References BOTAN_FFI_VISIT.

◆ botan_mp_is_prime()

int botan_mp_is_prime ( botan_mp_t n,
botan_rng_t rng,
size_t test_prob )

Returns 0 if n is not prime Returns 1 if n is prime Returns negative number on error

Definition at line 241 of file ffi_mp.cpp.

241 {
242 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { return (Botan::is_prime(n, safe_get(rng), test_prob)) ? 1 : 0; });
243}
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
Definition numthry.cpp:355

References BOTAN_FFI_VISIT, Botan::is_prime(), and Botan_FFI::safe_get().

◆ botan_mp_is_zero()

int botan_mp_is_zero ( const botan_mp_t mp)

Definition at line 181 of file ffi_mp.cpp.

181 {
182 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_zero(); });
183}

References BOTAN_FFI_VISIT.

◆ botan_mp_lshift()

int botan_mp_lshift ( botan_mp_t out,
const botan_mp_t in,
size_t shift )

Definition at line 207 of file ffi_mp.cpp.

207 {
208 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) << shift; });
209}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_mod_inverse()

int botan_mp_mod_inverse ( botan_mp_t out,
const botan_mp_t in,
const botan_mp_t modulus )

Definition at line 215 of file ffi_mp.cpp.

215 {
216 return BOTAN_FFI_VISIT(out, [=](auto& o) {
218 });
219}
static BigInt zero()
Definition bigint.h:50
std::optional< BigInt > inverse_mod_general(const BigInt &x, const BigInt &mod)
Definition mod_inv.cpp:177

References BOTAN_FFI_VISIT, Botan::inverse_mod_general(), Botan_FFI::safe_get(), and Botan::BigInt::zero().

◆ botan_mp_mod_mul()

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 at line 221 of file ffi_mp.cpp.

221 {
222 return BOTAN_FFI_VISIT(out, [=](auto& o) {
224 o = reducer.multiply(safe_get(x), safe_get(y));
225 });
226}
static Modular_Reducer for_secret_modulus(const BigInt &m)
Definition reducer.cpp:32

References BOTAN_FFI_VISIT, Botan::Modular_Reducer::for_secret_modulus(), and Botan_FFI::safe_get().

◆ botan_mp_mul()

int botan_mp_mul ( botan_mp_t result,
const botan_mp_t x,
const botan_mp_t y )

Definition at line 159 of file ffi_mp.cpp.

159 {
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}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_num_bits()

int botan_mp_num_bits ( botan_mp_t n,
size_t * bits )

Return the number of significant bits in the MPI

Definition at line 257 of file ffi_mp.cpp.

257 {
258 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bits = n.bits(); });
259}

References BOTAN_FFI_VISIT.

◆ botan_mp_num_bytes()

int botan_mp_num_bytes ( botan_mp_t n,
size_t * bytes )

Return the number of significant bytes in the MPI

Definition at line 261 of file ffi_mp.cpp.

261 {
262 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bytes = n.bytes(); });
263}

References BOTAN_FFI_VISIT.

◆ botan_mp_powmod()

int botan_mp_powmod ( botan_mp_t out,
const botan_mp_t base,
const botan_mp_t exponent,
const botan_mp_t modulus )

Definition at line 202 of file ffi_mp.cpp.

202 {
203 return BOTAN_FFI_VISIT(
204 out, [=](auto& o) { o = Botan::power_mod(safe_get(base), safe_get(exponent), safe_get(modulus)); });
205}
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
Definition numthry.cpp:284

References BOTAN_FFI_VISIT, Botan::power_mod(), and Botan_FFI::safe_get().

◆ botan_mp_rand_bits()

int botan_mp_rand_bits ( botan_mp_t rand_out,
botan_rng_t rng,
size_t bits )

Definition at line 228 of file ffi_mp.cpp.

228 {
229 return BOTAN_FFI_VISIT(rng, [=](auto& r) { safe_get(rand_out).randomize(r, bits); });
230}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_rand_range()

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 at line 232 of file ffi_mp.cpp.

232 {
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}
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition big_rand.cpp:43

References BOTAN_FFI_VISIT, Botan::BigInt::random_integer(), and Botan_FFI::safe_get().

◆ botan_mp_rshift()

int botan_mp_rshift ( botan_mp_t out,
const botan_mp_t in,
size_t shift )

Definition at line 211 of file ffi_mp.cpp.

211 {
212 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) >> shift; });
213}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_set_bit()

int botan_mp_set_bit ( botan_mp_t n,
size_t bit )

Set the specified bit

Definition at line 249 of file ffi_mp.cpp.

249 {
250 return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.set_bit(bit); });
251}

References BOTAN_FFI_VISIT.

◆ botan_mp_set_from_int()

int botan_mp_set_from_int ( botan_mp_t mp,
int initial_value )

Set the MPI value from an int

Definition at line 38 of file ffi_mp.cpp.

38 {
39 return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn = Botan::BigInt::from_s32(initial_value); });
40}
static BigInt from_s32(int32_t n)
Definition bigint.cpp:49

References BOTAN_FFI_VISIT, and Botan::BigInt::from_s32().

◆ botan_mp_set_from_mp()

int botan_mp_set_from_mp ( botan_mp_t dest,
botan_mp_t source )

Set the MPI value from another MP object

Definition at line 65 of file ffi_mp.cpp.

65 {
66 return BOTAN_FFI_VISIT(dest, [=](auto& bn) { bn = safe_get(source); });
67}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_set_from_radix_str()

int botan_mp_set_from_radix_str ( botan_mp_t dest,
const char * str,
size_t radix )

Set the MPI value from a string with arbitrary radix. For arbitrary being 10 or 16.

Definition at line 46 of file ffi_mp.cpp.

46 {
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}
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:135
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition mem_ops.h:274

References BOTAN_FFI_ERROR_NOT_IMPLEMENTED, BOTAN_FFI_SUCCESS, BOTAN_FFI_VISIT, Botan::cast_char_ptr_to_uint8(), Botan::BigInt::Decimal, and Botan::BigInt::Hexadecimal.

◆ botan_mp_set_from_str()

int botan_mp_set_from_str ( botan_mp_t dest,
const char * str )

Set the MPI value from a string

Definition at line 42 of file ffi_mp.cpp.

42 {
43 return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn = Botan::BigInt(str); });
44}

References BOTAN_FFI_VISIT.

◆ botan_mp_sub()

int botan_mp_sub ( botan_mp_t result,
const botan_mp_t x,
const botan_mp_t y )

Definition at line 129 of file ffi_mp.cpp.

129 {
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}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_sub_u32()

int botan_mp_sub_u32 ( botan_mp_t result,
const botan_mp_t x,
uint32_t y )

Definition at line 149 of file ffi_mp.cpp.

149 {
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}

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_swap()

int botan_mp_swap ( botan_mp_t x_w,
botan_mp_t y_w )

Definition at line 197 of file ffi_mp.cpp.

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

References BOTAN_FFI_VISIT, and Botan_FFI::safe_get().

◆ botan_mp_to_bin()

int botan_mp_to_bin ( const botan_mp_t mp,
uint8_t vec[] )

Definition at line 104 of file ffi_mp.cpp.

104 {
105 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { bn.serialize_to(std::span{vec, bn.bytes()}); });
106}

References BOTAN_FFI_VISIT.

◆ botan_mp_to_hex()

int botan_mp_to_hex ( botan_mp_t mp,
char * out )

Convert the MPI to a hex string. Writes botan_mp_num_bytes(mp)*2 + 1 bytes

Definition at line 85 of file ffi_mp.cpp.

85 {
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}

References BOTAN_FFI_VISIT.

◆ botan_mp_to_str()

int botan_mp_to_str ( botan_mp_t mp,
uint8_t base,
char * out,
size_t * out_len )

Convert the MPI to a string. Currently base == 10 and base == 16 are supported.

Definition at line 92 of file ffi_mp.cpp.

92 {
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}
@ BOTAN_FFI_ERROR_BAD_PARAMETER
Definition ffi.h:130
int write_str_output(uint8_t out[], size_t *out_len, std::string_view str)
Definition ffi_util.h:205

References BOTAN_FFI_ERROR_BAD_PARAMETER, BOTAN_FFI_VISIT, and Botan_FFI::write_str_output().

◆ botan_mp_to_uint32()

int botan_mp_to_uint32 ( const botan_mp_t mp,
uint32_t * val )

Definition at line 108 of file ffi_mp.cpp.

108 {
109 if(val == nullptr) {
111 }
112 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { *val = bn.to_u32bit(); });
113}

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.