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 | * |
| 4 | * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine |
| 5 | * |
Paul Bakker | 785a9ee | 2009-01-25 14:15:10 +0000 | [diff] [blame] | 6 | * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org> |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 21 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 22 | #ifndef POLARSSL_BIGNUM_H |
| 23 | #define POLARSSL_BIGNUM_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 24 | |
| 25 | #include <stdio.h> |
| 26 | |
Paul Bakker | b5bf176 | 2009-07-19 20:28:35 +0000 | [diff] [blame] | 27 | #define POLARSSL_ERR_MPI_FILE_IO_ERROR 0x0002 |
| 28 | #define POLARSSL_ERR_MPI_BAD_INPUT_DATA 0x0004 |
| 29 | #define POLARSSL_ERR_MPI_INVALID_CHARACTER 0x0006 |
| 30 | #define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL 0x0008 |
| 31 | #define POLARSSL_ERR_MPI_NEGATIVE_VALUE 0x000A |
| 32 | #define POLARSSL_ERR_MPI_DIVISION_BY_ZERO 0x000C |
| 33 | #define POLARSSL_ERR_MPI_NOT_ACCEPTABLE 0x000E |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 34 | |
| 35 | #define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup |
| 36 | |
| 37 | /* |
| 38 | * Define the base integer type, architecture-wise |
| 39 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 40 | #if defined(POLARSSL_HAVE_INT8) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 41 | typedef unsigned char t_int; |
| 42 | typedef unsigned short t_dbl; |
| 43 | #else |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 44 | #if defined(POLARSSL_HAVE_INT16) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 45 | typedef unsigned short t_int; |
| 46 | typedef unsigned long t_dbl; |
| 47 | #else |
| 48 | typedef unsigned long t_int; |
| 49 | #if defined(_MSC_VER) && defined(_M_IX86) |
| 50 | typedef unsigned __int64 t_dbl; |
| 51 | #else |
| 52 | #if defined(__amd64__) || defined(__x86_64__) || \ |
| 53 | defined(__ppc64__) || defined(__powerpc64__) || \ |
| 54 | defined(__ia64__) || defined(__alpha__) |
| 55 | typedef unsigned int t_dbl __attribute__((mode(TI))); |
| 56 | #else |
Paul Bakker | 1a9382e | 2009-07-11 16:35:32 +0000 | [diff] [blame] | 57 | #if defined(POLARSSL_HAVE_LONGLONG) |
| 58 | typedef unsigned long long t_dbl; |
| 59 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 60 | #endif |
| 61 | #endif |
| 62 | #endif |
| 63 | #endif |
| 64 | |
| 65 | /** |
| 66 | * \brief MPI structure |
| 67 | */ |
| 68 | typedef struct |
| 69 | { |
| 70 | int s; /*!< integer sign */ |
| 71 | int n; /*!< total # of limbs */ |
| 72 | t_int *p; /*!< pointer to limbs */ |
| 73 | } |
| 74 | mpi; |
| 75 | |
| 76 | #ifdef __cplusplus |
| 77 | extern "C" { |
| 78 | #endif |
| 79 | |
| 80 | /** |
| 81 | * \brief Initialize one or more mpi |
| 82 | */ |
| 83 | void mpi_init( mpi *X, ... ); |
| 84 | |
| 85 | /** |
| 86 | * \brief Unallocate one or more mpi |
| 87 | */ |
| 88 | void mpi_free( mpi *X, ... ); |
| 89 | |
| 90 | /** |
| 91 | * \brief Enlarge to the specified number of limbs |
| 92 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 93 | * \param X MPI to grow |
| 94 | * \param nblimbs The target number of limbs |
| 95 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 | * \return 0 if successful, |
| 97 | * 1 if memory allocation failed |
| 98 | */ |
| 99 | int mpi_grow( mpi *X, int nblimbs ); |
| 100 | |
| 101 | /** |
| 102 | * \brief Copy the contents of Y into X |
| 103 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 104 | * \param X Destination MPI |
| 105 | * \param Y Source MPI |
| 106 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 107 | * \return 0 if successful, |
| 108 | * 1 if memory allocation failed |
| 109 | */ |
| 110 | int mpi_copy( mpi *X, mpi *Y ); |
| 111 | |
| 112 | /** |
| 113 | * \brief Swap the contents of X and Y |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 114 | * |
| 115 | * \param X First MPI value |
| 116 | * \param Y Second MPI value |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 117 | */ |
| 118 | void mpi_swap( mpi *X, mpi *Y ); |
| 119 | |
| 120 | /** |
| 121 | * \brief Set value from integer |
| 122 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 123 | * \param X MPI to set |
| 124 | * \param z Value to use |
| 125 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 126 | * \return 0 if successful, |
| 127 | * 1 if memory allocation failed |
| 128 | */ |
| 129 | int mpi_lset( mpi *X, int z ); |
| 130 | |
| 131 | /** |
| 132 | * \brief Return the number of least significant bits |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 133 | * |
| 134 | * \param X MPI to use |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 135 | */ |
| 136 | int mpi_lsb( mpi *X ); |
| 137 | |
| 138 | /** |
| 139 | * \brief Return the number of most significant bits |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 140 | * |
| 141 | * \param X MPI to use |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 142 | */ |
| 143 | int mpi_msb( mpi *X ); |
| 144 | |
| 145 | /** |
| 146 | * \brief Return the total size in bytes |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 147 | * |
| 148 | * \param X MPI to use |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 149 | */ |
| 150 | int mpi_size( mpi *X ); |
| 151 | |
| 152 | /** |
| 153 | * \brief Import from an ASCII string |
| 154 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 155 | * \param X Destination MPI |
| 156 | * \param radix Input numeric base |
| 157 | * \param s Null-terminated string buffer |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 158 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 159 | * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 160 | */ |
| 161 | int mpi_read_string( mpi *X, int radix, char *s ); |
| 162 | |
| 163 | /** |
| 164 | * \brief Export into an ASCII string |
| 165 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 166 | * \param X Source MPI |
| 167 | * \param radix Output numeric base |
| 168 | * \param s String buffer |
| 169 | * \param slen String buffer size |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 170 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 171 | * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 172 | * |
| 173 | * \note Call this function with *slen = 0 to obtain the |
| 174 | * minimum required buffer size in *slen. |
| 175 | */ |
| 176 | int mpi_write_string( mpi *X, int radix, char *s, int *slen ); |
| 177 | |
| 178 | /** |
| 179 | * \brief Read X from an opened file |
| 180 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 181 | * \param X Destination MPI |
| 182 | * \param radix Input numeric base |
| 183 | * \param fin Input file handle |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 184 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 185 | * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 186 | */ |
| 187 | int mpi_read_file( mpi *X, int radix, FILE *fin ); |
| 188 | |
| 189 | /** |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 190 | * \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] | 191 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 192 | * \param p Prefix, can be NULL |
| 193 | * \param X Source MPI |
| 194 | * \param radix Output numeric base |
| 195 | * \param fout Output file handle (can be NULL) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 196 | * |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 197 | * \return 0 if successful, or an POLARSSL_ERR_MPI_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 198 | * |
| 199 | * \note Set fout == NULL to print X on the console. |
| 200 | */ |
| 201 | int mpi_write_file( char *p, mpi *X, int radix, FILE *fout ); |
| 202 | |
| 203 | /** |
| 204 | * \brief Import X from unsigned binary data, big endian |
| 205 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 206 | * \param X Destination MPI |
| 207 | * \param buf Input buffer |
| 208 | * \param buflen Input buffer size |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 209 | * |
| 210 | * \return 0 if successful, |
| 211 | * 1 if memory allocation failed |
| 212 | */ |
| 213 | int mpi_read_binary( mpi *X, unsigned char *buf, int buflen ); |
| 214 | |
| 215 | /** |
| 216 | * \brief Export X into unsigned binary data, big endian |
| 217 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 218 | * \param X Source MPI |
| 219 | * \param buf Output buffer |
| 220 | * \param buflen Output buffer size |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 221 | * |
| 222 | * \return 0 if successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 223 | * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 224 | */ |
| 225 | int mpi_write_binary( mpi *X, unsigned char *buf, int buflen ); |
| 226 | |
| 227 | /** |
| 228 | * \brief Left-shift: X <<= count |
| 229 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 230 | * \param X MPI to shift |
| 231 | * \param count Amount to shift |
| 232 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 233 | * \return 0 if successful, |
| 234 | * 1 if memory allocation failed |
| 235 | */ |
| 236 | int mpi_shift_l( mpi *X, int count ); |
| 237 | |
| 238 | /** |
| 239 | * \brief Right-shift: X >>= count |
| 240 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 241 | * \param X MPI to shift |
| 242 | * \param count Amount to shift |
| 243 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 244 | * \return 0 if successful, |
| 245 | * 1 if memory allocation failed |
| 246 | */ |
| 247 | int mpi_shift_r( mpi *X, int count ); |
| 248 | |
| 249 | /** |
| 250 | * \brief Compare unsigned values |
| 251 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 252 | * \param X Left-hand MPI |
| 253 | * \param Y Right-hand MPI |
| 254 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 255 | * \return 1 if |X| is greater than |Y|, |
| 256 | * -1 if |X| is lesser than |Y| or |
| 257 | * 0 if |X| is equal to |Y| |
| 258 | */ |
| 259 | int mpi_cmp_abs( mpi *X, mpi *Y ); |
| 260 | |
| 261 | /** |
| 262 | * \brief Compare signed values |
| 263 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 264 | * \param X Left-hand MPI |
| 265 | * \param Y Right-hand MPI |
| 266 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 267 | * \return 1 if X is greater than Y, |
| 268 | * -1 if X is lesser than Y or |
| 269 | * 0 if X is equal to Y |
| 270 | */ |
| 271 | int mpi_cmp_mpi( mpi *X, mpi *Y ); |
| 272 | |
| 273 | /** |
| 274 | * \brief Compare signed values |
| 275 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 276 | * \param X Left-hand MPI |
| 277 | * \param z The integer value to compare to |
| 278 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 279 | * \return 1 if X is greater than z, |
| 280 | * -1 if X is lesser than z or |
| 281 | * 0 if X is equal to z |
| 282 | */ |
| 283 | int mpi_cmp_int( mpi *X, int z ); |
| 284 | |
| 285 | /** |
| 286 | * \brief Unsigned addition: X = |A| + |B| |
| 287 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 288 | * \param X Destination MPI |
| 289 | * \param A Left-hand MPI |
| 290 | * \param B Right-hand MPI |
| 291 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 292 | * \return 0 if successful, |
| 293 | * 1 if memory allocation failed |
| 294 | */ |
| 295 | int mpi_add_abs( mpi *X, mpi *A, mpi *B ); |
| 296 | |
| 297 | /** |
| 298 | * \brief Unsigned substraction: X = |A| - |B| |
| 299 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 300 | * \param X Destination MPI |
| 301 | * \param A Left-hand MPI |
| 302 | * \param B Right-hand MPI |
| 303 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 304 | * \return 0 if successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 305 | * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 306 | */ |
| 307 | int mpi_sub_abs( mpi *X, mpi *A, mpi *B ); |
| 308 | |
| 309 | /** |
| 310 | * \brief Signed addition: X = A + B |
| 311 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 312 | * \param X Destination MPI |
| 313 | * \param A Left-hand MPI |
| 314 | * \param B Right-hand MPI |
| 315 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 316 | * \return 0 if successful, |
| 317 | * 1 if memory allocation failed |
| 318 | */ |
| 319 | int mpi_add_mpi( mpi *X, mpi *A, mpi *B ); |
| 320 | |
| 321 | /** |
| 322 | * \brief Signed substraction: X = A - B |
| 323 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 324 | * \param X Destination MPI |
| 325 | * \param A Left-hand MPI |
| 326 | * \param B Right-hand MPI |
| 327 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 328 | * \return 0 if successful, |
| 329 | * 1 if memory allocation failed |
| 330 | */ |
| 331 | int mpi_sub_mpi( mpi *X, mpi *A, mpi *B ); |
| 332 | |
| 333 | /** |
| 334 | * \brief Signed addition: X = A + b |
| 335 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 336 | * \param X Destination MPI |
| 337 | * \param A Left-hand MPI |
| 338 | * \param b The integer value to add |
| 339 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 340 | * \return 0 if successful, |
| 341 | * 1 if memory allocation failed |
| 342 | */ |
| 343 | int mpi_add_int( mpi *X, mpi *A, int b ); |
| 344 | |
| 345 | /** |
| 346 | * \brief Signed substraction: X = A - b |
| 347 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 348 | * \param X Destination MPI |
| 349 | * \param A Left-hand MPI |
| 350 | * \param b The integer value to subtract |
| 351 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 352 | * \return 0 if successful, |
| 353 | * 1 if memory allocation failed |
| 354 | */ |
| 355 | int mpi_sub_int( mpi *X, mpi *A, int b ); |
| 356 | |
| 357 | /** |
| 358 | * \brief Baseline multiplication: X = A * B |
| 359 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 360 | * \param X Destination MPI |
| 361 | * \param A Left-hand MPI |
| 362 | * \param B Right-hand MPI |
| 363 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 364 | * \return 0 if successful, |
| 365 | * 1 if memory allocation failed |
| 366 | */ |
| 367 | int mpi_mul_mpi( mpi *X, mpi *A, mpi *B ); |
| 368 | |
| 369 | /** |
| 370 | * \brief Baseline multiplication: X = A * b |
Paul Bakker | ce40a6d | 2009-06-23 19:46:08 +0000 | [diff] [blame] | 371 | * Note: b is an unsigned integer type, thus |
| 372 | * Negative values of b are ignored. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 373 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 374 | * \param X Destination MPI |
| 375 | * \param A Left-hand MPI |
| 376 | * \param b The integer value to multiply with |
| 377 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 378 | * \return 0 if successful, |
| 379 | * 1 if memory allocation failed |
| 380 | */ |
| 381 | int mpi_mul_int( mpi *X, mpi *A, t_int b ); |
| 382 | |
| 383 | /** |
| 384 | * \brief Division by mpi: A = Q * B + R |
| 385 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 386 | * \param Q Destination MPI for the quotient |
| 387 | * \param R Destination MPI for the rest value |
| 388 | * \param A Left-hand MPI |
| 389 | * \param B Right-hand MPI |
| 390 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 391 | * \return 0 if successful, |
| 392 | * 1 if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 393 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 394 | * |
| 395 | * \note Either Q or R can be NULL. |
| 396 | */ |
| 397 | int mpi_div_mpi( mpi *Q, mpi *R, mpi *A, mpi *B ); |
| 398 | |
| 399 | /** |
| 400 | * \brief Division by int: A = Q * b + R |
| 401 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 402 | * \param Q Destination MPI for the quotient |
| 403 | * \param R Destination MPI for the rest value |
| 404 | * \param A Left-hand MPI |
| 405 | * \param b Integer to divide by |
| 406 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 407 | * \return 0 if successful, |
| 408 | * 1 if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 409 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 410 | * |
| 411 | * \note Either Q or R can be NULL. |
| 412 | */ |
| 413 | int mpi_div_int( mpi *Q, mpi *R, mpi *A, int b ); |
| 414 | |
| 415 | /** |
| 416 | * \brief Modulo: R = A mod B |
| 417 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 418 | * \param R Destination MPI for the rest value |
| 419 | * \param A Left-hand MPI |
| 420 | * \param B Right-hand MPI |
| 421 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 422 | * \return 0 if successful, |
| 423 | * 1 if memory allocation failed, |
Paul Bakker | ce40a6d | 2009-06-23 19:46:08 +0000 | [diff] [blame] | 424 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0, |
| 425 | * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 426 | */ |
| 427 | int mpi_mod_mpi( mpi *R, mpi *A, mpi *B ); |
| 428 | |
| 429 | /** |
| 430 | * \brief Modulo: r = A mod b |
| 431 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 432 | * \param a Destination t_int |
| 433 | * \param A Left-hand MPI |
| 434 | * \param b Integer to divide by |
| 435 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 436 | * \return 0 if successful, |
| 437 | * 1 if memory allocation failed, |
Paul Bakker | ce40a6d | 2009-06-23 19:46:08 +0000 | [diff] [blame] | 438 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0, |
| 439 | * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 440 | */ |
| 441 | int mpi_mod_int( t_int *r, mpi *A, int b ); |
| 442 | |
| 443 | /** |
| 444 | * \brief Sliding-window exponentiation: X = A^E mod N |
| 445 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 446 | * \param X Destination MPI |
| 447 | * \param A Left-hand MPI |
| 448 | * \param E Exponent MPI |
| 449 | * \param N Modular MPI |
| 450 | * \param _RR Speed-up MPI used for recalculations |
| 451 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 452 | * \return 0 if successful, |
| 453 | * 1 if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 454 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 455 | * |
| 456 | * \note _RR is used to avoid re-computing R*R mod N across |
| 457 | * multiple calls, which speeds up things a bit. It can |
| 458 | * be set to NULL if the extra performance is unneeded. |
| 459 | */ |
| 460 | int mpi_exp_mod( mpi *X, mpi *A, mpi *E, mpi *N, mpi *_RR ); |
| 461 | |
| 462 | /** |
| 463 | * \brief Greatest common divisor: G = gcd(A, B) |
| 464 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 465 | * \param G Destination MPI |
| 466 | * \param A Left-hand MPI |
| 467 | * \param B Right-hand MPI |
| 468 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 469 | * \return 0 if successful, |
| 470 | * 1 if memory allocation failed |
| 471 | */ |
| 472 | int mpi_gcd( mpi *G, mpi *A, mpi *B ); |
| 473 | |
| 474 | /** |
| 475 | * \brief Modular inverse: X = A^-1 mod N |
| 476 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 477 | * \param X Destination MPI |
| 478 | * \param A Left-hand MPI |
| 479 | * \param N Right-hand MPI |
| 480 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 481 | * \return 0 if successful, |
| 482 | * 1 if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 483 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 484 | POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 485 | */ |
| 486 | int mpi_inv_mod( mpi *X, mpi *A, mpi *N ); |
| 487 | |
| 488 | /** |
| 489 | * \brief Miller-Rabin primality test |
| 490 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 491 | * \param X MPI to check |
| 492 | * \param f_rng RNG function |
| 493 | * \param p_rng RNG parameter |
| 494 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 495 | * \return 0 if successful (probably prime), |
| 496 | * 1 if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 497 | * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 498 | */ |
| 499 | int mpi_is_prime( mpi *X, int (*f_rng)(void *), void *p_rng ); |
| 500 | |
| 501 | /** |
| 502 | * \brief Prime number generation |
| 503 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame^] | 504 | * \param X Destination MPI |
| 505 | * \param nbits Required size of X in bits |
| 506 | * \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] | 507 | * \param f_rng RNG function |
| 508 | * \param p_rng RNG parameter |
| 509 | * |
| 510 | * \return 0 if successful (probably prime), |
| 511 | * 1 if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 512 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 513 | */ |
| 514 | int mpi_gen_prime( mpi *X, int nbits, int dh_flag, |
| 515 | int (*f_rng)(void *), void *p_rng ); |
| 516 | |
| 517 | /** |
| 518 | * \brief Checkup routine |
| 519 | * |
| 520 | * \return 0 if successful, or 1 if the test failed |
| 521 | */ |
| 522 | int mpi_self_test( int verbose ); |
| 523 | |
| 524 | #ifdef __cplusplus |
| 525 | } |
| 526 | #endif |
| 527 | |
| 528 | #endif /* bignum.h */ |