blob: a2fa3bdaf779f467a438d1f487a4f84a07643030 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file bignum.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Darryl Greena40a1012018-01-05 15:33:17 +00004 * \brief Multi-precision integer library
5 */
6/*
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02007 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02008 * 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é-Gonnard37ff1402015-09-04 14:21:07 +020015 *
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 Bakkerb96f1542010-07-18 20:36:00 +000027 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020028 * **********
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 Bakker5121ce52009-01-03 21:22:43 +000048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#ifndef MBEDTLS_BIGNUM_H
50#define MBEDTLS_BIGNUM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkercf0360a2012-01-20 10:08:14 +000053#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020054#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#endif
Paul Bakkercf0360a2012-01-20 10:08:14 +000057
Rich Evans00ab4702015-02-06 13:43:58 +000058#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020059#include <stdint.h>
Rich Evans00ab4702015-02-06 13:43:58 +000060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnarde94e6e52015-01-19 15:01:53 +000062#include <stdio.h>
63#endif
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#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é-Gonnard6a8ca332015-05-28 09:33:39 +020072#define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 /**< Memory allocation failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000075
76/*
Paul Bakkerf9688572011-05-05 10:00:45 +000077 * Maximum size MPIs are allowed to grow to in number of limbs.
78 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#define MBEDTLS_MPI_MAX_LIMBS 10000
Paul Bakkerf9688572011-05-05 10:00:45 +000080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if !defined(MBEDTLS_MPI_WINDOW_SIZE)
Paul Bakkerf9688572011-05-05 10:00:45 +000082/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000083 * Maximum window size used for modular exponentiation. Default: 6
84 * Minimum value: 1. Maximum value: 6.
85 *
Daniel Otted9854682020-09-07 13:07:14 +020086 * Result is an array of ( 2 ** MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
Paul Bakkerb6d5f082011-11-25 11:52:11 +000087 * for the sliding window calculation. (So 64 by default)
88 *
89 * Reduction in size, reduces speed.
90 */
Daniel Ottee6f2fb42020-09-07 13:06:40 +020091#define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum window size used. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#endif /* !MBEDTLS_MPI_WINDOW_SIZE */
Paul Bakkerb6d5f082011-11-25 11:52:11 +000093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094#if !defined(MBEDTLS_MPI_MAX_SIZE)
Paul Bakkerb6d5f082011-11-25 11:52:11 +000095/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000096 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakkerf626e1d2013-01-21 12:10:00 +010097 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +000098 *
Hanno Beckerefeef6c2018-01-05 08:07:47 +000099 * Note: Calculations can temporarily result in larger MPIs. So the number
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
103#endif /* !MBEDTLS_MPI_MAX_SIZE */
Paul Bakker9bcf16c2013-06-24 19:31:17 +0200104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105#define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000106
107/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 * 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 Bakkercb37aa52011-11-30 16:00:20 +0000110 * 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 Bakkerf9183102012-09-27 20:42:35 +0000115 * Autosized at compile time for at least a 10 char label, a minimum radix
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
Paul Bakkerf9183102012-09-27 20:42:35 +0000117 *
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
Paul Bakkerf9183102012-09-27 20:42:35 +0000123 * LabelSize + 6
Paul Bakkercb37aa52011-11-30 16:00:20 +0000124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#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 Bakkercb37aa52011-11-30 16:00:20 +0000128
129/*
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200130 * Define the base integer type, architecture-wise.
131 *
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100132 * 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 Garcia93db11a2017-07-20 12:11:19 +0100136 * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100137 * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100139#if !defined(MBEDTLS_HAVE_INT32)
140 #if defined(_MSC_VER) && defined(_M_AMD64)
141 /* Always choose 64-bit when using MSC */
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100142 #if !defined(MBEDTLS_HAVE_INT64)
143 #define MBEDTLS_HAVE_INT64
144 #endif /* !MBEDTLS_HAVE_INT64 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100145 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 Garciad7fce002017-07-20 11:49:32 +0100153 #if !defined(MBEDTLS_HAVE_INT64)
154 #define MBEDTLS_HAVE_INT64
155 #endif /* MBEDTLS_HAVE_INT64 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100156 typedef int64_t mbedtls_mpi_sint;
157 typedef uint64_t mbedtls_mpi_uint;
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100158 #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 Garciaaa27dfe2017-05-04 11:05:55 +0100163 #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100164 /*
165 * __ARMCC_VERSION is defined for both armcc and armclang and
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100166 * __aarch64__ is only defined by armclang when compiling 64-bit code
167 */
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100168 #if !defined(MBEDTLS_HAVE_INT64)
169 #define MBEDTLS_HAVE_INT64
170 #endif /* !MBEDTLS_HAVE_INT64 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100171 typedef int64_t mbedtls_mpi_sint;
172 typedef uint64_t mbedtls_mpi_uint;
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100173 #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 Garciaaa27dfe2017-05-04 11:05:55 +0100182 #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 Garciad7fce002017-07-20 11:49:32 +0100192 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
193 typedef uint64_t mbedtls_t_udbl;
Andres Amaya Garciadf1486a2017-07-20 17:33:09 +0100194 #define MBEDTLS_HAVE_UDBL
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100195 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100196#endif /* !MBEDTLS_HAVE_INT64 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000197
Paul Bakker407a0da2013-06-27 14:29:21 +0200198#ifdef __cplusplus
199extern "C" {
200#endif
201
Paul Bakker5121ce52009-01-03 21:22:43 +0000202/**
203 * \brief MPI structure
204 */
205typedef struct
206{
Janos Follath9741fa62019-10-28 12:07:52 +0000207 int s; /*!< Sign: -1 if the mpi is negative, 1 otherwise */
Paul Bakker23986e52011-04-24 08:57:21 +0000208 size_t n; /*!< total # of limbs */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 mbedtls_mpi_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000210}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211mbedtls_mpi;
Paul Bakker5121ce52009-01-03 21:22:43 +0000212
Paul Bakker5121ce52009-01-03 21:22:43 +0000213/**
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200214 * \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 Bakker6c591fa2011-05-05 11:49:20 +0000217 *
218 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220void mbedtls_mpi_init( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000221
222/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000223 * \brief Unallocate one MPI
224 *
225 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227void mbedtls_mpi_free( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000228
229/**
230 * \brief Enlarge to the specified number of limbs
231 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000232 * \param X MPI to grow
233 * \param nblimbs The target number of limbs
234 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200236 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
240/**
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100241 * \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é-Gonnard6a8ca332015-05-28 09:33:39 +0200247 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100250
251/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 * \brief Copy the contents of Y into X
253 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000254 * \param X Destination MPI
255 * \param Y Source MPI
256 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200258 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000261
262/**
263 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000264 *
265 * \param X First MPI value
266 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000269
270/**
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100271 * \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é-Gonnarda60fe892013-12-04 21:41:50 +0100275 * \param assign 1: perform the assignment, 0: keep X's original value
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100276 *
277 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200278 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100279 *
280 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 * if( assign ) mbedtls_mpi_copy( X, Y );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100282 * except that it avoids leaking any information about whether
283 * the assignment was done or not (the above code may leak
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100284 * information through branch prediction and/or memory access
285 * patterns analysis).
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100288
289/**
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100290 * \brief Safe conditional swap X <-> Y if swap is 1
291 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 * \param X First mbedtls_mpi value
293 * \param Y Second mbedtls_mpi value
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100294 * \param assign 1: perform the swap, 0: keep X and Y's original values
295 *
296 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200297 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100298 *
299 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 * if( assign ) mbedtls_mpi_swap( X, Y );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100301 * 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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100307
308/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 * \brief Set value from integer
310 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000311 * \param X MPI to set
312 * \param z Value to use
313 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000314 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200315 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000318
Paul Bakker9a736322012-11-14 12:39:52 +0000319/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000320 * \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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000328
Paul Bakker9a736322012-11-14 12:39:52 +0000329/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000330 * \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é-Gonnard6a8ca332015-05-28 09:33:39 +0200340 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
Paul Bakker2f5947e2011-05-18 15:47:11 +0000342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000344
Paul Bakker5121ce52009-01-03 21:22:43 +0000345/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000346 * \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 Bakker13e2dfe2009-07-28 07:18:38 +0000350 *
351 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000354
355/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000356 * \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 Bakker13e2dfe2009-07-28 07:18:38 +0000360 *
361 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200363size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000364
365/**
366 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000367 *
368 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370size_t mbedtls_mpi_size( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000371
372/**
373 * \brief Import from an ASCII string
374 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000375 * \param X Destination MPI
376 * \param radix Input numeric base
377 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000382
383/**
384 * \brief Export into an ASCII string
385 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000386 * \param X Source MPI
387 * \param radix Output numeric base
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100388 * \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 Bakker5121ce52009-01-03 21:22:43 +0000391 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code.
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100393 * *olen is always updated to reflect the amount
Paul Bakkerff60ee62010-03-16 21:09:09 +0000394 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 *
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100396 * \note Call this function with buflen = 0 to obtain the
397 * minimum required buffer size in *olen.
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100399int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
400 char *buf, size_t buflen, size_t *olen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000403/**
Hanno Beckerb2034b72017-04-26 11:46:46 +0100404 * \brief Read MPI from a line in an opened file
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000406 * \param X Destination MPI
407 * \param radix Input numeric base
408 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
Paul Bakkercb37aa52011-11-30 16:00:20 +0000411 * the file read buffer is too small or a
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 * MBEDTLS_ERR_MPI_XXX error code
Hanno Beckerb2034b72017-04-26 11:46:46 +0100413 *
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 Bakker5121ce52009-01-03 21:22:43 +0000422 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
Paul Bakker5121ce52009-01-03 21:22:43 +0000424
425/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000426 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000428 * \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 Bakker5121ce52009-01-03 21:22:43 +0000432 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 *
435 * \note Set fout == NULL to print X on the console.
436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
438#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
440/**
441 * \brief Import X from unsigned binary data, big endian
442 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000443 * \param X Destination MPI
444 * \param buf Input buffer
445 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 *
447 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200448 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000451
452/**
Manuel Pégourié-Gonnard3926a2c2014-06-25 12:57:47 +0200453 * \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 Bakker5121ce52009-01-03 21:22:43 +0000456 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000457 * \param X Source MPI
458 * \param buf Output buffer
459 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 *
461 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000465
466/**
467 * \brief Left-shift: X <<= count
468 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000469 * \param X MPI to shift
470 * \param count Amount to shift
471 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200473 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
477/**
478 * \brief Right-shift: X >>= count
479 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000480 * \param X MPI to shift
481 * \param count Amount to shift
482 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000483 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200484 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000487
488/**
489 * \brief Compare unsigned values
490 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000491 * \param X Left-hand MPI
492 * \param Y Right-hand MPI
493 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000494 * \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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000499
500/**
501 * \brief Compare signed values
502 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000503 * \param X Left-hand MPI
504 * \param Y Right-hand MPI
505 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 * \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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
512/**
Janos Follathc3b376e2019-10-11 14:21:53 +0100513 * \brief Check if an MPI is less than the other in constant time.
Janos Follathe0187b92019-09-05 14:47:19 +0100514 *
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 Follathc3b376e2019-10-11 14:21:53 +0100520 * \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 Follathe0187b92019-09-05 14:47:19 +0100522 *
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 Follathc3b376e2019-10-11 14:21:53 +0100527int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
528 unsigned *ret );
Janos Follathe0187b92019-09-05 14:47:19 +0100529
530/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 * \brief Compare signed values
532 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000533 * \param X Left-hand MPI
534 * \param z The integer value to compare to
535 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 * \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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000541
542/**
543 * \brief Unsigned addition: X = |A| + |B|
544 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000545 * \param X Destination MPI
546 * \param A Left-hand MPI
547 * \param B Right-hand MPI
548 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000549 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200550 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000553
554/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100555 * \brief Unsigned subtraction: X = |A| - |B|
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000557 * \param X Destination MPI
558 * \param A Left-hand MPI
559 * \param B Right-hand MPI
560 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000561 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000565
566/**
567 * \brief Signed addition: X = A + B
568 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000569 * \param X Destination MPI
570 * \param A Left-hand MPI
571 * \param B Right-hand MPI
572 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000573 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200574 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000577
578/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100579 * \brief Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +0000580 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000581 * \param X Destination MPI
582 * \param A Left-hand MPI
583 * \param B Right-hand MPI
584 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200586 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000589
590/**
591 * \brief Signed addition: X = A + b
592 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000593 * \param X Destination MPI
594 * \param A Left-hand MPI
595 * \param b The integer value to add
596 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000597 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200598 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
602/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100603 * \brief Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000605 * \param X Destination MPI
606 * \param A Left-hand MPI
607 * \param b The integer value to subtract
608 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000609 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200610 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000613
614/**
615 * \brief Baseline multiplication: X = A * B
616 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000617 * \param X Destination MPI
618 * \param A Left-hand MPI
619 * \param B Right-hand MPI
620 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000621 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200622 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000625
626/**
627 * \brief Baseline multiplication: X = A * b
628 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000629 * \param X Destination MPI
630 * \param A Left-hand MPI
Manuel Pégourié-Gonnard35f1d7f2015-03-19 12:42:40 +0000631 * \param b The unsigned integer value to multiply with
632 *
633 * \note b is unsigned
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000634 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200636 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000639
640/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 * \brief Division by mbedtls_mpi: A = Q * B + R
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000643 * \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 Bakker5121ce52009-01-03 21:22:43 +0000648 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200649 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 *
652 * \note Either Q or R can be NULL.
653 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
656/**
657 * \brief Division by int: A = Q * b + R
658 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000659 * \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 Bakker5121ce52009-01-03 21:22:43 +0000664 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200665 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 *
668 * \note Either Q or R can be NULL.
669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672/**
673 * \brief Modulo: R = A mod B
674 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000675 * \param R Destination MPI for the rest value
676 * \param A Left-hand MPI
677 * \param B Right-hand MPI
678 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200680 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0,
682 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000685
686/**
687 * \brief Modulo: r = A mod b
688 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 * \param r Destination mbedtls_mpi_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000690 * \param A Left-hand MPI
691 * \param b Integer to divide by
692 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200694 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0,
696 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000699
700/**
701 * \brief Sliding-window exponentiation: X = A^E mod N
702 *
Paul Bakker9af723c2014-05-01 13:03:14 +0200703 * \param X Destination MPI
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000704 * \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 Bakker5121ce52009-01-03 21:22:43 +0000709 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200710 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200712 * if E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000713 *
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000719
720/**
Paul Bakker287781a2011-03-26 13:18:49 +0000721 * \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é-Gonnard6a8ca332015-05-28 09:33:39 +0200729 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Hanno Becker15f2b3e2017-10-17 15:17:05 +0100730 *
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 Bakker287781a2011-03-26 13:18:49 +0000734 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000736 int (*f_rng)(void *, unsigned char *, size_t),
737 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000738
739/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000740 * \brief Greatest common divisor: G = gcd(A, B)
741 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000742 * \param G Destination MPI
743 * \param A Left-hand MPI
744 * \param B Right-hand MPI
745 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000746 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200747 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000750
751/**
752 * \brief Modular inverse: X = A^-1 mod N
753 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000754 * \param X Destination MPI
755 * \param A Left-hand MPI
756 * \param N Right-hand MPI
757 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000758 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200759 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Hanno Becker4bcb4912017-04-18 15:49:39 +0100760 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is <= 1,
761 MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000762 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200763int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000764
765/**
766 * \brief Miller-Rabin primality test
767 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000768 * \param X MPI to check
769 * \param f_rng RNG function
770 * \param p_rng RNG parameter
771 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000772 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200773 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000775 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000777 int (*f_rng)(void *, unsigned char *, size_t),
778 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000779
780/**
781 * \brief Prime number generation
782 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000783 * \param X Destination MPI
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200784 * \param nbits Required size of X in bits
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000786 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000787 * \param f_rng RNG function
788 * \param p_rng RNG parameter
789 *
790 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200791 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000793 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000795 int (*f_rng)(void *, unsigned char *, size_t),
796 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000797
798/**
799 * \brief Checkup routine
800 *
801 * \return 0 if successful, or 1 if the test failed
802 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803int mbedtls_mpi_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000804
805#ifdef __cplusplus
806}
807#endif
808
809#endif /* bignum.h */