bignum.c File Reference

#include "polarssl/config.h"
#include "polarssl/bignum.h"
#include "polarssl/bn_mul.h"
#include <stdlib.h>
Include dependency graph for bignum.c:

Go to the source code of this file.

Defines

#define polarssl_malloc   malloc
#define polarssl_free   free
#define ciL   (sizeof(t_uint))
#define biL   (ciL << 3)
#define biH   (ciL << 2)
#define BITS_TO_LIMBS(i)   (((i) + biL - 1) / biL)
#define CHARS_TO_LIMBS(i)   (((i) + ciL - 1) / ciL)
#define GCD_PAIR_COUNT   3

Functions

void mpi_init (mpi *X)
 Initialize one MPI.
void mpi_free (mpi *X)
 Unallocate one MPI.
int mpi_grow (mpi *X, size_t nblimbs)
 Enlarge to the specified number of limbs.
int mpi_copy (mpi *X, const mpi *Y)
 Copy the contents of Y into X.
void mpi_swap (mpi *X, mpi *Y)
 Swap the contents of X and Y.
int mpi_lset (mpi *X, t_sint z)
 Set value from integer.
int mpi_get_bit (const mpi *X, size_t pos)
 Get a specific bit from X.
int mpi_set_bit (mpi *X, size_t pos, unsigned char val)
 Set a bit of X to a specific value of 0 or 1.
size_t mpi_lsb (const mpi *X)
 Return the number of zero-bits before the least significant '1' bit.
size_t mpi_msb (const mpi *X)
 Return the number of bits up to and including the most significant '1' bit'.
size_t mpi_size (const mpi *X)
 Return the total size in bytes.
static int mpi_get_digit (t_uint *d, int radix, char c)
int mpi_read_string (mpi *X, int radix, const char *s)
 Import from an ASCII string.
static int mpi_write_hlp (mpi *X, int radix, char **p)
int mpi_write_string (const mpi *X, int radix, char *s, size_t *slen)
 Export into an ASCII string.
int mpi_read_file (mpi *X, int radix, FILE *fin)
int mpi_write_file (const char *p, const mpi *X, int radix, FILE *fout)
int mpi_read_binary (mpi *X, const unsigned char *buf, size_t buflen)
 Import X from unsigned binary data, big endian.
int mpi_write_binary (const mpi *X, unsigned char *buf, size_t buflen)
 Export X into unsigned binary data, big endian.
int mpi_shift_l (mpi *X, size_t count)
 Left-shift: X <<= count.
int mpi_shift_r (mpi *X, size_t count)
 Right-shift: X >>= count.
int mpi_cmp_abs (const mpi *X, const mpi *Y)
 Compare unsigned values.
int mpi_cmp_mpi (const mpi *X, const mpi *Y)
 Compare signed values.
int mpi_cmp_int (const mpi *X, t_sint z)
 Compare signed values.
int mpi_add_abs (mpi *X, const mpi *A, const mpi *B)
 Unsigned addition: X = |A| + |B|.
static void mpi_sub_hlp (size_t n, t_uint *s, t_uint *d)
int mpi_sub_abs (mpi *X, const mpi *A, const mpi *B)
 Unsigned subtraction: X = |A| - |B|.
int mpi_add_mpi (mpi *X, const mpi *A, const mpi *B)
 Signed addition: X = A + B.
int mpi_sub_mpi (mpi *X, const mpi *A, const mpi *B)
 Signed subtraction: X = A - B.
int mpi_add_int (mpi *X, const mpi *A, t_sint b)
 Signed addition: X = A + b.
int mpi_sub_int (mpi *X, const mpi *A, t_sint b)
 Signed subtraction: X = A - b.
static void mpi_mul_hlp (size_t i, t_uint *s, t_uint *d, t_uint b)
int mpi_mul_mpi (mpi *X, const mpi *A, const mpi *B)
 Baseline multiplication: X = A * B.
int mpi_mul_int (mpi *X, const mpi *A, t_sint b)
 Baseline multiplication: X = A * b Note: b is an unsigned integer type, thus Negative values of b are ignored.
int mpi_div_mpi (mpi *Q, mpi *R, const mpi *A, const mpi *B)
 Division by mpi: A = Q * B + R.
int mpi_div_int (mpi *Q, mpi *R, const mpi *A, t_sint b)
 Division by int: A = Q * b + R.
int mpi_mod_mpi (mpi *R, const mpi *A, const mpi *B)
 Modulo: R = A mod B.
int mpi_mod_int (t_uint *r, const mpi *A, t_sint b)
 Modulo: r = A mod b.
static void mpi_montg_init (t_uint *mm, const mpi *N)
static void mpi_montmul (mpi *A, const mpi *B, const mpi *N, t_uint mm, const mpi *T)
static void mpi_montred (mpi *A, const mpi *N, t_uint mm, const mpi *T)
int mpi_exp_mod (mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR)
 Sliding-window exponentiation: X = A^E mod N.
int mpi_gcd (mpi *G, const mpi *A, const mpi *B)
 Greatest common divisor: G = gcd(A, B).
