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