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 | 9bcf16c | 2013-06-24 19:31:17 +0200 | [diff] [blame] | 6 | * Copyright (C) 2006-2013, Brainspark B.V. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
| 8 | * This file is part of PolarSSL (http://www.polarssl.org) |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 9 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 10 | * |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 11 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 12 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License along |
| 24 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 25 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 27 | #ifndef POLARSSL_BIGNUM_H |
| 28 | #define POLARSSL_BIGNUM_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
| 30 | #include <stdio.h> |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 31 | #include <string.h> |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 32 | |
Paul Bakker | cf0360a | 2012-01-20 10:08:14 +0000 | [diff] [blame] | 33 | #include "config.h" |
| 34 | |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 35 | #ifdef _MSC_VER |
| 36 | #include <basetsd.h> |
Paul Bakker | 4a2bd0d | 2012-11-02 11:06:08 +0000 | [diff] [blame] | 37 | #if (_MSC_VER <= 1200) |
| 38 | typedef signed short int16_t; |
| 39 | typedef unsigned short uint16_t; |
| 40 | #else |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 41 | typedef INT16 int16_t; |
| 42 | typedef UINT16 uint16_t; |
Paul Bakker | 4a2bd0d | 2012-11-02 11:06:08 +0000 | [diff] [blame] | 43 | #endif |
Paul Bakker | 9ef6e2b | 2012-10-01 20:57:38 +0000 | [diff] [blame] | 44 | typedef INT32 int32_t; |
Paul Bakker | 8ea31ff | 2013-02-27 15:02:50 +0100 | [diff] [blame] | 45 | typedef INT64 int64_t; |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 46 | typedef UINT32 uint32_t; |
| 47 | typedef UINT64 uint64_t; |
| 48 | #else |
| 49 | #include <inttypes.h> |
| 50 | #endif |
| 51 | |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 52 | #define POLARSSL_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */ |
| 53 | #define POLARSSL_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */ |
| 54 | #define POLARSSL_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */ |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 55 | #define POLARSSL_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */ |
Paul Bakker | 9d78140 | 2011-05-09 16:17:09 +0000 | [diff] [blame] | 56 | #define POLARSSL_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */ |
| 57 | #define POLARSSL_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */ |
| 58 | #define POLARSSL_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */ |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 59 | #define POLARSSL_ERR_MPI_MALLOC_FAILED -0x0010 /**< Memory allocation failed. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 60 | |
| 61 | #define MPI_CHK(f) if( ( ret = f ) != 0 ) goto cleanup |
| 62 | |
| 63 | /* |
Paul Bakker | f968857 | 2011-05-05 10:00:45 +0000 | [diff] [blame] | 64 | * Maximum size MPIs are allowed to grow to in number of limbs. |
| 65 | */ |
| 66 | #define POLARSSL_MPI_MAX_LIMBS 10000 |
| 67 | |
Paul Bakker | 9bcf16c | 2013-06-24 19:31:17 +0200 | [diff] [blame] | 68 | #if !defined(POLARSSL_CONFIG_OPTIONS) |
Paul Bakker | f968857 | 2011-05-05 10:00:45 +0000 | [diff] [blame] | 69 | /* |
Paul Bakker | b6d5f08 | 2011-11-25 11:52:11 +0000 | [diff] [blame] | 70 | * Maximum window size used for modular exponentiation. Default: 6 |
| 71 | * Minimum value: 1. Maximum value: 6. |
| 72 | * |
| 73 | * Result is an array of ( 2 << POLARSSL_MPI_WINDOW_SIZE ) MPIs used |
| 74 | * for the sliding window calculation. (So 64 by default) |
| 75 | * |
| 76 | * Reduction in size, reduces speed. |
| 77 | */ |
| 78 | #define POLARSSL_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */ |
| 79 | |
| 80 | /* |
Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 81 | * Maximum size of MPIs allowed in bits and bytes for user-MPIs. |
Paul Bakker | f626e1d | 2013-01-21 12:10:00 +0100 | [diff] [blame] | 82 | * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits ) |
Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 83 | * |
| 84 | * Note: Calculations can results temporarily in larger MPIs. So the number |
| 85 | * of limbs required (POLARSSL_MPI_MAX_LIMBS) is higher. |
| 86 | */ |
| 87 | #define POLARSSL_MPI_MAX_SIZE 512 /**< Maximum number of bytes for usable MPIs. */ |
Paul Bakker | 9bcf16c | 2013-06-24 19:31:17 +0200 | [diff] [blame] | 88 | |
| 89 | #endif /* !POLARSSL_CONFIG_OPTIONS */ |
| 90 | |
Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 91 | #define POLARSSL_MPI_MAX_BITS ( 8 * POLARSSL_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */ |
| 92 | |
| 93 | /* |
Paul Bakker | 5531c6d | 2012-09-26 19:20:46 +0000 | [diff] [blame] | 94 | * When reading from files with mpi_read_file() and writing to files with |
| 95 | * mpi_write_file() the buffer should have space |
Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 96 | * for a (short) label, the MPI (in the provided radix), the newline |
| 97 | * characters and the '\0'. |
| 98 | * |
| 99 | * By default we assume at least a 10 char label, a minimum radix of 10 |
| 100 | * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars). |
Paul Bakker | f918310 | 2012-09-27 20:42:35 +0000 | [diff] [blame] | 101 | * Autosized at compile time for at least a 10 char label, a minimum radix |
| 102 | * of 10 (decimal) for a number of POLARSSL_MPI_MAX_BITS size. |
| 103 | * |
| 104 | * This used to be statically sized to 1250 for a maximum of 4096 bit |
| 105 | * numbers (1234 decimal chars). |
| 106 | * |
| 107 | * Calculate using the formula: |
| 108 | * POLARSSL_MPI_RW_BUFFER_SIZE = ceil(POLARSSL_MPI_MAX_BITS / ln(10) * ln(2)) + |
| 109 | * LabelSize + 6 |
Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 110 | */ |
Paul Bakker | f918310 | 2012-09-27 20:42:35 +0000 | [diff] [blame] | 111 | #define POLARSSL_MPI_MAX_BITS_SCALE100 ( 100 * POLARSSL_MPI_MAX_BITS ) |
| 112 | #define LN_2_DIV_LN_10_SCALE100 332 |
| 113 | #define POLARSSL_MPI_RW_BUFFER_SIZE ( ((POLARSSL_MPI_MAX_BITS_SCALE100 + LN_2_DIV_LN_10_SCALE100 - 1) / LN_2_DIV_LN_10_SCALE100) + 10 + 6 ) |
Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 114 | |
| 115 | /* |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 116 | * Define the base integer type, architecture-wise |
| 117 | */ |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 118 | #if defined(POLARSSL_HAVE_INT8) |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 119 | typedef signed char t_sint; |
| 120 | typedef unsigned char t_uint; |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 121 | typedef uint16_t t_udbl; |
Paul Bakker | 62261d6 | 2012-10-02 12:19:31 +0000 | [diff] [blame] | 122 | #define POLARSSL_HAVE_UDBL |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 123 | #else |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 124 | #if defined(POLARSSL_HAVE_INT16) |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 125 | typedef int16_t t_sint; |
| 126 | typedef uint16_t t_uint; |
| 127 | typedef uint32_t t_udbl; |
Paul Bakker | 62261d6 | 2012-10-02 12:19:31 +0000 | [diff] [blame] | 128 | #define POLARSSL_HAVE_UDBL |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 129 | #else |
Paul Bakker | 9f2018e | 2013-02-27 15:01:34 +0100 | [diff] [blame] | 130 | #if ( defined(_MSC_VER) && defined(_M_AMD64) ) |
Manuel Pégourié-Gonnard | 5779cbe | 2013-10-23 20:17:00 +0200 | [diff] [blame] | 131 | #define POLARSSL_HAVE_INT64 |
Paul Bakker | 62261d6 | 2012-10-02 12:19:31 +0000 | [diff] [blame] | 132 | typedef int64_t t_sint; |
| 133 | typedef uint64_t t_uint; |
| 134 | #else |
| 135 | #if ( defined(__GNUC__) && ( \ |
| 136 | defined(__amd64__) || defined(__x86_64__) || \ |
| 137 | defined(__ppc64__) || defined(__powerpc64__) || \ |
| 138 | defined(__ia64__) || defined(__alpha__) || \ |
| 139 | (defined(__sparc__) && defined(__arch64__)) || \ |
| 140 | defined(__s390x__) ) ) |
Manuel Pégourié-Gonnard | 5779cbe | 2013-10-23 20:17:00 +0200 | [diff] [blame] | 141 | #define POLARSSL_HAVE_INT64 |
Paul Bakker | 62261d6 | 2012-10-02 12:19:31 +0000 | [diff] [blame] | 142 | typedef int64_t t_sint; |
| 143 | typedef uint64_t t_uint; |
| 144 | typedef unsigned int t_udbl __attribute__((mode(TI))); |
| 145 | #define POLARSSL_HAVE_UDBL |
| 146 | #else |
Manuel Pégourié-Gonnard | a47e705 | 2013-10-21 17:51:45 +0200 | [diff] [blame] | 147 | #define POLARSSL_HAVE_INT32 |
Paul Bakker | 62261d6 | 2012-10-02 12:19:31 +0000 | [diff] [blame] | 148 | typedef int32_t t_sint; |
| 149 | typedef uint32_t t_uint; |
| 150 | #if ( defined(_MSC_VER) && defined(_M_IX86) ) |
| 151 | typedef uint64_t t_udbl; |
| 152 | #define POLARSSL_HAVE_UDBL |
| 153 | #else |
| 154 | #if defined( POLARSSL_HAVE_LONGLONG ) |
| 155 | typedef unsigned long long t_udbl; |
| 156 | #define POLARSSL_HAVE_UDBL |
| 157 | #endif |
| 158 | #endif |
| 159 | #endif |
Paul Bakker | 5c2364c | 2012-10-01 14:41:15 +0000 | [diff] [blame] | 160 | #endif |
| 161 | #endif /* POLARSSL_HAVE_INT16 */ |
| 162 | #endif /* POLARSSL_HAVE_INT8 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 163 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 164 | #ifdef __cplusplus |
| 165 | extern "C" { |
| 166 | #endif |
| 167 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 168 | /** |
| 169 | * \brief MPI structure |
| 170 | */ |
| 171 | typedef struct |
| 172 | { |
| 173 | int s; /*!< integer sign */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 174 | size_t n; /*!< total # of limbs */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 175 | t_uint *p; /*!< pointer to limbs */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 176 | } |
| 177 | mpi; |
| 178 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 179 | /** |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 180 | * \brief Initialize one MPI |
| 181 | * |
| 182 | * \param X One MPI to initialize. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 183 | */ |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 184 | void mpi_init( mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 185 | |
| 186 | /** |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 187 | * \brief Unallocate one MPI |
| 188 | * |
| 189 | * \param X One MPI to unallocate. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 190 | */ |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 191 | void mpi_free( mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 192 | |
| 193 | /** |
| 194 | * \brief Enlarge to the specified number of limbs |
| 195 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 196 | * \param X MPI to grow |
| 197 | * \param nblimbs The target number of limbs |
| 198 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 199 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 200 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 201 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 202 | int mpi_grow( mpi *X, size_t nblimbs ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 203 | |
| 204 | /** |
| 205 | * \brief Copy the contents of Y into X |
| 206 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 207 | * \param X Destination MPI |
| 208 | * \param Y Source MPI |
| 209 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 210 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 211 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 212 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 213 | int mpi_copy( mpi *X, const mpi *Y ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 214 | |
| 215 | /** |
| 216 | * \brief Swap the contents of X and Y |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 217 | * |
| 218 | * \param X First MPI value |
| 219 | * \param Y Second MPI value |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 220 | */ |
| 221 | void mpi_swap( mpi *X, mpi *Y ); |
| 222 | |
| 223 | /** |
| 224 | * \brief Set value from integer |
| 225 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 226 | * \param X MPI to set |
| 227 | * \param z Value to use |
| 228 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 229 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 230 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 231 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 232 | int mpi_lset( mpi *X, t_sint z ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 233 | |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 234 | /** |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 235 | * \brief Get a specific bit from X |
| 236 | * |
| 237 | * \param X MPI to use |
| 238 | * \param pos Zero-based index of the bit in X |
| 239 | * |
| 240 | * \return Either a 0 or a 1 |
| 241 | */ |
Paul Bakker | 6b906e5 | 2012-05-08 12:01:43 +0000 | [diff] [blame] | 242 | int mpi_get_bit( const mpi *X, size_t pos ); |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 243 | |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 244 | /** |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 245 | * \brief Set a bit of X to a specific value of 0 or 1 |
| 246 | * |
| 247 | * \note Will grow X if necessary to set a bit to 1 in a not yet |
| 248 | * existing limb. Will not grow if bit should be set to 0 |
| 249 | * |
| 250 | * \param X MPI to use |
| 251 | * \param pos Zero-based index of the bit in X |
| 252 | * \param val The value to set the bit to (0 or 1) |
| 253 | * |
| 254 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 255 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 256 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1 |
| 257 | */ |
| 258 | int mpi_set_bit( mpi *X, size_t pos, unsigned char val ); |
| 259 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 260 | /** |
Paul Bakker | 6b906e5 | 2012-05-08 12:01:43 +0000 | [diff] [blame] | 261 | * \brief Return the number of zero-bits before the least significant |
| 262 | * '1' bit |
| 263 | * |
| 264 | * Note: Thus also the zero-based index of the least significant '1' bit |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 265 | * |
| 266 | * \param X MPI to use |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 267 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 268 | size_t mpi_lsb( const mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 269 | |
| 270 | /** |
Paul Bakker | 6b906e5 | 2012-05-08 12:01:43 +0000 | [diff] [blame] | 271 | * \brief Return the number of bits up to and including the most |
| 272 | * significant '1' bit' |
| 273 | * |
| 274 | * Note: Thus also the one-based index of the most significant '1' bit |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 275 | * |
| 276 | * \param X MPI to use |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 277 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 278 | size_t mpi_msb( const mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 279 | |
| 280 | /** |
| 281 | * \brief Return the total size in bytes |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 282 | * |
| 283 | * \param X MPI to use |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 284 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 285 | size_t mpi_size( const mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 286 | |
| 287 | /** |
| 288 | * \brief Import from an ASCII string |
| 289 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 290 | * \param X Destination MPI |
| 291 | * \param radix Input numeric base |
| 292 | * \param s Null-terminated string buffer |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 293 | * |
Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 294 | * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 295 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 296 | int mpi_read_string( mpi *X, int radix, const char *s ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 297 | |
| 298 | /** |
| 299 | * \brief Export into an ASCII string |
| 300 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 301 | * \param X Source MPI |
| 302 | * \param radix Output numeric base |
| 303 | * \param s String buffer |
| 304 | * \param slen String buffer size |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 305 | * |
Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 306 | * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code. |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 307 | * *slen is always updated to reflect the amount |
| 308 | * of data that has (or would have) been written. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 309 | * |
| 310 | * \note Call this function with *slen = 0 to obtain the |
| 311 | * minimum required buffer size in *slen. |
| 312 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 313 | 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] | 314 | |
Paul Bakker | 2b6af2f | 2012-10-23 11:08:02 +0000 | [diff] [blame] | 315 | #if defined(POLARSSL_FS_IO) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 316 | /** |
| 317 | * \brief Read X from an opened file |
| 318 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 319 | * \param X Destination MPI |
| 320 | * \param radix Input numeric base |
| 321 | * \param fin Input file handle |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 322 | * |
Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 323 | * \return 0 if successful, POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if |
| 324 | * the file read buffer is too small or a |
| 325 | * POLARSSL_ERR_MPI_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 326 | */ |
| 327 | int mpi_read_file( mpi *X, int radix, FILE *fin ); |
| 328 | |
| 329 | /** |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 330 | * \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] | 331 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 332 | * \param p Prefix, can be NULL |
| 333 | * \param X Source MPI |
| 334 | * \param radix Output numeric base |
| 335 | * \param fout Output file handle (can be NULL) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 336 | * |
Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 337 | * \return 0 if successful, or a POLARSSL_ERR_MPI_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 338 | * |
| 339 | * \note Set fout == NULL to print X on the console. |
| 340 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 341 | int mpi_write_file( const char *p, const mpi *X, int radix, FILE *fout ); |
Paul Bakker | 2b6af2f | 2012-10-23 11:08:02 +0000 | [diff] [blame] | 342 | #endif /* POLARSSL_FS_IO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 343 | |
| 344 | /** |
| 345 | * \brief Import X from unsigned binary data, big endian |
| 346 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 347 | * \param X Destination MPI |
| 348 | * \param buf Input buffer |
| 349 | * \param buflen Input buffer size |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 350 | * |
| 351 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 352 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 353 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 354 | 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] | 355 | |
| 356 | /** |
| 357 | * \brief Export X into unsigned binary data, big endian |
| 358 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 359 | * \param X Source MPI |
| 360 | * \param buf Output buffer |
| 361 | * \param buflen Output buffer size |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 362 | * |
| 363 | * \return 0 if successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 364 | * POLARSSL_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 365 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 366 | 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] | 367 | |
| 368 | /** |
| 369 | * \brief Left-shift: X <<= count |
| 370 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 371 | * \param X MPI to shift |
| 372 | * \param count Amount to shift |
| 373 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 374 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 375 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 376 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 377 | int mpi_shift_l( mpi *X, size_t count ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 378 | |
| 379 | /** |
| 380 | * \brief Right-shift: X >>= count |
| 381 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 382 | * \param X MPI to shift |
| 383 | * \param count Amount to shift |
| 384 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 385 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 386 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 387 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 388 | int mpi_shift_r( mpi *X, size_t count ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 389 | |
| 390 | /** |
| 391 | * \brief Compare unsigned values |
| 392 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 393 | * \param X Left-hand MPI |
| 394 | * \param Y Right-hand MPI |
| 395 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 396 | * \return 1 if |X| is greater than |Y|, |
| 397 | * -1 if |X| is lesser than |Y| or |
| 398 | * 0 if |X| is equal to |Y| |
| 399 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 400 | int mpi_cmp_abs( const mpi *X, const mpi *Y ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 401 | |
| 402 | /** |
| 403 | * \brief Compare signed values |
| 404 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 405 | * \param X Left-hand MPI |
| 406 | * \param Y Right-hand MPI |
| 407 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 408 | * \return 1 if X is greater than Y, |
| 409 | * -1 if X is lesser than Y or |
| 410 | * 0 if X is equal to Y |
| 411 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 412 | int mpi_cmp_mpi( const mpi *X, const mpi *Y ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 413 | |
| 414 | /** |
| 415 | * \brief Compare signed values |
| 416 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 417 | * \param X Left-hand MPI |
| 418 | * \param z The integer value to compare to |
| 419 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 420 | * \return 1 if X is greater than z, |
| 421 | * -1 if X is lesser than z or |
| 422 | * 0 if X is equal to z |
| 423 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 424 | int mpi_cmp_int( const mpi *X, t_sint z ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 425 | |
| 426 | /** |
| 427 | * \brief Unsigned addition: X = |A| + |B| |
| 428 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 429 | * \param X Destination MPI |
| 430 | * \param A Left-hand MPI |
| 431 | * \param B Right-hand MPI |
| 432 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 433 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 434 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 435 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 436 | int mpi_add_abs( mpi *X, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 437 | |
| 438 | /** |
| 439 | * \brief Unsigned substraction: X = |A| - |B| |
| 440 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 441 | * \param X Destination MPI |
| 442 | * \param A Left-hand MPI |
| 443 | * \param B Right-hand MPI |
| 444 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 445 | * \return 0 if successful, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 446 | * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B is greater than A |
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_sub_abs( mpi *X, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 449 | |
| 450 | /** |
| 451 | * \brief Signed addition: X = A + B |
| 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 B Right-hand MPI |
| 456 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 457 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 458 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 459 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 460 | int mpi_add_mpi( mpi *X, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 461 | |
| 462 | /** |
| 463 | * \brief Signed substraction: X = A - B |
| 464 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 465 | * \param X Destination MPI |
| 466 | * \param A Left-hand MPI |
| 467 | * \param B Right-hand MPI |
| 468 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 469 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 470 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 471 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 472 | int mpi_sub_mpi( mpi *X, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 473 | |
| 474 | /** |
| 475 | * \brief Signed addition: X = A + b |
| 476 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 477 | * \param X Destination MPI |
| 478 | * \param A Left-hand MPI |
| 479 | * \param b The integer value to add |
| 480 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 481 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 482 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 483 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 484 | int mpi_add_int( mpi *X, const mpi *A, t_sint b ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 485 | |
| 486 | /** |
| 487 | * \brief Signed substraction: X = A - b |
| 488 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 489 | * \param X Destination MPI |
| 490 | * \param A Left-hand MPI |
| 491 | * \param b The integer value to subtract |
| 492 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 493 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 494 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 495 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 496 | int mpi_sub_int( mpi *X, const mpi *A, t_sint b ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 497 | |
| 498 | /** |
| 499 | * \brief Baseline multiplication: X = A * B |
| 500 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 501 | * \param X Destination MPI |
| 502 | * \param A Left-hand MPI |
| 503 | * \param B Right-hand MPI |
| 504 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 505 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 506 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 507 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 508 | int mpi_mul_mpi( mpi *X, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 509 | |
| 510 | /** |
| 511 | * \brief Baseline multiplication: X = A * b |
Paul Bakker | ce40a6d | 2009-06-23 19:46:08 +0000 | [diff] [blame] | 512 | * Note: b is an unsigned integer type, thus |
| 513 | * Negative values of b are ignored. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 514 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 515 | * \param X Destination MPI |
| 516 | * \param A Left-hand MPI |
| 517 | * \param b The integer value to multiply with |
| 518 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 519 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 520 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 521 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 522 | int mpi_mul_int( mpi *X, const mpi *A, t_sint b ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 523 | |
| 524 | /** |
| 525 | * \brief Division by mpi: A = Q * B + R |
| 526 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 527 | * \param Q Destination MPI for the quotient |
| 528 | * \param R Destination MPI for the rest value |
| 529 | * \param A Left-hand MPI |
| 530 | * \param B Right-hand MPI |
| 531 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 532 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 533 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 534 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 535 | * |
| 536 | * \note Either Q or R can be NULL. |
| 537 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 538 | 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] | 539 | |
| 540 | /** |
| 541 | * \brief Division by int: A = Q * b + R |
| 542 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 543 | * \param Q Destination MPI for the quotient |
| 544 | * \param R Destination MPI for the rest value |
| 545 | * \param A Left-hand MPI |
| 546 | * \param b Integer to divide by |
| 547 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 548 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 549 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 550 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 551 | * |
| 552 | * \note Either Q or R can be NULL. |
| 553 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 554 | 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] | 555 | |
| 556 | /** |
| 557 | * \brief Modulo: R = A mod B |
| 558 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 559 | * \param R Destination MPI for the rest value |
| 560 | * \param A Left-hand MPI |
| 561 | * \param B Right-hand MPI |
| 562 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 563 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 564 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, |
Paul Bakker | ce40a6d | 2009-06-23 19:46:08 +0000 | [diff] [blame] | 565 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if B == 0, |
| 566 | * POLARSSL_ERR_MPI_NEGATIVE_VALUE if B < 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 567 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 568 | int mpi_mod_mpi( mpi *R, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 569 | |
| 570 | /** |
| 571 | * \brief Modulo: r = A mod b |
| 572 | * |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 573 | * \param r Destination t_uint |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 574 | * \param A Left-hand MPI |
| 575 | * \param b Integer to divide by |
| 576 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 577 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 578 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, |
Paul Bakker | ce40a6d | 2009-06-23 19:46:08 +0000 | [diff] [blame] | 579 | * POLARSSL_ERR_MPI_DIVISION_BY_ZERO if b == 0, |
| 580 | * POLARSSL_ERR_MPI_NEGATIVE_VALUE if b < 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 581 | */ |
Paul Bakker | a755ca1 | 2011-04-24 09:11:17 +0000 | [diff] [blame] | 582 | 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] | 583 | |
| 584 | /** |
| 585 | * \brief Sliding-window exponentiation: X = A^E mod N |
| 586 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 587 | * \param X Destination MPI |
| 588 | * \param A Left-hand MPI |
| 589 | * \param E Exponent MPI |
| 590 | * \param N Modular MPI |
| 591 | * \param _RR Speed-up MPI used for recalculations |
| 592 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 593 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 594 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, |
Paul Bakker | f6198c1 | 2012-05-16 08:02:29 +0000 | [diff] [blame] | 595 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or even or if |
| 596 | * E is negative |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 597 | * |
| 598 | * \note _RR is used to avoid re-computing R*R mod N across |
| 599 | * multiple calls, which speeds up things a bit. It can |
| 600 | * be set to NULL if the extra performance is unneeded. |
| 601 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 602 | 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] | 603 | |
| 604 | /** |
Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 605 | * \brief Fill an MPI X with size bytes of random |
| 606 | * |
| 607 | * \param X Destination MPI |
| 608 | * \param size Size in bytes |
| 609 | * \param f_rng RNG function |
| 610 | * \param p_rng RNG parameter |
| 611 | * |
| 612 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 613 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 614 | */ |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 615 | int mpi_fill_random( mpi *X, size_t size, |
| 616 | int (*f_rng)(void *, unsigned char *, size_t), |
| 617 | void *p_rng ); |
Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 618 | |
| 619 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 620 | * \brief Greatest common divisor: G = gcd(A, B) |
| 621 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 622 | * \param G Destination MPI |
| 623 | * \param A Left-hand MPI |
| 624 | * \param B Right-hand MPI |
| 625 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 626 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 627 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 628 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 629 | int mpi_gcd( mpi *G, const mpi *A, const mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 630 | |
| 631 | /** |
| 632 | * \brief Modular inverse: X = A^-1 mod N |
| 633 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 634 | * \param X Destination MPI |
| 635 | * \param A Left-hand MPI |
| 636 | * \param N Right-hand MPI |
| 637 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 638 | * \return 0 if successful, |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 639 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 640 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if N is negative or nil |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 641 | POLARSSL_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 642 | */ |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 643 | int mpi_inv_mod( mpi *X, const mpi *A, const mpi *N ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 644 | |
| 645 | /** |
| 646 | * \brief Miller-Rabin primality test |
| 647 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 648 | * \param X MPI to check |
| 649 | * \param f_rng RNG function |
| 650 | * \param p_rng RNG parameter |
| 651 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 652 | * \return 0 if successful (probably prime), |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 653 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 654 | * POLARSSL_ERR_MPI_NOT_ACCEPTABLE if X is not prime |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 655 | */ |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 656 | int mpi_is_prime( mpi *X, |
| 657 | int (*f_rng)(void *, unsigned char *, size_t), |
| 658 | void *p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 659 | |
| 660 | /** |
| 661 | * \brief Prime number generation |
| 662 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 663 | * \param X Destination MPI |
Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 664 | * \param nbits Required size of X in bits ( 3 <= nbits <= POLARSSL_MPI_MAX_BITS ) |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 665 | * \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] | 666 | * \param f_rng RNG function |
| 667 | * \param p_rng RNG parameter |
| 668 | * |
| 669 | * \return 0 if successful (probably prime), |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 670 | * POLARSSL_ERR_MPI_MALLOC_FAILED if memory allocation failed, |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 671 | * POLARSSL_ERR_MPI_BAD_INPUT_DATA if nbits is < 3 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 672 | */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 673 | int mpi_gen_prime( mpi *X, size_t nbits, int dh_flag, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 674 | int (*f_rng)(void *, unsigned char *, size_t), |
| 675 | void *p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 676 | |
| 677 | /** |
| 678 | * \brief Checkup routine |
| 679 | * |
| 680 | * \return 0 if successful, or 1 if the test failed |
| 681 | */ |
| 682 | int mpi_self_test( int verbose ); |
| 683 | |
| 684 | #ifdef __cplusplus |
| 685 | } |
| 686 | #endif |
| 687 | |
| 688 | #endif /* bignum.h */ |