BA / Mbed OS BaBoRo1
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers bignum.h Source File

bignum.h

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