blob: 5187d866116bf61bfe408c42c2a44c4defce7b30 [file] [log] [blame]
Fabio Utzigba05f2a2017-12-05 11:00:41 -02001/**
2 * \file bignum.h
3 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -02004 * \brief Multi-precision integer library
5 */
6/*
Roman Okhrimenko977b3752022-03-31 14:40:48 +03007 * Copyright The Mbed TLS Contributors
Fabio Utzigba05f2a2017-12-05 11:00:41 -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.
Fabio Utzigba05f2a2017-12-05 11:00:41 -020021 */
22#ifndef MBEDTLS_BIGNUM_H
23#define MBEDTLS_BIGNUM_H
Roman Okhrimenko977b3752022-03-31 14:40:48 +030024#include "mbedtls/private_access.h"
Fabio Utzigba05f2a2017-12-05 11:00:41 -020025
Roman Okhrimenko977b3752022-03-31 14:40:48 +030026#include "mbedtls/build_info.h"
Fabio Utzigba05f2a2017-12-05 11:00:41 -020027
28#include <stddef.h>
29#include <stdint.h>
30
31#if defined(MBEDTLS_FS_IO)
32#include <stdio.h>
33#endif
34
35#define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
36#define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
37#define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
38#define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
39#define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
40#define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
41#define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
42#define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 /**< Memory allocation failed. */
43
Roman Okhrimenko977b3752022-03-31 14:40:48 +030044#define MBEDTLS_MPI_CHK(f) \
45 do \
46 { \
47 if( ( ret = (f) ) != 0 ) \
48 goto cleanup; \
49 } while( 0 )
Fabio Utzigba05f2a2017-12-05 11:00:41 -020050
51/*
52 * Maximum size MPIs are allowed to grow to in number of limbs.
53 */
54#define MBEDTLS_MPI_MAX_LIMBS 10000
55
56#if !defined(MBEDTLS_MPI_WINDOW_SIZE)
57/*
58 * Maximum window size used for modular exponentiation. Default: 6
59 * Minimum value: 1. Maximum value: 6.
60 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +030061 * Result is an array of ( 2 ** MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
Fabio Utzigba05f2a2017-12-05 11:00:41 -020062 * for the sliding window calculation. (So 64 by default)
63 *
64 * Reduction in size, reduces speed.
65 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +030066#define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum window size used. */
Fabio Utzigba05f2a2017-12-05 11:00:41 -020067#endif /* !MBEDTLS_MPI_WINDOW_SIZE */
68
69#if !defined(MBEDTLS_MPI_MAX_SIZE)
70/*
71 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
72 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
73 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -020074 * Note: Calculations can temporarily result in larger MPIs. So the number
Fabio Utzigba05f2a2017-12-05 11:00:41 -020075 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
76 */
77#define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
78#endif /* !MBEDTLS_MPI_MAX_SIZE */
79
80#define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
81
82/*
83 * When reading from files with mbedtls_mpi_read_file() and writing to files with
84 * mbedtls_mpi_write_file() the buffer should have space
85 * for a (short) label, the MPI (in the provided radix), the newline
86 * characters and the '\0'.
87 *
88 * By default we assume at least a 10 char label, a minimum radix of 10
89 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
90 * Autosized at compile time for at least a 10 char label, a minimum radix
91 * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
92 *
93 * This used to be statically sized to 1250 for a maximum of 4096 bit
94 * numbers (1234 decimal chars).
95 *
96 * Calculate using the formula:
97 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
98 * LabelSize + 6
99 */
100#define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
101#define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
102#define MBEDTLS_MPI_RW_BUFFER_SIZE ( ((MBEDTLS_MPI_MAX_BITS_SCALE100 + MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
103
104/*
105 * Define the base integer type, architecture-wise.
106 *
107 * 32 or 64-bit integer types can be forced regardless of the underlying
108 * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
109 * respectively and undefining MBEDTLS_HAVE_ASM.
110 *
111 * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
112 * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
113 */
114#if !defined(MBEDTLS_HAVE_INT32)
115 #if defined(_MSC_VER) && defined(_M_AMD64)
116 /* Always choose 64-bit when using MSC */
117 #if !defined(MBEDTLS_HAVE_INT64)
118 #define MBEDTLS_HAVE_INT64
119 #endif /* !MBEDTLS_HAVE_INT64 */
120 typedef int64_t mbedtls_mpi_sint;
121 typedef uint64_t mbedtls_mpi_uint;
122 #elif defined(__GNUC__) && ( \
123 defined(__amd64__) || defined(__x86_64__) || \
124 defined(__ppc64__) || defined(__powerpc64__) || \
125 defined(__ia64__) || defined(__alpha__) || \
126 ( defined(__sparc__) && defined(__arch64__) ) || \
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300127 defined(__s390x__) || defined(__mips64) || \
128 defined(__aarch64__) )
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200129 #if !defined(MBEDTLS_HAVE_INT64)
130 #define MBEDTLS_HAVE_INT64
131 #endif /* MBEDTLS_HAVE_INT64 */
132 typedef int64_t mbedtls_mpi_sint;
133 typedef uint64_t mbedtls_mpi_uint;
134 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
135 /* mbedtls_t_udbl defined as 128-bit unsigned int */
136 typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
137 #define MBEDTLS_HAVE_UDBL
138 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
139 #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
140 /*
141 * __ARMCC_VERSION is defined for both armcc and armclang and
142 * __aarch64__ is only defined by armclang when compiling 64-bit code
143 */
144 #if !defined(MBEDTLS_HAVE_INT64)
145 #define MBEDTLS_HAVE_INT64
146 #endif /* !MBEDTLS_HAVE_INT64 */
147 typedef int64_t mbedtls_mpi_sint;
148 typedef uint64_t mbedtls_mpi_uint;
149 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
150 /* mbedtls_t_udbl defined as 128-bit unsigned int */
151 typedef __uint128_t mbedtls_t_udbl;
152 #define MBEDTLS_HAVE_UDBL
153 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
154 #elif defined(MBEDTLS_HAVE_INT64)
155 /* Force 64-bit integers with unknown compiler */
156 typedef int64_t mbedtls_mpi_sint;
157 typedef uint64_t mbedtls_mpi_uint;
158 #endif
159#endif /* !MBEDTLS_HAVE_INT32 */
160
161#if !defined(MBEDTLS_HAVE_INT64)
162 /* Default to 32-bit compilation */
163 #if !defined(MBEDTLS_HAVE_INT32)
164 #define MBEDTLS_HAVE_INT32
165 #endif /* !MBEDTLS_HAVE_INT32 */
166 typedef int32_t mbedtls_mpi_sint;
167 typedef uint32_t mbedtls_mpi_uint;
168 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
169 typedef uint64_t mbedtls_t_udbl;
170 #define MBEDTLS_HAVE_UDBL
171 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
172#endif /* !MBEDTLS_HAVE_INT64 */
173
174#ifdef __cplusplus
175extern "C" {
176#endif
177
178/**
179 * \brief MPI structure
180 */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200181typedef struct mbedtls_mpi
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200182{
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300183 int MBEDTLS_PRIVATE(s); /*!< Sign: -1 if the mpi is negative, 1 otherwise */
184 size_t MBEDTLS_PRIVATE(n); /*!< total # of limbs */
185 mbedtls_mpi_uint *MBEDTLS_PRIVATE(p); /*!< pointer to limbs */
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200186}
187mbedtls_mpi;
188
189/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300190 * \brief Initialize an MPI context.
191 *
192 * This makes the MPI ready to be set or freed,
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200193 * but does not define a value for the MPI.
194 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300195 * \param X The MPI context to initialize. This must not be \c NULL.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200196 */
197void mbedtls_mpi_init( mbedtls_mpi *X );
198
199/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300200 * \brief This function frees the components of an MPI context.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200201 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300202 * \param X The MPI context to be cleared. This may be \c NULL,
203 * in which case this function is a no-op. If it is
204 * not \c NULL, it must point to an initialized MPI.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200205 */
206void mbedtls_mpi_free( mbedtls_mpi *X );
207
208/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300209 * \brief Enlarge an MPI to the specified number of limbs.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200210 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300211 * \note This function does nothing if the MPI is
212 * already large enough.
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200213 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300214 * \param X The MPI to grow. It must be initialized.
215 * \param nblimbs The target number of limbs.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200216 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300217 * \return \c 0 if successful.
218 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
219 * \return Another negative error code on other kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200220 */
221int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
222
223/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300224 * \brief This function resizes an MPI downwards, keeping at least the
225 * specified number of limbs.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200226 *
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200227 * If \c X is smaller than \c nblimbs, it is resized up
228 * instead.
229 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300230 * \param X The MPI to shrink. This must point to an initialized MPI.
231 * \param nblimbs The minimum number of limbs to keep.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200232 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300233 * \return \c 0 if successful.
234 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200235 * (this can only happen when resizing up).
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300236 * \return Another negative error code on other kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200237 */
238int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
239
240/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300241 * \brief Make a copy of an MPI.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200242 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300243 * \param X The destination MPI. This must point to an initialized MPI.
244 * \param Y The source MPI. This must point to an initialized MPI.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200245 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300246 * \note The limb-buffer in the destination MPI is enlarged
247 * if necessary to hold the value in the source MPI.
248 *
249 * \return \c 0 if successful.
250 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
251 * \return Another negative error code on other kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200252 */
253int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
254
255/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300256 * \brief Swap the contents of two MPIs.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200257 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300258 * \param X The first MPI. It must be initialized.
259 * \param Y The second MPI. It must be initialized.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200260 */
261void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
262
263/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300264 * \brief Perform a safe conditional copy of MPI which doesn't
265 * reveal whether the condition was true or not.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200266 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300267 * \param X The MPI to conditionally assign to. This must point
268 * to an initialized MPI.
269 * \param Y The MPI to be assigned from. This must point to an
270 * initialized MPI.
271 * \param assign The condition deciding whether to perform the
272 * assignment or not. Possible values:
273 * * \c 1: Perform the assignment `X = Y`.
274 * * \c 0: Keep the original value of \p X.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200275 *
276 * \note This function is equivalent to
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300277 * `if( assign ) mbedtls_mpi_copy( X, Y );`
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200278 * except that it avoids leaking any information about whether
279 * the assignment was done or not (the above code may leak
280 * information through branch prediction and/or memory access
281 * patterns analysis).
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300282 *
283 * \return \c 0 if successful.
284 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
285 * \return Another negative error code on other kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200286 */
287int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
288
289/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300290 * \brief Perform a safe conditional swap which doesn't
291 * reveal whether the condition was true or not.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200292 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300293 * \param X The first MPI. This must be initialized.
294 * \param Y The second MPI. This must be initialized.
295 * \param assign The condition deciding whether to perform
296 * the swap or not. Possible values:
297 * * \c 1: Swap the values of \p X and \p Y.
298 * * \c 0: Keep the original values of \p X and \p Y.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200299 *
300 * \note This function is equivalent to
301 * if( assign ) mbedtls_mpi_swap( X, Y );
302 * except that it avoids leaking any information about whether
303 * the assignment was done or not (the above code may leak
304 * information through branch prediction and/or memory access
305 * patterns analysis).
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300306 *
307 * \return \c 0 if successful.
308 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
309 * \return Another negative error code on other kinds of failure.
310 *
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200311 */
312int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
313
314/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300315 * \brief Store integer value in MPI.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200316 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300317 * \param X The MPI to set. This must be initialized.
318 * \param z The value to use.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200319 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300320 * \return \c 0 if successful.
321 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
322 * \return Another negative error code on other kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200323 */
324int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
325
326/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300327 * \brief Get a specific bit from an MPI.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200328 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300329 * \param X The MPI to query. This must be initialized.
330 * \param pos Zero-based index of the bit to query.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200331 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300332 * \return \c 0 or \c 1 on success, depending on whether bit \c pos
333 * of \c X is unset or set.
334 * \return A negative error code on failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200335 */
336int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
337
338/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300339 * \brief Modify a specific bit in an MPI.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200340 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300341 * \note This function will grow the target MPI if necessary to set a
342 * bit to \c 1 in a not yet existing limb. It will not grow if
343 * the bit should be set to \c 0.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200344 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300345 * \param X The MPI to modify. This must be initialized.
346 * \param pos Zero-based index of the bit to modify.
347 * \param val The desired value of bit \c pos: \c 0 or \c 1.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200348 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300349 * \return \c 0 if successful.
350 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
351 * \return Another negative error code on other kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200352 */
353int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
354
355/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300356 * \brief Return the number of bits of value \c 0 before the
357 * least significant bit of value \c 1.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200358 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300359 * \note This is the same as the zero-based index of
360 * the least significant bit of value \c 1.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200361 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300362 * \param X The MPI to query.
363 *
364 * \return The number of bits of value \c 0 before the least significant
365 * bit of value \c 1 in \p X.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200366 */
367size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
368
369/**
370 * \brief Return the number of bits up to and including the most
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300371 * significant bit of value \c 1.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200372 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300373 * * \note This is same as the one-based index of the most
374 * significant bit of value \c 1.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200375 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300376 * \param X The MPI to query. This must point to an initialized MPI.
377 *
378 * \return The number of bits up to and including the most
379 * significant bit of value \c 1.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200380 */
381size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
382
383/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300384 * \brief Return the total size of an MPI value in bytes.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200385 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300386 * \param X The MPI to use. This must point to an initialized MPI.
387 *
388 * \note The value returned by this function may be less than
389 * the number of bytes used to store \p X internally.
390 * This happens if and only if there are trailing bytes
391 * of value zero.
392 *
393 * \return The least number of bytes capable of storing
394 * the absolute value of \p X.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200395 */
396size_t mbedtls_mpi_size( const mbedtls_mpi *X );
397
398/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300399 * \brief Import an MPI from an ASCII string.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200400 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300401 * \param X The destination MPI. This must point to an initialized MPI.
402 * \param radix The numeric base of the input string.
403 * \param s Null-terminated string buffer.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200404 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300405 * \return \c 0 if successful.
406 * \return A negative error code on failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200407 */
408int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
409
410/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300411 * \brief Export an MPI to an ASCII string.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200412 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300413 * \param X The source MPI. This must point to an initialized MPI.
414 * \param radix The numeric base of the output string.
415 * \param buf The buffer to write the string to. This must be writable
416 * buffer of length \p buflen Bytes.
417 * \param buflen The available size in Bytes of \p buf.
418 * \param olen The address at which to store the length of the string
419 * written, including the final \c NULL byte. This must
420 * not be \c NULL.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200421 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300422 * \note You can call this function with `buflen == 0` to obtain the
423 * minimum required buffer size in `*olen`.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200424 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300425 * \return \c 0 if successful.
426 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the target buffer \p buf
427 * is too small to hold the value of \p X in the desired base.
428 * In this case, `*olen` is nonetheless updated to contain the
429 * size of \p buf required for a successful call.
430 * \return Another negative error code on different kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200431 */
432int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
433 char *buf, size_t buflen, size_t *olen );
434
435#if defined(MBEDTLS_FS_IO)
436/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300437 * \brief Read an MPI from a line in an opened file.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200438 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300439 * \param X The destination MPI. This must point to an initialized MPI.
440 * \param radix The numeric base of the string representation used
441 * in the source line.
442 * \param fin The input file handle to use. This must not be \c NULL.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200443 *
444 * \note On success, this function advances the file stream
445 * to the end of the current line or to EOF.
446 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300447 * The function returns \c 0 on an empty line.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200448 *
449 * Leading whitespaces are ignored, as is a
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300450 * '0x' prefix for radix \c 16.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200451 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300452 * \return \c 0 if successful.
453 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the file read buffer
454 * is too small.
455 * \return Another negative error code on failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200456 */
457int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
458
459/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300460 * \brief Export an MPI into an opened file.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200461 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300462 * \param p A string prefix to emit prior to the MPI data.
463 * For example, this might be a label, or "0x" when
464 * printing in base \c 16. This may be \c NULL if no prefix
465 * is needed.
466 * \param X The source MPI. This must point to an initialized MPI.
467 * \param radix The numeric base to be used in the emitted string.
468 * \param fout The output file handle. This may be \c NULL, in which case
469 * the output is written to \c stdout.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200470 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300471 * \return \c 0 if successful.
472 * \return A negative error code on failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200473 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300474int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X,
475 int radix, FILE *fout );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200476#endif /* MBEDTLS_FS_IO */
477
478/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300479 * \brief Import an MPI from unsigned big endian binary data.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200480 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300481 * \param X The destination MPI. This must point to an initialized MPI.
482 * \param buf The input buffer. This must be a readable buffer of length
483 * \p buflen Bytes.
484 * \param buflen The length of the input buffer \p p in Bytes.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200485 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300486 * \return \c 0 if successful.
487 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
488 * \return Another negative error code on different kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200489 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300490int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf,
491 size_t buflen );
492
493/**
494 * \brief Import X from unsigned binary data, little endian
495 *
496 * \param X The destination MPI. This must point to an initialized MPI.
497 * \param buf The input buffer. This must be a readable buffer of length
498 * \p buflen Bytes.
499 * \param buflen The length of the input buffer \p p in Bytes.
500 *
501 * \return \c 0 if successful.
502 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
503 * \return Another negative error code on different kinds of failure.
504 */
505int mbedtls_mpi_read_binary_le( mbedtls_mpi *X,
506 const unsigned char *buf, size_t buflen );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200507
508/**
509 * \brief Export X into unsigned binary data, big endian.
510 * Always fills the whole buffer, which will start with zeros
511 * if the number is smaller.
512 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300513 * \param X The source MPI. This must point to an initialized MPI.
514 * \param buf The output buffer. This must be a writable buffer of length
515 * \p buflen Bytes.
516 * \param buflen The size of the output buffer \p buf in Bytes.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200517 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300518 * \return \c 0 if successful.
519 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
520 * large enough to hold the value of \p X.
521 * \return Another negative error code on different kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200522 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300523int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf,
524 size_t buflen );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200525
526/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300527 * \brief Export X into unsigned binary data, little endian.
528 * Always fills the whole buffer, which will end with zeros
529 * if the number is smaller.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200530 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300531 * \param X The source MPI. This must point to an initialized MPI.
532 * \param buf The output buffer. This must be a writable buffer of length
533 * \p buflen Bytes.
534 * \param buflen The size of the output buffer \p buf in Bytes.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200535 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300536 * \return \c 0 if successful.
537 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
538 * large enough to hold the value of \p X.
539 * \return Another negative error code on different kinds of failure.
540 */
541int mbedtls_mpi_write_binary_le( const mbedtls_mpi *X,
542 unsigned char *buf, size_t buflen );
543
544/**
545 * \brief Perform a left-shift on an MPI: X <<= count
546 *
547 * \param X The MPI to shift. This must point to an initialized MPI.
548 * \param count The number of bits to shift by.
549 *
550 * \return \c 0 if successful.
551 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
552 * \return Another negative error code on different kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200553 */
554int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
555
556/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300557 * \brief Perform a right-shift on an MPI: X >>= count
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200558 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300559 * \param X The MPI to shift. This must point to an initialized MPI.
560 * \param count The number of bits to shift by.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200561 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300562 * \return \c 0 if successful.
563 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
564 * \return Another negative error code on different kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200565 */
566int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
567
568/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300569 * \brief Compare the absolute values of two MPIs.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200570 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300571 * \param X The left-hand MPI. This must point to an initialized MPI.
572 * \param Y The right-hand MPI. This must point to an initialized MPI.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200573 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300574 * \return \c 1 if `|X|` is greater than `|Y|`.
575 * \return \c -1 if `|X|` is lesser than `|Y|`.
576 * \return \c 0 if `|X|` is equal to `|Y|`.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200577 */
578int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
579
580/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300581 * \brief Compare two MPIs.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200582 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300583 * \param X The left-hand MPI. This must point to an initialized MPI.
584 * \param Y The right-hand MPI. This must point to an initialized MPI.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200585 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300586 * \return \c 1 if \p X is greater than \p Y.
587 * \return \c -1 if \p X is lesser than \p Y.
588 * \return \c 0 if \p X is equal to \p Y.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200589 */
590int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
591
592/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300593 * \brief Check if an MPI is less than the other in constant time.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200594 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300595 * \param X The left-hand MPI. This must point to an initialized MPI
596 * with the same allocated length as Y.
597 * \param Y The right-hand MPI. This must point to an initialized MPI
598 * with the same allocated length as X.
599 * \param ret The result of the comparison:
600 * \c 1 if \p X is less than \p Y.
601 * \c 0 if \p X is greater than or equal to \p Y.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200602 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300603 * \return 0 on success.
604 * \return MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the allocated length of
605 * the two input MPIs is not the same.
606 */
607int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
608 unsigned *ret );
609
610/**
611 * \brief Compare an MPI with an integer.
612 *
613 * \param X The left-hand MPI. This must point to an initialized MPI.
614 * \param z The integer value to compare \p X to.
615 *
616 * \return \c 1 if \p X is greater than \p z.
617 * \return \c -1 if \p X is lesser than \p z.
618 * \return \c 0 if \p X is equal to \p z.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200619 */
620int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
621
622/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300623 * \brief Perform an unsigned addition of MPIs: X = |A| + |B|
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200624 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300625 * \param X The destination MPI. This must point to an initialized MPI.
626 * \param A The first summand. This must point to an initialized MPI.
627 * \param B The second summand. This must point to an initialized MPI.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200628 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300629 * \return \c 0 if successful.
630 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
631 * \return Another negative error code on different kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200632 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300633int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A,
634 const mbedtls_mpi *B );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200635
636/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300637 * \brief Perform an unsigned subtraction of MPIs: X = |A| - |B|
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200638 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300639 * \param X The destination MPI. This must point to an initialized MPI.
640 * \param A The minuend. This must point to an initialized MPI.
641 * \param B The subtrahend. This must point to an initialized MPI.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200642 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300643 * \return \c 0 if successful.
644 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p B is greater than \p A.
645 * \return Another negative error code on different kinds of failure.
646 *
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200647 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300648int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A,
649 const mbedtls_mpi *B );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200650
651/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300652 * \brief Perform a signed addition of MPIs: X = A + B
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200653 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300654 * \param X The destination MPI. This must point to an initialized MPI.
655 * \param A The first summand. This must point to an initialized MPI.
656 * \param B The second summand. This must point to an initialized MPI.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200657 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300658 * \return \c 0 if successful.
659 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
660 * \return Another negative error code on different kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200661 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300662int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A,
663 const mbedtls_mpi *B );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200664
665/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300666 * \brief Perform a signed subtraction of MPIs: X = A - B
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200667 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300668 * \param X The destination MPI. This must point to an initialized MPI.
669 * \param A The minuend. This must point to an initialized MPI.
670 * \param B The subtrahend. This must point to an initialized MPI.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200671 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300672 * \return \c 0 if successful.
673 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
674 * \return Another negative error code on different kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200675 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300676int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A,
677 const mbedtls_mpi *B );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200678
679/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300680 * \brief Perform a signed addition of an MPI and an integer: X = A + b
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200681 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300682 * \param X The destination MPI. This must point to an initialized MPI.
683 * \param A The first summand. This must point to an initialized MPI.
684 * \param b The second summand.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200685 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300686 * \return \c 0 if successful.
687 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
688 * \return Another negative error code on different kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200689 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300690int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A,
691 mbedtls_mpi_sint b );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200692
693/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300694 * \brief Perform a signed subtraction of an MPI and an integer:
695 * X = A - b
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200696 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300697 * \param X The destination MPI. This must point to an initialized MPI.
698 * \param A The minuend. This must point to an initialized MPI.
699 * \param b The subtrahend.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200700 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300701 * \return \c 0 if successful.
702 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
703 * \return Another negative error code on different kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200704 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300705int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A,
706 mbedtls_mpi_sint b );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200707
708/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300709 * \brief Perform a multiplication of two MPIs: X = A * B
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200710 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300711 * \param X The destination MPI. This must point to an initialized MPI.
712 * \param A The first factor. This must point to an initialized MPI.
713 * \param B The second factor. This must point to an initialized MPI.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200714 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300715 * \return \c 0 if successful.
716 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
717 * \return Another negative error code on different kinds of failure.
718 *
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200719 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300720int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A,
721 const mbedtls_mpi *B );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200722
723/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300724 * \brief Perform a multiplication of an MPI with an unsigned integer:
725 * X = A * b
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200726 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300727 * \param X The destination MPI. This must point to an initialized MPI.
728 * \param A The first factor. This must point to an initialized MPI.
729 * \param b The second factor.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200730 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300731 * \return \c 0 if successful.
732 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
733 * \return Another negative error code on different kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200734 *
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200735 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300736int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A,
737 mbedtls_mpi_uint b );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200738
739/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300740 * \brief Perform a division with remainder of two MPIs:
741 * A = Q * B + R
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200742 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300743 * \param Q The destination MPI for the quotient.
744 * This may be \c NULL if the value of the
745 * quotient is not needed.
746 * \param R The destination MPI for the remainder value.
747 * This may be \c NULL if the value of the
748 * remainder is not needed.
749 * \param A The dividend. This must point to an initialized MPi.
750 * \param B The divisor. This must point to an initialized MPI.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200751 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300752 * \return \c 0 if successful.
753 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
754 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p B equals zero.
755 * \return Another negative error code on different kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200756 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300757int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
758 const mbedtls_mpi *B );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200759
760/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300761 * \brief Perform a division with remainder of an MPI by an integer:
762 * A = Q * b + R
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200763 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300764 * \param Q The destination MPI for the quotient.
765 * This may be \c NULL if the value of the
766 * quotient is not needed.
767 * \param R The destination MPI for the remainder value.
768 * This may be \c NULL if the value of the
769 * remainder is not needed.
770 * \param A The dividend. This must point to an initialized MPi.
771 * \param b The divisor.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200772 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300773 * \return \c 0 if successful.
774 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
775 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p b equals zero.
776 * \return Another negative error code on different kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200777 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300778int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
779 mbedtls_mpi_sint b );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200780
781/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300782 * \brief Perform a modular reduction. R = A mod B
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200783 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300784 * \param R The destination MPI for the residue value.
785 * This must point to an initialized MPI.
786 * \param A The MPI to compute the residue of.
787 * This must point to an initialized MPI.
788 * \param B The base of the modular reduction.
789 * This must point to an initialized MPI.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200790 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300791 * \return \c 0 if successful.
792 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
793 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p B equals zero.
794 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p B is negative.
795 * \return Another negative error code on different kinds of failure.
796 *
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200797 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300798int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A,
799 const mbedtls_mpi *B );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200800
801/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300802 * \brief Perform a modular reduction with respect to an integer.
803 * r = A mod b
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200804 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300805 * \param r The address at which to store the residue.
806 * This must not be \c NULL.
807 * \param A The MPI to compute the residue of.
808 * This must point to an initialized MPi.
809 * \param b The integer base of the modular reduction.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200810 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300811 * \return \c 0 if successful.
812 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
813 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p b equals zero.
814 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p b is negative.
815 * \return Another negative error code on different kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200816 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300817int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A,
818 mbedtls_mpi_sint b );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200819
820/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300821 * \brief Perform a sliding-window exponentiation: X = A^E mod N
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200822 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300823 * \param X The destination MPI. This must point to an initialized MPI.
824 * \param A The base of the exponentiation.
825 * This must point to an initialized MPI.
826 * \param E The exponent MPI. This must point to an initialized MPI.
827 * \param N The base for the modular reduction. This must point to an
828 * initialized MPI.
829 * \param _RR A helper MPI depending solely on \p N which can be used to
830 * speed-up multiple modular exponentiations for the same value
831 * of \p N. This may be \c NULL. If it is not \c NULL, it must
832 * point to an initialized MPI. If it hasn't been used after
833 * the call to mbedtls_mpi_init(), this function will compute
834 * the helper value and store it in \p _RR for reuse on
835 * subsequent calls to this function. Otherwise, the function
836 * will assume that \p _RR holds the helper value set by a
837 * previous call to mbedtls_mpi_exp_mod(), and reuse it.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200838 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300839 * \return \c 0 if successful.
840 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
841 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \c N is negative or
842 * even, or if \c E is negative.
843 * \return Another negative error code on different kinds of failures.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200844 *
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200845 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300846int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
847 const mbedtls_mpi *E, const mbedtls_mpi *N,
848 mbedtls_mpi *_RR );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200849
850/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300851 * \brief Fill an MPI with a number of random bytes.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200852 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300853 * \param X The destination MPI. This must point to an initialized MPI.
854 * \param size The number of random bytes to generate.
855 * \param f_rng The RNG function to use. This must not be \c NULL.
856 * \param p_rng The RNG parameter to be passed to \p f_rng. This may be
857 * \c NULL if \p f_rng doesn't need a context argument.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200858 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300859 * \return \c 0 if successful.
860 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
861 * \return Another negative error code on failure.
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200862 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300863 * \note The bytes obtained from the RNG are interpreted
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200864 * as a big-endian representation of an MPI; this can
865 * be relevant in applications like deterministic ECDSA.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200866 */
867int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
868 int (*f_rng)(void *, unsigned char *, size_t),
869 void *p_rng );
870
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300871/** Generate a random number uniformly in a range.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200872 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300873 * This function generates a random number between \p min inclusive and
874 * \p N exclusive.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200875 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300876 * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA)
877 * when the RNG is a suitably parametrized instance of HMAC_DRBG
878 * and \p min is \c 1.
879 *
880 * \note There are `N - min` possible outputs. The lower bound
881 * \p min can be reached, but the upper bound \p N cannot.
882 *
883 * \param X The destination MPI. This must point to an initialized MPI.
884 * \param min The minimum value to return.
885 * It must be nonnegative.
886 * \param N The upper bound of the range, exclusive.
887 * In other words, this is one plus the maximum value to return.
888 * \p N must be strictly larger than \p min.
889 * \param f_rng The RNG function to use. This must not be \c NULL.
890 * \param p_rng The RNG parameter to be passed to \p f_rng.
891 *
892 * \return \c 0 if successful.
893 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
894 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p min or \p N is invalid
895 * or if they are incompatible.
896 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was
897 * unable to find a suitable value within a limited number
898 * of attempts. This has a negligible probability if \p N
899 * is significantly larger than \p min, which is the case
900 * for all usual cryptographic applications.
901 * \return Another negative error code on failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200902 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300903int mbedtls_mpi_random( mbedtls_mpi *X,
904 mbedtls_mpi_sint min,
905 const mbedtls_mpi *N,
906 int (*f_rng)(void *, unsigned char *, size_t),
907 void *p_rng );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200908
909/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300910 * \brief Compute the greatest common divisor: G = gcd(A, B)
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200911 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300912 * \param G The destination MPI. This must point to an initialized MPI.
913 * \param A The first operand. This must point to an initialized MPI.
914 * \param B The second operand. This must point to an initialized MPI.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200915 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300916 * \return \c 0 if successful.
917 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
918 * \return Another negative error code on different kinds of failure.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200919 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300920int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A,
921 const mbedtls_mpi *B );
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200922
923/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300924 * \brief Compute the modular inverse: X = A^-1 mod N
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200925 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300926 * \param X The destination MPI. This must point to an initialized MPI.
927 * \param A The MPI to calculate the modular inverse of. This must point
928 * to an initialized MPI.
929 * \param N The base of the modular inversion. This must point to an
930 * initialized MPI.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200931 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300932 * \return \c 0 if successful.
933 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
934 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p N is less than
935 * or equal to one.
936 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p has no modular inverse
937 * with respect to \p N.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200938 */
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300939int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
940 const mbedtls_mpi *N );
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200941
942/**
943 * \brief Miller-Rabin primality test.
944 *
945 * \warning If \p X is potentially generated by an adversary, for example
946 * when validating cryptographic parameters that you didn't
947 * generate yourself and that are supposed to be prime, then
948 * \p rounds should be at least the half of the security
949 * strength of the cryptographic algorithm. On the other hand,
950 * if \p X is chosen uniformly or non-adversially (as is the
951 * case when mbedtls_mpi_gen_prime calls this function), then
952 * \p rounds can be much lower.
953 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300954 * \param X The MPI to check for primality.
955 * This must point to an initialized MPI.
956 * \param rounds The number of bases to perform the Miller-Rabin primality
957 * test for. The probability of returning 0 on a composite is
958 * at most 2<sup>-2*\p rounds</sup>.
959 * \param f_rng The RNG function to use. This must not be \c NULL.
960 * \param p_rng The RNG parameter to be passed to \p f_rng.
961 * This may be \c NULL if \p f_rng doesn't use
962 * a context parameter.
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200963 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300964 * \return \c 0 if successful, i.e. \p X is probably prime.
965 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
966 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p X is not prime.
967 * \return Another negative error code on other kinds of failure.
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200968 */
969int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
970 int (*f_rng)(void *, unsigned char *, size_t),
971 void *p_rng );
972/**
973 * \brief Flags for mbedtls_mpi_gen_prime()
974 *
975 * Each of these flags is a constraint on the result X returned by
976 * mbedtls_mpi_gen_prime().
977 */
978typedef enum {
979 MBEDTLS_MPI_GEN_PRIME_FLAG_DH = 0x0001, /**< (X-1)/2 is prime too */
980 MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR = 0x0002, /**< lower error rate from 2<sup>-80</sup> to 2<sup>-128</sup> */
981} mbedtls_mpi_gen_prime_flag_t;
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200982
983/**
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300984 * \brief Generate a prime number.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200985 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300986 * \param X The destination MPI to store the generated prime in.
987 * This must point to an initialized MPi.
988 * \param nbits The required size of the destination MPI in bits.
989 * This must be between \c 3 and #MBEDTLS_MPI_MAX_BITS.
990 * \param flags A mask of flags of type #mbedtls_mpi_gen_prime_flag_t.
991 * \param f_rng The RNG function to use. This must not be \c NULL.
992 * \param p_rng The RNG parameter to be passed to \p f_rng.
993 * This may be \c NULL if \p f_rng doesn't use
994 * a context parameter.
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200995 *
Roman Okhrimenko977b3752022-03-31 14:40:48 +0300996 * \return \c 0 if successful, in which case \p X holds a
997 * probably prime number.
998 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
999 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if `nbits` is not between
1000 * \c 3 and #MBEDTLS_MPI_MAX_BITS.
Fabio Utzigba05f2a2017-12-05 11:00:41 -02001001 */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -02001002int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
Fabio Utzigba05f2a2017-12-05 11:00:41 -02001003 int (*f_rng)(void *, unsigned char *, size_t),
1004 void *p_rng );
1005
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001006#if defined(MBEDTLS_SELF_TEST)
1007
Fabio Utzigba05f2a2017-12-05 11:00:41 -02001008/**
1009 * \brief Checkup routine
1010 *
1011 * \return 0 if successful, or 1 if the test failed
1012 */
1013int mbedtls_mpi_self_test( int verbose );
1014
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001015#endif /* MBEDTLS_SELF_TEST */
1016
Fabio Utzigba05f2a2017-12-05 11:00:41 -02001017#ifdef __cplusplus
1018}
1019#endif
1020
1021#endif /* bignum.h */