PolarSSL v1.1.4
|
Multi-precision integer library. More...
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_READ_BUFFER_SIZE 1250 |
Typedefs | |
typedef signed long | t_sint |
typedef unsigned long | 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 (mpi *X, size_t pos) |
int | mpi_set_bit (mpi *X, size_t pos, unsigned char val) |
size_t | mpi_lsb (const mpi *X) |
Return the number of least significant bits. | |
size_t | mpi_msb (const mpi *X) |
Return the number of most significant bits. | |
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_file (mpi *X, int radix, FILE *fin) |
Read X from an opened file. | |
int | mpi_write_file (const char *p, const mpi *X, int radix, FILE *fout) |
Write X into an opened file, or stdout if fout is NULL. | |
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 substraction: 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 substraction: 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 substraction: 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. |
Multi-precision integer library.
Copyright (C) 2006-2010, 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 POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 |
#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 |
#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C |
#define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 |
#define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 |
#define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010 |
#define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A |
#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E |
#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) |
#define POLARSSL_MPI_MAX_LIMBS 10000 |
#define POLARSSL_MPI_MAX_SIZE 512 |
#define POLARSSL_MPI_READ_BUFFER_SIZE 1250 |
#define POLARSSL_MPI_WINDOW_SIZE 6 |
int mpi_add_abs | ( | mpi * | X, |
const mpi * | A, | ||
const mpi * | B | ||
) |
Unsigned addition: X = |A| + |B|.
X | Destination MPI |
A | Left-hand MPI |
B | Right-hand MPI |
Referenced by FCT_BGN().
int mpi_add_int | ( | mpi * | X, |
const mpi * | A, | ||
t_sint | b | ||
) |
Signed addition: X = A + b.
X | Destination MPI |
A | Left-hand MPI |
b | The integer value to add |
Referenced by FCT_BGN().
int mpi_add_mpi | ( | mpi * | X, |
const mpi * | A, | ||
const mpi * | B | ||
) |
Signed addition: X = A + B.
X | Destination MPI |
A | Left-hand MPI |
B | Right-hand MPI |
Referenced by FCT_BGN().
int mpi_cmp_abs | ( | const mpi * | X, |
const mpi * | Y | ||
) |
Compare unsigned values.
X | Left-hand MPI |
Y | Right-hand MPI |
Referenced by FCT_BGN().
int mpi_cmp_int | ( | const mpi * | X, |
t_sint | z | ||
) |
Compare signed values.
X | Left-hand MPI |
z | The integer value to compare to |
Referenced by FCT_BGN().
int mpi_cmp_mpi | ( | const mpi * | X, |
const mpi * | Y | ||
) |
Compare signed values.
X | Left-hand MPI |
Y | Right-hand MPI |
Referenced by FCT_BGN().
Copy the contents of Y into X.
X | Destination MPI |
Y | Source MPI |
Referenced by FCT_BGN().
int mpi_div_int | ( | mpi * | Q, |
mpi * | R, | ||
const mpi * | A, | ||
t_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 |
Referenced by FCT_BGN().
int mpi_div_mpi | ( | mpi * | Q, |
mpi * | R, | ||
const mpi * | A, | ||
const mpi * | B | ||
) |
Division by 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 |
Referenced by FCT_BGN().
Sliding-window exponentiation: X = A^E mod N.
X | Destination MPI |
A | Left-hand MPI |
E | Exponent MPI |
N | Modular MPI |
_RR | Speed-up MPI used for recalculations |
Referenced by FCT_BGN().
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.
X | Destination MPI |
size | Size in bytes |
f_rng | RNG function |
p_rng | RNG parameter |
Greatest common divisor: G = gcd(A, B)
G | Destination MPI |
A | Left-hand MPI |
B | Right-hand MPI |
Referenced by FCT_BGN().
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.
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 |
int mpi_get_bit | ( | mpi * | X, |
size_t | pos | ||
) |
Referenced by FCT_BGN().
Enlarge to the specified number of limbs.
X | MPI to grow |
nblimbs | The target number of limbs |
int mpi_inv_mod | ( | mpi * | X, |
const mpi * | A, | ||
const mpi * | N | ||
) |
Modular inverse: X = A^-1 mod N.
X | Destination MPI |
A | Left-hand MPI |
N | Right-hand MPI |
Referenced by FCT_BGN().
int mpi_is_prime | ( | 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 |
Referenced by FCT_BGN().
Set value from integer.
X | MPI to set |
z | Value to use |
Referenced by FCT_BGN().
int mpi_mod_int | ( | t_uint * | r, |
const mpi * | A, | ||
t_sint | b | ||
) |
Modulo: r = A mod b.
r | Destination t_uint |
A | Left-hand MPI |
b | Integer to divide by |
Referenced by FCT_BGN().
int mpi_mod_mpi | ( | mpi * | R, |
const mpi * | A, | ||
const mpi * | B | ||
) |
Modulo: R = A mod B.
R | Destination MPI for the rest value |
A | Left-hand MPI |
B | Right-hand MPI |
Referenced by FCT_BGN().
Return the number of most significant bits.
X | MPI to use |
Referenced by d2i_RSA_PUBKEY(), and FCT_BGN().
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.
X | Destination MPI |
A | Left-hand MPI |
b | The integer value to multiply with |
Referenced by FCT_BGN().
int mpi_mul_mpi | ( | mpi * | X, |
const mpi * | A, | ||
const mpi * | B | ||
) |
Baseline multiplication: X = A * B.
X | Destination MPI |
A | Left-hand MPI |
B | Right-hand MPI |
Referenced by FCT_BGN().
int mpi_read_binary | ( | 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 |
Referenced by d2i_RSA_PUBKEY(), and FCT_BGN().
int mpi_read_file | ( | mpi * | X, |
int | radix, | ||
FILE * | fin | ||
) |
Read X from an opened file.
X | Destination MPI |
radix | Input numeric base |
fin | Input file handle |
Referenced by FCT_BGN().
int mpi_read_string | ( | mpi * | X, |
int | radix, | ||
const char * | s | ||
) |
Import from an ASCII string.
X | Destination MPI |
radix | Input numeric base |
s | Null-terminated string buffer |
Referenced by FCT_BGN().
int mpi_self_test | ( | int | verbose | ) |
int mpi_set_bit | ( | mpi * | X, |
size_t | pos, | ||
unsigned char | val | ||
) |
Referenced by FCT_BGN().
int mpi_shift_l | ( | mpi * | X, |
size_t | count | ||
) |
Left-shift: X <<= count.
X | MPI to shift |
count | Amount to shift |
Referenced by FCT_BGN().
int mpi_shift_r | ( | mpi * | X, |
size_t | count | ||
) |
Right-shift: X >>= count.
X | MPI to shift |
count | Amount to shift |
Referenced by FCT_BGN().
int mpi_sub_abs | ( | mpi * | X, |
const mpi * | A, | ||
const mpi * | B | ||
) |
Unsigned substraction: X = |A| - |B|.
X | Destination MPI |
A | Left-hand MPI |
B | Right-hand MPI |
Referenced by FCT_BGN().
int mpi_sub_int | ( | mpi * | X, |
const mpi * | A, | ||
t_sint | b | ||
) |
Signed substraction: X = A - b.
X | Destination MPI |
A | Left-hand MPI |
b | The integer value to subtract |
Referenced by FCT_BGN().
int mpi_sub_mpi | ( | mpi * | X, |
const mpi * | A, | ||
const mpi * | B | ||
) |
Signed substraction: X = A - B.
X | Destination MPI |
A | Left-hand MPI |
B | Right-hand MPI |
Referenced by FCT_BGN().
Swap the contents of X and Y.
X | First MPI value |
Y | Second MPI value |
Referenced by FCT_BGN().
int mpi_write_binary | ( | const mpi * | X, |
unsigned char * | buf, | ||
size_t | buflen | ||
) |
Export X into unsigned binary data, big endian.
X | Source MPI |
buf | Output buffer |
buflen | Output buffer size |
Referenced by FCT_BGN().
int mpi_write_file | ( | const char * | p, |
const 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) |
Referenced by FCT_BGN().
int mpi_write_string | ( | const mpi * | X, |
int | radix, | ||
char * | s, | ||
size_t * | slen | ||
) |
Export into an ASCII string.
X | Source MPI |
radix | Output numeric base |
s | String buffer |
slen | String buffer size |
Referenced by FCT_BGN().