Digi XBee(R) ANSI C Host Library
bignum.h
Go to the documentation of this file.
1 
6 /*
7  * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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  * This file is part of mbed TLS (https://tls.mbed.org)
23  */
24 #ifndef MBEDTLS_BIGNUM_H
25 #define MBEDTLS_BIGNUM_H
26 
27 #if !defined(MBEDTLS_CONFIG_FILE)
28 #include "mbedtls/config.h"
29 #else
30 #include MBEDTLS_CONFIG_FILE
31 #endif
32 
33 #include <stddef.h>
34 #include <stdint.h>
35 
36 #if defined(MBEDTLS_FS_IO)
37 #include <stdio.h>
38 #endif
39 
40 #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002
41 #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004
42 #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006
43 #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008
44 #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A
45 #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C
46 #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E
47 #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010
49 #define MBEDTLS_MPI_CHK(f) \
50  do \
51  { \
52  if( ( ret = (f) ) != 0 ) \
53  goto cleanup; \
54  } while( 0 )
55 
56 /*
57  * Maximum size MPIs are allowed to grow to in number of limbs.
58  */
59 #define MBEDTLS_MPI_MAX_LIMBS 10000
60 
61 #if !defined(MBEDTLS_MPI_WINDOW_SIZE)
62 /*
63  * Maximum window size used for modular exponentiation. Default: 6
64  * Minimum value: 1. Maximum value: 6.
65  *
66  * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
67  * for the sliding window calculation. (So 64 by default)
68  *
69  * Reduction in size, reduces speed.
70  */
71 #define MBEDTLS_MPI_WINDOW_SIZE 6
72 #endif /* !MBEDTLS_MPI_WINDOW_SIZE */
73 
74 #if !defined(MBEDTLS_MPI_MAX_SIZE)
75 /*
76  * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
77  * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
78  *
79  * Note: Calculations can temporarily result in larger MPIs. So the number
80  * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
81  */
82 #define MBEDTLS_MPI_MAX_SIZE 1024
83 #endif /* !MBEDTLS_MPI_MAX_SIZE */
84 
85 #define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE )
87 /*
88  * When reading from files with mbedtls_mpi_read_file() and writing to files with
89  * mbedtls_mpi_write_file() the buffer should have space
90  * for a (short) label, the MPI (in the provided radix), the newline
91  * characters and the '\0'.
92  *
93  * By default we assume at least a 10 char label, a minimum radix of 10
94  * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
95  * Autosized at compile time for at least a 10 char label, a minimum radix
96  * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
97  *
98  * This used to be statically sized to 1250 for a maximum of 4096 bit
99  * numbers (1234 decimal chars).
100  *
101  * Calculate using the formula:
102  * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
103  * LabelSize + 6
104  */
105 #define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
106 #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
107 #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 )
108 
109 /*
110  * Define the base integer type, architecture-wise.
111  *
112  * 32 or 64-bit integer types can be forced regardless of the underlying
113  * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
114  * respectively and undefining MBEDTLS_HAVE_ASM.
115  *
116  * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
117  * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
118  */
119 #if !defined(MBEDTLS_HAVE_INT32)
120  #if defined(_MSC_VER) && defined(_M_AMD64)
121  /* Always choose 64-bit when using MSC */
122  #if !defined(MBEDTLS_HAVE_INT64)
123  #define MBEDTLS_HAVE_INT64
124  #endif /* !MBEDTLS_HAVE_INT64 */
125  typedef int64_t mbedtls_mpi_sint;
126  typedef uint64_t mbedtls_mpi_uint;
127  #elif defined(__GNUC__) && ( \
128  defined(__amd64__) || defined(__x86_64__) || \
129  defined(__ppc64__) || defined(__powerpc64__) || \
130  defined(__ia64__) || defined(__alpha__) || \
131  ( defined(__sparc__) && defined(__arch64__) ) || \
132  defined(__s390x__) || defined(__mips64) || \
133  defined(__aarch64__) )
134  #if !defined(MBEDTLS_HAVE_INT64)
135  #define MBEDTLS_HAVE_INT64
136  #endif /* MBEDTLS_HAVE_INT64 */
137  typedef int64_t mbedtls_mpi_sint;
138  typedef uint64_t mbedtls_mpi_uint;
139  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
140  /* mbedtls_t_udbl defined as 128-bit unsigned int */
141  typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
142  #define MBEDTLS_HAVE_UDBL
143  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
144  #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
145  /*
146  * __ARMCC_VERSION is defined for both armcc and armclang and
147  * __aarch64__ is only defined by armclang when compiling 64-bit code
148  */
149  #if !defined(MBEDTLS_HAVE_INT64)
150  #define MBEDTLS_HAVE_INT64
151  #endif /* !MBEDTLS_HAVE_INT64 */
152  typedef int64_t mbedtls_mpi_sint;
153  typedef uint64_t mbedtls_mpi_uint;
154  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
155  /* mbedtls_t_udbl defined as 128-bit unsigned int */
156  typedef __uint128_t mbedtls_t_udbl;
157  #define MBEDTLS_HAVE_UDBL
158  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
159  #elif defined(MBEDTLS_HAVE_INT64)
160  /* Force 64-bit integers with unknown compiler */
161  typedef int64_t mbedtls_mpi_sint;
162  typedef uint64_t mbedtls_mpi_uint;
163  #endif
164 #endif /* !MBEDTLS_HAVE_INT32 */
165 
166 #if !defined(MBEDTLS_HAVE_INT64)
167  /* Default to 32-bit compilation */
168  #if !defined(MBEDTLS_HAVE_INT32)
169  #define MBEDTLS_HAVE_INT32
170  #endif /* !MBEDTLS_HAVE_INT32 */
171  typedef int32_t mbedtls_mpi_sint;
172  typedef uint32_t mbedtls_mpi_uint;
173  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
174  typedef uint64_t mbedtls_t_udbl;
175  #define MBEDTLS_HAVE_UDBL
176  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
177 #endif /* !MBEDTLS_HAVE_INT64 */
178 
179 #ifdef __cplusplus
180 extern "C" {
181 #endif
182 
186 typedef struct mbedtls_mpi
187 {
188  int s;
189  size_t n;
190  mbedtls_mpi_uint *p;
191 }
193 
202 void mbedtls_mpi_init( mbedtls_mpi *X );
203 
211 void mbedtls_mpi_free( mbedtls_mpi *X );
212 
226 int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
227 
243 int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
244 
258 int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
259 
267 
292 int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
293 
317 int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
318 
329 int mbedtls_mpi_lset( mbedtls_mpi *X, mbedtls_mpi_sint z );
330 
341 int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
342 
358 int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
359 
372 size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
373 
386 size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
387 
401 size_t mbedtls_mpi_size( const mbedtls_mpi *X );
402 
413 int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
414 
437 int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
438  char *buf, size_t buflen, size_t *olen );
439 
440 #if defined(MBEDTLS_FS_IO)
441 
462 int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
463 
479 int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X,
480  int radix, FILE *fout );
481 #endif /* MBEDTLS_FS_IO */
482 
495 int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf,
496  size_t buflen );
497 
511  const unsigned char *buf, size_t buflen );
512 
528 int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf,
529  size_t buflen );
530 
547  unsigned char *buf, size_t buflen );
548 
559 int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
560 
571 int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
572 
583 int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
584 
595 int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
596 
607 int mbedtls_mpi_cmp_int( const mbedtls_mpi *X, mbedtls_mpi_sint z );
608 
621  const mbedtls_mpi *B );
622 
636  const mbedtls_mpi *B );
637 
650  const mbedtls_mpi *B );
651 
664  const mbedtls_mpi *B );
665 
678  mbedtls_mpi_sint b );
679 
693  mbedtls_mpi_sint b );
694 
708  const mbedtls_mpi *B );
709 
724  mbedtls_mpi_uint b );
725 
745  const mbedtls_mpi *B );
746 
766  mbedtls_mpi_sint b );
767 
786  const mbedtls_mpi *B );
787 
804 int mbedtls_mpi_mod_int( mbedtls_mpi_uint *r, const mbedtls_mpi *A,
805  mbedtls_mpi_sint b );
806 
834  const mbedtls_mpi *E, const mbedtls_mpi *N,
835  mbedtls_mpi *_RR );
836 
854 int mbedtls_mpi_fill_random( mbedtls_mpi *X, size_t size,
855  int (*f_rng)(void *, unsigned char *, size_t),
856  void *p_rng );
857 
869 int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A,
870  const mbedtls_mpi *B );
871 
889  const mbedtls_mpi *N );
890 
891 #if !defined(MBEDTLS_DEPRECATED_REMOVED)
892 #if defined(MBEDTLS_DEPRECATED_WARNING)
893 #define MBEDTLS_DEPRECATED __attribute__((deprecated))
894 #else
895 #define MBEDTLS_DEPRECATED
896 #endif
897 
916 MBEDTLS_DEPRECATED int mbedtls_mpi_is_prime( const mbedtls_mpi *X,
917  int (*f_rng)(void *, unsigned char *, size_t),
918  void *p_rng );
919 #undef MBEDTLS_DEPRECATED
920 #endif /* !MBEDTLS_DEPRECATED_REMOVED */
921 
949 int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
950  int (*f_rng)(void *, unsigned char *, size_t),
951  void *p_rng );
958 typedef enum {
962 
982 int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
983  int (*f_rng)(void *, unsigned char *, size_t),
984  void *p_rng );
985 
986 #if defined(MBEDTLS_SELF_TEST)
987 
993 int mbedtls_mpi_self_test( int verbose );
994 
995 #endif /* MBEDTLS_SELF_TEST */
996 
997 #ifdef __cplusplus
998 }
999 #endif
1000 
1001 #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.
unsigned long uint32_t
32-bit unsigned integer
Definition: platform_config.h:43
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 n
Definition: bignum.h:189
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&#39;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.
int mbedtls_mpi_exp_mod(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR)
Perform a sliding-window exponentiation: X = A^E mod N.
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.
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.
(X-1)/2 is prime too
Definition: bignum.h:959
int s
Definition: bignum.h:188
int mbedtls_mpi_set_bit(mbedtls_mpi *X, size_t pos, unsigned char val)
Modify a specific bit in an MPI.
lower error rate from 2-80 to 2-128
Definition: bignum.h:960
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_read_binary_le(mbedtls_mpi *X, const unsigned char *buf, size_t buflen)
Import X from unsigned binary data, little endian.
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_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.
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.
int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
Perform a left-shift on an MPI: X <<= count.
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_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign)
Perform a safe conditional swap which doesn&#39;t reveal whether the condition was true or not...
void mbedtls_mpi_init(mbedtls_mpi *X)
Initialize an MPI context.
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:186
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.
mbedtls_mpi_uint * p
Definition: bignum.h:190
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_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.
long int32_t
32-bit signed integer
Definition: platform_config.h:42
mbedtls_mpi_gen_prime_flag_t
Flags for mbedtls_mpi_gen_prime()
Definition: bignum.h:958
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_get_bit(const mbedtls_mpi *X, size_t pos)
Get a specific bit from an MPI.
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.