Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510
bignum.h
00001 /** 00002 * \file bignum.h 00003 * 00004 * \brief Multi-precision integer library 00005 * 00006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved 00007 * SPDX-License-Identifier: Apache-2.0 00008 * 00009 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00010 * not use this file except in compliance with the License. 00011 * You may obtain a copy of the License at 00012 * 00013 * http://www.apache.org/licenses/LICENSE-2.0 00014 * 00015 * Unless required by applicable law or agreed to in writing, software 00016 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00017 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00018 * See the License for the specific language governing permissions and 00019 * limitations under the License. 00020 * 00021 * This file is part of mbed TLS (https://tls.mbed.org) 00022 */ 00023 #ifndef MBEDTLS_BIGNUM_H 00024 #define MBEDTLS_BIGNUM_H 00025 00026 #if !defined(MBEDTLS_CONFIG_FILE) 00027 #include "config.h" 00028 #else 00029 #include MBEDTLS_CONFIG_FILE 00030 #endif 00031 00032 #include <stddef.h> 00033 #include <stdint.h> 00034 00035 #if defined(MBEDTLS_FS_IO) 00036 #include <stdio.h> 00037 #endif 00038 00039 #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002 /**< An error occurred while reading from or writing to a file. */ 00040 #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004 /**< Bad input parameters to function. */ 00041 #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006 /**< There is an invalid character in the digit string. */ 00042 #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008 /**< The buffer is too small to write to. */ 00043 #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A /**< The input arguments are negative or result in illegal output. */ 00044 #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C /**< The input argument for division is zero, which is not allowed. */ 00045 #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E /**< The input arguments are not acceptable. */ 00046 #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 /**< Memory allocation failed. */ 00047 00048 #define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 ) 00049 00050 /* 00051 * Maximum size MPIs are allowed to grow to in number of limbs. 00052 */ 00053 #define MBEDTLS_MPI_MAX_LIMBS 10000 00054 00055 #if !defined(MBEDTLS_MPI_WINDOW_SIZE) 00056 /* 00057 * Maximum window size used for modular exponentiation. Default: 6 00058 * Minimum value: 1. Maximum value: 6. 00059 * 00060 * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used 00061 * for the sliding window calculation. (So 64 by default) 00062 * 00063 * Reduction in size, reduces speed. 00064 */ 00065 #define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum windows size used. */ 00066 #endif /* !MBEDTLS_MPI_WINDOW_SIZE */ 00067 00068 #if !defined(MBEDTLS_MPI_MAX_SIZE) 00069 /* 00070 * Maximum size of MPIs allowed in bits and bytes for user-MPIs. 00071 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits ) 00072 * 00073 * Note: Calculations can results temporarily in larger MPIs. So the number 00074 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher. 00075 */ 00076 #define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */ 00077 #endif /* !MBEDTLS_MPI_MAX_SIZE */ 00078 00079 #define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */ 00080 00081 /* 00082 * When reading from files with mbedtls_mpi_read_file() and writing to files with 00083 * mbedtls_mpi_write_file() the buffer should have space 00084 * for a (short) label, the MPI (in the provided radix), the newline 00085 * characters and the '\0'. 00086 * 00087 * By default we assume at least a 10 char label, a minimum radix of 10 00088 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars). 00089 * Autosized at compile time for at least a 10 char label, a minimum radix 00090 * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size. 00091 * 00092 * This used to be statically sized to 1250 for a maximum of 4096 bit 00093 * numbers (1234 decimal chars). 00094 * 00095 * Calculate using the formula: 00096 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) + 00097 * LabelSize + 6 00098 */ 00099 #define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS ) 00100 #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332 00101 #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 ) 00102 00103 /* 00104 * Define the base integer type, architecture-wise. 00105 * 00106 * 32-bit integers can be forced on 64-bit arches (eg. for testing purposes) 00107 * by defining MBEDTLS_HAVE_INT32 and undefining MBEDTLS_HAVE_ASM 00108 */ 00109 #if ( ! defined(MBEDTLS_HAVE_INT32) && \ 00110 defined(_MSC_VER) && defined(_M_AMD64) ) 00111 #define MBEDTLS_HAVE_INT64 00112 typedef int64_t mbedtls_mpi_sint; 00113 typedef uint64_t mbedtls_mpi_uint; 00114 #else 00115 #if ( ! defined(MBEDTLS_HAVE_INT32) && \ 00116 defined(__GNUC__) && ( \ 00117 defined(__amd64__) || defined(__x86_64__) || \ 00118 defined(__ppc64__) || defined(__powerpc64__) || \ 00119 defined(__ia64__) || defined(__alpha__) || \ 00120 (defined(__sparc__) && defined(__arch64__)) || \ 00121 defined(__s390x__) || defined(__mips64) ) ) 00122 #define MBEDTLS_HAVE_INT64 00123 typedef int64_t mbedtls_mpi_sint; 00124 typedef uint64_t mbedtls_mpi_uint; 00125 /* mbedtls_t_udbl defined as 128-bit unsigned int */ 00126 typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI))); 00127 #define MBEDTLS_HAVE_UDBL 00128 #else 00129 #define MBEDTLS_HAVE_INT32 00130 typedef int32_t mbedtls_mpi_sint; 00131 typedef uint32_t mbedtls_mpi_uint; 00132 typedef uint64_t mbedtls_t_udbl; 00133 #define MBEDTLS_HAVE_UDBL 00134 #endif /* !MBEDTLS_HAVE_INT32 && __GNUC__ && 64-bit platform */ 00135 #endif /* !MBEDTLS_HAVE_INT32 && _MSC_VER && _M_AMD64 */ 00136 00137 #ifdef __cplusplus 00138 extern "C" { 00139 #endif 00140 00141 /** 00142 * \brief MPI structure 00143 */ 00144 typedef struct 00145 { 00146 int s ; /*!< integer sign */ 00147 size_t n ; /*!< total # of limbs */ 00148 mbedtls_mpi_uint *p ; /*!< pointer to limbs */ 00149 } 00150 mbedtls_mpi; 00151 00152 /** 00153 * \brief Initialize one MPI (make internal references valid) 00154 * This just makes it ready to be set or freed, 00155 * but does not define a value for the MPI. 00156 * 00157 * \param X One MPI to initialize. 00158 */ 00159 void mbedtls_mpi_init( mbedtls_mpi *X ); 00160 00161 /** 00162 * \brief Unallocate one MPI 00163 * 00164 * \param X One MPI to unallocate. 00165 */ 00166 void mbedtls_mpi_free( mbedtls_mpi *X ); 00167 00168 /** 00169 * \brief Enlarge to the specified number of limbs 00170 * 00171 * \param X MPI to grow 00172 * \param nblimbs The target number of limbs 00173 * 00174 * \return 0 if successful, 00175 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed 00176 */ 00177 int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs ); 00178 00179 /** 00180 * \brief Resize down, keeping at least the specified number of limbs 00181 * 00182 * \param X MPI to shrink 00183 * \param nblimbs The minimum number of limbs to keep 00184 * 00185 * \return 0 if successful, 00186 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed 00187 */ 00188 int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs ); 00189 00190 /** 00191 * \brief Copy the contents of Y into X 00192 * 00193 * \param X Destination MPI 00194 * \param Y Source MPI 00195 * 00196 * \return 0 if successful, 00197 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed 00198 */ 00199 int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y ); 00200 00201 /** 00202 * \brief Swap the contents of X and Y 00203 * 00204 * \param X First MPI value 00205 * \param Y Second MPI value 00206 */ 00207 void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y ); 00208 00209 /** 00210 * \brief Safe conditional assignement X = Y if assign is 1 00211 * 00212 * \param X MPI to conditionally assign to 00213 * \param Y Value to be assigned 00214 * \param assign 1: perform the assignment, 0: keep X's original value 00215 * 00216 * \return 0 if successful, 00217 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, 00218 * 00219 * \note This function is equivalent to 00220 * if( assign ) mbedtls_mpi_copy( X, Y ); 00221 * except that it avoids leaking any information about whether 00222 * the assignment was done or not (the above code may leak 00223 * information through branch prediction and/or memory access 00224 * patterns analysis). 00225 */ 00226 int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign ); 00227 00228 /** 00229 * \brief Safe conditional swap X <-> Y if swap is 1 00230 * 00231 * \param X First mbedtls_mpi value 00232 * \param Y Second mbedtls_mpi value 00233 * \param assign 1: perform the swap, 0: keep X and Y's original values 00234 * 00235 * \return 0 if successful, 00236 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, 00237 * 00238 * \note This function is equivalent to 00239 * if( assign ) mbedtls_mpi_swap( X, Y ); 00240 * except that it avoids leaking any information about whether 00241 * the assignment was done or not (the above code may leak 00242 * information through branch prediction and/or memory access 00243 * patterns analysis). 00244 */ 00245 int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign ); 00246 00247 /** 00248 * \brief Set value from integer 00249 * 00250 * \param X MPI to set 00251 * \param z Value to use 00252 * 00253 * \return 0 if successful, 00254 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed 00255 */ 00256 int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z ); 00257 00258 /** 00259 * \brief Get a specific bit from X 00260 * 00261 * \param X MPI to use 00262 * \param pos Zero-based index of the bit in X 00263 * 00264 * \return Either a 0 or a 1 00265 */ 00266 int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos ); 00267 00268 /** 00269 * \brief Set a bit of X to a specific value of 0 or 1 00270 * 00271 * \note Will grow X if necessary to set a bit to 1 in a not yet 00272 * existing limb. Will not grow if bit should be set to 0 00273 * 00274 * \param X MPI to use 00275 * \param pos Zero-based index of the bit in X 00276 * \param val The value to set the bit to (0 or 1) 00277 * 00278 * \return 0 if successful, 00279 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, 00280 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if val is not 0 or 1 00281 */ 00282 int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val ); 00283 00284 /** 00285 * \brief Return the number of zero-bits before the least significant 00286 * '1' bit 00287 * 00288 * Note: Thus also the zero-based index of the least significant '1' bit 00289 * 00290 * \param X MPI to use 00291 */ 00292 size_t mbedtls_mpi_lsb( const mbedtls_mpi *X ); 00293 00294 /** 00295 * \brief Return the number of bits up to and including the most 00296 * significant '1' bit' 00297 * 00298 * Note: Thus also the one-based index of the most significant '1' bit 00299 * 00300 * \param X MPI to use 00301 */ 00302 size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X ); 00303 00304 /** 00305 * \brief Return the total size in bytes 00306 * 00307 * \param X MPI to use 00308 */ 00309 size_t mbedtls_mpi_size( const mbedtls_mpi *X ); 00310 00311 /** 00312 * \brief Import from an ASCII string 00313 * 00314 * \param X Destination MPI 00315 * \param radix Input numeric base 00316 * \param s Null-terminated string buffer 00317 * 00318 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code 00319 */ 00320 int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s ); 00321 00322 /** 00323 * \brief Export into an ASCII string 00324 * 00325 * \param X Source MPI 00326 * \param radix Output numeric base 00327 * \param buf Buffer to write the string to 00328 * \param buflen Length of buf 00329 * \param olen Length of the string written, including final NUL byte 00330 * 00331 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code. 00332 * *olen is always updated to reflect the amount 00333 * of data that has (or would have) been written. 00334 * 00335 * \note Call this function with buflen = 0 to obtain the 00336 * minimum required buffer size in *olen. 00337 */ 00338 int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, 00339 char *buf, size_t buflen, size_t *olen ); 00340 00341 #if defined(MBEDTLS_FS_IO) 00342 /** 00343 * \brief Read X from an opened file 00344 * 00345 * \param X Destination MPI 00346 * \param radix Input numeric base 00347 * \param fin Input file handle 00348 * 00349 * \return 0 if successful, MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if 00350 * the file read buffer is too small or a 00351 * MBEDTLS_ERR_MPI_XXX error code 00352 */ 00353 int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin ); 00354 00355 /** 00356 * \brief Write X into an opened file, or stdout if fout is NULL 00357 * 00358 * \param p Prefix, can be NULL 00359 * \param X Source MPI 00360 * \param radix Output numeric base 00361 * \param fout Output file handle (can be NULL) 00362 * 00363 * \return 0 if successful, or a MBEDTLS_ERR_MPI_XXX error code 00364 * 00365 * \note Set fout == NULL to print X on the console. 00366 */ 00367 int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout ); 00368 #endif /* MBEDTLS_FS_IO */ 00369 00370 /** 00371 * \brief Import X from unsigned binary data, big endian 00372 * 00373 * \param X Destination MPI 00374 * \param buf Input buffer 00375 * \param buflen Input buffer size 00376 * 00377 * \return 0 if successful, 00378 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed 00379 */ 00380 int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen ); 00381 00382 /** 00383 * \brief Export X into unsigned binary data, big endian. 00384 * Always fills the whole buffer, which will start with zeros 00385 * if the number is smaller. 00386 * 00387 * \param X Source MPI 00388 * \param buf Output buffer 00389 * \param buflen Output buffer size 00390 * 00391 * \return 0 if successful, 00392 * MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if buf isn't large enough 00393 */ 00394 int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen ); 00395 00396 /** 00397 * \brief Left-shift: X <<= count 00398 * 00399 * \param X MPI to shift 00400 * \param count Amount to shift 00401 * 00402 * \return 0 if successful, 00403 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed 00404 */ 00405 int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count ); 00406 00407 /** 00408 * \brief Right-shift: X >>= count 00409 * 00410 * \param X MPI to shift 00411 * \param count Amount to shift 00412 * 00413 * \return 0 if successful, 00414 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed 00415 */ 00416 int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count ); 00417 00418 /** 00419 * \brief Compare unsigned values 00420 * 00421 * \param X Left-hand MPI 00422 * \param Y Right-hand MPI 00423 * 00424 * \return 1 if |X| is greater than |Y|, 00425 * -1 if |X| is lesser than |Y| or 00426 * 0 if |X| is equal to |Y| 00427 */ 00428 int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y ); 00429 00430 /** 00431 * \brief Compare signed values 00432 * 00433 * \param X Left-hand MPI 00434 * \param Y Right-hand MPI 00435 * 00436 * \return 1 if X is greater than Y, 00437 * -1 if X is lesser than Y or 00438 * 0 if X is equal to Y 00439 */ 00440 int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y ); 00441 00442 /** 00443 * \brief Compare signed values 00444 * 00445 * \param X Left-hand MPI 00446 * \param z The integer value to compare to 00447 * 00448 * \return 1 if X is greater than z, 00449 * -1 if X is lesser than z or 00450 * 0 if X is equal to z 00451 */ 00452 int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z ); 00453 00454 /** 00455 * \brief Unsigned addition: X = |A| + |B| 00456 * 00457 * \param X Destination MPI 00458 * \param A Left-hand MPI 00459 * \param B Right-hand MPI 00460 * 00461 * \return 0 if successful, 00462 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed 00463 */ 00464 int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ); 00465 00466 /** 00467 * \brief Unsigned subtraction: X = |A| - |B| 00468 * 00469 * \param X Destination MPI 00470 * \param A Left-hand MPI 00471 * \param B Right-hand MPI 00472 * 00473 * \return 0 if successful, 00474 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B is greater than A 00475 */ 00476 int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ); 00477 00478 /** 00479 * \brief Signed addition: X = A + B 00480 * 00481 * \param X Destination MPI 00482 * \param A Left-hand MPI 00483 * \param B Right-hand MPI 00484 * 00485 * \return 0 if successful, 00486 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed 00487 */ 00488 int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ); 00489 00490 /** 00491 * \brief Signed subtraction: X = A - B 00492 * 00493 * \param X Destination MPI 00494 * \param A Left-hand MPI 00495 * \param B Right-hand MPI 00496 * 00497 * \return 0 if successful, 00498 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed 00499 */ 00500 int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ); 00501 00502 /** 00503 * \brief Signed addition: X = A + b 00504 * 00505 * \param X Destination MPI 00506 * \param A Left-hand MPI 00507 * \param b The integer value to add 00508 * 00509 * \return 0 if successful, 00510 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed 00511 */ 00512 int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b ); 00513 00514 /** 00515 * \brief Signed subtraction: X = A - b 00516 * 00517 * \param X Destination MPI 00518 * \param A Left-hand MPI 00519 * \param b The integer value to subtract 00520 * 00521 * \return 0 if successful, 00522 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed 00523 */ 00524 int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b ); 00525 00526 /** 00527 * \brief Baseline multiplication: X = A * B 00528 * 00529 * \param X Destination MPI 00530 * \param A Left-hand MPI 00531 * \param B Right-hand MPI 00532 * 00533 * \return 0 if successful, 00534 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed 00535 */ 00536 int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B ); 00537 00538 /** 00539 * \brief Baseline multiplication: X = A * b 00540 * 00541 * \param X Destination MPI 00542 * \param A Left-hand MPI 00543 * \param b The unsigned integer value to multiply with 00544 * 00545 * \note b is unsigned 00546 * 00547 * \return 0 if successful, 00548 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed 00549 */ 00550 int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b ); 00551 00552 /** 00553 * \brief Division by mbedtls_mpi: A = Q * B + R 00554 * 00555 * \param Q Destination MPI for the quotient 00556 * \param R Destination MPI for the rest value 00557 * \param A Left-hand MPI 00558 * \param B Right-hand MPI 00559 * 00560 * \return 0 if successful, 00561 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, 00562 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0 00563 * 00564 * \note Either Q or R can be NULL. 00565 */ 00566 int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B ); 00567 00568 /** 00569 * \brief Division by int: A = Q * b + R 00570 * 00571 * \param Q Destination MPI for the quotient 00572 * \param R Destination MPI for the rest value 00573 * \param A Left-hand MPI 00574 * \param b Integer to divide by 00575 * 00576 * \return 0 if successful, 00577 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, 00578 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0 00579 * 00580 * \note Either Q or R can be NULL. 00581 */ 00582 int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b ); 00583 00584 /** 00585 * \brief Modulo: R = A mod B 00586 * 00587 * \param R Destination MPI for the rest value 00588 * \param A Left-hand MPI 00589 * \param B Right-hand MPI 00590 * 00591 * \return 0 if successful, 00592 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, 00593 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if B == 0, 00594 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if B < 0 00595 */ 00596 int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B ); 00597 00598 /** 00599 * \brief Modulo: r = A mod b 00600 * 00601 * \param r Destination mbedtls_mpi_uint 00602 * \param A Left-hand MPI 00603 * \param b Integer to divide by 00604 * 00605 * \return 0 if successful, 00606 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, 00607 * MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if b == 0, 00608 * MBEDTLS_ERR_MPI_NEGATIVE_VALUE if b < 0 00609 */ 00610 int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b ); 00611 00612 /** 00613 * \brief Sliding-window exponentiation: X = A^E mod N 00614 * 00615 * \param X Destination MPI 00616 * \param A Left-hand MPI 00617 * \param E Exponent MPI 00618 * \param N Modular MPI 00619 * \param _RR Speed-up MPI used for recalculations 00620 * 00621 * \return 0 if successful, 00622 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, 00623 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or even or 00624 * if E is negative 00625 * 00626 * \note _RR is used to avoid re-computing R*R mod N across 00627 * multiple calls, which speeds up things a bit. It can 00628 * be set to NULL if the extra performance is unneeded. 00629 */ 00630 int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR ); 00631 00632 /** 00633 * \brief Fill an MPI X with size bytes of random 00634 * 00635 * \param X Destination MPI 00636 * \param size Size in bytes 00637 * \param f_rng RNG function 00638 * \param p_rng RNG parameter 00639 * 00640 * \return 0 if successful, 00641 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed 00642 */ 00643 int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size, 00644 int (*f_rng)(void *, unsigned char *, size_t), 00645 void *p_rng ); 00646 00647 /** 00648 * \brief Greatest common divisor: G = gcd(A, B) 00649 * 00650 * \param G Destination MPI 00651 * \param A Left-hand MPI 00652 * \param B Right-hand MPI 00653 * 00654 * \return 0 if successful, 00655 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed 00656 */ 00657 int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B ); 00658 00659 /** 00660 * \brief Modular inverse: X = A^-1 mod N 00661 * 00662 * \param X Destination MPI 00663 * \param A Left-hand MPI 00664 * \param N Right-hand MPI 00665 * 00666 * \return 0 if successful, 00667 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, 00668 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if N is negative or nil 00669 MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if A has no inverse mod N 00670 */ 00671 int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N ); 00672 00673 /** 00674 * \brief Miller-Rabin primality test 00675 * 00676 * \param X MPI to check 00677 * \param f_rng RNG function 00678 * \param p_rng RNG parameter 00679 * 00680 * \return 0 if successful (probably prime), 00681 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, 00682 * MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if X is not prime 00683 */ 00684 int mbedtls_mpi_is_prime( const mbedtls_mpi *X, 00685 int (*f_rng)(void *, unsigned char *, size_t), 00686 void *p_rng ); 00687 00688 /** 00689 * \brief Prime number generation 00690 * 00691 * \param X Destination MPI 00692 * \param nbits Required size of X in bits 00693 * ( 3 <= nbits <= MBEDTLS_MPI_MAX_BITS ) 00694 * \param dh_flag If 1, then (X-1)/2 will be prime too 00695 * \param f_rng RNG function 00696 * \param p_rng RNG parameter 00697 * 00698 * \return 0 if successful (probably prime), 00699 * MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed, 00700 * MBEDTLS_ERR_MPI_BAD_INPUT_DATA if nbits is < 3 00701 */ 00702 int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag, 00703 int (*f_rng)(void *, unsigned char *, size_t), 00704 void *p_rng ); 00705 00706 /** 00707 * \brief Checkup routine 00708 * 00709 * \return 0 if successful, or 1 if the test failed 00710 */ 00711 int mbedtls_mpi_self_test( int verbose ); 00712 00713 #ifdef __cplusplus 00714 } 00715 #endif 00716 00717 #endif /* bignum.h */
Generated on Tue Jul 12 2022 11:02:33 by
