Microchip® Advanced Software Framework

bignum.h File Reference

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.

#include <stdio.h>

Data Structures

struct  mpi
 MPI structure. More...
 

Macros

#define MPI_CHK(f)   if( ( ret = f ) != 0 ) goto cleanup
 
#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_NEGATIVE_VALUE   0x000A
 
#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE   0x000E
 

Typedefs

typedef unsigned long t_int
 

Functions

int mpi_add_abs (mpi *X, const mpi *A, const mpi *B)
 Unsigned addition: X = |A| + |B|. More...
 
int mpi_add_int (mpi *X, const mpi *A, int b)
 Signed addition: X = A + b. More...
 
int mpi_add_mpi (mpi *X, const mpi *A, const mpi *B)
 Signed addition: X = A + B. More...
 
int mpi_cmp_abs (const mpi *X, const mpi *Y)
 Compare unsigned values. More...
 
int mpi_cmp_int (const mpi *X, int z)
 Compare signed values. More...
 
int mpi_cmp_mpi (const mpi *X, const mpi *Y)
 Compare signed values. More...
 
int mpi_copy (mpi *X, const mpi *Y)
 Copy the contents of Y into X. More...
 
int mpi_div_int (mpi *Q, mpi *R, const mpi *A, int b)
 Division by int: A = Q * b + R. More...
 
int mpi_div_mpi (mpi *Q, mpi *R, const mpi *A, const mpi *B)
 Division by mpi: A = Q * B + R. More...
 
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. More...
 
void mpi_free (mpi *X,...)
 Unallocate one or more mpi. More...
 
int mpi_gcd (mpi *G, const mpi *A, const mpi *B)
 Greatest common divisor: G = gcd(A, B) More...
 
int mpi_gen_prime (mpi *X, int nbits, int dh_flag, int(*f_rng)(void *), void *p_rng)
 Prime number generation. More...
 
int mpi_grow (mpi *X, int nblimbs)
 Enlarge to the specified number of limbs. More...
 
void mpi_init (mpi *X,...)
 Initialize one or more mpi. More...
 
int mpi_inv_mod (mpi *X, const mpi *A, const mpi *N)
 Modular inverse: X = A^-1 mod N. More...
 
int mpi_is_prime (mpi *X, int(*f_rng)(void *), void *p_rng)
 Miller-Rabin primality test. More...
 
int mpi_lsb (const mpi *X)
 Return the number of least significant bits. More...
 
int mpi_lset (mpi *X, int z)
 Set value from integer. More...
 
int mpi_mod_int (t_int *r, const mpi *A, int b)
 Modulo: r = A mod b. More...
 
int mpi_mod_mpi (mpi *R, const mpi *A, const mpi *B)
 Modulo: R = A mod B. More...
 
int mpi_msb (const mpi *X)
 Return the number of most significant bits. More...
 
int mpi_mul_int (mpi *X, const mpi *A, t_int b)
 Baseline multiplication: X = A * b Note: b is an unsigned integer type, thus Negative values of b are ignored. More...
 
int mpi_mul_mpi (mpi *X, const mpi *A, const mpi *B)
 Baseline multiplication: X = A * B. More...
 
int mpi_read_binary (mpi *X, const unsigned char *buf, int buflen)
 Import X from unsigned binary data, big endian. More...
 
int mpi_read_file (mpi *X, int radix, FILE *fin)
 Read X from an opened file. More...
 
int mpi_read_string (mpi *X, int radix, const char *s)
 Import from an ASCII string. More...
 
int mpi_self_test (int verbose)
 Checkup routine. More...
 
int mpi_shift_l (mpi *X, int count)
 Left-shift: X <<= count. More...
 
int mpi_shift_r (mpi *X, int count)
 Right-shift: X >>= count. More...
 
int mpi_size (const mpi *X)
 Return the total size in bytes. More...
 
int mpi_sub_abs (mpi *X, const mpi *A, const mpi *B)
 Unsigned substraction: X = |A| - |B|. More...
 
int mpi_sub_int (mpi *X, const mpi *A, int b)
 Signed substraction: X = A - b. More...
 
int mpi_sub_mpi (mpi *X, const mpi *A, const mpi *B)
 Signed substraction: X = A - B. More...
 
