1 /** 2 * \file bignum.h 3 * 4 * \brief Multi-precision integer library 5 */ 6 /* 7 * Copyright The Mbed TLS Contributors 8 * SPDX-License-Identifier: Apache-2.0 9 * 10 * Licensed under the Apache License, Version 2.0 (the "License"); you may 11 * not use this file except in compliance with the License. 12 * You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, software 17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 19 * See the License for the specific language governing permissions and 20 * limitations under the License. 21 */ 22 #ifndef MBEDTLS_BIGNUM_H 23 #define MBEDTLS_BIGNUM_H 24 25 #if !defined(MBEDTLS_CONFIG_FILE) 26 #include "mbedtls/config.h" 27 #else 28 #include MBEDTLS_CONFIG_FILE 29 #endif 30 31 #include <stddef.h> 32 #include <stdint.h> 33 34 #if defined(MBEDTLS_FS_IO) 35 #include <stdio.h> 36 #endif 37 38 /** An error occurred while reading from or writing to a file. */ 39 #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002 40 /** Bad input parameters to function. */ 41 #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004 42 /** There is an invalid character in the digit string. */ 43 #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006 44 /** The buffer is too small to write to. */ 45 #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008 46 /** The input arguments are negative or result in illegal output. */ 47 #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A 48 /** The input argument for division is zero, which is not allowed. */ 49 #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C 50 /** The input arguments are not acceptable. */ 51 #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E 52 /** Memory allocation failed. */ 53 #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010 54 55 #define MBEDTLS_MPI_CHK(f) \ 56 do \ 57 { \ 58 if( ( ret = (f) ) != 0 ) \ 59 goto cleanup; \ 60 } while( 0 ) 61 62 /* 63 * Maximum size MPIs are allowed to grow to in number of limbs. 64 */ 65 #define MBEDTLS_MPI_MAX_LIMBS 10000 66 67 #if !defined(MBEDTLS_MPI_WINDOW_SIZE) 68 /* 69 * Maximum window size used for modular exponentiation. Default: 6 70 * Minimum value: 1. Maximum value: 6. 71 * 72 * Result is an array of ( 2 ** MBEDTLS_MPI_WINDOW_SIZE ) MPIs used 73 * for the sliding window calculation. (So 64 by default) 74 * 75 * Reduction in size, reduces speed. 76 */ 77 #define MBEDTLS_MPI_WINDOW_SIZE 6 /**< Maximum window size used. */ 78 #endif /* !MBEDTLS_MPI_WINDOW_SIZE */ 79 80 #if !defined(MBEDTLS_MPI_MAX_SIZE) 81 /* 82 * Maximum size of MPIs allowed in bits and bytes for user-MPIs. 83 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits ) 84 * 85 * Note: Calculations can temporarily result in larger MPIs. So the number 86 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher. 87 */ 88 #define MBEDTLS_MPI_MAX_SIZE 1024 /**< Maximum number of bytes for usable MPIs. */ 89 #endif /* !MBEDTLS_MPI_MAX_SIZE */ 90 91 #define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE ) /**< Maximum number of bits for usable MPIs. */ 92 93 /* 94 * When reading from files with mbedtls_mpi_read_file() and writing to files with 95 * mbedtls_mpi_write_file() the buffer should have space 96 * for a (short) label, the MPI (in the provided radix), the newline 97 * characters and the '\0'. 98 * 99 * By default we assume at least a 10 char label, a minimum radix of 10 100 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars). 101 * Autosized at compile time for at least a 10 char label, a minimum radix 102 * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size. 103 * 104 * This used to be statically sized to 1250 for a maximum of 4096 bit 105 * numbers (1234 decimal chars). 106 * 107 * Calculate using the formula: 108 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) + 109 * LabelSize + 6 110 */ 111 #define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS ) 112 #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332 113 #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 ) 114 115 /* 116 * Define the base integer type, architecture-wise. 117 * 118 * 32 or 64-bit integer types can be forced regardless of the underlying 119 * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64 120 * respectively and undefining MBEDTLS_HAVE_ASM. 121 * 122 * Double-width integers (e.g. 128-bit in 64-bit architectures) can be 123 * disabled by defining MBEDTLS_NO_UDBL_DIVISION. 124 */ 125 #if !defined(MBEDTLS_HAVE_INT32) 126 #if defined(_MSC_VER) && defined(_M_AMD64) 127 /* Always choose 64-bit when using MSC */ 128 #if !defined(MBEDTLS_HAVE_INT64) 129 #define MBEDTLS_HAVE_INT64 130 #endif /* !MBEDTLS_HAVE_INT64 */ 131 typedef int64_t mbedtls_mpi_sint; 132 typedef uint64_t mbedtls_mpi_uint; 133 #elif defined(__GNUC__) && ( \ 134 defined(__amd64__) || defined(__x86_64__) || \ 135 defined(__ppc64__) || defined(__powerpc64__) || \ 136 defined(__ia64__) || defined(__alpha__) || \ 137 ( defined(__sparc__) && defined(__arch64__) ) || \ 138 defined(__s390x__) || defined(__mips64) || \ 139 defined(__aarch64__) ) 140 #if !defined(MBEDTLS_HAVE_INT64) 141 #define MBEDTLS_HAVE_INT64 142 #endif /* MBEDTLS_HAVE_INT64 */ 143 typedef int64_t mbedtls_mpi_sint; 144 typedef uint64_t mbedtls_mpi_uint; 145 #if !defined(MBEDTLS_NO_UDBL_DIVISION) 146 /* mbedtls_t_udbl defined as 128-bit unsigned int */ 147 typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI))); 148 #define MBEDTLS_HAVE_UDBL 149 #endif /* !MBEDTLS_NO_UDBL_DIVISION */ 150 #elif defined(__ARMCC_VERSION) && defined(__aarch64__) 151 /* 152 * __ARMCC_VERSION is defined for both armcc and armclang and 153 * __aarch64__ is only defined by armclang when compiling 64-bit code 154 */ 155 #if !defined(MBEDTLS_HAVE_INT64) 156 #define MBEDTLS_HAVE_INT64 157 #endif /* !MBEDTLS_HAVE_INT64 */ 158 typedef int64_t mbedtls_mpi_sint; 159 typedef uint64_t mbedtls_mpi_uint; 160 #if !defined(MBEDTLS_NO_UDBL_DIVISION) 161 /* mbedtls_t_udbl defined as 128-bit unsigned int */ 162 typedef __uint128_t mbedtls_t_udbl; 163 #define MBEDTLS_HAVE_UDBL 164 #endif /* !MBEDTLS_NO_UDBL_DIVISION */ 165 #elif defined(MBEDTLS_HAVE_INT64) 166 /* Force 64-bit integers with unknown compiler */ 167 typedef int64_t mbedtls_mpi_sint; 168 typedef uint64_t mbedtls_mpi_uint; 169 #endif 170 #endif /* !MBEDTLS_HAVE_INT32 */ 171 172 #if !defined(MBEDTLS_HAVE_INT64) 173 /* Default to 32-bit compilation */ 174 #if !defined(MBEDTLS_HAVE_INT32) 175 #define MBEDTLS_HAVE_INT32 176 #endif /* !MBEDTLS_HAVE_INT32 */ 177 typedef int32_t mbedtls_mpi_sint; 178 typedef uint32_t mbedtls_mpi_uint; 179 #if !defined(MBEDTLS_NO_UDBL_DIVISION) 180 typedef uint64_t mbedtls_t_udbl; 181 #define MBEDTLS_HAVE_UDBL 182 #endif /* !MBEDTLS_NO_UDBL_DIVISION */ 183 #endif /* !MBEDTLS_HAVE_INT64 */ 184 185 #ifdef __cplusplus 186 extern "C" { 187 #endif 188 189 /** 190 * \brief MPI structure 191 */ 192 typedef struct mbedtls_mpi 193 { 194 short s; /*!< Sign: -1 if the mpi is negative, 1 otherwise */ 195 short use_mempool; 196 size_t n; /*!< total # of limbs */ 197 mbedtls_mpi_uint *p; /*!< pointer to limbs */ 198 } 199 mbedtls_mpi; 200 201 extern void *mbedtls_mpi_mempool; 202 203 /** 204 * \brief Initialize an MPI context. 205 * 206 * This makes the MPI ready to be set or freed, 207 * but does not define a value for the MPI. 208 * 209 * \param X The MPI context to initialize. This must not be \c NULL. 210 */ 211 void mbedtls_mpi_init( mbedtls_mpi *X ); 212 void mbedtls_mpi_init_mempool( mbedtls_mpi *X ); 213 214 /** 215 * \brief This function frees the components of an MPI context. 216 * 217 * \param X The MPI context to be cleared. This may be \c NULL, 218 * in which case this function is a no-op. If it is 219 * not \c NULL, it must point to an initialized MPI. 220 */ 221 void mbedtls_mpi_free( mbedtls_mpi *X ); 222 223 /** 224 * \brief Enlarge an MPI to the specified number of limbs. 225 * 226 * \note This function does nothing if the MPI is 227 * already large enough. 228 * 229 * \param X The MPI to grow. It must be initialized. 230 * \param nblimbs The target number of limbs. 231 * 232 * \return \c 0 if successful. 233 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed. 234 * \return Another negative error code on other kinds of failure. 235 */ 236 int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs ); 237 238 /** 239 * \brief This function resizes an MPI downwards, keeping at least the 240 * specified number of limbs. 241 * 242 * If \c X is smaller than \c nblimbs, it is resized up 243 * instead. 244 * 245 * \param X The MPI to shrink. This must point to an initialized MPI. 246 * \param nblimbs The minimum number of limbs to keep. 247 * 248 * \return \c 0 if successful. 249 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed 250 * (this can only happen when resizing up). 251 * \return Another negative error code on other kinds of failure. 252 */ 253 int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs ); 254 255 /** 256 * \brief Make a copy of an MPI. 257 * 258 * \param X The destination MPI. This must point to an initialized MPI. 259 * \param Y The source MPI. This must point to an initialized MPI. 260 * 261 * \note The limb-buffer in the destination MPI is enlarged 262 * if necessary to hold the value in the source MPI. 263 * 264 * \return \c 0 if successful. 265 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed. 266 * \return Another negative error code on other kinds of failure. 267 */ 268 int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y ); 269 270 /** 271 * \brief Swap the contents of two MPIs. 272 * 273 * \param X The first MPI. It must be initialized. 274 * \param Y The second MPI. It must be initialized. 275 */ 276 void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y ); 277 278 /** 279 * \brief Perform a safe conditional copy of MPI which doesn't 280 * reveal whether the condition was true or not. 281 * 282 * \param X The MPI to conditionally assign to. This must point 283 * to an initialized MPI. 284 * \param Y The MPI to be assigned from. This must point to an 285 * initialized MPI. 286 * \param assign The condition deciding whether to perform the 287 * assignment or not. Possible values: 288 * * \c 1: Perform the assignment `X = Y`. 289 * * \c 0: Keep the original value of \p X. 290 * 291 * \note This function is equivalent to 292 * `if( assign ) mbedtls_mpi_copy( X, Y );` 293 * except that it avoids leaking any information about whether 294 * the assignment was done or not (the above code may leak 295 * information through branch prediction and/or memory access 296 * patterns analysis). 297 * 298 * \return \c 0 if successful. 299 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed. 300 * \return Another negative error code on other kinds of failure. 301 */ 302 int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign ); 303 304 /** 305 * \brief Perform a safe conditional swap which doesn't 306 * reveal whether the condition was true or not. 307 * 308 * \param X The first MPI. This must be initialized. 309 * \param Y The second MPI. This must be initialized. 310 * \param assign The condition deciding whether to perform 311 * the swap or not. Possible values: 312 * * \c 1: Swap the values of \p X and \p Y. 313 * * \c 0: Keep the original values of \p X and \p Y. 314 * 315 * \note This function is equivalent to 316 * if( assign ) mbedtls_mpi_swap( X, Y ); 317 * except that it avoids leaking any information about whether 318 * the assignment was done or not (the above code may leak 319 * information through branch prediction and/or memory access 320 * patterns analysis). 321 * 322 * \return \c 0 if successful. 323 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed. 324 * \return Another negative error code on other kinds of failure. 325 * 326 */ 327 int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign ); 328 329 /** 330 * \brief Store integer value in MPI. 331 * 332 * \param X The MPI to set. This must be initialized. 333 * \param z The value to use. 334 * 335 * \return \c 0 if successful. 336 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed. 337 * \return Another negative error code on other kinds of failure. 338 */ 339 int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z ); 340 341 /** 342 * \brief Get a specific bit from an MPI. 343 * 344 * \param X The MPI to query. This must be initialized. 345 * \param pos Zero-based index of the bit to query. 346 * 347 * \return \c 0 or \c 1 on success, depending on whether bit \c pos 348 * of \c X is unset or set. 349 * \return A negative error code on failure. 350 */ 351 int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos ); 352 353 /** 354 * \brief Modify a specific bit in an MPI. 355 * 356 * \note This function will grow the target MPI if necessary to set a 357 * bit to \c 1 in a not yet existing limb. It will not grow if 358 * the bit should be set to \c 0. 359 * 360 * \param X The MPI to modify. This must be initialized. 361 * \param pos Zero-based index of the bit to modify. 362 * \param val The desired value of bit \c pos: \c 0 or \c 1. 363 * 364 * \return \c 0 if successful. 365 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed. 366 * \return Another negative error code on other kinds of failure. 367 */ 368 int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val ); 369 370 /** 371 * \brief Return the number of bits of value \c 0 before the 372 * least significant bit of value \c 1. 373 * 374 * \note This is the same as the zero-based index of 375 * the least significant bit of value \c 1. 376 * 377 * \param X The MPI to query. 378 * 379 * \return The number of bits of value \c 0 before the least significant 380 * bit of value \c 1 in \p X. 381 */ 382 size_t mbedtls_mpi_lsb( const mbedtls_mpi *X ); 383 384 /** 385 * \brief Return the number of bits up to and including the most 386 * significant bit of value \c 1. 387 * 388 * * \note This is same as the one-based index of the most 389 * significant bit of value \c 1. 390 * 391 * \param X The MPI to query. This must point to an initialized MPI. 392 * 393 * \return The number of bits up to and including the most 394 * significant bit of value \c 1. 395 */ 396 size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X ); 397 398 /** 399 * \brief Return the total size of an MPI value in bytes. 400 * 401 * \param X The MPI to use. This must point to an initialized MPI. 402 * 403 * \note The value returned by this function may be less than 404 * the number of bytes used to store \p X internally. 405 * This happens if and only if there are trailing bytes 406 * of value zero. 407 * 408 * \return The least number of bytes capable of storing 409 * the absolute value of \p X. 410 */ 411 size_t mbedtls_mpi_size( const mbedtls_mpi *X ); 412 413 /** 414 * \brief Import an MPI from an ASCII string. 415 * 416 * \param X The destination MPI. This must point to an initialized MPI. 417 * \param radix The numeric base of the input string. 418 * \param s Null-terminated string buffer. 419 * 420 * \return \c 0 if successful. 421 * \return A negative error code on failure. 422 */ 423 int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s ); 424 425 /** 426 * \brief Export an MPI to an ASCII string. 427 * 428 * \param X The source MPI. This must point to an initialized MPI. 429 * \param radix The numeric base of the output string. 430 * \param buf The buffer to write the string to. This must be writable 431 * buffer of length \p buflen Bytes. 432 * \param buflen The available size in Bytes of \p buf. 433 * \param olen The address at which to store the length of the string 434 * written, including the final \c NULL byte. This must 435 * not be \c NULL. 436 * 437 * \note You can call this function with `buflen == 0` to obtain the 438 * minimum required buffer size in `*olen`. 439 * 440 * \return \c 0 if successful. 441 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the target buffer \p buf 442 * is too small to hold the value of \p X in the desired base. 443 * In this case, `*olen` is nonetheless updated to contain the 444 * size of \p buf required for a successful call. 445 * \return Another negative error code on different kinds of failure. 446 */ 447 int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix, 448 char *buf, size_t buflen, size_t *olen ); 449 450 #if defined(MBEDTLS_FS_IO) 451 /** 452 * \brief Read an MPI from a line in an opened file. 453 * 454 * \param X The destination MPI. This must point to an initialized MPI. 455 * \param radix The numeric base of the string representation used 456 * in the source line. 457 * \param fin The input file handle to use. This must not be \c NULL. 458 * 459 * \note On success, this function advances the file stream 460 * to the end of the current line or to EOF. 461 * 462 * The function returns \c 0 on an empty line. 463 * 464 * Leading whitespaces are ignored, as is a 465 * '0x' prefix for radix \c 16. 466 * 467 * \return \c 0 if successful. 468 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if the file read buffer 469 * is too small. 470 * \return Another negative error code on failure. 471 */ 472 int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin ); 473 474 /** 475 * \brief Export an MPI into an opened file. 476 * 477 * \param p A string prefix to emit prior to the MPI data. 478 * For example, this might be a label, or "0x" when 479 * printing in base \c 16. This may be \c NULL if no prefix 480 * is needed. 481 * \param X The source MPI. This must point to an initialized MPI. 482 * \param radix The numeric base to be used in the emitted string. 483 * \param fout The output file handle. This may be \c NULL, in which case 484 * the output is written to \c stdout. 485 * 486 * \return \c 0 if successful. 487 * \return A negative error code on failure. 488 */ 489 int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, 490 int radix, FILE *fout ); 491 #endif /* MBEDTLS_FS_IO */ 492 493 /** 494 * \brief Import an MPI from unsigned big endian binary data. 495 * 496 * \param X The destination MPI. This must point to an initialized MPI. 497 * \param buf The input buffer. This must be a readable buffer of length 498 * \p buflen Bytes. 499 * \param buflen The length of the input buffer \p p in Bytes. 500 * 501 * \return \c 0 if successful. 502 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed. 503 * \return Another negative error code on different kinds of failure. 504 */ 505 int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, 506 size_t buflen ); 507 508 /** 509 * \brief Import X from unsigned binary data, little endian 510 * 511 * \param X The destination MPI. This must point to an initialized MPI. 512 * \param buf The input buffer. This must be a readable buffer of length 513 * \p buflen Bytes. 514 * \param buflen The length of the input buffer \p p in Bytes. 515 * 516 * \return \c 0 if successful. 517 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed. 518 * \return Another negative error code on different kinds of failure. 519 */ 520 int mbedtls_mpi_read_binary_le( mbedtls_mpi *X, 521 const unsigned char *buf, size_t buflen ); 522 523 /** 524 * \brief Export X into unsigned binary data, big endian. 525 * Always fills the whole buffer, which will start with zeros 526 * if the number is smaller. 527 * 528 * \param X The source MPI. This must point to an initialized MPI. 529 * \param buf The output buffer. This must be a writable buffer of length 530 * \p buflen Bytes. 531 * \param buflen The size of the output buffer \p buf in Bytes. 532 * 533 * \return \c 0 if successful. 534 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't 535 * large enough to hold the value of \p X. 536 * \return Another negative error code on different kinds of failure. 537 */ 538 int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, 539 size_t buflen ); 540 541 /** 542 * \brief Export X into unsigned binary data, little endian. 543 * Always fills the whole buffer, which will end with zeros 544 * if the number is smaller. 545 * 546 * \param X The source MPI. This must point to an initialized MPI. 547 * \param buf The output buffer. This must be a writable buffer of length 548 * \p buflen Bytes. 549 * \param buflen The size of the output buffer \p buf in Bytes. 550 * 551 * \return \c 0 if successful. 552 * \return #MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL if \p buf isn't 553 * large enough to hold the value of \p X. 554 * \return Another negative error code on different kinds of failure. 555 */ 556 int mbedtls_mpi_write_binary_le( const mbedtls_mpi *X, 557 unsigned char *buf, size_t buflen ); 558 559 /** 560 * \brief Perform a left-shift on an MPI: X <<= count 561 * 562 * \param X The MPI to shift. This must point to an initialized MPI. 563 * \param count The number of bits to shift by. 564 * 565 * \return \c 0 if successful. 566 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 567 * \return Another negative error code on different kinds of failure. 568 */ 569 int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count ); 570 571 /** 572 * \brief Perform a right-shift on an MPI: X >>= count 573 * 574 * \param X The MPI to shift. This must point to an initialized MPI. 575 * \param count The number of bits to shift by. 576 * 577 * \return \c 0 if successful. 578 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 579 * \return Another negative error code on different kinds of failure. 580 */ 581 int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count ); 582 583 /** 584 * \brief Compare the absolute values of two MPIs. 585 * 586 * \param X The left-hand MPI. This must point to an initialized MPI. 587 * \param Y The right-hand MPI. This must point to an initialized MPI. 588 * 589 * \return \c 1 if `|X|` is greater than `|Y|`. 590 * \return \c -1 if `|X|` is lesser than `|Y|`. 591 * \return \c 0 if `|X|` is equal to `|Y|`. 592 */ 593 int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y ); 594 595 /** 596 * \brief Compare two MPIs. 597 * 598 * \param X The left-hand MPI. This must point to an initialized MPI. 599 * \param Y The right-hand MPI. This must point to an initialized MPI. 600 * 601 * \return \c 1 if \p X is greater than \p Y. 602 * \return \c -1 if \p X is lesser than \p Y. 603 * \return \c 0 if \p X is equal to \p Y. 604 */ 605 int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y ); 606 607 /** 608 * \brief Check if an MPI is less than the other in constant time. 609 * 610 * \param X The left-hand MPI. This must point to an initialized MPI 611 * with the same allocated length as Y. 612 * \param Y The right-hand MPI. This must point to an initialized MPI 613 * with the same allocated length as X. 614 * \param ret The result of the comparison: 615 * \c 1 if \p X is less than \p Y. 616 * \c 0 if \p X is greater than or equal to \p Y. 617 * 618 * \return 0 on success. 619 * \return MBEDTLS_ERR_MPI_BAD_INPUT_DATA if the allocated length of 620 * the two input MPIs is not the same. 621 */ 622 int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y, 623 unsigned *ret ); 624 625 /** 626 * \brief Compare an MPI with an integer. 627 * 628 * \param X The left-hand MPI. This must point to an initialized MPI. 629 * \param z The integer value to compare \p X to. 630 * 631 * \return \c 1 if \p X is greater than \p z. 632 * \return \c -1 if \p X is lesser than \p z. 633 * \return \c 0 if \p X is equal to \p z. 634 */ 635 int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z ); 636 637 /** 638 * \brief Perform an unsigned addition of MPIs: X = |A| + |B| 639 * 640 * \param X The destination MPI. This must point to an initialized MPI. 641 * \param A The first summand. This must point to an initialized MPI. 642 * \param B The second summand. This must point to an initialized MPI. 643 * 644 * \return \c 0 if successful. 645 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 646 * \return Another negative error code on different kinds of failure. 647 */ 648 int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, 649 const mbedtls_mpi *B ); 650 651 /** 652 * \brief Perform an unsigned subtraction of MPIs: X = |A| - |B| 653 * 654 * \param X The destination MPI. This must point to an initialized MPI. 655 * \param A The minuend. This must point to an initialized MPI. 656 * \param B The subtrahend. This must point to an initialized MPI. 657 * 658 * \return \c 0 if successful. 659 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p B is greater than \p A. 660 * \return Another negative error code on different kinds of failure. 661 * 662 */ 663 int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, 664 const mbedtls_mpi *B ); 665 666 /** 667 * \brief Perform a signed addition of MPIs: X = A + B 668 * 669 * \param X The destination MPI. This must point to an initialized MPI. 670 * \param A The first summand. This must point to an initialized MPI. 671 * \param B The second summand. This must point to an initialized MPI. 672 * 673 * \return \c 0 if successful. 674 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 675 * \return Another negative error code on different kinds of failure. 676 */ 677 int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, 678 const mbedtls_mpi *B ); 679 680 /** 681 * \brief Perform a signed subtraction of MPIs: X = A - B 682 * 683 * \param X The destination MPI. This must point to an initialized MPI. 684 * \param A The minuend. This must point to an initialized MPI. 685 * \param B The subtrahend. This must point to an initialized MPI. 686 * 687 * \return \c 0 if successful. 688 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 689 * \return Another negative error code on different kinds of failure. 690 */ 691 int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, 692 const mbedtls_mpi *B ); 693 694 /** 695 * \brief Perform a signed addition of an MPI and an integer: X = A + b 696 * 697 * \param X The destination MPI. This must point to an initialized MPI. 698 * \param A The first summand. This must point to an initialized MPI. 699 * \param b The second summand. 700 * 701 * \return \c 0 if successful. 702 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 703 * \return Another negative error code on different kinds of failure. 704 */ 705 int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, 706 mbedtls_mpi_sint b ); 707 708 /** 709 * \brief Perform a signed subtraction of an MPI and an integer: 710 * X = A - b 711 * 712 * \param X The destination MPI. This must point to an initialized MPI. 713 * \param A The minuend. This must point to an initialized MPI. 714 * \param b The subtrahend. 715 * 716 * \return \c 0 if successful. 717 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 718 * \return Another negative error code on different kinds of failure. 719 */ 720 int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, 721 mbedtls_mpi_sint b ); 722 723 /** 724 * \brief Perform a multiplication of two MPIs: X = A * B 725 * 726 * \param X The destination MPI. This must point to an initialized MPI. 727 * \param A The first factor. This must point to an initialized MPI. 728 * \param B The second factor. This must point to an initialized MPI. 729 * 730 * \return \c 0 if successful. 731 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 732 * \return Another negative error code on different kinds of failure. 733 * 734 */ 735 int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, 736 const mbedtls_mpi *B ); 737 738 /** 739 * \brief Perform a multiplication of an MPI with an unsigned integer: 740 * X = A * b 741 * 742 * \param X The destination MPI. This must point to an initialized MPI. 743 * \param A The first factor. This must point to an initialized MPI. 744 * \param b The second factor. 745 * 746 * \return \c 0 if successful. 747 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 748 * \return Another negative error code on different kinds of failure. 749 * 750 */ 751 int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, 752 mbedtls_mpi_uint b ); 753 754 /** 755 * \brief Perform a division with remainder of two MPIs: 756 * A = Q * B + R 757 * 758 * \param Q The destination MPI for the quotient. 759 * This may be \c NULL if the value of the 760 * quotient is not needed. 761 * \param R The destination MPI for the remainder value. 762 * This may be \c NULL if the value of the 763 * remainder is not needed. 764 * \param A The dividend. This must point to an initialized MPi. 765 * \param B The divisor. This must point to an initialized MPI. 766 * 767 * \return \c 0 if successful. 768 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed. 769 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p B equals zero. 770 * \return Another negative error code on different kinds of failure. 771 */ 772 int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, 773 const mbedtls_mpi *B ); 774 775 /** 776 * \brief Perform a division with remainder of an MPI by an integer: 777 * A = Q * b + R 778 * 779 * \param Q The destination MPI for the quotient. 780 * This may be \c NULL if the value of the 781 * quotient is not needed. 782 * \param R The destination MPI for the remainder value. 783 * This may be \c NULL if the value of the 784 * remainder is not needed. 785 * \param A The dividend. This must point to an initialized MPi. 786 * \param b The divisor. 787 * 788 * \return \c 0 if successful. 789 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if memory allocation failed. 790 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p b equals zero. 791 * \return Another negative error code on different kinds of failure. 792 */ 793 int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, 794 mbedtls_mpi_sint b ); 795 796 /** 797 * \brief Perform a modular reduction. R = A mod B 798 * 799 * \param R The destination MPI for the residue value. 800 * This must point to an initialized MPI. 801 * \param A The MPI to compute the residue of. 802 * This must point to an initialized MPI. 803 * \param B The base of the modular reduction. 804 * This must point to an initialized MPI. 805 * 806 * \return \c 0 if successful. 807 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 808 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p B equals zero. 809 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p B is negative. 810 * \return Another negative error code on different kinds of failure. 811 * 812 */ 813 int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, 814 const mbedtls_mpi *B ); 815 816 /** 817 * \brief Perform a modular reduction with respect to an integer. 818 * r = A mod b 819 * 820 * \param r The address at which to store the residue. 821 * This must not be \c NULL. 822 * \param A The MPI to compute the residue of. 823 * This must point to an initialized MPi. 824 * \param b The integer base of the modular reduction. 825 * 826 * \return \c 0 if successful. 827 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 828 * \return #MBEDTLS_ERR_MPI_DIVISION_BY_ZERO if \p b equals zero. 829 * \return #MBEDTLS_ERR_MPI_NEGATIVE_VALUE if \p b is negative. 830 * \return Another negative error code on different kinds of failure. 831 */ 832 int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, 833 mbedtls_mpi_sint b ); 834 835 /** 836 * \brief Perform a sliding-window exponentiation: X = A^E mod N 837 * 838 * \param X The destination MPI. This must point to an initialized MPI. 839 * \param A The base of the exponentiation. 840 * This must point to an initialized MPI. 841 * \param E The exponent MPI. This must point to an initialized MPI. 842 * \param N The base for the modular reduction. This must point to an 843 * initialized MPI. 844 * \param prec_RR A helper MPI depending solely on \p N which can be used to 845 * speed-up multiple modular exponentiations for the same value 846 * of \p N. This may be \c NULL. If it is not \c NULL, it must 847 * point to an initialized MPI. If it hasn't been used after 848 * the call to mbedtls_mpi_init(), this function will compute 849 * the helper value and store it in \p prec_RR for reuse on 850 * subsequent calls to this function. Otherwise, the function 851 * will assume that \p prec_RR holds the helper value set by a 852 * previous call to mbedtls_mpi_exp_mod(), and reuse it. 853 * 854 * \return \c 0 if successful. 855 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 856 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \c N is negative or 857 * even, or if \c E is negative. 858 * \return Another negative error code on different kinds of failures. 859 * 860 */ 861 int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, 862 const mbedtls_mpi *E, const mbedtls_mpi *N, 863 mbedtls_mpi *prec_RR ); 864 865 /** 866 * \brief Fill an MPI with a number of random bytes. 867 * 868 * \param X The destination MPI. This must point to an initialized MPI. 869 * \param size The number of random bytes to generate. 870 * \param f_rng The RNG function to use. This must not be \c NULL. 871 * \param p_rng The RNG parameter to be passed to \p f_rng. This may be 872 * \c NULL if \p f_rng doesn't need a context argument. 873 * 874 * \return \c 0 if successful. 875 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 876 * \return Another negative error code on failure. 877 * 878 * \note The bytes obtained from the RNG are interpreted 879 * as a big-endian representation of an MPI; this can 880 * be relevant in applications like deterministic ECDSA. 881 */ 882 int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size, 883 int (*f_rng)(void *, unsigned char *, size_t), 884 void *p_rng ); 885 886 /** Generate a random number uniformly in a range. 887 * 888 * This function generates a random number between \p min inclusive and 889 * \p N exclusive. 890 * 891 * The procedure complies with RFC 6979 §3.3 (deterministic ECDSA) 892 * when the RNG is a suitably parametrized instance of HMAC_DRBG 893 * and \p min is \c 1. 894 * 895 * \note There are `N - min` possible outputs. The lower bound 896 * \p min can be reached, but the upper bound \p N cannot. 897 * 898 * \param X The destination MPI. This must point to an initialized MPI. 899 * \param min The minimum value to return. 900 * It must be nonnegative. 901 * \param N The upper bound of the range, exclusive. 902 * In other words, this is one plus the maximum value to return. 903 * \p N must be strictly larger than \p min. 904 * \param f_rng The RNG function to use. This must not be \c NULL. 905 * \param p_rng The RNG parameter to be passed to \p f_rng. 906 * 907 * \return \c 0 if successful. 908 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 909 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p min or \p N is invalid 910 * or if they are incompatible. 911 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if the implementation was 912 * unable to find a suitable value within a limited number 913 * of attempts. This has a negligible probability if \p N 914 * is significantly larger than \p min, which is the case 915 * for all usual cryptographic applications. 916 * \return Another negative error code on failure. 917 */ 918 int mbedtls_mpi_random( mbedtls_mpi *X, 919 mbedtls_mpi_sint min, 920 const mbedtls_mpi *N, 921 int (*f_rng)(void *, unsigned char *, size_t), 922 void *p_rng ); 923 924 /** 925 * \brief Compute the greatest common divisor: G = gcd(A, B) 926 * 927 * \param G The destination MPI. This must point to an initialized MPI. 928 * \param A The first operand. This must point to an initialized MPI. 929 * \param B The second operand. This must point to an initialized MPI. 930 * 931 * \return \c 0 if successful. 932 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 933 * \return Another negative error code on different kinds of failure. 934 */ 935 int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, 936 const mbedtls_mpi *B ); 937 938 /** 939 * \brief Compute the modular inverse: X = A^-1 mod N 940 * 941 * \param X The destination MPI. This must point to an initialized MPI. 942 * \param A The MPI to calculate the modular inverse of. This must point 943 * to an initialized MPI. 944 * \param N The base of the modular inversion. This must point to an 945 * initialized MPI. 946 * 947 * \return \c 0 if successful. 948 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 949 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if \p N is less than 950 * or equal to one. 951 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p has no modular inverse 952 * with respect to \p N. 953 */ 954 int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, 955 const mbedtls_mpi *N ); 956 957 #if !defined(MBEDTLS_DEPRECATED_REMOVED) 958 #if defined(MBEDTLS_DEPRECATED_WARNING) 959 #define MBEDTLS_DEPRECATED __attribute__((deprecated)) 960 #else 961 #define MBEDTLS_DEPRECATED 962 #endif 963 /** 964 * \brief Perform a Miller-Rabin primality test with error 965 * probability of 2<sup>-80</sup>. 966 * 967 * \deprecated Superseded by mbedtls_mpi_is_prime_ext() which allows 968 * specifying the number of Miller-Rabin rounds. 969 * 970 * \param X The MPI to check for primality. 971 * This must point to an initialized MPI. 972 * \param f_rng The RNG function to use. This must not be \c NULL. 973 * \param p_rng The RNG parameter to be passed to \p f_rng. 974 * This may be \c NULL if \p f_rng doesn't use a 975 * context parameter. 976 * 977 * \return \c 0 if successful, i.e. \p X is probably prime. 978 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 979 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p X is not prime. 980 * \return Another negative error code on other kinds of failure. 981 */ 982 MBEDTLS_DEPRECATED int mbedtls_mpi_is_prime( const mbedtls_mpi *X, 983 int (*f_rng)(void *, unsigned char *, size_t), 984 void *p_rng ); 985 #undef MBEDTLS_DEPRECATED 986 #endif /* !MBEDTLS_DEPRECATED_REMOVED */ 987 988 /** 989 * \brief Miller-Rabin primality test. 990 * 991 * \warning If \p X is potentially generated by an adversary, for example 992 * when validating cryptographic parameters that you didn't 993 * generate yourself and that are supposed to be prime, then 994 * \p rounds should be at least the half of the security 995 * strength of the cryptographic algorithm. On the other hand, 996 * if \p X is chosen uniformly or non-adversarially (as is the 997 * case when mbedtls_mpi_gen_prime calls this function), then 998 * \p rounds can be much lower. 999 * 1000 * \param X The MPI to check for primality. 1001 * This must point to an initialized MPI. 1002 * \param rounds The number of bases to perform the Miller-Rabin primality 1003 * test for. The probability of returning 0 on a composite is 1004 * at most 2<sup>-2*\p rounds</sup>. 1005 * \param f_rng The RNG function to use. This must not be \c NULL. 1006 * \param p_rng The RNG parameter to be passed to \p f_rng. 1007 * This may be \c NULL if \p f_rng doesn't use 1008 * a context parameter. 1009 * 1010 * \return \c 0 if successful, i.e. \p X is probably prime. 1011 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 1012 * \return #MBEDTLS_ERR_MPI_NOT_ACCEPTABLE if \p X is not prime. 1013 * \return Another negative error code on other kinds of failure. 1014 */ 1015 int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds, 1016 int (*f_rng)(void *, unsigned char *, size_t), 1017 void *p_rng ); 1018 /** 1019 * \brief Flags for mbedtls_mpi_gen_prime() 1020 * 1021 * Each of these flags is a constraint on the result X returned by 1022 * mbedtls_mpi_gen_prime(). 1023 */ 1024 typedef enum { 1025 MBEDTLS_MPI_GEN_PRIME_FLAG_DH = 0x0001, /**< (X-1)/2 is prime too */ 1026 MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR = 0x0002, /**< lower error rate from 2<sup>-80</sup> to 2<sup>-128</sup> */ 1027 } mbedtls_mpi_gen_prime_flag_t; 1028 1029 /** 1030 * \brief Generate a prime number. 1031 * 1032 * \param X The destination MPI to store the generated prime in. 1033 * This must point to an initialized MPi. 1034 * \param nbits The required size of the destination MPI in bits. 1035 * This must be between \c 3 and #MBEDTLS_MPI_MAX_BITS. 1036 * \param flags A mask of flags of type #mbedtls_mpi_gen_prime_flag_t. 1037 * \param f_rng The RNG function to use. This must not be \c NULL. 1038 * \param p_rng The RNG parameter to be passed to \p f_rng. 1039 * This may be \c NULL if \p f_rng doesn't use 1040 * a context parameter. 1041 * 1042 * \return \c 0 if successful, in which case \p X holds a 1043 * probably prime number. 1044 * \return #MBEDTLS_ERR_MPI_ALLOC_FAILED if a memory allocation failed. 1045 * \return #MBEDTLS_ERR_MPI_BAD_INPUT_DATA if `nbits` is not between 1046 * \c 3 and #MBEDTLS_MPI_MAX_BITS. 1047 */ 1048 int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags, 1049 int (*f_rng)(void *, unsigned char *, size_t), 1050 void *p_rng ); 1051 1052 /** 1053 * \brief Montgomery initialization 1054 * 1055 * \param mm The -1/m mod N result 1056 * \param N The modulus 1057 */ 1058 void mbedtls_mpi_montg_init( mbedtls_mpi_uint *mm, const mbedtls_mpi *N ); 1059 1060 /** 1061 * \brief Montgomery multiplication: A = A * B * R^-1 mod N 1062 * \A Parameter and result 1063 * \B Parameter 1064 * \N Modulus 1065 * \mm Parameter from mbedtls_mpi_montg_init() 1066 * \T Temporary variable, should be as twice as big as N + 2 1067 */ 1068 void mbedtls_mpi_montmul( mbedtls_mpi *A, const mbedtls_mpi *B, 1069 const mbedtls_mpi *N, mbedtls_mpi_uint mm, 1070 const mbedtls_mpi *T ); 1071 1072 /** 1073 * \brief Montgomery reduction: A = A * R^-1 mod N 1074 * \A Parameter and result 1075 * \N Modulus 1076 * \mm Parameter from mbedtls_mpi_montg_init() 1077 * \T Temporary variable, should be as twice as big as N + 2 1078 */ 1079 void mbedtls_mpi_montred( mbedtls_mpi *A, const mbedtls_mpi *N, 1080 mbedtls_mpi_uint mm, const mbedtls_mpi *T ); 1081 1082 #if defined(MBEDTLS_SELF_TEST) 1083 1084 /** 1085 * \brief Checkup routine 1086 * 1087 * \return 0 if successful, or 1 if the test failed 1088 */ 1089 int mbedtls_mpi_self_test( int verbose ); 1090 1091 #endif /* MBEDTLS_SELF_TEST */ 1092 1093 #ifdef __cplusplus 1094 } 1095 #endif 1096 1097 #endif /* bignum.h */ 1098