#include "xyssl/config.h"
#include "xyssl/bignum.h"
#include "xyssl/bn_mul.h"
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
Go to the source code of this file.
Defines | |
#define | ciL ((int) sizeof(t_int)) |
#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) |
Functions | |
void | mpi_init (mpi *X,...) |
Initialize one or more mpi. | |
void | mpi_free (mpi *X,...) |
Unallocate one or more mpi. | |
int | mpi_grow (mpi *X, int nblimbs) |
Enlarge to the specified number of limbs. | |
int | mpi_copy (mpi *X, 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, int z) |
Set value from integer. | |
int | mpi_lsb (mpi *X) |
Return the number of least significant bits. | |
int | mpi_msb (mpi *X) |
Return the number of most significant bits. | |
int | mpi_size (mpi *X) |
Return the total size in bytes. | |
static int | mpi_get_digit (t_int *d, int radix, char c) |
int | mpi_read_string (mpi *X, int radix, char *s) |
Import from an ASCII string. | |
static int | mpi_write_hlp (mpi *X, int radix, char **p) |
int | mpi_write_string (mpi *X, int radix, char *s, int *slen) |
Export into an ASCII string. | |
int | mpi_read_file (mpi *X, int radix, FILE *fin) |
Read X from an opened file. | |
int | mpi_read_mystring (mpi *X, int radix, char *s) |
int | mpi_write_file (char *p, mpi *X, int radix, FILE *fout) |
Write X into an opened file, or stdout. | |
int | mpi_read_binary (mpi *X, unsigned char *buf, int buflen) |
Import X from unsigned binary data, big endian. | |
int | mpi_write_binary (mpi *X, unsigned char *buf, int buflen) |
Export X into unsigned binary data, big endian. | |
int | mpi_shift_l (mpi *X, int count) |
Left-shift: X <<= count. | |
int | mpi_shift_r (mpi *X, int count) |
Right-shift: X >>= count. | |
int | mpi_cmp_abs (mpi *X, mpi *Y) |
Compare unsigned values. | |
int | mpi_cmp_mpi (mpi *X, mpi *Y) |
Compare signed values. | |
int | mpi_cmp_int (mpi *X, int z) |
Compare signed values. | |
int | mpi_add_abs (mpi *X, mpi *A, mpi *B) |
Unsigned addition: X = |A| + |B|. | |
static void | mpi_sub_hlp (int n, t_int *s, t_int *d) |
int | mpi_sub_abs (mpi *X, mpi *A, mpi *B) |
Unsigned substraction: X = |A| - |B|. | |
int | mpi_add_mpi (mpi *X, mpi *A, mpi *B) |
Signed addition: X = A + B. | |
int | mpi_sub_mpi (mpi *X, mpi *A, mpi *B) |
Signed substraction: X = A - B. | |
int | mpi_add_int (mpi *X, mpi *A, int b) |
Signed addition: X = A + b. | |
int | mpi_sub_int (mpi *X, mpi *A, int b) |
Signed substraction: X = A - b. | |
static void | mpi_mul_hlp (int i, t_int *s, t_int *d, t_int b) |
int | mpi_mul_mpi (mpi *X, mpi *A, mpi *B) |
Baseline multiplication: X = A * B. | |
int | mpi_mul_int (mpi *X, mpi *A, t_int b) |
Baseline multiplication: X = A * b. | |
int | mpi_div_mpi (mpi *Q, mpi *R, mpi *A, mpi *B) |
Division by mpi: A = Q * B + R. | |
int | mpi_div_int (mpi *Q, mpi *R, mpi *A, int b) |
Division by int: A = Q * b + R. | |
int | mpi_mod_mpi (mpi *R, mpi *A, mpi *B) |
Modulo: R = A mod B. | |
int | mpi_mod_int (t_int *r, mpi *A, int b) |
Modulo: r = A mod b. | |
static void | mpi_montg_init (t_int *mm, mpi *N) |
static void | mpi_montmul (mpi *A, mpi *B, mpi *N, t_int mm, mpi *T) |
static void | mpi_montred (mpi *A, mpi *N, t_int mm, mpi *T) |
int | mpi_exp_mod (mpi *X, mpi *A, mpi *E, mpi *N, mpi *_RR) |
Sliding-window exponentiation: X = A^E mod N. | |
int | mpi_gcd (mpi *G, mpi *A, mpi *B) |
Greatest common divisor: G = gcd(A, B). | |
int | mpi_inv_mod (mpi *X, mpi *A, mpi *N) |
Modular inverse: X = A^-1 mod N. | |
int | mpi_is_prime (mpi *X, int(*f_rng)(void *), void *p_rng) |
Miller-Rabin primality test. | |
int | mpi_gen_prime (mpi *X, int nbits, int dh_flag, int(*f_rng)(void *), void *p_rng) |
Prime number generation. | |
int | mpi_self_test (int verbose) |
Checkup routine. | |
Variables | |
static const int | small_prime [] |
#define biH (ciL << 2) |
Definition at line 41 of file bignum.c.
Referenced by mpi_div_mpi(), and mpi_mod_int().
#define biL (ciL << 3) |
Definition at line 40 of file bignum.c.
Referenced by mpi_div_mpi(), mpi_exp_mod(), mpi_lsb(), mpi_montg_init(), mpi_msb(), mpi_shift_l(), and mpi_shift_r().
#define BITS_TO_LIMBS | ( | i | ) | (((i) + biL - 1) / biL) |
Definition at line 46 of file bignum.c.
Referenced by mpi_gen_prime(), mpi_read_string(), and mpi_shift_l().
#define CHARS_TO_LIMBS | ( | i | ) | (((i) + ciL - 1) / ciL) |
Definition at line 47 of file bignum.c.
Referenced by mpi_read_binary().
Definition at line 39 of file bignum.c.
Referenced by mpi_copy(), mpi_free(), mpi_gen_prime(), mpi_grow(), mpi_is_prime(), mpi_lset(), mpi_montmul(), mpi_read_binary(), mpi_read_string(), mpi_write_binary(), and mpi_write_string().
Unsigned addition: X = |A| + |B|.
Definition at line 694 of file bignum.c.
References MPI_CHK, mpi_copy(), mpi_grow(), mpi::n, and mpi::p.
Referenced by mpi_add_mpi(), and mpi_sub_mpi().
Signed addition: X = A + b.
Definition at line 860 of file bignum.c.
References mpi_add_mpi(), mpi::n, mpi::p, and mpi::s.
Referenced by mpi_gen_prime(), and mpi_read_string().
Signed addition: X = A + B.
Definition at line 798 of file bignum.c.
References mpi_add_abs(), MPI_CHK, mpi_cmp_abs(), mpi_sub_abs(), and mpi::s.
Referenced by mpi_add_int(), mpi_div_mpi(), mpi_inv_mod(), mpi_mod_mpi(), and rsa_private().
Compare unsigned values.
Definition at line 615 of file bignum.c.
References mpi::n, and mpi::p.
Referenced by mpi_add_mpi(), mpi_div_mpi(), mpi_montmul(), mpi_sub_abs(), and mpi_sub_mpi().
Compare signed values.
Definition at line 678 of file bignum.c.
References mpi_cmp_mpi(), mpi::n, mpi::p, and mpi::s.
Referenced by mpi_div_mpi(), mpi_exp_mod(), mpi_gcd(), mpi_inv_mod(), mpi_is_prime(), mpi_mod_mpi(), mpi_write_hlp(), rsa_check_privkey(), and rsa_gen_key().
Compare signed values.
Definition at line 645 of file bignum.c.
References mpi::n, mpi::p, and mpi::s.
Referenced by dhm_make_params(), dhm_make_public(), 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(), and rsa_public().
Copy the contents of Y into X.
Definition at line 128 of file bignum.c.
References ciL, MPI_CHK, mpi_grow(), mpi::n, mpi::p, and mpi::s.
Referenced by mpi_add_abs(), mpi_div_mpi(), mpi_exp_mod(), mpi_gcd(), mpi_inv_mod(), mpi_is_prime(), mpi_mul_mpi(), mpi_sub_abs(), and mpi_write_string().
Division by int: A = Q * b + R.
Definition at line 1173 of file bignum.c.
References mpi_div_mpi(), mpi::n, mpi::p, and mpi::s.
Referenced by main(), and mpi_write_hlp().
Division by mpi: A = Q * B + R.
Definition at line 1008 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, mpi::s, and XYSSL_ERR_MPI_DIVISION_BY_ZERO.
Referenced by mpi_div_int(), mpi_mod_mpi(), and mpi_self_test().
Sliding-window exponentiation: X = A^E mod N.
Definition at line 1328 of file bignum.c.
References biL, 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, and XYSSL_ERR_MPI_BAD_INPUT_DATA.
Referenced by dhm_calc_secret(), dhm_make_params(), dhm_make_public(), main(), mpi_is_prime(), mpi_self_test(), rsa_private(), and rsa_public().
void mpi_free | ( | mpi * | X, | |
... | ||||
) |
Unallocate one or more mpi.
Definition at line 73 of file bignum.c.
References ciL, mpi::n, mpi::p, and mpi::s.
Referenced by dhm_free(), main(), 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(), and rsa_public().
Greatest common divisor: G = gcd(A, B).
Definition at line 1507 of file bignum.c.
References MPI_CHK, mpi_cmp_int(), mpi_cmp_mpi(), mpi_copy(), mpi_free(), mpi_init(), mpi_lset(), mpi_mul_mpi(), mpi_shift_r(), mpi_sub_abs(), mpi::p, and mpi::s.
Referenced by mpi_inv_mod(), rsa_check_privkey(), and rsa_gen_key().
Prime number generation.
X | destination mpi | |
nbits | required size of X in bits | |
dh_flag | if 1, then (X-1)/2 will be prime too | |
f_rng | RNG function | |
p_rng | RNG parameter |
Definition at line 1778 of file bignum.c.
References BITS_TO_LIMBS, ciL, mpi_add_int(), MPI_CHK, mpi_free(), mpi_grow(), mpi_init(), mpi_is_prime(), mpi_lset(), mpi_msb(), mpi_shift_l(), mpi_shift_r(), mpi_sub_int(), mpi::p, XYSSL_ERR_MPI_BAD_INPUT_DATA, and XYSSL_ERR_MPI_NOT_ACCEPTABLE.
Referenced by main(), and rsa_gen_key().
Definition at line 226 of file bignum.c.
References XYSSL_ERR_MPI_INVALID_CHARACTER.
Referenced by mpi_read_file(), mpi_read_mystring(), and mpi_read_string().
Enlarge to the specified number of limbs.
Definition at line 100 of file bignum.c.
References ciL, mpi::n, and mpi::p.
Referenced by dhm_make_params(), dhm_make_public(), mpi_add_abs(), mpi_copy(), mpi_div_mpi(), mpi_exp_mod(), mpi_gen_prime(), mpi_is_prime(), mpi_lset(), mpi_mul_mpi(), mpi_read_binary(), mpi_read_string(), and mpi_shift_l().
void mpi_init | ( | mpi * | X, | |
... | ||||
) |
Initialize one or more mpi.
Definition at line 52 of file bignum.c.
References mpi::n, mpi::p, and mpi::s.
Referenced by main(), 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(), and rsa_public().
Modular inverse: X = A^-1 mod N.
Definition at line 1549 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, XYSSL_ERR_MPI_BAD_INPUT_DATA, and XYSSL_ERR_MPI_NOT_ACCEPTABLE.
Referenced by main(), mpi_self_test(), and rsa_gen_key().
Miller-Rabin primality test.
Definition at line 1667 of file bignum.c.
References ciL, MPI_CHK, mpi_cmp_int(), mpi_cmp_mpi(), mpi_copy(), mpi_exp_mod(), mpi_free(), mpi_grow(), 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, R, mpi::s, small_prime, and XYSSL_ERR_MPI_NOT_ACCEPTABLE.
Referenced by main(), and mpi_gen_prime().
Set value from integer.
Definition at line 167 of file bignum.c.
References ciL, MPI_CHK, mpi_grow(), mpi::n, mpi::p, and mpi::s.
Referenced by dhm_make_params(), dhm_make_public(), mpi_div_mpi(), mpi_exp_mod(), mpi_gcd(), mpi_gen_prime(), mpi_inv_mod(), mpi_mul_mpi(), mpi_read_binary(), mpi_read_string(), and rsa_gen_key().
Modulo: r = A mod b.
Definition at line 1209 of file bignum.c.
References biH, mpi::n, mpi::p, and XYSSL_ERR_MPI_DIVISION_BY_ZERO.
Referenced by mpi_is_prime(), and mpi_write_hlp().
Modulo: R = A mod B.
Definition at line 1189 of file bignum.c.
References mpi_add_mpi(), MPI_CHK, mpi_cmp_int(), mpi_cmp_mpi(), mpi_div_mpi(), and mpi_sub_mpi().
Referenced by mpi_exp_mod(), mpi_inv_mod(), mpi_is_prime(), rsa_check_privkey(), rsa_gen_key(), and rsa_private().
Definition at line 1277 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().
Definition at line 1314 of file bignum.c.
References mpi_montmul(), mpi::n, mpi::p, and mpi::s.
Referenced by mpi_exp_mod().
Return the number of most significant bits.
Definition at line 200 of file bignum.c.
References biL, mpi::n, and mpi::p.
Referenced by d2i_RSA_PUBKEY(), main(), mpi_div_mpi(), mpi_exp_mod(), mpi_gen_prime(), mpi_is_prime(), mpi_shift_l(), mpi_size(), mpi_write_string(), rsa_check_pubkey(), rsa_decryption(), rsa_encryption(), and rsa_gen_key().
Definition at line 892 of file bignum.c.
References MULADDC_CORE, MULADDC_INIT, and MULADDC_STOP.
Referenced by mpi_montmul(), and mpi_mul_mpi().
Baseline multiplication: X = A * b.
Definition at line 992 of file bignum.c.
References mpi_mul_mpi(), mpi::n, mpi::p, and mpi::s.
Referenced by mpi_div_mpi(), and mpi_read_string().
Baseline multiplication: X = A * B.
Definition at line 956 of file bignum.c.
References MPI_CHK, mpi_copy(), mpi_free(), mpi_grow(), mpi_init(), mpi_lset(), mpi_mul_hlp(), mpi::n, mpi::p, and mpi::s.
Referenced by main(), mpi_gcd(), mpi_is_prime(), mpi_mul_int(), mpi_self_test(), rsa_check_privkey(), rsa_gen_key(), and rsa_private().
Import X from unsigned binary data, big endian.
X | destination mpi | |
buf | input buffer | |
buflen | input buffer size |
Definition at line 484 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(), dhm_read_bignum(), dhm_read_public(), rsa_private(), and rsa_public().
Read X from an opened file.
X | destination mpi | |
radix | input numeric base | |
fin | input file handle |
Definition at line 391 of file bignum.c.
References mpi_get_digit(), mpi_read_string(), and XYSSL_ERR_MPI_FILE_IO_ERROR.
Referenced by main().
Definition at line 418 of file bignum.c.
References mpi_get_digit(), and mpi_read_string().
Referenced by rsa_decryption(), and rsa_encryption().
Import from an ASCII string.
X | destination mpi | |
radix | input numeric base | |
s | null-terminated string buffer |
Definition at line 243 of file bignum.c.
References BITS_TO_LIMBS, ciL, int, mpi_add_int(), MPI_CHK, mpi_free(), mpi_get_digit(), mpi_grow(), mpi_init(), mpi_lset(), mpi_mul_int(), mpi::p, mpi::s, and XYSSL_ERR_MPI_BAD_INPUT_DATA.
Referenced by main(), mpi_read_file(), mpi_read_mystring(), mpi_self_test(), rsa_self_test(), and ssl_set_dh_param().
Checkup routine.
Definition at line 1854 of file bignum.c.
References MPI_CHK, mpi_cmp_mpi(), mpi_div_mpi(), mpi_exp_mod(), mpi_free(), mpi_init(), mpi_inv_mod(), mpi_mul_mpi(), and mpi_read_string().
Referenced by main().
Left-shift: X <<= count.
Definition at line 526 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(), and mpi_gen_prime().
Right-shift: X >>= count.
Definition at line 575 of file bignum.c.
References biL, mpi::n, and mpi::p.
Referenced by dhm_make_params(), dhm_make_public(), mpi_div_mpi(), mpi_gcd(), mpi_gen_prime(), mpi_inv_mod(), and mpi_is_prime().
Return the total size in bytes.
Definition at line 218 of file bignum.c.
References mpi_msb().
Referenced by dhm_calc_secret(), dhm_make_params(), dhm_read_params(), mpi_write_binary(), x509parse_crt(), and x509parse_key().
Unsigned substraction: X = |A| - |B|.
Definition at line 761 of file bignum.c.
References MPI_CHK, mpi_cmp_abs(), mpi_copy(), mpi_free(), mpi_init(), mpi_sub_hlp(), mpi::n, mpi::p, and XYSSL_ERR_MPI_NEGATIVE_VALUE.
Referenced by mpi_add_mpi(), mpi_gcd(), and mpi_sub_mpi().
Definition at line 740 of file bignum.c.
Referenced by mpi_montmul(), and mpi_sub_abs().
Signed substraction: X = A - b.
Definition at line 876 of file bignum.c.
References mpi_sub_mpi(), mpi::n, mpi::p, and mpi::s.
Referenced by main(), mpi_gen_prime(), mpi_is_prime(), rsa_check_privkey(), and rsa_gen_key().
Signed substraction: X = A - B.
Definition at line 829 of file bignum.c.
References mpi_add_abs(), MPI_CHK, mpi_cmp_abs(), mpi_sub_abs(), and mpi::s.
Referenced by mpi_div_mpi(), mpi_inv_mod(), mpi_mod_mpi(), mpi_sub_int(), and rsa_private().
Export X into unsigned binary data, big endian.
X | source mpi | |
buf | output buffer | |
buflen | output buffer size |
Definition at line 506 of file bignum.c.
References ciL, mpi_size(), mpi::p, and XYSSL_ERR_MPI_BUFFER_TOO_SMALL.
Referenced by dhm_calc_secret(), dhm_make_public(), rsa_private(), and rsa_public().
Write X into an opened file, or stdout.
p | prefix, can be NULL | |
X | source mpi | |
radix | output numeric base | |
fout | output file handle |
Definition at line 447 of file bignum.c.
References MPI_CHK, mpi_write_string(), and XYSSL_ERR_MPI_FILE_IO_ERROR.
Referenced by generate_RSA_keys_ciphertext(), generate_RSA_keys_plaintext(), and main().
Definition at line 301 of file bignum.c.
References MPI_CHK, mpi_cmp_int(), mpi_div_int(), mpi_mod_int(), and XYSSL_ERR_MPI_BAD_INPUT_DATA.
Referenced by mpi_write_string().
Export into an ASCII string.
X | source mpi | |
radix | output numeric base | |
s | string buffer | |
slen | string buffer size |
Definition at line 328 of file bignum.c.
References ciL, MPI_CHK, mpi_copy(), mpi_free(), mpi_init(), mpi_msb(), mpi_write_hlp(), mpi::n, mpi::p, mpi::s, XYSSL_ERR_MPI_BAD_INPUT_DATA, and XYSSL_ERR_MPI_BUFFER_TOO_SMALL.
Referenced by mpi_write_file().
const int small_prime[] [static] |
{ 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 1639 of file bignum.c.
Referenced by mpi_is_prime().