mbed TLS v3.4.0
bignum.h
Go to the documentation of this file.
1 
6 /*
7  * Copyright The Mbed TLS Contributors
8  * SPDX-License-Identifier: Apache-2.0
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License"); you may
11  * not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 #ifndef MBEDTLS_BIGNUM_H
23 #define MBEDTLS_BIGNUM_H
24 #include "mbedtls/private_access.h"
25 
26 #include "mbedtls/build_info.h"
27 
28 #include <stddef.h>
29 #include <stdint.h>
30 
31 #if defined(MBEDTLS_FS_IO)
32 #include <stdio.h>
33 #endif
34 
36 #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002
37 
38 #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004
39 
40 #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006
41 
42 #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008
43 
44 #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A
45 
46 #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C
47 
48 #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E
49 
50 #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010
51 
52 #define MBEDTLS_MPI_CHK(f) \
53  do \
54  { \
55  if ((ret = (f)) != 0) \
56  goto cleanup; \
57  } while (0)
58 
59 /*
60  * Maximum size MPIs are allowed to grow to in number of limbs.
61  */
62 #define MBEDTLS_MPI_MAX_LIMBS 10000
63 
64 #if !defined(MBEDTLS_MPI_WINDOW_SIZE)
65 /*
66  * Maximum window size used for modular exponentiation. Default: 2
67  * Minimum value: 1. Maximum value: 6.
68  *
69  * Result is an array of ( 2 ** MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
70  * for the sliding window calculation. (So 64 by default)
71  *
72  * Reduction in size, reduces speed.
73  */
74 #define MBEDTLS_MPI_WINDOW_SIZE 2
75 #endif /* !MBEDTLS_MPI_WINDOW_SIZE */
76 
77 #if !defined(MBEDTLS_MPI_MAX_SIZE)
78 /*
79  * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
80  * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
81  *
82  * Note: Calculations can temporarily result in larger MPIs. So the number
83  * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
84  */
85 #define MBEDTLS_MPI_MAX_SIZE 1024
86 #endif /* !MBEDTLS_MPI_MAX_SIZE */
87 
88 #define MBEDTLS_MPI_MAX_BITS (8 * MBEDTLS_MPI_MAX_SIZE)
90 /*
91  * When reading from files with mbedtls_mpi_read_file() and writing to files with
92  * mbedtls_mpi_write_file() the buffer should have space
93  * for a (short) label, the MPI (in the provided radix), the newline
94  * characters and the '\0'.
95  *
96  * By default we assume at least a 10 char label, a minimum radix of 10
97  * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
98  * Autosized at compile time for at least a 10 char label, a minimum radix
99  * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
100  *
101  * This used to be statically sized to 1250 for a maximum of 4096 bit
102  * numbers (1234 decimal chars).
103  *
104  * Calculate using the formula:
105  * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
106  * LabelSize + 6
107  */
108 #define MBEDTLS_MPI_MAX_BITS_SCALE100 (100 * MBEDTLS_MPI_MAX_BITS)
109 #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
110 #define MBEDTLS_MPI_RW_BUFFER_SIZE (((MBEDTLS_MPI_MAX_BITS_SCALE100 + \
111  MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / \
112  MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6)
113 
114 /*
115  * Define the base integer type, architecture-wise.
116  *
117  * 32 or 64-bit integer types can be forced regardless of the underlying
118  * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
119  * respectively and undefining MBEDTLS_HAVE_ASM.
120  *
121  * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
122  * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
123  */
124 #if !defined(MBEDTLS_HAVE_INT32)
125  #if defined(_MSC_VER) && defined(_M_AMD64)
126 /* Always choose 64-bit when using MSC */
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  #elif defined(__GNUC__) && ( \
133  defined(__amd64__) || defined(__x86_64__) || \
134  defined(__ppc64__) || defined(__powerpc64__) || \
135  defined(__ia64__) || defined(__alpha__) || \
136  (defined(__sparc__) && defined(__arch64__)) || \
137  defined(__s390x__) || defined(__mips64) || \
138  defined(__aarch64__))
139  #if !defined(MBEDTLS_HAVE_INT64)
140  #define MBEDTLS_HAVE_INT64
141  #endif /* MBEDTLS_HAVE_INT64 */
142 typedef int64_t mbedtls_mpi_sint;
143 typedef uint64_t mbedtls_mpi_uint;
144  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
145 /* mbedtls_t_udbl defined as 128-bit unsigned int */
146 typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
147  #define MBEDTLS_HAVE_UDBL
148  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
149  #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
150 /*
151  * __ARMCC_VERSION is defined for both armcc and armclang and
152  * __aarch64__ is only defined by armclang when compiling 64-bit code
153  */
154  #if !defined(MBEDTLS_HAVE_INT64)
155  #define MBEDTLS_HAVE_INT64
156  #endif /* !MBEDTLS_HAVE_INT64 */
157 typedef int64_t mbedtls_mpi_sint;
158 typedef uint64_t mbedtls_mpi_uint;
159  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
160 /* mbedtls_t_udbl defined as 128-bit unsigned int */
161 typedef __uint128_t mbedtls_t_udbl;
162  #define MBEDTLS_HAVE_UDBL
163  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
164  #elif defined(MBEDTLS_HAVE_INT64)
165 /* Force 64-bit integers with unknown compiler */
166 typedef int64_t mbedtls_mpi_sint;
167 typedef uint64_t mbedtls_mpi_uint;
168  #endif
169 #endif /* !MBEDTLS_HAVE_INT32 */
170 
171 #if !defined(MBEDTLS_HAVE_INT64)
172 /* Default to 32-bit compilation */
173  #if !defined(MBEDTLS_HAVE_INT32)
174  #define MBEDTLS_HAVE_INT32
175  #endif /* !MBEDTLS_HAVE_INT32 */
176 typedef int32_t mbedtls_mpi_sint;
177 typedef uint32_t mbedtls_mpi_uint;
178  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
179 typedef uint64_t mbedtls_t_udbl;
180  #define MBEDTLS_HAVE_UDBL
181  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
182 #endif /* !MBEDTLS_HAVE_INT64 */
183 
198 #ifdef __cplusplus
199 extern "C" {
200 #endif
201 
205 typedef struct mbedtls_mpi {
218 
220  size_t MBEDTLS_PRIVATE(n);
221 
226  mbedtls_mpi_uint *MBEDTLS_PRIVATE(p);
227 }
229 
239 
248 
262 int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs);
263 
279 int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs);
280 
294 int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y);
295 
303 
332 int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign);
333 
361 int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap);
362 
373 int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z);
374 
385 int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos);
386 
402 int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val);
403 
416 size_t mbedtls_mpi_lsb(const mbedtls_mpi *X);
417 
430 size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X);
431 
445 size_t mbedtls_mpi_size(const mbedtls_mpi *X);
446 
457 int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s);
458 
481 int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
482  char *buf, size_t buflen, size_t *olen);
483 
484 #if defined(MBEDTLS_FS_IO)
485 
506 int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin);
507 
523 int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X,
524  int radix, FILE *fout);
525 #endif /* MBEDTLS_FS_IO */
526 
539 int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf,
540  size_t buflen);
541 
555  const unsigned char *buf, size_t buflen);
556 
572 int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf,
573  size_t buflen);
574 
591  unsigned char *buf, size_t buflen);
592 
603 int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count);
604 
615 int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count);
616 
627 int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y);
628 
639 int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y);
640 
656 int mbedtls_mpi_lt_mpi_ct(const mbedtls_mpi *X, const mbedtls_mpi *Y,
657  unsigned *ret);
658 
669 int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z);
670 
683  const mbedtls_mpi *B);
684 
698  const mbedtls_mpi *B);
699 
712  const mbedtls_mpi *B);
713 
726  const mbedtls_mpi *B);
727 
740  mbedtls_mpi_sint b);
741 
755  mbedtls_mpi_sint b);
756 
770  const mbedtls_mpi *B);
771 
786  mbedtls_mpi_uint b);
787 
807  const mbedtls_mpi *B);
808 
828  mbedtls_mpi_sint b);
829 
848  const mbedtls_mpi *B);
849 
866 int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A,
867  mbedtls_mpi_sint b);
868 
897  const mbedtls_mpi *E, const mbedtls_mpi *N,
898  mbedtls_mpi *prec_RR);
899 
917 int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size,
918  int (*f_rng)(void *, unsigned char *, size_t),
919  void *p_rng);
920 
954  mbedtls_mpi_sint min,
955  const mbedtls_mpi *N,
956  int (*f_rng)(void *, unsigned char *, size_t),
957  void *p_rng);
958 
970 int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A,
971  const mbedtls_mpi *B);
972 
990  const mbedtls_mpi *N);
991 
1019 int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
1020  int (*f_rng)(void *, unsigned char *, size_t),
1021  void *p_rng);
1028 typedef enum {
1032 
1052 int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
1053  int (*f_rng)(void *, unsigned char *, size_t),
1054  void *p_rng);
1055 
1056 #if defined(MBEDTLS_SELF_TEST)
1057 
1063 int mbedtls_mpi_self_test(int verbose);
1064 
1065 #endif /* MBEDTLS_SELF_TEST */
1066 
1067 #ifdef __cplusplus
1068 }
1069 #endif
1070 
1071 #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. ...
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'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.
#define MBEDTLS_PRIVATE(member)
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|.
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:179
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.
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.
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:176
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:177
int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
Perform a left-shift on an MPI: X <<= count.
Macro wrapper for struct's members.
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.
Build-time configuration info.
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:205
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.
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:1028
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'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.