Mbed TLS v3.6.3
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"
15
16#include <stddef.h>
17#include <stdint.h>
18
19#if defined(MBEDTLS_FS_IO)
20#include <stdio.h>
21#endif
22
24#define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002
26#define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004
28#define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006
30#define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008
32#define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A
34#define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C
36#define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E
38#define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010
39
40#define MBEDTLS_MPI_CHK(f) \
41 do \
42 { \
43 if ((ret = (f)) != 0) \
44 goto cleanup; \
45 } while (0)
46
47/*
48 * Maximum size MPIs are allowed to grow to in number of limbs.
49 */
50#define MBEDTLS_MPI_MAX_LIMBS 10000
51
52#if !defined(MBEDTLS_MPI_WINDOW_SIZE)
53/*
54 * Maximum window size used for modular exponentiation. Default: 3
55 * Minimum value: 1. Maximum value: 6.
56 *
57 * Result is an array of ( 2 ** MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
58 * for the sliding window calculation. (So 8 by default)
59 *
60 * Reduction in size, reduces speed.
61 */
62#define MBEDTLS_MPI_WINDOW_SIZE 3
63#endif /* !MBEDTLS_MPI_WINDOW_SIZE */
64
65#if !defined(MBEDTLS_MPI_MAX_SIZE)
66/*
67 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
68 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
69 *
70 * Note: Calculations can temporarily result in larger MPIs. So the number
71 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
72 */
73#define MBEDTLS_MPI_MAX_SIZE 1024
74#endif /* !MBEDTLS_MPI_MAX_SIZE */
75
76#define MBEDTLS_MPI_MAX_BITS (8 * MBEDTLS_MPI_MAX_SIZE)
78/*
79 * When reading from files with mbedtls_mpi_read_file() and writing to files with
80 * mbedtls_mpi_write_file() the buffer should have space
81 * for a (short) label, the MPI (in the provided radix), the newline
82 * characters and the '\0'.
83 *
84 * By default we assume at least a 10 char label, a minimum radix of 10
85 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
86 * Autosized at compile time for at least a 10 char label, a minimum radix
87 * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
88 *
89 * This used to be statically sized to 1250 for a maximum of 4096 bit
90 * numbers (1234 decimal chars).
91 *
92 * Calculate using the formula:
93 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
94 * LabelSize + 6
95 */
96#define MBEDTLS_MPI_MAX_BITS_SCALE100 (100 * MBEDTLS_MPI_MAX_BITS)
97#define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
98#define MBEDTLS_MPI_RW_BUFFER_SIZE (((MBEDTLS_MPI_MAX_BITS_SCALE100 + \
99 MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / \
100 MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6)
101
102/*
103 * Define the base integer type, architecture-wise.
104 *
105 * 32 or 64-bit integer types can be forced regardless of the underlying
106 * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
107 * respectively and undefining MBEDTLS_HAVE_ASM.
108 *
109 * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
110 * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
111 */
112#if !defined(MBEDTLS_HAVE_INT32)
113 #if defined(_MSC_VER) && defined(_M_AMD64)
114/* Always choose 64-bit when using MSC */
115 #if !defined(MBEDTLS_HAVE_INT64)
116 #define MBEDTLS_HAVE_INT64
117 #endif /* !MBEDTLS_HAVE_INT64 */
118typedef int64_t mbedtls_mpi_sint;
119typedef uint64_t mbedtls_mpi_uint;
120#define MBEDTLS_MPI_UINT_MAX UINT64_MAX
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 defined(__aarch64__))
128 #if !defined(MBEDTLS_HAVE_INT64)
129 #define MBEDTLS_HAVE_INT64
130 #endif /* MBEDTLS_HAVE_INT64 */
131typedef int64_t mbedtls_mpi_sint;
132typedef uint64_t mbedtls_mpi_uint;
133#define MBEDTLS_MPI_UINT_MAX UINT64_MAX
134 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
135/* mbedtls_t_udbl defined as 128-bit unsigned int */
136typedef 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 */
147typedef int64_t mbedtls_mpi_sint;
148typedef uint64_t mbedtls_mpi_uint;
149#define MBEDTLS_MPI_UINT_MAX UINT64_MAX
150 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
151/* mbedtls_t_udbl defined as 128-bit unsigned int */
152typedef __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 */
157typedef int64_t mbedtls_mpi_sint;
158typedef uint64_t mbedtls_mpi_uint;
159#define MBEDTLS_MPI_UINT_MAX UINT64_MAX
160 #endif
161#endif /* !MBEDTLS_HAVE_INT32 */
162
163#if !defined(MBEDTLS_HAVE_INT64)
164/* Default to 32-bit compilation */
165 #if !defined(MBEDTLS_HAVE_INT32)
166 #define MBEDTLS_HAVE_INT32
167 #endif /* !MBEDTLS_HAVE_INT32 */
168typedef int32_t mbedtls_mpi_sint;
169typedef uint32_t mbedtls_mpi_uint;
170#define MBEDTLS_MPI_UINT_MAX UINT32_MAX
171 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
172typedef uint64_t mbedtls_t_udbl;
173 #define MBEDTLS_HAVE_UDBL
174 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
175#endif /* !MBEDTLS_HAVE_INT64 */
176
177/*
178 * Sanity check that exactly one of MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64 is defined,
179 * so that code elsewhere doesn't have to check.
180 */
181#if (!(defined(MBEDTLS_HAVE_INT32) || defined(MBEDTLS_HAVE_INT64))) || \
182 (defined(MBEDTLS_HAVE_INT32) && defined(MBEDTLS_HAVE_INT64))
183#error "Only 32-bit or 64-bit limbs are supported in bignum"
184#endif
185
200#ifdef __cplusplus
201extern "C" {
202#endif
203
207typedef struct mbedtls_mpi {
213
225 signed short MBEDTLS_PRIVATE(s);
226
228 unsigned short MBEDTLS_PRIVATE(n);
229 /* Make sure that MBEDTLS_MPI_MAX_LIMBS fits in n.
230 * Use the same limit value on all platforms so that we don't have to
231 * think about different behavior on the rare platforms where
232 * unsigned short can store values larger than the minimum required by
233 * the C language, which is 65535.
234 */
235#if MBEDTLS_MPI_MAX_LIMBS > 65535
236#error "MBEDTLS_MPI_MAX_LIMBS > 65535 is not supported"
237#endif
238}
240
250
259
273int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs);
274
290int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs);
291
306
314
343int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign);
344
372int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap);
373
385
396int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos);
397
413int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val);
414
428
442
457
468int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s);
469
492int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
493 char *buf, size_t buflen, size_t *olen);
494
495#if defined(MBEDTLS_FS_IO)
517int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin);
518
534int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X,
535 int radix, FILE *fout);
536#endif /* MBEDTLS_FS_IO */
537
550int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf,
551 size_t buflen);
552
566 const unsigned char *buf, size_t buflen);
567
583int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf,
584 size_t buflen);
585
602 unsigned char *buf, size_t buflen);
603
616int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count);
617
628int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count);
629
641
653
670 unsigned *ret);
671
683
696 const mbedtls_mpi *B);
697
711 const mbedtls_mpi *B);
712
725 const mbedtls_mpi *B);
726
739 const mbedtls_mpi *B);
740
754
769
783 const mbedtls_mpi *B);
784
800
820 const mbedtls_mpi *B);
821
842
861 const mbedtls_mpi *B);
862
881
910 const mbedtls_mpi *E, const mbedtls_mpi *N,
911 mbedtls_mpi *prec_RR);
912
931 int (*f_rng)(void *, unsigned char *, size_t),
932 void *p_rng);
933
968 const mbedtls_mpi *N,
969 int (*f_rng)(void *, unsigned char *, size_t),
970 void *p_rng);
971
984 const mbedtls_mpi *B);
985
1003 const mbedtls_mpi *N);
1004
1032int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
1033 int (*f_rng)(void *, unsigned char *, size_t),
1034 void *p_rng);
1041typedef enum {
1045
1065int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
1066 int (*f_rng)(void *, unsigned char *, size_t),
1067 void *p_rng);
1068
1069#if defined(MBEDTLS_SELF_TEST)
1070
1076int mbedtls_mpi_self_test(int verbose);
1077
1078#endif /* MBEDTLS_SELF_TEST */
1079
1080#ifdef __cplusplus
1081}
1082#endif
1083
1084#endif /* bignum.h */
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_is_prime_ext(const mbedtls_mpi *X, int rounds, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Miller-Rabin primality test.
int32_t mbedtls_mpi_sint
The signed type corresponding to mbedtls_mpi_uint.
Definition: bignum.h:168
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:1041
@ MBEDTLS_MPI_GEN_PRIME_FLAG_DH
Definition: bignum.h:1042
@ MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR
Definition: bignum.h:1043
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_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_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_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Compare the absolute values of two MPIs.
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.
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.
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.
uint32_t mbedtls_mpi_uint
The type of machine digits in a bignum, called limbs.
Definition: bignum.h:169
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|.
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_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:172
Build-time configuration info.
Macro wrapper for struct's members.
#define MBEDTLS_PRIVATE(member)
MPI structure.
Definition: bignum.h:207