blob: ac89069dc208c44a8446c0c1424bd934c9247328 [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 */
Andres Amaya Garciaaa27dfe2017-05-04 11:05:55 +0100109#if !defined(MBEDTLS_HAVE_INT32)
110 #if defined(_MSC_VER) && defined(_M_AMD64)
111 /* Always choose 64-bit when using MSC */
112 #define MBEDTLS_HAVE_INT64
113 typedef int64_t mbedtls_mpi_sint;
114 typedef uint64_t mbedtls_mpi_uint;
115 #elif defined(__GNUC__) && ( \
116 defined(__amd64__) || defined(__x86_64__) || \
117 defined(__ppc64__) || defined(__powerpc64__) || \
118 defined(__ia64__) || defined(__alpha__) || \
119 ( defined(__sparc__) && defined(__arch64__) ) || \
120 defined(__s390x__) || defined(__mips64) )
121 #define MBEDTLS_HAVE_INT64
122 typedef int64_t mbedtls_mpi_sint;
123 typedef uint64_t mbedtls_mpi_uint;
124 /* mbedtls_t_udbl defined as 128-bit unsigned int */
125 typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
126 #define MBEDTLS_HAVE_UDBL
127 #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
128 /* __ARMCC_VERSION is defined for both armcc and armclang and
129 * __aarch64__ is only defined by armclang when compiling 64-bit code
130 */
131 #define MBEDTLS_HAVE_INT64
132 typedef int64_t mbedtls_mpi_sint;
133 typedef uint64_t mbedtls_mpi_uint;
134 /* mbedtls_t_udbl defined as 128-bit unsigned int */
135 typedef __uint128_t mbedtls_t_udbl;
136 #define MBEDTLS_HAVE_UDBL
137 #endif
138#endif /* !MBEDTLS_HAVE_INT32 */
139
140#if !defined(MBEDTLS_HAVE_INT64)
141 /* Default to 32-bit compilation */
142 #if !defined(MBEDTLS_HAVE_INT32)
143 #define MBEDTLS_HAVE_INT32
144 #endif /* !MBEDTLS_HAVE_INT32 */
145 typedef int32_t mbedtls_mpi_sint;
146 typedef uint32_t mbedtls_mpi_uint;
147 typedef uint64_t mbedtls_t_udbl;
148 #define MBEDTLS_HAVE_UDBL
149#endif /* !MBEDTLS_HAVE_INT64 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
Paul Bakker407a0da2013-06-27 14:29:21 +0200151#ifdef __cplusplus
152extern "C" {
153#endif
154
Paul Bakker5121ce52009-01-03 21:22:43 +0000155/**
156 * \brief MPI structure
157 */
158typedef struct
159{
160 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000161 size_t n; /*!< total # of limbs */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_mpi_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000163}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164mbedtls_mpi;
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
Paul Bakker5121ce52009-01-03 21:22:43 +0000166/**
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200167 * \brief Initialize one MPI (make internal references valid)
168 * This just makes it ready to be set or freed,
169 * but does not define a value for the MPI.
Paul Bakker6c591fa2011-05-05 11:49:20 +0000170 *
171 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173void mbedtls_mpi_init( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
175/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000176 * \brief Unallocate one MPI
177 *
178 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180void mbedtls_mpi_free( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
182/**
183 * \brief Enlarge to the specified number of limbs
184 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000185 * \param X MPI to grow
186 * \param nblimbs The target number of limbs
187 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200189 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
193/**
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100194 * \brief Resize down, keeping at least the specified number of limbs
195 *
196 * \param X MPI to shrink
197 * \param nblimbs The minimum number of limbs to keep
198 *
199 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200200 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100203
204/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000205 * \brief Copy the contents of Y into X
206 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000207 * \param X Destination MPI
208 * \param Y Source MPI
209 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200211 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000214
215/**
216 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000217 *
218 * \param X First MPI value
219 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222
223/**
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100224 * \brief Safe conditional assignement X = Y if assign is 1
225 *
226 * \param X MPI to conditionally assign to
227 * \param Y Value to be assigned
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100228 * \param assign 1: perform the assignment, 0: keep X's original value
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100229 *
230 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200231 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100232 *
233 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 * if( assign ) mbedtls_mpi_copy( X, Y );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100235 * except that it avoids leaking any information about whether
236 * the assignment was done or not (the above code may leak
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100237 * information through branch prediction and/or memory access
238 * patterns analysis).
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100241
242/**
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100243 * \brief Safe conditional swap X <-> Y if swap is 1
244 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 * \param X First mbedtls_mpi value
246 * \param Y Second mbedtls_mpi value
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100247 * \param assign 1: perform the swap, 0: keep X and Y's original values
248 *
249 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200250 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100251 *
252 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 * if( assign ) mbedtls_mpi_swap( X, Y );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100254 * except that it avoids leaking any information about whether
255 * the assignment was done or not (the above code may leak
256 * information through branch prediction and/or memory access
257 * patterns analysis).
258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100260
261/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 * \brief Set value from integer
263 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000264 * \param X MPI to set
265 * \param z Value to use
266 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000267 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200268 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
Paul Bakker9a736322012-11-14 12:39:52 +0000272/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000273 * \brief Get a specific bit from X
274 *
275 * \param X MPI to use
276 * \param pos Zero-based index of the bit in X
277 *
278 * \return Either a 0 or a 1
279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000281
Paul Bakker9a736322012-11-14 12:39:52 +0000282/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000283 * \brief Set a bit of X to a specific value of 0 or 1
284 *
285 * \note Will grow X if necessary to set a bit to 1 in a not yet
286 * existing limb. Will not grow if bit should be set to 0
287 *
288 * \param X MPI to use
289 * \param pos Zero-based index of the bit in X
290 * \param val The value to set the bit to (0 or 1)
291 *
292 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200293 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
Paul Bakker2f5947e2011-05-18 15:47:11 +0000295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000297
Paul Bakker5121ce52009-01-03 21:22:43 +0000298/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000299 * \brief Return the number of zero-bits before the least significant
300 * '1' bit
301 *
302 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000303 *
304 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
308/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000309 * \brief Return the number of bits up to and including the most
310 * significant '1' bit'
311 *
312 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000313 *
314 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000315 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200316size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000317
318/**
319 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000320 *
321 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323size_t mbedtls_mpi_size( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000324
325/**
326 * \brief Import from an ASCII string
327 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000328 * \param X Destination MPI
329 * \param radix Input numeric base
330 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000335
336/**
337 * \brief Export into an ASCII string
338 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000339 * \param X Source MPI
340 * \param radix Output numeric base
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100341 * \param buf Buffer to write the string to
342 * \param buflen Length of buf
343 * \param olen Length of the string written, including final NUL byte
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code.
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100346 * *olen is always updated to reflect the amount
Paul Bakkerff60ee62010-03-16 21:09:09 +0000347 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 *
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100349 * \note Call this function with buflen = 0 to obtain the
350 * minimum required buffer size in *olen.
Paul Bakker5121ce52009-01-03 21:22:43 +0000351 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100352int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
353 char *buf, size_t buflen, size_t *olen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000356/**
Hanno Beckerb2034b72017-04-26 11:46:46 +0100357 * \brief Read MPI from a line in an opened file
Paul Bakker5121ce52009-01-03 21:22:43 +0000358 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000359 * \param X Destination MPI
360 * \param radix Input numeric base
361 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
Paul Bakkercb37aa52011-11-30 16:00:20 +0000364 * the file read buffer is too small or a
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 * MBEDTLS_ERR_MPI_XXX error code
Hanno Beckerb2034b72017-04-26 11:46:46 +0100366 *
367 * \note On success, this function advances the file stream
368 * to the end of the current line or to EOF.
369 *
370 * The function returns 0 on an empty line.
371 *
372 * Leading whitespaces are ignored, as is a
373 * '0x' prefix for radix 16.
374 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
Paul Bakker5121ce52009-01-03 21:22:43 +0000377
378/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000379 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000380 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000381 * \param p Prefix, can be NULL
382 * \param X Source MPI
383 * \param radix Output numeric base
384 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 *
388 * \note Set fout == NULL to print X on the console.
389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
391#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
393/**
394 * \brief Import X from unsigned binary data, big endian
395 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000396 * \param X Destination MPI
397 * \param buf Input buffer
398 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000399 *
400 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200401 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000404
405/**
Manuel Pégourié-Gonnard3926a2c2014-06-25 12:57:47 +0200406 * \brief Export X into unsigned binary data, big endian.
407 * Always fills the whole buffer, which will start with zeros
408 * if the number is smaller.
Paul Bakker5121ce52009-01-03 21:22:43 +0000409 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000410 * \param X Source MPI
411 * \param buf Output buffer
412 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 *
414 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000418
419/**
420 * \brief Left-shift: X <<= count
421 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000422 * \param X MPI to shift
423 * \param count Amount to shift
424 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000425 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200426 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000427 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000429
430/**
431 * \brief Right-shift: X >>= count
432 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000433 * \param X MPI to shift
434 * \param count Amount to shift
435 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000436 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200437 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000438 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000440
441/**
442 * \brief Compare unsigned values
443 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000444 * \param X Left-hand MPI
445 * \param Y Right-hand MPI
446 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000447 * \return 1 if |X| is greater than |Y|,
448 * -1 if |X| is lesser than |Y| or
449 * 0 if |X| is equal to |Y|
450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000452
453/**
454 * \brief Compare signed values
455 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000456 * \param X Left-hand MPI
457 * \param Y Right-hand MPI
458 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000459 * \return 1 if X is greater than Y,
460 * -1 if X is lesser than Y or
461 * 0 if X is equal to Y
462 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000464
465/**
466 * \brief Compare signed values
467 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000468 * \param X Left-hand MPI
469 * \param z The integer value to compare to
470 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000471 * \return 1 if X is greater than z,
472 * -1 if X is lesser than z or
473 * 0 if X is equal to z
474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000476
477/**
478 * \brief Unsigned 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_abs( 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 Unsigned 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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499int mbedtls_mpi_sub_abs( 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 Right-hand MPI
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_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *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 Right-hand MPI
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_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
525/**
526 * \brief Signed addition: 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 The integer value to add
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_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000536
537/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100538 * \brief Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +0000539 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000540 * \param X Destination MPI
541 * \param A Left-hand MPI
542 * \param b The integer value to subtract
543 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200545 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549/**
550 * \brief Baseline multiplication: X = A * B
551 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000552 * \param X Destination MPI
553 * \param A Left-hand MPI
554 * \param B Right-hand MPI
555 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000556 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200557 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000558 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
561/**
562 * \brief Baseline multiplication: X = A * b
563 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000564 * \param X Destination MPI
565 * \param A Left-hand MPI
Manuel Pégourié-Gonnard35f1d7f2015-03-19 12:42:40 +0000566 * \param b The unsigned integer value to multiply with
567 *
568 * \note b is unsigned
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000569 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000570 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200571 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
575/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 * \brief Division by mbedtls_mpi: A = Q * B + R
Paul Bakker5121ce52009-01-03 21:22:43 +0000577 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000578 * \param Q Destination MPI for the quotient
579 * \param R Destination MPI for the rest value
580 * \param A Left-hand MPI
581 * \param B Right-hand MPI
582 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000583 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200584 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000586 *
587 * \note Either Q or R can be NULL.
588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589int 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 +0000590
591/**
592 * \brief Division by int: A = Q * b + R
593 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000594 * \param Q Destination MPI for the quotient
595 * \param R Destination MPI for the rest value
596 * \param A Left-hand MPI
597 * \param b Integer to divide by
598 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000599 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200600 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000602 *
603 * \note Either Q or R can be NULL.
604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605int 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 +0000606
607/**
608 * \brief Modulo: R = A mod B
609 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000610 * \param R Destination MPI for the rest value
611 * \param A Left-hand MPI
612 * \param B Right-hand MPI
613 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200615 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0,
617 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
621/**
622 * \brief Modulo: r = A mod b
623 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200624 * \param r Destination mbedtls_mpi_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000625 * \param A Left-hand MPI
626 * \param b Integer to divide by
627 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000628 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200629 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0,
631 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000634
635/**
636 * \brief Sliding-window exponentiation: X = A^E mod N
637 *
Paul Bakker9af723c2014-05-01 13:03:14 +0200638 * \param X Destination MPI
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000639 * \param A Left-hand MPI
640 * \param E Exponent MPI
641 * \param N Modular MPI
642 * \param _RR Speed-up MPI used for recalculations
643 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000644 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200645 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200647 * if E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000648 *
649 * \note _RR is used to avoid re-computing R*R mod N across
650 * multiple calls, which speeds up things a bit. It can
651 * be set to NULL if the extra performance is unneeded.
652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653int 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 +0000654
655/**
Paul Bakker287781a2011-03-26 13:18:49 +0000656 * \brief Fill an MPI X with size bytes of random
657 *
658 * \param X Destination MPI
659 * \param size Size in bytes
660 * \param f_rng RNG function
661 * \param p_rng RNG parameter
662 *
663 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200664 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker287781a2011-03-26 13:18:49 +0000665 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000667 int (*f_rng)(void *, unsigned char *, size_t),
668 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000669
670/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000671 * \brief Greatest common divisor: G = gcd(A, B)
672 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000673 * \param G Destination MPI
674 * \param A Left-hand MPI
675 * \param B Right-hand MPI
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
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000681
682/**
683 * \brief Modular inverse: X = A^-1 mod N
684 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000685 * \param X Destination MPI
686 * \param A Left-hand MPI
687 * \param N Right-hand MPI
688 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000689 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200690 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Hanno Becker4bcb4912017-04-18 15:49:39 +0100691 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is <= 1,
692 MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000695
696/**
697 * \brief Miller-Rabin primality test
698 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000699 * \param X MPI to check
700 * \param f_rng RNG function
701 * \param p_rng RNG parameter
702 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000703 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200704 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000708 int (*f_rng)(void *, unsigned char *, size_t),
709 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000710
711/**
712 * \brief Prime number generation
713 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000714 * \param X Destination MPI
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200715 * \param nbits Required size of X in bits
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000717 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000718 * \param f_rng RNG function
719 * \param p_rng RNG parameter
720 *
721 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200722 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000726 int (*f_rng)(void *, unsigned char *, size_t),
727 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000728
729/**
730 * \brief Checkup routine
731 *
732 * \return 0 if successful, or 1 if the test failed
733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734int mbedtls_mpi_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000735
736#ifdef __cplusplus
737}
738#endif
739
740#endif /* bignum.h */