bignum.h

Go to the documentation of this file.
00001 
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  
00040 #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA                    -0x0004  
00041 #define MBEDTLS_ERR_MPI_INVALID_CHARACTER                 -0x0006  
00042 #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL                  -0x0008  
00043 #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE                    -0x000A  
00044 #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO                  -0x000C  
00045 #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE                    -0x000E  
00046 #define MBEDTLS_ERR_MPI_ALLOC_FAILED                      -0x0010  
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        
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     
00077 #endif /* !MBEDTLS_MPI_MAX_SIZE */
00078 
00079 #define MBEDTLS_MPI_MAX_BITS                              ( 8 * MBEDTLS_MPI_MAX_SIZE )    
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 
00144 typedef struct
00145 {
00146     int s;              
00147     size_t n;           
00148     mbedtls_mpi_uint *p;          
00149 }
00150 mbedtls_mpi;
00151 
00159 void mbedtls_mpi_init( mbedtls_mpi *X );
00160 
00166 void mbedtls_mpi_free( mbedtls_mpi *X );
00167 
00177 int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
00178 
00188 int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
00189 
00199 int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
00200 
00207 void mbedtls_mpi_swap( mbedtls_mpi *X, mbedtls_mpi *Y );
00208 
00226 int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
00227 
00245 int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
00246 
00256 int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
00257 
00266 int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
00267 
00282 int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
00283 
00292 size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
00293 
00302 size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
00303 
00309 size_t mbedtls_mpi_size( const mbedtls_mpi *X );
00310 
00320 int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
00321 
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 
00353 int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
00354 
00367 int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
00368 #endif /* MBEDTLS_FS_IO */
00369 
00380 int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
00381 
00394 int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
00395 
00405 int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
00406 
00416 int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
00417 
00428 int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
00429 
00440 int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
00441 
00452 int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
00453 
00464 int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
00465 
00476 int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
00477 
00488 int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
00489 
00500 int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
00501 
00512 int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
00513 
00524 int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
00525 
00536 int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
00537 
00550 int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
00551 
00566 int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
00567 
00582 int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b );
00583 
00596 int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
00597 
00610 int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
00611 
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 
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 
00657 int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
00658 
00671 int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
00672 
00684 int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
00685                   int (*f_rng)(void *, unsigned char *, size_t),
00686                   void *p_rng );
00687 
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 
00711 int mbedtls_mpi_self_test( int verbose );
00712 
00713 #ifdef __cplusplus
00714 }
00715 #endif
00716 
00717 #endif /* bignum.h */

Generated on 11 Mar 2017 for mbed TLS v2.4.2 by  doxygen 1.4.7