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 | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2010, 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 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 33 | #define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */ |
| 34 | #define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */ |
| 35 | #define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */ |
Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame^] | 36 | #define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write too. */ |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 37 | #define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */ |
| 38 | #define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */ |
| 39 | #define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 40 | |
| 41 | #define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup |
| 42 | |
| 43 | /* |
Paul Bakker | f968857 | 2011-05-05 10:00:45 +0000 | [diff] [blame] | 44 | * Maximum size MPIs are allowed to grow to in number of limbs. |
| 45 | */ |
| 46 | #define POLARSSL_MPI_MAX_LIMBS 10000 |
| 47 | |
| 48 | /* |
Paul Bakker | b6d5f08 | 2011-11-25 11:52:11 +0000 | [diff] [blame] | 49 | * Maximum window size used for modular exponentiation. Default: 6 |
| 50 | * Minimum value: 1. Maximum value: 6. |
| 51 | * |
| 52 | * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used |
| 53 | * for the sliding window calculation. (So 64 by default) |
| 54 | * |
| 55 | * Reduction in size, reduces speed. |
| 56 | */ |
| 57 | #define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */ |
| 58 | |
| 59 | /* |
Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 60 | * Maximum size of MPIs allowed in bits and bytes for user-MPIs. |
| 61 | * ( Default: 512 bytes => 4096 bits ) |
| 62 | * |
| 63 | * Note: Calculations can results temporarily in larger MPIs. So the number |
| 64 | * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher. |
| 65 | */ |
| 66 | #define POLARSSL_MPI_MAX_SIZE 512 /**< Maximum number of bytes for usable MPIs. */ |
| 67 | #define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */ |
| 68 | |
| 69 | /* |
Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame^] | 70 | * When reading from files with mpi_read_file() the buffer should have space |
| 71 | * for a (short) label, the MPI (in the provided radix), the newline |
| 72 | * characters and the '\0'. |
| 73 | * |
| 74 | * By default we assume at least a 10 char label, a minimum radix of 10 |
| 75 | * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars). |
| 76 | */ |
| 77 | #define POLARSSL_MPI_READ_BUFFER_SIZE 1250 |
| 78 | |
| 79 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 80 | * Define the base integer type, architecture-wise |
| 81 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 82 | #if defined(POLARSSL_HAVE_INT8) |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 83 | typedef signed char t_sint; |
| 84 | typedef unsigned char t_uint; |
| 85 | typedef unsigned short t_udbl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 86 | #else |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 87 | #if defined(POLARSSL_HAVE_INT16) |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 88 | typedef signed short t_sint; |
| 89 | typedef unsigned short t_uint; |
| 90 | typedef unsigned long t_udbl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 91 | #else |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 92 | typedef signed long t_sint; |
| 93 | typedef unsigned long t_uint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 94 | #if defined(_MSC_VER) && defined(_M_IX86) |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 95 | typedef unsigned __int64 t_udbl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 | #else |
| 97 | #if defined(__amd64__) || defined(__x86_64__) || \ |
| 98 | defined(__ppc64__) || defined(__powerpc64__) || \ |
Paul Bakker | 4463740 | 2011-11-26 09:23:07 +0000 | [diff] [blame] | 99 | defined(__ia64__) || defined(__alpha__) || \ |
| 100 | (defined(__sparc__) && defined(__arch64__)) || \ |
| 101 | defined(__s390x__) |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 102 | typedef unsigned int t_udbl __attribute__((mode(TI))); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 103 | #else |
Paul Bakker | 1a9382e | 2009-07-11 16:35:32 +0000 | [diff] [blame] | 104 | #if defined(POLARSSL_HAVE_LONGLONG) |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 105 | typedef unsigned long long t_udbl; |
Paul Bakker | 1a9382e | 2009-07-11 16:35:32 +0000 | [diff] [blame] | 106 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 107 | #endif |
| 108 | #endif |
| 109 | #endif |
| 110 | #endif |
| 111 | |
| 112 | /** |
| 113 | * \brief MPI structure |
| 114 | */ |
| 115 | typedef struct |
| 116 | { |
| 117 | int s; /*!< integer sign */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 118 | size_t n; /*!< total # of limbs */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 119 | t_uint *p; /*!< pointer to limbs */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 | } |
| 121 | mpi; |
| 122 | |
| 123 | #ifdef __cplusplus |
| 124 | extern "C" { |
| 125 | #endif |
| 126 | |
| 127 | /** |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 128 | * \brief Initialize one MPI |
| 129 | * |
| 130 | * \param X One MPI to initialize. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 | */ |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 132 | void mpi_init( mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 133 | |
| 134 | /** |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 135 | * \brief Unallocate one MPI |
| 136 | * |
| 137 | * \param X One MPI to unallocate. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 138 | */ |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 139 | void mpi_free( mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 140 | |
| 141 | /** |
| 142 | * \brief Enlarge to the specified number of limbs |
| 143 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 144 | * \param X MPI to grow |
| 145 | * \param nblimbs The target number of limbs |
| 146 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 147 | * \return 0 if successful, |
| 148 | * 1 if memory allocation failed |
| 149 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 150 | int mpi_grow( mpi *X, size_t nblimbs ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * \brief Copy the contents of Y into X |
| 154 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 155 | * \param X Destination MPI |
| 156 | * \param Y Source MPI |
| 157 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 158 | * \return 0 if successful, |
| 159 | * 1 if memory allocation failed |
| 160 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 161 | int mpi_copy( mpi *X, const mpi *Y ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 162 | |
| 163 | /** |
| 164 | * \brief Swap the contents of X and Y |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 165 | * |
| 166 | * \param X First MPI value |
| 167 | * \param Y Second MPI value |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 168 | */ |
| 169 | void mpi_swap( mpi *X, mpi *Y ); |
| 170 | |
| 171 | /** |
| 172 | * \brief Set value from integer |
| 173 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 174 | * \param X MPI to set |
| 175 | * \param z Value to use |
| 176 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 177 | * \return 0 if successful, |
| 178 | * 1 if memory allocation failed |
| 179 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 180 | int mpi_lset( mpi *X, t_sint z ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 181 | |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 182 | /* |
| 183 | * \brief Get a specific bit from X |
| 184 | * |
| 185 | * \param X MPI to use |
| 186 | * \param pos Zero-based index of the bit in X |
| 187 | * |
| 188 | * \return Either a 0 or a 1 |
| 189 | */ |
| 190 | int mpi_get_bit( mpi *X, size_t pos ); |
| 191 | |
| 192 | /* |
| 193 | * \brief Set a bit of X to a specific value of 0 or 1 |
| 194 | * |
| 195 | * \note Will grow X if necessary to set a bit to 1 in a not yet |
| 196 | * existing limb. Will not grow if bit should be set to 0 |
| 197 | * |
| 198 | * \param X MPI to use |
| 199 | * \param pos Zero-based index of the bit in X |
| 200 | * \param val The value to set the bit to (0 or 1) |
| 201 | * |
| 202 | * \return 0 if successful, |
| 203 | * 1 if memory allocation failed, |
| 204 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1 |
| 205 | */ |
| 206 | int mpi_set_bit( mpi *X, size_t pos, unsigned char val ); |
| 207 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 208 | /** |
| 209 | * \brief Return the number of least significant bits |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 210 | * |
| 211 | * \param X MPI to use |
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 | size_t mpi_lsb( const mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 214 | |
| 215 | /** |
| 216 | * \brief Return the number of most significant bits |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 217 | * |
| 218 | * \param X MPI to use |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 219 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 220 | size_t mpi_msb( const mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 221 | |
| 222 | /** |
| 223 | * \brief Return the total size in bytes |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 224 | * |
| 225 | * \param X MPI to use |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 226 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 227 | size_t mpi_size( const mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 228 | |
| 229 | /** |
| 230 | * \brief Import from an ASCII string |
| 231 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 232 | * \param X Destination MPI |
| 233 | * \param radix Input numeric base |
| 234 | * \param s Null-terminated string buffer |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 235 | * |
Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame^] | 236 | * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 237 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 238 | int mpi_read_string( mpi *X, int radix, const char *s ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 239 | |
| 240 | /** |
| 241 | * \brief Export into an ASCII string |
| 242 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 243 | * \param X Source MPI |
| 244 | * \param radix Output numeric base |
| 245 | * \param s String buffer |
| 246 | * \param slen String buffer size |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 247 | * |
Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame^] | 248 | * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code. |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 249 | * *slen is always updated to reflect the amount |
| 250 | * of data that has (or would have) been written. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 251 | * |
| 252 | * \note Call this function with *slen = 0 to obtain the |
| 253 | * minimum required buffer size in *slen. |
| 254 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 255 | 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] | 256 | |
| 257 | /** |
| 258 | * \brief Read X from an opened file |
| 259 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 260 | * \param X Destination MPI |
| 261 | * \param radix Input numeric base |
| 262 | * \param fin Input file handle |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 263 | * |
Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame^] | 264 | * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if |
| 265 | * the file read buffer is too small or a |
| 266 | * POLARSSL_ERR_MPI_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 267 | */ |
| 268 | int mpi_read_file( mpi *X, int radix, FILE *fin ); |
| 269 | |
| 270 | /** |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 271 | * \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] | 272 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 273 | * \param p Prefix, can be NULL |
| 274 | * \param X Source MPI |
| 275 | * \param radix Output numeric base |
| 276 | * \param fout Output file handle (can be NULL) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 277 | * |
Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame^] | 278 | * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 279 | * |
| 280 | * \note Set fout == NULL to print X on the console. |
| 281 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 282 | int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 283 | |
| 284 | /** |
| 285 | * \brief Import X from unsigned binary data, big endian |
| 286 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 287 | * \param X Destination MPI |
| 288 | * \param buf Input buffer |
| 289 | * \param buflen Input buffer size |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 290 | * |
| 291 | * \return 0 if successful, |
| 292 | * 1 if memory allocation failed |
| 293 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 294 | 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] | 295 | |
| 296 | /** |
| 297 | * \brief Export X into unsigned binary data, big endian |
| 298 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 299 | * \param X Source MPI |
| 300 | * \param buf Output buffer |
| 301 | * \param buflen Output buffer size |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 302 | * |
| 303 | * \return 0 if successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 304 | * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 305 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 306 | 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] | 307 | |
| 308 | /** |
| 309 | * \brief Left-shift: X <<= count |
| 310 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 311 | * \param X MPI to shift |
| 312 | * \param count Amount to shift |
| 313 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 314 | * \return 0 if successful, |
| 315 | * 1 if memory allocation failed |
| 316 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 317 | int mpi_shift_l( mpi *X, size_t count ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 318 | |
| 319 | /** |
| 320 | * \brief Right-shift: X >>= count |
| 321 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 322 | * \param X MPI to shift |
| 323 | * \param count Amount to shift |
| 324 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 325 | * \return 0 if successful, |
| 326 | * 1 if memory allocation failed |
| 327 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 328 | int mpi_shift_r( mpi *X, size_t count ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 329 | |
| 330 | /** |
| 331 | * \brief Compare unsigned values |
| 332 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 333 | * \param X Left-hand MPI |
| 334 | * \param Y Right-hand MPI |
| 335 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 336 | * \return 1 if |X| is greater than |Y|, |
| 337 | * -1 if |X| is lesser than |Y| or |
| 338 | * 0 if |X| is equal to |Y| |
| 339 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 340 | int mpi_cmp_abs( const mpi *X, const mpi *Y ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 341 | |
| 342 | /** |
| 343 | * \brief Compare signed values |
| 344 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 345 | * \param X Left-hand MPI |
| 346 | * \param Y Right-hand MPI |
| 347 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 348 | * \return 1 if X is greater than Y, |
| 349 | * -1 if X is lesser than Y or |
| 350 | * 0 if X is equal to Y |
| 351 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 352 | int mpi_cmp_mpi( const mpi *X, const mpi *Y ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 353 | |
| 354 | /** |
| 355 | * \brief Compare signed values |
| 356 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 357 | * \param X Left-hand MPI |
| 358 | * \param z The integer value to compare to |
| 359 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 360 | * \return 1 if X is greater than z, |
| 361 | * -1 if X is lesser than z or |
| 362 | * 0 if X is equal to z |
| 363 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 364 | int mpi_cmp_int( const mpi *X, t_sint z ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 365 | |
| 366 | /** |
| 367 | * \brief Unsigned addition: X = |A| + |B| |
| 368 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 369 | * \param X Destination MPI |
| 370 | * \param A Left-hand MPI |
| 371 | * \param B Right-hand MPI |
| 372 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 373 | * \return 0 if successful, |
| 374 | * 1 if memory allocation failed |
| 375 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 376 | int mpi_add_abs( mpi *X, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 377 | |
| 378 | /** |
| 379 | * \brief Unsigned substraction: X = |A| - |B| |
| 380 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 381 | * \param X Destination MPI |
| 382 | * \param A Left-hand MPI |
| 383 | * \param B Right-hand MPI |
| 384 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 385 | * \return 0 if successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 386 | * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 387 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 388 | int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 389 | |
| 390 | /** |
| 391 | * \brief Signed addition: X = A + B |
| 392 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 393 | * \param X Destination MPI |
| 394 | * \param A Left-hand MPI |
| 395 | * \param B Right-hand MPI |
| 396 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 397 | * \return 0 if successful, |
| 398 | * 1 if memory allocation failed |
| 399 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 400 | int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 401 | |
| 402 | /** |
| 403 | * \brief Signed substraction: X = A - B |
| 404 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 405 | * \param X Destination MPI |
| 406 | * \param A Left-hand MPI |
| 407 | * \param B Right-hand MPI |
| 408 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 409 | * \return 0 if successful, |
| 410 | * 1 if memory allocation failed |
| 411 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 412 | int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 413 | |
| 414 | /** |
| 415 | * \brief Signed addition: X = A + b |
| 416 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 417 | * \param X Destination MPI |
| 418 | * \param A Left-hand MPI |
| 419 | * \param b The integer value to add |
| 420 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 421 | * \return 0 if successful, |
| 422 | * 1 if memory allocation failed |
| 423 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 424 | int mpi_add_int( mpi *X, const mpi *A, t_sint b ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 425 | |
| 426 | /** |
| 427 | * \brief Signed substraction: X = A - b |
| 428 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 429 | * \param X Destination MPI |
| 430 | * \param A Left-hand MPI |
| 431 | * \param b The integer value to subtract |
| 432 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 433 | * \return 0 if successful, |
| 434 | * 1 if memory allocation failed |
| 435 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 436 | int mpi_sub_int( mpi *X, const mpi *A, t_sint b ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 437 | |
| 438 | /** |
| 439 | * \brief Baseline multiplication: X = A * B |
| 440 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 441 | * \param X Destination MPI |
| 442 | * \param A Left-hand MPI |
| 443 | * \param B Right-hand MPI |
| 444 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 445 | * \return 0 if successful, |
| 446 | * 1 if memory allocation failed |
| 447 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 448 | int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 449 | |
| 450 | /** |
| 451 | * \brief Baseline multiplication: X = A * b |
Paul Bakker | ce40a6d | 2009-06-23 19:46:08 +0000 | [diff] [blame] | 452 | * Note: b is an unsigned integer type, thus |
| 453 | * Negative values of b are ignored. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 454 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 455 | * \param X Destination MPI |
| 456 | * \param A Left-hand MPI |
| 457 | * \param b The integer value to multiply with |
| 458 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 459 | * \return 0 if successful, |
| 460 | * 1 if memory allocation failed |
| 461 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 462 | int mpi_mul_int( mpi *X, const mpi *A, t_sint b ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 463 | |
| 464 | /** |
| 465 | * \brief Division by mpi: A = Q * B + R |
| 466 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 467 | * \param Q Destination MPI for the quotient |
| 468 | * \param R Destination MPI for the rest value |
| 469 | * \param A Left-hand MPI |
| 470 | * \param B Right-hand MPI |
| 471 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 472 | * \return 0 if successful, |
| 473 | * 1 if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 474 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 475 | * |
| 476 | * \note Either Q or R can be NULL. |
| 477 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 478 | 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] | 479 | |
| 480 | /** |
| 481 | * \brief Division by int: A = Q * b + R |
| 482 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 483 | * \param Q Destination MPI for the quotient |
| 484 | * \param R Destination MPI for the rest value |
| 485 | * \param A Left-hand MPI |
| 486 | * \param b Integer to divide by |
| 487 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 488 | * \return 0 if successful, |
| 489 | * 1 if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 490 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 491 | * |
| 492 | * \note Either Q or R can be NULL. |
| 493 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 494 | 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] | 495 | |
| 496 | /** |
| 497 | * \brief Modulo: R = A mod B |
| 498 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 499 | * \param R Destination MPI for the rest value |
| 500 | * \param A Left-hand MPI |
| 501 | * \param B Right-hand MPI |
| 502 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 503 | * \return 0 if successful, |
| 504 | * 1 if memory allocation failed, |
Paul Bakker | ce40a6d | 2009-06-23 19:46:08 +0000 | [diff] [blame] | 505 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0, |
| 506 | * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 507 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 508 | int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 509 | |
| 510 | /** |
| 511 | * \brief Modulo: r = A mod b |
| 512 | * |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 513 | * \param r Destination t_uint |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 514 | * \param A Left-hand MPI |
| 515 | * \param b Integer to divide by |
| 516 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 517 | * \return 0 if successful, |
| 518 | * 1 if memory allocation failed, |
Paul Bakker | ce40a6d | 2009-06-23 19:46:08 +0000 | [diff] [blame] | 519 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0, |
| 520 | * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 521 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 522 | 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] | 523 | |
| 524 | /** |
| 525 | * \brief Sliding-window exponentiation: X = A^E mod N |
| 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 E Exponent MPI |
| 530 | * \param N Modular MPI |
| 531 | * \param _RR Speed-up MPI used for recalculations |
| 532 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 533 | * \return 0 if successful, |
| 534 | * 1 if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 535 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 536 | * |
| 537 | * \note _RR is used to avoid re-computing R*R mod N across |
| 538 | * multiple calls, which speeds up things a bit. It can |
| 539 | * be set to NULL if the extra performance is unneeded. |
| 540 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 541 | 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] | 542 | |
| 543 | /** |
Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 544 | * \brief Fill an MPI X with size bytes of random |
| 545 | * |
| 546 | * \param X Destination MPI |
| 547 | * \param size Size in bytes |
| 548 | * \param f_rng RNG function |
| 549 | * \param p_rng RNG parameter |
| 550 | * |
| 551 | * \return 0 if successful, |
| 552 | * 1 if memory allocation failed |
| 553 | */ |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 554 | int mpi_fill_random( mpi *X, size_t size, |
| 555 | int (*f_rng)(void *, unsigned char *, size_t), |
| 556 | void *p_rng ); |
Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 557 | |
| 558 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 559 | * \brief Greatest common divisor: G = gcd(A, B) |
| 560 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 561 | * \param G Destination MPI |
| 562 | * \param A Left-hand MPI |
| 563 | * \param B Right-hand MPI |
| 564 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 565 | * \return 0 if successful, |
| 566 | * 1 if memory allocation failed |
| 567 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 568 | int mpi_gcd( mpi *G, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 569 | |
| 570 | /** |
| 571 | * \brief Modular inverse: X = A^-1 mod N |
| 572 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 573 | * \param X Destination MPI |
| 574 | * \param A Left-hand MPI |
| 575 | * \param N Right-hand MPI |
| 576 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 577 | * \return 0 if successful, |
| 578 | * 1 if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 579 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 580 | POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 581 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 582 | int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 583 | |
| 584 | /** |
| 585 | * \brief Miller-Rabin primality test |
| 586 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 587 | * \param X MPI to check |
| 588 | * \param f_rng RNG function |
| 589 | * \param p_rng RNG parameter |
| 590 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 591 | * \return 0 if successful (probably prime), |
| 592 | * 1 if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 593 | * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 594 | */ |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 595 | int mpi_is_prime( mpi *X, |
| 596 | int (*f_rng)(void *, unsigned char *, size_t), |
| 597 | void *p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 598 | |
| 599 | /** |
| 600 | * \brief Prime number generation |
| 601 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 602 | * \param X Destination MPI |
Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 603 | * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS ) |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 604 | * \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] | 605 | * \param f_rng RNG function |
| 606 | * \param p_rng RNG parameter |
| 607 | * |
| 608 | * \return 0 if successful (probably prime), |
| 609 | * 1 if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 610 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 611 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 612 | int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 613 | int (*f_rng)(void *, unsigned char *, size_t), |
| 614 | void *p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 615 | |
| 616 | /** |
| 617 | * \brief Checkup routine |
| 618 | * |
| 619 | * \return 0 if successful, or 1 if the test failed |
| 620 | */ |
| 621 | int mpi_self_test( int verbose ); |
| 622 | |
| 623 | #ifdef __cplusplus |
| 624 | } |
| 625 | #endif |
| 626 | |
| 627 | #endif /* bignum.h */ |