blob: c8d94c920a97af385f42a233e8c460733c1e8820 [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 Garcia6ee7dad2017-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 Garciabebc5f62017-07-20 12:11:19 +0100110 * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
Andres Amaya Garcia6ee7dad2017-07-20 11:49:32 +0100111 * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
112 *
Andres Amaya Garciabebc5f62017-07-20 12:11:19 +0100113 * The double-width integer types can be configured by defining
Andres Amaya Garcia6ee7dad2017-07-20 11:49:32 +0100114 * MBEDTLS_TYPE_UDBL when the type cannot be automatically deduced by the
115 * library (e.g. the compiler is unknown). The definition of MBEDTLS_TYPE_UDBL
116 * must be a complete statement of the form:
117 * typedef <UDBL_TYPE> mbedtls_t_udbl <OTHER_DIRECTIVES>
118 * for example:
119 * #define MBEDTLS_TYPE_UDBL \
120 * typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)))
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 */
Andres Amaya Garcia05d95352017-05-04 11:05:55 +0100122#if !defined(MBEDTLS_HAVE_INT32)
123 #if defined(_MSC_VER) && defined(_M_AMD64)
124 /* Always choose 64-bit when using MSC */
Andres Amaya Garcia6ee7dad2017-07-20 11:49:32 +0100125 #if !defined(MBEDTLS_HAVE_INT64)
126 #define MBEDTLS_HAVE_INT64
127 #endif /* !MBEDTLS_HAVE_INT64 */
Andres Amaya Garcia05d95352017-05-04 11:05:55 +0100128 typedef int64_t mbedtls_mpi_sint;
129 typedef uint64_t mbedtls_mpi_uint;
130 #elif defined(__GNUC__) && ( \
131 defined(__amd64__) || defined(__x86_64__) || \
132 defined(__ppc64__) || defined(__powerpc64__) || \
133 defined(__ia64__) || defined(__alpha__) || \
134 ( defined(__sparc__) && defined(__arch64__) ) || \
135 defined(__s390x__) || defined(__mips64) )
Andres Amaya Garcia6ee7dad2017-07-20 11:49:32 +0100136 #if !defined(MBEDTLS_HAVE_INT64)
137 #define MBEDTLS_HAVE_INT64
138 #endif /* MBEDTLS_HAVE_INT64 */
Andres Amaya Garcia05d95352017-05-04 11:05:55 +0100139 typedef int64_t mbedtls_mpi_sint;
140 typedef uint64_t mbedtls_mpi_uint;
Andres Amaya Garcia6ee7dad2017-07-20 11:49:32 +0100141 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
142 /* mbedtls_t_udbl defined as 128-bit unsigned int */
143 typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
144 #define MBEDTLS_HAVE_UDBL
145 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
Andres Amaya Garcia05d95352017-05-04 11:05:55 +0100146 #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
Andres Amaya Garcia6ee7dad2017-07-20 11:49:32 +0100147 /*
148 * __ARMCC_VERSION is defined for both armcc and armclang and
Andres Amaya Garcia05d95352017-05-04 11:05:55 +0100149 * __aarch64__ is only defined by armclang when compiling 64-bit code
150 */
Andres Amaya Garcia6ee7dad2017-07-20 11:49:32 +0100151 #if !defined(MBEDTLS_HAVE_INT64)
152 #define MBEDTLS_HAVE_INT64
153 #endif /* !MBEDTLS_HAVE_INT64 */
Andres Amaya Garcia05d95352017-05-04 11:05:55 +0100154 typedef int64_t mbedtls_mpi_sint;
155 typedef uint64_t mbedtls_mpi_uint;
Andres Amaya Garcia6ee7dad2017-07-20 11:49:32 +0100156 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
157 /* mbedtls_t_udbl defined as 128-bit unsigned int */
158 typedef __uint128_t mbedtls_t_udbl;
159 #define MBEDTLS_HAVE_UDBL
160 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
161 #elif defined(MBEDTLS_HAVE_INT64)
162 /* Force 64-bit integers with unknown compiler */
163 typedef int64_t mbedtls_mpi_sint;
164 typedef uint64_t mbedtls_mpi_uint;
165 #if !defined(MBEDTLS_NO_UDBL_DIVISION) && defined(MBEDTLS_TYPE_UDBL)
166 MBEDTLS_TYPE_UDBL;
167 #define MBEDTLS_HAVE_UDBL
168 #endif /* !MBEDTLS_NO_UDBL_DIVISION && MBEDTLS_TYPE_UDBL */
Andres Amaya Garcia05d95352017-05-04 11:05:55 +0100169 #endif
170#endif /* !MBEDTLS_HAVE_INT32 */
171
172#if !defined(MBEDTLS_HAVE_INT64)
173 /* Default to 32-bit compilation */
174 #if !defined(MBEDTLS_HAVE_INT32)
175 #define MBEDTLS_HAVE_INT32
176 #endif /* !MBEDTLS_HAVE_INT32 */
177 typedef int32_t mbedtls_mpi_sint;
178 typedef uint32_t mbedtls_mpi_uint;
Andres Amaya Garcia6ee7dad2017-07-20 11:49:32 +0100179 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
180 typedef uint64_t mbedtls_t_udbl;
181 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
Andres Amaya Garcia05d95352017-05-04 11:05:55 +0100182#endif /* !MBEDTLS_HAVE_INT64 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
Paul Bakker407a0da2013-06-27 14:29:21 +0200184#ifdef __cplusplus
185extern "C" {
186#endif
187
Paul Bakker5121ce52009-01-03 21:22:43 +0000188/**
189 * \brief MPI structure
190 */
191typedef struct
192{
193 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000194 size_t n; /*!< total # of limbs */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_mpi_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000196}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197mbedtls_mpi;
Paul Bakker5121ce52009-01-03 21:22:43 +0000198
Paul Bakker5121ce52009-01-03 21:22:43 +0000199/**
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200200 * \brief Initialize one MPI (make internal references valid)
201 * This just makes it ready to be set or freed,
202 * but does not define a value for the MPI.
Paul Bakker6c591fa2011-05-05 11:49:20 +0000203 *
204 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206void mbedtls_mpi_init( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
208/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000209 * \brief Unallocate one MPI
210 *
211 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213void mbedtls_mpi_free( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
215/**
216 * \brief Enlarge to the specified number of limbs
217 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000218 * \param X MPI to grow
219 * \param nblimbs The target number of limbs
220 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200222 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225
226/**
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100227 * \brief Resize down, keeping at least the specified number of limbs
228 *
229 * \param X MPI to shrink
230 * \param nblimbs The minimum number of limbs to keep
231 *
232 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200233 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100236
237/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 * \brief Copy the contents of Y into X
239 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000240 * \param X Destination MPI
241 * \param Y Source MPI
242 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200244 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
248/**
249 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000250 *
251 * \param X First MPI value
252 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
256/**
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100257 * \brief Safe conditional assignement X = Y if assign is 1
258 *
259 * \param X MPI to conditionally assign to
260 * \param Y Value to be assigned
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100261 * \param assign 1: perform the assignment, 0: keep X's original value
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100262 *
263 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200264 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100265 *
266 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 * if( assign ) mbedtls_mpi_copy( X, Y );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100268 * except that it avoids leaking any information about whether
269 * the assignment was done or not (the above code may leak
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100270 * information through branch prediction and/or memory access
271 * patterns analysis).
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100274
275/**
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100276 * \brief Safe conditional swap X <-> Y if swap is 1
277 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 * \param X First mbedtls_mpi value
279 * \param Y Second mbedtls_mpi value
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100280 * \param assign 1: perform the swap, 0: keep X and Y's original values
281 *
282 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200283 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100284 *
285 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 * if( assign ) mbedtls_mpi_swap( X, Y );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100287 * except that it avoids leaking any information about whether
288 * the assignment was done or not (the above code may leak
289 * information through branch prediction and/or memory access
290 * patterns analysis).
291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100293
294/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 * \brief Set value from integer
296 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000297 * \param X MPI to set
298 * \param z Value to use
299 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200301 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000304
Paul Bakker9a736322012-11-14 12:39:52 +0000305/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000306 * \brief Get a specific bit from X
307 *
308 * \param X MPI to use
309 * \param pos Zero-based index of the bit in X
310 *
311 * \return Either a 0 or a 1
312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000314
Paul Bakker9a736322012-11-14 12:39:52 +0000315/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000316 * \brief Set a bit of X to a specific value of 0 or 1
317 *
318 * \note Will grow X if necessary to set a bit to 1 in a not yet
319 * existing limb. Will not grow if bit should be set to 0
320 *
321 * \param X MPI to use
322 * \param pos Zero-based index of the bit in X
323 * \param val The value to set the bit to (0 or 1)
324 *
325 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200326 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
Paul Bakker2f5947e2011-05-18 15:47:11 +0000328 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000330
Paul Bakker5121ce52009-01-03 21:22:43 +0000331/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000332 * \brief Return the number of zero-bits before the least significant
333 * '1' bit
334 *
335 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000336 *
337 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000340
341/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000342 * \brief Return the number of bits up to and including the most
343 * significant '1' bit'
344 *
345 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000346 *
347 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200349size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000350
351/**
352 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000353 *
354 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356size_t mbedtls_mpi_size( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000357
358/**
359 * \brief Import from an ASCII string
360 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000361 * \param X Destination MPI
362 * \param radix Input numeric base
363 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000364 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000366 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000368
369/**
370 * \brief Export into an ASCII string
371 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000372 * \param X Source MPI
373 * \param radix Output numeric base
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100374 * \param buf Buffer to write the string to
375 * \param buflen Length of buf
376 * \param olen Length of the string written, including final NUL byte
Paul Bakker5121ce52009-01-03 21:22:43 +0000377 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code.
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100379 * *olen is always updated to reflect the amount
Paul Bakkerff60ee62010-03-16 21:09:09 +0000380 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000381 *
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100382 * \note Call this function with buflen = 0 to obtain the
383 * minimum required buffer size in *olen.
Paul Bakker5121ce52009-01-03 21:22:43 +0000384 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100385int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
386 char *buf, size_t buflen, size_t *olen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000389/**
Hanno Beckerb2034b72017-04-26 11:46:46 +0100390 * \brief Read MPI from a line in an opened file
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000392 * \param X Destination MPI
393 * \param radix Input numeric base
394 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
Paul Bakkercb37aa52011-11-30 16:00:20 +0000397 * the file read buffer is too small or a
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 * MBEDTLS_ERR_MPI_XXX error code
Hanno Beckerb2034b72017-04-26 11:46:46 +0100399 *
400 * \note On success, this function advances the file stream
401 * to the end of the current line or to EOF.
402 *
403 * The function returns 0 on an empty line.
404 *
405 * Leading whitespaces are ignored, as is a
406 * '0x' prefix for radix 16.
407 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
Paul Bakker5121ce52009-01-03 21:22:43 +0000410
411/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000412 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000414 * \param p Prefix, can be NULL
415 * \param X Source MPI
416 * \param radix Output numeric base
417 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000418 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000420 *
421 * \note Set fout == NULL to print X on the console.
422 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
424#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000425
426/**
427 * \brief Import X from unsigned binary data, big endian
428 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000429 * \param X Destination MPI
430 * \param buf Input buffer
431 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000432 *
433 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200434 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000437
438/**
Manuel Pégourié-Gonnard3926a2c2014-06-25 12:57:47 +0200439 * \brief Export X into unsigned binary data, big endian.
440 * Always fills the whole buffer, which will start with zeros
441 * if the number is smaller.
Paul Bakker5121ce52009-01-03 21:22:43 +0000442 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000443 * \param X Source MPI
444 * \param buf Output buffer
445 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000446 *
447 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000451
452/**
453 * \brief Left-shift: X <<= count
454 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000455 * \param X MPI to shift
456 * \param count Amount to shift
457 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000458 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200459 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
463/**
464 * \brief Right-shift: X >>= count
465 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000466 * \param X MPI to shift
467 * \param count Amount to shift
468 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000469 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200470 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000473
474/**
475 * \brief Compare unsigned 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_abs( 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 Y Right-hand MPI
491 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000492 * \return 1 if X is greater than Y,
493 * -1 if X is lesser than Y or
494 * 0 if X is equal to Y
495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000497
498/**
499 * \brief Compare signed values
500 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000501 * \param X Left-hand MPI
502 * \param z The integer value to compare to
503 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000504 * \return 1 if X is greater than z,
505 * -1 if X is lesser than z or
506 * 0 if X is equal to z
507 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000509
510/**
511 * \brief Unsigned addition: X = |A| + |B|
512 *
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é-Gonnard6a8ca332015-05-28 09:33:39 +0200518 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000521
522/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100523 * \brief Unsigned subtraction: X = |A| - |B|
Paul Bakker5121ce52009-01-03 21:22:43 +0000524 *
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000531 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000533
534/**
535 * \brief Signed addition: X = A + B
536 *
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_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000545
546/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100547 * \brief Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +0000548 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000549 * \param X Destination MPI
550 * \param A Left-hand MPI
551 * \param B Right-hand MPI
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_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000557
558/**
559 * \brief Signed addition: X = A + b
560 *
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 add
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_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
570/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100571 * \brief Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000573 * \param X Destination MPI
574 * \param A Left-hand MPI
575 * \param b The integer value to subtract
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_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint 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
587 * \param B Right-hand MPI
588 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000589 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200590 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000593
594/**
595 * \brief Baseline multiplication: X = A * b
596 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000597 * \param X Destination MPI
598 * \param A Left-hand MPI
Manuel Pégourié-Gonnard35f1d7f2015-03-19 12:42:40 +0000599 * \param b The unsigned integer value to multiply with
600 *
601 * \note b is unsigned
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000602 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000603 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200604 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000607
608/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 * \brief Division by mbedtls_mpi: A = Q * B + R
Paul Bakker5121ce52009-01-03 21:22:43 +0000610 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000611 * \param Q Destination MPI for the quotient
612 * \param R Destination MPI for the rest value
613 * \param A Left-hand MPI
614 * \param B Right-hand MPI
615 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200617 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000619 *
620 * \note Either Q or R can be NULL.
621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622int 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 +0000623
624/**
625 * \brief Division by int: A = Q * b + R
626 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000627 * \param Q Destination MPI for the quotient
628 * \param R Destination MPI for the rest value
629 * \param A Left-hand MPI
630 * \param b Integer to divide by
631 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200633 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000635 *
636 * \note Either Q or R can be NULL.
637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638int 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 +0000639
640/**
641 * \brief Modulo: R = A mod B
642 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000643 * \param R Destination MPI for the rest value
644 * \param A Left-hand MPI
645 * \param B Right-hand MPI
646 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000647 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200648 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0,
650 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000651 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000653
654/**
655 * \brief Modulo: r = A mod b
656 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 * \param r Destination mbedtls_mpi_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000658 * \param A Left-hand MPI
659 * \param b Integer to divide by
660 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200662 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0,
664 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000665 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000667
668/**
669 * \brief Sliding-window exponentiation: X = A^E mod N
670 *
Paul Bakker9af723c2014-05-01 13:03:14 +0200671 * \param X Destination MPI
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000672 * \param A Left-hand MPI
673 * \param E Exponent MPI
674 * \param N Modular MPI
675 * \param _RR Speed-up MPI used for recalculations
676 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000677 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200678 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200680 * if E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000681 *
682 * \note _RR is used to avoid re-computing R*R mod N across
683 * multiple calls, which speeds up things a bit. It can
684 * be set to NULL if the extra performance is unneeded.
685 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686int 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 +0000687
688/**
Paul Bakker287781a2011-03-26 13:18:49 +0000689 * \brief Fill an MPI X with size bytes of random
690 *
691 * \param X Destination MPI
692 * \param size Size in bytes
693 * \param f_rng RNG function
694 * \param p_rng RNG parameter
695 *
696 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200697 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000700 int (*f_rng)(void *, unsigned char *, size_t),
701 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000702
703/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000704 * \brief Greatest common divisor: G = gcd(A, B)
705 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000706 * \param G Destination MPI
707 * \param A Left-hand MPI
708 * \param B 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
Paul Bakker5121ce52009-01-03 21:22:43 +0000712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000714
715/**
716 * \brief Modular inverse: X = A^-1 mod N
717 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000718 * \param X Destination MPI
719 * \param A Left-hand MPI
720 * \param N Right-hand MPI
721 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000722 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200723 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Hanno Becker4bcb4912017-04-18 15:49:39 +0100724 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is <= 1,
725 MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000728
729/**
730 * \brief Miller-Rabin primality test
731 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000732 * \param X MPI to check
733 * \param f_rng RNG function
734 * \param p_rng RNG parameter
735 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000736 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200737 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000739 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000741 int (*f_rng)(void *, unsigned char *, size_t),
742 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000743
744/**
745 * \brief Prime number generation
746 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000747 * \param X Destination MPI
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200748 * \param nbits Required size of X in bits
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000750 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000751 * \param f_rng RNG function
752 * \param p_rng RNG parameter
753 *
754 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200755 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000759 int (*f_rng)(void *, unsigned char *, size_t),
760 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000761
762/**
763 * \brief Checkup routine
764 *
765 * \return 0 if successful, or 1 if the test failed
766 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767int mbedtls_mpi_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000768
769#ifdef __cplusplus
770}
771#endif
772
773#endif /* bignum.h */