2009-01-03 21:22:43 +00:00
|
|
|
/**
|
|
|
|
* \file bignum.h
|
2009-01-04 16:27:10 +00:00
|
|
|
*
|
2011-01-06 12:28:03 +00:00
|
|
|
* \brief Multi-precision integer library
|
|
|
|
*
|
2015-01-23 09:45:19 +00:00
|
|
|
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
2015-01-23 11:06:27 +00:00
|
|
|
* This file is part of mbed TLS (https://www.polarssl.org)
|
2010-07-18 20:36:00 +00:00
|
|
|
*
|
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
|
|
|
|
2011-04-24 08:57:21 +00:00
|
|
|
#include <string.h>
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2014-04-29 10:39:06 +00:00
|
|
|
#if !defined(POLARSSL_CONFIG_FILE)
|
2012-01-20 10:08:14 +00:00
|
|
|
#include "config.h"
|
2014-04-29 10:39:06 +00:00
|
|
|
#else
|
|
|
|
#include POLARSSL_CONFIG_FILE
|
|
|
|
#endif
|
2012-01-20 10:08:14 +00:00
|
|
|
|
2015-01-19 15:01:53 +00:00
|
|
|
#if defined(POLARSSL_FS_IO)
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2013-10-28 17:48:30 +00:00
|
|
|
#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
|
2012-10-01 14:41:15 +00:00
|
|
|
#include <basetsd.h>
|
2012-11-02 11:06:08 +00:00
|
|
|
#if (_MSC_VER <= 1200)
|
|
|
|
typedef signed short int16_t;
|
|
|
|
typedef unsigned short uint16_t;
|
|
|
|
#else
|
2012-10-01 14:41:15 +00:00
|
|
|
typedef INT16 int16_t;
|
|
|
|
typedef UINT16 uint16_t;
|
2012-11-02 11:06:08 +00:00
|
|
|
#endif
|
2012-10-01 20:57:38 +00:00
|
|
|
typedef INT32 int32_t;
|
2013-02-27 14:02:50 +00:00
|
|
|
typedef INT64 int64_t;
|
2012-10-01 14:41:15 +00:00
|
|
|
typedef UINT32 uint32_t;
|
|
|
|
typedef UINT64 uint64_t;
|
|
|
|
#else
|
|
|
|
#include <inttypes.h>
|
2014-05-01 11:03:14 +00:00
|
|
|
#endif /* _MSC_VER && !EFIX64 && !EFI32 */
|
2012-10-01 14:41:15 +00:00
|
|
|
|
2011-05-09 16:17:09 +00:00
|
|
|
#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. */
|
2011-12-10 21:55:01 +00:00
|
|
|
#define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
|
2011-05-09 16:17:09 +00:00
|
|
|
#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. */
|
2011-12-10 21:55:01 +00:00
|
|
|
#define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010 /**< Memory allocation failed. */
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-11-22 16:54:59 +00:00
|
|
|
#define MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2011-05-05 10:00:45 +00:00
|
|
|
/*
|
|
|
|
* Maximum size MPIs are allowed to grow to in number of limbs.
|
|
|
|
*/
|
|
|
|
#define POLARSSL_MPI_MAX_LIMBS 10000
|
|
|
|
|
2014-05-05 09:40:14 +00:00
|
|
|
#if !defined(POLARSSL_MPI_WINDOW_SIZE)
|
2011-11-25 11:52:11 +00:00
|
|
|
/*
|
|
|
|
* Maximum window size used for modular exponentiation. Default: 6
|
|
|
|
* Minimum value: 1. Maximum value: 6.
|
|
|
|
*
|
|
|
|
* Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used
|
|
|
|
* for the sliding window calculation. (So 64 by default)
|
|
|
|
*
|
|
|
|
* Reduction in size, reduces speed.
|
|
|
|
*/
|
|
|
|
#define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
|
2014-05-05 09:40:14 +00:00
|
|
|
#endif /* !POLARSSL_MPI_WINDOW_SIZE */
|
2011-11-25 11:52:11 +00:00
|
|
|
|
2014-05-05 09:40:14 +00:00
|
|
|
#if !defined(POLARSSL_MPI_MAX_SIZE)
|
2011-11-25 12:11:43 +00:00
|
|
|
/*
|
|
|
|
* Maximum size of MPIs allowed in bits and bytes for user-MPIs.
|
2013-01-21 11:10:00 +00:00
|
|
|
* ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
|
2011-11-25 12:11:43 +00:00
|
|
|
*
|
|
|
|
* Note: Calculations can results temporarily in larger MPIs. So the number
|
|
|
|
* of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher.
|
|
|
|
*/
|
2014-09-08 15:03:50 +00:00
|
|
|
#define POLARSSL_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
|
2014-05-05 09:40:14 +00:00
|
|
|
#endif /* !POLARSSL_MPI_MAX_SIZE */
|
2013-06-24 17:31:17 +00:00
|
|
|
|
2011-11-25 12:11:43 +00:00
|
|
|
#define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
|
|
|
|
|
2011-11-30 16:00:20 +00:00
|
|
|
/*
|
2012-09-26 19:20:46 +00:00
|
|
|
* When reading from files with mpi_read_file() and writing to files with
|
|
|
|
* mpi_write_file() the buffer should have space
|
2011-11-30 16:00:20 +00:00
|
|
|
* for a (short) label, the MPI (in the provided radix), the newline
|
|
|
|
* characters and the '\0'.
|
|
|
|
*
|
|
|
|
* By default we assume at least a 10 char label, a minimum radix of 10
|
|
|
|
* (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
|
2012-09-27 20:42:35 +00:00
|
|
|
* Autosized at compile time for at least a 10 char label, a minimum radix
|
|
|
|
* of 10 (decimal) for a number of POLARSSL_MPI_MAX_BITS size.
|
|
|
|
*
|
|
|
|
* This used to be statically sized to 1250 for a maximum of 4096 bit
|
|
|
|
* numbers (1234 decimal chars).
|
|
|
|
*
|
|
|
|
* Calculate using the formula:
|
|
|
|
* POLARSSL_MPI_RW_BUFFER_SIZE = ceil(POLARSSL_MPI_MAX_BITS / ln(10) * ln(2)) +
|
|
|
|
* LabelSize + 6
|
2011-11-30 16:00:20 +00:00
|
|
|
*/
|
2012-09-27 20:42:35 +00:00
|
|
|
#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 )
|
2011-11-30 16:00:20 +00:00
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/*
|
|
|
|
* Define the base integer type, architecture-wise
|
|
|
|
*/
|
2009-01-03 21:51:57 +00:00
|
|
|
#if defined(POLARSSL_HAVE_INT8)
|
2011-04-24 09:11:17 +00:00
|
|
|
typedef signed char t_sint;
|
|
|
|
typedef unsigned char t_uint;
|
2012-10-01 14:41:15 +00:00
|
|
|
typedef uint16_t t_udbl;
|
2012-10-02 12:19:31 +00:00
|
|
|
#define POLARSSL_HAVE_UDBL
|
2009-01-03 21:22:43 +00:00
|
|
|
#else
|
2009-01-03 21:51:57 +00:00
|
|
|
#if defined(POLARSSL_HAVE_INT16)
|
2012-10-01 14:41:15 +00:00
|
|
|
typedef int16_t t_sint;
|
|
|
|
typedef uint16_t t_uint;
|
|
|
|
typedef uint32_t t_udbl;
|
2012-10-02 12:19:31 +00:00
|
|
|
#define POLARSSL_HAVE_UDBL
|
2009-01-03 21:22:43 +00:00
|
|
|
#else
|
2013-12-12 14:49:10 +00:00
|
|
|
/*
|
|
|
|
* 32-bit integers can be forced on 64-bit arches (eg. for testing purposes)
|
2014-05-06 01:08:57 +00:00
|
|
|
* by defining POLARSSL_HAVE_INT32 and undefining POLARSSL_HAVE_ASM
|
2013-12-12 14:49:10 +00:00
|
|
|
*/
|
|
|
|
#if ( ! defined(POLARSSL_HAVE_INT32) && \
|
|
|
|
defined(_MSC_VER) && defined(_M_AMD64) )
|
2013-10-23 18:17:00 +00:00
|
|
|
#define POLARSSL_HAVE_INT64
|
2012-10-02 12:19:31 +00:00
|
|
|
typedef int64_t t_sint;
|
|
|
|
typedef uint64_t t_uint;
|
|
|
|
#else
|
2013-12-12 14:49:10 +00:00
|
|
|
#if ( ! defined(POLARSSL_HAVE_INT32) && \
|
|
|
|
defined(__GNUC__) && ( \
|
2012-10-02 12:19:31 +00:00
|
|
|
defined(__amd64__) || defined(__x86_64__) || \
|
|
|
|
defined(__ppc64__) || defined(__powerpc64__) || \
|
|
|
|
defined(__ia64__) || defined(__alpha__) || \
|
|
|
|
(defined(__sparc__) && defined(__arch64__)) || \
|
|
|
|
defined(__s390x__) ) )
|
2013-10-23 18:17:00 +00:00
|
|
|
#define POLARSSL_HAVE_INT64
|
2012-10-02 12:19:31 +00:00
|
|
|
typedef int64_t t_sint;
|
|
|
|
typedef uint64_t t_uint;
|
|
|
|
typedef unsigned int t_udbl __attribute__((mode(TI)));
|
|
|
|
#define POLARSSL_HAVE_UDBL
|
|
|
|
#else
|
2013-10-21 15:51:45 +00:00
|
|
|
#define POLARSSL_HAVE_INT32
|
2012-10-02 12:19:31 +00:00
|
|
|
typedef int32_t t_sint;
|
|
|
|
typedef uint32_t t_uint;
|
|
|
|
#if ( defined(_MSC_VER) && defined(_M_IX86) )
|
|
|
|
typedef uint64_t t_udbl;
|
|
|
|
#define POLARSSL_HAVE_UDBL
|
|
|
|
#else
|
|
|
|
#if defined( POLARSSL_HAVE_LONGLONG )
|
|
|
|
typedef unsigned long long t_udbl;
|
|
|
|
#define POLARSSL_HAVE_UDBL
|
|
|
|
#endif
|
|
|
|
#endif
|
2014-06-17 12:34:44 +00:00
|
|
|
#endif /* !POLARSSL_HAVE_INT32 && __GNUC__ && 64-bit platform */
|
|
|
|
#endif /* !POLARSSL_HAVE_INT32 && _MSC_VER && _M_AMD64 */
|
2012-10-01 14:41:15 +00:00
|
|
|
#endif /* POLARSSL_HAVE_INT16 */
|
|
|
|
#endif /* POLARSSL_HAVE_INT8 */
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-06-27 12:29:21 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/**
|
|
|
|
* \brief MPI structure
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int s; /*!< integer sign */
|
2011-04-24 08:57:21 +00:00
|
|
|
size_t n; /*!< total # of limbs */
|
2011-04-24 09:11:17 +00:00
|
|
|
t_uint *p; /*!< pointer to limbs */
|
2009-01-03 21:22:43 +00:00
|
|
|
}
|
|
|
|
mpi;
|
|
|
|
|
|
|
|
/**
|
2011-05-05 11:49:20 +00:00
|
|
|
* \brief Initialize one MPI
|
|
|
|
*
|
|
|
|
* \param X One MPI to initialize.
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2011-05-05 11:49:20 +00:00
|
|
|
void mpi_init( mpi *X );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
2011-05-05 11:49:20 +00:00
|
|
|
* \brief Unallocate one MPI
|
|
|
|
*
|
|
|
|
* \param X One MPI to unallocate.
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2011-05-05 11:49:20 +00:00
|
|
|
void mpi_free( mpi *X );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \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,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2011-04-24 08:57:21 +00:00
|
|
|
int mpi_grow( mpi *X, size_t nblimbs );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2013-11-21 09:39:37 +00:00
|
|
|
/**
|
|
|
|
* \brief Resize down, keeping at least the specified number of limbs
|
|
|
|
*
|
|
|
|
* \param X MPI to shrink
|
|
|
|
* \param nblimbs The minimum number of limbs to keep
|
|
|
|
*
|
|
|
|
* \return 0 if successful,
|
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
|
|
|
*/
|
|
|
|
int mpi_shrink( mpi *X, size_t nblimbs );
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/**
|
|
|
|
* \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,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
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 );
|
|
|
|
|
2013-11-21 15:56:39 +00:00
|
|
|
/**
|
|
|
|
* \brief Safe conditional assignement X = Y if assign is 1
|
|
|
|
*
|
|
|
|
* \param X MPI to conditionally assign to
|
|
|
|
* \param Y Value to be assigned
|
2013-12-04 20:41:50 +00:00
|
|
|
* \param assign 1: perform the assignment, 0: keep X's original value
|
2013-11-21 15:56:39 +00:00
|
|
|
*
|
|
|
|
* \return 0 if successful,
|
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
|
|
|
*
|
|
|
|
* \note This function is equivalent to
|
|
|
|
* if( assign ) mpi_copy( X, Y );
|
|
|
|
* except that it avoids leaking any information about whether
|
|
|
|
* the assignment was done or not (the above code may leak
|
2013-11-21 19:00:38 +00:00
|
|
|
* information through branch prediction and/or memory access
|
|
|
|
* patterns analysis).
|
2013-11-21 15:56:39 +00:00
|
|
|
*/
|
2013-11-25 17:28:53 +00:00
|
|
|
int mpi_safe_cond_assign( mpi *X, const mpi *Y, unsigned char assign );
|
2013-11-21 15:56:39 +00:00
|
|
|
|
2013-12-04 20:41:50 +00:00
|
|
|
/**
|
|
|
|
* \brief Safe conditional swap X <-> Y if swap is 1
|
|
|
|
*
|
|
|
|
* \param X First mpi value
|
|
|
|
* \param Y Second mpi value
|
|
|
|
* \param assign 1: perform the swap, 0: keep X and Y's original values
|
|
|
|
*
|
|
|
|
* \return 0 if successful,
|
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
|
|
|
*
|
|
|
|
* \note This function is equivalent to
|
|
|
|
* if( assign ) mpi_swap( X, Y );
|
|
|
|
* except that it avoids leaking any information about whether
|
|
|
|
* the assignment was done or not (the above code may leak
|
|
|
|
* information through branch prediction and/or memory access
|
|
|
|
* patterns analysis).
|
|
|
|
*/
|
|
|
|
int mpi_safe_cond_swap( mpi *X, mpi *Y, unsigned char assign );
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/**
|
|
|
|
* \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,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2011-04-24 09:11:17 +00:00
|
|
|
int mpi_lset( mpi *X, t_sint z );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-11-14 12:39:52 +00:00
|
|
|
/**
|
2011-05-18 15:47:11 +00:00
|
|
|
* \brief Get a specific bit from X
|
|
|
|
*
|
|
|
|
* \param X MPI to use
|
|
|
|
* \param pos Zero-based index of the bit in X
|
|
|
|
*
|
|
|
|
* \return Either a 0 or a 1
|
|
|
|
*/
|
2012-05-08 12:01:43 +00:00
|
|
|
int mpi_get_bit( const mpi *X, size_t pos );
|
2011-05-18 15:47:11 +00:00
|
|
|
|
2012-11-14 12:39:52 +00:00
|
|
|
/**
|
2011-05-18 15:47:11 +00:00
|
|
|
* \brief 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
|
|
|
|
*
|
|
|
|
* \param X MPI to use
|
|
|
|
* \param pos Zero-based index of the bit in X
|
|
|
|
* \param val The value to set the bit to (0 or 1)
|
|
|
|
*
|
|
|
|
* \return 0 if successful,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
2011-05-18 15:47:11 +00:00
|
|
|
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
|
|
|
|
*/
|
|
|
|
int mpi_set_bit( mpi *X, size_t pos, unsigned char val );
|
|
|
|
|
2009-01-03 21:22:43 +00:00
|
|
|
/**
|
2012-05-08 12:01:43 +00:00
|
|
|
* \brief 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
|
2009-07-28 07:18:38 +00:00
|
|
|
*
|
|
|
|
* \param X MPI to use
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2011-04-24 08:57:21 +00:00
|
|
|
size_t mpi_lsb( const mpi *X );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
2012-05-08 12:01:43 +00:00
|
|
|
* \brief 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
|
2009-07-28 07:18:38 +00:00
|
|
|
*
|
|
|
|
* \param X MPI to use
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2011-04-24 08:57:21 +00:00
|
|
|
size_t 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
|
|
|
*/
|
2011-04-24 08:57:21 +00:00
|
|
|
size_t 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
|
|
|
*
|
2011-11-30 16:00:20 +00:00
|
|
|
* \return 0 if successful, or a 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
|
|
|
*
|
2011-11-30 16:00:20 +00:00
|
|
|
* \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code.
|
2010-03-16 21:09:09 +00:00
|
|
|
* *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.
|
|
|
|
*/
|
2011-04-24 08:57:21 +00:00
|
|
|
int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
2012-10-23 11:08:02 +00:00
|
|
|
#if defined(POLARSSL_FS_IO)
|
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
|
|
|
*
|
2011-11-30 16:00:20 +00:00
|
|
|
* \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if
|
|
|
|
* the file read buffer is too small or a
|
|
|
|
* 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
|
|
|
*
|
2011-11-30 16:00:20 +00:00
|
|
|
* \return 0 if successful, or a 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 );
|
2012-10-23 11:08:02 +00:00
|
|
|
#endif /* POLARSSL_FS_IO */
|
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,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2011-04-24 08:57:21 +00:00
|
|
|
int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
2014-06-25 10:57:47 +00:00
|
|
|
* \brief Export X into unsigned binary data, big endian.
|
|
|
|
* Always fills the whole buffer, which will start with zeros
|
|
|
|
* if the number is smaller.
|
2009-01-03 21:22:43 +00:00
|
|
|
*
|
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
|
|
|
*/
|
2011-04-24 08:57:21 +00:00
|
|
|
int mpi_write_binary( const mpi *X, unsigned char *buf, size_t 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,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2011-04-24 08:57:21 +00:00
|
|
|
int mpi_shift_l( mpi *X, size_t count );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \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,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2011-04-24 08:57:21 +00:00
|
|
|
int mpi_shift_r( mpi *X, size_t count );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \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
|
|
|
|
*/
|
2011-04-24 09:11:17 +00:00
|
|
|
int mpi_cmp_int( const mpi *X, t_sint 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,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
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
|
|
|
|
|
|
|
/**
|
2013-10-29 09:02:51 +00:00
|
|
|
* \brief Unsigned subtraction: X = |A| - |B|
|
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 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,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
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
|
|
|
|
|
|
|
/**
|
2013-10-29 09:02:51 +00:00
|
|
|
* \brief Signed subtraction: X = A - B
|
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 Right-hand MPI
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
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,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2011-04-24 09:11:17 +00:00
|
|
|
int mpi_add_int( mpi *X, const mpi *A, t_sint b );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
2013-10-29 09:02:51 +00:00
|
|
|
* \brief Signed subtraction: X = A - b
|
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 subtract
|
|
|
|
*
|
2009-01-03 21:22:43 +00:00
|
|
|
* \return 0 if successful,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2011-04-24 09:11:17 +00:00
|
|
|
int mpi_sub_int( mpi *X, const mpi *A, t_sint 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,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
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
|
2013-11-25 15:16:33 +00:00
|
|
|
* Note: despite the functon signature, b is treated as a
|
|
|
|
* t_uint. Negative values of b are treated as large positive
|
|
|
|
* values.
|
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,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
2011-04-24 09:11:17 +00:00
|
|
|
int mpi_mul_int( mpi *X, const mpi *A, t_sint 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,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED 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,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED 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.
|
|
|
|
*/
|
2011-04-24 09:11:17 +00:00
|
|
|
int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint 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,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED 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
|
|
|
|
*
|
2011-04-24 09:11:17 +00:00
|
|
|
* \param r Destination t_uint
|
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,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED 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
|
|
|
*/
|
2011-04-24 09:11:17 +00:00
|
|
|
int mpi_mod_int( t_uint *r, const mpi *A, t_sint b );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Sliding-window exponentiation: X = A^E mod N
|
|
|
|
*
|
2014-05-01 11:03:14 +00:00
|
|
|
* \param X Destination MPI
|
2009-07-28 07:18:38 +00:00
|
|
|
* \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,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed,
|
2014-05-01 12:18:25 +00:00
|
|
|
* POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
|
|
|
|
* if E is negative
|
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
|
|
|
|
2011-03-26 13:18:49 +00:00
|
|
|
/**
|
|
|
|
* \brief Fill an MPI X with size bytes of random
|
|
|
|
*
|
|
|
|
* \param X Destination MPI
|
|
|
|
* \param size Size in bytes
|
|
|
|
* \param f_rng RNG function
|
|
|
|
* \param p_rng RNG parameter
|
|
|
|
*
|
|
|
|
* \return 0 if successful,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2011-03-26 13:18:49 +00:00
|
|
|
*/
|
2011-11-27 21:07:34 +00:00
|
|
|
int mpi_fill_random( mpi *X, size_t size,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng );
|
2011-03-26 13:18:49 +00:00
|
|
|
|
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,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed
|
2009-01-03 21:22:43 +00:00
|
|
|
*/
|
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,
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED 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),
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED 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
|
|
|
*/
|
2013-11-25 13:26:52 +00:00
|
|
|
int mpi_is_prime( mpi *X,
|
2011-11-27 21:07:34 +00:00
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Prime number generation
|
|
|
|
*
|
2009-07-28 07:18:38 +00:00
|
|
|
* \param X Destination MPI
|
2014-05-01 12:18:25 +00:00
|
|
|
* \param nbits Required size of X in bits
|
|
|
|
* ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS )
|
2009-07-28 07:18:38 +00:00
|
|
|
* \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),
|
2011-12-10 21:55:01 +00:00
|
|
|
* POLARSSL_ERR_MPI_MALLOC_FAILED 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
|
|
|
*/
|
2011-04-24 08:57:21 +00:00
|
|
|
int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag,
|
2011-11-27 21:07:34 +00:00
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng );
|
2009-01-03 21:22:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \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 */
|