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. */ |
| 36 | #define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The output buffer is too small to write too. */ |
| 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 | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 70 | * Define the base integer type, architecture-wise |
| 71 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 72 | #if defined(POLARSSL_HAVE_INT8) |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 73 | typedef signed char t_sint; |
| 74 | typedef unsigned char t_uint; |
| 75 | typedef unsigned short t_udbl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 76 | #else |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 77 | #if defined(POLARSSL_HAVE_INT16) |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 78 | typedef signed short t_sint; |
| 79 | typedef unsigned short t_uint; |
| 80 | typedef unsigned long t_udbl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 81 | #else |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 82 | typedef signed long t_sint; |
| 83 | typedef unsigned long t_uint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 84 | #if defined(_MSC_VER) && defined(_M_IX86) |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 85 | typedef unsigned __int64 t_udbl; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 86 | #else |
| 87 | #if defined(__amd64__) || defined(__x86_64__) || \ |
| 88 | defined(__ppc64__) || defined(__powerpc64__) || \ |
Paul Bakker | 4463740 | 2011-11-26 09:23:07 +0000 | [diff] [blame] | 89 | defined(__ia64__) || defined(__alpha__) || \ |
| 90 | (defined(__sparc__) && defined(__arch64__)) || \ |
| 91 | defined(__s390x__) |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 92 | typedef unsigned int t_udbl __attribute__((mode(TI))); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 93 | #else |
Paul Bakker | 1a9382e | 2009-07-11 16:35:32 +0000 | [diff] [blame] | 94 | #if defined(POLARSSL_HAVE_LONGLONG) |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 95 | typedef unsigned long long t_udbl; |
Paul Bakker | 1a9382e | 2009-07-11 16:35:32 +0000 | [diff] [blame] | 96 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 97 | #endif |
| 98 | #endif |
| 99 | #endif |
| 100 | #endif |
| 101 | |
| 102 | /** |
| 103 | * \brief MPI structure |
| 104 | */ |
| 105 | typedef struct |
| 106 | { |
| 107 | int s; /*!< integer sign */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 108 | size_t n; /*!< total # of limbs */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 109 | t_uint *p; /*!< pointer to limbs */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 110 | } |
| 111 | mpi; |
| 112 | |
| 113 | #ifdef __cplusplus |
| 114 | extern "C" { |
| 115 | #endif |
| 116 | |
| 117 | /** |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 118 | * \brief Initialize one MPI |
| 119 | * |
| 120 | * \param X One MPI to initialize. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 121 | */ |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 122 | void mpi_init( mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 123 | |
| 124 | /** |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 125 | * \brief Unallocate one MPI |
| 126 | * |
| 127 | * \param X One MPI to unallocate. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 128 | */ |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 129 | void mpi_free( mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 130 | |
| 131 | /** |
| 132 | * \brief Enlarge to the specified number of limbs |
| 133 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 134 | * \param X MPI to grow |
| 135 | * \param nblimbs The target number of limbs |
| 136 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 137 | * \return 0 if successful, |
| 138 | * 1 if memory allocation failed |
| 139 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 140 | int mpi_grow( mpi *X, size_t nblimbs ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * \brief Copy the contents of Y into X |
| 144 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 145 | * \param X Destination MPI |
| 146 | * \param Y Source MPI |
| 147 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 148 | * \return 0 if successful, |
| 149 | * 1 if memory allocation failed |
| 150 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 151 | int mpi_copy( mpi *X, const mpi *Y ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * \brief Swap the contents of X and Y |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 155 | * |
| 156 | * \param X First MPI value |
| 157 | * \param Y Second MPI value |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 158 | */ |
| 159 | void mpi_swap( mpi *X, mpi *Y ); |
| 160 | |
| 161 | /** |
| 162 | * \brief Set value from integer |
| 163 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 164 | * \param X MPI to set |
| 165 | * \param z Value to use |
| 166 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 167 | * \return 0 if successful, |
| 168 | * 1 if memory allocation failed |
| 169 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 170 | int mpi_lset( mpi *X, t_sint z ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 171 | |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 172 | /* |
| 173 | * \brief Get a specific bit from X |
| 174 | * |
| 175 | * \param X MPI to use |
| 176 | * \param pos Zero-based index of the bit in X |
| 177 | * |
| 178 | * \return Either a 0 or a 1 |
| 179 | */ |
| 180 | int mpi_get_bit( mpi *X, size_t pos ); |
| 181 | |
| 182 | /* |
| 183 | * \brief Set a bit of X to a specific value of 0 or 1 |
| 184 | * |
| 185 | * \note Will grow X if necessary to set a bit to 1 in a not yet |
| 186 | * existing limb. Will not grow if bit should be set to 0 |
| 187 | * |
| 188 | * \param X MPI to use |
| 189 | * \param pos Zero-based index of the bit in X |
| 190 | * \param val The value to set the bit to (0 or 1) |
| 191 | * |
| 192 | * \return 0 if successful, |
| 193 | * 1 if memory allocation failed, |
| 194 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1 |
| 195 | */ |
| 196 | int mpi_set_bit( mpi *X, size_t pos, unsigned char val ); |
| 197 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 198 | /** |
| 199 | * \brief Return the number of least significant bits |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 200 | * |
| 201 | * \param X MPI to use |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 202 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 203 | size_t mpi_lsb( const mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 204 | |
| 205 | /** |
| 206 | * \brief Return the number of most significant bits |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 207 | * |
| 208 | * \param X MPI to use |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 209 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 210 | size_t mpi_msb( const mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 211 | |
| 212 | /** |
| 213 | * \brief Return the total size in bytes |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 214 | * |
| 215 | * \param X MPI to use |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 216 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 217 | size_t mpi_size( const mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 218 | |
| 219 | /** |
| 220 | * \brief Import from an ASCII string |
| 221 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 222 | * \param X Destination MPI |
| 223 | * \param radix Input numeric base |
| 224 | * \param s Null-terminated string buffer |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 225 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 226 | * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 227 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 228 | int mpi_read_string( mpi *X, int radix, const char *s ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 229 | |
| 230 | /** |
| 231 | * \brief Export into an ASCII string |
| 232 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 233 | * \param X Source MPI |
| 234 | * \param radix Output numeric base |
| 235 | * \param s String buffer |
| 236 | * \param slen String buffer size |
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 | * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code. |
| 239 | * *slen is always updated to reflect the amount |
| 240 | * of data that has (or would have) been written. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 241 | * |
| 242 | * \note Call this function with *slen = 0 to obtain the |
| 243 | * minimum required buffer size in *slen. |
| 244 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 245 | 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] | 246 | |
| 247 | /** |
| 248 | * \brief Read X from an opened file |
| 249 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 250 | * \param X Destination MPI |
| 251 | * \param radix Input numeric base |
| 252 | * \param fin Input file handle |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 253 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 254 | * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 255 | */ |
| 256 | int mpi_read_file( mpi *X, int radix, FILE *fin ); |
| 257 | |
| 258 | /** |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 259 | * \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] | 260 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 261 | * \param p Prefix, can be NULL |
| 262 | * \param X Source MPI |
| 263 | * \param radix Output numeric base |
| 264 | * \param fout Output file handle (can be NULL) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 265 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 266 | * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 267 | * |
| 268 | * \note Set fout == NULL to print X on the console. |
| 269 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 270 | 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] | 271 | |
| 272 | /** |
| 273 | * \brief Import X from unsigned binary data, big endian |
| 274 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 275 | * \param X Destination MPI |
| 276 | * \param buf Input buffer |
| 277 | * \param buflen Input buffer size |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 278 | * |
| 279 | * \return 0 if successful, |
| 280 | * 1 if memory allocation failed |
| 281 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 282 | 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] | 283 | |
| 284 | /** |
| 285 | * \brief Export X into unsigned binary data, big endian |
| 286 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 287 | * \param X Source MPI |
| 288 | * \param buf Output buffer |
| 289 | * \param buflen Output buffer size |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 290 | * |
| 291 | * \return 0 if successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 292 | * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 293 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 294 | 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] | 295 | |
| 296 | /** |
| 297 | * \brief Left-shift: X <<= count |
| 298 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 299 | * \param X MPI to shift |
| 300 | * \param count Amount to shift |
| 301 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 302 | * \return 0 if successful, |
| 303 | * 1 if memory allocation failed |
| 304 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 305 | int mpi_shift_l( mpi *X, size_t count ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 306 | |
| 307 | /** |
| 308 | * \brief Right-shift: X >>= count |
| 309 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 310 | * \param X MPI to shift |
| 311 | * \param count Amount to shift |
| 312 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 313 | * \return 0 if successful, |
| 314 | * 1 if memory allocation failed |
| 315 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 316 | int mpi_shift_r( mpi *X, size_t count ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 317 | |
| 318 | /** |
| 319 | * \brief Compare unsigned values |
| 320 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 321 | * \param X Left-hand MPI |
| 322 | * \param Y Right-hand MPI |
| 323 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 324 | * \return 1 if |X| is greater than |Y|, |
| 325 | * -1 if |X| is lesser than |Y| or |
| 326 | * 0 if |X| is equal to |Y| |
| 327 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 328 | int mpi_cmp_abs( const mpi *X, const mpi *Y ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 329 | |
| 330 | /** |
| 331 | * \brief Compare signed 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_mpi( 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 z The integer value to compare to |
| 347 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 348 | * \return 1 if X is greater than z, |
| 349 | * -1 if X is lesser than z or |
| 350 | * 0 if X is equal to z |
| 351 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 352 | int mpi_cmp_int( const mpi *X, t_sint z ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 353 | |
| 354 | /** |
| 355 | * \brief Unsigned addition: X = |A| + |B| |
| 356 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 357 | * \param X Destination MPI |
| 358 | * \param A Left-hand MPI |
| 359 | * \param B Right-hand MPI |
| 360 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 361 | * \return 0 if successful, |
| 362 | * 1 if memory allocation failed |
| 363 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 364 | int mpi_add_abs( mpi *X, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 365 | |
| 366 | /** |
| 367 | * \brief Unsigned substraction: 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, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 374 | * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 375 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 376 | int mpi_sub_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 Signed addition: 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, |
| 386 | * 1 if memory allocation failed |
| 387 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 388 | int mpi_add_mpi( 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 substraction: 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_sub_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 addition: 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 The integer value to add |
| 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 | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 412 | int mpi_add_int( mpi *X, const mpi *A, t_sint b ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 413 | |
| 414 | /** |
| 415 | * \brief Signed substraction: 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 subtract |
| 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_sub_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 Baseline multiplication: 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 Right-hand MPI |
| 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 | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 436 | int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 437 | |
| 438 | /** |
| 439 | * \brief Baseline multiplication: X = A * b |
Paul Bakker | ce40a6d | 2009-06-23 19:46:08 +0000 | [diff] [blame] | 440 | * Note: b is an unsigned integer type, thus |
| 441 | * Negative values of b are ignored. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 442 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 443 | * \param X Destination MPI |
| 444 | * \param A Left-hand MPI |
| 445 | * \param b The integer value to multiply with |
| 446 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 447 | * \return 0 if successful, |
| 448 | * 1 if memory allocation failed |
| 449 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 450 | int mpi_mul_int( mpi *X, const mpi *A, t_sint b ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 451 | |
| 452 | /** |
| 453 | * \brief Division by mpi: A = Q * B + R |
| 454 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 455 | * \param Q Destination MPI for the quotient |
| 456 | * \param R Destination MPI for the rest value |
| 457 | * \param A Left-hand MPI |
| 458 | * \param B Right-hand MPI |
| 459 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 460 | * \return 0 if successful, |
| 461 | * 1 if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 462 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 463 | * |
| 464 | * \note Either Q or R can be NULL. |
| 465 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 466 | 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] | 467 | |
| 468 | /** |
| 469 | * \brief Division by int: A = Q * b + R |
| 470 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 471 | * \param Q Destination MPI for the quotient |
| 472 | * \param R Destination MPI for the rest value |
| 473 | * \param A Left-hand MPI |
| 474 | * \param b Integer to divide by |
| 475 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 476 | * \return 0 if successful, |
| 477 | * 1 if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 478 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 479 | * |
| 480 | * \note Either Q or R can be NULL. |
| 481 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 482 | 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] | 483 | |
| 484 | /** |
| 485 | * \brief Modulo: R = A mod B |
| 486 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 487 | * \param R Destination MPI for the rest value |
| 488 | * \param A Left-hand MPI |
| 489 | * \param B Right-hand MPI |
| 490 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 491 | * \return 0 if successful, |
| 492 | * 1 if memory allocation failed, |
Paul Bakker | ce40a6d | 2009-06-23 19:46:08 +0000 | [diff] [blame] | 493 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0, |
| 494 | * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 495 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 496 | int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 497 | |
| 498 | /** |
| 499 | * \brief Modulo: r = A mod b |
| 500 | * |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 501 | * \param r Destination t_uint |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 502 | * \param A Left-hand MPI |
| 503 | * \param b Integer to divide by |
| 504 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 505 | * \return 0 if successful, |
| 506 | * 1 if memory allocation failed, |
Paul Bakker | ce40a6d | 2009-06-23 19:46:08 +0000 | [diff] [blame] | 507 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0, |
| 508 | * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 509 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 510 | 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] | 511 | |
| 512 | /** |
| 513 | * \brief Sliding-window exponentiation: X = A^E mod N |
| 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 E Exponent MPI |
| 518 | * \param N Modular MPI |
| 519 | * \param _RR Speed-up MPI used for recalculations |
| 520 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 521 | * \return 0 if successful, |
| 522 | * 1 if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 523 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 524 | * |
| 525 | * \note _RR is used to avoid re-computing R*R mod N across |
| 526 | * multiple calls, which speeds up things a bit. It can |
| 527 | * be set to NULL if the extra performance is unneeded. |
| 528 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 529 | 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] | 530 | |
| 531 | /** |
Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 532 | * \brief Fill an MPI X with size bytes of random |
| 533 | * |
| 534 | * \param X Destination MPI |
| 535 | * \param size Size in bytes |
| 536 | * \param f_rng RNG function |
| 537 | * \param p_rng RNG parameter |
| 538 | * |
| 539 | * \return 0 if successful, |
| 540 | * 1 if memory allocation failed |
| 541 | */ |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame^] | 542 | int mpi_fill_random( mpi *X, size_t size, |
| 543 | int (*f_rng)(void *, unsigned char *, size_t), |
| 544 | void *p_rng ); |
Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 545 | |
| 546 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 547 | * \brief Greatest common divisor: G = gcd(A, B) |
| 548 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 549 | * \param G Destination MPI |
| 550 | * \param A Left-hand MPI |
| 551 | * \param B Right-hand MPI |
| 552 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 553 | * \return 0 if successful, |
| 554 | * 1 if memory allocation failed |
| 555 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 556 | int mpi_gcd( mpi *G, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 557 | |
| 558 | /** |
| 559 | * \brief Modular inverse: X = A^-1 mod N |
| 560 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 561 | * \param X Destination MPI |
| 562 | * \param A Left-hand MPI |
| 563 | * \param N 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, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 567 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 568 | POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N |
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_inv_mod( mpi *X, const mpi *A, const mpi *N ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 571 | |
| 572 | /** |
| 573 | * \brief Miller-Rabin primality test |
| 574 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 575 | * \param X MPI to check |
| 576 | * \param f_rng RNG function |
| 577 | * \param p_rng RNG parameter |
| 578 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 579 | * \return 0 if successful (probably prime), |
| 580 | * 1 if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 581 | * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 582 | */ |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame^] | 583 | int mpi_is_prime( mpi *X, |
| 584 | int (*f_rng)(void *, unsigned char *, size_t), |
| 585 | void *p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 586 | |
| 587 | /** |
| 588 | * \brief Prime number generation |
| 589 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 590 | * \param X Destination MPI |
Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 591 | * \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] | 592 | * \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] | 593 | * \param f_rng RNG function |
| 594 | * \param p_rng RNG parameter |
| 595 | * |
| 596 | * \return 0 if successful (probably prime), |
| 597 | * 1 if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 598 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 599 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 600 | int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame^] | 601 | int (*f_rng)(void *, unsigned char *, size_t), |
| 602 | void *p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 603 | |
| 604 | /** |
| 605 | * \brief Checkup routine |
| 606 | * |
| 607 | * \return 0 if successful, or 1 if the test failed |
| 608 | */ |
| 609 | int mpi_self_test( int verbose ); |
| 610 | |
| 611 | #ifdef __cplusplus |
| 612 | } |
| 613 | #endif |
| 614 | |
| 615 | #endif /* bignum.h */ |