blob: 8e44190e79212bdebaf9f3e5f92727bbc34ce2bf [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_BIGNUM_H
25#define MBEDTLS_BIGNUM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkercf0360a2012-01-20 10:08:14 +000028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakkercf0360a2012-01-20 10:08:14 +000032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020034#include <stdint.h>
Rich Evans00ab4702015-02-06 13:43:58 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnarde94e6e52015-01-19 15:01:53 +000037#include <stdio.h>
38#endif
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
41#define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
42#define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
43#define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
44#define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
45#define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
46#define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020047#define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 /**< Memory allocation failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000050
51/*
Paul Bakkerf9688572011-05-05 10:00:45 +000052 * Maximum size MPIs are allowed to grow to in number of limbs.
53 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define MBEDTLS_MPI_MAX_LIMBS 10000
Paul Bakkerf9688572011-05-05 10:00:45 +000055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if !defined(MBEDTLS_MPI_WINDOW_SIZE)
Paul Bakkerf9688572011-05-05 10:00:45 +000057/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000058 * Maximum window size used for modular exponentiation. Default: 6
59 * Minimum value: 1. Maximum value: 6.
60 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
Paul Bakkerb6d5f082011-11-25 11:52:11 +000062 * for the sliding window calculation. (So 64 by default)
63 *
64 * Reduction in size, reduces speed.
65 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
67#endif /* !MBEDTLS_MPI_WINDOW_SIZE */
Paul Bakkerb6d5f082011-11-25 11:52:11 +000068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if !defined(MBEDTLS_MPI_MAX_SIZE)
Paul Bakkerb6d5f082011-11-25 11:52:11 +000070/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000071 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakkerf626e1d2013-01-21 12:10:00 +010072 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +000073 *
Hanno Beckerefeef6c2018-01-05 08:07:47 +000074 * Note: Calculations can temporarily result in larger MPIs. So the number
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
Paul Bakkerfe3256e2011-11-25 12:11:43 +000076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
78#endif /* !MBEDTLS_MPI_MAX_SIZE */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
Paul Bakkerfe3256e2011-11-25 12:11:43 +000081
82/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 * When reading from files with mbedtls_mpi_read_file() and writing to files with
84 * mbedtls_mpi_write_file() the buffer should have space
Paul Bakkercb37aa52011-11-30 16:00:20 +000085 * for a (short) label, the MPI (in the provided radix), the newline
86 * characters and the '\0'.
87 *
88 * By default we assume at least a 10 char label, a minimum radix of 10
89 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
Paul Bakkerf9183102012-09-27 20:42:35 +000090 * Autosized at compile time for at least a 10 char label, a minimum radix
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
Paul Bakkerf9183102012-09-27 20:42:35 +000092 *
93 * This used to be statically sized to 1250 for a maximum of 4096 bit
94 * numbers (1234 decimal chars).
95 *
96 * Calculate using the formula:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
Paul Bakkerf9183102012-09-27 20:42:35 +000098 * LabelSize + 6
Paul Bakkercb37aa52011-11-30 16:00:20 +000099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
101#define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
102#define MBEDTLS_MPI_RW_BUFFER_SIZE ( ((MBEDTLS_MPI_MAX_BITS_SCALE100 + MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
Paul Bakkercb37aa52011-11-30 16:00:20 +0000103
104/*
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200105 * Define the base integer type, architecture-wise.
106 *
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100107 * 32 or 64-bit integer types can be forced regardless of the underlying
108 * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
109 * respectively and undefining MBEDTLS_HAVE_ASM.
110 *
Andres Amaya Garcia93db11a2017-07-20 12:11:19 +0100111 * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100112 * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100114#if !defined(MBEDTLS_HAVE_INT32)
115 #if defined(_MSC_VER) && defined(_M_AMD64)
116 /* Always choose 64-bit when using MSC */
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100117 #if !defined(MBEDTLS_HAVE_INT64)
118 #define MBEDTLS_HAVE_INT64
119 #endif /* !MBEDTLS_HAVE_INT64 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100120 typedef int64_t mbedtls_mpi_sint;
121 typedef uint64_t mbedtls_mpi_uint;
122 #elif defined(__GNUC__) && ( \
123 defined(__amd64__) || defined(__x86_64__) || \
124 defined(__ppc64__) || defined(__powerpc64__) || \
125 defined(__ia64__) || defined(__alpha__) || \
126 ( defined(__sparc__) && defined(__arch64__) ) || \
127 defined(__s390x__) || defined(__mips64) )
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100128 #if !defined(MBEDTLS_HAVE_INT64)
129 #define MBEDTLS_HAVE_INT64
130 #endif /* MBEDTLS_HAVE_INT64 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100131 typedef int64_t mbedtls_mpi_sint;
132 typedef uint64_t mbedtls_mpi_uint;
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100133 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
134 /* mbedtls_t_udbl defined as 128-bit unsigned int */
135 typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
136 #define MBEDTLS_HAVE_UDBL
137 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100138 #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100139 /*
140 * __ARMCC_VERSION is defined for both armcc and armclang and
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100141 * __aarch64__ is only defined by armclang when compiling 64-bit code
142 */
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100143 #if !defined(MBEDTLS_HAVE_INT64)
144 #define MBEDTLS_HAVE_INT64
145 #endif /* !MBEDTLS_HAVE_INT64 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100146 typedef int64_t mbedtls_mpi_sint;
147 typedef uint64_t mbedtls_mpi_uint;
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100148 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
149 /* mbedtls_t_udbl defined as 128-bit unsigned int */
150 typedef __uint128_t mbedtls_t_udbl;
151 #define MBEDTLS_HAVE_UDBL
152 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
153 #elif defined(MBEDTLS_HAVE_INT64)
154 /* Force 64-bit integers with unknown compiler */
155 typedef int64_t mbedtls_mpi_sint;
156 typedef uint64_t mbedtls_mpi_uint;
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100157 #endif
158#endif /* !MBEDTLS_HAVE_INT32 */
159
160#if !defined(MBEDTLS_HAVE_INT64)
161 /* Default to 32-bit compilation */
162 #if !defined(MBEDTLS_HAVE_INT32)
163 #define MBEDTLS_HAVE_INT32
164 #endif /* !MBEDTLS_HAVE_INT32 */
165 typedef int32_t mbedtls_mpi_sint;
166 typedef uint32_t mbedtls_mpi_uint;
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100167 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
168 typedef uint64_t mbedtls_t_udbl;
Andres Amaya Garciadf1486a2017-07-20 17:33:09 +0100169 #define MBEDTLS_HAVE_UDBL
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100170 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100171#endif /* !MBEDTLS_HAVE_INT64 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000172
Paul Bakker407a0da2013-06-27 14:29:21 +0200173#ifdef __cplusplus
174extern "C" {
175#endif
176
Paul Bakker5121ce52009-01-03 21:22:43 +0000177/**
178 * \brief MPI structure
179 */
180typedef struct
181{
Janos Follath9741fa62019-10-28 12:07:52 +0000182 int s; /*!< Sign: -1 if the mpi is negative, 1 otherwise */
Paul Bakker23986e52011-04-24 08:57:21 +0000183 size_t n; /*!< total # of limbs */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_mpi_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000185}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186mbedtls_mpi;
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
Paul Bakker5121ce52009-01-03 21:22:43 +0000188/**
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200189 * \brief Initialize one MPI (make internal references valid)
190 * This just makes it ready to be set or freed,
191 * but does not define a value for the MPI.
Paul Bakker6c591fa2011-05-05 11:49:20 +0000192 *
193 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195void mbedtls_mpi_init( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196
197/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000198 * \brief Unallocate one MPI
199 *
200 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202void mbedtls_mpi_free( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000203
204/**
205 * \brief Enlarge to the specified number of limbs
206 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000207 * \param X MPI to grow
208 * \param nblimbs The target number of limbs
209 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200211 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
215/**
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100216 * \brief Resize down, keeping at least the specified number of limbs
217 *
218 * \param X MPI to shrink
219 * \param nblimbs The minimum number of limbs to keep
220 *
221 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200222 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100225
226/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 * \brief Copy the contents of Y into X
228 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000229 * \param X Destination MPI
230 * \param Y Source MPI
231 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200233 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
237/**
238 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000239 *
240 * \param X First MPI value
241 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000244
245/**
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100246 * \brief Safe conditional assignement X = Y if assign is 1
247 *
248 * \param X MPI to conditionally assign to
249 * \param Y Value to be assigned
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100250 * \param assign 1: perform the assignment, 0: keep X's original value
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100251 *
252 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200253 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100254 *
255 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 * if( assign ) mbedtls_mpi_copy( X, Y );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100257 * except that it avoids leaking any information about whether
258 * the assignment was done or not (the above code may leak
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100259 * information through branch prediction and/or memory access
260 * patterns analysis).
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100263
264/**
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100265 * \brief Safe conditional swap X <-> Y if swap is 1
266 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 * \param X First mbedtls_mpi value
268 * \param Y Second mbedtls_mpi value
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100269 * \param assign 1: perform the swap, 0: keep X and Y's original values
270 *
271 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200272 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100273 *
274 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 * if( assign ) mbedtls_mpi_swap( X, Y );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100276 * except that it avoids leaking any information about whether
277 * the assignment was done or not (the above code may leak
278 * information through branch prediction and/or memory access
279 * patterns analysis).
280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100282
283/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 * \brief Set value from integer
285 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000286 * \param X MPI to set
287 * \param z Value to use
288 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200290 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000293
Paul Bakker9a736322012-11-14 12:39:52 +0000294/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000295 * \brief Get a specific bit from X
296 *
297 * \param X MPI to use
298 * \param pos Zero-based index of the bit in X
299 *
300 * \return Either a 0 or a 1
301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000303
Paul Bakker9a736322012-11-14 12:39:52 +0000304/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000305 * \brief Set a bit of X to a specific value of 0 or 1
306 *
307 * \note Will grow X if necessary to set a bit to 1 in a not yet
308 * existing limb. Will not grow if bit should be set to 0
309 *
310 * \param X MPI to use
311 * \param pos Zero-based index of the bit in X
312 * \param val The value to set the bit to (0 or 1)
313 *
314 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200315 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
Paul Bakker2f5947e2011-05-18 15:47:11 +0000317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000319
Paul Bakker5121ce52009-01-03 21:22:43 +0000320/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000321 * \brief Return the number of zero-bits before the least significant
322 * '1' bit
323 *
324 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000325 *
326 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000329
330/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000331 * \brief Return the number of bits up to and including the most
332 * significant '1' bit'
333 *
334 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000335 *
336 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200338size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
340/**
341 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000342 *
343 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345size_t mbedtls_mpi_size( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000346
347/**
348 * \brief Import from an ASCII string
349 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000350 * \param X Destination MPI
351 * \param radix Input numeric base
352 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000353 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000357
358/**
359 * \brief Export into an ASCII string
360 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000361 * \param X Source MPI
362 * \param radix Output numeric base
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100363 * \param buf Buffer to write the string to
364 * \param buflen Length of buf
365 * \param olen Length of the string written, including final NUL byte
Paul Bakker5121ce52009-01-03 21:22:43 +0000366 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code.
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100368 * *olen is always updated to reflect the amount
Paul Bakkerff60ee62010-03-16 21:09:09 +0000369 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 *
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100371 * \note Call this function with buflen = 0 to obtain the
372 * minimum required buffer size in *olen.
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100374int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
375 char *buf, size_t buflen, size_t *olen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000378/**
Hanno Beckerb2034b72017-04-26 11:46:46 +0100379 * \brief Read MPI from a line in an opened file
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000381 * \param X Destination MPI
382 * \param radix Input numeric base
383 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
Paul Bakkercb37aa52011-11-30 16:00:20 +0000386 * the file read buffer is too small or a
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 * MBEDTLS_ERR_MPI_XXX error code
Hanno Beckerb2034b72017-04-26 11:46:46 +0100388 *
389 * \note On success, this function advances the file stream
390 * to the end of the current line or to EOF.
391 *
392 * The function returns 0 on an empty line.
393 *
394 * Leading whitespaces are ignored, as is a
395 * '0x' prefix for radix 16.
396 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
Paul Bakker5121ce52009-01-03 21:22:43 +0000399
400/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000401 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000403 * \param p Prefix, can be NULL
404 * \param X Source MPI
405 * \param radix Output numeric base
406 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000407 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 *
410 * \note Set fout == NULL to print X on the console.
411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
413#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000414
415/**
416 * \brief Import X from unsigned binary data, big endian
417 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000418 * \param X Destination MPI
419 * \param buf Input buffer
420 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000421 *
422 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200423 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
427/**
Manuel Pégourié-Gonnard3926a2c2014-06-25 12:57:47 +0200428 * \brief Export X into unsigned binary data, big endian.
429 * Always fills the whole buffer, which will start with zeros
430 * if the number is smaller.
Paul Bakker5121ce52009-01-03 21:22:43 +0000431 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000432 * \param X Source MPI
433 * \param buf Output buffer
434 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 *
436 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000440
441/**
442 * \brief Left-shift: X <<= count
443 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000444 * \param X MPI to shift
445 * \param count Amount to shift
446 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 * \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_shift_l( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000451
452/**
453 * \brief Right-shift: X >>= count
454 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000455 * \param X MPI to shift
456 * \param count Amount to shift
457 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200459 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
463/**
464 * \brief Compare unsigned values
465 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000466 * \param X Left-hand MPI
467 * \param Y Right-hand MPI
468 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 * \return 1 if |X| is greater than |Y|,
470 * -1 if |X| is lesser than |Y| or
471 * 0 if |X| is equal to |Y|
472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000474
475/**
476 * \brief Compare signed values
477 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000478 * \param X Left-hand MPI
479 * \param Y Right-hand MPI
480 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000481 * \return 1 if X is greater than Y,
482 * -1 if X is lesser than Y or
483 * 0 if X is equal to Y
484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
487/**
Janos Follathc3b376e2019-10-11 14:21:53 +0100488 * \brief Check if an MPI is less than the other in constant time.
Janos Follathe0187b92019-09-05 14:47:19 +0100489 *
490 * \param X The left-hand MPI. This must point to an initialized MPI
491 * with the same allocated length as Y.
492 * \param Y The right-hand MPI. This must point to an initialized MPI
493 * with the same allocated length as X.
494 * \param ret The result of the comparison:
Janos Follathc3b376e2019-10-11 14:21:53 +0100495 * \c 1 if \p X is less than \p Y.
496 * \c 0 if \p X is greater than or equal to \p Y.
Janos Follathe0187b92019-09-05 14:47:19 +0100497 *
498 * \return 0 on success.
499 * \return MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the allocated length of
500 * the two input MPIs is not the same.
501 */
Janos Follathc3b376e2019-10-11 14:21:53 +0100502int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
503 unsigned *ret );
Janos Follathe0187b92019-09-05 14:47:19 +0100504
505/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 * \brief Compare signed values
507 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000508 * \param X Left-hand MPI
509 * \param z The integer value to compare to
510 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000511 * \return 1 if X is greater than z,
512 * -1 if X is lesser than z or
513 * 0 if X is equal to z
514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000516
517/**
518 * \brief Unsigned addition: X = |A| + |B|
519 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000520 * \param X Destination MPI
521 * \param A Left-hand MPI
522 * \param B Right-hand MPI
523 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200525 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
529/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100530 * \brief Unsigned subtraction: X = |A| - |B|
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000532 * \param X Destination MPI
533 * \param A Left-hand MPI
534 * \param B Right-hand MPI
535 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000538 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
541/**
542 * \brief Signed addition: X = A + B
543 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000544 * \param X Destination MPI
545 * \param A Left-hand MPI
546 * \param B Right-hand MPI
547 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200549 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000552
553/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100554 * \brief Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000556 * \param X Destination MPI
557 * \param A Left-hand MPI
558 * \param B Right-hand MPI
559 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200561 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000564
565/**
566 * \brief Signed addition: X = A + b
567 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000568 * \param X Destination MPI
569 * \param A Left-hand MPI
570 * \param b The integer value to add
571 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200573 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000576
577/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100578 * \brief Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000580 * \param X Destination MPI
581 * \param A Left-hand MPI
582 * \param b The integer value to subtract
583 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000584 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200585 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000588
589/**
590 * \brief Baseline multiplication: X = A * B
591 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000592 * \param X Destination MPI
593 * \param A Left-hand MPI
594 * \param B Right-hand MPI
595 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000596 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200597 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000600
601/**
602 * \brief Baseline multiplication: X = A * b
603 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000604 * \param X Destination MPI
605 * \param A Left-hand MPI
Manuel Pégourié-Gonnard35f1d7f2015-03-19 12:42:40 +0000606 * \param b The unsigned integer value to multiply with
607 *
608 * \note b is unsigned
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000609 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200611 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000612 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000614
615/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 * \brief Division by mbedtls_mpi: A = Q * B + R
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000618 * \param Q Destination MPI for the quotient
619 * \param R Destination MPI for the rest value
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,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000626 *
627 * \note Either Q or R can be NULL.
628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629int 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 +0000630
631/**
632 * \brief Division by int: A = Q * b + R
633 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000634 * \param Q Destination MPI for the quotient
635 * \param R Destination MPI for the rest value
636 * \param A Left-hand MPI
637 * \param b Integer to divide by
638 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200640 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 *
643 * \note Either Q or R can be NULL.
644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645int 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 +0000646
647/**
648 * \brief Modulo: R = A mod B
649 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000650 * \param R Destination MPI for the rest value
651 * \param A Left-hand MPI
652 * \param B Right-hand MPI
653 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000654 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200655 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0,
657 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000660
661/**
662 * \brief Modulo: r = A mod b
663 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 * \param r Destination mbedtls_mpi_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000665 * \param A Left-hand MPI
666 * \param b Integer to divide by
667 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000668 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200669 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0,
671 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000674
675/**
676 * \brief Sliding-window exponentiation: X = A^E mod N
677 *
Paul Bakker9af723c2014-05-01 13:03:14 +0200678 * \param X Destination MPI
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000679 * \param A Left-hand MPI
680 * \param E Exponent MPI
681 * \param N Modular MPI
682 * \param _RR Speed-up MPI used for recalculations
683 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000684 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200685 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200687 * if E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000688 *
689 * \note _RR is used to avoid re-computing R*R mod N across
690 * multiple calls, which speeds up things a bit. It can
691 * be set to NULL if the extra performance is unneeded.
692 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693int 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 +0000694
695/**
Paul Bakker287781a2011-03-26 13:18:49 +0000696 * \brief Fill an MPI X with size bytes of random
697 *
698 * \param X Destination MPI
699 * \param size Size in bytes
700 * \param f_rng RNG function
701 * \param p_rng RNG parameter
702 *
703 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200704 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Hanno Becker15f2b3e2017-10-17 15:17:05 +0100705 *
706 * \note The bytes obtained from the PRNG are interpreted
707 * as a big-endian representation of an MPI; this can
708 * be relevant in applications like deterministic ECDSA.
Paul Bakker287781a2011-03-26 13:18:49 +0000709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000711 int (*f_rng)(void *, unsigned char *, size_t),
712 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000713
714/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000715 * \brief Greatest common divisor: G = gcd(A, B)
716 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000717 * \param G Destination MPI
718 * \param A Left-hand MPI
719 * \param B Right-hand MPI
720 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000721 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200722 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000725
726/**
727 * \brief Modular inverse: X = A^-1 mod N
728 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000729 * \param X Destination MPI
730 * \param A Left-hand MPI
731 * \param N Right-hand MPI
732 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000733 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200734 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Hanno Becker4bcb4912017-04-18 15:49:39 +0100735 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is <= 1,
736 MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000739
740/**
741 * \brief Miller-Rabin primality test
742 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000743 * \param X MPI to check
744 * \param f_rng RNG function
745 * \param p_rng RNG parameter
746 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000747 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200748 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000752 int (*f_rng)(void *, unsigned char *, size_t),
753 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000754
755/**
756 * \brief Prime number generation
757 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000758 * \param X Destination MPI
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200759 * \param nbits Required size of X in bits
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000761 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000762 * \param f_rng RNG function
763 * \param p_rng RNG parameter
764 *
765 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200766 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000768 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000770 int (*f_rng)(void *, unsigned char *, size_t),
771 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000772
773/**
774 * \brief Checkup routine
775 *
776 * \return 0 if successful, or 1 if the test failed
777 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778int mbedtls_mpi_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000779
780#ifdef __cplusplus
781}
782#endif
783
784#endif /* bignum.h */