blob: 956c57f3f49fb383f82359da2a05c7521b56f91b [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/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * **********
48 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000049 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#ifndef MBEDTLS_BIGNUM_H
52#define MBEDTLS_BIGNUM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkercf0360a2012-01-20 10:08:14 +000055#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020058#endif
Paul Bakkercf0360a2012-01-20 10:08:14 +000059
Rich Evans00ab4702015-02-06 13:43:58 +000060#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020061#include <stdint.h>
Rich Evans00ab4702015-02-06 13:43:58 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnarde94e6e52015-01-19 15:01:53 +000064#include <stdio.h>
65#endif
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
68#define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
69#define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
70#define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
71#define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
72#define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
73#define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020074#define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 /**< Memory allocation failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000077
78/*
Paul Bakkerf9688572011-05-05 10:00:45 +000079 * Maximum size MPIs are allowed to grow to in number of limbs.
80 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#define MBEDTLS_MPI_MAX_LIMBS 10000
Paul Bakkerf9688572011-05-05 10:00:45 +000082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#if !defined(MBEDTLS_MPI_WINDOW_SIZE)
Paul Bakkerf9688572011-05-05 10:00:45 +000084/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000085 * Maximum window size used for modular exponentiation. Default: 6
86 * Minimum value: 1. Maximum value: 6.
87 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
Paul Bakkerb6d5f082011-11-25 11:52:11 +000089 * for the sliding window calculation. (So 64 by default)
90 *
91 * Reduction in size, reduces speed.
92 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
94#endif /* !MBEDTLS_MPI_WINDOW_SIZE */
Paul Bakkerb6d5f082011-11-25 11:52:11 +000095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#if !defined(MBEDTLS_MPI_MAX_SIZE)
Paul Bakkerb6d5f082011-11-25 11:52:11 +000097/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000098 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakkerf626e1d2013-01-21 12:10:00 +010099 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000100 *
Hanno Beckerefeef6c2018-01-05 08:07:47 +0000101 * Note: Calculations can temporarily result in larger MPIs. So the number
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104#define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
105#endif /* !MBEDTLS_MPI_MAX_SIZE */
Paul Bakker9bcf16c2013-06-24 19:31:17 +0200106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
Paul Bakkerfe3256e2011-11-25 12:11:43 +0000108
109/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 * When reading from files with mbedtls_mpi_read_file() and writing to files with
111 * mbedtls_mpi_write_file() the buffer should have space
Paul Bakkercb37aa52011-11-30 16:00:20 +0000112 * for a (short) label, the MPI (in the provided radix), the newline
113 * characters and the '\0'.
114 *
115 * By default we assume at least a 10 char label, a minimum radix of 10
116 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
Paul Bakkerf9183102012-09-27 20:42:35 +0000117 * Autosized at compile time for at least a 10 char label, a minimum radix
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
Paul Bakkerf9183102012-09-27 20:42:35 +0000119 *
120 * This used to be statically sized to 1250 for a maximum of 4096 bit
121 * numbers (1234 decimal chars).
122 *
123 * Calculate using the formula:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
Paul Bakkerf9183102012-09-27 20:42:35 +0000125 * LabelSize + 6
Paul Bakkercb37aa52011-11-30 16:00:20 +0000126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127#define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
128#define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
129#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 +0000130
131/*
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200132 * Define the base integer type, architecture-wise.
133 *
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100134 * 32 or 64-bit integer types can be forced regardless of the underlying
135 * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
136 * respectively and undefining MBEDTLS_HAVE_ASM.
137 *
Andres Amaya Garcia93db11a2017-07-20 12:11:19 +0100138 * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100139 * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100141#if !defined(MBEDTLS_HAVE_INT32)
142 #if defined(_MSC_VER) && defined(_M_AMD64)
143 /* Always choose 64-bit when using MSC */
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100144 #if !defined(MBEDTLS_HAVE_INT64)
145 #define MBEDTLS_HAVE_INT64
146 #endif /* !MBEDTLS_HAVE_INT64 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100147 typedef int64_t mbedtls_mpi_sint;
148 typedef uint64_t mbedtls_mpi_uint;
149 #elif defined(__GNUC__) && ( \
150 defined(__amd64__) || defined(__x86_64__) || \
151 defined(__ppc64__) || defined(__powerpc64__) || \
152 defined(__ia64__) || defined(__alpha__) || \
153 ( defined(__sparc__) && defined(__arch64__) ) || \
154 defined(__s390x__) || defined(__mips64) )
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100155 #if !defined(MBEDTLS_HAVE_INT64)
156 #define MBEDTLS_HAVE_INT64
157 #endif /* MBEDTLS_HAVE_INT64 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100158 typedef int64_t mbedtls_mpi_sint;
159 typedef uint64_t mbedtls_mpi_uint;
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100160 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
161 /* mbedtls_t_udbl defined as 128-bit unsigned int */
162 typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
163 #define MBEDTLS_HAVE_UDBL
164 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100165 #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100166 /*
167 * __ARMCC_VERSION is defined for both armcc and armclang and
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100168 * __aarch64__ is only defined by armclang when compiling 64-bit code
169 */
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100170 #if !defined(MBEDTLS_HAVE_INT64)
171 #define MBEDTLS_HAVE_INT64
172 #endif /* !MBEDTLS_HAVE_INT64 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100173 typedef int64_t mbedtls_mpi_sint;
174 typedef uint64_t mbedtls_mpi_uint;
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100175 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
176 /* mbedtls_t_udbl defined as 128-bit unsigned int */
177 typedef __uint128_t mbedtls_t_udbl;
178 #define MBEDTLS_HAVE_UDBL
179 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
180 #elif defined(MBEDTLS_HAVE_INT64)
181 /* Force 64-bit integers with unknown compiler */
182 typedef int64_t mbedtls_mpi_sint;
183 typedef uint64_t mbedtls_mpi_uint;
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100184 #endif
185#endif /* !MBEDTLS_HAVE_INT32 */
186
187#if !defined(MBEDTLS_HAVE_INT64)
188 /* Default to 32-bit compilation */
189 #if !defined(MBEDTLS_HAVE_INT32)
190 #define MBEDTLS_HAVE_INT32
191 #endif /* !MBEDTLS_HAVE_INT32 */
192 typedef int32_t mbedtls_mpi_sint;
193 typedef uint32_t mbedtls_mpi_uint;
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100194 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
195 typedef uint64_t mbedtls_t_udbl;
Andres Amaya Garciadf1486a2017-07-20 17:33:09 +0100196 #define MBEDTLS_HAVE_UDBL
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100197 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100198#endif /* !MBEDTLS_HAVE_INT64 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
Paul Bakker407a0da2013-06-27 14:29:21 +0200200#ifdef __cplusplus
201extern "C" {
202#endif
203
Paul Bakker5121ce52009-01-03 21:22:43 +0000204/**
205 * \brief MPI structure
206 */
207typedef struct
208{
Janos Follath9741fa62019-10-28 12:07:52 +0000209 int s; /*!< Sign: -1 if the mpi is negative, 1 otherwise */
Paul Bakker23986e52011-04-24 08:57:21 +0000210 size_t n; /*!< total # of limbs */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_mpi_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000212}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213mbedtls_mpi;
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
Paul Bakker5121ce52009-01-03 21:22:43 +0000215/**
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200216 * \brief Initialize one MPI (make internal references valid)
217 * This just makes it ready to be set or freed,
218 * but does not define a value for the MPI.
Paul Bakker6c591fa2011-05-05 11:49:20 +0000219 *
220 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222void mbedtls_mpi_init( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223
224/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000225 * \brief Unallocate one MPI
226 *
227 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229void mbedtls_mpi_free( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000230
231/**
232 * \brief Enlarge to the specified number of limbs
233 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000234 * \param X MPI to grow
235 * \param nblimbs The target number of limbs
236 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000237 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200238 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000241
242/**
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100243 * \brief Resize down, keeping at least the specified number of limbs
244 *
245 * \param X MPI to shrink
246 * \param nblimbs The minimum number of limbs to keep
247 *
248 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200249 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100250 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100252
253/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 * \brief Copy the contents of Y into X
255 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000256 * \param X Destination MPI
257 * \param Y Source MPI
258 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200260 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000263
264/**
265 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000266 *
267 * \param X First MPI value
268 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
272/**
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100273 * \brief Safe conditional assignement X = Y if assign is 1
274 *
275 * \param X MPI to conditionally assign to
276 * \param Y Value to be assigned
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100277 * \param assign 1: perform the assignment, 0: keep X's original value
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100278 *
279 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200280 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100281 *
282 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 * if( assign ) mbedtls_mpi_copy( X, Y );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100284 * except that it avoids leaking any information about whether
285 * the assignment was done or not (the above code may leak
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100286 * information through branch prediction and/or memory access
287 * patterns analysis).
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100290
291/**
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100292 * \brief Safe conditional swap X <-> Y if swap is 1
293 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 * \param X First mbedtls_mpi value
295 * \param Y Second mbedtls_mpi value
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100296 * \param assign 1: perform the swap, 0: keep X and Y's original values
297 *
298 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200299 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100300 *
301 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 * if( assign ) mbedtls_mpi_swap( X, Y );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100303 * except that it avoids leaking any information about whether
304 * the assignment was done or not (the above code may leak
305 * information through branch prediction and/or memory access
306 * patterns analysis).
307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100309
310/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 * \brief Set value from integer
312 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000313 * \param X MPI to set
314 * \param z Value to use
315 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200317 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
Paul Bakker9a736322012-11-14 12:39:52 +0000321/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000322 * \brief Get a specific bit from X
323 *
324 * \param X MPI to use
325 * \param pos Zero-based index of the bit in X
326 *
327 * \return Either a 0 or a 1
328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000330
Paul Bakker9a736322012-11-14 12:39:52 +0000331/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000332 * \brief Set a bit of X to a specific value of 0 or 1
333 *
334 * \note Will grow X if necessary to set a bit to 1 in a not yet
335 * existing limb. Will not grow if bit should be set to 0
336 *
337 * \param X MPI to use
338 * \param pos Zero-based index of the bit in X
339 * \param val The value to set the bit to (0 or 1)
340 *
341 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200342 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
Paul Bakker2f5947e2011-05-18 15:47:11 +0000344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000346
Paul Bakker5121ce52009-01-03 21:22:43 +0000347/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000348 * \brief Return the number of zero-bits before the least significant
349 * '1' bit
350 *
351 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000352 *
353 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000356
357/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000358 * \brief Return the number of bits up to and including the most
359 * significant '1' bit'
360 *
361 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000362 *
363 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200365size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000366
367/**
368 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000369 *
370 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372size_t mbedtls_mpi_size( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000373
374/**
375 * \brief Import from an ASCII string
376 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000377 * \param X Destination MPI
378 * \param radix Input numeric base
379 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000384
385/**
386 * \brief Export into an ASCII string
387 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000388 * \param X Source MPI
389 * \param radix Output numeric base
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100390 * \param buf Buffer to write the string to
391 * \param buflen Length of buf
392 * \param olen Length of the string written, including final NUL byte
Paul Bakker5121ce52009-01-03 21:22:43 +0000393 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code.
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100395 * *olen is always updated to reflect the amount
Paul Bakkerff60ee62010-03-16 21:09:09 +0000396 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 *
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100398 * \note Call this function with buflen = 0 to obtain the
399 * minimum required buffer size in *olen.
Paul Bakker5121ce52009-01-03 21:22:43 +0000400 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100401int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
402 char *buf, size_t buflen, size_t *olen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000405/**
Hanno Beckerb2034b72017-04-26 11:46:46 +0100406 * \brief Read MPI from a line in an opened file
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000408 * \param X Destination MPI
409 * \param radix Input numeric base
410 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
Paul Bakkercb37aa52011-11-30 16:00:20 +0000413 * the file read buffer is too small or a
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 * MBEDTLS_ERR_MPI_XXX error code
Hanno Beckerb2034b72017-04-26 11:46:46 +0100415 *
416 * \note On success, this function advances the file stream
417 * to the end of the current line or to EOF.
418 *
419 * The function returns 0 on an empty line.
420 *
421 * Leading whitespaces are ignored, as is a
422 * '0x' prefix for radix 16.
423 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
427/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000428 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000429 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000430 * \param p Prefix, can be NULL
431 * \param X Source MPI
432 * \param radix Output numeric base
433 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000436 *
437 * \note Set fout == NULL to print X on the console.
438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
440#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000441
442/**
443 * \brief Import X from unsigned binary data, big endian
444 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000445 * \param X Destination MPI
446 * \param buf Input buffer
447 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000448 *
449 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200450 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000453
454/**
Manuel Pégourié-Gonnard3926a2c2014-06-25 12:57:47 +0200455 * \brief Export X into unsigned binary data, big endian.
456 * Always fills the whole buffer, which will start with zeros
457 * if the number is smaller.
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000459 * \param X Source MPI
460 * \param buf Output buffer
461 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 *
463 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000465 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000467
468/**
469 * \brief Left-shift: X <<= count
470 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000471 * \param X MPI to shift
472 * \param count Amount to shift
473 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000474 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200475 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000478
479/**
480 * \brief Right-shift: X >>= count
481 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000482 * \param X MPI to shift
483 * \param count Amount to shift
484 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000485 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200486 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000489
490/**
491 * \brief Compare unsigned values
492 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000493 * \param X Left-hand MPI
494 * \param Y Right-hand MPI
495 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 * \return 1 if |X| is greater than |Y|,
497 * -1 if |X| is lesser than |Y| or
498 * 0 if |X| is equal to |Y|
499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000501
502/**
503 * \brief Compare signed values
504 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000505 * \param X Left-hand MPI
506 * \param Y Right-hand MPI
507 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 * \return 1 if X is greater than Y,
509 * -1 if X is lesser than Y or
510 * 0 if X is equal to Y
511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000513
514/**
Janos Follathc3b376e2019-10-11 14:21:53 +0100515 * \brief Check if an MPI is less than the other in constant time.
Janos Follathe0187b92019-09-05 14:47:19 +0100516 *
517 * \param X The left-hand MPI. This must point to an initialized MPI
518 * with the same allocated length as Y.
519 * \param Y The right-hand MPI. This must point to an initialized MPI
520 * with the same allocated length as X.
521 * \param ret The result of the comparison:
Janos Follathc3b376e2019-10-11 14:21:53 +0100522 * \c 1 if \p X is less than \p Y.
523 * \c 0 if \p X is greater than or equal to \p Y.
Janos Follathe0187b92019-09-05 14:47:19 +0100524 *
525 * \return 0 on success.
526 * \return MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the allocated length of
527 * the two input MPIs is not the same.
528 */
Janos Follathc3b376e2019-10-11 14:21:53 +0100529int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
530 unsigned *ret );
Janos Follathe0187b92019-09-05 14:47:19 +0100531
532/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000533 * \brief Compare signed values
534 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000535 * \param X Left-hand MPI
536 * \param z The integer value to compare to
537 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 * \return 1 if X is greater than z,
539 * -1 if X is lesser than z or
540 * 0 if X is equal to z
541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000543
544/**
545 * \brief Unsigned addition: X = |A| + |B|
546 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000547 * \param X Destination MPI
548 * \param A Left-hand MPI
549 * \param B Right-hand MPI
550 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000551 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200552 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
556/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100557 * \brief Unsigned subtraction: X = |A| - |B|
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000559 * \param X Destination MPI
560 * \param A Left-hand MPI
561 * \param B Right-hand MPI
562 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000567
568/**
569 * \brief Signed addition: X = A + B
570 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000571 * \param X Destination MPI
572 * \param A Left-hand MPI
573 * \param B Right-hand MPI
574 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200576 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000579
580/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100581 * \brief Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000583 * \param X Destination MPI
584 * \param A Left-hand MPI
585 * \param B Right-hand MPI
586 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000587 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200588 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000591
592/**
593 * \brief Signed addition: X = A + b
594 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000595 * \param X Destination MPI
596 * \param A Left-hand MPI
597 * \param b The integer value to add
598 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200600 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000603
604/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100605 * \brief Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000607 * \param X Destination MPI
608 * \param A Left-hand MPI
609 * \param b The integer value to subtract
610 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200612 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000615
616/**
617 * \brief Baseline multiplication: X = A * B
618 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000619 * \param X Destination MPI
620 * \param A Left-hand MPI
621 * \param B Right-hand MPI
622 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200624 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
628/**
629 * \brief Baseline multiplication: X = A * b
630 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000631 * \param X Destination MPI
632 * \param A Left-hand MPI
Manuel Pégourié-Gonnard35f1d7f2015-03-19 12:42:40 +0000633 * \param b The unsigned integer value to multiply with
634 *
635 * \note b is unsigned
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000636 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000637 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200638 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000641
642/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 * \brief Division by mbedtls_mpi: A = Q * B + R
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000645 * \param Q Destination MPI for the quotient
646 * \param R Destination MPI for the rest value
647 * \param A Left-hand MPI
648 * \param B Right-hand MPI
649 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000650 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200651 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 *
654 * \note Either Q or R can be NULL.
655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656int 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 +0000657
658/**
659 * \brief Division by int: A = Q * b + R
660 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000661 * \param Q Destination MPI for the quotient
662 * \param R Destination MPI for the rest value
663 * \param A Left-hand MPI
664 * \param b Integer to divide by
665 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000666 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200667 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 *
670 * \note Either Q or R can be NULL.
671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672int 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 +0000673
674/**
675 * \brief Modulo: R = A mod B
676 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000677 * \param R Destination MPI for the rest value
678 * \param A Left-hand MPI
679 * \param B Right-hand MPI
680 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200682 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0,
684 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000687
688/**
689 * \brief Modulo: r = A mod b
690 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 * \param r Destination mbedtls_mpi_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000692 * \param A Left-hand MPI
693 * \param b Integer to divide by
694 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000695 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200696 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0,
698 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000701
702/**
703 * \brief Sliding-window exponentiation: X = A^E mod N
704 *
Paul Bakker9af723c2014-05-01 13:03:14 +0200705 * \param X Destination MPI
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000706 * \param A Left-hand MPI
707 * \param E Exponent MPI
708 * \param N Modular MPI
709 * \param _RR Speed-up MPI used for recalculations
710 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000711 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200712 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200714 * if E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 *
716 * \note _RR is used to avoid re-computing R*R mod N across
717 * multiple calls, which speeds up things a bit. It can
718 * be set to NULL if the extra performance is unneeded.
719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720int 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 +0000721
722/**
Paul Bakker287781a2011-03-26 13:18:49 +0000723 * \brief Fill an MPI X with size bytes of random
724 *
725 * \param X Destination MPI
726 * \param size Size in bytes
727 * \param f_rng RNG function
728 * \param p_rng RNG parameter
729 *
730 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200731 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Hanno Becker15f2b3e2017-10-17 15:17:05 +0100732 *
733 * \note The bytes obtained from the PRNG are interpreted
734 * as a big-endian representation of an MPI; this can
735 * be relevant in applications like deterministic ECDSA.
Paul Bakker287781a2011-03-26 13:18:49 +0000736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000738 int (*f_rng)(void *, unsigned char *, size_t),
739 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000740
741/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000742 * \brief Greatest common divisor: G = gcd(A, B)
743 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000744 * \param G Destination MPI
745 * \param A Left-hand MPI
746 * \param B Right-hand MPI
747 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000748 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200749 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000752
753/**
754 * \brief Modular inverse: X = A^-1 mod N
755 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000756 * \param X Destination MPI
757 * \param A Left-hand MPI
758 * \param N Right-hand MPI
759 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000760 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200761 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Hanno Becker4bcb4912017-04-18 15:49:39 +0100762 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is <= 1,
763 MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000764 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000766
767/**
768 * \brief Miller-Rabin primality test
769 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000770 * \param X MPI to check
771 * \param f_rng RNG function
772 * \param p_rng RNG parameter
773 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000774 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200775 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000779 int (*f_rng)(void *, unsigned char *, size_t),
780 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000781
782/**
783 * \brief Prime number generation
784 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000785 * \param X Destination MPI
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200786 * \param nbits Required size of X in bits
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000788 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000789 * \param f_rng RNG function
790 * \param p_rng RNG parameter
791 *
792 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200793 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000795 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000797 int (*f_rng)(void *, unsigned char *, size_t),
798 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000799
800/**
801 * \brief Checkup routine
802 *
803 * \return 0 if successful, or 1 if the test failed
804 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805int mbedtls_mpi_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000806
807#ifdef __cplusplus
808}
809#endif
810
811#endif /* bignum.h */