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