4 * \brief Multi-precision integer library
6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: GPL-2.0
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * This file is part of mbed TLS (https://tls.mbed.org)
25 #ifndef MBEDTLS_BIGNUM_H
26 #define MBEDTLS_BIGNUM_H
28 #if !defined(MBEDTLS_CONFIG_FILE)
31 #include MBEDTLS_CONFIG_FILE
37 #if defined(MBEDTLS_FS_IO)
41 #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
42 #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
43 #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
44 #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
45 #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
46 #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
47 #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
48 #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 /**< Memory allocation failed. */
50 #define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
53 * Maximum size MPIs are allowed to grow to in number of limbs.
55 #define MBEDTLS_MPI_MAX_LIMBS 10000
57 #if !defined(MBEDTLS_MPI_WINDOW_SIZE)
59 * Maximum window size used for modular exponentiation. Default: 6
60 * Minimum value: 1. Maximum value: 6.
62 * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
63 * for the sliding window calculation. (So 64 by default)
65 * Reduction in size, reduces speed.
67 #define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
68 #endif /* !MBEDTLS_MPI_WINDOW_SIZE */
70 #if !defined(MBEDTLS_MPI_MAX_SIZE)
72 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
73 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
75 * Note: Calculations can results temporarily in larger MPIs. So the number
76 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
78 #define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
79 #endif /* !MBEDTLS_MPI_MAX_SIZE */
81 #define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
84 * When reading from files with mbedtls_mpi_read_file() and writing to files with
85 * mbedtls_mpi_write_file() the buffer should have space
86 * for a (short) label, the MPI (in the provided radix), the newline
87 * characters and the '\0'.
89 * By default we assume at least a 10 char label, a minimum radix of 10
90 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
91 * Autosized at compile time for at least a 10 char label, a minimum radix
92 * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
94 * This used to be statically sized to 1250 for a maximum of 4096 bit
95 * numbers (1234 decimal chars).
97 * Calculate using the formula:
98 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
101 #define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
102 #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
103 #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 )
106 * Define the base integer type, architecture-wise.
108 * 32-bit integers can be forced on 64-bit arches (eg. for testing purposes)
109 * by defining MBEDTLS_HAVE_INT32 and undefining MBEDTLS_HAVE_ASM
111 #if ( ! defined(MBEDTLS_HAVE_INT32) && \
112 defined(_MSC_VER) && defined(_M_AMD64) )
113 #define MBEDTLS_HAVE_INT64
114 typedef int64_t mbedtls_mpi_sint
;
115 typedef uint64_t mbedtls_mpi_uint
;
117 #if ( ! defined(MBEDTLS_HAVE_INT32) && \
118 defined(__GNUC__) && ( \
119 defined(__amd64__) || defined(__x86_64__) || \
120 defined(__ppc64__) || defined(__powerpc64__) || \
121 defined(__ia64__) || defined(__alpha__) || \
122 (defined(__sparc__) && defined(__arch64__)) || \
123 defined(__s390x__) || defined(__mips64) ) )
124 #define MBEDTLS_HAVE_INT64
125 typedef int64_t mbedtls_mpi_sint
;
126 typedef uint64_t mbedtls_mpi_uint
;
127 /* mbedtls_t_udbl defined as 128-bit unsigned int */
128 typedef unsigned int mbedtls_t_udbl
__attribute__((mode(TI
)));
129 #define MBEDTLS_HAVE_UDBL
131 #define MBEDTLS_HAVE_INT32
132 typedef int32_t mbedtls_mpi_sint
;
133 typedef uint32_t mbedtls_mpi_uint
;
134 typedef uint64_t mbedtls_t_udbl
;
135 #define MBEDTLS_HAVE_UDBL
136 #endif /* !MBEDTLS_HAVE_INT32 && __GNUC__ && 64-bit platform */
137 #endif /* !MBEDTLS_HAVE_INT32 && _MSC_VER && _M_AMD64 */
144 * \brief MPI structure
148 int s
; /*!< integer sign */
149 size_t n
; /*!< total # of limbs */
150 mbedtls_mpi_uint
*p
; /*!< pointer to limbs */
155 * \brief Initialize one MPI (make internal references valid)
156 * This just makes it ready to be set or freed,
157 * but does not define a value for the MPI.
159 * \param X One MPI to initialize.
161 void mbedtls_mpi_init( mbedtls_mpi
*X
);
164 * \brief Unallocate one MPI
166 * \param X One MPI to unallocate.
168 void mbedtls_mpi_free( mbedtls_mpi
*X
);
171 * \brief Enlarge to the specified number of limbs
173 * \param X MPI to grow
174 * \param nblimbs The target number of limbs
176 * \return 0 if successful,
177 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
179 int mbedtls_mpi_grow( mbedtls_mpi
*X
, size_t nblimbs
);
182 * \brief Resize down, keeping at least the specified number of limbs
184 * \param X MPI to shrink
185 * \param nblimbs The minimum number of limbs to keep
187 * \return 0 if successful,
188 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
190 int mbedtls_mpi_shrink( mbedtls_mpi
*X
, size_t nblimbs
);
193 * \brief Copy the contents of Y into X
195 * \param X Destination MPI
196 * \param Y Source MPI
198 * \return 0 if successful,
199 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
201 int mbedtls_mpi_copy( mbedtls_mpi
*X
, const mbedtls_mpi
*Y
);
204 * \brief Swap the contents of X and Y
206 * \param X First MPI value
207 * \param Y Second MPI value
209 void mbedtls_mpi_swap( mbedtls_mpi
*X
, mbedtls_mpi
*Y
);
212 * \brief Safe conditional assignement X = Y if assign is 1
214 * \param X MPI to conditionally assign to
215 * \param Y Value to be assigned
216 * \param assign 1: perform the assignment, 0: keep X's original value
218 * \return 0 if successful,
219 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
221 * \note This function is equivalent to
222 * if( assign ) mbedtls_mpi_copy( X, Y );
223 * except that it avoids leaking any information about whether
224 * the assignment was done or not (the above code may leak
225 * information through branch prediction and/or memory access
226 * patterns analysis).
228 int mbedtls_mpi_safe_cond_assign( mbedtls_mpi
*X
, const mbedtls_mpi
*Y
, unsigned char assign
);
231 * \brief Safe conditional swap X <-> Y if swap is 1
233 * \param X First mbedtls_mpi value
234 * \param Y Second mbedtls_mpi value
235 * \param assign 1: perform the swap, 0: keep X and Y's original values
237 * \return 0 if successful,
238 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
240 * \note This function is equivalent to
241 * if( assign ) mbedtls_mpi_swap( X, Y );
242 * except that it avoids leaking any information about whether
243 * the assignment was done or not (the above code may leak
244 * information through branch prediction and/or memory access
245 * patterns analysis).
247 int mbedtls_mpi_safe_cond_swap( mbedtls_mpi
*X
, mbedtls_mpi
*Y
, unsigned char assign
);
250 * \brief Set value from integer
252 * \param X MPI to set
253 * \param z Value to use
255 * \return 0 if successful,
256 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
258 int mbedtls_mpi_lset( mbedtls_mpi
*X
, mbedtls_mpi_sint z
);
261 * \brief Get a specific bit from X
263 * \param X MPI to use
264 * \param pos Zero-based index of the bit in X
266 * \return Either a 0 or a 1
268 int mbedtls_mpi_get_bit( const mbedtls_mpi
*X
, size_t pos
);
271 * \brief Set a bit of X to a specific value of 0 or 1
273 * \note Will grow X if necessary to set a bit to 1 in a not yet
274 * existing limb. Will not grow if bit should be set to 0
276 * \param X MPI to use
277 * \param pos Zero-based index of the bit in X
278 * \param val The value to set the bit to (0 or 1)
280 * \return 0 if successful,
281 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
282 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
284 int mbedtls_mpi_set_bit( mbedtls_mpi
*X
, size_t pos
, unsigned char val
);
287 * \brief Return the number of zero-bits before the least significant
290 * Note: Thus also the zero-based index of the least significant '1' bit
292 * \param X MPI to use
294 size_t mbedtls_mpi_lsb( const mbedtls_mpi
*X
);
297 * \brief Return the number of bits up to and including the most
298 * significant '1' bit'
300 * Note: Thus also the one-based index of the most significant '1' bit
302 * \param X MPI to use
304 size_t mbedtls_mpi_bitlen( const mbedtls_mpi
*X
);
307 * \brief Return the total size in bytes
309 * \param X MPI to use
311 size_t mbedtls_mpi_size( const mbedtls_mpi
*X
);
314 * \brief Import from an ASCII string
316 * \param X Destination MPI
317 * \param radix Input numeric base
318 * \param s Null-terminated string buffer
320 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
322 int mbedtls_mpi_read_string( mbedtls_mpi
*X
, int radix
, const char *s
);
325 * \brief Export into an ASCII string
327 * \param X Source MPI
328 * \param radix Output numeric base
329 * \param buf Buffer to write the string to
330 * \param buflen Length of buf
331 * \param olen Length of the string written, including final NUL byte
333 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code.
334 * *olen is always updated to reflect the amount
335 * of data that has (or would have) been written.
337 * \note Call this function with buflen = 0 to obtain the
338 * minimum required buffer size in *olen.
340 int mbedtls_mpi_write_string( const mbedtls_mpi
*X
, int radix
,
341 char *buf
, size_t buflen
, size_t *olen
);
343 #if defined(MBEDTLS_FS_IO)
345 * \brief Read X from an opened file
347 * \param X Destination MPI
348 * \param radix Input numeric base
349 * \param fin Input file handle
351 * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
352 * the file read buffer is too small or a
353 * MBEDTLS_ERR_MPI_XXX error code
355 int mbedtls_mpi_read_file( mbedtls_mpi
*X
, int radix
, FILE *fin
);
358 * \brief Write X into an opened file, or stdout if fout is NULL
360 * \param p Prefix, can be NULL
361 * \param X Source MPI
362 * \param radix Output numeric base
363 * \param fout Output file handle (can be NULL)
365 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
367 * \note Set fout == NULL to print X on the console.
369 int mbedtls_mpi_write_file( const char *p
, const mbedtls_mpi
*X
, int radix
, FILE *fout
);
370 #endif /* MBEDTLS_FS_IO */
373 * \brief Import X from unsigned binary data, big endian
375 * \param X Destination MPI
376 * \param buf Input buffer
377 * \param buflen Input buffer size
379 * \return 0 if successful,
380 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
382 int mbedtls_mpi_read_binary( mbedtls_mpi
*X
, const unsigned char *buf
, size_t buflen
);
385 * \brief Export X into unsigned binary data, big endian.
386 * Always fills the whole buffer, which will start with zeros
387 * if the number is smaller.
389 * \param X Source MPI
390 * \param buf Output buffer
391 * \param buflen Output buffer size
393 * \return 0 if successful,
394 * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
396 int mbedtls_mpi_write_binary( const mbedtls_mpi
*X
, unsigned char *buf
, size_t buflen
);
399 * \brief Left-shift: X <<= count
401 * \param X MPI to shift
402 * \param count Amount to shift
404 * \return 0 if successful,
405 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
407 int mbedtls_mpi_shift_l( mbedtls_mpi
*X
, size_t count
);
410 * \brief Right-shift: X >>= count
412 * \param X MPI to shift
413 * \param count Amount to shift
415 * \return 0 if successful,
416 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
418 int mbedtls_mpi_shift_r( mbedtls_mpi
*X
, size_t count
);
421 * \brief Compare unsigned values
423 * \param X Left-hand MPI
424 * \param Y Right-hand MPI
426 * \return 1 if |X| is greater than |Y|,
427 * -1 if |X| is lesser than |Y| or
428 * 0 if |X| is equal to |Y|
430 int mbedtls_mpi_cmp_abs( const mbedtls_mpi
*X
, const mbedtls_mpi
*Y
);
433 * \brief Compare signed values
435 * \param X Left-hand MPI
436 * \param Y Right-hand MPI
438 * \return 1 if X is greater than Y,
439 * -1 if X is lesser than Y or
440 * 0 if X is equal to Y
442 int mbedtls_mpi_cmp_mpi( const mbedtls_mpi
*X
, const mbedtls_mpi
*Y
);
445 * \brief Compare signed values
447 * \param X Left-hand MPI
448 * \param z The integer value to compare to
450 * \return 1 if X is greater than z,
451 * -1 if X is lesser than z or
452 * 0 if X is equal to z
454 int mbedtls_mpi_cmp_int( const mbedtls_mpi
*X
, mbedtls_mpi_sint z
);
457 * \brief Unsigned addition: X = |A| + |B|
459 * \param X Destination MPI
460 * \param A Left-hand MPI
461 * \param B Right-hand MPI
463 * \return 0 if successful,
464 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
466 int mbedtls_mpi_add_abs( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, const mbedtls_mpi
*B
);
469 * \brief Unsigned subtraction: X = |A| - |B|
471 * \param X Destination MPI
472 * \param A Left-hand MPI
473 * \param B Right-hand MPI
475 * \return 0 if successful,
476 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
478 int mbedtls_mpi_sub_abs( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, const mbedtls_mpi
*B
);
481 * \brief Signed addition: X = A + B
483 * \param X Destination MPI
484 * \param A Left-hand MPI
485 * \param B Right-hand MPI
487 * \return 0 if successful,
488 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
490 int mbedtls_mpi_add_mpi( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, const mbedtls_mpi
*B
);
493 * \brief Signed subtraction: X = A - B
495 * \param X Destination MPI
496 * \param A Left-hand MPI
497 * \param B Right-hand MPI
499 * \return 0 if successful,
500 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
502 int mbedtls_mpi_sub_mpi( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, const mbedtls_mpi
*B
);
505 * \brief Signed addition: X = A + b
507 * \param X Destination MPI
508 * \param A Left-hand MPI
509 * \param b The integer value to add
511 * \return 0 if successful,
512 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
514 int mbedtls_mpi_add_int( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, mbedtls_mpi_sint b
);
517 * \brief Signed subtraction: X = A - b
519 * \param X Destination MPI
520 * \param A Left-hand MPI
521 * \param b The integer value to subtract
523 * \return 0 if successful,
524 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
526 int mbedtls_mpi_sub_int( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, mbedtls_mpi_sint b
);
529 * \brief Baseline multiplication: X = A * B
531 * \param X Destination MPI
532 * \param A Left-hand MPI
533 * \param B Right-hand MPI
535 * \return 0 if successful,
536 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
538 int mbedtls_mpi_mul_mpi( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, const mbedtls_mpi
*B
);
541 * \brief Baseline multiplication: X = A * b
543 * \param X Destination MPI
544 * \param A Left-hand MPI
545 * \param b The unsigned integer value to multiply with
547 * \note b is unsigned
549 * \return 0 if successful,
550 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
552 int mbedtls_mpi_mul_int( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, mbedtls_mpi_uint b
);
555 * \brief Division by mbedtls_mpi: A = Q * B + R
557 * \param Q Destination MPI for the quotient
558 * \param R Destination MPI for the rest value
559 * \param A Left-hand MPI
560 * \param B Right-hand MPI
562 * \return 0 if successful,
563 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
564 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0
566 * \note Either Q or R can be NULL.
568 int mbedtls_mpi_div_mpi( mbedtls_mpi
*Q
, mbedtls_mpi
*R
, const mbedtls_mpi
*A
, const mbedtls_mpi
*B
);
571 * \brief Division by int: A = Q * b + R
573 * \param Q Destination MPI for the quotient
574 * \param R Destination MPI for the rest value
575 * \param A Left-hand MPI
576 * \param b Integer to divide by
578 * \return 0 if successful,
579 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
580 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0
582 * \note Either Q or R can be NULL.
584 int mbedtls_mpi_div_int( mbedtls_mpi
*Q
, mbedtls_mpi
*R
, const mbedtls_mpi
*A
, mbedtls_mpi_sint b
);
587 * \brief Modulo: R = A mod B
589 * \param R Destination MPI for the rest value
590 * \param A Left-hand MPI
591 * \param B Right-hand MPI
593 * \return 0 if successful,
594 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
595 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0,
596 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
598 int mbedtls_mpi_mod_mpi( mbedtls_mpi
*R
, const mbedtls_mpi
*A
, const mbedtls_mpi
*B
);
601 * \brief Modulo: r = A mod b
603 * \param r Destination mbedtls_mpi_uint
604 * \param A Left-hand MPI
605 * \param b Integer to divide by
607 * \return 0 if successful,
608 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
609 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0,
610 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
612 int mbedtls_mpi_mod_int( mbedtls_mpi_uint
*r
, const mbedtls_mpi
*A
, mbedtls_mpi_sint b
);
615 * \brief Sliding-window exponentiation: X = A^E mod N
617 * \param X Destination MPI
618 * \param A Left-hand MPI
619 * \param E Exponent MPI
620 * \param N Modular MPI
621 * \param _RR Speed-up MPI used for recalculations
623 * \return 0 if successful,
624 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
625 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
628 * \note _RR is used to avoid re-computing R*R mod N across
629 * multiple calls, which speeds up things a bit. It can
630 * be set to NULL if the extra performance is unneeded.
632 int mbedtls_mpi_exp_mod( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, const mbedtls_mpi
*E
, const mbedtls_mpi
*N
, mbedtls_mpi
*_RR
);
635 * \brief Fill an MPI X with size bytes of random
637 * \param X Destination MPI
638 * \param size Size in bytes
639 * \param f_rng RNG function
640 * \param p_rng RNG parameter
642 * \return 0 if successful,
643 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
645 int mbedtls_mpi_fill_random( mbedtls_mpi
*X
, size_t size
,
646 int (*f_rng
)(void *, unsigned char *, size_t),
650 * \brief Greatest common divisor: G = gcd(A, B)
652 * \param G Destination MPI
653 * \param A Left-hand MPI
654 * \param B Right-hand MPI
656 * \return 0 if successful,
657 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
659 int mbedtls_mpi_gcd( mbedtls_mpi
*G
, const mbedtls_mpi
*A
, const mbedtls_mpi
*B
);
662 * \brief Modular inverse: X = A^-1 mod N
664 * \param X Destination MPI
665 * \param A Left-hand MPI
666 * \param N Right-hand MPI
668 * \return 0 if successful,
669 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
670 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
671 MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
673 int mbedtls_mpi_inv_mod( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, const mbedtls_mpi
*N
);
676 * \brief Miller-Rabin primality test
678 * \param X MPI to check
679 * \param f_rng RNG function
680 * \param p_rng RNG parameter
682 * \return 0 if successful (probably prime),
683 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
684 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
686 int mbedtls_mpi_is_prime( const mbedtls_mpi
*X
,
687 int (*f_rng
)(void *, unsigned char *, size_t),
691 * \brief Prime number generation
693 * \param X Destination MPI
694 * \param nbits Required size of X in bits
695 * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
696 * \param dh_flag If 1, then (X-1)/2 will be prime too
697 * \param f_rng RNG function
698 * \param p_rng RNG parameter
700 * \return 0 if successful (probably prime),
701 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
702 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
704 int mbedtls_mpi_gen_prime( mbedtls_mpi
*X
, size_t nbits
, int dh_flag
,
705 int (*f_rng
)(void *, unsigned char *, size_t),
709 * \brief Checkup routine
711 * \return 0 if successful, or 1 if the test failed
713 int mbedtls_mpi_self_test( int verbose
);
719 #endif /* bignum.h */