mbed TLS v2.6.0
bignum.h
Go to the documentation of this file.
1 
25 #ifndef MBEDTLS_BIGNUM_H
26 #define MBEDTLS_BIGNUM_H
27 
28 #if !defined(MBEDTLS_CONFIG_FILE)
29 #include "config.h"
30 #else
31 #include MBEDTLS_CONFIG_FILE
32 #endif
33 
34 #include <stddef.h>
35 #include <stdint.h>
36 
37 #if defined(MBEDTLS_FS_IO)
38 #include <stdio.h>
39 #endif
40 
41 #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002
42 #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004
43 #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006
44 #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008
45 #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A
46 #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C
47 #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E
48 #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010
50 #define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
51 
52 /*
53  * Maximum size MPIs are allowed to grow to in number of limbs.
54  */
55 #define MBEDTLS_MPI_MAX_LIMBS 10000
56 
57 #if !defined(MBEDTLS_MPI_WINDOW_SIZE)
58 /*
59  * Maximum window size used for modular exponentiation. Default: 6
60  * Minimum value: 1. Maximum value: 6.
61  *
62  * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
63  * for the sliding window calculation. (So 64 by default)
64  *
65  * Reduction in size, reduces speed.
66  */
67 #define MBEDTLS_MPI_WINDOW_SIZE 6
68 #endif /* !MBEDTLS_MPI_WINDOW_SIZE */
69 
70 #if !defined(MBEDTLS_MPI_MAX_SIZE)
71 /*
72  * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
73  * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
74  *
75  * Note: Calculations can results temporarily in larger MPIs. So the number
76  * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
77  */
78 #define MBEDTLS_MPI_MAX_SIZE 1024
79 #endif /* !MBEDTLS_MPI_MAX_SIZE */
80 
81 #define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE )
83 /*
84  * When reading from files with mbedtls_mpi_read_file() and writing to files with
85  * mbedtls_mpi_write_file() the buffer should have space
86  * for a (short) label, the MPI (in the provided radix), the newline
87  * characters and the '\0'.
88  *
89  * By default we assume at least a 10 char label, a minimum radix of 10
90  * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
91  * Autosized at compile time for at least a 10 char label, a minimum radix
92  * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
93  *
94  * This used to be statically sized to 1250 for a maximum of 4096 bit
95  * numbers (1234 decimal chars).
96  *
97  * Calculate using the formula:
98  * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
99  * LabelSize + 6
100  */
101 #define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
102 #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
103 #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 )
104 
105 /*
106  * Define the base integer type, architecture-wise.
107  *
108  * 32 or 64-bit integer types can be forced regardless of the underlying
109  * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
110  * respectively and undefining MBEDTLS_HAVE_ASM.
111  *
112  * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
113  * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
114  */
115 #if !defined(MBEDTLS_HAVE_INT32)
116  #if defined(_MSC_VER) && defined(_M_AMD64)
117  /* Always choose 64-bit when using MSC */
118  #if !defined(MBEDTLS_HAVE_INT64)
119  #define MBEDTLS_HAVE_INT64
120  #endif /* !MBEDTLS_HAVE_INT64 */
121  typedef int64_t mbedtls_mpi_sint;
122  typedef uint64_t mbedtls_mpi_uint;
123  #elif defined(__GNUC__) && ( \
124  defined(__amd64__) || defined(__x86_64__) || \
125  defined(__ppc64__) || defined(__powerpc64__) || \
126  defined(__ia64__) || defined(__alpha__) || \
127  ( defined(__sparc__) && defined(__arch64__) ) || \
128  defined(__s390x__) || defined(__mips64) )
129  #if !defined(MBEDTLS_HAVE_INT64)
130  #define MBEDTLS_HAVE_INT64
131  #endif /* MBEDTLS_HAVE_INT64 */
132  typedef int64_t mbedtls_mpi_sint;
133  typedef uint64_t mbedtls_mpi_uint;
134  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
135  /* mbedtls_t_udbl defined as 128-bit unsigned int */
136  typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
137  #define MBEDTLS_HAVE_UDBL
138  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
139  #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
140  /*
141  * __ARMCC_VERSION is defined for both armcc and armclang and
142  * __aarch64__ is only defined by armclang when compiling 64-bit code
143  */
144  #if !defined(MBEDTLS_HAVE_INT64)
145  #define MBEDTLS_HAVE_INT64
146  #endif /* !MBEDTLS_HAVE_INT64 */
147  typedef int64_t mbedtls_mpi_sint;
148  typedef uint64_t mbedtls_mpi_uint;
149  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
150  /* mbedtls_t_udbl defined as 128-bit unsigned int */
151  typedef __uint128_t mbedtls_t_udbl;
152  #define MBEDTLS_HAVE_UDBL
153  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
154  #elif defined(MBEDTLS_HAVE_INT64)
155  /* Force 64-bit integers with unknown compiler */
156  typedef int64_t mbedtls_mpi_sint;
157  typedef uint64_t mbedtls_mpi_uint;
158  #endif
159 #endif /* !MBEDTLS_HAVE_INT32 */
160 
161 #if !defined(MBEDTLS_HAVE_INT64)
162  /* Default to 32-bit compilation */
163  #if !defined(MBEDTLS_HAVE_INT32)
164  #define MBEDTLS_HAVE_INT32
165  #endif /* !MBEDTLS_HAVE_INT32 */
166  typedef int32_t mbedtls_mpi_sint;
167  typedef uint32_t mbedtls_mpi_uint;
168  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
169  typedef uint64_t mbedtls_t_udbl;
170  #define MBEDTLS_HAVE_UDBL
171  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
172 #endif /* !MBEDTLS_HAVE_INT64 */
173 
174 #ifdef __cplusplus
175 extern "C" {
176 #endif
177 
181 typedef struct
182 {
183  int s;
184  size_t n;
185  mbedtls_mpi_uint *p;
186 }
188 
196 void mbedtls_mpi_init( mbedtls_mpi *X );
197 
203 void mbedtls_mpi_free( mbedtls_mpi *X );
204 
214 int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
215 
225 int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
226 
236 int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
237 
245 
263 int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
264 
282 int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
283 
293 int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
294 
303 int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
304 
319 int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
320 
329 size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
330 
339 size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
340 
346 size_t mbedtls_mpi_size( const mbedtls_mpi *X );
347 
357 int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
358 
375 int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
376  char *buf, size_t buflen, size_t *olen );
377 
378 #if defined(MBEDTLS_FS_IO)
379 
399 int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
400 
413 int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
414 #endif /* MBEDTLS_FS_IO */
415 
426 int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
427 
440 int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
441 
451 int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
452 
462 int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
463 
474 int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
475 
486 int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
487 
498 int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
499 
510 int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
511 
522 int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
523 
534 int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
535 
546 int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
547 
558 int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
559 
570 int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
571 
582 int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
583 
596 int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
597 
612 int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
613 
628 int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b );
629 
642 int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
643 
656 int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
657 
676 int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR );
677 
689 int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
690  int (*f_rng)(void *, unsigned char *, size_t),
691  void *p_rng );
692 
703 int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
704 
717 int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
718 
730 int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
731  int (*f_rng)(void *, unsigned char *, size_t),
732  void *p_rng );
733 
748 int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
749  int (*f_rng)(void *, unsigned char *, size_t),
750  void *p_rng );
751 
757 int mbedtls_mpi_self_test( int verbose );
758 
759 #ifdef __cplusplus
760 }
761 #endif
762 
763 #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:166
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|.
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:169
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:185
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:155
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:167
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:181
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:184
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.