mbed TLS v2.2.0
bignum.h
Go to the documentation of this file.
1 
23 #ifndef MBEDTLS_BIGNUM_H
24 #define MBEDTLS_BIGNUM_H
25 
26 #if !defined(MBEDTLS_CONFIG_FILE)
27 #include "config.h"
28 #else
29 #include MBEDTLS_CONFIG_FILE
30 #endif
31 
32 #include <stddef.h>
33 #include <stdint.h>
34 
35 #if defined(MBEDTLS_FS_IO)
36 #include <stdio.h>
37 #endif
38 
39 #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002
40 #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004
41 #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006
42 #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008
43 #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A
44 #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C
45 #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E
46 #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010
48 #define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
49 
50 /*
51  * Maximum size MPIs are allowed to grow to in number of limbs.
52  */
53 #define MBEDTLS_MPI_MAX_LIMBS 10000
54 
55 #if !defined(MBEDTLS_MPI_WINDOW_SIZE)
56 /*
57  * Maximum window size used for modular exponentiation. Default: 6
58  * Minimum value: 1. Maximum value: 6.
59  *
60  * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
61  * for the sliding window calculation. (So 64 by default)
62  *
63  * Reduction in size, reduces speed.
64  */
65 #define MBEDTLS_MPI_WINDOW_SIZE 6
66 #endif /* !MBEDTLS_MPI_WINDOW_SIZE */
67 
68 #if !defined(MBEDTLS_MPI_MAX_SIZE)
69 /*
70  * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
71  * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
72  *
73  * Note: Calculations can results temporarily in larger MPIs. So the number
74  * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
75  */
76 #define MBEDTLS_MPI_MAX_SIZE 1024
77 #endif /* !MBEDTLS_MPI_MAX_SIZE */
78 
79 #define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE )
81 /*
82  * When reading from files with mbedtls_mpi_read_file() and writing to files with
83  * mbedtls_mpi_write_file() the buffer should have space
84  * for a (short) label, the MPI (in the provided radix), the newline
85  * characters and the '\0'.
86  *
87  * By default we assume at least a 10 char label, a minimum radix of 10
88  * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
89  * Autosized at compile time for at least a 10 char label, a minimum radix
90  * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
91  *
92  * This used to be statically sized to 1250 for a maximum of 4096 bit
93  * numbers (1234 decimal chars).
94  *
95  * Calculate using the formula:
96  * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
97  * LabelSize + 6
98  */
99 #define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
100 #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
101 #define MBEDTLS_MPI_RW_BUFFER_SIZE ( ((MBEDTLS_MPI_MAX_BITS_SCALE100 + MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
102 
103 /*
104  * Define the base integer type, architecture-wise.
105  *
106  * 32-bit integers can be forced on 64-bit arches (eg. for testing purposes)
107  * by defining MBEDTLS_HAVE_INT32 and undefining MBEDTLS_HAVE_ASM
108  */
109 #if ( ! defined(MBEDTLS_HAVE_INT32) && \
110  defined(_MSC_VER) && defined(_M_AMD64) )
111  #define MBEDTLS_HAVE_INT64
112  typedef int64_t mbedtls_mpi_sint;
113  typedef uint64_t mbedtls_mpi_uint;
114 #else
115  #if ( ! defined(MBEDTLS_HAVE_INT32) && \
116  defined(__GNUC__) && ( \
117  defined(__amd64__) || defined(__x86_64__) || \
118  defined(__ppc64__) || defined(__powerpc64__) || \
119  defined(__ia64__) || defined(__alpha__) || \
120  (defined(__sparc__) && defined(__arch64__)) || \
121  defined(__s390x__) || defined(__mips64) ) )
122  #define MBEDTLS_HAVE_INT64
123  typedef int64_t mbedtls_mpi_sint;
124  typedef uint64_t mbedtls_mpi_uint;
125  typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
126  #define MBEDTLS_HAVE_UDBL
127  #else
128  #define MBEDTLS_HAVE_INT32
129  typedef int32_t mbedtls_mpi_sint;
130  typedef uint32_t mbedtls_mpi_uint;
131  typedef uint64_t mbedtls_t_udbl;
132  #define MBEDTLS_HAVE_UDBL
133  #endif /* !MBEDTLS_HAVE_INT32 && __GNUC__ && 64-bit platform */
134 #endif /* !MBEDTLS_HAVE_INT32 && _MSC_VER && _M_AMD64 */
135 
136 #ifdef __cplusplus
137 extern "C" {
138 #endif
139 
143 typedef struct
144 {
145  int s;
146  size_t n;
147  mbedtls_mpi_uint *p;
148 }
150 
158 void mbedtls_mpi_init( mbedtls_mpi *X );
159 
165 void mbedtls_mpi_free( mbedtls_mpi *X );
166 
176 int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
177 
187 int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
188 
198 int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
199 
207 
225 int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
226 
244 int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
245 
255 int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
256 
265 int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
266 
281 int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
282 
291 size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
292 
301 size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
302 
308 size_t mbedtls_mpi_size( const mbedtls_mpi *X );
309 
319 int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
320 
337 int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
338  char *buf, size_t buflen, size_t *olen );
339 
340 #if defined(MBEDTLS_FS_IO)
341 
352 int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
353 
366 int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
367 #endif /* MBEDTLS_FS_IO */
368 
379 int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
380 
393 int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
394 
404 int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
405 
415 int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
416 
427 int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
428 
439 int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
440 
451 int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
452 
463 int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
464 
475 int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
476 
487 int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
488 
499 int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
500 
511 int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
512 
523 int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
524 
535 int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
536 
549 int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
550 
565 int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
566 
581 int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b );
582 
595 int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
596 
609 int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
610 
629 int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR );
630 
642 int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
643  int (*f_rng)(void *, unsigned char *, size_t),
644  void *p_rng );
645 
656 int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
657 
670 int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
671 
683 int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
684  int (*f_rng)(void *, unsigned char *, size_t),
685  void *p_rng );
686 
701 int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
702  int (*f_rng)(void *, unsigned char *, size_t),
703  void *p_rng );
704 
710 int mbedtls_mpi_self_test( int verbose );
711 
712 #ifdef __cplusplus
713 }
714 #endif
715 
716 #endif /* bignum.h */
int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Division by mbedtls_mpi: A = Q * B + R.
void mbedtls_mpi_free(mbedtls_mpi *X)
Unallocate one MPI.
int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
Greatest common divisor: G = gcd(A, B)
int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Fill an MPI X with size bytes of random.
int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Signed subtraction: X = A - b.
size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
Return the number of zero-bits before the least significant '1' bit.
int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
Enlarge to the specified number of limbs.
int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign)
Safe conditional assignement X = Y if assign is 1.
int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Modulo: R = A mod B.
int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR)
Sliding-window exponentiation: X = A^E mod N.
int32_t mbedtls_mpi_sint
Definition: bignum.h:129
size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
Return the number of bits up to and including the most significant '1' bit'.
int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Compare signed values.
size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Return the total size in bytes.
int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Signed subtraction: X = A - B.
int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Unsigned subtraction: X = |A| - |B|.
Compatibility names (set of defines)
int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
Import from an ASCII string.
uint64_t mbedtls_t_udbl
Definition: bignum.h:131
int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
Set a bit of X to a specific value of 0 or 1.
int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
Set value from integer.
int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Division by int: A = Q * b + R.
int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Import X from unsigned binary data, big endian.
mbedtls_mpi_uint * p
Definition: bignum.h:147
int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
Resize down, keeping at least the specified number of limbs.
int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Signed addition: X = A + b.
int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
Right-shift: X >>= count.
int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int dh_flag, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Prime number generation.
int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Modulo: r = A mod b.
int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
Left-shift: X <<= count.
mbedtls_asn1_buf val
The named value.
Definition: asn1.h:153
int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
Baseline multiplication: X = A * b.
int mbedtls_mpi_is_prime(const mbedtls_mpi *X, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Miller-Rabin primality test.
uint32_t mbedtls_mpi_uint
Definition: bignum.h:130
int mbedtls_mpi_self_test(int verbose)
Checkup routine.
int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign)
Safe conditional swap X <-> Y if swap is 1.
void mbedtls_mpi_init(mbedtls_mpi *X)
Initialize one MPI (make internal references valid) This just makes it ready to be set or freed...
void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
Swap the contents of X and Y.
int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Signed addition: X = A + B.
int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
Modular inverse: X = A^-1 mod N.
MPI structure.
Definition: bignum.h:143
int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
Copy the contents of Y into X.
int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix, char *buf, size_t buflen, size_t *olen)
Export into an ASCII string.
size_t n
Definition: bignum.h:146
int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Unsigned addition: X = |A| + |B|.
int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Compare unsigned values.
int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf, size_t buflen)
Export X into unsigned binary data, big endian.
int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Baseline multiplication: X = A * B.
int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
Compare signed values.
int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
Get a specific bit from X.