mbed TLS v2.7.17
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  * This file is provided under the Apache License 2.0, or the
11  * GNU General Public License v2.0 or later.
12  *
13  * **********
14  * Apache License 2.0:
15  *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may
17  * not use this file except in compliance with the License.
18  * You may obtain a copy of the License at
19  *
20  * http://www.apache.org/licenses/LICENSE-2.0
21  *
22  * Unless required by applicable law or agreed to in writing, software
23  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
24  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25  * See the License for the specific language governing permissions and
26  * limitations under the License.
27  *
28  * **********
29  *
30  * **********
31  * GNU General Public License v2.0 or later:
32  *
33  * This program is free software; you can redistribute it and/or modify
34  * it under the terms of the GNU General Public License as published by
35  * the Free Software Foundation; either version 2 of the License, or
36  * (at your option) any later version.
37  *
38  * This program is distributed in the hope that it will be useful,
39  * but WITHOUT ANY WARRANTY; without even the implied warranty of
40  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41  * GNU General Public License for more details.
42  *
43  * You should have received a copy of the GNU General Public License along
44  * with this program; if not, write to the Free Software Foundation, Inc.,
45  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
46  *
47  * **********
48  */
49 #ifndef MBEDTLS_BIGNUM_H
50 #define MBEDTLS_BIGNUM_H
51 
52 #if !defined(MBEDTLS_CONFIG_FILE)
53 #include "config.h"
54 #else
55 #include MBEDTLS_CONFIG_FILE
56 #endif
57 
58 #include <stddef.h>
59 #include <stdint.h>
60 
61 #if defined(MBEDTLS_FS_IO)
62 #include <stdio.h>
63 #endif
64 
65 #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002
66 #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004
67 #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006
68 #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008
69 #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A
70 #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C
71 #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E
72 #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010
74 #define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
75 
76 /*
77  * Maximum size MPIs are allowed to grow to in number of limbs.
78  */
79 #define MBEDTLS_MPI_MAX_LIMBS 10000
80 
81 #if !defined(MBEDTLS_MPI_WINDOW_SIZE)
82 /*
83  * Maximum window size used for modular exponentiation. Default: 6
84  * Minimum value: 1. Maximum value: 6.
85  *
86  * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
87  * for the sliding window calculation. (So 64 by default)
88  *
89  * Reduction in size, reduces speed.
90  */
91 #define MBEDTLS_MPI_WINDOW_SIZE 6
92 #endif /* !MBEDTLS_MPI_WINDOW_SIZE */
93 
94 #if !defined(MBEDTLS_MPI_MAX_SIZE)
95 /*
96  * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
97  * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
98  *
99  * Note: Calculations can temporarily result in larger MPIs. So the number
100  * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
101  */
102 #define MBEDTLS_MPI_MAX_SIZE 1024
103 #endif /* !MBEDTLS_MPI_MAX_SIZE */
104 
105 #define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE )
107 /*
108  * When reading from files with mbedtls_mpi_read_file() and writing to files with
109  * mbedtls_mpi_write_file() the buffer should have space
110  * for a (short) label, the MPI (in the provided radix), the newline
111  * characters and the '\0'.
112  *
113  * By default we assume at least a 10 char label, a minimum radix of 10
114  * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
115  * Autosized at compile time for at least a 10 char label, a minimum radix
116  * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
117  *
118  * This used to be statically sized to 1250 for a maximum of 4096 bit
119  * numbers (1234 decimal chars).
120  *
121  * Calculate using the formula:
122  * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
123  * LabelSize + 6
124  */
125 #define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
126 #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
127 #define MBEDTLS_MPI_RW_BUFFER_SIZE ( ((MBEDTLS_MPI_MAX_BITS_SCALE100 + MBEDTLS_LN_2_DIV_LN_10_SCALE100 - 1) / MBEDTLS_LN_2_DIV_LN_10_SCALE100) + 10 + 6 )
128 
129 /*
130  * Define the base integer type, architecture-wise.
131  *
132  * 32 or 64-bit integer types can be forced regardless of the underlying
133  * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
134  * respectively and undefining MBEDTLS_HAVE_ASM.
135  *
136  * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
137  * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
138  */
139 #if !defined(MBEDTLS_HAVE_INT32)
140  #if defined(_MSC_VER) && defined(_M_AMD64)
141  /* Always choose 64-bit when using MSC */
142  #if !defined(MBEDTLS_HAVE_INT64)
143  #define MBEDTLS_HAVE_INT64
144  #endif /* !MBEDTLS_HAVE_INT64 */
145  typedef int64_t mbedtls_mpi_sint;
146  typedef uint64_t mbedtls_mpi_uint;
147  #elif defined(__GNUC__) && ( \
148  defined(__amd64__) || defined(__x86_64__) || \
149  defined(__ppc64__) || defined(__powerpc64__) || \
150  defined(__ia64__) || defined(__alpha__) || \
151  ( defined(__sparc__) && defined(__arch64__) ) || \
152  defined(__s390x__) || defined(__mips64) )
153  #if !defined(MBEDTLS_HAVE_INT64)
154  #define MBEDTLS_HAVE_INT64
155  #endif /* MBEDTLS_HAVE_INT64 */
156  typedef int64_t mbedtls_mpi_sint;
157  typedef uint64_t mbedtls_mpi_uint;
158  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
159  /* mbedtls_t_udbl defined as 128-bit unsigned int */
160  typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
161  #define MBEDTLS_HAVE_UDBL
162  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
163  #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
164  /*
165  * __ARMCC_VERSION is defined for both armcc and armclang and
166  * __aarch64__ is only defined by armclang when compiling 64-bit code
167  */
168  #if !defined(MBEDTLS_HAVE_INT64)
169  #define MBEDTLS_HAVE_INT64
170  #endif /* !MBEDTLS_HAVE_INT64 */
171  typedef int64_t mbedtls_mpi_sint;
172  typedef uint64_t mbedtls_mpi_uint;
173  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
174  /* mbedtls_t_udbl defined as 128-bit unsigned int */
175  typedef __uint128_t mbedtls_t_udbl;
176  #define MBEDTLS_HAVE_UDBL
177  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
178  #elif defined(MBEDTLS_HAVE_INT64)
179  /* Force 64-bit integers with unknown compiler */
180  typedef int64_t mbedtls_mpi_sint;
181  typedef uint64_t mbedtls_mpi_uint;
182  #endif
183 #endif /* !MBEDTLS_HAVE_INT32 */
184 
185 #if !defined(MBEDTLS_HAVE_INT64)
186  /* Default to 32-bit compilation */
187  #if !defined(MBEDTLS_HAVE_INT32)
188  #define MBEDTLS_HAVE_INT32
189  #endif /* !MBEDTLS_HAVE_INT32 */
190  typedef int32_t mbedtls_mpi_sint;
191  typedef uint32_t mbedtls_mpi_uint;
192  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
193  typedef uint64_t mbedtls_t_udbl;
194  #define MBEDTLS_HAVE_UDBL
195  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
196 #endif /* !MBEDTLS_HAVE_INT64 */
197 
198 #ifdef __cplusplus
199 extern "C" {
200 #endif
201 
205 typedef struct
206 {
207  int s;
208  size_t n;
209  mbedtls_mpi_uint *p;
210 }
212 
220 void mbedtls_mpi_init( mbedtls_mpi *X );
221 
227 void mbedtls_mpi_free( mbedtls_mpi *X );
228 
238 int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
239 
249 int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
250 
260 int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
261 
269 
287 int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
288 
306 int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
307 
317 int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
318 
327 int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
328 
343 int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
344 
353 size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
354 
363 size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
364 
370 size_t mbedtls_mpi_size( const mbedtls_mpi *X );
371 
381 int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
382 
399 int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
400  char *buf, size_t buflen, size_t *olen );
401 
402 #if defined(MBEDTLS_FS_IO)
403 
423 int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
424 
437 int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
438 #endif /* MBEDTLS_FS_IO */
439 
450 int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
451 
464 int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
465 
475 int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
476 
486 int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
487 
498 int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
499 
510 int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
511 
527 int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
528  unsigned *ret );
529 
540 int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
541 
552 int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
553 
564 int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
565 
576 int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
577 
588 int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
589 
600 int mbedtls_mpi_add_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
601 
612 int mbedtls_mpi_sub_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b );
613 
624 int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
625 
638 int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b );
639 
654 int mbedtls_mpi_div_mpi( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
655 
670 int mbedtls_mpi_div_int( mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b );
671 
684 int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
685 
698 int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b );
699 
718 int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR );
719 
735 int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
736  int (*f_rng)(void *, unsigned char *, size_t),
737  void *p_rng );
738 
749 int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
750 
763 int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
764 
776 int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
777  int (*f_rng)(void *, unsigned char *, size_t),
778  void *p_rng );
779 
794 int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
795  int (*f_rng)(void *, unsigned char *, size_t),
796  void *p_rng );
797 
803 int mbedtls_mpi_self_test( int verbose );
804 
805 #ifdef __cplusplus
806 }
807 #endif
808 
809 #endif /* bignum.h */
int mbedtls_mpi_div_mpi(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Division by mbedtls_mpi: A = Q * B + R.
void mbedtls_mpi_free(mbedtls_mpi *X)
Unallocate one MPI.
int mbedtls_mpi_gcd(mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B)
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 X with size bytes of random.
int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Signed subtraction: X = A - b.
size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
Return the number of zero-bits before the least significant &#39;1&#39; bit.
int mbedtls_mpi_grow(mbedtls_mpi *X, size_t nblimbs)
Enlarge to the specified number of limbs.
int mbedtls_mpi_safe_cond_assign(mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign)
Safe conditional assignement X = Y if assign is 1.
int mbedtls_mpi_mod_mpi(mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B)
Modulo: R = A mod B.
int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR)
Sliding-window exponentiation: X = A^E mod N.
int32_t mbedtls_mpi_sint
Definition: bignum.h:190
size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
Return the number of bits up to and including the most significant &#39;1&#39; bit&#39;.
int mbedtls_mpi_cmp_mpi(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Compare signed values.
size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Return the total size in bytes.
int mbedtls_mpi_sub_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Signed subtraction: X = A - B.
int mbedtls_mpi_sub_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Unsigned subtraction: X = |A| - |B|.
Configuration options (set of defines)
int mbedtls_mpi_read_string(mbedtls_mpi *X, int radix, const char *s)
Import from an ASCII string.
uint64_t mbedtls_t_udbl
Definition: bignum.h:193
int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
Set a bit of X to a specific value of 0 or 1.
int mbedtls_mpi_lset(mbedtls_mpi *X, mbedtls_mpi_sint z)
Set value from integer.
int mbedtls_mpi_div_int(mbedtls_mpi *Q, mbedtls_mpi *R, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Division by int: A = Q * b + R.
int mbedtls_mpi_read_binary(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Import X from unsigned binary data, big endian.
mbedtls_mpi_uint * p
Definition: bignum.h:209
int mbedtls_mpi_shrink(mbedtls_mpi *X, size_t nblimbs)
Resize down, 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_add_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Signed addition: X = A + b.
int mbedtls_mpi_shift_r(mbedtls_mpi *X, size_t count)
Right-shift: X &gt;&gt;= count.
int mbedtls_mpi_gen_prime(mbedtls_mpi *X, size_t nbits, int dh_flag, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Prime number generation.
int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Modulo: r = A mod b.
int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
Left-shift: X &lt;&lt;= count.
mbedtls_asn1_buf val
Definition: asn1.h:194
int mbedtls_mpi_mul_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b)
Baseline multiplication: X = A * b.
int mbedtls_mpi_is_prime(const mbedtls_mpi *X, int(*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Miller-Rabin primality test.
uint32_t mbedtls_mpi_uint
Definition: bignum.h:191
int mbedtls_mpi_self_test(int verbose)
Checkup routine.
int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign)
Safe conditional swap X &lt;-&gt; Y if swap is 1.
void mbedtls_mpi_init(mbedtls_mpi *X)
Initialize one MPI (make internal references valid) This just makes it ready to be set or freed...
void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
Swap the contents of X and Y.
int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Signed addition: X = A + B.
int mbedtls_mpi_inv_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N)
Modular inverse: X = A^-1 mod N.
MPI structure.
Definition: bignum.h:205
int mbedtls_mpi_copy(mbedtls_mpi *X, const mbedtls_mpi *Y)
Copy the contents of Y into X.
int mbedtls_mpi_write_string(const mbedtls_mpi *X, int radix, char *buf, size_t buflen, size_t *olen)
Export into an ASCII string.
int mbedtls_mpi_read_file(mbedtls_mpi *X, int radix, FILE *fin)
Read MPI from a line in an opened file.
int mbedtls_mpi_write_file(const char *p, const mbedtls_mpi *X, int radix, FILE *fout)
Write X into an opened file, or stdout if fout is NULL.
size_t n
Definition: bignum.h:208
int mbedtls_mpi_add_abs(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Unsigned addition: X = |A| + |B|.
int mbedtls_mpi_cmp_abs(const mbedtls_mpi *X, const mbedtls_mpi *Y)
Compare unsigned values.
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_mul_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Baseline multiplication: X = A * B.
int mbedtls_mpi_cmp_int(const mbedtls_mpi *X, mbedtls_mpi_sint z)
Compare signed values.
int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
Get a specific bit from X.