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