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