mbed TLS v2.6.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 or 64-bit integer types can be forced regardless of the underlying
107  * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
108  * respectively and undefining MBEDTLS_HAVE_ASM.
109  *
110  * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
111  * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
112  */
113 #if !defined(MBEDTLS_HAVE_INT32)
114  #if defined(_MSC_VER) && defined(_M_AMD64)
115  /* Always choose 64-bit when using MSC */
116  #if !defined(MBEDTLS_HAVE_INT64)
117  #define MBEDTLS_HAVE_INT64
118  #endif /* !MBEDTLS_HAVE_INT64 */
119  typedef int64_t mbedtls_mpi_sint;
120  typedef uint64_t mbedtls_mpi_uint;
121  #elif defined(__GNUC__) && ( \
122  defined(__amd64__) || defined(__x86_64__) || \
123  defined(__ppc64__) || defined(__powerpc64__) || \
124  defined(__ia64__) || defined(__alpha__) || \
125  ( defined(__sparc__) && defined(__arch64__) ) || \
126  defined(__s390x__) || defined(__mips64) )
127  #if !defined(MBEDTLS_HAVE_INT64)
128  #define MBEDTLS_HAVE_INT64
129  #endif /* MBEDTLS_HAVE_INT64 */
130  typedef int64_t mbedtls_mpi_sint;
131  typedef uint64_t mbedtls_mpi_uint;
132  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
133  /* mbedtls_t_udbl defined as 128-bit unsigned int */
134  typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
135  #define MBEDTLS_HAVE_UDBL
136  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
137  #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
138  /*
139  * __ARMCC_VERSION is defined for both armcc and armclang and
140  * __aarch64__ is only defined by armclang when compiling 64-bit code
141  */
142  #if !defined(MBEDTLS_HAVE_INT64)
143  #define MBEDTLS_HAVE_INT64
144  #endif /* !MBEDTLS_HAVE_INT64 */
145  typedef int64_t mbedtls_mpi_sint;
146  typedef uint64_t mbedtls_mpi_uint;
147  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
148  /* mbedtls_t_udbl defined as 128-bit unsigned int */
149  typedef __uint128_t mbedtls_t_udbl;
150  #define MBEDTLS_HAVE_UDBL
151  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
152  #elif defined(MBEDTLS_HAVE_INT64)
153  /* Force 64-bit integers with unknown compiler */
154  typedef int64_t mbedtls_mpi_sint;
155  typedef uint64_t mbedtls_mpi_uint;
156  #endif
157 #endif /* !MBEDTLS_HAVE_INT32 */
158 
159 #if !defined(MBEDTLS_HAVE_INT64)
160  /* Default to 32-bit compilation */
161  #if !defined(MBEDTLS_HAVE_INT32)
162  #define MBEDTLS_HAVE_INT32
163  #endif /* !MBEDTLS_HAVE_INT32 */
164  typedef int32_t mbedtls_mpi_sint;
165  typedef uint32_t mbedtls_mpi_uint;
166  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
167  typedef uint64_t mbedtls_t_udbl;
168  #define MBEDTLS_HAVE_UDBL
169  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
170 #endif /* !MBEDTLS_HAVE_INT64 */
171 
172 #ifdef __cplusplus
173 extern "C" {
174 #endif
175 
179 typedef struct
180 {
181  int s;
182  size_t n;
183  mbedtls_mpi_uint *p;
184 }
186 
194 void mbedtls_mpi_init( mbedtls_mpi *X );
195 
201 void mbedtls_mpi_free( mbedtls_mpi *X );
202 
212 int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
213 
223 int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
224 
234 int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
235 
243 
261 int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
262 
280 int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
281 
291 int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
292 
301 int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
302 
317 int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
318 
327 size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
328 
337 size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
338 
344 size_t mbedtls_mpi_size( const mbedtls_mpi *X );
345 
355 int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
356 
373 int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
374  char *buf, size_t buflen, size_t *olen );
375 
376 #if defined(MBEDTLS_FS_IO)
377 
397 int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
398 
411 int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
412 #endif /* MBEDTLS_FS_IO */
413 
424 int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
425 
438 int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
439 
449 int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
450 
460 int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
461 
472 int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
473 
484 int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
485 
496 int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
497 
508 int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
509 
520 int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
521 
532 int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
533 
544 int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
545 
556 int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
557 
568 int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
569 
580 int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
581 
594 int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
595 
610 int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
611 
626 int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b );
627 
640 int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
641 
654 int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
655 
674 int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR );
675 
687 int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
688  int (*f_rng)(void *, unsigned char *, size_t),
689  void *p_rng );
690 
701 int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
702 
715 int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
716 
728 int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
729  int (*f_rng)(void *, unsigned char *, size_t),
730  void *p_rng );
731 
746 int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
747  int (*f_rng)(void *, unsigned char *, size_t),
748  void *p_rng );
749 
755 int mbedtls_mpi_self_test( int verbose );
756 
757 #ifdef __cplusplus
758 }
759 #endif
760 
761 #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 &#39;1&#39; 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:164
size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
Return the number of bits up to and including the most significant &#39;1&#39; bit&#39;.
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|.
Configuration options (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:167
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:183
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 &gt;&gt;= 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 &lt;&lt;= 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:165
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 &lt;-&gt; 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:179
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.
int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
Read MPI from a line in an opened file.
int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
Write X into an opened file, or stdout if fout is NULL.
size_t n
Definition: bignum.h:182
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.