Mbed TLS v3.6.4
Loading...
Searching...
No Matches
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
13
14#include "mbedtls/build_info.h"
16
17#include <stddef.h>
18#include <stdint.h>
19
20#if defined(MBEDTLS_FS_IO)
21#include <stdio.h>
22#endif
23
25#define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002
27#define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004
29#define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006
31#define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008
33#define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A
35#define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C
37#define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E
39#define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010
40
41#define MBEDTLS_MPI_CHK(f) \
42 do \
43 { \
44 if ((ret = (f)) != 0) \
45 goto cleanup; \
46 } while (0)
47
48/*
49 * Maximum size MPIs are allowed to grow to in number of limbs.
50 */
51#define MBEDTLS_MPI_MAX_LIMBS 10000
52
53#if !defined(MBEDTLS_MPI_WINDOW_SIZE)
54/*
55 * Maximum window size used for modular exponentiation. Default: 3
56 * Minimum value: 1. Maximum value: 6.
57 *
58 * Result is an array of ( 2 ** MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
59 * for the sliding window calculation. (So 8 by default)
60 *
61 * Reduction in size, reduces speed.
62 */
63#define MBEDTLS_MPI_WINDOW_SIZE 3
64#endif /* !MBEDTLS_MPI_WINDOW_SIZE */
65
66#if !defined(MBEDTLS_MPI_MAX_SIZE)
67/*
68 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
69 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
70 *
71 * Note: Calculations can temporarily result in larger MPIs. So the number
72 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
73 */
74#define MBEDTLS_MPI_MAX_SIZE 1024
75#endif /* !MBEDTLS_MPI_MAX_SIZE */
76
77#define MBEDTLS_MPI_MAX_BITS (8 * MBEDTLS_MPI_MAX_SIZE)
78
79/*
80 * When reading from files with mbedtls_mpi_read_file() and writing to files with
81 * mbedtls_mpi_write_file() the buffer should have space
82 * for a (short) label, the MPI (in the provided radix), the newline
83 * characters and the '\0'.
84 *
85 * By default we assume at least a 10 char label, a minimum radix of 10
86 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
87 * Autosized at compile time for at least a 10 char label, a minimum radix
88 * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
89 *
90 * This used to be statically sized to 1250 for a maximum of 4096 bit
91 * numbers (1234 decimal chars).
92 *
93 * Calculate using the formula:
94 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
95 * LabelSize + 6
96 */
97#define MBEDTLS_MPI_MAX_BITS_SCALE100 (100 * MBEDTLS_MPI_MAX_BITS)
98#define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
99#define MBEDTLS_MPI_RW_BUFFER_SIZE (((MBEDTLS_MPI_MAX_BITS_SCALE100 + \
100 MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / \
101 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 */
119typedef int64_t mbedtls_mpi_sint;
120typedef uint64_t mbedtls_mpi_uint;
121#define MBEDTLS_MPI_UINT_MAX UINT64_MAX
122 #elif defined(__GNUC__) && ( \
123 defined(__amd64__) || defined(__x86_64__) || \
124 defined(__ppc64__) || defined(__powerpc64__) || \
125 defined(__ia64__) || defined(__alpha__) || \
126 (defined(__sparc__) && defined(__arch64__)) || \
127 defined(__s390x__) || defined(__mips64) || \
128 defined(__aarch64__))
129 #if !defined(MBEDTLS_HAVE_INT64)
130 #define MBEDTLS_HAVE_INT64
131 #endif /* MBEDTLS_HAVE_INT64 */
132typedef int64_t mbedtls_mpi_sint;
133typedef uint64_t mbedtls_mpi_uint;
134#define MBEDTLS_MPI_UINT_MAX UINT64_MAX
135 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
136/* mbedtls_t_udbl defined as 128-bit unsigned int */
137typedef 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 */
148typedef int64_t mbedtls_mpi_sint;
149typedef uint64_t mbedtls_mpi_uint;
150#define MBEDTLS_MPI_UINT_MAX UINT64_MAX
151 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
152/* mbedtls_t_udbl defined as 128-bit unsigned int */
153typedef __uint128_t mbedtls_t_udbl;
154 #define MBEDTLS_HAVE_UDBL
155 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
156 #elif defined(MBEDTLS_HAVE_INT64)
157/* Force 64-bit integers with unknown compiler */
158typedef int64_t mbedtls_mpi_sint;
159typedef uint64_t mbedtls_mpi_uint;
160#define MBEDTLS_MPI_UINT_MAX UINT64_MAX
161 #endif
162#endif /* !MBEDTLS_HAVE_INT32 */
163
164#if !defined(MBEDTLS_HAVE_INT64)
165/* Default to 32-bit compilation */
166 #if !defined(MBEDTLS_HAVE_INT32)
167 #define MBEDTLS_HAVE_INT32
168 #endif /* !MBEDTLS_HAVE_INT32 */
169typedef int32_t mbedtls_mpi_sint;
170typedef uint32_t mbedtls_mpi_uint;
171#define MBEDTLS_MPI_UINT_MAX UINT32_MAX
172 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
173typedef uint64_t mbedtls_t_udbl;
174 #define MBEDTLS_HAVE_UDBL
175 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
176#endif /* !MBEDTLS_HAVE_INT64 */
177
178/*
179 * Sanity check that exactly one of MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64 is defined,
180 * so that code elsewhere doesn't have to check.
181 */
182#if (!(defined(MBEDTLS_HAVE_INT32) || defined(MBEDTLS_HAVE_INT64))) || \
183 (defined(MBEDTLS_HAVE_INT32) && defined(MBEDTLS_HAVE_INT64))
184#error "Only 32-bit or 64-bit limbs are supported in bignum"
185#endif
186
193
200
201#ifdef __cplusplus
202extern "C" {
203#endif
204
208typedef struct mbedtls_mpi {
214
226 signed short MBEDTLS_PRIVATE(s);
227
229 unsigned short MBEDTLS_PRIVATE(n);
230 /* Make sure that MBEDTLS_MPI_MAX_LIMBS fits in n.
231 * Use the same limit value on all platforms so that we don't have to
232 * think about different behavior on the rare platforms where
233 * unsigned short can store values larger than the minimum required by
234 * the C language, which is 65535.
235 */
236#if MBEDTLS_MPI_MAX_LIMBS > 65535
237#error "MBEDTLS_MPI_MAX_LIMBS > 65535 is not supported"
238#endif
239}
241
251
260
274int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs);
275
291int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs);
292
307
315
344int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign);
345
373int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap);
374
386
397int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos);
398
414int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val);
415
429
443
458
469int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s);
470
493int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
494 char *buf, size_t buflen, size_t *olen);
495
496#if defined(MBEDTLS_FS_IO)
518int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin);
519
535int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X,
536 int radix, FILE *fout);
537#endif /* MBEDTLS_FS_IO */
538
551int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf,
552 size_t buflen);
553
567 const unsigned char *buf, size_t buflen);
568
584int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf,
585 size_t buflen);
586
603 unsigned char *buf, size_t buflen);
604
617int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count);
618
629int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count);
630
642
654
671 unsigned *ret);
672
684
697 const mbedtls_mpi *B);
698
712 const mbedtls_mpi *B);
713
726 const mbedtls_mpi *B);
727
740 const mbedtls_mpi *B);
741
755
770
784 const mbedtls_mpi *B);
785
801
821 const mbedtls_mpi *B);
822
843
862 const mbedtls_mpi *B);
863
882
911 const mbedtls_mpi *E, const mbedtls_mpi *N,
912 mbedtls_mpi *prec_RR);
913
932 mbedtls_f_rng_t *f_rng,
933 void *p_rng);
934
969 const mbedtls_mpi *N,
970 mbedtls_f_rng_t *f_rng,
971 void *p_rng);
972
985 const mbedtls_mpi *B);
986
1004 const mbedtls_mpi *N);
1005
1033int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
1034 mbedtls_f_rng_t *f_rng,
1035 void *p_rng);
1046
1066int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
1067 mbedtls_f_rng_t *f_rng,
1068 void *p_rng);
1069
1070#if defined(MBEDTLS_SELF_TEST)
1071
1077int mbedtls_mpi_self_test(int verbose);
1078
1079#endif /* MBEDTLS_SELF_TEST */
1080
1081#ifdef __cplusplus
1082}
1083#endif
1084
1085#endif /* bignum.h */
int mbedtls_mpi_fill_random(mbedtls_mpi *X, size_t size, mbedtls_f_rng_t *f_rng, void *p_rng)
Fill an MPI with a number of random bytes.
int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags, mbedtls_f_rng_t *f_rng, void *p_rng)
Generate a prime number.
int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
Import an MPI from an ASCII string.
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_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.
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_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_read_binary_le(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Import X from unsigned binary data, little endian.
int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
Enlarge an MPI to the specified number of limbs.
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 modular exponentiation: X = A^E mod N.
int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
Make a copy of an MPI.
int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
Modify a specific bit in an MPI.
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.
mbedtls_mpi_gen_prime_flag_t
Flags for mbedtls_mpi_gen_prime()
Definition bignum.h:1042
@ MBEDTLS_MPI_GEN_PRIME_FLAG_DH
Definition bignum.h:1043
@ MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR
Definition bignum.h:1044
size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Return the total size of an MPI value in bytes.
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...
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_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.
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.
void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
Swap the contents of two MPIs.
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_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
Store integer value in MPI.
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_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Import an MPI from unsigned big endian binary data.
int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds, mbedtls_f_rng_t *f_rng, void *p_rng)
Miller-Rabin primality test.
int mbedtls_mpi_self_test(int verbose)
Checkup routine.
int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Compare two MPIs.
int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Perform a modular reduction. R = A mod B.
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_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Compare the absolute values of two MPIs.
int32_t mbedtls_mpi_sint
The signed type corresponding to mbedtls_mpi_uint.
Definition bignum.h:169
int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
Perform a left-shift on an MPI: X <<= count.
void mbedtls_mpi_init(mbedtls_mpi *X)
Initialize an MPI context.
int mbedtls_mpi_random(mbedtls_mpi *X, mbedtls_mpi_sint min, const mbedtls_mpi *N, mbedtls_f_rng_t *f_rng, void *p_rng)
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_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_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_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
Export an MPI into an opened file.
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_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
Compute the modular inverse: X = A^-1 mod N.
int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
Get a specific bit from an MPI.
void mbedtls_mpi_free(mbedtls_mpi *X)
This function frees the components of an MPI context.
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_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_file(mbedtls_mpi *X, int radix, FILE *fin)
Read an MPI from a line in an opened file.
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 ...
int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
Compare an MPI with an integer.
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|.
uint32_t mbedtls_mpi_uint
The type of machine digits in a bignum, called limbs.
Definition bignum.h:170
int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
Perform a right-shift on an MPI: X >>= count.
int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
Compute the greatest common divisor: G = gcd(A, B)
uint64_t mbedtls_t_udbl
Definition bignum.h:173
Build-time configuration info.
Common and shared functions used by multiple modules in the Mbed TLS library.
int mbedtls_f_rng_t(void *p_rng, unsigned char *output, size_t output_size)
The type of custom random generator (RNG) callbacks.
Macro wrapper for struct's members.
#define MBEDTLS_PRIVATE(member)
MPI structure.
Definition bignum.h:208