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 | |
Paul Bakker | 2b6af2f | 2012-10-23 11:08:02 +0000 | [diff] [blame^] | 302 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 303 | /** |
| 304 | * \brief Read X from an opened file |
| 305 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 306 | * \param X Destination MPI |
| 307 | * \param radix Input numeric base |
| 308 | * \param fin Input file handle |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 309 | * |
Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 310 | * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if |
| 311 | * the file read buffer is too small or a |
| 312 | * POLARSSL_ERR_MPI_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 313 | */ |
| 314 | int mpi_read_file( mpi *X, int radix, FILE *fin ); |
| 315 | |
| 316 | /** |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 317 | * \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] | 318 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 319 | * \param p Prefix, can be NULL |
| 320 | * \param X Source MPI |
| 321 | * \param radix Output numeric base |
| 322 | * \param fout Output file handle (can be NULL) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 323 | * |
Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 324 | * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 325 | * |
| 326 | * \note Set fout == NULL to print X on the console. |
| 327 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 328 | int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout ); |
Paul Bakker | 2b6af2f | 2012-10-23 11:08:02 +0000 | [diff] [blame^] | 329 | #endif /* POLARSSL_FS_IO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 330 | |
| 331 | /** |
| 332 | * \brief Import X from unsigned binary data, big endian |
| 333 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 334 | * \param X Destination MPI |
| 335 | * \param buf Input buffer |
| 336 | * \param buflen Input buffer size |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 337 | * |
| 338 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 339 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 340 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 341 | 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] | 342 | |
| 343 | /** |
| 344 | * \brief Export X into unsigned binary data, big endian |
| 345 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 346 | * \param X Source MPI |
| 347 | * \param buf Output buffer |
| 348 | * \param buflen Output buffer size |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 349 | * |
| 350 | * \return 0 if successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 351 | * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 352 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 353 | 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] | 354 | |
| 355 | /** |
| 356 | * \brief Left-shift: X <<= count |
| 357 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 358 | * \param X MPI to shift |
| 359 | * \param count Amount to shift |
| 360 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 361 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 362 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 363 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 364 | int mpi_shift_l( mpi *X, size_t count ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 365 | |
| 366 | /** |
| 367 | * \brief Right-shift: X >>= count |
| 368 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 369 | * \param X MPI to shift |
| 370 | * \param count Amount to shift |
| 371 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 372 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 373 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 374 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 375 | int mpi_shift_r( mpi *X, size_t count ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 376 | |
| 377 | /** |
| 378 | * \brief Compare unsigned values |
| 379 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 380 | * \param X Left-hand MPI |
| 381 | * \param Y Right-hand MPI |
| 382 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 383 | * \return 1 if |X| is greater than |Y|, |
| 384 | * -1 if |X| is lesser than |Y| or |
| 385 | * 0 if |X| is equal to |Y| |
| 386 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 387 | int mpi_cmp_abs( const mpi *X, const mpi *Y ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 388 | |
| 389 | /** |
| 390 | * \brief Compare signed values |
| 391 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 392 | * \param X Left-hand MPI |
| 393 | * \param Y Right-hand MPI |
| 394 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 395 | * \return 1 if X is greater than Y, |
| 396 | * -1 if X is lesser than Y or |
| 397 | * 0 if X is equal to Y |
| 398 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 399 | int mpi_cmp_mpi( const mpi *X, const mpi *Y ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 400 | |
| 401 | /** |
| 402 | * \brief Compare signed values |
| 403 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 404 | * \param X Left-hand MPI |
| 405 | * \param z The integer value to compare to |
| 406 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 407 | * \return 1 if X is greater than z, |
| 408 | * -1 if X is lesser than z or |
| 409 | * 0 if X is equal to z |
| 410 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 411 | int mpi_cmp_int( const mpi *X, t_sint z ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 412 | |
| 413 | /** |
| 414 | * \brief Unsigned addition: X = |A| + |B| |
| 415 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 416 | * \param X Destination MPI |
| 417 | * \param A Left-hand MPI |
| 418 | * \param B Right-hand MPI |
| 419 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 420 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 421 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 422 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 423 | int mpi_add_abs( mpi *X, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 424 | |
| 425 | /** |
| 426 | * \brief Unsigned substraction: X = |A| - |B| |
| 427 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 428 | * \param X Destination MPI |
| 429 | * \param A Left-hand MPI |
| 430 | * \param B Right-hand MPI |
| 431 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 432 | * \return 0 if successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 433 | * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 434 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 435 | int mpi_sub_abs( mpi *X, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 436 | |
| 437 | /** |
| 438 | * \brief Signed addition: X = A + B |
| 439 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 440 | * \param X Destination MPI |
| 441 | * \param A Left-hand MPI |
| 442 | * \param B Right-hand MPI |
| 443 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 444 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 445 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 446 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 447 | int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 448 | |
| 449 | /** |
| 450 | * \brief Signed substraction: X = A - B |
| 451 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 452 | * \param X Destination MPI |
| 453 | * \param A Left-hand MPI |
| 454 | * \param B Right-hand MPI |
| 455 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 456 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 457 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 458 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 459 | int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 460 | |
| 461 | /** |
| 462 | * \brief Signed addition: X = A + b |
| 463 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 464 | * \param X Destination MPI |
| 465 | * \param A Left-hand MPI |
| 466 | * \param b The integer value to add |
| 467 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 468 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 469 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 470 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 471 | int mpi_add_int( mpi *X, const mpi *A, t_sint b ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 472 | |
| 473 | /** |
| 474 | * \brief Signed substraction: X = A - b |
| 475 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 476 | * \param X Destination MPI |
| 477 | * \param A Left-hand MPI |
| 478 | * \param b The integer value to subtract |
| 479 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 480 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 481 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 482 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 483 | int mpi_sub_int( mpi *X, const mpi *A, t_sint b ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 484 | |
| 485 | /** |
| 486 | * \brief Baseline multiplication: X = A * B |
| 487 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 488 | * \param X Destination MPI |
| 489 | * \param A Left-hand MPI |
| 490 | * \param B Right-hand MPI |
| 491 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 492 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 493 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 494 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 495 | int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 496 | |
| 497 | /** |
| 498 | * \brief Baseline multiplication: X = A * b |
Paul Bakker | ce40a6d | 2009-06-23 19:46:08 +0000 | [diff] [blame] | 499 | * Note: b is an unsigned integer type, thus |
| 500 | * Negative values of b are ignored. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 501 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 502 | * \param X Destination MPI |
| 503 | * \param A Left-hand MPI |
| 504 | * \param b The integer value to multiply with |
| 505 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 506 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 507 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 508 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 509 | int mpi_mul_int( mpi *X, const mpi *A, t_sint b ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 510 | |
| 511 | /** |
| 512 | * \brief Division by mpi: A = Q * B + R |
| 513 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 514 | * \param Q Destination MPI for the quotient |
| 515 | * \param R Destination MPI for the rest value |
| 516 | * \param A Left-hand MPI |
| 517 | * \param B Right-hand MPI |
| 518 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 519 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 520 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 521 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 522 | * |
| 523 | * \note Either Q or R can be NULL. |
| 524 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 525 | 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] | 526 | |
| 527 | /** |
| 528 | * \brief Division by int: A = Q * b + R |
| 529 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 530 | * \param Q Destination MPI for the quotient |
| 531 | * \param R Destination MPI for the rest value |
| 532 | * \param A Left-hand MPI |
| 533 | * \param b Integer to divide by |
| 534 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 535 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 536 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 537 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 538 | * |
| 539 | * \note Either Q or R can be NULL. |
| 540 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 541 | 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] | 542 | |
| 543 | /** |
| 544 | * \brief Modulo: R = A mod B |
| 545 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 546 | * \param R Destination MPI for the rest value |
| 547 | * \param A Left-hand MPI |
| 548 | * \param B Right-hand MPI |
| 549 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 550 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 551 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, |
Paul Bakker | ce40a6d | 2009-06-23 19:46:08 +0000 | [diff] [blame] | 552 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0, |
| 553 | * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 554 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 555 | int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 556 | |
| 557 | /** |
| 558 | * \brief Modulo: r = A mod b |
| 559 | * |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 560 | * \param r Destination t_uint |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 561 | * \param A Left-hand MPI |
| 562 | * \param b Integer to divide by |
| 563 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 564 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 565 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, |
Paul Bakker | ce40a6d | 2009-06-23 19:46:08 +0000 | [diff] [blame] | 566 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0, |
| 567 | * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 568 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 569 | 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] | 570 | |
| 571 | /** |
| 572 | * \brief Sliding-window exponentiation: X = A^E mod N |
| 573 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 574 | * \param X Destination MPI |
| 575 | * \param A Left-hand MPI |
| 576 | * \param E Exponent MPI |
| 577 | * \param N Modular MPI |
| 578 | * \param _RR Speed-up MPI used for recalculations |
| 579 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 580 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 581 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, |
Paul Bakker | f6198c1 | 2012-05-16 08:02:29 +0000 | [diff] [blame] | 582 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or if |
| 583 | * E is negative |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 584 | * |
| 585 | * \note _RR is used to avoid re-computing R*R mod N across |
| 586 | * multiple calls, which speeds up things a bit. It can |
| 587 | * be set to NULL if the extra performance is unneeded. |
| 588 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 589 | 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] | 590 | |
| 591 | /** |
Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 592 | * \brief Fill an MPI X with size bytes of random |
| 593 | * |
| 594 | * \param X Destination MPI |
| 595 | * \param size Size in bytes |
| 596 | * \param f_rng RNG function |
| 597 | * \param p_rng RNG parameter |
| 598 | * |
| 599 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 600 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 601 | */ |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 602 | int mpi_fill_random( mpi *X, size_t size, |
| 603 | int (*f_rng)(void *, unsigned char *, size_t), |
| 604 | void *p_rng ); |
Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 605 | |
| 606 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 607 | * \brief Greatest common divisor: G = gcd(A, B) |
| 608 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 609 | * \param G Destination MPI |
| 610 | * \param A Left-hand MPI |
| 611 | * \param B Right-hand MPI |
| 612 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 613 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 614 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 615 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 616 | int mpi_gcd( mpi *G, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 617 | |
| 618 | /** |
| 619 | * \brief Modular inverse: X = A^-1 mod N |
| 620 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 621 | * \param X Destination MPI |
| 622 | * \param A Left-hand MPI |
| 623 | * \param N Right-hand MPI |
| 624 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 625 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 626 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 627 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 628 | POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 629 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 630 | int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 631 | |
| 632 | /** |
| 633 | * \brief Miller-Rabin primality test |
| 634 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 635 | * \param X MPI to check |
| 636 | * \param f_rng RNG function |
| 637 | * \param p_rng RNG parameter |
| 638 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 639 | * \return 0 if successful (probably prime), |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 640 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 641 | * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 642 | */ |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 643 | int mpi_is_prime( mpi *X, |
| 644 | int (*f_rng)(void *, unsigned char *, size_t), |
| 645 | void *p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 646 | |
| 647 | /** |
| 648 | * \brief Prime number generation |
| 649 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 650 | * \param X Destination MPI |
Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 651 | * \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] | 652 | * \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] | 653 | * \param f_rng RNG function |
| 654 | * \param p_rng RNG parameter |
| 655 | * |
| 656 | * \return 0 if successful (probably prime), |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 657 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 658 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 659 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 660 | int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 661 | int (*f_rng)(void *, unsigned char *, size_t), |
| 662 | void *p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 663 | |
| 664 | /** |
| 665 | * \brief Checkup routine |
| 666 | * |
| 667 | * \return 0 if successful, or 1 if the test failed |
| 668 | */ |
| 669 | int mpi_self_test( int verbose ); |
| 670 | |
| 671 | #ifdef __cplusplus |
| 672 | } |
| 673 | #endif |
| 674 | |
| 675 | #endif /* bignum.h */ |