| 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 | * | 
| Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 6 | *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 7 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 8 | * | 
|  | 9 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 10 | *  not use this file except in compliance with the License. | 
|  | 11 | *  You may obtain a copy of the License at | 
|  | 12 | * | 
|  | 13 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 14 | * | 
|  | 15 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 16 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 17 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 18 | *  See the License for the specific language governing permissions and | 
|  | 19 | *  limitations under the License. | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 20 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 21 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 22 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #ifndef MBEDTLS_BIGNUM_H | 
|  | 24 | #define MBEDTLS_BIGNUM_H | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 25 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Paul Bakker | cf0360a | 2012-01-20 10:08:14 +0000 | [diff] [blame] | 27 | #include "config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 28 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 30 | #endif | 
| Paul Bakker | cf0360a | 2012-01-20 10:08:14 +0000 | [diff] [blame] | 31 |  | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 32 | #include <stddef.h> | 
| Manuel Pégourié-Gonnard | ab22910 | 2015-04-15 11:53:16 +0200 | [diff] [blame] | 33 | #include <stdint.h> | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 34 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_FS_IO) | 
| Manuel Pégourié-Gonnard | e94e6e5 | 2015-01-19 15:01:53 +0000 | [diff] [blame] | 36 | #include <stdio.h> | 
|  | 37 | #endif | 
|  | 38 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 39 | #define MBEDTLS_ERR_MPI_FILE_IO_ERROR                     -0x0002  /**< An error occurred while reading from or writing to a file. */ | 
|  | 40 | #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA                    -0x0004  /**< Bad input parameters to function. */ | 
|  | 41 | #define MBEDTLS_ERR_MPI_INVALID_CHARACTER                 -0x0006  /**< There is an invalid character in the digit string. */ | 
|  | 42 | #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL                  -0x0008  /**< The buffer is too small to write to. */ | 
|  | 43 | #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE                    -0x000A  /**< The input arguments are negative or result in illegal output. */ | 
|  | 44 | #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO                  -0x000C  /**< The input argument for division is zero, which is not allowed. */ | 
|  | 45 | #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE                    -0x000E  /**< The input arguments are not acceptable. */ | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 46 | #define MBEDTLS_ERR_MPI_ALLOC_FAILED                      -0x0010  /**< Memory allocation failed. */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 47 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | #define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 49 |  | 
|  | 50 | /* | 
| Paul Bakker | f968857 | 2011-05-05 10:00:45 +0000 | [diff] [blame] | 51 | * Maximum size MPIs are allowed to grow to in number of limbs. | 
|  | 52 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | #define MBEDTLS_MPI_MAX_LIMBS                             10000 | 
| Paul Bakker | f968857 | 2011-05-05 10:00:45 +0000 | [diff] [blame] | 54 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | #if !defined(MBEDTLS_MPI_WINDOW_SIZE) | 
| Paul Bakker | f968857 | 2011-05-05 10:00:45 +0000 | [diff] [blame] | 56 | /* | 
| Paul Bakker | b6d5f08 | 2011-11-25 11:52:11 +0000 | [diff] [blame] | 57 | * Maximum window size used for modular exponentiation. Default: 6 | 
|  | 58 | * Minimum value: 1. Maximum value: 6. | 
|  | 59 | * | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used | 
| Paul Bakker | b6d5f08 | 2011-11-25 11:52:11 +0000 | [diff] [blame] | 61 | * for the sliding window calculation. (So 64 by default) | 
|  | 62 | * | 
|  | 63 | * Reduction in size, reduces speed. | 
|  | 64 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | #define MBEDTLS_MPI_WINDOW_SIZE                           6        /**< Maximum windows size used. */ | 
|  | 66 | #endif /* !MBEDTLS_MPI_WINDOW_SIZE */ | 
| Paul Bakker | b6d5f08 | 2011-11-25 11:52:11 +0000 | [diff] [blame] | 67 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 68 | #if !defined(MBEDTLS_MPI_MAX_SIZE) | 
| Paul Bakker | b6d5f08 | 2011-11-25 11:52:11 +0000 | [diff] [blame] | 69 | /* | 
| Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 70 | * Maximum size of MPIs allowed in bits and bytes for user-MPIs. | 
| Paul Bakker | f626e1d | 2013-01-21 12:10:00 +0100 | [diff] [blame] | 71 | * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits ) | 
| Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 72 | * | 
|  | 73 | * Note: Calculations can results temporarily in larger MPIs. So the number | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 74 | * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher. | 
| Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 75 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 76 | #define MBEDTLS_MPI_MAX_SIZE                              1024     /**< Maximum number of bytes for usable MPIs. */ | 
|  | 77 | #endif /* !MBEDTLS_MPI_MAX_SIZE */ | 
| Paul Bakker | 9bcf16c | 2013-06-24 19:31:17 +0200 | [diff] [blame] | 78 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 79 | #define MBEDTLS_MPI_MAX_BITS                              ( 8 * MBEDTLS_MPI_MAX_SIZE )    /**< Maximum number of bits for usable MPIs. */ | 
| Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 80 |  | 
|  | 81 | /* | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 82 | * When reading from files with mbedtls_mpi_read_file() and writing to files with | 
|  | 83 | * mbedtls_mpi_write_file() the buffer should have space | 
| Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 84 | * for a (short) label, the MPI (in the provided radix), the newline | 
|  | 85 | * characters and the '\0'. | 
|  | 86 | * | 
|  | 87 | * By default we assume at least a 10 char label, a minimum radix of 10 | 
|  | 88 | * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars). | 
| Paul Bakker | f918310 | 2012-09-27 20:42:35 +0000 | [diff] [blame] | 89 | * Autosized at compile time for at least a 10 char label, a minimum radix | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 90 | * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size. | 
| Paul Bakker | f918310 | 2012-09-27 20:42:35 +0000 | [diff] [blame] | 91 | * | 
|  | 92 | * This used to be statically sized to 1250 for a maximum of 4096 bit | 
|  | 93 | * numbers (1234 decimal chars). | 
|  | 94 | * | 
|  | 95 | * Calculate using the formula: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 96 | *  MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) + | 
| Paul Bakker | f918310 | 2012-09-27 20:42:35 +0000 | [diff] [blame] | 97 | *                                LabelSize + 6 | 
| Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 98 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | #define MBEDTLS_MPI_MAX_BITS_SCALE100          ( 100 * MBEDTLS_MPI_MAX_BITS ) | 
|  | 100 | #define MBEDTLS_LN_2_DIV_LN_10_SCALE100                 332 | 
|  | 101 | #define MBEDTLS_MPI_RW_BUFFER_SIZE             ( ((MBEDTLS_MPI_MAX_BITS_SCALE100 + MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6 ) | 
| Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 102 |  | 
|  | 103 | /* | 
| Manuel Pégourié-Gonnard | 7b53889 | 2015-04-09 17:00:17 +0200 | [diff] [blame] | 104 | * Define the base integer type, architecture-wise. | 
|  | 105 | * | 
| Andres Amaya Garcia | d7fce00 | 2017-07-20 11:49:32 +0100 | [diff] [blame] | 106 | * 32 or 64-bit integer types can be forced regardless of the underlying | 
|  | 107 | * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64 | 
|  | 108 | * respectively and undefining MBEDTLS_HAVE_ASM. | 
|  | 109 | * | 
| Andres Amaya Garcia | 93db11a | 2017-07-20 12:11:19 +0100 | [diff] [blame] | 110 | * Double-width integers (e.g. 128-bit in 64-bit architectures) can be | 
| Andres Amaya Garcia | d7fce00 | 2017-07-20 11:49:32 +0100 | [diff] [blame] | 111 | * disabled by defining MBEDTLS_NO_UDBL_DIVISION. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 112 | */ | 
| Andres Amaya Garcia | aa27dfe | 2017-05-04 11:05:55 +0100 | [diff] [blame] | 113 | #if !defined(MBEDTLS_HAVE_INT32) | 
|  | 114 | #if defined(_MSC_VER) && defined(_M_AMD64) | 
|  | 115 | /* Always choose 64-bit when using MSC */ | 
| Andres Amaya Garcia | d7fce00 | 2017-07-20 11:49:32 +0100 | [diff] [blame] | 116 | #if !defined(MBEDTLS_HAVE_INT64) | 
|  | 117 | #define MBEDTLS_HAVE_INT64 | 
|  | 118 | #endif /* !MBEDTLS_HAVE_INT64 */ | 
| Andres Amaya Garcia | aa27dfe | 2017-05-04 11:05:55 +0100 | [diff] [blame] | 119 | typedef  int64_t mbedtls_mpi_sint; | 
|  | 120 | typedef uint64_t mbedtls_mpi_uint; | 
|  | 121 | #elif defined(__GNUC__) && (                         \ | 
|  | 122 | defined(__amd64__) || defined(__x86_64__)     || \ | 
|  | 123 | defined(__ppc64__) || defined(__powerpc64__)  || \ | 
|  | 124 | defined(__ia64__)  || defined(__alpha__)      || \ | 
|  | 125 | ( defined(__sparc__) && defined(__arch64__) ) || \ | 
|  | 126 | defined(__s390x__) || defined(__mips64) ) | 
| Andres Amaya Garcia | d7fce00 | 2017-07-20 11:49:32 +0100 | [diff] [blame] | 127 | #if !defined(MBEDTLS_HAVE_INT64) | 
|  | 128 | #define MBEDTLS_HAVE_INT64 | 
|  | 129 | #endif /* MBEDTLS_HAVE_INT64 */ | 
| Andres Amaya Garcia | aa27dfe | 2017-05-04 11:05:55 +0100 | [diff] [blame] | 130 | typedef  int64_t mbedtls_mpi_sint; | 
|  | 131 | typedef uint64_t mbedtls_mpi_uint; | 
| Andres Amaya Garcia | d7fce00 | 2017-07-20 11:49:32 +0100 | [diff] [blame] | 132 | #if !defined(MBEDTLS_NO_UDBL_DIVISION) | 
|  | 133 | /* mbedtls_t_udbl defined as 128-bit unsigned int */ | 
|  | 134 | typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI))); | 
|  | 135 | #define MBEDTLS_HAVE_UDBL | 
|  | 136 | #endif /* !MBEDTLS_NO_UDBL_DIVISION */ | 
| Andres Amaya Garcia | aa27dfe | 2017-05-04 11:05:55 +0100 | [diff] [blame] | 137 | #elif defined(__ARMCC_VERSION) && defined(__aarch64__) | 
| Andres Amaya Garcia | d7fce00 | 2017-07-20 11:49:32 +0100 | [diff] [blame] | 138 | /* | 
|  | 139 | * __ARMCC_VERSION is defined for both armcc and armclang and | 
| Andres Amaya Garcia | aa27dfe | 2017-05-04 11:05:55 +0100 | [diff] [blame] | 140 | * __aarch64__ is only defined by armclang when compiling 64-bit code | 
|  | 141 | */ | 
| Andres Amaya Garcia | d7fce00 | 2017-07-20 11:49:32 +0100 | [diff] [blame] | 142 | #if !defined(MBEDTLS_HAVE_INT64) | 
|  | 143 | #define MBEDTLS_HAVE_INT64 | 
|  | 144 | #endif /* !MBEDTLS_HAVE_INT64 */ | 
| Andres Amaya Garcia | aa27dfe | 2017-05-04 11:05:55 +0100 | [diff] [blame] | 145 | typedef  int64_t mbedtls_mpi_sint; | 
|  | 146 | typedef uint64_t mbedtls_mpi_uint; | 
| Andres Amaya Garcia | d7fce00 | 2017-07-20 11:49:32 +0100 | [diff] [blame] | 147 | #if !defined(MBEDTLS_NO_UDBL_DIVISION) | 
|  | 148 | /* mbedtls_t_udbl defined as 128-bit unsigned int */ | 
|  | 149 | typedef __uint128_t mbedtls_t_udbl; | 
|  | 150 | #define MBEDTLS_HAVE_UDBL | 
|  | 151 | #endif /* !MBEDTLS_NO_UDBL_DIVISION */ | 
|  | 152 | #elif defined(MBEDTLS_HAVE_INT64) | 
|  | 153 | /* Force 64-bit integers with unknown compiler */ | 
|  | 154 | typedef  int64_t mbedtls_mpi_sint; | 
|  | 155 | typedef uint64_t mbedtls_mpi_uint; | 
| Andres Amaya Garcia | aa27dfe | 2017-05-04 11:05:55 +0100 | [diff] [blame] | 156 | #endif | 
|  | 157 | #endif /* !MBEDTLS_HAVE_INT32 */ | 
|  | 158 |  | 
|  | 159 | #if !defined(MBEDTLS_HAVE_INT64) | 
|  | 160 | /* Default to 32-bit compilation */ | 
|  | 161 | #if !defined(MBEDTLS_HAVE_INT32) | 
|  | 162 | #define MBEDTLS_HAVE_INT32 | 
|  | 163 | #endif /* !MBEDTLS_HAVE_INT32 */ | 
|  | 164 | typedef  int32_t mbedtls_mpi_sint; | 
|  | 165 | typedef uint32_t mbedtls_mpi_uint; | 
| Andres Amaya Garcia | d7fce00 | 2017-07-20 11:49:32 +0100 | [diff] [blame] | 166 | #if !defined(MBEDTLS_NO_UDBL_DIVISION) | 
|  | 167 | typedef uint64_t mbedtls_t_udbl; | 
| Andres Amaya Garcia | df1486a | 2017-07-20 17:33:09 +0100 | [diff] [blame] | 168 | #define MBEDTLS_HAVE_UDBL | 
| Andres Amaya Garcia | d7fce00 | 2017-07-20 11:49:32 +0100 | [diff] [blame] | 169 | #endif /* !MBEDTLS_NO_UDBL_DIVISION */ | 
| Andres Amaya Garcia | aa27dfe | 2017-05-04 11:05:55 +0100 | [diff] [blame] | 170 | #endif /* !MBEDTLS_HAVE_INT64 */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 171 |  | 
| Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 172 | #ifdef __cplusplus | 
|  | 173 | extern "C" { | 
|  | 174 | #endif | 
|  | 175 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 176 | /** | 
|  | 177 | * \brief          MPI structure | 
|  | 178 | */ | 
|  | 179 | typedef struct | 
|  | 180 | { | 
|  | 181 | int s;              /*!<  integer sign      */ | 
| Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 182 | size_t n;           /*!<  total # of limbs  */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 183 | mbedtls_mpi_uint *p;          /*!<  pointer to limbs  */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 184 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 185 | mbedtls_mpi; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 186 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 187 | /** | 
| Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 188 | * \brief           Initialize one MPI (make internal references valid) | 
|  | 189 | *                  This just makes it ready to be set or freed, | 
|  | 190 | *                  but does not define a value for the MPI. | 
| Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 191 | * | 
|  | 192 | * \param X         One MPI to initialize. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 193 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | void mbedtls_mpi_init( mbedtls_mpi *X ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 195 |  | 
|  | 196 | /** | 
| Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 197 | * \brief          Unallocate one MPI | 
|  | 198 | * | 
|  | 199 | * \param X        One MPI to unallocate. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 200 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | void mbedtls_mpi_free( mbedtls_mpi *X ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 202 |  | 
|  | 203 | /** | 
|  | 204 | * \brief          Enlarge to the specified number of limbs | 
|  | 205 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 206 | * \param X        MPI to grow | 
|  | 207 | * \param nblimbs  The target number of limbs | 
|  | 208 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 209 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 210 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 211 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 212 | int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 213 |  | 
|  | 214 | /** | 
| Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 215 | * \brief          Resize down, keeping at least the specified number of limbs | 
|  | 216 | * | 
|  | 217 | * \param X        MPI to shrink | 
|  | 218 | * \param nblimbs  The minimum number of limbs to keep | 
|  | 219 | * | 
|  | 220 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 221 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed | 
| Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 222 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 223 | int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs ); | 
| Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 224 |  | 
|  | 225 | /** | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 226 | * \brief          Copy the contents of Y into X | 
|  | 227 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 228 | * \param X        Destination MPI | 
|  | 229 | * \param Y        Source MPI | 
|  | 230 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 231 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 232 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 233 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 234 | int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 235 |  | 
|  | 236 | /** | 
|  | 237 | * \brief          Swap the contents of X and Y | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 238 | * | 
|  | 239 | * \param X        First MPI value | 
|  | 240 | * \param Y        Second MPI value | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 241 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 242 | void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 243 |  | 
|  | 244 | /** | 
| Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 245 | * \brief          Safe conditional assignement X = Y if assign is 1 | 
|  | 246 | * | 
|  | 247 | * \param X        MPI to conditionally assign to | 
|  | 248 | * \param Y        Value to be assigned | 
| Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 249 | * \param assign   1: perform the assignment, 0: keep X's original value | 
| Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 250 | * | 
|  | 251 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 252 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, | 
| Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 253 | * | 
|  | 254 | * \note           This function is equivalent to | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 255 | *                      if( assign ) mbedtls_mpi_copy( X, Y ); | 
| Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 256 | *                 except that it avoids leaking any information about whether | 
|  | 257 | *                 the assignment was done or not (the above code may leak | 
| Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 258 | *                 information through branch prediction and/or memory access | 
|  | 259 | *                 patterns analysis). | 
| Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 260 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 261 | int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign ); | 
| Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 262 |  | 
|  | 263 | /** | 
| Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 264 | * \brief          Safe conditional swap X <-> Y if swap is 1 | 
|  | 265 | * | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 266 | * \param X        First mbedtls_mpi value | 
|  | 267 | * \param Y        Second mbedtls_mpi value | 
| Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 268 | * \param assign   1: perform the swap, 0: keep X and Y's original values | 
|  | 269 | * | 
|  | 270 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 271 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, | 
| Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 272 | * | 
|  | 273 | * \note           This function is equivalent to | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 274 | *                      if( assign ) mbedtls_mpi_swap( X, Y ); | 
| Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 275 | *                 except that it avoids leaking any information about whether | 
|  | 276 | *                 the assignment was done or not (the above code may leak | 
|  | 277 | *                 information through branch prediction and/or memory access | 
|  | 278 | *                 patterns analysis). | 
|  | 279 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign ); | 
| Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 281 |  | 
|  | 282 | /** | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 283 | * \brief          Set value from integer | 
|  | 284 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 285 | * \param X        MPI to set | 
|  | 286 | * \param z        Value to use | 
|  | 287 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 288 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 289 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 290 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 291 | int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 292 |  | 
| Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 293 | /** | 
| Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 294 | * \brief          Get a specific bit from X | 
|  | 295 | * | 
|  | 296 | * \param X        MPI to use | 
|  | 297 | * \param pos      Zero-based index of the bit in X | 
|  | 298 | * | 
|  | 299 | * \return         Either a 0 or a 1 | 
|  | 300 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 301 | int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos ); | 
| Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 302 |  | 
| Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 303 | /** | 
| Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 304 | * \brief          Set a bit of X to a specific value of 0 or 1 | 
|  | 305 | * | 
|  | 306 | * \note           Will grow X if necessary to set a bit to 1 in a not yet | 
|  | 307 | *                 existing limb. Will not grow if bit should be set to 0 | 
|  | 308 | * | 
|  | 309 | * \param X        MPI to use | 
|  | 310 | * \param pos      Zero-based index of the bit in X | 
|  | 311 | * \param val      The value to set the bit to (0 or 1) | 
|  | 312 | * | 
|  | 313 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 314 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 315 | *                 MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1 | 
| Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 316 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 317 | int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val ); | 
| Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 318 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 319 | /** | 
| Paul Bakker | 6b906e5 | 2012-05-08 12:01:43 +0000 | [diff] [blame] | 320 | * \brief          Return the number of zero-bits before the least significant | 
|  | 321 | *                 '1' bit | 
|  | 322 | * | 
|  | 323 | * 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] | 324 | * | 
|  | 325 | * \param X        MPI to use | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 326 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 327 | size_t mbedtls_mpi_lsb( const mbedtls_mpi *X ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 328 |  | 
|  | 329 | /** | 
| Paul Bakker | 6b906e5 | 2012-05-08 12:01:43 +0000 | [diff] [blame] | 330 | * \brief          Return the number of bits up to and including the most | 
|  | 331 | *                 significant '1' bit' | 
|  | 332 | * | 
|  | 333 | * 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] | 334 | * | 
|  | 335 | * \param X        MPI to use | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 336 | */ | 
| Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 337 | size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 338 |  | 
|  | 339 | /** | 
|  | 340 | * \brief          Return the total size in bytes | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 341 | * | 
|  | 342 | * \param X        MPI to use | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 343 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 344 | size_t mbedtls_mpi_size( const mbedtls_mpi *X ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 345 |  | 
|  | 346 | /** | 
|  | 347 | * \brief          Import from an ASCII string | 
|  | 348 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 349 | * \param X        Destination MPI | 
|  | 350 | * \param radix    Input numeric base | 
|  | 351 | * \param s        Null-terminated string buffer | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 352 | * | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 353 | * \return         0 if successful, or a MBEDTLS_ERR_MPI_XXX error code | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 354 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 355 | int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 356 |  | 
|  | 357 | /** | 
|  | 358 | * \brief          Export into an ASCII string | 
|  | 359 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 360 | * \param X        Source MPI | 
|  | 361 | * \param radix    Output numeric base | 
| Manuel Pégourié-Gonnard | f79b425 | 2015-06-02 15:41:48 +0100 | [diff] [blame] | 362 | * \param buf      Buffer to write the string to | 
|  | 363 | * \param buflen   Length of buf | 
|  | 364 | * \param olen     Length of the string written, including final NUL byte | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 365 | * | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 366 | * \return         0 if successful, or a MBEDTLS_ERR_MPI_XXX error code. | 
| Manuel Pégourié-Gonnard | f79b425 | 2015-06-02 15:41:48 +0100 | [diff] [blame] | 367 | *                 *olen is always updated to reflect the amount | 
| Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 368 | *                 of data that has (or would have) been written. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 369 | * | 
| Manuel Pégourié-Gonnard | f79b425 | 2015-06-02 15:41:48 +0100 | [diff] [blame] | 370 | * \note           Call this function with buflen = 0 to obtain the | 
|  | 371 | *                 minimum required buffer size in *olen. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 372 | */ | 
| Manuel Pégourié-Gonnard | f79b425 | 2015-06-02 15:41:48 +0100 | [diff] [blame] | 373 | int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, | 
|  | 374 | char *buf, size_t buflen, size_t *olen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 375 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 376 | #if defined(MBEDTLS_FS_IO) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 377 | /** | 
| Hanno Becker | b2034b7 | 2017-04-26 11:46:46 +0100 | [diff] [blame] | 378 | * \brief          Read MPI from a line in an opened file | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 379 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 380 | * \param X        Destination MPI | 
|  | 381 | * \param radix    Input numeric base | 
|  | 382 | * \param fin      Input file handle | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 383 | * | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 384 | * \return         0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if | 
| Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 385 | *                 the file read buffer is too small or a | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 386 | *                 MBEDTLS_ERR_MPI_XXX error code | 
| Hanno Becker | b2034b7 | 2017-04-26 11:46:46 +0100 | [diff] [blame] | 387 | * | 
|  | 388 | * \note           On success, this function advances the file stream | 
|  | 389 | *                 to the end of the current line or to EOF. | 
|  | 390 | * | 
|  | 391 | *                 The function returns 0 on an empty line. | 
|  | 392 | * | 
|  | 393 | *                 Leading whitespaces are ignored, as is a | 
|  | 394 | *                 '0x' prefix for radix 16. | 
|  | 395 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 396 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 397 | int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 398 |  | 
|  | 399 | /** | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 400 | * \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] | 401 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 402 | * \param p        Prefix, can be NULL | 
|  | 403 | * \param X        Source MPI | 
|  | 404 | * \param radix    Output numeric base | 
|  | 405 | * \param fout     Output file handle (can be NULL) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 406 | * | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 407 | * \return         0 if successful, or a MBEDTLS_ERR_MPI_XXX error code | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 408 | * | 
|  | 409 | * \note           Set fout == NULL to print X on the console. | 
|  | 410 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 411 | int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout ); | 
|  | 412 | #endif /* MBEDTLS_FS_IO */ | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 413 |  | 
|  | 414 | /** | 
|  | 415 | * \brief          Import X from unsigned binary data, big endian | 
|  | 416 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 417 | * \param X        Destination MPI | 
|  | 418 | * \param buf      Input buffer | 
|  | 419 | * \param buflen   Input buffer size | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 420 | * | 
|  | 421 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 422 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 423 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 424 | int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 425 |  | 
|  | 426 | /** | 
| Manuel Pégourié-Gonnard | 3926a2c | 2014-06-25 12:57:47 +0200 | [diff] [blame] | 427 | * \brief          Export X into unsigned binary data, big endian. | 
|  | 428 | *                 Always fills the whole buffer, which will start with zeros | 
|  | 429 | *                 if the number is smaller. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 430 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 431 | * \param X        Source MPI | 
|  | 432 | * \param buf      Output buffer | 
|  | 433 | * \param buflen   Output buffer size | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 434 | * | 
|  | 435 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 436 | *                 MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 437 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 438 | int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 439 |  | 
|  | 440 | /** | 
|  | 441 | * \brief          Left-shift: X <<= count | 
|  | 442 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 443 | * \param X        MPI to shift | 
|  | 444 | * \param count    Amount to shift | 
|  | 445 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 446 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 447 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 448 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 449 | int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 450 |  | 
|  | 451 | /** | 
|  | 452 | * \brief          Right-shift: X >>= count | 
|  | 453 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 454 | * \param X        MPI to shift | 
|  | 455 | * \param count    Amount to shift | 
|  | 456 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 457 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 458 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 459 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 460 | int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 461 |  | 
|  | 462 | /** | 
|  | 463 | * \brief          Compare unsigned values | 
|  | 464 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 465 | * \param X        Left-hand MPI | 
|  | 466 | * \param Y        Right-hand MPI | 
|  | 467 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 468 | * \return         1 if |X| is greater than |Y|, | 
|  | 469 | *                -1 if |X| is lesser  than |Y| or | 
|  | 470 | *                 0 if |X| is equal to |Y| | 
|  | 471 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 472 | int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 473 |  | 
|  | 474 | /** | 
|  | 475 | * \brief          Compare signed values | 
|  | 476 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 477 | * \param X        Left-hand MPI | 
|  | 478 | * \param Y        Right-hand MPI | 
|  | 479 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 480 | * \return         1 if X is greater than Y, | 
|  | 481 | *                -1 if X is lesser  than Y or | 
|  | 482 | *                 0 if X is equal to Y | 
|  | 483 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 484 | int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 485 |  | 
|  | 486 | /** | 
|  | 487 | * \brief          Compare signed values | 
|  | 488 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 489 | * \param X        Left-hand MPI | 
|  | 490 | * \param z        The integer value to compare to | 
|  | 491 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 492 | * \return         1 if X is greater than z, | 
|  | 493 | *                -1 if X is lesser  than z or | 
|  | 494 | *                 0 if X is equal to z | 
|  | 495 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 496 | int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 497 |  | 
|  | 498 | /** | 
|  | 499 | * \brief          Unsigned addition: 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, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 506 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 507 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 508 | int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 509 |  | 
|  | 510 | /** | 
| Paul Bakker | 60b1d10 | 2013-10-29 10:02:51 +0100 | [diff] [blame] | 511 | * \brief          Unsigned subtraction: X = |A| - |B| | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 512 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 513 | * \param X        Destination MPI | 
|  | 514 | * \param A        Left-hand MPI | 
|  | 515 | * \param B        Right-hand MPI | 
|  | 516 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 517 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 518 | *                 MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 519 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 520 | int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 521 |  | 
|  | 522 | /** | 
|  | 523 | * \brief          Signed addition: X = A + B | 
|  | 524 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 525 | * \param X        Destination MPI | 
|  | 526 | * \param A        Left-hand MPI | 
|  | 527 | * \param B        Right-hand MPI | 
|  | 528 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 529 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 530 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 531 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 532 | int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 533 |  | 
|  | 534 | /** | 
| Paul Bakker | 60b1d10 | 2013-10-29 10:02:51 +0100 | [diff] [blame] | 535 | * \brief          Signed subtraction: X = A - B | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 536 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 537 | * \param X        Destination MPI | 
|  | 538 | * \param A        Left-hand MPI | 
|  | 539 | * \param B        Right-hand MPI | 
|  | 540 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 541 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 542 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 543 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 544 | int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 545 |  | 
|  | 546 | /** | 
|  | 547 | * \brief          Signed addition: X = A + b | 
|  | 548 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 549 | * \param X        Destination MPI | 
|  | 550 | * \param A        Left-hand MPI | 
|  | 551 | * \param b        The integer value to add | 
|  | 552 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 553 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 554 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 555 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 556 | int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 557 |  | 
|  | 558 | /** | 
| Paul Bakker | 60b1d10 | 2013-10-29 10:02:51 +0100 | [diff] [blame] | 559 | * \brief          Signed subtraction: X = A - b | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 560 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 561 | * \param X        Destination MPI | 
|  | 562 | * \param A        Left-hand MPI | 
|  | 563 | * \param b        The integer value to subtract | 
|  | 564 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 565 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 566 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 567 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 568 | int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 569 |  | 
|  | 570 | /** | 
|  | 571 | * \brief          Baseline multiplication: X = A * B | 
|  | 572 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 573 | * \param X        Destination MPI | 
|  | 574 | * \param A        Left-hand MPI | 
|  | 575 | * \param B        Right-hand MPI | 
|  | 576 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 577 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 578 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 579 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 580 | int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 581 |  | 
|  | 582 | /** | 
|  | 583 | * \brief          Baseline multiplication: X = A * b | 
|  | 584 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 585 | * \param X        Destination MPI | 
|  | 586 | * \param A        Left-hand MPI | 
| Manuel Pégourié-Gonnard | 35f1d7f | 2015-03-19 12:42:40 +0000 | [diff] [blame] | 587 | * \param b        The unsigned integer value to multiply with | 
|  | 588 | * | 
|  | 589 | * \note           b is unsigned | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 590 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 591 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 592 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 593 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 594 | int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 595 |  | 
|  | 596 | /** | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 597 | * \brief          Division by mbedtls_mpi: A = Q * B + R | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 598 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 599 | * \param Q        Destination MPI for the quotient | 
|  | 600 | * \param R        Destination MPI for the rest value | 
|  | 601 | * \param A        Left-hand MPI | 
|  | 602 | * \param B        Right-hand MPI | 
|  | 603 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 604 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 605 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 606 | *                 MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 607 | * | 
|  | 608 | * \note           Either Q or R can be NULL. | 
|  | 609 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 610 | int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 611 |  | 
|  | 612 | /** | 
|  | 613 | * \brief          Division by int: A = Q * b + R | 
|  | 614 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 615 | * \param Q        Destination MPI for the quotient | 
|  | 616 | * \param R        Destination MPI for the rest value | 
|  | 617 | * \param A        Left-hand MPI | 
|  | 618 | * \param b        Integer to divide by | 
|  | 619 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 620 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 621 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 622 | *                 MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 623 | * | 
|  | 624 | * \note           Either Q or R can be NULL. | 
|  | 625 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 626 | int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 627 |  | 
|  | 628 | /** | 
|  | 629 | * \brief          Modulo: R = A mod B | 
|  | 630 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 631 | * \param R        Destination MPI for the rest value | 
|  | 632 | * \param A        Left-hand MPI | 
|  | 633 | * \param B        Right-hand MPI | 
|  | 634 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 635 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 636 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 637 | *                 MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0, | 
|  | 638 | *                 MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 639 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 640 | int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 641 |  | 
|  | 642 | /** | 
|  | 643 | * \brief          Modulo: r = A mod b | 
|  | 644 | * | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 645 | * \param r        Destination mbedtls_mpi_uint | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 646 | * \param A        Left-hand MPI | 
|  | 647 | * \param b        Integer to divide by | 
|  | 648 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 649 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 650 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 651 | *                 MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0, | 
|  | 652 | *                 MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 653 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 654 | int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 655 |  | 
|  | 656 | /** | 
|  | 657 | * \brief          Sliding-window exponentiation: X = A^E mod N | 
|  | 658 | * | 
| Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 659 | * \param X        Destination MPI | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 660 | * \param A        Left-hand MPI | 
|  | 661 | * \param E        Exponent MPI | 
|  | 662 | * \param N        Modular MPI | 
|  | 663 | * \param _RR      Speed-up MPI used for recalculations | 
|  | 664 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 665 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 666 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 667 | *                 MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 668 | *                 if E is negative | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 669 | * | 
|  | 670 | * \note           _RR is used to avoid re-computing R*R mod N across | 
|  | 671 | *                 multiple calls, which speeds up things a bit. It can | 
|  | 672 | *                 be set to NULL if the extra performance is unneeded. | 
|  | 673 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 674 | int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 675 |  | 
|  | 676 | /** | 
| Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 677 | * \brief          Fill an MPI X with size bytes of random | 
|  | 678 | * | 
|  | 679 | * \param X        Destination MPI | 
|  | 680 | * \param size     Size in bytes | 
|  | 681 | * \param f_rng    RNG function | 
|  | 682 | * \param p_rng    RNG parameter | 
|  | 683 | * | 
|  | 684 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 685 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 686 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 687 | int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size, | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 688 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 689 | void *p_rng ); | 
| Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 690 |  | 
|  | 691 | /** | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 692 | * \brief          Greatest common divisor: G = gcd(A, B) | 
|  | 693 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 694 | * \param G        Destination MPI | 
|  | 695 | * \param A        Left-hand MPI | 
|  | 696 | * \param B        Right-hand MPI | 
|  | 697 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 698 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 699 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 700 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 701 | int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 702 |  | 
|  | 703 | /** | 
|  | 704 | * \brief          Modular inverse: X = A^-1 mod N | 
|  | 705 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 706 | * \param X        Destination MPI | 
|  | 707 | * \param A        Left-hand MPI | 
|  | 708 | * \param N        Right-hand MPI | 
|  | 709 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 710 | * \return         0 if successful, | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 711 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, | 
| Hanno Becker | 4bcb491 | 2017-04-18 15:49:39 +0100 | [diff] [blame] | 712 | *                 MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is <= 1, | 
|  | 713 | MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N. | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 714 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 715 | int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 716 |  | 
|  | 717 | /** | 
|  | 718 | * \brief          Miller-Rabin primality test | 
|  | 719 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 720 | * \param X        MPI to check | 
|  | 721 | * \param f_rng    RNG function | 
|  | 722 | * \param p_rng    RNG parameter | 
|  | 723 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 724 | * \return         0 if successful (probably prime), | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 725 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 726 | *                 MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 727 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 728 | int mbedtls_mpi_is_prime( const mbedtls_mpi *X, | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 729 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 730 | void *p_rng ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 731 |  | 
|  | 732 | /** | 
|  | 733 | * \brief          Prime number generation | 
|  | 734 | * | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 735 | * \param X        Destination MPI | 
| Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 736 | * \param nbits    Required size of X in bits | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 737 | *                 ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS ) | 
| Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 738 | * \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] | 739 | * \param f_rng    RNG function | 
|  | 740 | * \param p_rng    RNG parameter | 
|  | 741 | * | 
|  | 742 | * \return         0 if successful (probably prime), | 
| Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 743 | *                 MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 744 | *                 MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3 | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 745 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 746 | int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag, | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 747 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 748 | void *p_rng ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 749 |  | 
|  | 750 | /** | 
|  | 751 | * \brief          Checkup routine | 
|  | 752 | * | 
|  | 753 | * \return         0 if successful, or 1 if the test failed | 
|  | 754 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 755 | int mbedtls_mpi_self_test( int verbose ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 756 |  | 
|  | 757 | #ifdef __cplusplus | 
|  | 758 | } | 
|  | 759 | #endif | 
|  | 760 |  | 
|  | 761 | #endif /* bignum.h */ |