blob: 146224a2b7acec5994c088e655f14d0582ff9e54 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file bignum.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief Multi-precision integer library
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
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
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
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.
Paul Bakkerb96f1542010-07-18 20:36:00 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_BIGNUM_H
24#define MBEDTLS_BIGNUM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkercf0360a2012-01-20 10:08:14 +000027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakkercf0360a2012-01-20 10:08:14 +000031
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020033#include <stdint.h>
Rich Evans00ab4702015-02-06 13:43:58 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnarde94e6e52015-01-19 15:01:53 +000036#include <stdio.h>
37#endif
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#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. */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020046#define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 /**< Memory allocation failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000049
50/*
Paul Bakkerf9688572011-05-05 10:00:45 +000051 * Maximum size MPIs are allowed to grow to in number of limbs.
52 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#define MBEDTLS_MPI_MAX_LIMBS 10000
Paul Bakkerf9688572011-05-05 10:00:45 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if !defined(MBEDTLS_MPI_WINDOW_SIZE)
Paul Bakkerf9688572011-05-05 10:00:45 +000056/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000057 * Maximum window size used for modular exponentiation. Default: 6
58 * Minimum value: 1. Maximum value: 6.
59 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
Paul Bakkerb6d5f082011-11-25 11:52:11 +000061 * for the sliding window calculation. (So 64 by default)
62 *
63 * Reduction in size, reduces speed.
64 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
66#endif /* !MBEDTLS_MPI_WINDOW_SIZE */
Paul Bakkerb6d5f082011-11-25 11:52:11 +000067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if !defined(MBEDTLS_MPI_MAX_SIZE)
Paul Bakkerb6d5f082011-11-25 11:52:11 +000069/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000070 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakkerf626e1d2013-01-21 12:10:00 +010071 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +000072 *
73 * Note: Calculations can results temporarily in larger MPIs. So the number
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
Paul Bakkerfe3256e2011-11-25 12:11:43 +000075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
77#endif /* !MBEDTLS_MPI_MAX_SIZE */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
Paul Bakkerfe3256e2011-11-25 12:11:43 +000080
81/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 * When reading from files with mbedtls_mpi_read_file() and writing to files with
83 * mbedtls_mpi_write_file() the buffer should have space
Paul Bakkercb37aa52011-11-30 16:00:20 +000084 * for a (short) label, the MPI (in the provided radix), the newline
85 * characters and the '\0'.
86 *
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).
Paul Bakkerf9183102012-09-27 20:42:35 +000089 * Autosized at compile time for at least a 10 char label, a minimum radix
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
Paul Bakkerf9183102012-09-27 20:42:35 +000091 *
92 * This used to be statically sized to 1250 for a maximum of 4096 bit
93 * numbers (1234 decimal chars).
94 *
95 * Calculate using the formula:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
Paul Bakkerf9183102012-09-27 20:42:35 +000097 * LabelSize + 6
Paul Bakkercb37aa52011-11-30 16:00:20 +000098 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#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 )
Paul Bakkercb37aa52011-11-30 16:00:20 +0000102
103/*
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200104 * Define the base integer type, architecture-wise.
105 *
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
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 */
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200109#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;
Paul Bakker5121ce52009-01-03 21:22:43 +0000114#else
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200115 #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
Paul Bakker62261d62012-10-02 12:19:31 +0000127 #else
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200128 #define MBEDTLS_HAVE_INT32
129 typedef int32_t mbedtls_mpi_sint;
130 typedef uint32_t mbedtls_mpi_uint;
Manuel Pégourié-Gonnard975d5fa2015-04-09 17:13:45 +0200131 typedef uint64_t mbedtls_t_udbl;
132 #define MBEDTLS_HAVE_UDBL
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200133 #endif /* !MBEDTLS_HAVE_INT32 && __GNUC__ && 64-bit platform */
134#endif /* !MBEDTLS_HAVE_INT32 && _MSC_VER && _M_AMD64 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
Paul Bakker407a0da2013-06-27 14:29:21 +0200136#ifdef __cplusplus
137extern "C" {
138#endif
139
Paul Bakker5121ce52009-01-03 21:22:43 +0000140/**
141 * \brief MPI structure
142 */
143typedef struct
144{
145 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000146 size_t n; /*!< total # of limbs */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_mpi_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000148}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149mbedtls_mpi;
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
Paul Bakker5121ce52009-01-03 21:22:43 +0000151/**
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200152 * \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.
Paul Bakker6c591fa2011-05-05 11:49:20 +0000155 *
156 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158void mbedtls_mpi_init( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
160/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000161 * \brief Unallocate one MPI
162 *
163 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165void mbedtls_mpi_free( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
167/**
168 * \brief Enlarge to the specified number of limbs
169 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000170 * \param X MPI to grow
171 * \param nblimbs The target number of limbs
172 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200174 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000177
178/**
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100179 * \brief Resize down, keeping at least the specified number of limbs
180 *
181 * \param X MPI to shrink
182 * \param nblimbs The minimum number of limbs to keep
183 *
184 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200185 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100188
189/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 * \brief Copy the contents of Y into X
191 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000192 * \param X Destination MPI
193 * \param Y Source MPI
194 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000195 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200196 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000199
200/**
201 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000202 *
203 * \param X First MPI value
204 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208/**
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100209 * \brief Safe conditional assignement X = Y if assign is 1
210 *
211 * \param X MPI to conditionally assign to
212 * \param Y Value to be assigned
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100213 * \param assign 1: perform the assignment, 0: keep X's original value
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100214 *
215 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200216 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100217 *
218 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 * if( assign ) mbedtls_mpi_copy( X, Y );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100220 * except that it avoids leaking any information about whether
221 * the assignment was done or not (the above code may leak
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100222 * information through branch prediction and/or memory access
223 * patterns analysis).
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100226
227/**
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100228 * \brief Safe conditional swap X <-> Y if swap is 1
229 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 * \param X First mbedtls_mpi value
231 * \param Y Second mbedtls_mpi value
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100232 * \param assign 1: perform the swap, 0: keep X and Y's original values
233 *
234 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200235 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100236 *
237 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 * if( assign ) mbedtls_mpi_swap( X, Y );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100239 * 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).
243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100245
246/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 * \brief Set value from integer
248 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000249 * \param X MPI to set
250 * \param z Value to use
251 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200253 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
Paul Bakker9a736322012-11-14 12:39:52 +0000257/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000258 * \brief Get a specific bit from X
259 *
260 * \param X MPI to use
261 * \param pos Zero-based index of the bit in X
262 *
263 * \return Either a 0 or a 1
264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000266
Paul Bakker9a736322012-11-14 12:39:52 +0000267/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000268 * \brief Set a bit of X to a specific value of 0 or 1
269 *
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
272 *
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)
276 *
277 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200278 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
Paul Bakker2f5947e2011-05-18 15:47:11 +0000280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000282
Paul Bakker5121ce52009-01-03 21:22:43 +0000283/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000284 * \brief Return the number of zero-bits before the least significant
285 * '1' bit
286 *
287 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000288 *
289 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
293/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000294 * \brief Return the number of bits up to and including the most
295 * significant '1' bit'
296 *
297 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000298 *
299 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200301size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000302
303/**
304 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000305 *
306 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308size_t mbedtls_mpi_size( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
310/**
311 * \brief Import from an ASCII string
312 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000313 * \param X Destination MPI
314 * \param radix Input numeric base
315 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000316 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000320
321/**
322 * \brief Export into an ASCII string
323 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000324 * \param X Source MPI
325 * \param radix Output numeric base
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100326 * \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
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code.
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100331 * *olen is always updated to reflect the amount
Paul Bakkerff60ee62010-03-16 21:09:09 +0000332 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 *
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100334 * \note Call this function with buflen = 0 to obtain the
335 * minimum required buffer size in *olen.
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100337int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
338 char *buf, size_t buflen, size_t *olen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000341/**
342 * \brief Read X from an opened file
343 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000344 * \param X Destination MPI
345 * \param radix Input numeric base
346 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000347 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
Paul Bakkercb37aa52011-11-30 16:00:20 +0000349 * the file read buffer is too small or a
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 * MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
Paul Bakker5121ce52009-01-03 21:22:43 +0000353
354/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000355 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000356 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000357 * \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)
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 *
364 * \note Set fout == NULL to print X on the console.
365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
367#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000368
369/**
370 * \brief Import X from unsigned binary data, big endian
371 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000372 * \param X Destination MPI
373 * \param buf Input buffer
374 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000375 *
376 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200377 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000378 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000380
381/**
Manuel Pégourié-Gonnard3926a2c2014-06-25 12:57:47 +0200382 * \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.
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000386 * \param X Source MPI
387 * \param buf Output buffer
388 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000389 *
390 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000394
395/**
396 * \brief Left-shift: X <<= count
397 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000398 * \param X MPI to shift
399 * \param count Amount to shift
400 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000401 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200402 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
406/**
407 * \brief Right-shift: X >>= count
408 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000409 * \param X MPI to shift
410 * \param count Amount to shift
411 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000412 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200413 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000416
417/**
418 * \brief Compare unsigned values
419 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000420 * \param X Left-hand MPI
421 * \param Y Right-hand MPI
422 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000423 * \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|
426 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000428
429/**
430 * \brief Compare signed values
431 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000432 * \param X Left-hand MPI
433 * \param Y Right-hand MPI
434 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 * \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
438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000440
441/**
442 * \brief Compare signed values
443 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000444 * \param X Left-hand MPI
445 * \param z The integer value to compare to
446 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 * \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
450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
453/**
454 * \brief Unsigned addition: X = |A| + |B|
455 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000456 * \param X Destination MPI
457 * \param A Left-hand MPI
458 * \param B Right-hand MPI
459 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200461 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
465/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100466 * \brief Unsigned subtraction: X = |A| - |B|
Paul Bakker5121ce52009-01-03 21:22:43 +0000467 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000468 * \param X Destination MPI
469 * \param A Left-hand MPI
470 * \param B Right-hand MPI
471 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
477/**
478 * \brief Signed addition: X = A + B
479 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000480 * \param X Destination MPI
481 * \param A Left-hand MPI
482 * \param B Right-hand MPI
483 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000484 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200485 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000486 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000488
489/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100490 * \brief Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +0000491 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000492 * \param X Destination MPI
493 * \param A Left-hand MPI
494 * \param B Right-hand MPI
495 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200497 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000500
501/**
502 * \brief Signed addition: X = A + b
503 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000504 * \param X Destination MPI
505 * \param A Left-hand MPI
506 * \param b The integer value to add
507 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200509 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000512
513/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100514 * \brief Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +0000515 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000516 * \param X Destination MPI
517 * \param A Left-hand MPI
518 * \param b The integer value to subtract
519 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200521 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525/**
526 * \brief Baseline multiplication: X = A * B
527 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000528 * \param X Destination MPI
529 * \param A Left-hand MPI
530 * \param B Right-hand MPI
531 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200533 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000536
537/**
538 * \brief Baseline multiplication: X = A * b
539 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000540 * \param X Destination MPI
541 * \param A Left-hand MPI
Manuel Pégourié-Gonnard35f1d7f2015-03-19 12:42:40 +0000542 * \param b The unsigned integer value to multiply with
543 *
544 * \note b is unsigned
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000545 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200547 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000550
551/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 * \brief Division by mbedtls_mpi: A = Q * B + R
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000554 * \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
558 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000559 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200560 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000562 *
563 * \note Either Q or R can be NULL.
564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000566
567/**
568 * \brief Division by int: A = Q * b + R
569 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000570 * \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
574 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000575 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200576 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 *
579 * \note Either Q or R can be NULL.
580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000582
583/**
584 * \brief Modulo: R = A mod B
585 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000586 * \param R Destination MPI for the rest value
587 * \param A Left-hand MPI
588 * \param B Right-hand MPI
589 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000590 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200591 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0,
593 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
597/**
598 * \brief Modulo: r = A mod b
599 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 * \param r Destination mbedtls_mpi_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000601 * \param A Left-hand MPI
602 * \param b Integer to divide by
603 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200605 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0,
607 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000610
611/**
612 * \brief Sliding-window exponentiation: X = A^E mod N
613 *
Paul Bakker9af723c2014-05-01 13:03:14 +0200614 * \param X Destination MPI
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000615 * \param A Left-hand MPI
616 * \param E Exponent MPI
617 * \param N Modular MPI
618 * \param _RR Speed-up MPI used for recalculations
619 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000620 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200621 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200623 * if E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000624 *
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.
628 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR );
Paul Bakker5121ce52009-01-03 21:22:43 +0000630
631/**
Paul Bakker287781a2011-03-26 13:18:49 +0000632 * \brief Fill an MPI X with size bytes of random
633 *
634 * \param X Destination MPI
635 * \param size Size in bytes
636 * \param f_rng RNG function
637 * \param p_rng RNG parameter
638 *
639 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200640 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000643 int (*f_rng)(void *, unsigned char *, size_t),
644 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000645
646/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 * \brief Greatest common divisor: G = gcd(A, B)
648 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000649 * \param G Destination MPI
650 * \param A Left-hand MPI
651 * \param B Right-hand MPI
652 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200654 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000657
658/**
659 * \brief Modular inverse: X = A^-1 mod N
660 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000661 * \param X Destination MPI
662 * \param A Left-hand MPI
663 * \param N Right-hand MPI
664 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200666 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 * 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
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672/**
673 * \brief Miller-Rabin primality test
674 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000675 * \param X MPI to check
676 * \param f_rng RNG function
677 * \param p_rng RNG parameter
678 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200680 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000684 int (*f_rng)(void *, unsigned char *, size_t),
685 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000686
687/**
688 * \brief Prime number generation
689 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000690 * \param X Destination MPI
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200691 * \param nbits Required size of X in bits
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000693 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000694 * \param f_rng RNG function
695 * \param p_rng RNG parameter
696 *
697 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200698 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000702 int (*f_rng)(void *, unsigned char *, size_t),
703 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000704
705/**
706 * \brief Checkup routine
707 *
708 * \return 0 if successful, or 1 if the test failed
709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710int mbedtls_mpi_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000711
712#ifdef __cplusplus
713}
714#endif
715
716#endif /* bignum.h */