1
#pragma GCC system_header
5
* \brief Multi-precision integer library
8
* Copyright The Mbed TLS Contributors
9
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
11
* This file is provided under the Apache License 2.0, or the
12
* GNU General Public License v2.0 or later.
17
* Licensed under the Apache License, Version 2.0 (the "License"); you may
18
* not use this file except in compliance with the License.
19
* You may obtain a copy of the License at
21
* http://www.apache.org/licenses/LICENSE-2.0
23
* Unless required by applicable law or agreed to in writing, software
24
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26
* See the License for the specific language governing permissions and
27
* limitations under the License.
32
* GNU General Public License v2.0 or later:
34
* This program is free software; you can redistribute it and/or modify
35
* it under the terms of the GNU General Public License as published by
36
* the Free Software Foundation; either version 2 of the License, or
37
* (at your option) any later version.
39
* This program is distributed in the hope that it will be useful,
40
* but WITHOUT ANY WARRANTY; without even the implied warranty of
41
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42
* GNU General Public License for more details.
44
* You should have received a copy of the GNU General Public License along
45
* with this program; if not, write to the Free Software Foundation, Inc.,
46
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
50
#ifndef MBEDTLS_BIGNUM_H
51
#define MBEDTLS_BIGNUM_H
53
#if !defined(MBEDTLS_CONFIG_FILE)
56
#include MBEDTLS_CONFIG_FILE
62
#if defined(MBEDTLS_FS_IO)
66
#define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
67
#define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
68
#define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
69
#define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
70
#define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
71
#define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
72
#define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
73
#define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 /**< Memory allocation failed. */
75
#define MBEDTLS_MPI_CHK(f) \
78
if( ( ret = (f) ) != 0 ) \
83
* Maximum size MPIs are allowed to grow to in number of limbs.
85
#define MBEDTLS_MPI_MAX_LIMBS 10000
87
#if !defined(MBEDTLS_MPI_WINDOW_SIZE)
89
* Maximum window size used for modular exponentiation. Default: 6
90
* Minimum value: 1. Maximum value: 6.
92
* Result is an array of ( 2 ** MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
93
* for the sliding window calculation. (So 64 by default)
95
* Reduction in size, reduces speed.
97
#define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum window size used. */
98
#endif /* !MBEDTLS_MPI_WINDOW_SIZE */
100
#if !defined(MBEDTLS_MPI_MAX_SIZE)
102
* Maximum size of MPIs allowed in bits and bytes for user-MPIs.
103
* ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
105
* Note: Calculations can temporarily result in larger MPIs. So the number
106
* of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
108
#define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
109
#endif /* !MBEDTLS_MPI_MAX_SIZE */
111
#define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
114
* When reading from files with mbedtls_mpi_read_file() and writing to files with
115
* mbedtls_mpi_write_file() the buffer should have space
116
* for a (short) label, the MPI (in the provided radix), the newline
117
* characters and the '\0'.
119
* By default we assume at least a 10 char label, a minimum radix of 10
120
* (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
121
* Autosized at compile time for at least a 10 char label, a minimum radix
122
* of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
124
* This used to be statically sized to 1250 for a maximum of 4096 bit
125
* numbers (1234 decimal chars).
127
* Calculate using the formula:
128
* MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
131
#define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
132
#define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
133
#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 )
136
* Define the base integer type, architecture-wise.
138
* 32 or 64-bit integer types can be forced regardless of the underlying
139
* architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
140
* respectively and undefining MBEDTLS_HAVE_ASM.
142
* Double-width integers (e.g. 128-bit in 64-bit architectures) can be
143
* disabled by defining MBEDTLS_NO_UDBL_DIVISION.
145
#if !defined(MBEDTLS_HAVE_INT32)
146
#if defined(_MSC_VER) && defined(_M_AMD64)
147
/* Always choose 64-bit when using MSC */
148
#if !defined(MBEDTLS_HAVE_INT64)
149
#define MBEDTLS_HAVE_INT64
150
#endif /* !MBEDTLS_HAVE_INT64 */
151
typedef int64_t mbedtls_mpi_sint;
152
typedef uint64_t mbedtls_mpi_uint;
153
#elif defined(__GNUC__) && ( \
154
defined(__amd64__) || defined(__x86_64__) || \
155
defined(__ppc64__) || defined(__powerpc64__) || \
156
defined(__ia64__) || defined(__alpha__) || \
157
( defined(__sparc__) && defined(__arch64__) ) || \
158
defined(__s390x__) || defined(__mips64) )
159
#if !defined(MBEDTLS_HAVE_INT64)
160
#define MBEDTLS_HAVE_INT64
161
#endif /* MBEDTLS_HAVE_INT64 */
162
typedef int64_t mbedtls_mpi_sint;
163
typedef uint64_t mbedtls_mpi_uint;
164
#if !defined(MBEDTLS_NO_UDBL_DIVISION)
165
/* mbedtls_t_udbl defined as 128-bit unsigned int */
166
typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
167
#define MBEDTLS_HAVE_UDBL
168
#endif /* !MBEDTLS_NO_UDBL_DIVISION */
169
#elif defined(__ARMCC_VERSION) && defined(__aarch64__)
171
* __ARMCC_VERSION is defined for both armcc and armclang and
172
* __aarch64__ is only defined by armclang when compiling 64-bit code
174
#if !defined(MBEDTLS_HAVE_INT64)
175
#define MBEDTLS_HAVE_INT64
176
#endif /* !MBEDTLS_HAVE_INT64 */
177
typedef int64_t mbedtls_mpi_sint;
178
typedef uint64_t mbedtls_mpi_uint;
179
#if !defined(MBEDTLS_NO_UDBL_DIVISION)
180
/* mbedtls_t_udbl defined as 128-bit unsigned int */
181
typedef __uint128_t mbedtls_t_udbl;
182
#define MBEDTLS_HAVE_UDBL
183
#endif /* !MBEDTLS_NO_UDBL_DIVISION */
184
#elif defined(MBEDTLS_HAVE_INT64)
185
/* Force 64-bit integers with unknown compiler */
186
typedef int64_t mbedtls_mpi_sint;
187
typedef uint64_t mbedtls_mpi_uint;
189
#endif /* !MBEDTLS_HAVE_INT32 */
191
#if !defined(MBEDTLS_HAVE_INT64)
192
/* Default to 32-bit compilation */
193
#if !defined(MBEDTLS_HAVE_INT32)
194
#define MBEDTLS_HAVE_INT32
195
#endif /* !MBEDTLS_HAVE_INT32 */
196
typedef int32_t mbedtls_mpi_sint;
197
typedef uint32_t mbedtls_mpi_uint;
198
#if !defined(MBEDTLS_NO_UDBL_DIVISION)
199
typedef uint64_t mbedtls_t_udbl;
200
#define MBEDTLS_HAVE_UDBL
201
#endif /* !MBEDTLS_NO_UDBL_DIVISION */
202
#endif /* !MBEDTLS_HAVE_INT64 */
209
* \brief MPI structure
211
typedef struct mbedtls_mpi
213
int s; /*!< Sign: -1 if the mpi is negative, 1 otherwise */
214
size_t n; /*!< total # of limbs */
215
mbedtls_mpi_uint *p; /*!< pointer to limbs */
220
* \brief Initialize an MPI context.
222
* This makes the MPI ready to be set or freed,
223
* but does not define a value for the MPI.
225
* \param X The MPI context to initialize. This must not be \c NULL.
227
void mbedtls_mpi_init( mbedtls_mpi *X );
230
* \brief This function frees the components of an MPI context.
232
* \param X The MPI context to be cleared. This may be \c NULL,
233
* in which case this function is a no-op. If it is
234
* not \c NULL, it must point to an initialized MPI.
236
void mbedtls_mpi_free( mbedtls_mpi *X );
239
* \brief Enlarge an MPI to the specified number of limbs.
241
* \note This function does nothing if the MPI is
242
* already large enough.
244
* \param X The MPI to grow. It must be initialized.
245
* \param nblimbs The target number of limbs.
247
* \return \c 0 if successful.
248
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
249
* \return Another negative error code on other kinds of failure.
251
int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
254
* \brief This function resizes an MPI downwards, keeping at least the
255
* specified number of limbs.
257
* If \c X is smaller than \c nblimbs, it is resized up
260
* \param X The MPI to shrink. This must point to an initialized MPI.
261
* \param nblimbs The minimum number of limbs to keep.
263
* \return \c 0 if successful.
264
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
265
* (this can only happen when resizing up).
266
* \return Another negative error code on other kinds of failure.
268
int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
271
* \brief Make a copy of an MPI.
273
* \param X The destination MPI. This must point to an initialized MPI.
274
* \param Y The source MPI. This must point to an initialized MPI.
276
* \note The limb-buffer in the destination MPI is enlarged
277
* if necessary to hold the value in the source MPI.
279
* \return \c 0 if successful.
280
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
281
* \return Another negative error code on other kinds of failure.
283
int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
286
* \brief Swap the contents of two MPIs.
288
* \param X The first MPI. It must be initialized.
289
* \param Y The second MPI. It must be initialized.
291
void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
294
* \brief Perform a safe conditional copy of MPI which doesn't
295
* reveal whether the condition was true or not.
297
* \param X The MPI to conditionally assign to. This must point
298
* to an initialized MPI.
299
* \param Y The MPI to be assigned from. This must point to an
301
* \param assign The condition deciding whether to perform the
302
* assignment or not. Possible values:
303
* * \c 1: Perform the assignment `X = Y`.
304
* * \c 0: Keep the original value of \p X.
306
* \note This function is equivalent to
307
* `if( assign ) mbedtls_mpi_copy( X, Y );`
308
* except that it avoids leaking any information about whether
309
* the assignment was done or not (the above code may leak
310
* information through branch prediction and/or memory access
311
* patterns analysis).
313
* \return \c 0 if successful.
314
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
315
* \return Another negative error code on other kinds of failure.
317
int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
320
* \brief Perform a safe conditional swap which doesn't
321
* reveal whether the condition was true or not.
323
* \param X The first MPI. This must be initialized.
324
* \param Y The second MPI. This must be initialized.
325
* \param assign The condition deciding whether to perform
326
* the swap or not. Possible values:
327
* * \c 1: Swap the values of \p X and \p Y.
328
* * \c 0: Keep the original values of \p X and \p Y.
330
* \note This function is equivalent to
331
* if( assign ) mbedtls_mpi_swap( X, Y );
332
* except that it avoids leaking any information about whether
333
* the assignment was done or not (the above code may leak
334
* information through branch prediction and/or memory access
335
* patterns analysis).
337
* \return \c 0 if successful.
338
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
339
* \return Another negative error code on other kinds of failure.
342
int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
345
* \brief Store integer value in MPI.
347
* \param X The MPI to set. This must be initialized.
348
* \param z The value to use.
350
* \return \c 0 if successful.
351
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
352
* \return Another negative error code on other kinds of failure.
354
int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
357
* \brief Get a specific bit from an MPI.
359
* \param X The MPI to query. This must be initialized.
360
* \param pos Zero-based index of the bit to query.
362
* \return \c 0 or \c 1 on success, depending on whether bit \c pos
363
* of \c X is unset or set.
364
* \return A negative error code on failure.
366
int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
369
* \brief Modify a specific bit in an MPI.
371
* \note This function will grow the target MPI if necessary to set a
372
* bit to \c 1 in a not yet existing limb. It will not grow if
373
* the bit should be set to \c 0.
375
* \param X The MPI to modify. This must be initialized.
376
* \param pos Zero-based index of the bit to modify.
377
* \param val The desired value of bit \c pos: \c 0 or \c 1.
379
* \return \c 0 if successful.
380
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
381
* \return Another negative error code on other kinds of failure.
383
int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
386
* \brief Return the number of bits of value \c 0 before the
387
* least significant bit of value \c 1.
389
* \note This is the same as the zero-based index of
390
* the least significant bit of value \c 1.
392
* \param X The MPI to query.
394
* \return The number of bits of value \c 0 before the least significant
395
* bit of value \c 1 in \p X.
397
size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
400
* \brief Return the number of bits up to and including the most
401
* significant bit of value \c 1.
403
* * \note This is same as the one-based index of the most
404
* significant bit of value \c 1.
406
* \param X The MPI to query. This must point to an initialized MPI.
408
* \return The number of bits up to and including the most
409
* significant bit of value \c 1.
411
size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
414
* \brief Return the total size of an MPI value in bytes.
416
* \param X The MPI to use. This must point to an initialized MPI.
418
* \note The value returned by this function may be less than
419
* the number of bytes used to store \p X internally.
420
* This happens if and only if there are trailing bytes
423
* \return The least number of bytes capable of storing
424
* the absolute value of \p X.
426
size_t mbedtls_mpi_size( const mbedtls_mpi *X );
429
* \brief Import an MPI from an ASCII string.
431
* \param X The destination MPI. This must point to an initialized MPI.
432
* \param radix The numeric base of the input string.
433
* \param s Null-terminated string buffer.
435
* \return \c 0 if successful.
436
* \return A negative error code on failure.
438
int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
441
* \brief Export an MPI to an ASCII string.
443
* \param X The source MPI. This must point to an initialized MPI.
444
* \param radix The numeric base of the output string.
445
* \param buf The buffer to write the string to. This must be writable
446
* buffer of length \p buflen Bytes.
447
* \param buflen The available size in Bytes of \p buf.
448
* \param olen The address at which to store the length of the string
449
* written, including the final \c NULL byte. This must
452
* \note You can call this function with `buflen == 0` to obtain the
453
* minimum required buffer size in `*olen`.
455
* \return \c 0 if successful.
456
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the target buffer \p buf
457
* is too small to hold the value of \p X in the desired base.
458
* In this case, `*olen` is nonetheless updated to contain the
459
* size of \p buf required for a successful call.
460
* \return Another negative error code on different kinds of failure.
462
int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
463
char *buf, size_t buflen, size_t *olen );
465
#if defined(MBEDTLS_FS_IO)
467
* \brief Read an MPI from a line in an opened file.
469
* \param X The destination MPI. This must point to an initialized MPI.
470
* \param radix The numeric base of the string representation used
471
* in the source line.
472
* \param fin The input file handle to use. This must not be \c NULL.
474
* \note On success, this function advances the file stream
475
* to the end of the current line or to EOF.
477
* The function returns \c 0 on an empty line.
479
* Leading whitespaces are ignored, as is a
480
* '0x' prefix for radix \c 16.
482
* \return \c 0 if successful.
483
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the file read buffer
485
* \return Another negative error code on failure.
487
int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
490
* \brief Export an MPI into an opened file.
492
* \param p A string prefix to emit prior to the MPI data.
493
* For example, this might be a label, or "0x" when
494
* printing in base \c 16. This may be \c NULL if no prefix
496
* \param X The source MPI. This must point to an initialized MPI.
497
* \param radix The numeric base to be used in the emitted string.
498
* \param fout The output file handle. This may be \c NULL, in which case
499
* the output is written to \c stdout.
501
* \return \c 0 if successful.
502
* \return A negative error code on failure.
504
int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X,
505
int radix, FILE *fout );
506
#endif /* MBEDTLS_FS_IO */
509
* \brief Import an MPI from unsigned big endian binary data.
511
* \param X The destination MPI. This must point to an initialized MPI.
512
* \param buf The input buffer. This must be a readable buffer of length
514
* \param buflen The length of the input buffer \p p in Bytes.
516
* \return \c 0 if successful.
517
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
518
* \return Another negative error code on different kinds of failure.
520
int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf,
524
* \brief Export an MPI into unsigned big endian binary data
527
* \param X The source MPI. This must point to an initialized MPI.
528
* \param buf The output buffer. This must be a writable buffer of length
530
* \param buflen The size of the output buffer \p buf in Bytes.
532
* \return \c 0 if successful.
533
* \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't
534
* large enough to hold the value of \p X.
535
* \return Another negative error code on different kinds of failure.
537
int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf,
541
* \brief Perform a left-shift on an MPI: X <<= count
543
* \param X The MPI to shift. This must point to an initialized MPI.
544
* \param count The number of bits to shift by.
546
* \return \c 0 if successful.
547
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
548
* \return Another negative error code on different kinds of failure.
550
int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
553
* \brief Perform a right-shift on an MPI: X >>= count
555
* \param X The MPI to shift. This must point to an initialized MPI.
556
* \param count The number of bits to shift by.
558
* \return \c 0 if successful.
559
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
560
* \return Another negative error code on different kinds of failure.
562
int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
565
* \brief Compare the absolute values of two MPIs.
567
* \param X The left-hand MPI. This must point to an initialized MPI.
568
* \param Y The right-hand MPI. This must point to an initialized MPI.
570
* \return \c 1 if `|X|` is greater than `|Y|`.
571
* \return \c -1 if `|X|` is lesser than `|Y|`.
572
* \return \c 0 if `|X|` is equal to `|Y|`.
574
int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
577
* \brief Compare two MPIs.
579
* \param X The left-hand MPI. This must point to an initialized MPI.
580
* \param Y The right-hand MPI. This must point to an initialized MPI.
582
* \return \c 1 if \p X is greater than \p Y.
583
* \return \c -1 if \p X is lesser than \p Y.
584
* \return \c 0 if \p X is equal to \p Y.
586
int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
589
* \brief Check if an MPI is less than the other in constant time.
591
* \param X The left-hand MPI. This must point to an initialized MPI
592
* with the same allocated length as Y.
593
* \param Y The right-hand MPI. This must point to an initialized MPI
594
* with the same allocated length as X.
595
* \param ret The result of the comparison:
596
* \c 1 if \p X is less than \p Y.
597
* \c 0 if \p X is greater than or equal to \p Y.
599
* \return 0 on success.
600
* \return MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the allocated length of
601
* the two input MPIs is not the same.
603
int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
607
* \brief Compare an MPI with an integer.
609
* \param X The left-hand MPI. This must point to an initialized MPI.
610
* \param z The integer value to compare \p X to.
612
* \return \c 1 if \p X is greater than \p z.
613
* \return \c -1 if \p X is lesser than \p z.
614
* \return \c 0 if \p X is equal to \p z.
616
int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
619
* \brief Perform an unsigned addition of MPIs: X = |A| + |B|
621
* \param X The destination MPI. This must point to an initialized MPI.
622
* \param A The first summand. This must point to an initialized MPI.
623
* \param B The second summand. This must point to an initialized MPI.
625
* \return \c 0 if successful.
626
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
627
* \return Another negative error code on different kinds of failure.
629
int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A,
630
const mbedtls_mpi *B );
633
* \brief Perform an unsigned subtraction of MPIs: X = |A| - |B|
635
* \param X The destination MPI. This must point to an initialized MPI.
636
* \param A The minuend. This must point to an initialized MPI.
637
* \param B The subtrahend. This must point to an initialized MPI.
639
* \return \c 0 if successful.
640
* \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p B is greater than \p A.
641
* \return Another negative error code on different kinds of failure.
644
int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A,
645
const mbedtls_mpi *B );
648
* \brief Perform a signed addition of MPIs: X = A + B
650
* \param X The destination MPI. This must point to an initialized MPI.
651
* \param A The first summand. This must point to an initialized MPI.
652
* \param B The second summand. This must point to an initialized MPI.
654
* \return \c 0 if successful.
655
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
656
* \return Another negative error code on different kinds of failure.
658
int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A,
659
const mbedtls_mpi *B );
662
* \brief Perform a signed subtraction of MPIs: X = A - B
664
* \param X The destination MPI. This must point to an initialized MPI.
665
* \param A The minuend. This must point to an initialized MPI.
666
* \param B The subtrahend. This must point to an initialized MPI.
668
* \return \c 0 if successful.
669
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
670
* \return Another negative error code on different kinds of failure.
672
int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A,
673
const mbedtls_mpi *B );
676
* \brief Perform a signed addition of an MPI and an integer: X = A + b
678
* \param X The destination MPI. This must point to an initialized MPI.
679
* \param A The first summand. This must point to an initialized MPI.
680
* \param b The second summand.
682
* \return \c 0 if successful.
683
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
684
* \return Another negative error code on different kinds of failure.
686
int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A,
687
mbedtls_mpi_sint b );
690
* \brief Perform a signed subtraction of an MPI and an integer:
693
* \param X The destination MPI. This must point to an initialized MPI.
694
* \param A The minuend. This must point to an initialized MPI.
695
* \param b The subtrahend.
697
* \return \c 0 if successful.
698
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
699
* \return Another negative error code on different kinds of failure.
701
int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A,
702
mbedtls_mpi_sint b );
705
* \brief Perform a multiplication of two MPIs: X = A * B
707
* \param X The destination MPI. This must point to an initialized MPI.
708
* \param A The first factor. This must point to an initialized MPI.
709
* \param B The second factor. This must point to an initialized MPI.
711
* \return \c 0 if successful.
712
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
713
* \return Another negative error code on different kinds of failure.
716
int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A,
717
const mbedtls_mpi *B );
720
* \brief Perform a multiplication of an MPI with an unsigned integer:
723
* \param X The destination MPI. This must point to an initialized MPI.
724
* \param A The first factor. This must point to an initialized MPI.
725
* \param b The second factor.
727
* \return \c 0 if successful.
728
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
729
* \return Another negative error code on different kinds of failure.
732
int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A,
733
mbedtls_mpi_uint b );
736
* \brief Perform a division with remainder of two MPIs:
739
* \param Q The destination MPI for the quotient.
740
* This may be \c NULL if the value of the
741
* quotient is not needed.
742
* \param R The destination MPI for the remainder value.
743
* This may be \c NULL if the value of the
744
* remainder is not needed.
745
* \param A The dividend. This must point to an initialized MPi.
746
* \param B The divisor. This must point to an initialized MPI.
748
* \return \c 0 if successful.
749
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
750
* \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p B equals zero.
751
* \return Another negative error code on different kinds of failure.
753
int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
754
const mbedtls_mpi *B );
757
* \brief Perform a division with remainder of an MPI by an integer:
760
* \param Q The destination MPI for the quotient.
761
* This may be \c NULL if the value of the
762
* quotient is not needed.
763
* \param R The destination MPI for the remainder value.
764
* This may be \c NULL if the value of the
765
* remainder is not needed.
766
* \param A The dividend. This must point to an initialized MPi.
767
* \param b The divisor.
769
* \return \c 0 if successful.
770
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed.
771
* \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p b equals zero.
772
* \return Another negative error code on different kinds of failure.
774
int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A,
775
mbedtls_mpi_sint b );
778
* \brief Perform a modular reduction. R = A mod B
780
* \param R The destination MPI for the residue value.
781
* This must point to an initialized MPI.
782
* \param A The MPI to compute the residue of.
783
* This must point to an initialized MPI.
784
* \param B The base of the modular reduction.
785
* This must point to an initialized MPI.
787
* \return \c 0 if successful.
788
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
789
* \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p B equals zero.
790
* \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p B is negative.
791
* \return Another negative error code on different kinds of failure.
794
int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A,
795
const mbedtls_mpi *B );
798
* \brief Perform a modular reduction with respect to an integer.
801
* \param r The address at which to store the residue.
802
* This must not be \c NULL.
803
* \param A The MPI to compute the residue of.
804
* This must point to an initialized MPi.
805
* \param b The integer base of the modular reduction.
807
* \return \c 0 if successful.
808
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
809
* \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p b equals zero.
810
* \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p b is negative.
811
* \return Another negative error code on different kinds of failure.
813
int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A,
814
mbedtls_mpi_sint b );
817
* \brief Perform a sliding-window exponentiation: X = A^E mod N
819
* \param X The destination MPI. This must point to an initialized MPI.
820
* \param A The base of the exponentiation.
821
* This must point to an initialized MPI.
822
* \param E The exponent MPI. This must point to an initialized MPI.
823
* \param N The base for the modular reduction. This must point to an
825
* \param _RR A helper MPI depending solely on \p N which can be used to
826
* speed-up multiple modular exponentiations for the same value
827
* of \p N. This may be \c NULL. If it is not \c NULL, it must
828
* point to an initialized MPI. If it hasn't been used after
829
* the call to mbedtls_mpi_init(), this function will compute
830
* the helper value and store it in \p _RR for reuse on
831
* subsequent calls to this function. Otherwise, the function
832
* will assume that \p _RR holds the helper value set by a
833
* previous call to mbedtls_mpi_exp_mod(), and reuse it.
835
* \return \c 0 if successful.
836
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
837
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \c N is negative or
838
* even, or if \c E is negative.
839
* \return Another negative error code on different kinds of failures.
842
int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
843
const mbedtls_mpi *E, const mbedtls_mpi *N,
847
* \brief Fill an MPI with a number of random bytes.
849
* \param X The destination MPI. This must point to an initialized MPI.
850
* \param size The number of random bytes to generate.
851
* \param f_rng The RNG function to use. This must not be \c NULL.
852
* \param p_rng The RNG parameter to be passed to \p f_rng. This may be
853
* \c NULL if \p f_rng doesn't need a context argument.
855
* \return \c 0 if successful.
856
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
857
* \return Another negative error code on failure.
859
* \note The bytes obtained from the RNG are interpreted
860
* as a big-endian representation of an MPI; this can
861
* be relevant in applications like deterministic ECDSA.
863
int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
864
int (*f_rng)(void *, unsigned char *, size_t),
868
* \brief Compute the greatest common divisor: G = gcd(A, B)
870
* \param G The destination MPI. This must point to an initialized MPI.
871
* \param A The first operand. This must point to an initialized MPI.
872
* \param B The second operand. This must point to an initialized MPI.
874
* \return \c 0 if successful.
875
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
876
* \return Another negative error code on different kinds of failure.
878
int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A,
879
const mbedtls_mpi *B );
882
* \brief Compute the modular inverse: X = A^-1 mod N
884
* \param X The destination MPI. This must point to an initialized MPI.
885
* \param A The MPI to calculate the modular inverse of. This must point
886
* to an initialized MPI.
887
* \param N The base of the modular inversion. This must point to an
890
* \return \c 0 if successful.
891
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
892
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p N is less than
894
* \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p has no modular inverse
895
* with respect to \p N.
897
int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
898
const mbedtls_mpi *N );
900
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
901
#if defined(MBEDTLS_DEPRECATED_WARNING)
902
#define MBEDTLS_DEPRECATED __attribute__((deprecated))
904
#define MBEDTLS_DEPRECATED
907
* \brief Perform a Miller-Rabin primality test with error
908
* probability of 2<sup>-80</sup>.
910
* \disabled_deprecated Superseded by mbedtls_mpi_is_prime_ext() which allows
911
* specifying the number of Miller-Rabin rounds.
913
* \param X The MPI to check for primality.
914
* This must point to an initialized MPI.
915
* \param f_rng The RNG function to use. This must not be \c NULL.
916
* \param p_rng The RNG parameter to be passed to \p f_rng.
917
* This may be \c NULL if \p f_rng doesn't use a
920
* \return \c 0 if successful, i.e. \p X is probably prime.
921
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
922
* \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p X is not prime.
923
* \return Another negative error code on other kinds of failure.
925
MBEDTLS_DEPRECATED int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
926
int (*f_rng)(void *, unsigned char *, size_t),
928
#undef MBEDTLS_DEPRECATED
929
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
932
* \brief Miller-Rabin primality test.
934
* \warning If \p X is potentially generated by an adversary, for example
935
* when validating cryptographic parameters that you didn't
936
* generate yourself and that are supposed to be prime, then
937
* \p rounds should be at least the half of the security
938
* strength of the cryptographic algorithm. On the other hand,
939
* if \p X is chosen uniformly or non-adversially (as is the
940
* case when mbedtls_mpi_gen_prime calls this function), then
941
* \p rounds can be much lower.
943
* \param X The MPI to check for primality.
944
* This must point to an initialized MPI.
945
* \param rounds The number of bases to perform the Miller-Rabin primality
946
* test for. The probability of returning 0 on a composite is
947
* at most 2<sup>-2*\p rounds</sup>.
948
* \param f_rng The RNG function to use. This must not be \c NULL.
949
* \param p_rng The RNG parameter to be passed to \p f_rng.
950
* This may be \c NULL if \p f_rng doesn't use
951
* a context parameter.
953
* \return \c 0 if successful, i.e. \p X is probably prime.
954
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
955
* \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p X is not prime.
956
* \return Another negative error code on other kinds of failure.
958
int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
959
int (*f_rng)(void *, unsigned char *, size_t),
962
* \brief Flags for mbedtls_mpi_gen_prime()
964
* Each of these flags is a constraint on the result X returned by
965
* mbedtls_mpi_gen_prime().
968
MBEDTLS_MPI_GEN_PRIME_FLAG_DH = 0x0001, /**< (X-1)/2 is prime too */
969
MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR = 0x0002, /**< lower error rate from 2<sup>-80</sup> to 2<sup>-128</sup> */
970
} mbedtls_mpi_gen_prime_flag_t;
973
* \brief Generate a prime number.
975
* \param X The destination MPI to store the generated prime in.
976
* This must point to an initialized MPi.
977
* \param nbits The required size of the destination MPI in bits.
978
* This must be between \c 3 and #MBEDTLS_MPI_MAX_BITS.
979
* \param flags A mask of flags of type #mbedtls_mpi_gen_prime_flag_t.
980
* \param f_rng The RNG function to use. This must not be \c NULL.
981
* \param p_rng The RNG parameter to be passed to \p f_rng.
982
* This may be \c NULL if \p f_rng doesn't use
983
* a context parameter.
985
* \return \c 0 if successful, in which case \p X holds a
986
* probably prime number.
987
* \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed.
988
* \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if `nbits` is not between
989
* \c 3 and #MBEDTLS_MPI_MAX_BITS.
991
int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
992
int (*f_rng)(void *, unsigned char *, size_t),
995
#if defined(MBEDTLS_SELF_TEST)
998
* \brief Checkup routine
1000
* \return 0 if successful, or 1 if the test failed
1002
int mbedtls_mpi_self_test( int verbose );
1004
#endif /* MBEDTLS_SELF_TEST */
1010
#endif /* bignum.h */