Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file bignum.h |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 3 | * |
Paul Bakker | 37ca75d | 2011-01-06 12:28:03 +0000 | [diff] [blame] | 4 | * \brief Multi-precision integer library |
| 5 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 8 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 9 | * |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 23 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #ifndef MBEDTLS_BIGNUM_H |
| 25 | #define MBEDTLS_BIGNUM_H |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 26 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | #if !defined(MBEDTLS_CONFIG_FILE) |
Paul Bakker | cf0360a | 2012-01-20 10:08:14 +0000 | [diff] [blame] | 28 | #include "config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 31 | #endif |
Paul Bakker | cf0360a | 2012-01-20 10:08:14 +0000 | [diff] [blame] | 32 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 33 | #include <stddef.h> |
Manuel Pégourié-Gonnard | ab22910 | 2015-04-15 11:53:16 +0200 | [diff] [blame] | 34 | #include <stdint.h> |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 36 | #if defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | e94e6e5 | 2015-01-19 15:01:53 +0000 | [diff] [blame] | 37 | #include <stdio.h> |
| 38 | #endif |
| 39 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */ |
| 41 | #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */ |
| 42 | #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */ |
| 43 | #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */ |
| 44 | #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */ |
| 45 | #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */ |
| 46 | #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */ |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 47 | #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 /**< Memory allocation failed. */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | #define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 50 | |
| 51 | /* |
Paul Bakker | f968857 | 2011-05-05 10:00:45 +0000 | [diff] [blame] | 52 | * Maximum size MPIs are allowed to grow to in number of limbs. |
| 53 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | #define MBEDTLS_MPI_MAX_LIMBS 10000 |
Paul Bakker | f968857 | 2011-05-05 10:00:45 +0000 | [diff] [blame] | 55 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | #if !defined(MBEDTLS_MPI_WINDOW_SIZE) |
Paul Bakker | f968857 | 2011-05-05 10:00:45 +0000 | [diff] [blame] | 57 | /* |
Paul Bakker | b6d5f08 | 2011-11-25 11:52:11 +0000 | [diff] [blame] | 58 | * Maximum window size used for modular exponentiation. Default: 6 |
| 59 | * Minimum value: 1. Maximum value: 6. |
| 60 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used |
Paul Bakker | b6d5f08 | 2011-11-25 11:52:11 +0000 | [diff] [blame] | 62 | * for the sliding window calculation. (So 64 by default) |
| 63 | * |
| 64 | * Reduction in size, reduces speed. |
| 65 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | #define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */ |
| 67 | #endif /* !MBEDTLS_MPI_WINDOW_SIZE */ |
Paul Bakker | b6d5f08 | 2011-11-25 11:52:11 +0000 | [diff] [blame] | 68 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 69 | #if !defined(MBEDTLS_MPI_MAX_SIZE) |
Paul Bakker | b6d5f08 | 2011-11-25 11:52:11 +0000 | [diff] [blame] | 70 | /* |
Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 71 | * Maximum size of MPIs allowed in bits and bytes for user-MPIs. |
Paul Bakker | f626e1d | 2013-01-21 12:10:00 +0100 | [diff] [blame] | 72 | * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits ) |
Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 73 | * |
| 74 | * Note: Calculations can results temporarily in larger MPIs. So the number |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 75 | * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher. |
Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 76 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 77 | #define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */ |
| 78 | #endif /* !MBEDTLS_MPI_MAX_SIZE */ |
Paul Bakker | 9bcf16c | 2013-06-24 19:31:17 +0200 | [diff] [blame] | 79 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 80 | #define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */ |
Paul Bakker | fe3256e | 2011-11-25 12:11:43 +0000 | [diff] [blame] | 81 | |
| 82 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 83 | * When reading from files with mbedtls_mpi_read_file() and writing to files with |
| 84 | * mbedtls_mpi_write_file() the buffer should have space |
Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 85 | * for a (short) label, the MPI (in the provided radix), the newline |
| 86 | * characters and the '\0'. |
| 87 | * |
| 88 | * By default we assume at least a 10 char label, a minimum radix of 10 |
| 89 | * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars). |
Paul Bakker | f918310 | 2012-09-27 20:42:35 +0000 | [diff] [blame] | 90 | * Autosized at compile time for at least a 10 char label, a minimum radix |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 91 | * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size. |
Paul Bakker | f918310 | 2012-09-27 20:42:35 +0000 | [diff] [blame] | 92 | * |
| 93 | * This used to be statically sized to 1250 for a maximum of 4096 bit |
| 94 | * numbers (1234 decimal chars). |
| 95 | * |
| 96 | * Calculate using the formula: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 97 | * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) + |
Paul Bakker | f918310 | 2012-09-27 20:42:35 +0000 | [diff] [blame] | 98 | * LabelSize + 6 |
Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 99 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 100 | #define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS ) |
| 101 | #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332 |
| 102 | #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 Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 103 | |
| 104 | /* |
Manuel Pégourié-Gonnard | 7b53889 | 2015-04-09 17:00:17 +0200 | [diff] [blame] | 105 | * Define the base integer type, architecture-wise. |
| 106 | * |
| 107 | * 32-bit integers can be forced on 64-bit arches (eg. for testing purposes) |
| 108 | * by defining MBEDTLS_HAVE_INT32 and undefining MBEDTLS_HAVE_ASM |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 109 | */ |
Manuel Pégourié-Gonnard | 7b53889 | 2015-04-09 17:00:17 +0200 | [diff] [blame] | 110 | #if ( ! defined(MBEDTLS_HAVE_INT32) && \ |
| 111 | defined(_MSC_VER) && defined(_M_AMD64) ) |
| 112 | #define MBEDTLS_HAVE_INT64 |
| 113 | typedef int64_t mbedtls_mpi_sint; |
| 114 | typedef uint64_t mbedtls_mpi_uint; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 115 | #else |
Manuel Pégourié-Gonnard | 7b53889 | 2015-04-09 17:00:17 +0200 | [diff] [blame] | 116 | #if ( ! defined(MBEDTLS_HAVE_INT32) && \ |
| 117 | defined(__GNUC__) && ( \ |
| 118 | defined(__amd64__) || defined(__x86_64__) || \ |
| 119 | defined(__ppc64__) || defined(__powerpc64__) || \ |
| 120 | defined(__ia64__) || defined(__alpha__) || \ |
| 121 | (defined(__sparc__) && defined(__arch64__)) || \ |
| 122 | defined(__s390x__) || defined(__mips64) ) ) |
| 123 | #define MBEDTLS_HAVE_INT64 |
| 124 | typedef int64_t mbedtls_mpi_sint; |
| 125 | typedef uint64_t mbedtls_mpi_uint; |
| 126 | typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI))); |
| 127 | #define MBEDTLS_HAVE_UDBL |
Paul Bakker | 62261d6 | 2012-10-02 12:19:31 +0000 | [diff] [blame] | 128 | #else |
Manuel Pégourié-Gonnard | 7b53889 | 2015-04-09 17:00:17 +0200 | [diff] [blame] | 129 | #define MBEDTLS_HAVE_INT32 |
| 130 | typedef int32_t mbedtls_mpi_sint; |
| 131 | typedef uint32_t mbedtls_mpi_uint; |
Manuel Pégourié-Gonnard | 975d5fa | 2015-04-09 17:13:45 +0200 | [diff] [blame] | 132 | typedef uint64_t mbedtls_t_udbl; |
| 133 | #define MBEDTLS_HAVE_UDBL |
Manuel Pégourié-Gonnard | 7b53889 | 2015-04-09 17:00:17 +0200 | [diff] [blame] | 134 | #endif /* !MBEDTLS_HAVE_INT32 && __GNUC__ && 64-bit platform */ |
| 135 | #endif /* !MBEDTLS_HAVE_INT32 && _MSC_VER && _M_AMD64 */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 136 | |
Paul Bakker | 407a0da | 2013-06-27 14:29:21 +0200 | [diff] [blame] | 137 | #ifdef __cplusplus |
| 138 | extern "C" { |
| 139 | #endif |
| 140 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 | /** |
| 142 | * \brief MPI structure |
| 143 | */ |
| 144 | typedef struct |
| 145 | { |
| 146 | int s; /*!< integer sign */ |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 147 | size_t n; /*!< total # of limbs */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 148 | mbedtls_mpi_uint *p; /*!< pointer to limbs */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 149 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | mbedtls_mpi; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 151 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 152 | /** |
Manuel Pégourié-Gonnard | da61ed3 | 2015-04-30 10:28:51 +0200 | [diff] [blame] | 153 | * \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 Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 156 | * |
| 157 | * \param X One MPI to initialize. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 158 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | void mbedtls_mpi_init( mbedtls_mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 160 | |
| 161 | /** |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 162 | * \brief Unallocate one MPI |
| 163 | * |
| 164 | * \param X One MPI to unallocate. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 165 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | void mbedtls_mpi_free( mbedtls_mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 167 | |
| 168 | /** |
| 169 | * \brief Enlarge to the specified number of limbs |
| 170 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 171 | * \param X MPI to grow |
| 172 | * \param nblimbs The target number of limbs |
| 173 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 174 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 175 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 176 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 178 | |
| 179 | /** |
Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 180 | * \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é-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 186 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 187 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs ); |
Manuel Pégourié-Gonnard | 5868163 | 2013-11-21 10:39:37 +0100 | [diff] [blame] | 189 | |
| 190 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 191 | * \brief Copy the contents of Y into X |
| 192 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 193 | * \param X Destination MPI |
| 194 | * \param Y Source MPI |
| 195 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 196 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 197 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 198 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 200 | |
| 201 | /** |
| 202 | * \brief Swap the contents of X and Y |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 203 | * |
| 204 | * \param X First MPI value |
| 205 | * \param Y Second MPI value |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 206 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 207 | void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 208 | |
| 209 | /** |
Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 210 | * \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é-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 214 | * \param assign 1: perform the assignment, 0: keep X's original value |
Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 215 | * |
| 216 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 217 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, |
Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 218 | * |
| 219 | * \note This function is equivalent to |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 220 | * if( assign ) mbedtls_mpi_copy( X, Y ); |
Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 221 | * except that it avoids leaking any information about whether |
| 222 | * the assignment was done or not (the above code may leak |
Manuel Pégourié-Gonnard | d728350 | 2013-11-21 20:00:38 +0100 | [diff] [blame] | 223 | * information through branch prediction and/or memory access |
| 224 | * patterns analysis). |
Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 225 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 226 | int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign ); |
Manuel Pégourié-Gonnard | 71c2c21 | 2013-11-21 16:56:39 +0100 | [diff] [blame] | 227 | |
| 228 | /** |
Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 229 | * \brief Safe conditional swap X <-> Y if swap is 1 |
| 230 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 231 | * \param X First mbedtls_mpi value |
| 232 | * \param Y Second mbedtls_mpi value |
Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 233 | * \param assign 1: perform the swap, 0: keep X and Y's original values |
| 234 | * |
| 235 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 236 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, |
Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 237 | * |
| 238 | * \note This function is equivalent to |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 239 | * if( assign ) mbedtls_mpi_swap( X, Y ); |
Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 240 | * 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é-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 245 | int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign ); |
Manuel Pégourié-Gonnard | a60fe89 | 2013-12-04 21:41:50 +0100 | [diff] [blame] | 246 | |
| 247 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 248 | * \brief Set value from integer |
| 249 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 250 | * \param X MPI to set |
| 251 | * \param z Value to use |
| 252 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 253 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 254 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 255 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 256 | int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 257 | |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 258 | /** |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 259 | * \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é-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 266 | int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos ); |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 267 | |
Paul Bakker | 9a73632 | 2012-11-14 12:39:52 +0000 | [diff] [blame] | 268 | /** |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 269 | * \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é-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 279 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1 |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 281 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 282 | int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val ); |
Paul Bakker | 2f5947e | 2011-05-18 15:47:11 +0000 | [diff] [blame] | 283 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 284 | /** |
Paul Bakker | 6b906e5 | 2012-05-08 12:01:43 +0000 | [diff] [blame] | 285 | * \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 Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 289 | * |
| 290 | * \param X MPI to use |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 291 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 292 | size_t mbedtls_mpi_lsb( const mbedtls_mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 293 | |
| 294 | /** |
Paul Bakker | 6b906e5 | 2012-05-08 12:01:43 +0000 | [diff] [blame] | 295 | * \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 Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 299 | * |
| 300 | * \param X MPI to use |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 301 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 302 | size_t mbedtls_mpi_msb( const mbedtls_mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 303 | |
| 304 | /** |
| 305 | * \brief Return the total size in bytes |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 306 | * |
| 307 | * \param X MPI to use |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 308 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 309 | size_t mbedtls_mpi_size( const mbedtls_mpi *X ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 310 | |
| 311 | /** |
| 312 | * \brief Import from an ASCII string |
| 313 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 314 | * \param X Destination MPI |
| 315 | * \param radix Input numeric base |
| 316 | * \param s Null-terminated string buffer |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 317 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 318 | * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 319 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 320 | int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 321 | |
| 322 | /** |
| 323 | * \brief Export into an ASCII string |
| 324 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 325 | * \param X Source MPI |
| 326 | * \param radix Output numeric base |
Manuel Pégourié-Gonnard | f79b425 | 2015-06-02 15:41:48 +0100 | [diff] [blame^] | 327 | * \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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 330 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 331 | * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code. |
Manuel Pégourié-Gonnard | f79b425 | 2015-06-02 15:41:48 +0100 | [diff] [blame^] | 332 | * *olen is always updated to reflect the amount |
Paul Bakker | ff60ee6 | 2010-03-16 21:09:09 +0000 | [diff] [blame] | 333 | * of data that has (or would have) been written. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 334 | * |
Manuel Pégourié-Gonnard | f79b425 | 2015-06-02 15:41:48 +0100 | [diff] [blame^] | 335 | * \note Call this function with buflen = 0 to obtain the |
| 336 | * minimum required buffer size in *olen. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 337 | */ |
Manuel Pégourié-Gonnard | f79b425 | 2015-06-02 15:41:48 +0100 | [diff] [blame^] | 338 | int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, |
| 339 | char *buf, size_t buflen, size_t *olen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 340 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 341 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 342 | /** |
| 343 | * \brief Read X from an opened file |
| 344 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 345 | * \param X Destination MPI |
| 346 | * \param radix Input numeric base |
| 347 | * \param fin Input file handle |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 348 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 349 | * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if |
Paul Bakker | cb37aa5 | 2011-11-30 16:00:20 +0000 | [diff] [blame] | 350 | * the file read buffer is too small or a |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 351 | * MBEDTLS_ERR_MPI_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 352 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 353 | int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 354 | |
| 355 | /** |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 356 | * \brief Write X into an opened file, or stdout if fout is NULL |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 357 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 358 | * \param p Prefix, can be NULL |
| 359 | * \param X Source MPI |
| 360 | * \param radix Output numeric base |
| 361 | * \param fout Output file handle (can be NULL) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 362 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 363 | * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 364 | * |
| 365 | * \note Set fout == NULL to print X on the console. |
| 366 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 367 | int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout ); |
| 368 | #endif /* MBEDTLS_FS_IO */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 369 | |
| 370 | /** |
| 371 | * \brief Import X from unsigned binary data, big endian |
| 372 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 373 | * \param X Destination MPI |
| 374 | * \param buf Input buffer |
| 375 | * \param buflen Input buffer size |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 376 | * |
| 377 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 378 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 379 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 380 | int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 381 | |
| 382 | /** |
Manuel Pégourié-Gonnard | 3926a2c | 2014-06-25 12:57:47 +0200 | [diff] [blame] | 383 | * \brief Export X into unsigned binary data, big endian. |
| 384 | * Always fills the whole buffer, which will start with zeros |
| 385 | * if the number is smaller. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 386 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 387 | * \param X Source MPI |
| 388 | * \param buf Output buffer |
| 389 | * \param buflen Output buffer size |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 390 | * |
| 391 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 392 | * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 393 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 394 | int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 395 | |
| 396 | /** |
| 397 | * \brief Left-shift: X <<= count |
| 398 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 399 | * \param X MPI to shift |
| 400 | * \param count Amount to shift |
| 401 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 402 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 403 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 404 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 405 | int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 406 | |
| 407 | /** |
| 408 | * \brief Right-shift: X >>= count |
| 409 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 410 | * \param X MPI to shift |
| 411 | * \param count Amount to shift |
| 412 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 413 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 414 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 415 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 416 | int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 417 | |
| 418 | /** |
| 419 | * \brief Compare unsigned values |
| 420 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 421 | * \param X Left-hand MPI |
| 422 | * \param Y Right-hand MPI |
| 423 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 424 | * \return 1 if |X| is greater than |Y|, |
| 425 | * -1 if |X| is lesser than |Y| or |
| 426 | * 0 if |X| is equal to |Y| |
| 427 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 428 | int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 429 | |
| 430 | /** |
| 431 | * \brief Compare signed values |
| 432 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 433 | * \param X Left-hand MPI |
| 434 | * \param Y Right-hand MPI |
| 435 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 436 | * \return 1 if X is greater than Y, |
| 437 | * -1 if X is lesser than Y or |
| 438 | * 0 if X is equal to Y |
| 439 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 440 | int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 441 | |
| 442 | /** |
| 443 | * \brief Compare signed values |
| 444 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 445 | * \param X Left-hand MPI |
| 446 | * \param z The integer value to compare to |
| 447 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 448 | * \return 1 if X is greater than z, |
| 449 | * -1 if X is lesser than z or |
| 450 | * 0 if X is equal to z |
| 451 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 452 | int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 453 | |
| 454 | /** |
| 455 | * \brief Unsigned addition: X = |A| + |B| |
| 456 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 457 | * \param X Destination MPI |
| 458 | * \param A Left-hand MPI |
| 459 | * \param B Right-hand MPI |
| 460 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 461 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 462 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 463 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 464 | int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 465 | |
| 466 | /** |
Paul Bakker | 60b1d10 | 2013-10-29 10:02:51 +0100 | [diff] [blame] | 467 | * \brief Unsigned subtraction: X = |A| - |B| |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 468 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 469 | * \param X Destination MPI |
| 470 | * \param A Left-hand MPI |
| 471 | * \param B Right-hand MPI |
| 472 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 473 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 474 | * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 475 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 476 | int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 477 | |
| 478 | /** |
| 479 | * \brief Signed addition: X = A + B |
| 480 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 481 | * \param X Destination MPI |
| 482 | * \param A Left-hand MPI |
| 483 | * \param B Right-hand MPI |
| 484 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 485 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 486 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 487 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 488 | int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 489 | |
| 490 | /** |
Paul Bakker | 60b1d10 | 2013-10-29 10:02:51 +0100 | [diff] [blame] | 491 | * \brief Signed subtraction: X = A - B |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 492 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 493 | * \param X Destination MPI |
| 494 | * \param A Left-hand MPI |
| 495 | * \param B Right-hand MPI |
| 496 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 497 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 498 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 499 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 500 | int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 501 | |
| 502 | /** |
| 503 | * \brief Signed addition: X = A + b |
| 504 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 505 | * \param X Destination MPI |
| 506 | * \param A Left-hand MPI |
| 507 | * \param b The integer value to add |
| 508 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 509 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 510 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 511 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 512 | int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 513 | |
| 514 | /** |
Paul Bakker | 60b1d10 | 2013-10-29 10:02:51 +0100 | [diff] [blame] | 515 | * \brief Signed subtraction: X = A - b |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 516 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 517 | * \param X Destination MPI |
| 518 | * \param A Left-hand MPI |
| 519 | * \param b The integer value to subtract |
| 520 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 521 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 522 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 523 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 524 | int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 525 | |
| 526 | /** |
| 527 | * \brief Baseline multiplication: X = A * B |
| 528 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 529 | * \param X Destination MPI |
| 530 | * \param A Left-hand MPI |
| 531 | * \param B Right-hand MPI |
| 532 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 533 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 534 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 535 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 536 | int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 537 | |
| 538 | /** |
| 539 | * \brief Baseline multiplication: X = A * b |
| 540 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 541 | * \param X Destination MPI |
| 542 | * \param A Left-hand MPI |
Manuel Pégourié-Gonnard | 35f1d7f | 2015-03-19 12:42:40 +0000 | [diff] [blame] | 543 | * \param b The unsigned integer value to multiply with |
| 544 | * |
| 545 | * \note b is unsigned |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 546 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 547 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 548 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 549 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 550 | int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 551 | |
| 552 | /** |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 553 | * \brief Division by mbedtls_mpi: A = Q * B + R |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 554 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 555 | * \param Q Destination MPI for the quotient |
| 556 | * \param R Destination MPI for the rest value |
| 557 | * \param A Left-hand MPI |
| 558 | * \param B Right-hand MPI |
| 559 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 560 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 561 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 562 | * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 563 | * |
| 564 | * \note Either Q or R can be NULL. |
| 565 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 566 | int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 567 | |
| 568 | /** |
| 569 | * \brief Division by int: A = Q * b + R |
| 570 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 571 | * \param Q Destination MPI for the quotient |
| 572 | * \param R Destination MPI for the rest value |
| 573 | * \param A Left-hand MPI |
| 574 | * \param b Integer to divide by |
| 575 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 576 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 577 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 578 | * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 579 | * |
| 580 | * \note Either Q or R can be NULL. |
| 581 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 582 | int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 583 | |
| 584 | /** |
| 585 | * \brief Modulo: R = A mod B |
| 586 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 587 | * \param R Destination MPI for the rest value |
| 588 | * \param A Left-hand MPI |
| 589 | * \param B Right-hand MPI |
| 590 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 591 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 592 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 593 | * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0, |
| 594 | * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 595 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 596 | int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 597 | |
| 598 | /** |
| 599 | * \brief Modulo: r = A mod b |
| 600 | * |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 601 | * \param r Destination mbedtls_mpi_uint |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 602 | * \param A Left-hand MPI |
| 603 | * \param b Integer to divide by |
| 604 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 605 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 606 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 607 | * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0, |
| 608 | * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 609 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 610 | int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 611 | |
| 612 | /** |
| 613 | * \brief Sliding-window exponentiation: X = A^E mod N |
| 614 | * |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 615 | * \param X Destination MPI |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 616 | * \param A Left-hand MPI |
| 617 | * \param E Exponent MPI |
| 618 | * \param N Modular MPI |
| 619 | * \param _RR Speed-up MPI used for recalculations |
| 620 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 621 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 622 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 623 | * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 624 | * if E is negative |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 625 | * |
| 626 | * \note _RR is used to avoid re-computing R*R mod N across |
| 627 | * multiple calls, which speeds up things a bit. It can |
| 628 | * be set to NULL if the extra performance is unneeded. |
| 629 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 630 | int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 631 | |
| 632 | /** |
Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 633 | * \brief Fill an MPI X with size bytes of random |
| 634 | * |
| 635 | * \param X Destination MPI |
| 636 | * \param size Size in bytes |
| 637 | * \param f_rng RNG function |
| 638 | * \param p_rng RNG parameter |
| 639 | * |
| 640 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 641 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 642 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 643 | int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 644 | int (*f_rng)(void *, unsigned char *, size_t), |
| 645 | void *p_rng ); |
Paul Bakker | 287781a | 2011-03-26 13:18:49 +0000 | [diff] [blame] | 646 | |
| 647 | /** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 648 | * \brief Greatest common divisor: G = gcd(A, B) |
| 649 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 650 | * \param G Destination MPI |
| 651 | * \param A Left-hand MPI |
| 652 | * \param B Right-hand MPI |
| 653 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 654 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 655 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 656 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 657 | int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 658 | |
| 659 | /** |
| 660 | * \brief Modular inverse: X = A^-1 mod N |
| 661 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 662 | * \param X Destination MPI |
| 663 | * \param A Left-hand MPI |
| 664 | * \param N Right-hand MPI |
| 665 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 666 | * \return 0 if successful, |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 667 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 668 | * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or nil |
| 669 | MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 670 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 671 | int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 672 | |
| 673 | /** |
| 674 | * \brief Miller-Rabin primality test |
| 675 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 676 | * \param X MPI to check |
| 677 | * \param f_rng RNG function |
| 678 | * \param p_rng RNG parameter |
| 679 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 680 | * \return 0 if successful (probably prime), |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 681 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 682 | * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 683 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 684 | int mbedtls_mpi_is_prime( const mbedtls_mpi *X, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 685 | int (*f_rng)(void *, unsigned char *, size_t), |
| 686 | void *p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 687 | |
| 688 | /** |
| 689 | * \brief Prime number generation |
| 690 | * |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 691 | * \param X Destination MPI |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 692 | * \param nbits Required size of X in bits |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 693 | * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS ) |
Paul Bakker | 13e2dfe | 2009-07-28 07:18:38 +0000 | [diff] [blame] | 694 | * \param dh_flag If 1, then (X-1)/2 will be prime too |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 695 | * \param f_rng RNG function |
| 696 | * \param p_rng RNG parameter |
| 697 | * |
| 698 | * \return 0 if successful (probably prime), |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 699 | * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 700 | * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3 |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 701 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 702 | int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag, |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 703 | int (*f_rng)(void *, unsigned char *, size_t), |
| 704 | void *p_rng ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 705 | |
| 706 | /** |
| 707 | * \brief Checkup routine |
| 708 | * |
| 709 | * \return 0 if successful, or 1 if the test failed |
| 710 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 711 | int mbedtls_mpi_self_test( int verbose ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 712 | |
| 713 | #ifdef __cplusplus |
| 714 | } |
| 715 | #endif |
| 716 | |
| 717 | #endif /* bignum.h */ |