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