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