int mpi_fill_random (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.
int mpi_inv_mod (mpi *X, const mpi *A, const mpi *N)
 Modular inverse: X = A^-1 mod N.
int mpi_is_prime (mpi *X, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
 Miller-Rabin primality test.
int mpi_gen_prime (mpi *X, size_t nbits, int dh_flag, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
 Prime number generation.
int mpi_self_test (int verbose)
 Checkup routine.

Variables

static const int small_prime []
static const int gcd_pairs [GCD_PAIR_COUNT][3]

Define Documentation

#define biH   (ciL << 2)

Definition at line 51 of file bignum.c.

Referenced by mpi_div_mpi(), and mpi_mod_int().

#define biL   (ciL << 3)
#define BITS_TO_LIMBS (  )     (((i) + biL - 1) / biL)

Definition at line 56 of file bignum.c.

Referenced by mpi_gen_prime(), mpi_read_string(), and mpi_shift_l().

#define CHARS_TO_LIMBS (  )     (((i) + ciL - 1) / ciL)

Definition at line 57 of file bignum.c.

Referenced by mpi_fill_random(), mpi_read_binary(), and not_rnd_mpi().

#define ciL   (sizeof(t_uint))
#define GCD_PAIR_COUNT   3

Definition at line 1989 of file bignum.c.

Referenced by mpi_self_test().

#define polarssl_free   free

Definition at line 44 of file bignum.c.

#define polarssl_malloc   malloc

Definition at line 43 of file bignum.c.


Function Documentation

int mpi_add_abs ( mpi X,
const mpi A,
const mpi B 
)

Unsigned addition: X = |A| + |B|.

Parameters:
X Destination MPI
A Left-hand MPI
B Right-hand MPI
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed

Definition at line 741 of file bignum.c.

References MPI_CHK, mpi_copy(), mpi_grow(), mpi::n, mpi::p, and mpi::s.

Referenced by ecp_mod_p521(), mpi_add_mpi(), mpi_sub_mpi(), test_suite_mpi_add_abs(), test_suite_mpi_add_abs_add_first(), and test_suite_mpi_add_abs_add_second().

int mpi_add_int ( mpi X,
const mpi A,
t_sint  b 
)

Signed addition: X = A + b.

Parameters:
X Destination MPI
A Left-hand MPI
b The integer value to add
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed

Definition at line 919 of file bignum.c.

References mpi_add_mpi(), mpi::n, mpi::p, and mpi::s.

Referenced by ecp_group_read_string(), ecp_mul(), mpi_gen_prime(), mpi_read_string(), and test_suite_mpi_add_int().

int mpi_add_mpi ( mpi X,
const mpi A,
const mpi B 
)

Signed addition: X = A + B.

Parameters:
X Destination MPI
A Left-hand MPI
B Right-hand MPI
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed

Definition at line 857 of file bignum.c.

References mpi_add_abs(), MPI_CHK, mpi_cmp_abs(), mpi_sub_abs(), and mpi::s.

Referenced by ecdsa_sign(), ecp_check_pubkey(), ecp_double_jac(), ecp_modp(), mpi_add_int(), mpi_div_mpi(), mpi_exp_mod(), mpi_inv_mod(), mpi_mod_mpi(), rsa_private(), and test_suite_mpi_add_mpi().

int mpi_cmp_abs ( const mpi X,
const mpi Y 
)

Compare unsigned values.

Parameters:
X Left-hand MPI
Y Right-hand MPI
Returns:
1 if |X| is greater than |Y|, -1 if |X| is lesser than |Y| or 0 if |X| is equal to |Y|

Definition at line 662 of file bignum.c.

References mpi::n, and mpi::p.

Referenced by mpi_add_mpi(), mpi_div_mpi(), mpi_montmul(), mpi_sub_abs(), mpi_sub_mpi(), and test_suite_mpi_cmp_abs().

int mpi_cmp_int ( const mpi X,
t_sint  z 
)
int mpi_cmp_mpi ( const mpi X,
const mpi Y 
)

Compare signed values.

Parameters:
X Left-hand MPI
Y Right-hand MPI
Returns:
1 if X is greater than Y, -1 if X is lesser than Y or 0 if X is equal to Y

Definition at line 692 of file bignum.c.

References mpi::n, mpi::p, and mpi::s.

Referenced by dhm_check_range(), dhm_make_params(), dhm_make_public(), dhm_update_blinding(), ecdsa_verify(), ecp_check_privkey(), ecp_check_pubkey(), ecp_gen_keypair(), ecp_modp(), ecp_mul(), ecp_randomize_coordinates(), mpi_cmp_int(), mpi_div_mpi(), mpi_exp_mod(), mpi_gcd(), mpi_inv_mod(), mpi_is_prime(), mpi_mod_mpi(), mpi_self_test(), rsa_check_privkey(), rsa_gen_key(), rsa_private(), rsa_public(), test_suite_ecdh_exchange(), test_suite_ecdh_primitive_random(), test_suite_ecdh_primitive_testvec(), test_suite_ecdsa_prim_test_vectors(), test_suite_ecp_fast_mod(), test_suite_ecp_read_binary(), test_suite_ecp_test_vect(), test_suite_ecp_tls_read_point(), test_suite_ecp_tls_write_read_group(), test_suite_ecp_tls_write_read_point(), test_suite_mpi_add_abs(), test_suite_mpi_add_abs_add_first(), test_suite_mpi_add_abs_add_second(), test_suite_mpi_add_int(), test_suite_mpi_add_mpi(), test_suite_mpi_cmp_mpi(), test_suite_mpi_copy(), test_suite_mpi_div_int(), test_suite_mpi_div_mpi(), test_suite_mpi_exp_mod(), test_suite_mpi_gcd(), test_suite_mpi_inv_mod(), test_suite_mpi_mod_mpi(), test_suite_mpi_mul_int(), test_suite_mpi_mul_mpi(), test_suite_mpi_set_bit(), test_suite_mpi_shift_l(), test_suite_mpi_shift_r(), test_suite_mpi_sub_abs(), test_suite_mpi_sub_int(), test_suite_mpi_sub_mpi(), test_suite_mpi_swap(), and test_suite_mpi_write_file().

int mpi_copy ( mpi X,
const mpi Y 
)
int mpi_div_int ( mpi Q,
mpi R,
const mpi A,
t_sint  b 
)

Division by int: A = Q * b + R.

Parameters:
Q Destination MPI for the quotient
R Destination MPI for the rest value
A Left-hand MPI
b Integer to divide by
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Note:
Either Q or R can be NULL.

Definition at line 1240 of file bignum.c.

References mpi_div_mpi(), mpi::n, mpi::p, and mpi::s.

Referenced by mpi_write_hlp(), and test_suite_mpi_div_int().

int mpi_div_mpi ( mpi Q,
mpi R,
const mpi A,
const mpi B 
)

Division by mpi: A = Q * B + R.

Parameters:
Q Destination MPI for the quotient
R Destination MPI for the rest value
A Left-hand MPI
B Right-hand MPI
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Note:
Either Q or R can be NULL.

Definition at line 1076 of file bignum.c.

References biH, biL, mpi_add_mpi(), MPI_CHK, mpi_cmp_abs(), mpi_cmp_int(), mpi_cmp_mpi(), mpi_copy(), mpi_free(), mpi_grow(), mpi_init(), mpi_lset(), mpi_msb(), mpi_mul_int(), mpi_shift_l(), mpi_shift_r(), mpi_sub_mpi(), mpi::n, mpi::p, POLARSSL_ERR_MPI_DIVISION_BY_ZERO, and mpi::s.

Referenced by mpi_div_int(), mpi_mod_mpi(), mpi_self_test(), rsa_check_privkey(), and test_suite_mpi_div_mpi().

int mpi_exp_mod ( mpi X,
const mpi A,
const mpi E,
const mpi N,
mpi _RR 
)

Sliding-window exponentiation: X = A^E mod N.

Parameters:
X Destination MPI
A Left-hand MPI
E Exponent MPI
N Modular MPI
_RR Speed-up MPI used for recalculations
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or if E is negative
Note:
_RR is used to avoid re-computing R*R mod N across multiple calls, which speeds up things a bit. It can be set to NULL if the extra performance is unneeded.

Definition at line 1405 of file bignum.c.

References biL, mpi_add_mpi(), MPI_CHK, mpi_cmp_int(), mpi_cmp_mpi(), mpi_copy(), mpi_free(), mpi_grow(), mpi_init(), mpi_lset(), mpi_mod_mpi(), mpi_montg_init(), mpi_montmul(), mpi_montred(), mpi_msb(), mpi_shift_l(), mpi::n, mpi::p, POLARSSL_ERR_MPI_BAD_INPUT_DATA, POLARSSL_MPI_WINDOW_SIZE, and mpi::s.

Referenced by dhm_calc_secret(), dhm_make_params(), dhm_make_public(), dhm_update_blinding(), mpi_is_prime(), mpi_self_test(), rsa_prepare_blinding(), rsa_private(), rsa_public(), and test_suite_mpi_exp_mod().

int mpi_fill_random ( 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.

Parameters:
X Destination MPI
size Size in bytes
f_rng RNG function
p_rng RNG parameter
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed

Definition at line 1662 of file bignum.c.

References CHARS_TO_LIMBS, MPI_CHK, mpi_grow(), mpi_lset(), and mpi::p.

Referenced by dhm_make_params(), dhm_make_public(), dhm_update_blinding(), ecp_gen_keypair(), ecp_randomize_coordinates(), mpi_gen_prime(), mpi_is_prime(), and rsa_prepare_blinding().

void mpi_free ( mpi X  ) 

Unallocate one MPI.

Parameters:
X One MPI to unallocate.

Definition at line 75 of file bignum.c.

References ciL, mpi::n, mpi::p, polarssl_free, and mpi::s.

Referenced by dhm_calc_secret(), dhm_check_range(), dhm_free(), ecdh_free(), ecdsa_free(), ecdsa_sign(), ecdsa_verify(), ecp_add_mixed(), ecp_check_pubkey(), ecp_double_jac(), ecp_group_free(), ecp_keypair_free(), ecp_mul(), ecp_normalize(), ecp_normalize_many(), ecp_point_free(), ecp_randomize_coordinates(), ecp_self_test(), ecp_w_naf_fixed(), mpi_copy(), mpi_div_mpi(), mpi_exp_mod(), mpi_gcd(), mpi_gen_prime(), mpi_inv_mod(), mpi_is_prime(), mpi_mul_mpi(), mpi_read_string(), mpi_self_test(), mpi_sub_abs(), mpi_write_string(), rsa_check_privkey(), rsa_free(), rsa_gen_key(), rsa_private(), rsa_public(), ssl_free(), test_suite_debug_print_mpi(), test_suite_ecdh_primitive_random(), test_suite_ecdh_primitive_testvec(), test_suite_ecdsa_prim_random(), test_suite_ecdsa_prim_test_vectors(), test_suite_ecp_check_privkey(), test_suite_ecp_fast_mod(), test_suite_ecp_gen_keypair(), test_suite_ecp_read_binary(), test_suite_ecp_small_mul(), test_suite_ecp_test_vect(), test_suite_ecp_tls_read_point(), test_suite_mpi_add_abs(), test_suite_mpi_add_abs_add_first(), test_suite_mpi_add_abs_add_second(), test_suite_mpi_add_int(), test_suite_mpi_add_mpi(), test_suite_mpi_cmp_abs(), test_suite_mpi_cmp_int(), test_suite_mpi_cmp_mpi(), test_suite_mpi_copy(), test_suite_mpi_copy_self(), test_suite_mpi_div_int(), test_suite_mpi_div_mpi(), test_suite_mpi_exp_mod(), test_suite_mpi_gcd(), test_suite_mpi_get_bit(), test_suite_mpi_inv_mod(), test_suite_mpi_is_prime(), test_suite_mpi_lsb(), test_suite_mpi_mod_int(), test_suite_mpi_mod_mpi(), test_suite_mpi_msb(), test_suite_mpi_mul_int(), test_suite_mpi_mul_mpi(), test_suite_mpi_read_binary(), test_suite_mpi_read_file(), test_suite_mpi_read_write_string(), test_suite_mpi_set_bit(), test_suite_mpi_shift_l(), test_suite_mpi_shift_r(), test_suite_mpi_sub_abs(), test_suite_mpi_sub_int(), test_suite_mpi_sub_mpi(), test_suite_mpi_swap(), test_suite_mpi_write_binary(), test_suite_mpi_write_file(), test_suite_pk_rsa_decrypt_test_vec(), test_suite_pkcs1_rsaes_oaep_decrypt(), test_suite_pkcs1_rsassa_pss_sign(), test_suite_rsa_pkcs1_decrypt(), test_suite_rsa_pkcs1_sign(), test_suite_rsa_pkcs1_sign_raw(), test_suite_rsa_private(), test_suite_x509_crt_check(), and x509write_crt_free().

int mpi_gcd ( mpi G,
const mpi A,
const mpi B 
)

Greatest common divisor: G = gcd(A, B).

Parameters:
G Destination MPI
A Left-hand MPI
B Right-hand MPI
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed

Definition at line 1613 of file bignum.c.

References MPI_CHK, mpi_cmp_int(), mpi_cmp_mpi(), mpi_copy(), mpi_free(), mpi_init(), mpi_lsb(), mpi_shift_l(), mpi_shift_r(), mpi_sub_abs(), and mpi::s.

Referenced by mpi_inv_mod(), mpi_self_test(), rsa_check_privkey(), rsa_gen_key(), rsa_prepare_blinding(), test_suite_mpi_gcd(), test_suite_pk_rsa_decrypt_test_vec(), test_suite_pkcs1_rsaes_oaep_decrypt(), test_suite_pkcs1_rsassa_pss_sign(), test_suite_rsa_pkcs1_decrypt(), test_suite_rsa_pkcs1_sign(), test_suite_rsa_pkcs1_sign_raw(), and test_suite_rsa_private().

int mpi_gen_prime ( mpi X,
size_t  nbits,
int  dh_flag,
int(*)(void *, unsigned char *, size_t)  f_rng,
void *  p_rng 
)

Prime number generation.

Parameters:
X Destination MPI
nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
dh_flag If 1, then (X-1)/2 will be prime too
f_rng RNG function
p_rng RNG parameter
Returns:
0 if successful (probably prime), POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3

Definition at line 1920 of file bignum.c.

References BITS_TO_LIMBS, ciL, mpi_add_int(), MPI_CHK, mpi_fill_random(), mpi_free(), mpi_init(), mpi_is_prime(), mpi_msb(), mpi_shift_l(), mpi_shift_r(), mpi_sub_int(), mpi::p, POLARSSL_ERR_MPI_BAD_INPUT_DATA, POLARSSL_ERR_MPI_NOT_ACCEPTABLE, and POLARSSL_MPI_MAX_BITS.

Referenced by rsa_gen_key().

int mpi_get_bit ( const mpi X,
size_t  pos 
)

Get a specific bit from X.

Parameters:
X MPI to use
pos Zero-based index of the bit in X
Returns:
Either a 0 or a 1

Definition at line 189 of file bignum.c.

References biL, mpi::n, and mpi::p.

Referenced by ecp_mul(), ecp_point_write_binary(), and test_suite_mpi_get_bit().

static int mpi_get_digit ( t_uint d,
int  radix,
char  c 
) [static]

Definition at line 268 of file bignum.c.

References POLARSSL_ERR_MPI_INVALID_CHARACTER.

Referenced by mpi_read_file(), and mpi_read_string().

int mpi_grow ( mpi X,
size_t  nblimbs 
)

Enlarge to the specified number of limbs.

Parameters:
X MPI to grow
nblimbs The target number of limbs
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed

Definition at line 94 of file bignum.c.

References ciL, mpi::n, mpi::p, POLARSSL_ERR_MPI_MALLOC_FAILED, polarssl_free, polarssl_malloc, and POLARSSL_MPI_MAX_LIMBS.

Referenced by ecp_mod_p192(), mpi_add_abs(), mpi_copy(), mpi_div_mpi(), mpi_exp_mod(), mpi_fill_random(), mpi_lset(), mpi_mul_mpi(), mpi_read_binary(), mpi_read_string(), mpi_set_bit(), and mpi_shift_l().

void mpi_init ( mpi X  ) 

Initialize one MPI.

Parameters:
X One MPI to initialize.

Definition at line 62 of file bignum.c.

References mpi::n, mpi::p, and mpi::s.

Referenced by dhm_calc_secret(), dhm_check_range(), ecdsa_init(), ecdsa_sign(), ecdsa_verify(), ecp_add_mixed(), ecp_check_pubkey(), ecp_double_jac(), ecp_keypair_init(), ecp_mul(), ecp_normalize(), ecp_normalize_many(), ecp_point_init(), ecp_randomize_coordinates(), ecp_self_test(), ecp_w_naf_fixed(), mpi_div_mpi(), mpi_exp_mod(), mpi_gcd(), mpi_gen_prime(), mpi_inv_mod(), mpi_is_prime(), mpi_mul_mpi(), mpi_read_string(), mpi_self_test(), mpi_sub_abs(), mpi_write_string(), rsa_check_privkey(), rsa_gen_key(), rsa_private(), rsa_public(), test_suite_debug_print_mpi(), test_suite_ecdh_primitive_random(), test_suite_ecdh_primitive_testvec(), test_suite_ecdsa_prim_random(), test_suite_ecdsa_prim_test_vectors(), test_suite_ecp_check_privkey(), test_suite_ecp_fast_mod(), test_suite_ecp_gen_keypair(), test_suite_ecp_read_binary(), test_suite_ecp_small_mul(), test_suite_ecp_test_vect(), test_suite_ecp_tls_read_point(), test_suite_mpi_add_abs(), test_suite_mpi_add_abs_add_first(), test_suite_mpi_add_abs_add_second(), test_suite_mpi_add_int(), test_suite_mpi_add_mpi(), test_suite_mpi_cmp_abs(), test_suite_mpi_cmp_int(), test_suite_mpi_cmp_mpi(), test_suite_mpi_copy(), test_suite_mpi_copy_self(), test_suite_mpi_div_int(), test_suite_mpi_div_mpi(), test_suite_mpi_exp_mod(), test_suite_mpi_gcd(), test_suite_mpi_get_bit(), test_suite_mpi_inv_mod(), test_suite_mpi_is_prime(), test_suite_mpi_lsb(), test_suite_mpi_mod_int(), test_suite_mpi_mod_mpi(), test_suite_mpi_msb(), test_suite_mpi_mul_int(), test_suite_mpi_mul_mpi(), test_suite_mpi_read_binary(), test_suite_mpi_read_file(), test_suite_mpi_read_write_string(), test_suite_mpi_set_bit(), test_suite_mpi_shift_l(), test_suite_mpi_shift_r(), test_suite_mpi_sub_abs(), test_suite_mpi_sub_int(), test_suite_mpi_sub_mpi(), test_suite_mpi_swap(), test_suite_mpi_write_binary(), test_suite_mpi_write_file(), test_suite_pk_rsa_decrypt_test_vec(), test_suite_pkcs1_rsaes_oaep_decrypt(), test_suite_pkcs1_rsassa_pss_sign(), test_suite_rsa_pkcs1_decrypt(), test_suite_rsa_pkcs1_sign(), test_suite_rsa_pkcs1_sign_raw(), test_suite_rsa_private(), test_suite_x509_crt_check(), and x509write_crt_init().

int mpi_inv_mod ( mpi X,
const mpi A,
const mpi N 
)

Modular inverse: X = A^-1 mod N.

Parameters:
X Destination MPI
A Left-hand MPI
N Right-hand MPI
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N

Definition at line 1680 of file bignum.c.

References mpi_add_mpi(), MPI_CHK, mpi_cmp_int(), mpi_cmp_mpi(), mpi_copy(), mpi_free(), mpi_gcd(), mpi_init(), mpi_lset(), mpi_mod_mpi(), mpi_shift_r(), mpi_sub_mpi(), mpi::p, POLARSSL_ERR_MPI_BAD_INPUT_DATA, and POLARSSL_ERR_MPI_NOT_ACCEPTABLE.

Referenced by dhm_update_blinding(), ecdsa_sign(), ecdsa_verify(), ecp_normalize(), ecp_normalize_many(), mpi_self_test(), rsa_check_privkey(), rsa_gen_key(), rsa_prepare_blinding(), test_suite_mpi_inv_mod(), test_suite_pk_rsa_decrypt_test_vec(), test_suite_pkcs1_rsaes_oaep_decrypt(), test_suite_pkcs1_rsassa_pss_sign(), test_suite_rsa_pkcs1_decrypt(), test_suite_rsa_pkcs1_sign(), test_suite_rsa_pkcs1_sign_raw(), and test_suite_rsa_private().

int mpi_is_prime ( mpi X,
int(*)(void *, unsigned char *, size_t)  f_rng,
void *  p_rng 
)

Miller-Rabin primality test.

Parameters:
X MPI to check
f_rng RNG function
p_rng RNG parameter
Returns:
0 if successful (probably prime), POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime

Definition at line 1802 of file bignum.c.

References A, ciL, MPI_CHK, mpi_cmp_int(), mpi_cmp_mpi(), mpi_copy(), mpi_exp_mod(), mpi_fill_random(), mpi_free(), mpi_init(), mpi_lsb(), mpi_mod_int(), mpi_mod_mpi(), mpi_msb(), mpi_mul_mpi(), mpi_shift_r(), mpi_sub_int(), mpi::n, mpi::p, POLARSSL_ERR_MPI_NOT_ACCEPTABLE, R, and mpi::s.

Referenced by mpi_gen_prime(), and test_suite_mpi_is_prime().

size_t mpi_lsb ( const 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

Parameters:
X MPI to use

Definition at line 227 of file bignum.c.

References biL, mpi::n, and mpi::p.

Referenced by mpi_gcd(), mpi_is_prime(), and test_suite_mpi_lsb().

int mpi_lset ( mpi X,
t_sint  z 
)
int mpi_mod_int ( t_uint r,
const mpi A,
t_sint  b 
)

Modulo: r = A mod b.

Parameters:
r Destination t_uint
A Left-hand MPI
b Integer to divide by
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0, POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0

Definition at line 1279 of file bignum.c.

References biH, mpi::n, mpi::p, POLARSSL_ERR_MPI_DIVISION_BY_ZERO, POLARSSL_ERR_MPI_NEGATIVE_VALUE, and mpi::s.

Referenced by mpi_is_prime(), mpi_write_hlp(), and test_suite_mpi_mod_int().

int mpi_mod_mpi ( mpi R,
const mpi A,
const mpi B 
)

Modulo: R = A mod B.

Parameters:
R Destination MPI for the rest value
A Left-hand MPI
B Right-hand MPI
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0, POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0

Definition at line 1256 of file bignum.c.

References mpi_add_mpi(), MPI_CHK, mpi_cmp_int(), mpi_cmp_mpi(), mpi_div_mpi(), mpi_sub_mpi(), and POLARSSL_ERR_MPI_NEGATIVE_VALUE.

Referenced by dhm_calc_secret(), dhm_update_blinding(), ecdsa_sign(), ecdsa_verify(), ecp_modp(), mpi_exp_mod(), mpi_inv_mod(), mpi_is_prime(), rsa_check_privkey(), rsa_gen_key(), rsa_prepare_blinding(), rsa_private(), test_suite_ecp_fast_mod(), test_suite_mpi_mod_mpi(), test_suite_pk_rsa_decrypt_test_vec(), test_suite_pkcs1_rsaes_oaep_decrypt(), test_suite_pkcs1_rsassa_pss_sign(), test_suite_rsa_pkcs1_decrypt(), test_suite_rsa_pkcs1_sign(), test_suite_rsa_pkcs1_sign_raw(), and test_suite_rsa_private().

static void mpi_montg_init ( t_uint mm,
const mpi N 
) [static]

Definition at line 1336 of file bignum.c.

References biL, and mpi::p.

Referenced by mpi_exp_mod().

static void mpi_montmul ( mpi A,
const mpi B,
const mpi N,
t_uint  mm,
const mpi T 
) [static]

Definition at line 1354 of file bignum.c.

References ciL, mpi_cmp_abs(), mpi_mul_hlp(), mpi_sub_hlp(), mpi::n, and mpi::p.

Referenced by mpi_exp_mod(), and mpi_montred().

static void mpi_montred ( mpi A,
const mpi N,
t_uint  mm,
const mpi T 
) [static]

Definition at line 1391 of file bignum.c.

References mpi_montmul(), mpi::n, mpi::p, and mpi::s.

Referenced by mpi_exp_mod().

size_t mpi_msb ( const 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

Parameters:
X MPI to use

Definition at line 242 of file bignum.c.

References biL, mpi::n, and mpi::p.

Referenced by d2i_RSA_PUBKEY(), ecp_group_read_string_gen(), ecp_modp(), ecp_mul(), mpi_div_mpi(), mpi_exp_mod(), mpi_gen_prime(), mpi_is_prime(), mpi_shift_l(), mpi_size(), mpi_write_string(), rsa_check_pubkey(), rsa_gen_key(), rsa_rsassa_pss_sign(), rsa_rsassa_pss_verify(), test_suite_ecp_fast_mod(), test_suite_ecp_tls_read_group(), and test_suite_mpi_msb().

static void mpi_mul_hlp ( size_t  i,
t_uint s,
t_uint d,
t_uint  b 
) [static]

Definition at line 959 of file bignum.c.

References MULADDC_CORE, MULADDC_INIT, and MULADDC_STOP.

Referenced by mpi_montmul(), and mpi_mul_mpi().

int mpi_mul_int ( mpi X,
const mpi A,
t_sint  b 
)

Baseline multiplication: X = A * b Note: b is an unsigned integer type, thus Negative values of b are ignored.

Parameters:
X Destination MPI
A Left-hand MPI
b The integer value to multiply with
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed

Definition at line 1060 of file bignum.c.

References mpi_mul_mpi(), mpi::n, mpi::p, and mpi::s.

Referenced by ecp_add_mixed(), ecp_double_jac(), mpi_div_mpi(), mpi_read_string(), and test_suite_mpi_mul_int().

int mpi_mul_mpi ( mpi X,
const mpi A,
const mpi B 
)
int mpi_read_binary ( mpi X,
const unsigned char *  buf,
size_t  buflen 
)

Import X from unsigned binary data, big endian.

Parameters:
X Destination MPI
buf Input buffer
buflen Input buffer size
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed

Definition at line 526 of file bignum.c.

References CHARS_TO_LIMBS, ciL, MPI_CHK, mpi_grow(), mpi_lset(), and mpi::p.

Referenced by asn1_get_mpi(), d2i_RSA_PUBKEY(), derive_mpi(), dhm_read_bignum(), dhm_read_public(), ecp_point_read_binary(), pk_parse_key_sec1_der(), rsa_private(), rsa_public(), and test_suite_mpi_read_binary().

int mpi_read_file ( mpi X,
int  radix,
FILE *  fin 
)
int mpi_read_string ( mpi X,
int  radix,
const char *  s 
)

Import from an ASCII string.

Parameters:
X Destination MPI
radix Input numeric base
s Null-terminated string buffer
Returns:
0 if successful, or a POLARSSL_ERR_MPI_XXX error code

Definition at line 285 of file bignum.c.

References BITS_TO_LIMBS, ciL, mpi_add_int(), MPI_CHK, mpi_free(), mpi_get_digit(), mpi_grow(), mpi_init(), mpi_lset(), mpi_mul_int(), mpi_sub_int(), mpi::p, POLARSSL_ERR_MPI_BAD_INPUT_DATA, mpi::s, and slen.

Referenced by ecp_group_read_string_gen(), ecp_point_read_string(), ecp_self_test(), mpi_read_file(), mpi_self_test(), not_rnd_mpi(), rsa_self_test(), ssl_init(), ssl_set_dh_param(), test_suite_debug_print_mpi(), test_suite_dhm_do_dhm(), test_suite_ecdh_primitive_testvec(), test_suite_ecdsa_prim_test_vectors(), test_suite_ecp_fast_mod(), test_suite_ecp_read_binary(), test_suite_ecp_test_vect(), test_suite_ecp_tls_read_point(), test_suite_ecp_write_binary(), test_suite_mpi_add_abs(), test_suite_mpi_add_abs_add_first(), test_suite_mpi_add_abs_add_second(), test_suite_mpi_add_int(), test_suite_mpi_add_mpi(), test_suite_mpi_cmp_abs(), test_suite_mpi_cmp_mpi(), test_suite_mpi_div_int(), test_suite_mpi_div_mpi(), test_suite_mpi_exp_mod(), test_suite_mpi_gcd(), test_suite_mpi_get_bit(), test_suite_mpi_inv_mod(), test_suite_mpi_is_prime(), test_suite_mpi_lsb(), test_suite_mpi_mod_int(), test_suite_mpi_mod_mpi(), test_suite_mpi_msb(), test_suite_mpi_mul_int(), test_suite_mpi_mul_mpi(), test_suite_mpi_read_write_string(), test_suite_mpi_set_bit(), test_suite_mpi_shift_l(), test_suite_mpi_shift_r(), test_suite_mpi_sub_abs(), test_suite_mpi_sub_int(), test_suite_mpi_sub_mpi(), test_suite_mpi_write_binary(), test_suite_mpi_write_file(), test_suite_pk_rsa_decrypt_test_vec(), test_suite_pk_rsa_encrypt_test_vec(), test_suite_pk_rsa_verify_test_vec(), test_suite_pkcs1_rsaes_oaep_decrypt(), test_suite_pkcs1_rsaes_oaep_encrypt(), test_suite_pkcs1_rsassa_pss_sign(), test_suite_pkcs1_rsassa_pss_verify(), test_suite_rsa_check_privkey(), test_suite_rsa_check_pubkey(), test_suite_rsa_pkcs1_decrypt(), test_suite_rsa_pkcs1_encrypt(), test_suite_rsa_pkcs1_encrypt_bad_rng(), test_suite_rsa_pkcs1_sign(), test_suite_rsa_pkcs1_sign_raw(), test_suite_rsa_pkcs1_verify(), test_suite_rsa_pkcs1_verify_raw(), test_suite_rsa_private(), test_suite_rsa_public(), and test_suite_x509_crt_check().

int mpi_self_test ( int  verbose  ) 

Checkup routine.

Returns:
0 if successful, or 1 if the test failed

Definition at line 2001 of file bignum.c.

References A, GCD_PAIR_COUNT, MPI_CHK, mpi_cmp_int(), mpi_cmp_mpi(), mpi_div_mpi(), mpi_exp_mod(), mpi_free(), mpi_gcd(), mpi_init(), mpi_inv_mod(), mpi_lset(), mpi_mul_mpi(), and mpi_read_string().

Referenced by test_suite_mpi_selftest().

int mpi_set_bit ( mpi X,
size_t  pos,
unsigned char  val 
)

Set a bit of X to a specific value of 0 or 1.

Note:
Will grow X if necessary to set a bit to 1 in a not yet existing limb. Will not grow if bit should be set to 0
Parameters:
X MPI to use
pos Zero-based index of the bit in X
val The value to set the bit to (0 or 1)
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1

Definition at line 200 of file bignum.c.

References biL, MPI_CHK, mpi_grow(), mpi::n, mpi::p, and POLARSSL_ERR_MPI_BAD_INPUT_DATA.

Referenced by test_suite_mpi_set_bit().

int mpi_shift_l ( mpi X,
size_t  count 
)

Left-shift: X <<= count.

Parameters:
X MPI to shift
count Amount to shift
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed

Definition at line 569 of file bignum.c.

References biL, BITS_TO_LIMBS, MPI_CHK, mpi_grow(), mpi_msb(), mpi::n, and mpi::p.

Referenced by mpi_div_mpi(), mpi_exp_mod(), mpi_gcd(), mpi_gen_prime(), and test_suite_mpi_shift_l().

int mpi_shift_r ( mpi X,
size_t  count 
)

Right-shift: X >>= count.

Parameters:
X MPI to shift
count Amount to shift
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed

Definition at line 619 of file bignum.c.

References biL, mpi_lset(), mpi::n, and mpi::p.

Referenced by dhm_make_params(), dhm_make_public(), dhm_update_blinding(), ecp_gen_keypair(), ecp_mod_p521(), ecp_randomize_coordinates(), ecp_w_naf_fixed(), mpi_div_mpi(), mpi_gcd(), mpi_gen_prime(), mpi_inv_mod(), mpi_is_prime(), and test_suite_mpi_shift_r().

size_t mpi_size ( const mpi X  ) 
int mpi_sub_abs ( mpi X,
const mpi A,
const mpi B 
)

Unsigned subtraction: X = |A| - |B|.

Parameters:
X Destination MPI
A Left-hand MPI
B Right-hand MPI
Returns:
0 if successful, POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A

Definition at line 814 of file bignum.c.

References MPI_CHK, mpi_cmp_abs(), mpi_copy(), mpi_free(), mpi_init(), mpi_sub_hlp(), mpi::n, mpi::p, POLARSSL_ERR_MPI_NEGATIVE_VALUE, and mpi::s.

Referenced by ecp_modp(), fix_negative(), mpi_add_mpi(), mpi_gcd(), mpi_sub_mpi(), and test_suite_mpi_sub_abs().

static void mpi_sub_hlp ( size_t  n,
t_uint s,
t_uint d 
) [static]

Definition at line 793 of file bignum.c.

Referenced by mpi_montmul(), and mpi_sub_abs().

int mpi_sub_int ( mpi X,
const mpi A,
t_sint  b 
)

Signed subtraction: X = A - b.

Parameters:
X Destination MPI
A Left-hand MPI
b The integer value to subtract
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed

Definition at line 935 of file bignum.c.

References mpi_sub_mpi(), mpi::n, mpi::p, and mpi::s.

Referenced by dhm_check_range(), mpi_gen_prime(), mpi_is_prime(), mpi_read_string(), rsa_check_privkey(), rsa_gen_key(), test_suite_mpi_sub_int(), test_suite_pk_rsa_decrypt_test_vec(), test_suite_pkcs1_rsaes_oaep_decrypt(), test_suite_pkcs1_rsassa_pss_sign(), test_suite_rsa_pkcs1_decrypt(), test_suite_rsa_pkcs1_sign(), test_suite_rsa_pkcs1_sign_raw(), and test_suite_rsa_private().

int mpi_sub_mpi ( mpi X,
const mpi A,
const mpi B 
)

Signed subtraction: X = A - B.

Parameters:
X Destination MPI
A Left-hand MPI
B Right-hand MPI
Returns:
0 if successful, POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed

Definition at line 888 of file bignum.c.

References mpi_add_abs(), MPI_CHK, mpi_cmp_abs(), mpi_sub_abs(), and mpi::s.

Referenced by ecp_add_mixed(), ecp_double_jac(), mpi_div_mpi(), mpi_inv_mod(), mpi_mod_mpi(), mpi_sub_int(), rsa_private(), and test_suite_mpi_sub_mpi().

void mpi_swap ( mpi X,
mpi Y 
)

Swap the contents of X and Y.

Parameters:
X First MPI value
Y Second MPI value

Definition at line 159 of file bignum.c.

Referenced by rsa_gen_key(), and test_suite_mpi_swap().

int mpi_write_binary ( const mpi X,
unsigned char *  buf,
size_t  buflen 
)

Export X into unsigned binary data, big endian.

Parameters:
X Source MPI
buf Output buffer
buflen Output buffer size
Returns:
0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough

Definition at line 549 of file bignum.c.

References ciL, mpi_size(), mpi::p, and POLARSSL_ERR_MPI_BUFFER_TOO_SMALL.

Referenced by asn1_write_mpi(), dhm_calc_secret(), dhm_make_public(), ecdh_calc_secret(), ecp_point_write_binary(), rsa_private(), rsa_public(), test_suite_mpi_read_file(), and test_suite_mpi_write_binary().

int mpi_write_file ( const char *  p,
const mpi X,
int  radix,
FILE *  fout 
)
static int mpi_write_hlp ( mpi X,
int  radix,
char **  p 
) [static]

Definition at line 354 of file bignum.c.

References MPI_CHK, mpi_cmp_int(), mpi_div_int(), mpi_mod_int(), and POLARSSL_ERR_MPI_BAD_INPUT_DATA.

Referenced by mpi_write_string().

int mpi_write_string ( const mpi X,
int  radix,
char *  s,
size_t *  slen 
)

Export into an ASCII string.

Parameters:
X Source MPI
radix Output numeric base
s String buffer
slen String buffer size
Returns:
0 if successful, or a POLARSSL_ERR_MPI_XXX error code. *slen is always updated to reflect the amount of data that has (or would have) been written.
Note:
Call this function with *slen = 0 to obtain the minimum required buffer size in *slen.

Definition at line 381 of file bignum.c.

References ciL, MPI_CHK, mpi_copy(), mpi_free(), mpi_init(), mpi_msb(), mpi_write_hlp(), mpi::n, mpi::p, POLARSSL_ERR_MPI_BAD_INPUT_DATA, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL, and mpi::s.

Referenced by mpi_write_file(), test_suite_mpi_read_binary(), and test_suite_mpi_read_write_string().


Variable Documentation

const int gcd_pairs[GCD_PAIR_COUNT][3] [static]
Initial value:
{
    { 693, 609, 21 },
    { 1764, 868, 28 },
    { 768454923, 542167814, 1 }
}

Definition at line 1991 of file bignum.c.

const int small_prime[] [static]
Initial value:
{
        3,    5,    7,   11,   13,   17,   19,   23,
       29,   31,   37,   41,   43,   47,   53,   59,
       61,   67,   71,   73,   79,   83,   89,   97,
      101,  103,  107,  109,  113,  127,  131,  137,
      139,  149,  151,  157,  163,  167,  173,  179,
      181,  191,  193,  197,  199,  211,  223,  227,
      229,  233,  239,  241,  251,  257,  263,  269,
      271,  277,  281,  283,  293,  307,  311,  313,
      317,  331,  337,  347,  349,  353,  359,  367,
      373,  379,  383,  389,  397,  401,  409,  419,
      421,  431,  433,  439,  443,  449,  457,  461,
      463,  467,  479,  487,  491,  499,  503,  509,
      521,  523,  541,  547,  557,  563,  569,  571,
      577,  587,  593,  599,  601,  607,  613,  617,
      619,  631,  641,  643,  647,  653,  659,  661,
      673,  677,  683,  691,  701,  709,  719,  727,
      733,  739,  743,  751,  757,  761,  769,  773,
      787,  797,  809,  811,  821,  823,  827,  829,
      839,  853,  857,  859,  863,  877,  881,  883,
      887,  907,  911,  919,  929,  937,  941,  947,
      953,  967,  971,  977,  983,  991,  997, -103
}

Definition at line 1774 of file bignum.c.


Generated on 9 Apr 2014 for PolarSSL v1.3.2 by  doxygen 1.6.1