mbed TLS v2.28.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
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
25#if !defined(MBEDTLS_CONFIG_FILE)
26#include "mbedtls/config.h"
27#else
28#include MBEDTLS_CONFIG_FILE
29#endif
30
31#include <stddef.h>
32#include <stdint.h>
33
34#if defined(MBEDTLS_FS_IO)
35#include <stdio.h>
36#endif
37
39#define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002
41#define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004
43#define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006
45#define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008
47#define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A
49#define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C
51#define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E
53#define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010
54
55#define MBEDTLS_MPI_CHK(f) \
56 do \
57 { \
58 if ((ret = (f)) != 0) \
59 goto cleanup; \
60 } while (0)
61
62/*
63 * Maximum size MPIs are allowed to grow to in number of limbs.
64 */
65#define MBEDTLS_MPI_MAX_LIMBS 10000
66
67#if !defined(MBEDTLS_MPI_WINDOW_SIZE)
68/*
69 * Maximum window size used for modular exponentiation. Default: 2
70 * Minimum value: 1. Maximum value: 6.
71 *
72 * Result is an array of ( 2 ** MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
73 * for the sliding window calculation. (So 64 by default)
74 *
75 * Reduction in size, reduces speed.
76 */
77#define MBEDTLS_MPI_WINDOW_SIZE 2
78#endif /* !MBEDTLS_MPI_WINDOW_SIZE */
79
80#if !defined(MBEDTLS_MPI_MAX_SIZE)
81/*
82 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
83 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
84 *
85 * Note: Calculations can temporarily result in larger MPIs. So the number
86 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
87 */
88#define MBEDTLS_MPI_MAX_SIZE 1024
89#endif /* !MBEDTLS_MPI_MAX_SIZE */
90
91#define MBEDTLS_MPI_MAX_BITS (8 * MBEDTLS_MPI_MAX_SIZE)
93/*
94 * When reading from files with mbedtls_mpi_read_file() and writing to files with
95 * mbedtls_mpi_write_file() the buffer should have space
96 * for a (short) label, the MPI (in the provided radix), the newline
97 * characters and the '\0'.
98 *
99 * By default we assume at least a 10 char label, a minimum radix of 10
100 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
101 * Autosized at compile time for at least a 10 char label, a minimum radix
102 * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
103 *
104 * This used to be statically sized to 1250 for a maximum of 4096 bit
105 * numbers (1234 decimal chars).
106 *
107 * Calculate using the formula:
108 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
109 * LabelSize + 6
110 */
111#define MBEDTLS_MPI_MAX_BITS_SCALE100 (100 * MBEDTLS_MPI_MAX_BITS)
112#define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
113#define MBEDTLS_MPI_RW_BUFFER_SIZE (((MBEDTLS_MPI_MAX_BITS_SCALE100 + \
114 MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / \
115 MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6)
116
117/*
118 * Define the base integer type, architecture-wise.
119 *
120 * 32 or 64-bit integer types can be forced regardless of the underlying
121 * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
122 * respectively and undefining MBEDTLS_HAVE_ASM.
123 *
124 * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
125 * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
126 */
127#if !defined(MBEDTLS_HAVE_INT32)
128 #if defined(_MSC_VER) && defined(_M_AMD64)
129/* Always choose 64-bit when using MSC */
130 #if !defined(MBEDTLS_HAVE_INT64)
131 #define MBEDTLS_HAVE_INT64
132 #endif /* !MBEDTLS_HAVE_INT64 */
133typedef int64_t mbedtls_mpi_sint;
134typedef uint64_t mbedtls_mpi_uint;
135 #elif defined(__GNUC__) && ( \
136 defined(__amd64__) || defined(__x86_64__) || \
137 defined(__ppc64__) || defined(__powerpc64__) || \
138 defined(__ia64__) || defined(__alpha__) || \
139 (defined(__sparc__) && defined(__arch64__)) || \
140 defined(__s390x__) || defined(__mips64) || \
141 defined(__aarch64__))
142 #if !defined(MBEDTLS_HAVE_INT64)
143 #define MBEDTLS_HAVE_INT64
144 #endif /* MBEDTLS_HAVE_INT64 */
145typedef int64_t mbedtls_mpi_sint;
146typedef uint64_t mbedtls_mpi_uint;
147 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
148/* mbedtls_t_udbl defined as 128-bit unsigned int */
149typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
150 #define MBEDTLS_HAVE_UDBL
151 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
152 #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
153/*
154 * __ARMCC_VERSION is defined for both armcc and armclang and
155 * __aarch64__ is only defined by armclang when compiling 64-bit code
156 */
157 #if !defined(MBEDTLS_HAVE_INT64)
158 #define MBEDTLS_HAVE_INT64
159 #endif /* !MBEDTLS_HAVE_INT64 */
160typedef int64_t mbedtls_mpi_sint;
161typedef uint64_t mbedtls_mpi_uint;
162 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
163/* mbedtls_t_udbl defined as 128-bit unsigned int */
164typedef __uint128_t mbedtls_t_udbl;
165 #define MBEDTLS_HAVE_UDBL
166 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
167 #elif defined(MBEDTLS_HAVE_INT64)
168/* Force 64-bit integers with unknown compiler */
169typedef int64_t mbedtls_mpi_sint;
170typedef uint64_t mbedtls_mpi_uint;
171 #endif
172#endif /* !MBEDTLS_HAVE_INT32 */
173
174#if !defined(MBEDTLS_HAVE_INT64)
175/* Default to 32-bit compilation */
176 #if !defined(MBEDTLS_HAVE_INT32)
177 #define MBEDTLS_HAVE_INT32
178 #endif /* !MBEDTLS_HAVE_INT32 */
179typedef int32_t mbedtls_mpi_sint;
180typedef uint32_t mbedtls_mpi_uint;
181 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
182typedef uint64_t mbedtls_t_udbl;
183 #define MBEDTLS_HAVE_UDBL
184 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
185#endif /* !MBEDTLS_HAVE_INT64 */
186
201#ifdef __cplusplus
202extern "C" {
203#endif
204
208typedef struct mbedtls_mpi {
220 int s;
221
223 size_t n;
224
230}
232
242
251
265int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs);
266
282int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs);
283
298
306
335int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign);
336
364int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char swap);
365
377
388int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos);
389
405int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val);
406
420
434
449
460int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s);
461
484int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix,
485 char *buf, size_t buflen, size_t *olen);
486
487#if defined(MBEDTLS_FS_IO)
509int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin);
510
526int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X,
527 int radix, FILE *fout);
528#endif /* MBEDTLS_FS_IO */
529
542int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf,
543 size_t buflen);
544
558 const unsigned char *buf, size_t buflen);
559
575int mbedtls_mpi_write_binary(const mbedtls_mpi *X, unsigned char *buf,
576 size_t buflen);
577
594 unsigned char *buf, size_t buflen);
595
606int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count);
607
618int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count);
619
631
643
660 unsigned *ret);
661
673
686 const mbedtls_mpi *B);
687
701 const mbedtls_mpi *B);
702
715 const mbedtls_mpi *B);
716
729 const mbedtls_mpi *B);
730
744
759
773 const mbedtls_mpi *B);
774
790
810 const mbedtls_mpi *B);
811
832
851 const mbedtls_mpi *B);
852
871
900 const mbedtls_mpi *E, const mbedtls_mpi *N,
901 mbedtls_mpi *prec_RR);
902
921 int (*f_rng)(void *, unsigned char *, size_t),
922 void *p_rng);
923
958 const mbedtls_mpi *N,
959 int (*f_rng)(void *, unsigned char *, size_t),
960 void *p_rng);
961
974 const mbedtls_mpi *B);
975
993 const mbedtls_mpi *N);
994
995#if !defined(MBEDTLS_DEPRECATED_REMOVED)
996#if defined(MBEDTLS_DEPRECATED_WARNING)
997#define MBEDTLS_DEPRECATED __attribute__((deprecated))
998#else
999#define MBEDTLS_DEPRECATED
1000#endif
1021 int (*f_rng)(void *, unsigned char *, size_t),
1022 void *p_rng);
1023#undef MBEDTLS_DEPRECATED
1024#endif /* !MBEDTLS_DEPRECATED_REMOVED */
1025
1053int mbedtls_mpi_is_prime_ext(const mbedtls_mpi *X, int rounds,
1054 int (*f_rng)(void *, unsigned char *, size_t),
1055 void *p_rng);
1062typedef enum {
1066
1086int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int flags,
1087 int (*f_rng)(void *, unsigned char *, size_t),
1088 void *p_rng);
1089
1090#if defined(MBEDTLS_SELF_TEST)
1091
1097int mbedtls_mpi_self_test(int verbose);
1098
1099#endif /* MBEDTLS_SELF_TEST */
1100
1101#ifdef __cplusplus
1102}
1103#endif
1104
1105#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 sliding-window 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:179
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:1062
@ MBEDTLS_MPI_GEN_PRIME_FLAG_DH
Definition: bignum.h:1063
@ MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR
Definition: bignum.h:1064
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.
#define MBEDTLS_DEPRECATED
Definition: bignum.h:999
uint32_t mbedtls_mpi_uint
The type of machine digits in a bignum, called limbs.
Definition: bignum.h:180
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.
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_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:182
Configuration options (set of defines)
MPI structure.
Definition: bignum.h:208
size_t n
Definition: bignum.h:223
mbedtls_mpi_uint * p
Definition: bignum.h:229