void mpi_swap (mpi *X, mpi *Y)
 Swap the contents of X and Y. More...
 
int mpi_write_binary (const mpi *X, unsigned char *buf, int buflen)
 Export X into unsigned binary data, big endian. More...
 
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. More...
 
int mpi_write_string (const mpi *X, int radix, char *s, int *slen)
 Export into an ASCII string. More...
 

#define MPI_CHK (   f)    if( ( ret = f ) != 0 ) goto cleanup
#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_NEGATIVE_VALUE   0x000A
#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE   0x000E

typedef unsigned long t_int

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

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

Parameters
XDestination MPI
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, 1 if memory allocation failed
int mpi_add_int ( mpi X,
const mpi A,
int  b 
)

Signed addition: X = A + b.

Parameters
XDestination MPI
ALeft-hand MPI
bThe integer value to add
Returns
0 if successful, 1 if memory allocation failed
int mpi_add_mpi ( mpi X,
const mpi A,
const mpi B 
)

Signed addition: X = A + B.

Parameters
XDestination MPI
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, 1 if memory allocation failed
int mpi_cmp_abs ( const mpi X,
const mpi Y 
)

Compare unsigned values.

Parameters
XLeft-hand MPI
YRight-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|
int mpi_cmp_int ( const mpi X,
int  z 
)

Compare signed values.

Parameters
XLeft-hand MPI
zThe integer value to compare to
Returns
1 if X is greater than z, -1 if X is lesser than z or 0 if X is equal to z
int mpi_cmp_mpi ( const mpi X,
const mpi Y 
)

Compare signed values.

Parameters
XLeft-hand MPI
YRight-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
int mpi_copy ( mpi X,
const mpi Y 
)

Copy the contents of Y into X.

Parameters
XDestination MPI
YSource MPI
Returns
0 if successful, 1 if memory allocation failed
int mpi_div_int ( mpi Q,
mpi R,
const mpi A,
int  b 
)

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

Parameters
QDestination MPI for the quotient
RDestination MPI for the rest value
ALeft-hand MPI
bInteger to divide by
Returns
0 if successful, 1 if memory allocation failed, POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
Note
Either Q or R can be NULL.
int mpi_div_mpi ( mpi Q,
mpi R,
const mpi A,
const mpi B 
)

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

Parameters
QDestination MPI for the quotient
RDestination MPI for the rest value
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, 1 if memory allocation failed, POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
Note
Either Q or R can be NULL.
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
XDestination MPI
ALeft-hand MPI
EExponent MPI
NModular MPI
_RRSpeed-up MPI used for recalculations
Returns
0 if successful, 1 if memory allocation failed, POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
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.
void mpi_free ( mpi X,
  ... 
)

Unallocate one or more mpi.

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

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

Parameters
GDestination MPI
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, 1 if memory allocation failed
int mpi_gen_prime ( mpi X,
int  nbits,
int  dh_flag,
int(*)(void *)  f_rng,
void *  p_rng 
)

Prime number generation.

Parameters
XDestination MPI
nbitsRequired size of X in bits
dh_flagIf 1, then (X-1)/2 will be prime too
f_rngRNG function
p_rngRNG parameter
Returns
0 if successful (probably prime), 1 if memory allocation failed, POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
int mpi_grow ( mpi X,
int  nblimbs 
)

Enlarge to the specified number of limbs.

Parameters
XMPI to grow
nblimbsThe target number of limbs
Returns
0 if successful, 1 if memory allocation failed
void mpi_init ( mpi X,
  ... 
)

Initialize one or more mpi.

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

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

Parameters
XDestination MPI
ALeft-hand MPI
NRight-hand MPI
Returns
0 if successful, 1 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
int mpi_is_prime ( mpi X,
int(*)(void *)  f_rng,
void *  p_rng 
)

Miller-Rabin primality test.

Parameters
XMPI to check
f_rngRNG function
p_rngRNG parameter
Returns
0 if successful (probably prime), 1 if memory allocation failed, POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
int mpi_lsb ( const mpi X)

Return the number of least significant bits.

Parameters
XMPI to use
int mpi_lset ( mpi X,
int  z 
)

