2009-01-03 21:22:43 +00:00
|
|
|
/**
|
|
|
|
* \file bignum.h
|
2009-01-04 16:27:10 +00:00
|
|
|
*
|
2010-07-18 10:13:04 +00:00
|
|
|
* Copyright (C) 2006-2010, Brainspark B.V.
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
|
|
|
* This file is part of PolarSSL (http://www.polarssl.org)
|
2010-07-18 10:13:04 +00:00
|
|
|
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2009-07-28 17:23:11 +00:00
|
|
|
* All rights reserved.
|
2009-01-04 16:27:10 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2009-01-03 21:51:57 +00:00
|
|
|
#ifndef POLARSSL_BIGNUM_H
|
|
|
|
#define POLARSSL_BIGNUM_H
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2009-07-19 20:28:35 +00:00
|
|
|
#define POLARSSL_ERR_MPI_FILE_IO_ERROR 0x0002
|
|
|
|
#define POLARSSL_ERR_MPI_BAD_INPUT_DATA 0x0004
|
|
|
|
#define POLARSSL_ERR_MPI_INVALID_CHARACTER 0x0006
|
|
|
|
#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL 0x0008
|
|
|
|
#define POLARSSL_ERR_MPI_NEGATIVE_VALUE 0x000A
|
|
|
|
#define POLARSSL_ERR_MPI_DIVISION_BY_ZERO 0x000C
|
|
|
|
#define POLARSSL_ERR_MPI_NOT_ACCEPTABLE 0x000E
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
#define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Define the base integer type, architecture-wise
|
|
|
|
*/
|
2009-01-03 21:51:57 +00:00
|
|
|
#if defined(POLARSSL_HAVE_INT8)
|
2009-01-03 21:22:43 +00:00
|
|
|
typedef unsigned char t_int;
|
|
|
|
typedef unsigned short t_dbl;
|
|
|
|
#else
|
2009-01-03 21:51:57 +00:00
|
|
|
#if defined(POLARSSL_HAVE_INT16)
|
2009-01-03 21:22:43 +00:00
|
|
|
typedef unsigned short t_int;
|
|
|
|
typedef unsigned long t_dbl;
|
|
|
|
#else
|
|
|
|
typedef unsigned long t_int;
|
|
|
|
#if defined(_MSC_VER) && defined(_M_IX86)
|
|
|
|
typedef unsigned __int64 t_dbl;
|
|
|
|
#else
|
|
|
|
#if defined(__amd64__) || defined(__x86_64__) || \
|
|
|
|
defined(__ppc64__) || defined(__powerpc64__) || \
|
|
|
|
defined(__ia64__) || defined(__alpha__)
|
|
|
|
typedef unsigned int t_dbl __attribute__((mode(TI)));
|
|
|
|
#else
|
2009-07-11 16:35:32 +00:00
|
|
|
#if defined(POLARSSL_HAVE_LONGLONG)
|
|
|
|
typedef unsigned long long t_dbl;
|
|
|
|
#endif
|
2009-01-03 21:22:43 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief MPI structure
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int s; /*!< integer sign */
|
|
|
|
int n; /*!< total # of limbs */
|
|
|
|
t_int *p; /*!< pointer to limbs */
|
|
|
|
}
|
|
|
|
mpi;
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Initialize one or more mpi
|
|
|
|
*/
|
|
|
|
void mpi_init( mpi *X, ... );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Unallocate one or more mpi
|
|
|
|
*/
|
|
|
|
void mpi_free( mpi *X, ... );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Enlarge to the specified number of limbs
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X MPI to grow
|
|
|
|
* \param nblimbs The target number of limbs
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed
|
|
|
|
*/
|
|
|
|
int mpi_grow( mpi *X, int nblimbs );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Copy the contents of Y into X
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Destination MPI
|
|
|
|
* \param Y Source MPI
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed
|
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_copy( mpi *X, const mpi *Y );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Swap the contents of X and Y
|
2009-07-28 07:18:38 +00:00
|
|
|
*
|
|
|
|
* \param X First MPI value
|
|
|
|
* \param Y Second MPI value
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
|
|
|
void mpi_swap( mpi *X, mpi *Y );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Set value from integer
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X MPI to set
|
|
|
|
* \param z Value to use
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed
|
|
|
|
*/
|
|
|
|
int mpi_lset( mpi *X, int z );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Return the number of least significant bits
|
2009-07-28 07:18:38 +00:00
|
|
|
*
|
|
|
|
* \param X MPI to use
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_lsb( const mpi *X );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Return the number of most significant bits
|
2009-07-28 07:18:38 +00:00
|
|
|
*
|
|
|
|
* \param X MPI to use
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_msb( const mpi *X );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Return the total size in bytes
|
2009-07-28 07:18:38 +00:00
|
|
|
*
|
|
|
|
* \param X MPI to use
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_size( const mpi *X );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Import from an ASCII string
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Destination MPI
|
|
|
|
* \param radix Input numeric base
|
|
|
|
* \param s Null-terminated string buffer
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
2009-01-03 21:51:57 +00:00
|
|
|
* \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_read_string( mpi *X, int radix, const char *s );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Export into an ASCII string
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Source MPI
|
|
|
|
* \param radix Output numeric base
|
|
|
|
* \param s String buffer
|
|
|
|
* \param slen String buffer size
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
2010-03-16 21:09:09 +00:00
|
|
|
* \return 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.
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
|
|
|
* \note Call this function with *slen = 0 to obtain the
|
|
|
|
* minimum required buffer size in *slen.
|
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_write_string( const mpi *X, int radix, char *s, int *slen );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Read X from an opened file
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Destination MPI
|
|
|
|
* \param radix Input numeric base
|
|
|
|
* \param fin Input file handle
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
2009-01-03 21:51:57 +00:00
|
|
|
* \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
|
|
|
int mpi_read_file( mpi *X, int radix, FILE *fin );
|
|
|
|
|
|
|
|
/**
|
2009-07-28 07:18:38 +00:00
|
|
|
* \brief Write X into an opened file, or stdout if fout is NULL
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param p Prefix, can be NULL
|
|
|
|
* \param X Source MPI
|
|
|
|
* \param radix Output numeric base
|
|
|
|
* \param fout Output file handle (can be NULL)
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
2009-01-03 21:51:57 +00:00
|
|
|
* \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
|
|
|
* \note Set fout == NULL to print X on the console.
|
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Import X from unsigned binary data, big endian
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Destination MPI
|
|
|
|
* \param buf Input buffer
|
|
|
|
* \param buflen Input buffer size
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed
|
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_read_binary( mpi *X, const unsigned char *buf, int buflen );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Export X into unsigned binary data, big endian
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Source MPI
|
|
|
|
* \param buf Output buffer
|
|
|
|
* \param buflen Output buffer size
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
|
|
|
* \return 0 if successful,
|
2009-01-03 21:51:57 +00:00
|
|
|
* POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_write_binary( const mpi *X, unsigned char *buf, int buflen );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Left-shift: X <<= count
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X MPI to shift
|
|
|
|
* \param count Amount to shift
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed
|
|
|
|
*/
|
|
|
|
int mpi_shift_l( mpi *X, int count );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Right-shift: X >>= count
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X MPI to shift
|
|
|
|
* \param count Amount to shift
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed
|
|
|
|
*/
|
|
|
|
int mpi_shift_r( mpi *X, int count );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Compare unsigned values
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Left-hand MPI
|
|
|
|
* \param Y Right-hand MPI
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 1 if |X| is greater than |Y|,
|
|
|
|
* -1 if |X| is lesser than |Y| or
|
|
|
|
* 0 if |X| is equal to |Y|
|
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_cmp_abs( const mpi *X, const mpi *Y );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Compare signed values
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Left-hand MPI
|
|
|
|
* \param Y Right-hand MPI
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 1 if X is greater than Y,
|
|
|
|
* -1 if X is lesser than Y or
|
|
|
|
* 0 if X is equal to Y
|
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_cmp_mpi( const mpi *X, const mpi *Y );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Compare signed values
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Left-hand MPI
|
|
|
|
* \param z The integer value to compare to
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 1 if X is greater than z,
|
|
|
|
* -1 if X is lesser than z or
|
|
|
|
* 0 if X is equal to z
|
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_cmp_int( const mpi *X, int z );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Unsigned addition: X = |A| + |B|
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Destination MPI
|
|
|
|
* \param A Left-hand MPI
|
|
|
|
* \param B Right-hand MPI
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed
|
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_add_abs( mpi *X, const mpi *A, const mpi *B );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Unsigned substraction: X = |A| - |B|
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Destination MPI
|
|
|
|
* \param A Left-hand MPI
|
|
|
|
* \param B Right-hand MPI
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
2009-01-03 21:51:57 +00:00
|
|
|
* POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Signed addition: X = A + B
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Destination MPI
|
|
|
|
* \param A Left-hand MPI
|
|
|
|
* \param B Right-hand MPI
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed
|
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Signed substraction: X = A - B
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Destination MPI
|
|
|
|
* \param A Left-hand MPI
|
|
|
|
* \param B Right-hand MPI
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed
|
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Signed addition: X = A + b
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Destination MPI
|
|
|
|
* \param A Left-hand MPI
|
|
|
|
* \param b The integer value to add
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed
|
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_add_int( mpi *X, const mpi *A, int b );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Signed substraction: X = A - b
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Destination MPI
|
|
|
|
* \param A Left-hand MPI
|
|
|
|
* \param b The integer value to subtract
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed
|
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_sub_int( mpi *X, const mpi *A, int b );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Baseline multiplication: X = A * B
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Destination MPI
|
|
|
|
* \param A Left-hand MPI
|
|
|
|
* \param B Right-hand MPI
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed
|
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Baseline multiplication: X = A * b
|
2009-06-23 19:46:08 +00:00
|
|
|
* Note: b is an unsigned integer type, thus
|
|
|
|
* Negative values of b are ignored.
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Destination MPI
|
|
|
|
* \param A Left-hand MPI
|
|
|
|
* \param b The integer value to multiply with
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed
|
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_mul_int( mpi *X, const mpi *A, t_int b );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Division by mpi: A = Q * B + R
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param Q Destination MPI for the quotient
|
|
|
|
* \param R Destination MPI for the rest value
|
|
|
|
* \param A Left-hand MPI
|
|
|
|
* \param B Right-hand MPI
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed,
|
2009-01-03 21:51:57 +00:00
|
|
|
* POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
|
|
|
* \note Either Q or R can be NULL.
|
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Division by int: A = Q * b + R
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param Q Destination MPI for the quotient
|
|
|
|
* \param R Destination MPI for the rest value
|
|
|
|
* \param A Left-hand MPI
|
|
|
|
* \param b Integer to divide by
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed,
|
2009-01-03 21:51:57 +00:00
|
|
|
* POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
|
|
|
* \note Either Q or R can be NULL.
|
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_div_int( mpi *Q, mpi *R, const mpi *A, int b );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Modulo: R = A mod B
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param R Destination MPI for the rest value
|
|
|
|
* \param A Left-hand MPI
|
|
|
|
* \param B Right-hand MPI
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed,
|
2009-06-23 19:46:08 +00:00
|
|
|
* POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0,
|
|
|
|
* POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Modulo: r = A mod b
|
|
|
|
*
|
2010-03-16 21:09:09 +00:00
|
|
|
* \param r Destination t_int
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param A Left-hand MPI
|
|
|
|
* \param b Integer to divide by
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed,
|
2009-06-23 19:46:08 +00:00
|
|
|
* POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0,
|
|
|
|
* POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_mod_int( t_int *r, const mpi *A, int b );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Sliding-window exponentiation: X = A^E mod N
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Destination MPI
|
|
|
|
* \param A Left-hand MPI
|
|
|
|
* \param E Exponent MPI
|
|
|
|
* \param N Modular MPI
|
|
|
|
* \param _RR Speed-up MPI used for recalculations
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed,
|
2009-01-03 21:51:57 +00:00
|
|
|
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
|
|
|
* \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.
|
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Greatest common divisor: G = gcd(A, B)
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param G Destination MPI
|
|
|
|
* \param A Left-hand MPI
|
|
|
|
* \param B Right-hand MPI
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed
|
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_gcd( mpi *G, const mpi *A, const mpi *B );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Modular inverse: X = A^-1 mod N
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Destination MPI
|
|
|
|
* \param A Left-hand MPI
|
|
|
|
* \param N Right-hand MPI
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* 1 if memory allocation failed,
|
2009-01-03 21:51:57 +00:00
|
|
|
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
|
2009-07-28 07:18:38 +00:00
|
|
|
POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2010-03-16 21:09:09 +00:00
|
|
|
int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Miller-Rabin primality test
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X MPI to check
|
|
|
|
* \param f_rng RNG function
|
|
|
|
* \param p_rng RNG parameter
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful (probably prime),
|
|
|
|
* 1 if memory allocation failed,
|
2009-01-03 21:51:57 +00:00
|
|
|
* POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
|
|
|
int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Prime number generation
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Destination MPI
|
|
|
|
* \param nbits Required size of X in bits
|
|
|
|
* \param dh_flag If 1, then (X-1)/2 will be prime too
|
2009-01-03 21:22:43 +00:00
|
|
|
* \param f_rng RNG function
|
|
|
|
* \param p_rng RNG parameter
|
|
|
|
*
|
|
|
|
* \return 0 if successful (probably prime),
|
|
|
|
* 1 if memory allocation failed,
|
2009-01-03 21:51:57 +00:00
|
|
|
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
|
|
|
int mpi_gen_prime( mpi *X, int nbits, int dh_flag,
|
|
|
|
int (*f_rng)(void *), void *p_rng );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Checkup routine
|
|
|
|
*
|
|
|
|
* \return 0 if successful, or 1 if the test failed
|
|
|
|
*/
|
|
|
|
int mpi_self_test( int verbose );
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* bignum.h */
|