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