RetroArch
|
#include "mbedtls/config.h"
#include "mbedtls/bignum.h"
#include "mbedtls/bn_mul.h"
#include <string.h>
#include "mbedtls/platform.h"
Macros | |
#define | ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */ |
#define | biL (ciL << 3) /* bits in limb */ |
#define | biH (ciL << 2) /* half limb size */ |
#define | MPI_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */ |
#define | BITS_TO_LIMBS(i) ( (i) / biL + ( (i) % biL != 0 ) ) |
#define | CHARS_TO_LIMBS(i) ( (i) / ciL + ( (i) % ciL != 0 ) ) |
#define | GCD_PAIR_COUNT 3 |
Functions | |
static void | mbedtls_mpi_zeroize (mbedtls_mpi_uint *v, size_t n) |
void | mbedtls_mpi_init (mbedtls_mpi *X) |
Initialize one MPI (make internal references valid) This just makes it ready to be set or freed, but does not define a value for the MPI. More... | |
void | mbedtls_mpi_free (mbedtls_mpi *X) |
Unallocate one MPI. More... | |
int | mbedtls_mpi_grow (mbedtls_mpi *X, size_t nblimbs) |
Enlarge to the specified number of limbs. More... | |
int | mbedtls_mpi_shrink (mbedtls_mpi *X, size_t nblimbs) |
Resize down, keeping at least the specified number of limbs. More... | |
int | mbedtls_mpi_copy (mbedtls_mpi *X, const mbedtls_mpi *Y) |
Copy the contents of Y into X. More... | |
void | mbedtls_mpi_swap (mbedtls_mpi *X, mbedtls_mpi *Y) |
Swap the contents of X and Y. More... | |
int | mbedtls_mpi_safe_cond_assign (mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign) |
Safe conditional assignement X = Y if assign is 1. More... | |
int | mbedtls_mpi_safe_cond_swap (mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap) |
Safe conditional swap X <-> Y if swap is 1. More... | |
int | mbedtls_mpi_lset (mbedtls_mpi *X, mbedtls_mpi_sint z) |
Set value from integer. More... | |
int | mbedtls_mpi_get_bit (const mbedtls_mpi *X, size_t pos) |
Get a specific bit from X. More... | |
int | mbedtls_mpi_set_bit (mbedtls_mpi *X, size_t pos, unsigned char val) |
Set a bit of X to a specific value of 0 or 1. More... | |
size_t | mbedtls_mpi_lsb (const mbedtls_mpi *X) |
Return the number of zero-bits before the least significant '1' bit. More... | |
static size_t | mbedtls_clz (const mbedtls_mpi_uint x) |
size_t | mbedtls_mpi_bitlen (const mbedtls_mpi *X) |
Return the number of bits up to and including the most significant '1' bit'. More... | |
size_t | mbedtls_mpi_size (const mbedtls_mpi *X) |
Return the total size in bytes. More... | |
static int | mpi_get_digit (mbedtls_mpi_uint *d, int radix, char c) |
int | mbedtls_mpi_read_string (mbedtls_mpi *X, int radix, const char *s) |
Import from an ASCII string. More... | |
static int | mpi_write_hlp (mbedtls_mpi *X, int radix, char **p) |
int | mbedtls_mpi_write_string (const mbedtls_mpi *X, int radix, char *buf, size_t buflen, size_t *olen) |
Export into an ASCII string. More... | |
int | mbedtls_mpi_read_file (mbedtls_mpi *X, int radix, FILE *fin) |
Read MPI from a line in an opened file. More... | |
int | mbedtls_mpi_write_file (const char *p, const mbedtls_mpi *X, int radix, FILE *fout) |
Write X into an opened file, or stdout if fout is NULL. More... | |
int | mbedtls_mpi_read_binary (mbedtls_mpi *X, const unsigned char *buf, size_t buflen) |
Import X from unsigned binary data, big endian. More... | |
int | mbedtls_mpi_write_binary (const mbedtls_mpi *X, unsigned char *buf, size_t buflen) |
Export X into unsigned binary data, big endian. Always fills the whole buffer, which will start with zeros if the number is smaller. More... | |
int | mbedtls_mpi_shift_l (mbedtls_mpi *X, size_t count) |
Left-shift: X <<= count. More... | |
int | mbedtls_mpi_shift_r (mbedtls_mpi *X, size_t count) |
Right-shift: X >>= count. More... | |
int | mbedtls_mpi_cmp_abs (const mbedtls_mpi *X, const mbedtls_mpi *Y) |
Compare unsigned values. More... | |
int | mbedtls_mpi_cmp_mpi (const mbedtls_mpi *X, const mbedtls_mpi *Y) |
Compare signed values. More... | |
int | mbedtls_mpi_cmp_int (const mbedtls_mpi *X, mbedtls_mpi_sint z) |
Compare signed values. More... | |
int | mbedtls_mpi_add_abs (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) |
Unsigned addition: X = |A| + |B|. More... | |
static void | mpi_sub_hlp (size_t n, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d) |
int | mbedtls_mpi_sub_abs (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) |
Unsigned subtraction: X = |A| - |B|. More... | |
int | mbedtls_mpi_add_mpi (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) |
Signed addition: X = A + B. More... | |
int | mbedtls_mpi_sub_mpi (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) |
Signed subtraction: X = A - B. More... | |
int | mbedtls_mpi_add_int (mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b) |
Signed addition: X = A + b. More... | |
int | mbedtls_mpi_sub_int (mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b) |
Signed subtraction: X = A - b. More... | |
static void | mpi_mul_hlp (size_t i, mbedtls_mpi_uint *s, mbedtls_mpi_uint *d, mbedtls_mpi_uint b) |
int | mbedtls_mpi_mul_mpi (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B) |
Baseline multiplication: X = A * B. More... | |
int | mbedtls_mpi_mul_int (mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b) |
Baseline multiplication: X = A * b. More... | |
static mbedtls_mpi_uint | mbedtls_int_div_int (mbedtls_mpi_uint u1, mbedtls_mpi_uint u0, mbedtls_mpi_uint d, mbedtls_mpi_uint *r) |
int | mbedtls_mpi_div_mpi (mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B) |
Division by mbedtls_mpi: A = Q * B + R. More... | |
int | mbedtls_mpi_div_int (mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b) |
Division by int: A = Q * b + R. More... | |
int | mbedtls_mpi_mod_mpi (mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B) |
Modulo: R = A mod B. More... | |
int | mbedtls_mpi_mod_int (mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b) |
Modulo: r = A mod b. More... | |
static void | mpi_montg_init (mbedtls_mpi_uint *mm, const mbedtls_mpi *N) |
static int | mpi_montmul (mbedtls_mpi *A, const mbedtls_mpi *B, const mbedtls_mpi *N, mbedtls_mpi_uint mm, const mbedtls_mpi *T) |
static int | mpi_montred (mbedtls_mpi *A, const mbedtls_mpi *N, mbedtls_mpi_uint mm, const mbedtls_mpi *T) |
int | mbedtls_mpi_exp_mod (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR) |
Sliding-window exponentiation: X = A^E mod N. More... | |
int | mbedtls_mpi_gcd (mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B) |
Greatest common divisor: G = gcd(A, B) More... | |
int | mbedtls_mpi_fill_random (mbedtls_mpi *X, size_t size, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Fill an MPI X with size bytes of random. More... | |
int | mbedtls_mpi_inv_mod (mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N) |
Modular inverse: X = A^-1 mod N. More... | |
static int | mpi_check_small_factors (const mbedtls_mpi *X) |
static int | mpi_miller_rabin (const mbedtls_mpi *X, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
int | mbedtls_mpi_is_prime (const mbedtls_mpi *X, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Miller-Rabin primality test. More... | |
int | mbedtls_mpi_gen_prime (mbedtls_mpi *X, size_t nbits, int dh_flag, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
Prime number generation. More... | |
int | mbedtls_mpi_self_test (int verbose) |
Checkup routine. More... | |
Variables | |
static const int | small_prime [] |
static const int | gcd_pairs [GCD_PAIR_COUNT][3] |
#define ciL (sizeof(mbedtls_mpi_uint)) /* chars in limb */ |
#define GCD_PAIR_COUNT 3 |
#define MPI_SIZE_T_MAX ( (size_t) -1 ) /* SIZE_T_MAX is not standard */ |
|
static |
|
static |
int mbedtls_mpi_add_abs | ( | mbedtls_mpi * | X, |
const mbedtls_mpi * | A, | ||
const mbedtls_mpi * | B | ||
) |
Unsigned addition: X = |A| + |B|.
X | Destination MPI |
A | Left-hand MPI |
B | Right-hand MPI |
int mbedtls_mpi_add_int | ( | mbedtls_mpi * | X, |
const mbedtls_mpi * | A, | ||
mbedtls_mpi_sint | b | ||
) |
Signed addition: X = A + b.
X | Destination MPI |
A | Left-hand MPI |
b | The integer value to add |
int mbedtls_mpi_add_mpi | ( | mbedtls_mpi * | X, |
const mbedtls_mpi * | A, | ||
const mbedtls_mpi * | B | ||
) |
Signed addition: X = A + B.
X | Destination MPI |
A | Left-hand MPI |
B | Right-hand MPI |
size_t mbedtls_mpi_bitlen | ( | const mbedtls_mpi * | X | ) |
Return the number of bits up to and including the most significant '1' bit'.
Note: Thus also the one-based index of the most significant '1' bit
X | MPI to use |
int mbedtls_mpi_cmp_abs | ( | const mbedtls_mpi * | X, |
const mbedtls_mpi * | Y | ||
) |
Compare unsigned values.
X | Left-hand MPI |
Y | Right-hand MPI |
int mbedtls_mpi_cmp_int | ( | const mbedtls_mpi * | X, |
mbedtls_mpi_sint | z | ||
) |
Compare signed values.
X | Left-hand MPI |
z | The integer value to compare to |
int mbedtls_mpi_cmp_mpi | ( | const mbedtls_mpi * | X, |
const mbedtls_mpi * | Y | ||
) |
Compare signed values.
X | Left-hand MPI |
Y | Right-hand MPI |
int mbedtls_mpi_copy | ( | mbedtls_mpi * | X, |
const mbedtls_mpi * | Y | ||
) |
Copy the contents of Y into X.
X | Destination MPI |
Y | Source MPI |
int mbedtls_mpi_div_int | ( | mbedtls_mpi * | Q, |
mbedtls_mpi * | R, | ||
const mbedtls_mpi * | A, | ||
mbedtls_mpi_sint | b | ||
) |
Division by int: A = Q * b + R.
Q | Destination MPI for the quotient |
R | Destination MPI for the rest value |
A | Left-hand MPI |
b | Integer to divide by |
int mbedtls_mpi_div_mpi | ( | mbedtls_mpi * | Q, |
mbedtls_mpi * | R, | ||
const mbedtls_mpi * | A, | ||
const mbedtls_mpi * | B | ||
) |
Division by mbedtls_mpi: A = Q * B + R.
Q | Destination MPI for the quotient |
R | Destination MPI for the rest value |
A | Left-hand MPI |
B | Right-hand MPI |
int mbedtls_mpi_exp_mod | ( | mbedtls_mpi * | X, |
const mbedtls_mpi * | A, | ||
const mbedtls_mpi * | E, | ||
const mbedtls_mpi * | N, | ||
mbedtls_mpi * | _RR | ||
) |
Sliding-window exponentiation: X = A^E mod N.
X | Destination MPI |
A | Left-hand MPI |
E | Exponent MPI |
N | Modular MPI |
int mbedtls_mpi_fill_random | ( | mbedtls_mpi * | X, |
size_t | size, | ||
int(*)(void *, unsigned char *, size_t) | f_rng, | ||
void * | p_rng | ||
) |
Fill an MPI X with size bytes of random.
X | Destination MPI |
size | Size in bytes |
f_rng | RNG function |
p_rng | RNG parameter |
void mbedtls_mpi_free | ( | mbedtls_mpi * | X | ) |
Unallocate one MPI.
X | One MPI to unallocate. |
int mbedtls_mpi_gcd | ( | mbedtls_mpi * | G, |
const mbedtls_mpi * | A, | ||
const mbedtls_mpi * | B | ||
) |
Greatest common divisor: G = gcd(A, B)
G | Destination MPI |
A | Left-hand MPI |
B | Right-hand MPI |
int mbedtls_mpi_gen_prime | ( | mbedtls_mpi * | X, |
size_t | nbits, | ||
int | dh_flag, | ||
int(*)(void *, unsigned char *, size_t) | f_rng, | ||
void * | p_rng | ||
) |
Prime number generation.
X | Destination MPI |
nbits | Required size of X in bits ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS ) |
dh_flag | If 1, then (X-1)/2 will be prime too |
f_rng | RNG function |
p_rng | RNG parameter |
int mbedtls_mpi_get_bit | ( | const mbedtls_mpi * | X, |
size_t | pos | ||
) |
Get a specific bit from X.
X | MPI to use |
pos | Zero-based index of the bit in X |
int mbedtls_mpi_grow | ( | mbedtls_mpi * | X, |
size_t | nblimbs | ||
) |
Enlarge to the specified number of limbs.
X | MPI to grow |
nblimbs | The target number of limbs |
void mbedtls_mpi_init | ( | mbedtls_mpi * | X | ) |
Initialize one MPI (make internal references valid) This just makes it ready to be set or freed, but does not define a value for the MPI.
X | One MPI to initialize. |
int mbedtls_mpi_inv_mod | ( | mbedtls_mpi * | X, |
const mbedtls_mpi * | A, | ||
const mbedtls_mpi * | N | ||
) |
Modular inverse: X = A^-1 mod N.
X | Destination MPI |
A | Left-hand MPI |
N | Right-hand MPI |
int mbedtls_mpi_is_prime | ( | const mbedtls_mpi * | X, |
int(*)(void *, unsigned char *, size_t) | f_rng, | ||
void * | p_rng | ||
) |
Miller-Rabin primality test.
X | MPI to check |
f_rng | RNG function |
p_rng | RNG parameter |
size_t mbedtls_mpi_lsb | ( | const mbedtls_mpi * | X | ) |
Return the number of zero-bits before the least significant '1' bit.
Note: Thus also the zero-based index of the least significant '1' bit
X | MPI to use |
int mbedtls_mpi_lset | ( | mbedtls_mpi * | X, |
mbedtls_mpi_sint | z | ||
) |
Set value from integer.
X | MPI to set |
z | Value to use |
int mbedtls_mpi_mod_int | ( | mbedtls_mpi_uint * | r, |
const mbedtls_mpi * | A, | ||
mbedtls_mpi_sint | b | ||
) |
Modulo: r = A mod b.
r | Destination mbedtls_mpi_uint |
A | Left-hand MPI |
b | Integer to divide by |
int mbedtls_mpi_mod_mpi | ( | mbedtls_mpi * | R, |
const mbedtls_mpi * | A, | ||
const mbedtls_mpi * | B | ||
) |
Modulo: R = A mod B.
R | Destination MPI for the rest value |
A | Left-hand MPI |
B | Right-hand MPI |
int mbedtls_mpi_mul_int | ( | mbedtls_mpi * | X, |
const mbedtls_mpi * | A, | ||
mbedtls_mpi_uint | b | ||
) |
Baseline multiplication: X = A * b.
X | Destination MPI |
A | Left-hand MPI |
b | The unsigned integer value to multiply with |
int mbedtls_mpi_mul_mpi | ( | mbedtls_mpi * | X, |
const mbedtls_mpi * | A, | ||
const mbedtls_mpi * | B | ||
) |
Baseline multiplication: X = A * B.
X | Destination MPI |
A | Left-hand MPI |
B | Right-hand MPI |
int mbedtls_mpi_read_binary | ( | mbedtls_mpi * | X, |
const unsigned char * | buf, | ||
size_t | buflen | ||
) |
Import X from unsigned binary data, big endian.
X | Destination MPI |
buf | Input buffer |
buflen | Input buffer size |
int mbedtls_mpi_read_file | ( | mbedtls_mpi * | X, |
int | radix, | ||
FILE * | fin | ||
) |
Read MPI from a line in an opened file.
X | Destination MPI |
radix | Input numeric base |
fin | Input file handle |
The function returns 0 on an empty line.
Leading whitespaces are ignored, as is a '0x' prefix for radix 16.
int mbedtls_mpi_read_string | ( | mbedtls_mpi * | X, |
int | radix, | ||
const char * | s | ||
) |
Import from an ASCII string.
X | Destination MPI |
radix | Input numeric base |
s | Null-terminated string buffer |
int mbedtls_mpi_safe_cond_assign | ( | mbedtls_mpi * | X, |
const mbedtls_mpi * | Y, | ||
unsigned char | assign | ||
) |
Safe conditional assignement X = Y if assign is 1.
X | MPI to conditionally assign to |
Y | Value to be assigned |
assign | 1: perform the assignment, 0: keep X's original value |
int mbedtls_mpi_safe_cond_swap | ( | mbedtls_mpi * | X, |
mbedtls_mpi * | Y, | ||
unsigned char | assign | ||
) |
Safe conditional swap X <-> Y if swap is 1.
X | First mbedtls_mpi value |
Y | Second mbedtls_mpi value |
assign | 1: perform the swap, 0: keep X and Y's original values |
int mbedtls_mpi_self_test | ( | int | verbose | ) |
Checkup routine.
int mbedtls_mpi_set_bit | ( | mbedtls_mpi * | X, |
size_t | pos, | ||
unsigned char | val | ||
) |
Set a bit of X to a specific value of 0 or 1.
X | MPI to use |
pos | Zero-based index of the bit in X |
val | The value to set the bit to (0 or 1) |
int mbedtls_mpi_shift_l | ( | mbedtls_mpi * | X, |
size_t | count | ||
) |
Left-shift: X <<= count.
X | MPI to shift |
count | Amount to shift |
int mbedtls_mpi_shift_r | ( | mbedtls_mpi * | X, |
size_t | count | ||
) |
Right-shift: X >>= count.
X | MPI to shift |
count | Amount to shift |
int mbedtls_mpi_shrink | ( | mbedtls_mpi * | X, |
size_t | nblimbs | ||
) |
Resize down, keeping at least the specified number of limbs.
X | MPI to shrink |
nblimbs | The minimum number of limbs to keep |
size_t mbedtls_mpi_size | ( | const mbedtls_mpi * | X | ) |
Return the total size in bytes.
X | MPI to use |
int mbedtls_mpi_sub_abs | ( | mbedtls_mpi * | X, |
const mbedtls_mpi * | A, | ||
const mbedtls_mpi * | B | ||
) |
Unsigned subtraction: X = |A| - |B|.
X | Destination MPI |
A | Left-hand MPI |
B | Right-hand MPI |
int mbedtls_mpi_sub_int | ( | mbedtls_mpi * | X, |
const mbedtls_mpi * | A, | ||
mbedtls_mpi_sint | b | ||
) |
Signed subtraction: X = A - b.
X | Destination MPI |
A | Left-hand MPI |
b | The integer value to subtract |
int mbedtls_mpi_sub_mpi | ( | mbedtls_mpi * | X, |
const mbedtls_mpi * | A, | ||
const mbedtls_mpi * | B | ||
) |
Signed subtraction: X = A - B.
X | Destination MPI |
A | Left-hand MPI |
B | Right-hand MPI |
void mbedtls_mpi_swap | ( | mbedtls_mpi * | X, |
mbedtls_mpi * | Y | ||
) |
Swap the contents of X and Y.
X | First MPI value |
Y | Second MPI value |
int mbedtls_mpi_write_binary | ( | const mbedtls_mpi * | X, |
unsigned char * | buf, | ||
size_t | buflen | ||
) |
Export X into unsigned binary data, big endian. Always fills the whole buffer, which will start with zeros if the number is smaller.
X | Source MPI |
buf | Output buffer |
buflen | Output buffer size |
int mbedtls_mpi_write_file | ( | const char * | p, |
const mbedtls_mpi * | X, | ||
int | radix, | ||
FILE * | fout | ||
) |
Write X into an opened file, or stdout if fout is NULL.
p | Prefix, can be NULL |
X | Source MPI |
radix | Output numeric base |
fout | Output file handle (can be NULL) |
int mbedtls_mpi_write_string | ( | const mbedtls_mpi * | X, |
int | radix, | ||
char * | buf, | ||
size_t | buflen, | ||
size_t * | olen | ||
) |
Export into an ASCII string.
X | Source MPI |
radix | Output numeric base |
buf | Buffer to write the string to |
buflen | Length of buf |
olen | Length of the string written, including final NUL byte |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |