mbed client lightswitch demo

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of mbed-client-classic-example-lwip by Austin Blackstone

Committer:
mbedAustin
Date:
Thu Jun 09 17:08:36 2016 +0000
Revision:
11:cada08fc8a70
Commit for public Consumption

Who changed what in which revision?

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