bignum.h File Reference

Multi-precision integer library. More...

#include <stdio.h>
#include <string.h>
#include "config.h"
#include <inttypes.h>
Include dependency graph for bignum.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  mpi
 MPI structure. More...

Defines

#define POLARSSL_ERR_MPI_FILE_IO_ERROR   -0x0002
 An error occurred while reading from or writing to a file.
#define POLARSSL_ERR_MPI_BAD_INPUT_DATA   -0x0004
 Bad input parameters to function.
#define POLARSSL_ERR_MPI_INVALID_CHARACTER   -0x0006
 There is an invalid character in the digit string.
#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL   -0x0008
 The buffer is too small to write to.
#define POLARSSL_ERR_MPI_NEGATIVE_VALUE   -0x000A
 The input arguments are negative or result in illegal output.
#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO   -0x000C
 The input argument for division is zero, which is not allowed.
#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE   -0x000E
 The input arguments are not acceptable.
#define POLARSSL_ERR_MPI_MALLOC_FAILED   -0x0010
 Memory allocation failed.
#define MPI_CHK(f)   if( ( ret = f ) != 0 ) goto cleanup
#define POLARSSL_MPI_MAX_LIMBS   10000
#define POLARSSL_MPI_WINDOW_SIZE   6
 Maximum windows size used.
#define POLARSSL_MPI_MAX_SIZE   512
 Maximum number of bytes for usable MPIs.
#define POLARSSL_MPI_MAX_BITS   ( 8 * POLARSSL_MPI_MAX_SIZE )
 Maximum number of bits for usable MPIs.
#define POLARSSL_MPI_MAX_BITS_SCALE100   ( 100 * POLARSSL_MPI_MAX_BITS )
#define LN_2_DIV_LN_10_SCALE100   332
#define POLARSSL_MPI_RW_BUFFER_SIZE   ( ((POLARSSL_MPI_MAX_BITS_SCALE100 + LN_2_DIV_LN_10_SCALE100 - 1) / LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
#define POLARSSL_HAVE_INT32

Typedefs

typedef int32_t t_sint
typedef uint32_t t_uint

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.
int mpi_read_string (mpi *X, int radix, const char *s)
 Import from an ASCII string.
int mpi_write_string (const mpi *X, int radix, char *s, size_t *slen)
 Export into an ASCII string.
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|.
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.
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.
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_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_gcd (mpi *G, const mpi *A, const mpi *B)
 Greatest common divisor: G = gcd(A, B).
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.

Detailed Description

Multi-precision integer library.

Copyright (C) 2006-2013, Brainspark B.V.

This file is part of PolarSSL (http://www.polarssl.org) Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>

All rights reserved.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Definition in file bignum.h.


Define Documentation

#define LN_2_DIV_LN_10_SCALE100   332

Definition at line 112 of file bignum.h.

#define MPI_CHK (  )     if( ( ret = f ) != 0 ) goto cleanup
#define POLARSSL_ERR_MPI_BAD_INPUT_DATA   -0x0004

Bad input parameters to function.

Definition at line 53 of file bignum.h.

Referenced by mpi_exp_mod(), mpi_gen_prime(), mpi_inv_mod(), mpi_read_string(), mpi_set_bit(), mpi_write_hlp(), mpi_write_string(), polarssl_strerror(), and verify_int().

#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL   -0x0008

The buffer is too small to write to.

Definition at line 55 of file bignum.h.

Referenced by mpi_read_file(), mpi_write_binary(), mpi_write_string(), polarssl_strerror(), and verify_int().

#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO   -0x000C

The input argument for division is zero, which is not allowed.

Definition at line 57 of file bignum.h.

Referenced by mpi_div_mpi(), mpi_mod_int(), polarssl_strerror(), and verify_int().

#define POLARSSL_ERR_MPI_FILE_IO_ERROR   -0x0002

An error occurred while reading from or writing to a file.

Definition at line 52 of file bignum.h.

Referenced by mpi_read_file(), mpi_write_file(), polarssl_strerror(), and verify_int().

#define POLARSSL_ERR_MPI_INVALID_CHARACTER   -0x0006

There is an invalid character in the digit string.

Definition at line 54 of file bignum.h.

Referenced by mpi_get_digit(), polarssl_strerror(), and verify_int().

#define POLARSSL_ERR_MPI_MALLOC_FAILED   -0x0010

Memory allocation failed.

Definition at line 59 of file bignum.h.

Referenced by mpi_grow(), and polarssl_strerror().

#define POLARSSL_ERR_MPI_NEGATIVE_VALUE   -0x000A

The input arguments are negative or result in illegal output.

Definition at line 56 of file bignum.h.

Referenced by mpi_mod_int(), mpi_mod_mpi(), mpi_sub_abs(), polarssl_strerror(), and verify_int().

#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE   -0x000E

The input arguments are not acceptable.

Definition at line 58 of file bignum.h.

Referenced by dhm_update_blinding(), mpi_gen_prime(), mpi_inv_mod(), mpi_is_prime(), polarssl_strerror(), and verify_int().

#define POLARSSL_HAVE_INT32

Definition at line 147 of file bignum.h.

#define POLARSSL_MPI_MAX_BITS   ( 8 * POLARSSL_MPI_MAX_SIZE )

Maximum number of bits for usable MPIs.

Definition at line 91 of file bignum.h.

Referenced by mpi_gen_prime(), and rsa_check_pubkey().

#define POLARSSL_MPI_MAX_BITS_SCALE100   ( 100 * POLARSSL_MPI_MAX_BITS )

Definition at line 111 of file bignum.h.

#define POLARSSL_MPI_MAX_LIMBS   10000

Definition at line 66 of file bignum.h.

Referenced by mpi_grow().

#define POLARSSL_MPI_MAX_SIZE   512
#define POLARSSL_MPI_RW_BUFFER_SIZE   ( ((POLARSSL_MPI_MAX_BITS_SCALE100 + LN_2_DIV_LN_10_SCALE100 - 1) / LN_2_DIV_LN_10_SCALE100) + 10 + 6 )

Definition at line 113 of file bignum.h.

Referenced by mpi_read_file(), and mpi_write_file().

#define POLARSSL_MPI_WINDOW_SIZE   6

Maximum windows size used.

Definition at line 78 of file bignum.h.

Referenced by mpi_exp_mod().


Typedef Documentation

typedef int32_t t_sint

Definition at line 148 of file bignum.h.

typedef uint32_t t_uint

Definition at line 149 of file bignum.h.


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().

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().

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().

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_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().

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_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().


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