146224a2b7acec5994c088e655f14d0582ff9e54
4 * \brief Multi-precision integer library
6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * This file is part of mbed TLS (https://tls.mbed.org)
23 #ifndef MBEDTLS_BIGNUM_H
24 #define MBEDTLS_BIGNUM_H
26 #if !defined(MBEDTLS_CONFIG_FILE)
29 #include MBEDTLS_CONFIG_FILE
35 #if defined(MBEDTLS_FS_IO)
39 #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
40 #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
41 #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
42 #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
43 #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
44 #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
45 #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
46 #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 /**< Memory allocation failed. */
48 #define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
51 * Maximum size MPIs are allowed to grow to in number of limbs.
53 #define MBEDTLS_MPI_MAX_LIMBS 10000
55 #if !defined(MBEDTLS_MPI_WINDOW_SIZE)
57 * Maximum window size used for modular exponentiation. Default: 6
58 * Minimum value: 1. Maximum value: 6.
60 * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
61 * for the sliding window calculation. (So 64 by default)
63 * Reduction in size, reduces speed.
65 #define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
66 #endif /* !MBEDTLS_MPI_WINDOW_SIZE */
68 #if !defined(MBEDTLS_MPI_MAX_SIZE)
70 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
71 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
73 * Note: Calculations can results temporarily in larger MPIs. So the number
74 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
76 #define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
77 #endif /* !MBEDTLS_MPI_MAX_SIZE */
79 #define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
82 * When reading from files with mbedtls_mpi_read_file() and writing to files with
83 * mbedtls_mpi_write_file() the buffer should have space
84 * for a (short) label, the MPI (in the provided radix), the newline
85 * characters and the '\0'.
87 * By default we assume at least a 10 char label, a minimum radix of 10
88 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
89 * Autosized at compile time for at least a 10 char label, a minimum radix
90 * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
92 * This used to be statically sized to 1250 for a maximum of 4096 bit
93 * numbers (1234 decimal chars).
95 * Calculate using the formula:
96 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
99 #define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
100 #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
101 #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 )
104 * Define the base integer type, architecture-wise.
106 * 32-bit integers can be forced on 64-bit arches (eg. for testing purposes)
107 * by defining MBEDTLS_HAVE_INT32 and undefining MBEDTLS_HAVE_ASM
109 #if ( ! defined(MBEDTLS_HAVE_INT32) && \
110 defined(_MSC_VER) && defined(_M_AMD64) )
111 #define MBEDTLS_HAVE_INT64
112 typedef int64_t mbedtls_mpi_sint
;
113 typedef uint64_t mbedtls_mpi_uint
;
115 #if ( ! defined(MBEDTLS_HAVE_INT32) && \
116 defined(__GNUC__) && ( \
117 defined(__amd64__) || defined(__x86_64__) || \
118 defined(__ppc64__) || defined(__powerpc64__) || \
119 defined(__ia64__) || defined(__alpha__) || \
120 (defined(__sparc__) && defined(__arch64__)) || \
121 defined(__s390x__) || defined(__mips64) ) )
122 #define MBEDTLS_HAVE_INT64
123 typedef int64_t mbedtls_mpi_sint
;
124 typedef uint64_t mbedtls_mpi_uint
;
125 typedef unsigned int mbedtls_t_udbl
__attribute__((mode(TI
)));
126 #define MBEDTLS_HAVE_UDBL
128 #define MBEDTLS_HAVE_INT32
129 typedef int32_t mbedtls_mpi_sint
;
130 typedef uint32_t mbedtls_mpi_uint
;
131 typedef uint64_t mbedtls_t_udbl
;
132 #define MBEDTLS_HAVE_UDBL
133 #endif /* !MBEDTLS_HAVE_INT32 && __GNUC__ && 64-bit platform */
134 #endif /* !MBEDTLS_HAVE_INT32 && _MSC_VER && _M_AMD64 */
141 * \brief MPI structure
145 int s
; /*!< integer sign */
146 size_t n
; /*!< total # of limbs */
147 mbedtls_mpi_uint
*p
; /*!< pointer to limbs */
152 * \brief Initialize one MPI (make internal references valid)
153 * This just makes it ready to be set or freed,
154 * but does not define a value for the MPI.
156 * \param X One MPI to initialize.
158 void mbedtls_mpi_init( mbedtls_mpi
*X
);
161 * \brief Unallocate one MPI
163 * \param X One MPI to unallocate.
165 void mbedtls_mpi_free( mbedtls_mpi
*X
);
168 * \brief Enlarge to the specified number of limbs
170 * \param X MPI to grow
171 * \param nblimbs The target number of limbs
173 * \return 0 if successful,
174 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
176 int mbedtls_mpi_grow( mbedtls_mpi
*X
, size_t nblimbs
);
179 * \brief Resize down, keeping at least the specified number of limbs
181 * \param X MPI to shrink
182 * \param nblimbs The minimum number of limbs to keep
184 * \return 0 if successful,
185 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
187 int mbedtls_mpi_shrink( mbedtls_mpi
*X
, size_t nblimbs
);
190 * \brief Copy the contents of Y into X
192 * \param X Destination MPI
193 * \param Y Source MPI
195 * \return 0 if successful,
196 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
198 int mbedtls_mpi_copy( mbedtls_mpi
*X
, const mbedtls_mpi
*Y
);
201 * \brief Swap the contents of X and Y
203 * \param X First MPI value
204 * \param Y Second MPI value
206 void mbedtls_mpi_swap( mbedtls_mpi
*X
, mbedtls_mpi
*Y
);
209 * \brief Safe conditional assignement X = Y if assign is 1
211 * \param X MPI to conditionally assign to
212 * \param Y Value to be assigned
213 * \param assign 1: perform the assignment, 0: keep X's original value
215 * \return 0 if successful,
216 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
218 * \note This function is equivalent to
219 * if( assign ) mbedtls_mpi_copy( X, Y );
220 * except that it avoids leaking any information about whether
221 * the assignment was done or not (the above code may leak
222 * information through branch prediction and/or memory access
223 * patterns analysis).
225 int mbedtls_mpi_safe_cond_assign( mbedtls_mpi
*X
, const mbedtls_mpi
*Y
, unsigned char assign
);
228 * \brief Safe conditional swap X <-> Y if swap is 1
230 * \param X First mbedtls_mpi value
231 * \param Y Second mbedtls_mpi value
232 * \param assign 1: perform the swap, 0: keep X and Y's original values
234 * \return 0 if successful,
235 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
237 * \note This function is equivalent to
238 * if( assign ) mbedtls_mpi_swap( X, Y );
239 * except that it avoids leaking any information about whether
240 * the assignment was done or not (the above code may leak
241 * information through branch prediction and/or memory access
242 * patterns analysis).
244 int mbedtls_mpi_safe_cond_swap( mbedtls_mpi
*X
, mbedtls_mpi
*Y
, unsigned char assign
);
247 * \brief Set value from integer
249 * \param X MPI to set
250 * \param z Value to use
252 * \return 0 if successful,
253 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
255 int mbedtls_mpi_lset( mbedtls_mpi
*X
, mbedtls_mpi_sint z
);
258 * \brief Get a specific bit from X
260 * \param X MPI to use
261 * \param pos Zero-based index of the bit in X
263 * \return Either a 0 or a 1
265 int mbedtls_mpi_get_bit( const mbedtls_mpi
*X
, size_t pos
);
268 * \brief Set a bit of X to a specific value of 0 or 1
270 * \note Will grow X if necessary to set a bit to 1 in a not yet
271 * existing limb. Will not grow if bit should be set to 0
273 * \param X MPI to use
274 * \param pos Zero-based index of the bit in X
275 * \param val The value to set the bit to (0 or 1)
277 * \return 0 if successful,
278 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
279 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
281 int mbedtls_mpi_set_bit( mbedtls_mpi
*X
, size_t pos
, unsigned char val
);
284 * \brief Return the number of zero-bits before the least significant
287 * Note: Thus also the zero-based index of the least significant '1' bit
289 * \param X MPI to use
291 size_t mbedtls_mpi_lsb( const mbedtls_mpi
*X
);
294 * \brief Return the number of bits up to and including the most
295 * significant '1' bit'
297 * Note: Thus also the one-based index of the most significant '1' bit
299 * \param X MPI to use
301 size_t mbedtls_mpi_bitlen( const mbedtls_mpi
*X
);
304 * \brief Return the total size in bytes
306 * \param X MPI to use
308 size_t mbedtls_mpi_size( const mbedtls_mpi
*X
);
311 * \brief Import from an ASCII string
313 * \param X Destination MPI
314 * \param radix Input numeric base
315 * \param s Null-terminated string buffer
317 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
319 int mbedtls_mpi_read_string( mbedtls_mpi
*X
, int radix
, const char *s
);
322 * \brief Export into an ASCII string
324 * \param X Source MPI
325 * \param radix Output numeric base
326 * \param buf Buffer to write the string to
327 * \param buflen Length of buf
328 * \param olen Length of the string written, including final NUL byte
330 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code.
331 * *olen is always updated to reflect the amount
332 * of data that has (or would have) been written.
334 * \note Call this function with buflen = 0 to obtain the
335 * minimum required buffer size in *olen.
337 int mbedtls_mpi_write_string( const mbedtls_mpi
*X
, int radix
,
338 char *buf
, size_t buflen
, size_t *olen
);
340 #if defined(MBEDTLS_FS_IO)
342 * \brief Read X from an opened file
344 * \param X Destination MPI
345 * \param radix Input numeric base
346 * \param fin Input file handle
348 * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
349 * the file read buffer is too small or a
350 * MBEDTLS_ERR_MPI_XXX error code
352 int mbedtls_mpi_read_file( mbedtls_mpi
*X
, int radix
, FILE *fin
);
355 * \brief Write X into an opened file, or stdout if fout is NULL
357 * \param p Prefix, can be NULL
358 * \param X Source MPI
359 * \param radix Output numeric base
360 * \param fout Output file handle (can be NULL)
362 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
364 * \note Set fout == NULL to print X on the console.
366 int mbedtls_mpi_write_file( const char *p
, const mbedtls_mpi
*X
, int radix
, FILE *fout
);
367 #endif /* MBEDTLS_FS_IO */
370 * \brief Import X from unsigned binary data, big endian
372 * \param X Destination MPI
373 * \param buf Input buffer
374 * \param buflen Input buffer size
376 * \return 0 if successful,
377 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
379 int mbedtls_mpi_read_binary( mbedtls_mpi
*X
, const unsigned char *buf
, size_t buflen
);
382 * \brief Export X into unsigned binary data, big endian.
383 * Always fills the whole buffer, which will start with zeros
384 * if the number is smaller.
386 * \param X Source MPI
387 * \param buf Output buffer
388 * \param buflen Output buffer size
390 * \return 0 if successful,
391 * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
393 int mbedtls_mpi_write_binary( const mbedtls_mpi
*X
, unsigned char *buf
, size_t buflen
);
396 * \brief Left-shift: X <<= count
398 * \param X MPI to shift
399 * \param count Amount to shift
401 * \return 0 if successful,
402 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
404 int mbedtls_mpi_shift_l( mbedtls_mpi
*X
, size_t count
);
407 * \brief Right-shift: X >>= count
409 * \param X MPI to shift
410 * \param count Amount to shift
412 * \return 0 if successful,
413 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
415 int mbedtls_mpi_shift_r( mbedtls_mpi
*X
, size_t count
);
418 * \brief Compare unsigned values
420 * \param X Left-hand MPI
421 * \param Y Right-hand MPI
423 * \return 1 if |X| is greater than |Y|,
424 * -1 if |X| is lesser than |Y| or
425 * 0 if |X| is equal to |Y|
427 int mbedtls_mpi_cmp_abs( const mbedtls_mpi
*X
, const mbedtls_mpi
*Y
);
430 * \brief Compare signed values
432 * \param X Left-hand MPI
433 * \param Y Right-hand MPI
435 * \return 1 if X is greater than Y,
436 * -1 if X is lesser than Y or
437 * 0 if X is equal to Y
439 int mbedtls_mpi_cmp_mpi( const mbedtls_mpi
*X
, const mbedtls_mpi
*Y
);
442 * \brief Compare signed values
444 * \param X Left-hand MPI
445 * \param z The integer value to compare to
447 * \return 1 if X is greater than z,
448 * -1 if X is lesser than z or
449 * 0 if X is equal to z
451 int mbedtls_mpi_cmp_int( const mbedtls_mpi
*X
, mbedtls_mpi_sint z
);
454 * \brief Unsigned addition: X = |A| + |B|
456 * \param X Destination MPI
457 * \param A Left-hand MPI
458 * \param B Right-hand MPI
460 * \return 0 if successful,
461 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
463 int mbedtls_mpi_add_abs( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, const mbedtls_mpi
*B
);
466 * \brief Unsigned subtraction: X = |A| - |B|
468 * \param X Destination MPI
469 * \param A Left-hand MPI
470 * \param B Right-hand MPI
472 * \return 0 if successful,
473 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
475 int mbedtls_mpi_sub_abs( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, const mbedtls_mpi
*B
);
478 * \brief Signed addition: X = A + B
480 * \param X Destination MPI
481 * \param A Left-hand MPI
482 * \param B Right-hand MPI
484 * \return 0 if successful,
485 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
487 int mbedtls_mpi_add_mpi( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, const mbedtls_mpi
*B
);
490 * \brief Signed subtraction: X = A - B
492 * \param X Destination MPI
493 * \param A Left-hand MPI
494 * \param B Right-hand MPI
496 * \return 0 if successful,
497 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
499 int mbedtls_mpi_sub_mpi( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, const mbedtls_mpi
*B
);
502 * \brief Signed addition: X = A + b
504 * \param X Destination MPI
505 * \param A Left-hand MPI
506 * \param b The integer value to add
508 * \return 0 if successful,
509 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
511 int mbedtls_mpi_add_int( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, mbedtls_mpi_sint b
);
514 * \brief Signed subtraction: X = A - b
516 * \param X Destination MPI
517 * \param A Left-hand MPI
518 * \param b The integer value to subtract
520 * \return 0 if successful,
521 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
523 int mbedtls_mpi_sub_int( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, mbedtls_mpi_sint b
);
526 * \brief Baseline multiplication: X = A * B
528 * \param X Destination MPI
529 * \param A Left-hand MPI
530 * \param B Right-hand MPI
532 * \return 0 if successful,
533 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
535 int mbedtls_mpi_mul_mpi( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, const mbedtls_mpi
*B
);
538 * \brief Baseline multiplication: X = A * b
540 * \param X Destination MPI
541 * \param A Left-hand MPI
542 * \param b The unsigned integer value to multiply with
544 * \note b is unsigned
546 * \return 0 if successful,
547 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
549 int mbedtls_mpi_mul_int( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, mbedtls_mpi_uint b
);
552 * \brief Division by mbedtls_mpi: A = Q * B + R
554 * \param Q Destination MPI for the quotient
555 * \param R Destination MPI for the rest value
556 * \param A Left-hand MPI
557 * \param B Right-hand MPI
559 * \return 0 if successful,
560 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
561 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0
563 * \note Either Q or R can be NULL.
565 int mbedtls_mpi_div_mpi( mbedtls_mpi
*Q
, mbedtls_mpi
*R
, const mbedtls_mpi
*A
, const mbedtls_mpi
*B
);
568 * \brief Division by int: A = Q * b + R
570 * \param Q Destination MPI for the quotient
571 * \param R Destination MPI for the rest value
572 * \param A Left-hand MPI
573 * \param b Integer to divide by
575 * \return 0 if successful,
576 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
577 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0
579 * \note Either Q or R can be NULL.
581 int mbedtls_mpi_div_int( mbedtls_mpi
*Q
, mbedtls_mpi
*R
, const mbedtls_mpi
*A
, mbedtls_mpi_sint b
);
584 * \brief Modulo: R = A mod B
586 * \param R Destination MPI for the rest value
587 * \param A Left-hand MPI
588 * \param B Right-hand MPI
590 * \return 0 if successful,
591 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
592 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0,
593 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
595 int mbedtls_mpi_mod_mpi( mbedtls_mpi
*R
, const mbedtls_mpi
*A
, const mbedtls_mpi
*B
);
598 * \brief Modulo: r = A mod b
600 * \param r Destination mbedtls_mpi_uint
601 * \param A Left-hand MPI
602 * \param b Integer to divide by
604 * \return 0 if successful,
605 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
606 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0,
607 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
609 int mbedtls_mpi_mod_int( mbedtls_mpi_uint
*r
, const mbedtls_mpi
*A
, mbedtls_mpi_sint b
);
612 * \brief Sliding-window exponentiation: X = A^E mod N
614 * \param X Destination MPI
615 * \param A Left-hand MPI
616 * \param E Exponent MPI
617 * \param N Modular MPI
618 * \param _RR Speed-up MPI used for recalculations
620 * \return 0 if successful,
621 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
622 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
625 * \note _RR is used to avoid re-computing R*R mod N across
626 * multiple calls, which speeds up things a bit. It can
627 * be set to NULL if the extra performance is unneeded.
629 int mbedtls_mpi_exp_mod( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, const mbedtls_mpi
*E
, const mbedtls_mpi
*N
, mbedtls_mpi
*_RR
);
632 * \brief Fill an MPI X with size bytes of random
634 * \param X Destination MPI
635 * \param size Size in bytes
636 * \param f_rng RNG function
637 * \param p_rng RNG parameter
639 * \return 0 if successful,
640 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
642 int mbedtls_mpi_fill_random( mbedtls_mpi
*X
, size_t size
,
643 int (*f_rng
)(void *, unsigned char *, size_t),
647 * \brief Greatest common divisor: G = gcd(A, B)
649 * \param G Destination MPI
650 * \param A Left-hand MPI
651 * \param B Right-hand MPI
653 * \return 0 if successful,
654 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
656 int mbedtls_mpi_gcd( mbedtls_mpi
*G
, const mbedtls_mpi
*A
, const mbedtls_mpi
*B
);
659 * \brief Modular inverse: X = A^-1 mod N
661 * \param X Destination MPI
662 * \param A Left-hand MPI
663 * \param N Right-hand MPI
665 * \return 0 if successful,
666 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
667 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or nil
668 MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N
670 int mbedtls_mpi_inv_mod( mbedtls_mpi
*X
, const mbedtls_mpi
*A
, const mbedtls_mpi
*N
);
673 * \brief Miller-Rabin primality test
675 * \param X MPI to check
676 * \param f_rng RNG function
677 * \param p_rng RNG parameter
679 * \return 0 if successful (probably prime),
680 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
681 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
683 int mbedtls_mpi_is_prime( const mbedtls_mpi
*X
,
684 int (*f_rng
)(void *, unsigned char *, size_t),
688 * \brief Prime number generation
690 * \param X Destination MPI
691 * \param nbits Required size of X in bits
692 * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
693 * \param dh_flag If 1, then (X-1)/2 will be prime too
694 * \param f_rng RNG function
695 * \param p_rng RNG parameter
697 * \return 0 if successful (probably prime),
698 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
699 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
701 int mbedtls_mpi_gen_prime( mbedtls_mpi
*X
, size_t nbits
, int dh_flag
,
702 int (*f_rng
)(void *, unsigned char *, size_t),
706 * \brief Checkup routine
708 * \return 0 if successful, or 1 if the test failed
710 int mbedtls_mpi_self_test( int verbose
);
716 #endif /* bignum.h */