Botan  2.7.0
Crypto and TLS for C++11
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 #include <botan/internal/ffi_util.h>
10 #include <botan/internal/ffi_rng.h>
11 #include <botan/internal/ffi_mp.h>
12 #include <botan/reducer.h>
13 #include <botan/numthry.h>
14 #include <botan/divide.h>
15 
16 extern "C" {
17 
18 using namespace Botan_FFI;
19 
21  {
22  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
23  if(mp_out == nullptr)
25 
26  *mp_out = new botan_mp_struct(new Botan::BigInt);
27  return BOTAN_FFI_SUCCESS;
28  });
29  }
30 
32  {
33  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { bn.clear(); });
34  }
35 
36 int botan_mp_set_from_int(botan_mp_t mp, int initial_value)
37  {
38  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, {
39  if(initial_value >= 0)
40  {
41  bn = Botan::BigInt(static_cast<uint64_t>(initial_value));
42  }
43  else
44  {
45  bn = Botan::BigInt(static_cast<uint64_t>(-initial_value));
46  bn.flip_sign();
47  }
48  });
49  }
50 
51 int botan_mp_set_from_str(botan_mp_t mp, const char* str)
52  {
53  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { bn = Botan::BigInt(str); });
54  }
55 
56 int botan_mp_set_from_radix_str(botan_mp_t mp, const char* str, size_t radix)
57  {
58  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, {
60  if(radix == 10)
62  else if(radix == 16)
64  else
66 
67  const uint8_t* bytes = Botan::cast_char_ptr_to_uint8(str);
68  const size_t len = strlen(str);
69 
70  bn = Botan::BigInt::decode(bytes, len, base);
71  });
72  }
73 
75  {
76  return BOTAN_FFI_DO(Botan::BigInt, dest, bn, { bn = safe_get(source); });
77  }
78 
80  {
81  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { return bn.is_negative() ? 1 : 0; });
82  }
83 
85  {
86  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { return bn.is_positive() ? 1 : 0; });
87  }
88 
90  {
91  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { bn.flip_sign(); });
92  }
93 
94 int botan_mp_from_bin(botan_mp_t mp, const uint8_t bin[], size_t bin_len)
95  {
96  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { bn.binary_decode(bin, bin_len); });
97  }
98 
99 int botan_mp_to_hex(const botan_mp_t mp, char* out)
100  {
101  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, {
102  std::vector<uint8_t> hex = Botan::BigInt::encode(bn, Botan::BigInt::Hexadecimal);
103  std::memcpy(out, hex.data(), hex.size());
104  out[hex.size()] = 0; // null terminate
105  });
106  }
107 
108 int botan_mp_to_str(const botan_mp_t mp, uint8_t digit_base, char* out, size_t* out_len)
109  {
110  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, {
111  Botan::BigInt::Base base;
112  if(digit_base == 0 || digit_base == 10)
113  base = Botan::BigInt::Decimal;
114  else if(digit_base == 16)
116  else
117  throw FFI_Error("botan_mp_to_str invalid digit base");
118 
119  std::vector<uint8_t> hex = Botan::BigInt::encode(bn, base);
120  hex.push_back(0); // null terminator
121  return write_str_output(out, out_len, hex);
122  });
123  }
124 
125 int botan_mp_to_bin(const botan_mp_t mp, uint8_t vec[])
126  {
127  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { bn.binary_encode(vec); });
128  }
129 
130 int botan_mp_to_uint32(const botan_mp_t mp, uint32_t* val)
131  {
132  if(val == nullptr)
133  {
135  }
136  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { *val = bn.to_u32bit(); });
137  }
138 
140  {
141  return BOTAN_FFI_CHECKED_DELETE(mp);
142  }
143 
144 int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
145  {
146  return BOTAN_FFI_DO(Botan::BigInt, result, res, { res = safe_get(x) + safe_get(y); });
147  }
148 
149 int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
150  {
151  return BOTAN_FFI_DO(Botan::BigInt, result, res, { res = safe_get(x) - safe_get(y); });
152  }
153 
154 int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
155  {
156  return BOTAN_FFI_DO(Botan::BigInt, result, res, { res = safe_get(x) * safe_get(y); });
157  }
158 
160  botan_mp_t remainder,
161  const botan_mp_t x, const botan_mp_t y)
162  {
163  return BOTAN_FFI_DO(Botan::BigInt, quotient, q, {
164  Botan::BigInt r;
165  Botan::divide(safe_get(x), safe_get(y), q, r);
166  safe_get(remainder) = r;
167  });
168  }
169 
170 int botan_mp_equal(const botan_mp_t x_w, const botan_mp_t y_w)
171  {
172  return BOTAN_FFI_DO(Botan::BigInt, x_w, x, { return x == safe_get(y_w); });
173  }
174 
176  {
177  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { return bn.is_zero(); });
178  }
179 
181  {
182  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { return bn.is_odd(); });
183  }
184 
186  {
187  return BOTAN_FFI_DO(Botan::BigInt, mp, bn, { return bn.is_even(); });
188  }
189 
190 int botan_mp_cmp(int* result, const botan_mp_t x_w, const botan_mp_t y_w)
191  {
192  return BOTAN_FFI_DO(Botan::BigInt, x_w, x, { *result = x.cmp(safe_get(y_w)); });
193  }
194 
196  {
197  return BOTAN_FFI_DO(Botan::BigInt, x_w, x, { x.swap(safe_get(y_w)); });
198  }
199 
200 // Return (base^exponent) % modulus
201 int botan_mp_powmod(botan_mp_t out, const botan_mp_t base, const botan_mp_t exponent, const botan_mp_t modulus)
202  {
203  return BOTAN_FFI_DO(Botan::BigInt, out, o,
204  { o = Botan::power_mod(safe_get(base), safe_get(exponent), safe_get(modulus)); });
205  }
206 
207 int botan_mp_lshift(botan_mp_t out, const botan_mp_t in, size_t shift)
208  {
209  return BOTAN_FFI_DO(Botan::BigInt, out, o, { o = safe_get(in) << shift; });
210  }
211 
212 int botan_mp_rshift(botan_mp_t out, const botan_mp_t in, size_t shift)
213  {
214  return BOTAN_FFI_DO(Botan::BigInt, out, o, { o = safe_get(in) >> shift; });
215  }
216 
217 int botan_mp_mod_inverse(botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus)
218  {
219  return BOTAN_FFI_DO(Botan::BigInt, out, o, { o = Botan::inverse_mod(safe_get(in), safe_get(modulus)); });
220  }
221 
222 int botan_mp_mod_mul(botan_mp_t out, const botan_mp_t x, const botan_mp_t y, const botan_mp_t modulus)
223  {
224  return BOTAN_FFI_DO(Botan::BigInt, out, o, {
225  Botan::Modular_Reducer reducer(safe_get(modulus));
226  o = reducer.multiply(safe_get(x), safe_get(y));
227  });
228  }
229 
230 int botan_mp_rand_bits(botan_mp_t rand_out, botan_rng_t rng, size_t bits)
231  {
233  safe_get(rand_out).randomize(r, bits); });
234  }
235 
237  botan_rng_t rng,
238  const botan_mp_t lower,
239  const botan_mp_t upper)
240  {
242  safe_get(rand_out) = Botan::BigInt::random_integer(r, safe_get(lower), safe_get(upper)); });
243  }
244 
245 int botan_mp_gcd(botan_mp_t out, const botan_mp_t x, const botan_mp_t y)
246  {
247  return BOTAN_FFI_DO(Botan::BigInt, out, o, {
248  o = Botan::gcd(safe_get(x), safe_get(y)); });
249  }
250 
251 int botan_mp_is_prime(const botan_mp_t mp, botan_rng_t rng, size_t test_prob)
252  {
253  return BOTAN_FFI_DO(Botan::BigInt, mp, n,
254  { return (Botan::is_prime(n, safe_get(rng), test_prob)) ? 1 : 0; });
255  }
256 
257 int botan_mp_get_bit(const botan_mp_t mp, size_t bit)
258  {
259  return BOTAN_FFI_DO(Botan::BigInt, mp, n, { return (n.get_bit(bit)); });
260  }
261 
262 int botan_mp_set_bit(botan_mp_t mp, size_t bit)
263  {
264  return BOTAN_FFI_DO(Botan::BigInt, mp, n, { n.set_bit(bit); });
265  }
266 
267 int botan_mp_clear_bit(botan_mp_t mp, size_t bit)
268  {
269  return BOTAN_FFI_DO(Botan::BigInt, mp, n, { n.clear_bit(bit); });
270  }
271 
272 int botan_mp_num_bits(const botan_mp_t mp, size_t* bits)
273  {
274  return BOTAN_FFI_DO(Botan::BigInt, mp, n, { *bits = n.bits(); });
275  }
276 
277 int botan_mp_num_bytes(const botan_mp_t mp, size_t* bytes)
278  {
279  return BOTAN_FFI_DO(Botan::BigInt, mp, n, { *bytes = n.bytes(); });
280  }
281 
282 }
int botan_mp_num_bytes(const botan_mp_t mp, size_t *bytes)
Definition: ffi_mp.cpp:277
int botan_mp_to_uint32(const botan_mp_t mp, uint32_t *val)
Definition: ffi_mp.cpp:130
int botan_mp_to_bin(const botan_mp_t mp, uint8_t vec[])
Definition: ffi_mp.cpp:125
bool is_negative() const
Definition: bigint.h:460
int botan_mp_gcd(botan_mp_t out, const botan_mp_t x, const botan_mp_t y)
Definition: ffi_mp.cpp:245
int botan_mp_set_from_radix_str(botan_mp_t mp, const char *str, size_t radix)
Definition: ffi_mp.cpp:56
BigInt gcd(const BigInt &a, const BigInt &b)
Definition: numthry.cpp:50
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:222
BigInt power_mod(const BigInt &base, const BigInt &exp, const BigInt &mod)
Definition: numthry.cpp:398
int botan_mp_mul(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
Definition: ffi_mp.cpp:154
size_t bits() const
Definition: bigint.cpp:228
int botan_mp_clear(botan_mp_t mp)
Definition: ffi_mp.cpp:31
int botan_mp_set_bit(botan_mp_t mp, size_t bit)
Definition: ffi_mp.cpp:262
int botan_mp_is_zero(const botan_mp_t mp)
Definition: ffi_mp.cpp:175
int32_t cmp(const BigInt &n, bool check_signs=true) const
Definition: bigint.cpp:131
int botan_mp_is_even(const botan_mp_t mp)
Definition: ffi_mp.cpp:185
void set_bit(size_t n)
Definition: bigint.cpp:201
int botan_mp_equal(const botan_mp_t x_w, const botan_mp_t y_w)
Definition: ffi_mp.cpp:170
int ffi_guard_thunk(const char *func_name, Thunk thunk)
Definition: ffi_util.h:64
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:131
bool is_zero() const
Definition: bigint.h:355
bool is_prime(const BigInt &n, RandomNumberGenerator &rng, size_t prob, bool is_random)
Definition: numthry.cpp:507
bool is_even() const
Definition: bigint.h:337
void binary_encode(uint8_t buf[]) const
Definition: bigint.cpp:316
void swap(BigInt &other)
Definition: bigint.h:150
int botan_mp_add(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
Definition: ffi_mp.cpp:144
static BigInt random_integer(RandomNumberGenerator &rng, const BigInt &min, const BigInt &max)
Definition: big_rand.cpp:45
int botan_mp_init(botan_mp_t *mp_out)
Definition: ffi_mp.cpp:20
int botan_mp_set_from_str(botan_mp_t mp, const char *str)
Definition: ffi_mp.cpp:51
void divide(const BigInt &x, const BigInt &y_arg, BigInt &q, BigInt &r)
Definition: divide.cpp:58
int botan_mp_lshift(botan_mp_t out, const botan_mp_t in, size_t shift)
Definition: ffi_mp.cpp:207
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:201
int botan_mp_is_odd(const botan_mp_t mp)
Definition: ffi_mp.cpp:180
int botan_mp_rshift(botan_mp_t out, const botan_mp_t in, size_t shift)
Definition: ffi_mp.cpp:212
int write_str_output(uint8_t out[], size_t *out_len, const std::string &str)
Definition: ffi_util.h:151
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:236
int botan_mp_num_bits(const botan_mp_t mp, size_t *bits)
Definition: ffi_mp.cpp:272
int botan_mp_to_hex(const botan_mp_t mp, char *out)
Definition: ffi_mp.cpp:99
struct botan_mp_struct * botan_mp_t
Definition: ffi.h:535
int botan_mp_to_str(const botan_mp_t mp, uint8_t digit_base, char *out, size_t *out_len)
Definition: ffi_mp.cpp:108
int botan_mp_set_from_int(botan_mp_t mp, int initial_value)
Definition: ffi_mp.cpp:36
int botan_mp_swap(botan_mp_t x_w, botan_mp_t y_w)
Definition: ffi_mp.cpp:195
bool is_odd() const
Definition: bigint.h:343
BigInt inverse_mod(const BigInt &n, const BigInt &mod)
Definition: numthry.cpp:288
int botan_mp_sub(botan_mp_t result, const botan_mp_t x, const botan_mp_t y)
Definition: ffi_mp.cpp:149
size_t bytes() const
Definition: bigint.cpp:220
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:126
int botan_mp_rand_bits(botan_mp_t rand_out, botan_rng_t rng, size_t bits)
Definition: ffi_mp.cpp:230
void clear()
Definition: bigint.h:314
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:49
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:159
#define BOTAN_CURRENT_FUNCTION
Definition: compiler.h:143
int botan_mp_flip_sign(botan_mp_t mp)
Definition: ffi_mp.cpp:89
uint32_t to_u32bit() const
Definition: bigint.cpp:185
void binary_decode(const uint8_t buf[], size_t length)
Definition: bigint.cpp:326
int botan_mp_destroy(botan_mp_t mp)
Definition: ffi_mp.cpp:139
int botan_mp_from_bin(botan_mp_t mp, const uint8_t bin[], size_t bin_len)
Definition: ffi_mp.cpp:94
int botan_mp_set_from_mp(botan_mp_t dest, const botan_mp_t source)
Definition: ffi_mp.cpp:74
void flip_sign()
Definition: bigint.h:487
static std::vector< uint8_t > encode(const BigInt &n, Base base=Binary)
Definition: big_code.cpp:54
void clear_bit(size_t n)
Definition: bigint.cpp:212
int botan_mp_mod_inverse(botan_mp_t out, const botan_mp_t in, const botan_mp_t modulus)
Definition: ffi_mp.cpp:217
int botan_mp_cmp(int *result, const botan_mp_t x_w, const botan_mp_t y_w)
Definition: ffi_mp.cpp:190
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98
int botan_mp_clear_bit(botan_mp_t mp, size_t bit)
Definition: ffi_mp.cpp:267
int botan_mp_get_bit(const botan_mp_t mp, size_t bit)
Definition: ffi_mp.cpp:257
int botan_mp_is_prime(const botan_mp_t mp, botan_rng_t rng, size_t test_prob)
Definition: ffi_mp.cpp:251
BigInt multiply(const BigInt &x, const BigInt &y) const
Definition: reducer.h:31
struct botan_rng_struct * botan_rng_t
Definition: ffi.h:182
bool is_positive() const
Definition: bigint.h:466
static BigInt decode(const uint8_t buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:114
int botan_mp_is_positive(const botan_mp_t mp)
Definition: ffi_mp.cpp:84
int botan_mp_is_negative(const botan_mp_t mp)
Definition: ffi_mp.cpp:79