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