blob: 456a8042044701947dcfc34f00209bb6ed8eda25 [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 *
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100106 * 32 or 64-bit integer types can be forced regardless of the underlying
107 * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
108 * respectively and undefining MBEDTLS_HAVE_ASM.
109 *
Andres Amaya Garcia93db11a2017-07-20 12:11:19 +0100110 * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100111 * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100113#if !defined(MBEDTLS_HAVE_INT32)
114 #if defined(_MSC_VER) && defined(_M_AMD64)
115 /* Always choose 64-bit when using MSC */
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100116 #if !defined(MBEDTLS_HAVE_INT64)
117 #define MBEDTLS_HAVE_INT64
118 #endif /* !MBEDTLS_HAVE_INT64 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100119 typedef int64_t mbedtls_mpi_sint;
120 typedef uint64_t mbedtls_mpi_uint;
121 #elif defined(__GNUC__) && ( \
122 defined(__amd64__) || defined(__x86_64__) || \
123 defined(__ppc64__) || defined(__powerpc64__) || \
124 defined(__ia64__) || defined(__alpha__) || \
125 ( defined(__sparc__) && defined(__arch64__) ) || \
126 defined(__s390x__) || defined(__mips64) )
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100127 #if !defined(MBEDTLS_HAVE_INT64)
128 #define MBEDTLS_HAVE_INT64
129 #endif /* MBEDTLS_HAVE_INT64 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100130 typedef int64_t mbedtls_mpi_sint;
131 typedef uint64_t mbedtls_mpi_uint;
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100132 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
133 /* mbedtls_t_udbl defined as 128-bit unsigned int */
134 typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
135 #define MBEDTLS_HAVE_UDBL
136 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100137 #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100138 /*
139 * __ARMCC_VERSION is defined for both armcc and armclang and
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100140 * __aarch64__ is only defined by armclang when compiling 64-bit code
141 */
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100142 #if !defined(MBEDTLS_HAVE_INT64)
143 #define MBEDTLS_HAVE_INT64
144 #endif /* !MBEDTLS_HAVE_INT64 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100145 typedef int64_t mbedtls_mpi_sint;
146 typedef uint64_t mbedtls_mpi_uint;
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100147 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
148 /* mbedtls_t_udbl defined as 128-bit unsigned int */
149 typedef __uint128_t mbedtls_t_udbl;
150 #define MBEDTLS_HAVE_UDBL
151 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
152 #elif defined(MBEDTLS_HAVE_INT64)
153 /* Force 64-bit integers with unknown compiler */
154 typedef int64_t mbedtls_mpi_sint;
155 typedef uint64_t mbedtls_mpi_uint;
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100156 #endif
157#endif /* !MBEDTLS_HAVE_INT32 */
158
159#if !defined(MBEDTLS_HAVE_INT64)
160 /* Default to 32-bit compilation */
161 #if !defined(MBEDTLS_HAVE_INT32)
162 #define MBEDTLS_HAVE_INT32
163 #endif /* !MBEDTLS_HAVE_INT32 */
164 typedef int32_t mbedtls_mpi_sint;
165 typedef uint32_t mbedtls_mpi_uint;
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100166 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
167 typedef uint64_t mbedtls_t_udbl;
Andres Amaya Garciadf1486a2017-07-20 17:33:09 +0100168 #define MBEDTLS_HAVE_UDBL
Andres Amaya Garciad7fce002017-07-20 11:49:32 +0100169 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100170#endif /* !MBEDTLS_HAVE_INT64 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000171
Paul Bakker407a0da2013-06-27 14:29:21 +0200172#ifdef __cplusplus
173extern "C" {
174#endif
175
Paul Bakker5121ce52009-01-03 21:22:43 +0000176/**
177 * \brief MPI structure
178 */
179typedef struct
180{
181 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000182 size_t n; /*!< total # of limbs */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_mpi_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000184}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185mbedtls_mpi;
Paul Bakker5121ce52009-01-03 21:22:43 +0000186
Paul Bakker5121ce52009-01-03 21:22:43 +0000187/**
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200188 * \brief Initialize one MPI (make internal references valid)
189 * This just makes it ready to be set or freed,
190 * but does not define a value for the MPI.
Paul Bakker6c591fa2011-05-05 11:49:20 +0000191 *
192 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194void mbedtls_mpi_init( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
196/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000197 * \brief Unallocate one MPI
198 *
199 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000200 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201void mbedtls_mpi_free( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000202
203/**
204 * \brief Enlarge to the specified number of limbs
205 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000206 * \param X MPI to grow
207 * \param nblimbs The target number of limbs
208 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200210 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
214/**
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100215 * \brief Resize down, keeping at least the specified number of limbs
216 *
217 * \param X MPI to shrink
218 * \param nblimbs The minimum number of limbs to keep
219 *
220 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200221 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100224
225/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 * \brief Copy the contents of Y into X
227 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000228 * \param X Destination MPI
229 * \param Y Source MPI
230 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200232 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000235
236/**
237 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000238 *
239 * \param X First MPI value
240 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000243
244/**
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100245 * \brief Safe conditional assignement X = Y if assign is 1
246 *
247 * \param X MPI to conditionally assign to
248 * \param Y Value to be assigned
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100249 * \param assign 1: perform the assignment, 0: keep X's original value
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100250 *
251 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200252 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100253 *
254 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 * if( assign ) mbedtls_mpi_copy( X, Y );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100256 * except that it avoids leaking any information about whether
257 * the assignment was done or not (the above code may leak
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100258 * information through branch prediction and/or memory access
259 * patterns analysis).
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100262
263/**
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100264 * \brief Safe conditional swap X <-> Y if swap is 1
265 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 * \param X First mbedtls_mpi value
267 * \param Y Second mbedtls_mpi value
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100268 * \param assign 1: perform the swap, 0: keep X and Y's original values
269 *
270 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200271 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100272 *
273 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 * if( assign ) mbedtls_mpi_swap( X, Y );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100275 * except that it avoids leaking any information about whether
276 * the assignment was done or not (the above code may leak
277 * information through branch prediction and/or memory access
278 * patterns analysis).
279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100281
282/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 * \brief Set value from integer
284 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000285 * \param X MPI to set
286 * \param z Value to use
287 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200289 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000292
Paul Bakker9a736322012-11-14 12:39:52 +0000293/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000294 * \brief Get a specific bit from X
295 *
296 * \param X MPI to use
297 * \param pos Zero-based index of the bit in X
298 *
299 * \return Either a 0 or a 1
300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000302
Paul Bakker9a736322012-11-14 12:39:52 +0000303/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000304 * \brief Set a bit of X to a specific value of 0 or 1
305 *
306 * \note Will grow X if necessary to set a bit to 1 in a not yet
307 * existing limb. Will not grow if bit should be set to 0
308 *
309 * \param X MPI to use
310 * \param pos Zero-based index of the bit in X
311 * \param val The value to set the bit to (0 or 1)
312 *
313 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200314 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
Paul Bakker2f5947e2011-05-18 15:47:11 +0000316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000318
Paul Bakker5121ce52009-01-03 21:22:43 +0000319/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000320 * \brief Return the number of zero-bits before the least significant
321 * '1' bit
322 *
323 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000324 *
325 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000328
329/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000330 * \brief Return the number of bits up to and including the most
331 * significant '1' bit'
332 *
333 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000334 *
335 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200337size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000338
339/**
340 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000341 *
342 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344size_t mbedtls_mpi_size( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000345
346/**
347 * \brief Import from an ASCII string
348 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000349 * \param X Destination MPI
350 * \param radix Input numeric base
351 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000356
357/**
358 * \brief Export into an ASCII string
359 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000360 * \param X Source MPI
361 * \param radix Output numeric base
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100362 * \param buf Buffer to write the string to
363 * \param buflen Length of buf
364 * \param olen Length of the string written, including final NUL byte
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code.
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100367 * *olen is always updated to reflect the amount
Paul Bakkerff60ee62010-03-16 21:09:09 +0000368 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000369 *
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100370 * \note Call this function with buflen = 0 to obtain the
371 * minimum required buffer size in *olen.
Paul Bakker5121ce52009-01-03 21:22:43 +0000372 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100373int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
374 char *buf, size_t buflen, size_t *olen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000377/**
Hanno Beckerb2034b72017-04-26 11:46:46 +0100378 * \brief Read MPI from a line in an opened file
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000380 * \param X Destination MPI
381 * \param radix Input numeric base
382 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
Paul Bakkercb37aa52011-11-30 16:00:20 +0000385 * the file read buffer is too small or a
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 * MBEDTLS_ERR_MPI_XXX error code
Hanno Beckerb2034b72017-04-26 11:46:46 +0100387 *
388 * \note On success, this function advances the file stream
389 * to the end of the current line or to EOF.
390 *
391 * The function returns 0 on an empty line.
392 *
393 * Leading whitespaces are ignored, as is a
394 * '0x' prefix for radix 16.
395 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
Paul Bakker5121ce52009-01-03 21:22:43 +0000398
399/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000400 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000401 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000402 * \param p Prefix, can be NULL
403 * \param X Source MPI
404 * \param radix Output numeric base
405 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000406 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 *
409 * \note Set fout == NULL to print X on the console.
410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
412#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000413
414/**
415 * \brief Import X from unsigned binary data, big endian
416 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000417 * \param X Destination MPI
418 * \param buf Input buffer
419 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 *
421 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200422 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426/**
Manuel Pégourié-Gonnard3926a2c2014-06-25 12:57:47 +0200427 * \brief Export X into unsigned binary data, big endian.
428 * Always fills the whole buffer, which will start with zeros
429 * if the number is smaller.
Paul Bakker5121ce52009-01-03 21:22:43 +0000430 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000431 * \param X Source MPI
432 * \param buf Output buffer
433 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000434 *
435 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000437 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000439
440/**
441 * \brief Left-shift: X <<= count
442 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000443 * \param X MPI to shift
444 * \param count Amount to shift
445 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200447 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000450
451/**
452 * \brief Right-shift: X >>= count
453 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000454 * \param X MPI to shift
455 * \param count Amount to shift
456 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200458 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000461
462/**
463 * \brief Compare unsigned values
464 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000465 * \param X Left-hand MPI
466 * \param Y Right-hand MPI
467 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000468 * \return 1 if |X| is greater than |Y|,
469 * -1 if |X| is lesser than |Y| or
470 * 0 if |X| is equal to |Y|
471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
474/**
475 * \brief Compare signed values
476 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000477 * \param X Left-hand MPI
478 * \param Y Right-hand MPI
479 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000480 * \return 1 if X is greater than Y,
481 * -1 if X is lesser than Y or
482 * 0 if X is equal to Y
483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000485
486/**
487 * \brief Compare signed values
488 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000489 * \param X Left-hand MPI
490 * \param z The integer value to compare to
491 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 * \return 1 if X is greater than z,
493 * -1 if X is lesser than z or
494 * 0 if X is equal to z
495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000497
498/**
499 * \brief Unsigned addition: X = |A| + |B|
500 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000501 * \param X Destination MPI
502 * \param A Left-hand MPI
503 * \param B Right-hand MPI
504 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000505 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200506 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100511 * \brief Unsigned subtraction: X = |A| - |B|
Paul Bakker5121ce52009-01-03 21:22:43 +0000512 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000513 * \param X Destination MPI
514 * \param A Left-hand MPI
515 * \param B Right-hand MPI
516 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000517 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
522/**
523 * \brief Signed addition: X = A + B
524 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000525 * \param X Destination MPI
526 * \param A Left-hand MPI
527 * \param B Right-hand MPI
528 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000529 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200530 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
534/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100535 * \brief Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +0000536 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000537 * \param X Destination MPI
538 * \param A Left-hand MPI
539 * \param B Right-hand MPI
540 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000541 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200542 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000543 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000545
546/**
547 * \brief Signed addition: X = A + b
548 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000549 * \param X Destination MPI
550 * \param A Left-hand MPI
551 * \param b The integer value to add
552 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200554 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100559 * \brief Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +0000560 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000561 * \param X Destination MPI
562 * \param A Left-hand MPI
563 * \param b The integer value to subtract
564 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000565 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200566 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
570/**
571 * \brief Baseline multiplication: X = A * B
572 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000573 * \param X Destination MPI
574 * \param A Left-hand MPI
575 * \param B Right-hand MPI
576 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200578 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000581
582/**
583 * \brief Baseline multiplication: X = A * b
584 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000585 * \param X Destination MPI
586 * \param A Left-hand MPI
Manuel Pégourié-Gonnard35f1d7f2015-03-19 12:42:40 +0000587 * \param b The unsigned integer value to multiply with
588 *
589 * \note b is unsigned
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000590 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200592 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000593 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000595
596/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 * \brief Division by mbedtls_mpi: A = Q * B + R
Paul Bakker5121ce52009-01-03 21:22:43 +0000598 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000599 * \param Q Destination MPI for the quotient
600 * \param R Destination MPI for the rest value
601 * \param A Left-hand MPI
602 * \param B Right-hand MPI
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
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 *
608 * \note Either Q or R can be NULL.
609 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610int 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 +0000611
612/**
613 * \brief Division by int: A = Q * b + R
614 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000615 * \param Q Destination MPI for the quotient
616 * \param R Destination MPI for the rest value
617 * \param A Left-hand MPI
618 * \param b Integer to divide by
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_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 *
624 * \note Either Q or R can be NULL.
625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626int 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 +0000627
628/**
629 * \brief Modulo: R = A mod B
630 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000631 * \param R Destination MPI for the rest value
632 * \param A Left-hand MPI
633 * \param B Right-hand MPI
634 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200636 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0,
638 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000641
642/**
643 * \brief Modulo: r = A mod b
644 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 * \param r Destination mbedtls_mpi_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000646 * \param A Left-hand MPI
647 * \param b Integer to divide by
648 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000649 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200650 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0,
652 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000653 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000655
656/**
657 * \brief Sliding-window exponentiation: X = A^E mod N
658 *
Paul Bakker9af723c2014-05-01 13:03:14 +0200659 * \param X Destination MPI
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000660 * \param A Left-hand MPI
661 * \param E Exponent MPI
662 * \param N Modular MPI
663 * \param _RR Speed-up MPI used for recalculations
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 even or
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200668 * if E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 *
670 * \note _RR is used to avoid re-computing R*R mod N across
671 * multiple calls, which speeds up things a bit. It can
672 * be set to NULL if the extra performance is unneeded.
673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674int 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 +0000675
676/**
Paul Bakker287781a2011-03-26 13:18:49 +0000677 * \brief Fill an MPI X with size bytes of random
678 *
679 * \param X Destination MPI
680 * \param size Size in bytes
681 * \param f_rng RNG function
682 * \param p_rng RNG parameter
683 *
684 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200685 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000688 int (*f_rng)(void *, unsigned char *, size_t),
689 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000690
691/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000692 * \brief Greatest common divisor: G = gcd(A, B)
693 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000694 * \param G Destination MPI
695 * \param A Left-hand MPI
696 * \param B Right-hand MPI
697 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000698 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200699 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000702
703/**
704 * \brief Modular inverse: X = A^-1 mod N
705 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000706 * \param X Destination MPI
707 * \param A Left-hand MPI
708 * \param N Right-hand MPI
709 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000710 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200711 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Hanno Becker4bcb4912017-04-18 15:49:39 +0100712 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is <= 1,
713 MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000716
717/**
718 * \brief Miller-Rabin primality test
719 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000720 * \param X MPI to check
721 * \param f_rng RNG function
722 * \param p_rng RNG parameter
723 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000724 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200725 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000729 int (*f_rng)(void *, unsigned char *, size_t),
730 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000731
732/**
733 * \brief Prime number generation
734 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000735 * \param X Destination MPI
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200736 * \param nbits Required size of X in bits
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000738 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000739 * \param f_rng RNG function
740 * \param p_rng RNG parameter
741 *
742 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200743 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000747 int (*f_rng)(void *, unsigned char *, size_t),
748 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000749
750/**
751 * \brief Checkup routine
752 *
753 * \return 0 if successful, or 1 if the test failed
754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755int mbedtls_mpi_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000756
757#ifdef __cplusplus
758}
759#endif
760
761#endif /* bignum.h */