Mbed TLS v2.28.8
bignum.h
Go to the documentation of this file.
1 
6 /*
7  * Copyright The Mbed TLS Contributors
8  * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
9  */
10 #ifndef MBEDTLS_BIGNUM_H
11 #define MBEDTLS_BIGNUM_H
12 
13 #if !defined(MBEDTLS_CONFIG_FILE)
14 #include "mbedtls/config.h"
15 #else
16 #include MBEDTLS_CONFIG_FILE
17 #endif
18 
19 #include <stddef.h>
20 #include <stdint.h>
21 
22 #if defined(MBEDTLS_FS_IO)
23 #include <stdio.h>
24 #endif
25 
27 #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002
28 
29 #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004
30 
31 #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006
32 
33 #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008
34 
35 #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A
36 
37 #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C
38 
39 #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E
40 
41 #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010
42 
43 #define MBEDTLS_MPI_CHK(f) \
44  do \
45  { \
46  if ((ret = (f)) != 0) \
47  goto cleanup; \
48  } 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: 2
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 2
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 temporarily result 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 + \
102  MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / \
103  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  defined(__aarch64__))
130  #if !defined(MBEDTLS_HAVE_INT64)
131  #define MBEDTLS_HAVE_INT64
132  #endif /* MBEDTLS_HAVE_INT64 */
133 typedef int64_t mbedtls_mpi_sint;
134 typedef uint64_t mbedtls_mpi_uint;
135  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
136 /* mbedtls_t_udbl defined as 128-bit unsigned int */
137 typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
138  #define MBEDTLS_HAVE_UDBL
139  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
140  #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
141 /*
142  * __ARMCC_VERSION is defined for both armcc and armclang and
143  * __aarch64__ is only defined by armclang when compiling 64-bit code
144  */
145  #if !defined(MBEDTLS_HAVE_INT64)
146  #define MBEDTLS_HAVE_INT64
147  #endif /* !MBEDTLS_HAVE_INT64 */
148 typedef int64_t mbedtls_mpi_sint;
149 typedef uint64_t mbedtls_mpi_uint;
150  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
151 /* mbedtls_t_udbl defined as 128-bit unsigned int */
152 typedef __uint128_t mbedtls_t_udbl;
153  #define MBEDTLS_HAVE_UDBL
154  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
155  #elif defined(MBEDTLS_HAVE_INT64)
156 /* Force 64-bit integers with unknown compiler */
157 typedef int64_t mbedtls_mpi_sint;
158 typedef uint64_t mbedtls_mpi_uint;
159  #endif
160 #endif /* !MBEDTLS_HAVE_INT32 */
161 
162 #if !defined(MBEDTLS_HAVE_INT64)
163 /* Default to 32-bit compilation */
164  #if !defined(MBEDTLS_HAVE_INT32)
165  #define MBEDTLS_HAVE_INT32
166  #endif /* !MBEDTLS_HAVE_INT32 */
167 typedef int32_t mbedtls_mpi_sint;
168 typedef uint32_t mbedtls_mpi_uint;
169  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
170 typedef uint64_t mbedtls_t_udbl;
171  #define MBEDTLS_HAVE_UDBL
172  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
173 #endif /* !MBEDTLS_HAVE_INT64 */
174 
189 #ifdef __cplusplus
190 extern "C" {
191 #endif
192 
196 typedef struct mbedtls_mpi {
208  int s;
209 
211  size_t n;
212 
218 }
220 
230 
239 
253 int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs);
254 
270 int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs);
271 
285 int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y);
286 
294 
323 int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign);
324 
352 int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap);
353 
365 
376 int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos);
377 
393 int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val);
394 
407 size_t mbedtls_mpi_lsb(const mbedtls_mpi *X);
408 
421 size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X);
422 
436 size_t mbedtls_mpi_size(const mbedtls_mpi *X);
437 
448 int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s);
449 
472 int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
473  char *buf, size_t buflen, size_t *olen);
474 
475 #if defined(MBEDTLS_FS_IO)
476 
497 int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin);
498 
514 int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X,
515  int radix, FILE *fout);
516 #endif /* MBEDTLS_FS_IO */
517 
530 int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf,
531  size_t buflen);
532 
546  const unsigned char *buf, size_t buflen);
547 
563 int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf,
564  size_t buflen);
565 
582  unsigned char *buf, size_t buflen);
583 
594 int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count);
595 
606 int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count);
607 
618 int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y);
619 
630 int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y);
631 
647 int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X, const mbedtls_mpi *Y,
648  unsigned *ret);
649 
661 
674  const mbedtls_mpi *B);
675 
689  const mbedtls_mpi *B);
690 
703  const mbedtls_mpi *B);
704 
717  const mbedtls_mpi *B);
718 
731  mbedtls_mpi_sint b);
732 
746  mbedtls_mpi_sint b);
747 
761  const mbedtls_mpi *B);
762 
777  mbedtls_mpi_uint b);
778 
798  const mbedtls_mpi *B);
799 
819  mbedtls_mpi_sint b);
820 
839  const mbedtls_mpi *B);
840 
858  mbedtls_mpi_sint b);
859 
888  const mbedtls_mpi *E, const mbedtls_mpi *N,
889  mbedtls_mpi *prec_RR);
890 
908 int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
909  int (*f_rng)(void *, unsigned char *, size_t),
910  void *p_rng);
911 
945  mbedtls_mpi_sint min,
946  const mbedtls_mpi *N,
947  int (*f_rng)(void *, unsigned char *, size_t),
948  void *p_rng);
949 
961 int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A,
962  const mbedtls_mpi *B);
963 
981  const mbedtls_mpi *N);
982 
983 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
984 #if defined(MBEDTLS_DEPRECATED_WARNING)
985 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
986 #else
987 #define MBEDTLS_DEPRECATED
988 #endif
989 
1009  int (*f_rng)(void *, unsigned char *, size_t),
1010  void *p_rng);
1011 #undef MBEDTLS_DEPRECATED
1012 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
1013 
1041 int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
1042  int (*f_rng)(void *, unsigned char *, size_t),
1043  void *p_rng);
1050 typedef enum {
1054 
1074 int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
1075  int (*f_rng)(void *, unsigned char *, size_t),
1076  void *p_rng);
1077 
1078 #if defined(MBEDTLS_SELF_TEST)
1079 
1085 int mbedtls_mpi_self_test(int verbose);
1086 
1087 #endif /* MBEDTLS_SELF_TEST */
1088 
1089 #ifdef __cplusplus
1090 }
1091 #endif
1092 
1093 #endif /* bignum.h */
int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform a division with remainder of two MPIs: A = Q * B + R.
void mbedtls_mpi_free(mbedtls_mpi *X)
This function frees the components of an MPI context.
int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
Compute the 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 with a number of random bytes.
int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Miller-Rabin primality test.
int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Perform a signed subtraction of an MPI and an integer: X = A - b.
size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
Return the number of bits of value 0 before the least significant bit of value 1. ...
#define MBEDTLS_DEPRECATED
Definition: bignum.h:987
int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
Enlarge an MPI to the specified number of limbs.
int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign)
Perform a safe conditional copy of MPI which doesn&#39;t reveal whether the condition was true or not...
int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform a modular reduction. R = A mod B.
size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
Return the number of bits up to and including the most significant bit of value 1.
int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Compare two MPIs.
size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Return the total size of an MPI value in bytes.
int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform a signed subtraction of MPIs: X = A - B.
int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform an unsigned subtraction of MPIs: X = |A| - |B|.
Configuration options (set of defines)
int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
Import an MPI from an ASCII string.
uint64_t mbedtls_t_udbl
Definition: bignum.h:170
int mbedtls_mpi_random(mbedtls_mpi *X, mbedtls_mpi_sint min, const mbedtls_mpi *N, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
Modify a specific bit in an MPI.
int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
Store integer value in MPI.
int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Perform a division with remainder of an MPI by an integer: A = Q * b + R.
int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Import an MPI from unsigned big endian binary data.
mbedtls_mpi_uint * p
Definition: bignum.h:217
int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
This function resizes an MPI downwards, keeping at least the specified number of limbs.
int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned *ret)
Check if an MPI is less than the other in constant time.
int mbedtls_mpi_read_binary_le(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Import X from unsigned binary data, little endian.
MBEDTLS_DEPRECATED int mbedtls_mpi_is_prime(const mbedtls_mpi *X, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Perform a Miller-Rabin primality test with error probability of 2-80.
int mbedtls_mpi_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Perform a signed addition of an MPI and an integer: X = A + b.
int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
Perform a right-shift on an MPI: X >>= count.
int mbedtls_mpi_write_binary_le(const mbedtls_mpi *X, unsigned char *buf, size_t buflen)
Export X into unsigned binary data, little endian. Always fills the whole buffer, which will end with...
int32_t mbedtls_mpi_sint
The signed type corresponding to mbedtls_mpi_uint.
Definition: bignum.h:167
int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Perform a modular reduction with respect to an integer. r = A mod b.
uint32_t mbedtls_mpi_uint
The type of machine digits in a bignum, called limbs.
Definition: bignum.h:168
int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
Perform a left-shift on an MPI: X <<= count.
int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
Perform a multiplication of an MPI with an unsigned integer: X = A * b.
int mbedtls_mpi_self_test(int verbose)
Checkup routine.
void mbedtls_mpi_init(mbedtls_mpi *X)
Initialize an MPI context.
void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
Swap the contents of two MPIs.
int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform a signed addition of MPIs: X = A + B.
int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
Compute the modular inverse: X = A^-1 mod N.
MPI structure.
Definition: bignum.h:196
int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
Make a copy of an MPI.
int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix, char *buf, size_t buflen, size_t *olen)
Export an MPI to an ASCII string.
int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
Read an MPI from a line in an opened file.
int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
Export an MPI into an opened file.
size_t n
Definition: bignum.h:211
int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform an unsigned addition of MPIs: X = |A| + |B|.
int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *prec_RR)
Perform a sliding-window exponentiation: X = A^E mod N.
int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Compare the absolute values of two MPIs.
int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf, size_t buflen)
Export X into unsigned binary data, big endian. Always fills the whole buffer, which will start with ...
mbedtls_mpi_gen_prime_flag_t
Flags for mbedtls_mpi_gen_prime()
Definition: bignum.h:1050
int mbedtls_mpi_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform a multiplication of two MPIs: X = A * B.
int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
Compare an MPI with an integer.
int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap)
Perform a safe conditional swap which doesn&#39;t reveal whether the condition was true or not...
int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
Get a specific bit from an MPI.
struct mbedtls_mpi mbedtls_mpi
MPI structure.
int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Generate a prime number.