Set value from integer.

Parameters
XMPI to set
zValue to use
Returns
0 if successful, 1 if memory allocation failed
int mpi_mod_int ( t_int r,
const mpi A,
int  b 
)

Modulo: r = A mod b.

Parameters
rDestination t_int
ALeft-hand MPI
bInteger to divide by
Returns
0 if successful, 1 if memory allocation failed, POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0, POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
int mpi_mod_mpi ( mpi R,
const mpi A,
const mpi B 
)

Modulo: R = A mod B.

Parameters
RDestination MPI for the rest value
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, 1 if memory allocation failed, POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0, POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
int mpi_msb ( const mpi X)

Return the number of most significant bits.

Parameters
XMPI to use

Referenced by d2i_RSA_PUBKEY().

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

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

Parameters
XDestination MPI
ALeft-hand MPI
bThe integer value to multiply with
Returns
0 if successful, 1 if memory allocation failed
int mpi_mul_mpi ( mpi X,
const mpi A,
const mpi B 
)

Baseline multiplication: X = A * B.

Parameters
XDestination MPI
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, 1 if memory allocation failed
int mpi_read_binary ( mpi X,
const unsigned char *  buf,
int  buflen 
)

Import X from unsigned binary data, big endian.

Parameters
XDestination MPI
bufInput buffer
buflenInput buffer size
Returns
0 if successful, 1 if memory allocation failed

Referenced by d2i_RSA_PUBKEY().

int mpi_read_file ( mpi X,
int  radix,
FILE *  fin 
)

Read X from an opened file.

Parameters
XDestination MPI
radixInput numeric base
finInput file handle
Returns
0 if successful, or an POLARSSL_ERR_MPI_XXX error code
int mpi_read_string ( mpi X,
int  radix,
const char *  s 
)

Import from an ASCII string.

Parameters
XDestination MPI
radixInput numeric base
sNull-terminated string buffer
Returns
0 if successful, or an POLARSSL_ERR_MPI_XXX error code
int mpi_self_test ( int  verbose)

Checkup routine.

Returns
0 if successful, or 1 if the test failed

Referenced by main().

int mpi_shift_l ( mpi X,
int  count 
)

Left-shift: X <<= count.

Parameters
XMPI to shift
countAmount to shift
Returns
0 if successful, 1 if memory allocation failed
int mpi_shift_r ( mpi X,
int  count 
)

Right-shift: X >>= count.

Parameters
XMPI to shift
countAmount to shift
Returns
0 if successful, 1 if memory allocation failed
int mpi_size ( const mpi X)

Return the total size in bytes.

Parameters
XMPI to use
int mpi_sub_abs ( mpi X,
const mpi A,
const mpi B 
)

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

Parameters
XDestination MPI
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
int mpi_sub_int ( mpi X,
const mpi A,
int  b 
)

Signed substraction: X = A - b.

Parameters
XDestination MPI
ALeft-hand MPI
bThe integer value to subtract
Returns
0 if successful, 1 if memory allocation failed
int mpi_sub_mpi ( mpi X,
const mpi A,
const mpi B 
)

Signed substraction: X = A - B.

Parameters
XDestination MPI
ALeft-hand MPI
BRight-hand MPI
Returns
0 if successful, 1 if memory allocation failed
void mpi_swap ( mpi X,
mpi Y 
)

Swap the contents of X and Y.

Parameters
XFirst MPI value
YSecond MPI value
int mpi_write_binary ( const mpi X,
unsigned char *  buf,
int  buflen 
)

Export X into unsigned binary data, big endian.

Parameters
XSource MPI
bufOutput buffer
buflenOutput buffer size
Returns
0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
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.

Parameters
pPrefix, can be NULL
XSource MPI
radixOutput numeric base
foutOutput file handle (can be NULL)
Returns
0 if successful, or an POLARSSL_ERR_MPI_XXX error code
Note
Set fout == NULL to print X on the console.
int mpi_write_string ( const mpi X,
int  radix,
char *  s,
int *  slen 
)

Export into an ASCII string.

Parameters
XSource MPI
radixOutput numeric base
sString buffer
slenString buffer size
Returns
0 if successful, or an 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.