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