Botan 3.6.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>

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 116 of file ffi_mp.cpp.

116 {
117 return BOTAN_FFI_VISIT(result, [=](auto& res) {
118 if(result == x)
119 res += safe_get(y);
120 else
121 res = safe_get(x) + safe_get(y);
122 });
123}
#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 134 of file ffi_mp.cpp.

134 {
135 return BOTAN_FFI_VISIT(result, [=](auto& res) {
136 if(result == x)
137 res += static_cast<Botan::word>(y);
138 else
139 res = safe_get(x) + static_cast<Botan::word>(y);
140 });
141}

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 33 of file ffi_mp.cpp.

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

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 243 of file ffi_mp.cpp.

243 {
244 return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.clear_bit(bit); });
245}

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 185 of file ffi_mp.cpp.

185 {
186 return BOTAN_FFI_VISIT(x_w, [=](auto& x) { *result = x.cmp(safe_get(y_w)); });
187}

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 112 of file ffi_mp.cpp.

112 {
113 return BOTAN_FFI_CHECKED_DELETE(mp);
114}
#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 161 of file ffi_mp.cpp.

161 {
162 return BOTAN_FFI_VISIT(quotient, [=](auto& q) {
165 safe_get(remainder) = r;
166 });
167}
void vartime_divide(const BigInt &x, const BigInt &y_arg, BigInt &q_out, BigInt &r_out)
Definition divide.cpp:155

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 169 of file ffi_mp.cpp.

169 {
170 return BOTAN_FFI_VISIT(x_w, [=](const auto& x) -> int { return x == safe_get(y_w); });
171}

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 75 of file ffi_mp.cpp.

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

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 79 of file ffi_mp.cpp.

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

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 227 of file ffi_mp.cpp.

227 {
228 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = Botan::gcd(safe_get(x), safe_get(y)); });
229}
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 235 of file ffi_mp.cpp.

235 {
236 return BOTAN_FFI_VISIT(mp, [=](const auto& n) -> int { return n.get_bit(bit); });
237}

References BOTAN_FFI_VISIT.

◆ botan_mp_init()

int botan_mp_init ( botan_mp_t * mp)

Initialize an MPI

Definition at line 21 of file ffi_mp.cpp.

21 {
22 return ffi_guard_thunk(__func__, [=]() -> int {
23 if(mp_out == nullptr) {
25 }
26
27 auto mp = std::make_unique<Botan::BigInt>();
28 *mp_out = new botan_mp_struct(std::move(mp));
29 return BOTAN_FFI_SUCCESS;
30 });
31}
@ 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:118

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 181 of file ffi_mp.cpp.

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

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 67 of file ffi_mp.cpp.

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

References BOTAN_FFI_VISIT.

◆ botan_mp_is_odd()

int botan_mp_is_odd ( const botan_mp_t mp)

Definition at line 177 of file ffi_mp.cpp.

177 {
178 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_odd(); });
179}

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 71 of file ffi_mp.cpp.

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

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 231 of file ffi_mp.cpp.

231 {
232 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { return (Botan::is_prime(n, safe_get(rng), test_prob)) ? 1 : 0; });
233}
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 173 of file ffi_mp.cpp.

173 {
174 return BOTAN_FFI_VISIT(mp, [](const auto& bn) -> int { return bn.is_zero(); });
175}

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 199 of file ffi_mp.cpp.

199 {
200 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) << shift; });
201}

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 207 of file ffi_mp.cpp.

207 {
208 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = Botan::inverse_mod(safe_get(in), safe_get(modulus)); });
209}
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition mod_inv.cpp:179

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

◆ 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 211 of file ffi_mp.cpp.

211 {
212 return BOTAN_FFI_VISIT(out, [=](auto& o) {
213 Botan::Modular_Reducer reducer(safe_get(modulus));
214 o = reducer.multiply(safe_get(x), safe_get(y));
215 });
216}

References BOTAN_FFI_VISIT, Botan::Modular_Reducer::multiply(), 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 152 of file ffi_mp.cpp.

