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