| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** | 
|  | 2 | * \file bignum.h | 
| Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * | 
| Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 4 | * \brief  Multi-precision integer library | 
|  | 5 | * | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 6 | *  Copyright (C) 2006-2014, Brainspark B.V. | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * | 
|  | 8 | *  This file is part of PolarSSL (http://www.polarssl.org) | 
| Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 9 | *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 10 | * | 
| Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 11 | *  All rights reserved. | 
| Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 12 | * | 
| Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 13 | *  This program is free software; you can redistribute it and/or modify | 
|  | 14 | *  it under the terms of the GNU General Public License as published by | 
|  | 15 | *  the Free Software Foundation; either version 2 of the License, or | 
|  | 16 | *  (at your option) any later version. | 
|  | 17 | * | 
|  | 18 | *  This program is distributed in the hope that it will be useful, | 
|  | 19 | *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 20 | *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 21 | *  GNU General Public License for more details. | 
|  | 22 | * | 
|  | 23 | *  You should have received a copy of the GNU General Public License along | 
|  | 24 | *  with this program; if not, write to the Free Software Foundation, Inc., | 
|  | 25 | *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | */ | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 27 | #ifndef POLARSSL_BIGNUM_H | 
|  | 28 | #define POLARSSL_BIGNUM_H | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 |  | 
|  | 30 | #include <stdio.h> | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 31 | #include <string.h> | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 32 |  | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 33 | #if !defined(POLARSSL_CONFIG_FILE) | 
| Paul Bakker | cf0360a | 2012-01-20 10:08:14 +0000 | [diff] [blame] | 34 | #include "config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 35 | #else | 
|  | 36 | #include POLARSSL_CONFIG_FILE | 
|  | 37 | #endif | 
| Paul Bakker | cf0360a | 2012-01-20 10:08:14 +0000 | [diff] [blame] | 38 |  | 
| Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 39 | #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32) | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 40 | #include <basetsd.h> | 
| Paul Bakker | 4a2bd0d | 2012-11-02 11:06:08 +0000 | [diff] [blame] | 41 | #if (_MSC_VER <= 1200) | 
|  | 42 | typedef   signed short  int16_t; | 
|  | 43 | typedef unsigned short uint16_t; | 
|  | 44 | #else | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 45 | typedef  INT16  int16_t; | 
|  | 46 | typedef UINT16 uint16_t; | 
| Paul Bakker | 4a2bd0d | 2012-11-02 11:06:08 +0000 | [diff] [blame] | 47 | #endif | 
| Paul Bakker | 9ef6e2b | 2012-10-01 20:57:38 +0000 | [diff] [blame] | 48 | typedef  INT32  int32_t; | 
| Paul Bakker | 8ea31ff | 2013-02-27 15:02:50 +0100 | [diff] [blame] | 49 | typedef  INT64  int64_t; | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 50 | typedef UINT32 uint32_t; | 
|  | 51 | typedef UINT64 uint64_t; | 
|  | 52 | #else | 
|  | 53 | #include <inttypes.h> | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 54 | #endif /* _MSC_VER && !EFIX64 && !EFI32 */ | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 55 |  | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 56 | #define POLARSSL_ERR_MPI_FILE_IO_ERROR                     -0x0002  /**< An error occurred while reading from or writing to a file. */ | 
|  | 57 | #define POLARSSL_ERR_MPI_BAD_INPUT_DATA                    -0x0004  /**< Bad input parameters to function. */ | 
|  | 58 | #define POLARSSL_ERR_MPI_INVALID_CHARACTER                 -0x0006  /**< There is an invalid character in the digit string. */ | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 59 | #define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL                  -0x0008  /**< The buffer is too small to write to. */ | 
| Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 60 | #define POLARSSL_ERR_MPI_NEGATIVE_VALUE                    -0x000A  /**< The input arguments are negative or result in illegal output. */ | 
|  | 61 | #define POLARSSL_ERR_MPI_DIVISION_BY_ZERO                  -0x000C  /**< The input argument for division is zero, which is not allowed. */ | 
|  | 62 | #define POLARSSL_ERR_MPI_NOT_ACCEPTABLE                    -0x000E  /**< The input arguments are not acceptable. */ | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 63 | #define POLARSSL_ERR_MPI_MALLOC_FAILED                     -0x0010  /**< Memory allocation failed. */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 64 |  | 
| Manuel Pégourié-Gonnard | 0160eac | 2013-11-22 17:54:59 +0100 | [diff] [blame] | 65 | #define MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 66 |  | 
|  | 67 | /* | 
| Paul Bakker | f968857 | 2011-05-05 10:00:45 +0000 | [diff] [blame] | 68 | * Maximum size MPIs are allowed to grow to in number of limbs. | 
|  | 69 | */ | 
|  | 70 | #define POLARSSL_MPI_MAX_LIMBS                             10000 | 
|  | 71 |  | 
| Paul Bakker | 770268f | 2014-05-05 11:40:14 +0200 | [diff] [blame] | 72 | #if !defined(POLARSSL_MPI_WINDOW_SIZE) | 
| Paul Bakker | f968857 | 2011-05-05 10:00:45 +0000 | [diff] [blame] | 73 | /* | 
| Paul Bakker | b6d5f08 | 2011-11-25 11:52:11 +0000 | [diff] [blame] | 74 | * Maximum window size used for modular exponentiation. Default: 6 | 
|  | 75 | * Minimum value: 1. Maximum value: 6. | 
|  | 76 | * | 
|  | 77 | * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used | 
|  | 78 | * for the sliding window calculation. (So 64 by default) | 
|  | 79 | * | 
|  | 80 | * Reduction in size, reduces speed. | 
|  | 81 | */ | 
|  | 82 | #define POLARSSL_MPI_WINDOW_SIZE                           6        /**< Maximum windows size used. */ | 
| Paul Bakker | 770268f | 2014-05-05 11:40:14 +0200 | [diff] [blame] | 83 | #endif /* !POLARSSL_MPI_WINDOW_SIZE */ | 
| Paul Bakker | b6d5f08 | 2011-11-25 11:52:11 +0000 | [diff] [blame] | 84 |  | 
| Paul Bakker | 770268f | 2014-05-05 11:40:14 +0200 | [diff] [blame] | 85 | #if !defined(POLARSSL_MPI_MAX_SIZE) | 
| Paul Bakker | b6d5f08 | 2011-11-25 11:52:11 +0000 | [diff] [blame] | 86 | /* | 
| Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 87 | * Maximum size of MPIs allowed in bits and bytes for user-MPIs. | 
| Paul Bakker | f626e1d | 2013-01-21 12:10:00 +0100 | [diff] [blame] | 88 | * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits ) | 
| Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 89 | * | 
|  | 90 | * Note: Calculations can results temporarily in larger MPIs. So the number | 
|  | 91 | * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher. | 
|  | 92 | */ | 
| Manuel Pégourié-Gonnard | da1b4de | 2014-09-08 17:03:50 +0200 | [diff] [blame] | 93 | #define POLARSSL_MPI_MAX_SIZE                              1024     /**< Maximum number of bytes for usable MPIs. */ | 
| Paul Bakker | 770268f | 2014-05-05 11:40:14 +0200 | [diff] [blame] | 94 | #endif /* !POLARSSL_MPI_MAX_SIZE */ | 
| Paul Bakker | 9bcf16c | 2013-06-24 19:31:17 +0200 | [diff] [blame] | 95 |  | 
| Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 96 | #define POLARSSL_MPI_MAX_BITS                              ( 8 * POLARSSL_MPI_MAX_SIZE )    /**< Maximum number of bits for usable MPIs. */ | 
|  | 97 |  | 
|  | 98 | /* | 
| Paul Bakker | 5531c6d | 2012-09-26 19:20:46 +0000 | [diff] [blame] | 99 | * When reading from files with mpi_read_file() and writing to files with | 
|  | 100 | * mpi_write_file() the buffer should have space | 
| Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 101 | * for a (short) label, the MPI (in the provided radix), the newline | 
|  | 102 | * characters and the '\0'. | 
|  | 103 | * | 
|  | 104 | * By default we assume at least a 10 char label, a minimum radix of 10 | 
|  | 105 | * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars). | 
| Paul Bakker | f918310 | 2012-09-27 20:42:35 +0000 | [diff] [blame] | 106 | * Autosized at compile time for at least a 10 char label, a minimum radix | 
|  | 107 | * of 10 (decimal) for a number of POLARSSL_MPI_MAX_BITS size. | 
|  | 108 | * | 
|  | 109 | * This used to be statically sized to 1250 for a maximum of 4096 bit | 
|  | 110 | * numbers (1234 decimal chars). | 
|  | 111 | * | 
|  | 112 | * Calculate using the formula: | 
|  | 113 | *  POLARSSL_MPI_RW_BUFFER_SIZE = ceil(POLARSSL_MPI_MAX_BITS / ln(10) * ln(2)) + | 
|  | 114 | *                                LabelSize + 6 | 
| Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 115 | */ | 
| Paul Bakker | f918310 | 2012-09-27 20:42:35 +0000 | [diff] [blame] | 116 | #define POLARSSL_MPI_MAX_BITS_SCALE100          ( 100 * POLARSSL_MPI_MAX_BITS ) | 
|  | 117 | #define LN_2_DIV_LN_10_SCALE100                 332 | 
|  | 118 | #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 ) | 
| Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 119 |  | 
|  | 120 | /* | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 121 | * Define the base integer type, architecture-wise | 
|  | 122 | */ | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 123 | #if defined(POLARSSL_HAVE_INT8) | 
| Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 124 | typedef   signed char  t_sint; | 
|  | 125 | typedef unsigned char  t_uint; | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 126 | typedef uint16_t       t_udbl; | 
| Paul Bakker | 62261d6 | 2012-10-02 12:19:31 +0000 | [diff] [blame] | 127 | #define POLARSSL_HAVE_UDBL | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 128 | #else | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 129 | #if defined(POLARSSL_HAVE_INT16) | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 130 | typedef  int16_t t_sint; | 
|  | 131 | typedef uint16_t t_uint; | 
|  | 132 | typedef uint32_t t_udbl; | 
| Paul Bakker | 62261d6 | 2012-10-02 12:19:31 +0000 | [diff] [blame] | 133 | #define POLARSSL_HAVE_UDBL | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 | #else | 
| Manuel Pégourié-Gonnard | 96eed7b | 2013-12-12 15:49:10 +0100 | [diff] [blame] | 135 | /* | 
|  | 136 | * 32-bit integers can be forced on 64-bit arches (eg. for testing purposes) | 
| Barry K. Nathan | 79e69f9 | 2014-05-05 18:08:57 -0700 | [diff] [blame] | 137 | * by defining POLARSSL_HAVE_INT32 and undefining POLARSSL_HAVE_ASM | 
| Manuel Pégourié-Gonnard | 96eed7b | 2013-12-12 15:49:10 +0100 | [diff] [blame] | 138 | */ | 
|  | 139 | #if ( ! defined(POLARSSL_HAVE_INT32) && \ | 
|  | 140 | defined(_MSC_VER) && defined(_M_AMD64) ) | 
| Manuel Pégourié-Gonnard | 5779cbe | 2013-10-23 20:17:00 +0200 | [diff] [blame] | 141 | #define POLARSSL_HAVE_INT64 | 
| Paul Bakker | 62261d6 | 2012-10-02 12:19:31 +0000 | [diff] [blame] | 142 | typedef  int64_t t_sint; | 
|  | 143 | typedef uint64_t t_uint; | 
|  | 144 | #else | 
| Manuel Pégourié-Gonnard | 96eed7b | 2013-12-12 15:49:10 +0100 | [diff] [blame] | 145 | #if ( ! defined(POLARSSL_HAVE_INT32) &&               \ | 
|  | 146 | defined(__GNUC__) && (                          \ | 
| Paul Bakker | 62261d6 | 2012-10-02 12:19:31 +0000 | [diff] [blame] | 147 | defined(__amd64__) || defined(__x86_64__)    || \ | 
|  | 148 | defined(__ppc64__) || defined(__powerpc64__) || \ | 
|  | 149 | defined(__ia64__)  || defined(__alpha__)     || \ | 
|  | 150 | (defined(__sparc__) && defined(__arch64__))  || \ | 
|  | 151 | defined(__s390x__) ) ) | 
| Manuel Pégourié-Gonnard | 5779cbe | 2013-10-23 20:17:00 +0200 | [diff] [blame] | 152 | #define POLARSSL_HAVE_INT64 | 
| Paul Bakker | 62261d6 | 2012-10-02 12:19:31 +0000 | [diff] [blame] | 153 | typedef  int64_t t_sint; | 
|  | 154 | typedef uint64_t t_uint; | 
|  | 155 | typedef unsigned int t_udbl __attribute__((mode(TI))); | 
|  | 156 | #define POLARSSL_HAVE_UDBL | 
|  | 157 | #else | 
| Manuel Pégourié-Gonnard | a47e705 | 2013-10-21 17:51:45 +0200 | [diff] [blame] | 158 | #define POLARSSL_HAVE_INT32 | 
| Paul Bakker | 62261d6 | 2012-10-02 12:19:31 +0000 | [diff] [blame] | 159 | typedef  int32_t t_sint; | 
|  | 160 | typedef uint32_t t_uint; | 
|  | 161 | #if ( defined(_MSC_VER) && defined(_M_IX86) ) | 
|  | 162 | typedef uint64_t t_udbl; | 
|  | 163 | #define POLARSSL_HAVE_UDBL | 
|  | 164 | #else | 
|  | 165 | #if defined( POLARSSL_HAVE_LONGLONG ) | 
|  | 166 | typedef unsigned long long t_udbl; | 
|  | 167 | #define POLARSSL_HAVE_UDBL | 
|  | 168 | #endif | 
|  | 169 | #endif | 
| Paul Bakker | db20c10 | 2014-06-17 14:34:44 +0200 | [diff] [blame] | 170 | #endif /* !POLARSSL_HAVE_INT32 && __GNUC__ && 64-bit platform */ | 
|  | 171 | #endif /* !POLARSSL_HAVE_INT32 && _MSC_VER && _M_AMD64 */ | 
| Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 172 | #endif /* POLARSSL_HAVE_INT16 */ | 
|  | 173 | #endif /* POLARSSL_HAVE_INT8  */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 174 |  | 
| Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 175 | #ifdef __cplusplus | 
|  | 176 | extern "C" { | 
|  | 177 | #endif | 
|  | 178 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 179 | /** | 
|  | 180 | * \brief          MPI structure | 
|  | 181 | */ | 
|  | 182 | typedef struct | 
|  | 183 | { | 
|  | 184 | int s;              /*!<  integer sign      */ | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 185 | size_t n;           /*!<  total # of limbs  */ | 
| Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 186 | t_uint *p;          /*!<  pointer to limbs  */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 187 | } | 
|  | 188 | mpi; | 
|  | 189 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 190 | /** | 
| Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 191 | * \brief           Initialize one MPI | 
|  | 192 | * | 
|  | 193 | * \param X         One MPI to initialize. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 194 | */ | 
| Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 195 | void mpi_init( mpi *X ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 196 |  | 
|  | 197 | /** | 
| Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 198 | * \brief          Unallocate one MPI | 
|  | 199 | * | 
|  | 200 | * \param X        One MPI to unallocate. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 201 | */ | 
| Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 202 | void mpi_free( mpi *X ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 203 |  | 
|  | 204 | /** | 
|  | 205 | * \brief          Enlarge to the specified number of limbs | 
|  | 206 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 207 | * \param X        MPI to grow | 
|  | 208 | * \param nblimbs  The target number of limbs | 
|  | 209 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 210 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 211 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 212 | */ | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 213 | int mpi_grow( mpi *X, size_t nblimbs ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 214 |  | 
|  | 215 | /** | 
| Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 216 | * \brief          Resize down, keeping at least the specified number of limbs | 
|  | 217 | * | 
|  | 218 | * \param X        MPI to shrink | 
|  | 219 | * \param nblimbs  The minimum number of limbs to keep | 
|  | 220 | * | 
|  | 221 | * \return         0 if successful, | 
|  | 222 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed | 
|  | 223 | */ | 
|  | 224 | int mpi_shrink( mpi *X, size_t nblimbs ); | 
|  | 225 |  | 
|  | 226 | /** | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 227 | * \brief          Copy the contents of Y into X | 
|  | 228 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 229 | * \param X        Destination MPI | 
|  | 230 | * \param Y        Source MPI | 
|  | 231 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 232 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 233 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 234 | */ | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 235 | int mpi_copy( mpi *X, const mpi *Y ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 236 |  | 
|  | 237 | /** | 
|  | 238 | * \brief          Swap the contents of X and Y | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 239 | * | 
|  | 240 | * \param X        First MPI value | 
|  | 241 | * \param Y        Second MPI value | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 242 | */ | 
|  | 243 | void mpi_swap( mpi *X, mpi *Y ); | 
|  | 244 |  | 
|  | 245 | /** | 
| Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 246 | * \brief          Safe conditional assignement X = Y if assign is 1 | 
|  | 247 | * | 
|  | 248 | * \param X        MPI to conditionally assign to | 
|  | 249 | * \param Y        Value to be assigned | 
| Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 250 | * \param assign   1: perform the assignment, 0: keep X's original value | 
| Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 251 | * | 
|  | 252 | * \return         0 if successful, | 
|  | 253 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, | 
| Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 254 | * | 
|  | 255 | * \note           This function is equivalent to | 
|  | 256 | *                      if( assign ) mpi_copy( X, Y ); | 
|  | 257 | *                 except that it avoids leaking any information about whether | 
|  | 258 | *                 the assignment was done or not (the above code may leak | 
| Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 259 | *                 information through branch prediction and/or memory access | 
|  | 260 | *                 patterns analysis). | 
| Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 261 | */ | 
| Manuel Pégourié-Gonnard | 96c7a92 | 2013-11-25 18:28:53 +0100 | [diff] [blame] | 262 | int mpi_safe_cond_assign( mpi *X, const mpi *Y, unsigned char assign ); | 
| Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 263 |  | 
|  | 264 | /** | 
| Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 265 | * \brief          Safe conditional swap X <-> Y if swap is 1 | 
|  | 266 | * | 
|  | 267 | * \param X        First mpi value | 
|  | 268 | * \param Y        Second mpi value | 
|  | 269 | * \param assign   1: perform the swap, 0: keep X and Y's original values | 
|  | 270 | * | 
|  | 271 | * \return         0 if successful, | 
|  | 272 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, | 
|  | 273 | * | 
|  | 274 | * \note           This function is equivalent to | 
|  | 275 | *                      if( assign ) mpi_swap( X, Y ); | 
|  | 276 | *                 except that it avoids leaking any information about whether | 
|  | 277 | *                 the assignment was done or not (the above code may leak | 
|  | 278 | *                 information through branch prediction and/or memory access | 
|  | 279 | *                 patterns analysis). | 
|  | 280 | */ | 
|  | 281 | int mpi_safe_cond_swap( mpi *X, mpi *Y, unsigned char assign ); | 
|  | 282 |  | 
|  | 283 | /** | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 284 | * \brief          Set value from integer | 
|  | 285 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 286 | * \param X        MPI to set | 
|  | 287 | * \param z        Value to use | 
|  | 288 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 289 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 290 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 291 | */ | 
| Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 292 | int mpi_lset( mpi *X, t_sint z ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 293 |  | 
| Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 294 | /** | 
| Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 295 | * \brief          Get a specific bit from X | 
|  | 296 | * | 
|  | 297 | * \param X        MPI to use | 
|  | 298 | * \param pos      Zero-based index of the bit in X | 
|  | 299 | * | 
|  | 300 | * \return         Either a 0 or a 1 | 
|  | 301 | */ | 
| Paul Bakker | 6b906e5 | 2012-05-08 12:01:43 +0000 | [diff] [blame] | 302 | int mpi_get_bit( const mpi *X, size_t pos ); | 
| Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 303 |  | 
| Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 304 | /** | 
| Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 305 | * \brief          Set a bit of X to a specific value of 0 or 1 | 
|  | 306 | * | 
|  | 307 | * \note           Will grow X if necessary to set a bit to 1 in a not yet | 
|  | 308 | *                 existing limb. Will not grow if bit should be set to 0 | 
|  | 309 | * | 
|  | 310 | * \param X        MPI to use | 
|  | 311 | * \param pos      Zero-based index of the bit in X | 
|  | 312 | * \param val      The value to set the bit to (0 or 1) | 
|  | 313 | * | 
|  | 314 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 315 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, | 
| Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 316 | *                 POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1 | 
|  | 317 | */ | 
|  | 318 | int mpi_set_bit( mpi *X, size_t pos, unsigned char val ); | 
|  | 319 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 320 | /** | 
| Paul Bakker | 6b906e5 | 2012-05-08 12:01:43 +0000 | [diff] [blame] | 321 | * \brief          Return the number of zero-bits before the least significant | 
|  | 322 | *                 '1' bit | 
|  | 323 | * | 
|  | 324 | * Note: Thus also the zero-based index of the least significant '1' bit | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 325 | * | 
|  | 326 | * \param X        MPI to use | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 327 | */ | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 328 | size_t mpi_lsb( const mpi *X ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 329 |  | 
|  | 330 | /** | 
| Paul Bakker | 6b906e5 | 2012-05-08 12:01:43 +0000 | [diff] [blame] | 331 | * \brief          Return the number of bits up to and including the most | 
|  | 332 | *                 significant '1' bit' | 
|  | 333 | * | 
|  | 334 | * Note: Thus also the one-based index of the most significant '1' bit | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 335 | * | 
|  | 336 | * \param X        MPI to use | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 337 | */ | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 338 | size_t mpi_msb( const mpi *X ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 339 |  | 
|  | 340 | /** | 
|  | 341 | * \brief          Return the total size in bytes | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 342 | * | 
|  | 343 | * \param X        MPI to use | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 344 | */ | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 345 | size_t mpi_size( const mpi *X ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 346 |  | 
|  | 347 | /** | 
|  | 348 | * \brief          Import from an ASCII string | 
|  | 349 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 350 | * \param X        Destination MPI | 
|  | 351 | * \param radix    Input numeric base | 
|  | 352 | * \param s        Null-terminated string buffer | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 353 | * | 
| Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 354 | * \return         0 if successful, or a POLARSSL_ERR_MPI_XXX error code | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 355 | */ | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 356 | int mpi_read_string( mpi *X, int radix, const char *s ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 357 |  | 
|  | 358 | /** | 
|  | 359 | * \brief          Export into an ASCII string | 
|  | 360 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 361 | * \param X        Source MPI | 
|  | 362 | * \param radix    Output numeric base | 
|  | 363 | * \param s        String buffer | 
|  | 364 | * \param slen     String buffer size | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 365 | * | 
| Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 366 | * \return         0 if successful, or a POLARSSL_ERR_MPI_XXX error code. | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 367 | *                 *slen is always updated to reflect the amount | 
|  | 368 | *                 of data that has (or would have) been written. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 369 | * | 
|  | 370 | * \note           Call this function with *slen = 0 to obtain the | 
|  | 371 | *                 minimum required buffer size in *slen. | 
|  | 372 | */ | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 373 | int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 374 |  | 
| Paul Bakker | 2b6af2f | 2012-10-23 11:08:02 +0000 | [diff] [blame] | 375 | #if defined(POLARSSL_FS_IO) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 376 | /** | 
|  | 377 | * \brief          Read X from an opened file | 
|  | 378 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 379 | * \param X        Destination MPI | 
|  | 380 | * \param radix    Input numeric base | 
|  | 381 | * \param fin      Input file handle | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 382 | * | 
| Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 383 | * \return         0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if | 
|  | 384 | *                 the file read buffer is too small or a | 
|  | 385 | *                 POLARSSL_ERR_MPI_XXX error code | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 386 | */ | 
|  | 387 | int mpi_read_file( mpi *X, int radix, FILE *fin ); | 
|  | 388 |  | 
|  | 389 | /** | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 390 | * \brief          Write X into an opened file, or stdout if fout is NULL | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 391 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 392 | * \param p        Prefix, can be NULL | 
|  | 393 | * \param X        Source MPI | 
|  | 394 | * \param radix    Output numeric base | 
|  | 395 | * \param fout     Output file handle (can be NULL) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 396 | * | 
| Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 397 | * \return         0 if successful, or a POLARSSL_ERR_MPI_XXX error code | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 398 | * | 
|  | 399 | * \note           Set fout == NULL to print X on the console. | 
|  | 400 | */ | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 401 | int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout ); | 
| Paul Bakker | 2b6af2f | 2012-10-23 11:08:02 +0000 | [diff] [blame] | 402 | #endif /* POLARSSL_FS_IO */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 403 |  | 
|  | 404 | /** | 
|  | 405 | * \brief          Import X from unsigned binary data, big endian | 
|  | 406 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 407 | * \param X        Destination MPI | 
|  | 408 | * \param buf      Input buffer | 
|  | 409 | * \param buflen   Input buffer size | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 410 | * | 
|  | 411 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 412 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 413 | */ | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 414 | int mpi_read_binary( mpi *X, const unsigned char *buf, size_t buflen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 415 |  | 
|  | 416 | /** | 
| Manuel Pégourié-Gonnard | 3926a2c | 2014-06-25 12:57:47 +0200 | [diff] [blame] | 417 | * \brief          Export X into unsigned binary data, big endian. | 
|  | 418 | *                 Always fills the whole buffer, which will start with zeros | 
|  | 419 | *                 if the number is smaller. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 420 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 421 | * \param X        Source MPI | 
|  | 422 | * \param buf      Output buffer | 
|  | 423 | * \param buflen   Output buffer size | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 424 | * | 
|  | 425 | * \return         0 if successful, | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 426 | *                 POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 427 | */ | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 428 | int mpi_write_binary( const mpi *X, unsigned char *buf, size_t buflen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 429 |  | 
|  | 430 | /** | 
|  | 431 | * \brief          Left-shift: X <<= count | 
|  | 432 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 433 | * \param X        MPI to shift | 
|  | 434 | * \param count    Amount to shift | 
|  | 435 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 436 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 437 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 438 | */ | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 439 | int mpi_shift_l( mpi *X, size_t count ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 440 |  | 
|  | 441 | /** | 
|  | 442 | * \brief          Right-shift: X >>= count | 
|  | 443 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 444 | * \param X        MPI to shift | 
|  | 445 | * \param count    Amount to shift | 
|  | 446 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 447 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 448 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 449 | */ | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 450 | int mpi_shift_r( mpi *X, size_t count ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 451 |  | 
|  | 452 | /** | 
|  | 453 | * \brief          Compare unsigned values | 
|  | 454 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 455 | * \param X        Left-hand MPI | 
|  | 456 | * \param Y        Right-hand MPI | 
|  | 457 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 458 | * \return         1 if |X| is greater than |Y|, | 
|  | 459 | *                -1 if |X| is lesser  than |Y| or | 
|  | 460 | *                 0 if |X| is equal to |Y| | 
|  | 461 | */ | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 462 | int mpi_cmp_abs( const mpi *X, const mpi *Y ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 463 |  | 
|  | 464 | /** | 
|  | 465 | * \brief          Compare signed values | 
|  | 466 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 467 | * \param X        Left-hand MPI | 
|  | 468 | * \param Y        Right-hand MPI | 
|  | 469 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 470 | * \return         1 if X is greater than Y, | 
|  | 471 | *                -1 if X is lesser  than Y or | 
|  | 472 | *                 0 if X is equal to Y | 
|  | 473 | */ | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 474 | int mpi_cmp_mpi( const mpi *X, const mpi *Y ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 475 |  | 
|  | 476 | /** | 
|  | 477 | * \brief          Compare signed values | 
|  | 478 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 479 | * \param X        Left-hand MPI | 
|  | 480 | * \param z        The integer value to compare to | 
|  | 481 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 482 | * \return         1 if X is greater than z, | 
|  | 483 | *                -1 if X is lesser  than z or | 
|  | 484 | *                 0 if X is equal to z | 
|  | 485 | */ | 
| Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 486 | int mpi_cmp_int( const mpi *X, t_sint z ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 487 |  | 
|  | 488 | /** | 
|  | 489 | * \brief          Unsigned addition: X = |A| + |B| | 
|  | 490 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 491 | * \param X        Destination MPI | 
|  | 492 | * \param A        Left-hand MPI | 
|  | 493 | * \param B        Right-hand MPI | 
|  | 494 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 495 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 496 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 497 | */ | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 498 | int mpi_add_abs( mpi *X, const mpi *A, const mpi *B ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 499 |  | 
|  | 500 | /** | 
| Paul Bakker | 60b1d10 | 2013-10-29 10:02:51 +0100 | [diff] [blame] | 501 | * \brief          Unsigned subtraction: X = |A| - |B| | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 502 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 503 | * \param X        Destination MPI | 
|  | 504 | * \param A        Left-hand MPI | 
|  | 505 | * \param B        Right-hand MPI | 
|  | 506 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 507 | * \return         0 if successful, | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 508 | *                 POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 509 | */ | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 510 | int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 511 |  | 
|  | 512 | /** | 
|  | 513 | * \brief          Signed addition: X = A + B | 
|  | 514 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 515 | * \param X        Destination MPI | 
|  | 516 | * \param A        Left-hand MPI | 
|  | 517 | * \param B        Right-hand MPI | 
|  | 518 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 519 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 520 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 521 | */ | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 522 | int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 523 |  | 
|  | 524 | /** | 
| Paul Bakker | 60b1d10 | 2013-10-29 10:02:51 +0100 | [diff] [blame] | 525 | * \brief          Signed subtraction: X = A - B | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 526 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 527 | * \param X        Destination MPI | 
|  | 528 | * \param A        Left-hand MPI | 
|  | 529 | * \param B        Right-hand MPI | 
|  | 530 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 531 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 532 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 533 | */ | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 534 | int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 535 |  | 
|  | 536 | /** | 
|  | 537 | * \brief          Signed addition: X = A + b | 
|  | 538 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 539 | * \param X        Destination MPI | 
|  | 540 | * \param A        Left-hand MPI | 
|  | 541 | * \param b        The integer value to add | 
|  | 542 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 543 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 544 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 545 | */ | 
| Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 546 | int mpi_add_int( mpi *X, const mpi *A, t_sint b ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 547 |  | 
|  | 548 | /** | 
| Paul Bakker | 60b1d10 | 2013-10-29 10:02:51 +0100 | [diff] [blame] | 549 | * \brief          Signed subtraction: X = A - b | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 550 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 551 | * \param X        Destination MPI | 
|  | 552 | * \param A        Left-hand MPI | 
|  | 553 | * \param b        The integer value to subtract | 
|  | 554 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 555 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 556 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 557 | */ | 
| Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 558 | int mpi_sub_int( mpi *X, const mpi *A, t_sint b ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 559 |  | 
|  | 560 | /** | 
|  | 561 | * \brief          Baseline multiplication: X = A * B | 
|  | 562 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 563 | * \param X        Destination MPI | 
|  | 564 | * \param A        Left-hand MPI | 
|  | 565 | * \param B        Right-hand MPI | 
|  | 566 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 567 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 568 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 569 | */ | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 570 | int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 571 |  | 
|  | 572 | /** | 
|  | 573 | * \brief          Baseline multiplication: X = A * b | 
| Manuel Pégourié-Gonnard | 3eaa8e7 | 2013-11-25 16:16:33 +0100 | [diff] [blame] | 574 | *                 Note: despite the functon signature, b is treated as a | 
|  | 575 | *                 t_uint.  Negative values of b are treated as large positive | 
|  | 576 | *                 values. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 577 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 578 | * \param X        Destination MPI | 
|  | 579 | * \param A        Left-hand MPI | 
|  | 580 | * \param b        The integer value to multiply with | 
|  | 581 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 582 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 583 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 584 | */ | 
| Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 585 | int mpi_mul_int( mpi *X, const mpi *A, t_sint b ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 586 |  | 
|  | 587 | /** | 
|  | 588 | * \brief          Division by mpi: A = Q * B + R | 
|  | 589 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 590 | * \param Q        Destination MPI for the quotient | 
|  | 591 | * \param R        Destination MPI for the rest value | 
|  | 592 | * \param A        Left-hand MPI | 
|  | 593 | * \param B        Right-hand MPI | 
|  | 594 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 595 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 596 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 597 | *                 POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 598 | * | 
|  | 599 | * \note           Either Q or R can be NULL. | 
|  | 600 | */ | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 601 | int mpi_div_mpi( mpi *Q, mpi *R, const mpi *A, const mpi *B ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 602 |  | 
|  | 603 | /** | 
|  | 604 | * \brief          Division by int: A = Q * b + R | 
|  | 605 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 606 | * \param Q        Destination MPI for the quotient | 
|  | 607 | * \param R        Destination MPI for the rest value | 
|  | 608 | * \param A        Left-hand MPI | 
|  | 609 | * \param b        Integer to divide by | 
|  | 610 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 611 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 612 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 613 | *                 POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 614 | * | 
|  | 615 | * \note           Either Q or R can be NULL. | 
|  | 616 | */ | 
| Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 617 | int mpi_div_int( mpi *Q, mpi *R, const mpi *A, t_sint b ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 618 |  | 
|  | 619 | /** | 
|  | 620 | * \brief          Modulo: R = A mod B | 
|  | 621 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 622 | * \param R        Destination MPI for the rest value | 
|  | 623 | * \param A        Left-hand MPI | 
|  | 624 | * \param B        Right-hand MPI | 
|  | 625 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 626 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 627 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, | 
| Paul Bakker | ce40a6d | 2009-06-23 19:46:08 +0000 | [diff] [blame] | 628 | *                 POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0, | 
|  | 629 | *                 POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 630 | */ | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 631 | int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 632 |  | 
|  | 633 | /** | 
|  | 634 | * \brief          Modulo: r = A mod b | 
|  | 635 | * | 
| Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 636 | * \param r        Destination t_uint | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 637 | * \param A        Left-hand MPI | 
|  | 638 | * \param b        Integer to divide by | 
|  | 639 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 640 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 641 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, | 
| Paul Bakker | ce40a6d | 2009-06-23 19:46:08 +0000 | [diff] [blame] | 642 | *                 POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0, | 
|  | 643 | *                 POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 644 | */ | 
| Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 645 | int mpi_mod_int( t_uint *r, const mpi *A, t_sint b ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 646 |  | 
|  | 647 | /** | 
|  | 648 | * \brief          Sliding-window exponentiation: X = A^E mod N | 
|  | 649 | * | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 650 | * \param X        Destination MPI | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 651 | * \param A        Left-hand MPI | 
|  | 652 | * \param E        Exponent MPI | 
|  | 653 | * \param N        Modular MPI | 
|  | 654 | * \param _RR      Speed-up MPI used for recalculations | 
|  | 655 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 656 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 657 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 658 | *                 POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or | 
|  | 659 | *                 if E is negative | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 660 | * | 
|  | 661 | * \note           _RR is used to avoid re-computing R*R mod N across | 
|  | 662 | *                 multiple calls, which speeds up things a bit. It can | 
|  | 663 | *                 be set to NULL if the extra performance is unneeded. | 
|  | 664 | */ | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 665 | int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 666 |  | 
|  | 667 | /** | 
| Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 668 | * \brief          Fill an MPI X with size bytes of random | 
|  | 669 | * | 
|  | 670 | * \param X        Destination MPI | 
|  | 671 | * \param size     Size in bytes | 
|  | 672 | * \param f_rng    RNG function | 
|  | 673 | * \param p_rng    RNG parameter | 
|  | 674 | * | 
|  | 675 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 676 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 677 | */ | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 678 | int mpi_fill_random( mpi *X, size_t size, | 
|  | 679 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 680 | void *p_rng ); | 
| Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 681 |  | 
|  | 682 | /** | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 683 | * \brief          Greatest common divisor: G = gcd(A, B) | 
|  | 684 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 685 | * \param G        Destination MPI | 
|  | 686 | * \param A        Left-hand MPI | 
|  | 687 | * \param B        Right-hand MPI | 
|  | 688 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 689 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 690 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 691 | */ | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 692 | int mpi_gcd( mpi *G, const mpi *A, const mpi *B ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 693 |  | 
|  | 694 | /** | 
|  | 695 | * \brief          Modular inverse: X = A^-1 mod N | 
|  | 696 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 697 | * \param X        Destination MPI | 
|  | 698 | * \param A        Left-hand MPI | 
|  | 699 | * \param N        Right-hand MPI | 
|  | 700 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 701 | * \return         0 if successful, | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 702 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 703 | *                 POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 704 | POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 705 | */ | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 706 | int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 707 |  | 
|  | 708 | /** | 
|  | 709 | * \brief          Miller-Rabin primality test | 
|  | 710 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 711 | * \param X        MPI to check | 
|  | 712 | * \param f_rng    RNG function | 
|  | 713 | * \param p_rng    RNG parameter | 
|  | 714 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 715 | * \return         0 if successful (probably prime), | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 716 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 717 | *                 POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 718 | */ | 
| Paul Bakker | 45f457d | 2013-11-25 14:26:52 +0100 | [diff] [blame] | 719 | int mpi_is_prime( mpi *X, | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 720 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 721 | void *p_rng ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 722 |  | 
|  | 723 | /** | 
|  | 724 | * \brief          Prime number generation | 
|  | 725 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 726 | * \param X        Destination MPI | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 727 | * \param nbits    Required size of X in bits | 
|  | 728 | *                 ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS ) | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 729 | * \param dh_flag  If 1, then (X-1)/2 will be prime too | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 730 | * \param f_rng    RNG function | 
|  | 731 | * \param p_rng    RNG parameter | 
|  | 732 | * | 
|  | 733 | * \return         0 if successful (probably prime), | 
| Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 734 | *                 POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 735 | *                 POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 736 | */ | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 737 | int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag, | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 738 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 739 | void *p_rng ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 740 |  | 
|  | 741 | /** | 
|  | 742 | * \brief          Checkup routine | 
|  | 743 | * | 
|  | 744 | * \return         0 if successful, or 1 if the test failed | 
|  | 745 | */ | 
|  | 746 | int mpi_self_test( int verbose ); | 
|  | 747 |  | 
|  | 748 | #ifdef __cplusplus | 
|  | 749 | } | 
|  | 750 | #endif | 
|  | 751 |  | 
|  | 752 | #endif /* bignum.h */ |