[MBEDTLS]
[reactos.git] / reactos / sdk / include / reactos / libs / mbedtls / bignum.h
1 /**
2 * \file bignum.h
3 *
4 * \brief Multi-precision integer library
5 *
6 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: GPL-2.0
8 *
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.
13 *
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.
18 *
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.
22 *
23 * This file is part of mbed TLS (https://tls.mbed.org)
24 */
25 #ifndef MBEDTLS_BIGNUM_H
26 #define MBEDTLS_BIGNUM_H
27
28 #if !defined(MBEDTLS_CONFIG_FILE)
29 #include "config.h"
30 #else
31 #include MBEDTLS_CONFIG_FILE
32 #endif
33
34 #include <stddef.h>
35 #include <stdint.h>
36
37 #if defined(MBEDTLS_FS_IO)
38 #include <stdio.h>
39 #endif
40
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. */
49
50 #define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
51
52 /*
53 * Maximum size MPIs are allowed to grow to in number of limbs.
54 */
55 #define MBEDTLS_MPI_MAX_LIMBS 10000
56
57 #if !defined(MBEDTLS_MPI_WINDOW_SIZE)
58 /*
59 * Maximum window size used for modular exponentiation. Default: 6
60 * Minimum value: 1. Maximum value: 6.
61 *
62 * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
63 * for the sliding window calculation. (So 64 by default)
64 *
65 * Reduction in size, reduces speed.
66 */
67 #define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
68 #endif /* !MBEDTLS_MPI_WINDOW_SIZE */
69
70 #if !defined(MBEDTLS_MPI_MAX_SIZE)
71 /*
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 )
74 *
75 * Note: Calculations can results temporarily in larger MPIs. So the number
76 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
77 */
78 #define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
79 #endif /* !MBEDTLS_MPI_MAX_SIZE */
80
81 #define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
82
83 /*
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'.
88 *
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.
93 *
94 * This used to be statically sized to 1250 for a maximum of 4096 bit
95 * numbers (1234 decimal chars).
96 *
97 * Calculate using the formula:
98 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
99 * LabelSize + 6
100 */
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 )
104
105 /*
106 * Define the base integer type, architecture-wise.
107 *
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
110 */
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;
116 #else
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
130 #else
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 */
138
139 #ifdef __cplusplus
140 extern "C" {
141 #endif
142
143 /**
144 * \brief MPI structure
145 */
146 typedef struct
147 {
148 int s; /*!< integer sign */
149 size_t n; /*!< total # of limbs */
150 mbedtls_mpi_uint *p; /*!< pointer to limbs */
151 }
152 mbedtls_mpi;
153
154 /**
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.
158 *
159 * \param X One MPI to initialize.
160 */
161 void mbedtls_mpi_init( mbedtls_mpi *X );
162
163 /**
164 * \brief Unallocate one MPI
165 *
166 * \param X One MPI to unallocate.
167 */
168 void mbedtls_mpi_free( mbedtls_mpi *X );
169
170 /**
171 * \brief Enlarge to the specified number of limbs
172 *
173 * \param X MPI to grow
174 * \param nblimbs The target number of limbs
175 *
176 * \return 0 if successful,
177 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
178 */
179 int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
180
181 /**
182 * \brief Resize down, keeping at least the specified number of limbs
183 *
184 * \param X MPI to shrink
185 * \param nblimbs The minimum number of limbs to keep
186 *
187 * \return 0 if successful,
188 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
189 */
190 int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
191
192 /**
193 * \brief Copy the contents of Y into X
194 *
195 * \param X Destination MPI
196 * \param Y Source MPI
197 *
198 * \return 0 if successful,
199 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
200 */
201 int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
202
203 /**
204 * \brief Swap the contents of X and Y
205 *
206 * \param X First MPI value
207 * \param Y Second MPI value
208 */
209 void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
210
211 /**
212 * \brief Safe conditional assignement X = Y if assign is 1
213 *
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
217 *
218 * \return 0 if successful,
219 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
220 *
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).
227 */
228 int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
229
230 /**
231 * \brief Safe conditional swap X <-> Y if swap is 1
232 *
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
236 *
237 * \return 0 if successful,
238 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
239 *
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).
246 */
247 int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
248
249 /**
250 * \brief Set value from integer
251 *
252 * \param X MPI to set
253 * \param z Value to use
254 *
255 * \return 0 if successful,
256 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
257 */
258 int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
259
260 /**
261 * \brief Get a specific bit from X
262 *
263 * \param X MPI to use
264 * \param pos Zero-based index of the bit in X
265 *
266 * \return Either a 0 or a 1
267 */
268 int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
269
270 /**
271 * \brief Set a bit of X to a specific value of 0 or 1
272 *
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
275 *
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)
279 *
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
283 */
284 int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
285
286 /**
287 * \brief Return the number of zero-bits before the least significant
288 * '1' bit
289 *
290 * Note: Thus also the zero-based index of the least significant '1' bit
291 *
292 * \param X MPI to use
293 */
294 size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
295
296 /**
297 * \brief Return the number of bits up to and including the most
298 * significant '1' bit'
299 *
300 * Note: Thus also the one-based index of the most significant '1' bit
301 *
302 * \param X MPI to use
303 */
304 size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
305
306 /**
307 * \brief Return the total size in bytes
308 *
309 * \param X MPI to use
310 */
311 size_t mbedtls_mpi_size( const mbedtls_mpi *X );
312
313 /**
314 * \brief Import from an ASCII string
315 *
316 * \param X Destination MPI
317 * \param radix Input numeric base
318 * \param s Null-terminated string buffer
319 *
320 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
321 */
322 int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
323
324 /**
325 * \brief Export into an ASCII string
326 *
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
332 *
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.
336 *
337 * \note Call this function with buflen = 0 to obtain the
338 * minimum required buffer size in *olen.
339 */
340 int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
341 char *buf, size_t buflen, size_t *olen );
342
343 #if defined(MBEDTLS_FS_IO)
344 /**
345 * \brief Read X from an opened file
346 *
347 * \param X Destination MPI
348 * \param radix Input numeric base
349 * \param fin Input file handle
350 *
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
354 */
355 int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
356
357 /**
358 * \brief Write X into an opened file, or stdout if fout is NULL
359 *
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)
364 *
365 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
366 *
367 * \note Set fout == NULL to print X on the console.
368 */
369 int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
370 #endif /* MBEDTLS_FS_IO */
371
372 /**
373 * \brief Import X from unsigned binary data, big endian
374 *
375 * \param X Destination MPI
376 * \param buf Input buffer
377 * \param buflen Input buffer size
378 *
379 * \return 0 if successful,
380 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
381 */
382 int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
383
384 /**
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.
388 *
389 * \param X Source MPI
390 * \param buf Output buffer
391 * \param buflen Output buffer size
392 *
393 * \return 0 if successful,
394 * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
395 */
396 int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
397
398 /**
399 * \brief Left-shift: X <<= count
400 *
401 * \param X MPI to shift
402 * \param count Amount to shift
403 *
404 * \return 0 if successful,
405 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
406 */
407 int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
408
409 /**
410 * \brief Right-shift: X >>= count
411 *
412 * \param X MPI to shift
413 * \param count Amount to shift
414 *
415 * \return 0 if successful,
416 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
417 */
418 int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
419
420 /**
421 * \brief Compare unsigned values
422 *
423 * \param X Left-hand MPI
424 * \param Y Right-hand MPI
425 *
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|
429 */
430 int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
431
432 /**
433 * \brief Compare signed values
434 *
435 * \param X Left-hand MPI
436 * \param Y Right-hand MPI
437 *
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
441 */
442 int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
443
444 /**
445 * \brief Compare signed values
446 *
447 * \param X Left-hand MPI
448 * \param z The integer value to compare to
449 *
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
453 */
454 int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
455
456 /**
457 * \brief Unsigned addition: X = |A| + |B|
458 *
459 * \param X Destination MPI
460 * \param A Left-hand MPI
461 * \param B Right-hand MPI
462 *
463 * \return 0 if successful,
464 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
465 */
466 int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
467
468 /**
469 * \brief Unsigned subtraction: X = |A| - |B|
470 *
471 * \param X Destination MPI
472 * \param A Left-hand MPI
473 * \param B Right-hand MPI
474 *
475 * \return 0 if successful,
476 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
477 */
478 int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
479
480 /**
481 * \brief Signed addition: X = A + B
482 *
483 * \param X Destination MPI
484 * \param A Left-hand MPI
485 * \param B Right-hand MPI
486 *
487 * \return 0 if successful,
488 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
489 */
490 int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
491
492 /**
493 * \brief Signed subtraction: X = A - B
494 *
495 * \param X Destination MPI
496 * \param A Left-hand MPI
497 * \param B Right-hand MPI
498 *
499 * \return 0 if successful,
500 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
501 */
502 int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
503
504 /**
505 * \brief Signed addition: X = A + b
506 *
507 * \param X Destination MPI
508 * \param A Left-hand MPI
509 * \param b The integer value to add
510 *
511 * \return 0 if successful,
512 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
513 */
514 int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
515
516 /**
517 * \brief Signed subtraction: X = A - b
518 *
519 * \param X Destination MPI
520 * \param A Left-hand MPI
521 * \param b The integer value to subtract
522 *
523 * \return 0 if successful,
524 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
525 */
526 int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
527
528 /**
529 * \brief Baseline multiplication: X = A * B
530 *
531 * \param X Destination MPI
532 * \param A Left-hand MPI
533 * \param B Right-hand MPI
534 *
535 * \return 0 if successful,
536 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
537 */
538 int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
539
540 /**
541 * \brief Baseline multiplication: X = A * b
542 *
543 * \param X Destination MPI
544 * \param A Left-hand MPI
545 * \param b The unsigned integer value to multiply with
546 *
547 * \note b is unsigned
548 *
549 * \return 0 if successful,
550 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
551 */
552 int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
553
554 /**
555 * \brief Division by mbedtls_mpi: A = Q * B + R
556 *
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
561 *
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
565 *
566 * \note Either Q or R can be NULL.
567 */
568 int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
569
570 /**
571 * \brief Division by int: A = Q * b + R
572 *
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
577 *
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
581 *
582 * \note Either Q or R can be NULL.
583 */
584 int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b );
585
586 /**
587 * \brief Modulo: R = A mod B
588 *
589 * \param R Destination MPI for the rest value
590 * \param A Left-hand MPI
591 * \param B Right-hand MPI
592 *
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
597 */
598 int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
599
600 /**
601 * \brief Modulo: r = A mod b
602 *
603 * \param r Destination mbedtls_mpi_uint
604 * \param A Left-hand MPI
605 * \param b Integer to divide by
606 *
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
611 */
612 int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
613
614 /**
615 * \brief Sliding-window exponentiation: X = A^E mod N
616 *
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
622 *
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
626 * if E is negative
627 *
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.
631 */
632 int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR );
633
634 /**
635 * \brief Fill an MPI X with size bytes of random
636 *
637 * \param X Destination MPI
638 * \param size Size in bytes
639 * \param f_rng RNG function
640 * \param p_rng RNG parameter
641 *
642 * \return 0 if successful,
643 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
644 */
645 int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
646 int (*f_rng)(void *, unsigned char *, size_t),
647 void *p_rng );
648
649 /**
650 * \brief Greatest common divisor: G = gcd(A, B)
651 *
652 * \param G Destination MPI
653 * \param A Left-hand MPI
654 * \param B Right-hand MPI
655 *
656 * \return 0 if successful,
657 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
658 */
659 int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
660
661 /**
662 * \brief Modular inverse: X = A^-1 mod N
663 *
664 * \param X Destination MPI
665 * \param A Left-hand MPI
666 * \param N Right-hand MPI
667 *
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
672 */
673 int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
674
675 /**
676 * \brief Miller-Rabin primality test
677 *
678 * \param X MPI to check
679 * \param f_rng RNG function
680 * \param p_rng RNG parameter
681 *
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
685 */
686 int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
687 int (*f_rng)(void *, unsigned char *, size_t),
688 void *p_rng );
689
690 /**
691 * \brief Prime number generation
692 *
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
699 *
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
703 */
704 int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
705 int (*f_rng)(void *, unsigned char *, size_t),
706 void *p_rng );
707
708 /**
709 * \brief Checkup routine
710 *
711 * \return 0 if successful, or 1 if the test failed
712 */
713 int mbedtls_mpi_self_test( int verbose );
714
715 #ifdef __cplusplus
716 }
717 #endif
718
719 #endif /* bignum.h */