152 {
153 return BOTAN_FFI_VISIT(result, [=](auto& res) {
154 if(result == x)
155 res *= safe_get(y);
156 else
157 res = safe_get(x) * safe_get(y);
158 });
159}

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 247 of file ffi_mp.cpp.

247 {
248 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bits = n.bits(); });
249}

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 251 of file ffi_mp.cpp.

251 {
252 return BOTAN_FFI_VISIT(mp, [=](const auto& n) { *bytes = n.bytes(); });
253}

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 194 of file ffi_mp.cpp.

194 {
195 return BOTAN_FFI_VISIT(
196 out, [=](auto& o) { o = Botan::power_mod(safe_get(base), safe_get(exponent), safe_get(modulus)); });
197}
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 218 of file ffi_mp.cpp.

218 {
219 return BOTAN_FFI_VISIT(rng, [=](auto& r) { safe_get(rand_out).randomize(r, bits); });
220}

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 222 of file ffi_mp.cpp.

222 {
223 return BOTAN_FFI_VISIT(
224 rng, [=](auto& r) { safe_get(rand_out) = Botan::BigInt::random_integer(r, safe_get(lower), safe_get(upper)); });
225}
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 203 of file ffi_mp.cpp.

203 {
204 return BOTAN_FFI_VISIT(out, [=](auto& o) { o = safe_get(in) >> shift; });
205}

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 239 of file ffi_mp.cpp.

239 {
240 return BOTAN_FFI_VISIT(mp, [=](auto& n) { n.set_bit(bit); });
241}

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 37 of file ffi_mp.cpp.

37 {
38 return BOTAN_FFI_VISIT(mp, [=](auto& bn) { bn = Botan::BigInt::from_s32(initial_value); });
39}
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 63 of file ffi_mp.cpp.

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

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 45 of file ffi_mp.cpp.

45 {
46 return BOTAN_FFI_VISIT(mp, [=](auto& bn) {
48 if(radix == 10)
50 else if(radix == 16)
52 else
54
55 const uint8_t* bytes = Botan::cast_char_ptr_to_uint8(str);
56 const size_t len = strlen(str);
57
58 bn = Botan::BigInt(bytes, len, base);
59 return BOTAN_FFI_SUCCESS;
60 });
61}
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:135
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition mem_ops.h:273

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 41 of file ffi_mp.cpp.

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

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 125 of file ffi_mp.cpp.

125 {
126 return BOTAN_FFI_VISIT(result, [=](auto& res) {
127 if(result == x)
128 res -= safe_get(y);
129 else
130 res = safe_get(x) - safe_get(y);
131 });
132}

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 143 of file ffi_mp.cpp.

143 {
144 return BOTAN_FFI_VISIT(result, [=](auto& res) {
145 if(result == x)
146 res -= static_cast<Botan::word>(y);
147 else
148 res = safe_get(x) - static_cast<Botan::word>(y);
149 });
150}

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 189 of file ffi_mp.cpp.

189 {
190 return BOTAN_FFI_VISIT(x_w, [=](auto& x) { x.swap(safe_get(y_w)); });
191}

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 101 of file ffi_mp.cpp.

101 {
102 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) { bn.serialize_to(std::span{vec, bn.bytes()}); });
103}

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 83 of file ffi_mp.cpp.

83 {
84 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) {
85 const std::string hex = bn.to_hex_string();
86 std::memcpy(out, hex.c_str(), 1 + hex.size());
87 });
88}

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 90 of file ffi_mp.cpp.

90 {
91 return BOTAN_FFI_VISIT(mp, [=](const auto& bn) -> int {
92 if(digit_base == 0 || digit_base == 10)
93 return write_str_output(out, out_len, bn.to_dec_string());
94 else if(digit_base == 16)
95 return write_str_output(out, out_len, bn.to_hex_string());
96 else
98 });
99}
@ 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 105 of file ffi_mp.cpp.

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

References BOTAN_FFI_ERROR_NULL_POINTER, and BOTAN_FFI_VISIT.