blob: 030982d863f21fdd6e80d6079bbc528ae56b9437 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file bignum.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakker37ca75d2011-01-06 12:28:03 +00004 * \brief Multi-precision integer library
5 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_BIGNUM_H
24#define MBEDTLS_BIGNUM_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakkercf0360a2012-01-20 10:08:14 +000027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakkercf0360a2012-01-20 10:08:14 +000031
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020033#include <stdint.h>
Rich Evans00ab4702015-02-06 13:43:58 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnarde94e6e52015-01-19 15:01:53 +000036#include <stdio.h>
37#endif
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */
40#define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */
41#define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */
42#define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */
43#define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */
44#define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */
45#define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020046#define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 /**< Memory allocation failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000049
50/*
Paul Bakkerf9688572011-05-05 10:00:45 +000051 * Maximum size MPIs are allowed to grow to in number of limbs.
52 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#define MBEDTLS_MPI_MAX_LIMBS 10000
Paul Bakkerf9688572011-05-05 10:00:45 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if !defined(MBEDTLS_MPI_WINDOW_SIZE)
Paul Bakkerf9688572011-05-05 10:00:45 +000056/*
Paul Bakkerb6d5f082011-11-25 11:52:11 +000057 * Maximum window size used for modular exponentiation. Default: 6
58 * Minimum value: 1. Maximum value: 6.
59 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
Paul Bakkerb6d5f082011-11-25 11:52:11 +000061 * for the sliding window calculation. (So 64 by default)
62 *
63 * Reduction in size, reduces speed.
64 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */
66#endif /* !MBEDTLS_MPI_WINDOW_SIZE */
Paul Bakkerb6d5f082011-11-25 11:52:11 +000067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if !defined(MBEDTLS_MPI_MAX_SIZE)
Paul Bakkerb6d5f082011-11-25 11:52:11 +000069/*
Paul Bakkerfe3256e2011-11-25 12:11:43 +000070 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
Paul Bakkerf626e1d2013-01-21 12:10:00 +010071 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
Paul Bakkerfe3256e2011-11-25 12:11:43 +000072 *
73 * Note: Calculations can results temporarily in larger MPIs. So the number
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
Paul Bakkerfe3256e2011-11-25 12:11:43 +000075 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */
77#endif /* !MBEDTLS_MPI_MAX_SIZE */
Paul Bakker9bcf16c2013-06-24 19:31:17 +020078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */
Paul Bakkerfe3256e2011-11-25 12:11:43 +000080
81/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 * When reading from files with mbedtls_mpi_read_file() and writing to files with
83 * mbedtls_mpi_write_file() the buffer should have space
Paul Bakkercb37aa52011-11-30 16:00:20 +000084 * for a (short) label, the MPI (in the provided radix), the newline
85 * characters and the '\0'.
86 *
87 * By default we assume at least a 10 char label, a minimum radix of 10
88 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
Paul Bakkerf9183102012-09-27 20:42:35 +000089 * Autosized at compile time for at least a 10 char label, a minimum radix
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
Paul Bakkerf9183102012-09-27 20:42:35 +000091 *
92 * This used to be statically sized to 1250 for a maximum of 4096 bit
93 * numbers (1234 decimal chars).
94 *
95 * Calculate using the formula:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
Paul Bakkerf9183102012-09-27 20:42:35 +000097 * LabelSize + 6
Paul Bakkercb37aa52011-11-30 16:00:20 +000098 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
100#define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
101#define MBEDTLS_MPI_RW_BUFFER_SIZE ( ((MBEDTLS_MPI_MAX_BITS_SCALE100 + MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
Paul Bakkercb37aa52011-11-30 16:00:20 +0000102
103/*
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200104 * Define the base integer type, architecture-wise.
105 *
106 * 32-bit integers can be forced on 64-bit arches (eg. for testing purposes)
107 * by defining MBEDTLS_HAVE_INT32 and undefining MBEDTLS_HAVE_ASM
Paul Bakker5121ce52009-01-03 21:22:43 +0000108 */
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200109#if ( ! defined(MBEDTLS_HAVE_INT32) && \
110 defined(_MSC_VER) && defined(_M_AMD64) )
111 #define MBEDTLS_HAVE_INT64
112 typedef int64_t mbedtls_mpi_sint;
113 typedef uint64_t mbedtls_mpi_uint;
Paul Bakker5121ce52009-01-03 21:22:43 +0000114#else
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200115 #if ( ! defined(MBEDTLS_HAVE_INT32) && \
116 defined(__GNUC__) && ( \
117 defined(__amd64__) || defined(__x86_64__) || \
118 defined(__ppc64__) || defined(__powerpc64__) || \
119 defined(__ia64__) || defined(__alpha__) || \
120 (defined(__sparc__) && defined(__arch64__)) || \
121 defined(__s390x__) || defined(__mips64) ) )
122 #define MBEDTLS_HAVE_INT64
123 typedef int64_t mbedtls_mpi_sint;
124 typedef uint64_t mbedtls_mpi_uint;
Simon Butcher61891752016-01-03 00:24:34 +0000125 /* mbedtls_t_udbl defined as 128-bit unsigned int */
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200126 typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
127 #define MBEDTLS_HAVE_UDBL
Paul Bakker62261d62012-10-02 12:19:31 +0000128 #else
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200129 #define MBEDTLS_HAVE_INT32
130 typedef int32_t mbedtls_mpi_sint;
131 typedef uint32_t mbedtls_mpi_uint;
Manuel Pégourié-Gonnard975d5fa2015-04-09 17:13:45 +0200132 typedef uint64_t mbedtls_t_udbl;
133 #define MBEDTLS_HAVE_UDBL
Manuel Pégourié-Gonnard7b538892015-04-09 17:00:17 +0200134 #endif /* !MBEDTLS_HAVE_INT32 && __GNUC__ && 64-bit platform */
135#endif /* !MBEDTLS_HAVE_INT32 && _MSC_VER && _M_AMD64 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
Paul Bakker407a0da2013-06-27 14:29:21 +0200137#ifdef __cplusplus
138extern "C" {
139#endif
140
Paul Bakker5121ce52009-01-03 21:22:43 +0000141/**
142 * \brief MPI structure
143 */
144typedef struct
145{
146 int s; /*!< integer sign */
Paul Bakker23986e52011-04-24 08:57:21 +0000147 size_t n; /*!< total # of limbs */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_mpi_uint *p; /*!< pointer to limbs */
Paul Bakker5121ce52009-01-03 21:22:43 +0000149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150mbedtls_mpi;
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
Paul Bakker5121ce52009-01-03 21:22:43 +0000152/**
Manuel Pégourié-Gonnardda61ed32015-04-30 10:28:51 +0200153 * \brief Initialize one MPI (make internal references valid)
154 * This just makes it ready to be set or freed,
155 * but does not define a value for the MPI.
Paul Bakker6c591fa2011-05-05 11:49:20 +0000156 *
157 * \param X One MPI to initialize.
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159void mbedtls_mpi_init( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
161/**
Paul Bakker6c591fa2011-05-05 11:49:20 +0000162 * \brief Unallocate one MPI
163 *
164 * \param X One MPI to unallocate.
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166void mbedtls_mpi_free( mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167
168/**
169 * \brief Enlarge to the specified number of limbs
170 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000171 * \param X MPI to grow
172 * \param nblimbs The target number of limbs
173 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200175 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
179/**
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100180 * \brief Resize down, keeping at least the specified number of limbs
181 *
182 * \param X MPI to shrink
183 * \param nblimbs The minimum number of limbs to keep
184 *
185 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200186 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
Manuel Pégourié-Gonnard58681632013-11-21 10:39:37 +0100189
190/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 * \brief Copy the contents of Y into X
192 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000193 * \param X Destination MPI
194 * \param Y Source MPI
195 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200197 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000200
201/**
202 * \brief Swap the contents of X and Y
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000203 *
204 * \param X First MPI value
205 * \param Y Second MPI value
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000208
209/**
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100210 * \brief Safe conditional assignement X = Y if assign is 1
211 *
212 * \param X MPI to conditionally assign to
213 * \param Y Value to be assigned
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100214 * \param assign 1: perform the assignment, 0: keep X's original value
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100215 *
216 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200217 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100218 *
219 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 * if( assign ) mbedtls_mpi_copy( X, Y );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100221 * except that it avoids leaking any information about whether
222 * the assignment was done or not (the above code may leak
Manuel Pégourié-Gonnardd7283502013-11-21 20:00:38 +0100223 * information through branch prediction and/or memory access
224 * patterns analysis).
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100225 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnard71c2c212013-11-21 16:56:39 +0100227
228/**
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100229 * \brief Safe conditional swap X <-> Y if swap is 1
230 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 * \param X First mbedtls_mpi value
232 * \param Y Second mbedtls_mpi value
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100233 * \param assign 1: perform the swap, 0: keep X and Y's original values
234 *
235 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200236 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100237 *
238 * \note This function is equivalent to
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 * if( assign ) mbedtls_mpi_swap( X, Y );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100240 * except that it avoids leaking any information about whether
241 * the assignment was done or not (the above code may leak
242 * information through branch prediction and/or memory access
243 * patterns analysis).
244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
Manuel Pégourié-Gonnarda60fe892013-12-04 21:41:50 +0100246
247/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 * \brief Set value from integer
249 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000250 * \param X MPI to set
251 * \param z Value to use
252 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200254 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000257
Paul Bakker9a736322012-11-14 12:39:52 +0000258/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000259 * \brief Get a specific bit from X
260 *
261 * \param X MPI to use
262 * \param pos Zero-based index of the bit in X
263 *
264 * \return Either a 0 or a 1
265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000267
Paul Bakker9a736322012-11-14 12:39:52 +0000268/**
Paul Bakker2f5947e2011-05-18 15:47:11 +0000269 * \brief Set a bit of X to a specific value of 0 or 1
270 *
271 * \note Will grow X if necessary to set a bit to 1 in a not yet
272 * existing limb. Will not grow if bit should be set to 0
273 *
274 * \param X MPI to use
275 * \param pos Zero-based index of the bit in X
276 * \param val The value to set the bit to (0 or 1)
277 *
278 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200279 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1
Paul Bakker2f5947e2011-05-18 15:47:11 +0000281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
Paul Bakker2f5947e2011-05-18 15:47:11 +0000283
Paul Bakker5121ce52009-01-03 21:22:43 +0000284/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000285 * \brief Return the number of zero-bits before the least significant
286 * '1' bit
287 *
288 * Note: Thus also the zero-based index of the least significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000289 *
290 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000293
294/**
Paul Bakker6b906e52012-05-08 12:01:43 +0000295 * \brief Return the number of bits up to and including the most
296 * significant '1' bit'
297 *
298 * Note: Thus also the one-based index of the most significant '1' bit
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000299 *
300 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 */
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200302size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000303
304/**
305 * \brief Return the total size in bytes
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000306 *
307 * \param X MPI to use
Paul Bakker5121ce52009-01-03 21:22:43 +0000308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309size_t mbedtls_mpi_size( const mbedtls_mpi *X );
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
311/**
312 * \brief Import from an ASCII string
313 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000314 * \param X Destination MPI
315 * \param radix Input numeric base
316 * \param s Null-terminated string buffer
Paul Bakker5121ce52009-01-03 21:22:43 +0000317 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
Paul Bakker5121ce52009-01-03 21:22:43 +0000321
322/**
323 * \brief Export into an ASCII string
324 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000325 * \param X Source MPI
326 * \param radix Output numeric base
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100327 * \param buf Buffer to write the string to
328 * \param buflen Length of buf
329 * \param olen Length of the string written, including final NUL byte
Paul Bakker5121ce52009-01-03 21:22:43 +0000330 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code.
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100332 * *olen is always updated to reflect the amount
Paul Bakkerff60ee62010-03-16 21:09:09 +0000333 * of data that has (or would have) been written.
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 *
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100335 * \note Call this function with buflen = 0 to obtain the
336 * minimum required buffer size in *olen.
Paul Bakker5121ce52009-01-03 21:22:43 +0000337 */
Manuel Pégourié-Gonnardf79b4252015-06-02 15:41:48 +0100338int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
339 char *buf, size_t buflen, size_t *olen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341#if defined(MBEDTLS_FS_IO)
Paul Bakker5121ce52009-01-03 21:22:43 +0000342/**
Hanno Becker4195e802017-04-26 11:46:46 +0100343 * \brief Read MPI from a line in an opened file
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000345 * \param X Destination MPI
346 * \param radix Input numeric base
347 * \param fin Input file handle
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if
Paul Bakkercb37aa52011-11-30 16:00:20 +0000350 * the file read buffer is too small or a
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 * MBEDTLS_ERR_MPI_XXX error code
Hanno Becker4195e802017-04-26 11:46:46 +0100352 *
353 * \note On success, this function advances the file stream
354 * to the end of the current line or to EOF.
355 *
356 * The function returns 0 on an empty line.
357 *
358 * Leading whitespaces are ignored, as is a
359 * '0x' prefix for radix 16.
360 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
Paul Bakker5121ce52009-01-03 21:22:43 +0000363
364/**
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000365 * \brief Write X into an opened file, or stdout if fout is NULL
Paul Bakker5121ce52009-01-03 21:22:43 +0000366 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000367 * \param p Prefix, can be NULL
368 * \param X Source MPI
369 * \param radix Output numeric base
370 * \param fout Output file handle (can be NULL)
Paul Bakker5121ce52009-01-03 21:22:43 +0000371 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 *
374 * \note Set fout == NULL to print X on the console.
375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
377#endif /* MBEDTLS_FS_IO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000378
379/**
380 * \brief Import X from unsigned binary data, big endian
381 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000382 * \param X Destination MPI
383 * \param buf Input buffer
384 * \param buflen Input buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 *
386 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200387 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000390
391/**
Manuel Pégourié-Gonnard3926a2c2014-06-25 12:57:47 +0200392 * \brief Export X into unsigned binary data, big endian.
393 * Always fills the whole buffer, which will start with zeros
394 * if the number is smaller.
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000396 * \param X Source MPI
397 * \param buf Output buffer
398 * \param buflen Output buffer size
Paul Bakker5121ce52009-01-03 21:22:43 +0000399 *
400 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
Paul Bakker5121ce52009-01-03 21:22:43 +0000404
405/**
406 * \brief Left-shift: X <<= count
407 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000408 * \param X MPI to shift
409 * \param count Amount to shift
410 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000411 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200412 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000415
416/**
417 * \brief Right-shift: X >>= count
418 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000419 * \param X MPI to shift
420 * \param count Amount to shift
421 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000422 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200423 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
Paul Bakker5121ce52009-01-03 21:22:43 +0000426
427/**
428 * \brief Compare unsigned values
429 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000430 * \param X Left-hand MPI
431 * \param Y Right-hand MPI
432 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000433 * \return 1 if |X| is greater than |Y|,
434 * -1 if |X| is lesser than |Y| or
435 * 0 if |X| is equal to |Y|
436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000438
439/**
440 * \brief Compare signed values
441 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000442 * \param X Left-hand MPI
443 * \param Y Right-hand MPI
444 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000445 * \return 1 if X is greater than Y,
446 * -1 if X is lesser than Y or
447 * 0 if X is equal to Y
448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
Paul Bakker5121ce52009-01-03 21:22:43 +0000450
451/**
452 * \brief Compare signed values
453 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000454 * \param X Left-hand MPI
455 * \param z The integer value to compare to
456 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000457 * \return 1 if X is greater than z,
458 * -1 if X is lesser than z or
459 * 0 if X is equal to z
460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
Paul Bakker5121ce52009-01-03 21:22:43 +0000462
463/**
464 * \brief Unsigned addition: X = |A| + |B|
465 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000466 * \param X Destination MPI
467 * \param A Left-hand MPI
468 * \param B Right-hand MPI
469 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000470 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200471 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000474
475/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100476 * \brief Unsigned subtraction: X = |A| - |B|
Paul Bakker5121ce52009-01-03 21:22:43 +0000477 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000478 * \param X Destination MPI
479 * \param A Left-hand MPI
480 * \param B Right-hand MPI
481 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000482 * \return 0 if successful,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A
Paul Bakker5121ce52009-01-03 21:22:43 +0000484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000486
487/**
488 * \brief Signed addition: X = A + B
489 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000490 * \param X Destination MPI
491 * \param A Left-hand MPI
492 * \param B Right-hand MPI
493 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000494 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200495 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000496 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000498
499/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100500 * \brief Signed subtraction: X = A - B
Paul Bakker5121ce52009-01-03 21:22:43 +0000501 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000502 * \param X Destination MPI
503 * \param A Left-hand MPI
504 * \param B Right-hand MPI
505 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000506 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200507 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000510
511/**
512 * \brief Signed addition: X = A + b
513 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000514 * \param X Destination MPI
515 * \param A Left-hand MPI
516 * \param b The integer value to add
517 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200519 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000520 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000522
523/**
Paul Bakker60b1d102013-10-29 10:02:51 +0100524 * \brief Signed subtraction: X = A - b
Paul Bakker5121ce52009-01-03 21:22:43 +0000525 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000526 * \param X Destination MPI
527 * \param A Left-hand MPI
528 * \param b The integer value to subtract
529 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000530 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200531 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000534
535/**
536 * \brief Baseline multiplication: X = A * B
537 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000538 * \param X Destination MPI
539 * \param A Left-hand MPI
540 * \param B Right-hand MPI
541 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000542 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200543 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000546
547/**
548 * \brief Baseline multiplication: X = A * b
549 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000550 * \param X Destination MPI
551 * \param A Left-hand MPI
Manuel Pégourié-Gonnard35f1d7f2015-03-19 12:42:40 +0000552 * \param b The unsigned integer value to multiply with
553 *
554 * \note b is unsigned
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000555 *
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_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000560
561/**
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 * \brief Division by mbedtls_mpi: A = Q * B + R
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000564 * \param Q Destination MPI for the quotient
565 * \param R Destination MPI for the rest value
566 * \param A Left-hand MPI
567 * \param B Right-hand MPI
568 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000569 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200570 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000572 *
573 * \note Either Q or R can be NULL.
574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575int 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 +0000576
577/**
578 * \brief Division by int: A = Q * b + R
579 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000580 * \param Q Destination MPI for the quotient
581 * \param R Destination MPI for the rest value
582 * \param A Left-hand MPI
583 * \param b Integer to divide by
584 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000585 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200586 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000588 *
589 * \note Either Q or R can be NULL.
590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591int 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 +0000592
593/**
594 * \brief Modulo: R = A mod B
595 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000596 * \param R Destination MPI for the rest value
597 * \param A Left-hand MPI
598 * \param B Right-hand MPI
599 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000600 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200601 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0,
603 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000606
607/**
608 * \brief Modulo: r = A mod b
609 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 * \param r Destination mbedtls_mpi_uint
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000611 * \param A Left-hand MPI
612 * \param b Integer to divide by
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_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
621/**
622 * \brief Sliding-window exponentiation: X = A^E mod N
623 *
Paul Bakker9af723c2014-05-01 13:03:14 +0200624 * \param X Destination MPI
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000625 * \param A Left-hand MPI
626 * \param E Exponent MPI
627 * \param N Modular MPI
628 * \param _RR Speed-up MPI used for recalculations
629 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200631 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200633 * if E is negative
Paul Bakker5121ce52009-01-03 21:22:43 +0000634 *
635 * \note _RR is used to avoid re-computing R*R mod N across
636 * multiple calls, which speeds up things a bit. It can
637 * be set to NULL if the extra performance is unneeded.
638 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639int 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 +0000640
641/**
Paul Bakker287781a2011-03-26 13:18:49 +0000642 * \brief Fill an MPI X with size bytes of random
643 *
644 * \param X Destination MPI
645 * \param size Size in bytes
646 * \param f_rng RNG function
647 * \param p_rng RNG parameter
648 *
649 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200650 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Hanno Beckercb2ba292017-10-17 15:17:05 +0100651 *
652 * \note The bytes obtained from the PRNG are interpreted
653 * as a big-endian representation of an MPI; this can
654 * be relevant in applications like deterministic ECDSA.
Paul Bakker287781a2011-03-26 13:18:49 +0000655 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000657 int (*f_rng)(void *, unsigned char *, size_t),
658 void *p_rng );
Paul Bakker287781a2011-03-26 13:18:49 +0000659
660/**
Paul Bakker5121ce52009-01-03 21:22:43 +0000661 * \brief Greatest common divisor: G = gcd(A, B)
662 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000663 * \param G Destination MPI
664 * \param A Left-hand MPI
665 * \param B Right-hand MPI
666 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000667 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200668 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed
Paul Bakker5121ce52009-01-03 21:22:43 +0000669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
Paul Bakker5121ce52009-01-03 21:22:43 +0000671
672/**
673 * \brief Modular inverse: X = A^-1 mod N
674 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000675 * \param X Destination MPI
676 * \param A Left-hand MPI
677 * \param N Right-hand MPI
678 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000679 * \return 0 if successful,
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200680 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Hanno Becker2938ccb2017-04-18 15:49:39 +0100681 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is <= 1,
682 MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N.
Paul Bakker5121ce52009-01-03 21:22:43 +0000683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
Paul Bakker5121ce52009-01-03 21:22:43 +0000685
686/**
687 * \brief Miller-Rabin primality test
688 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000689 * \param X MPI to check
690 * \param f_rng RNG function
691 * \param p_rng RNG parameter
692 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000693 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200694 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime
Paul Bakker5121ce52009-01-03 21:22:43 +0000696 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000698 int (*f_rng)(void *, unsigned char *, size_t),
699 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000700
701/**
702 * \brief Prime number generation
703 *
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000704 * \param X Destination MPI
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200705 * \param nbits Required size of X in bits
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS )
Paul Bakker13e2dfe2009-07-28 07:18:38 +0000707 * \param dh_flag If 1, then (X-1)/2 will be prime too
Paul Bakker5121ce52009-01-03 21:22:43 +0000708 * \param f_rng RNG function
709 * \param p_rng RNG parameter
710 *
711 * \return 0 if successful (probably prime),
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200712 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3
Paul Bakker5121ce52009-01-03 21:22:43 +0000714 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
Paul Bakkera3d195c2011-11-27 21:07:34 +0000716 int (*f_rng)(void *, unsigned char *, size_t),
717 void *p_rng );
Paul Bakker5121ce52009-01-03 21:22:43 +0000718
719/**
720 * \brief Checkup routine
721 *
722 * \return 0 if successful, or 1 if the test failed
723 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724int mbedtls_mpi_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000725
726#ifdef __cplusplus
727}
728#endif
729
730#endif /* bignum.h */