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