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