ReactOS  0.4.15-dev-1397-g19779b3
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 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  * This file is part of mbed TLS (https://tls.mbed.org)
50  */
51 #ifndef MBEDTLS_BIGNUM_H
52 #define MBEDTLS_BIGNUM_H
53 
54 #if !defined(MBEDTLS_CONFIG_FILE)
55 #include "config.h"
56 #else
57 #include MBEDTLS_CONFIG_FILE
58 #endif
59 
60 #include <stddef.h>
61 #include <stdint.h>
62 
63 #if defined(MBEDTLS_FS_IO)
64 #include <stdio.h>
65 #endif
66 
67 #define MBEDTLS_ERR_MPI_FILE_IO_ERROR -0x0002
68 #define MBEDTLS_ERR_MPI_BAD_INPUT_DATA -0x0004
69 #define MBEDTLS_ERR_MPI_INVALID_CHARACTER -0x0006
70 #define MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL -0x0008
71 #define MBEDTLS_ERR_MPI_NEGATIVE_VALUE -0x000A
72 #define MBEDTLS_ERR_MPI_DIVISION_BY_ZERO -0x000C
73 #define MBEDTLS_ERR_MPI_NOT_ACCEPTABLE -0x000E
74 #define MBEDTLS_ERR_MPI_ALLOC_FAILED -0x0010
76 #define MBEDTLS_MPI_CHK(f) do { if( ( ret = f ) != 0 ) goto cleanup; } while( 0 )
77 
78 /*
79  * Maximum size MPIs are allowed to grow to in number of limbs.
80  */
81 #define MBEDTLS_MPI_MAX_LIMBS 10000
82 
83 #if !defined(MBEDTLS_MPI_WINDOW_SIZE)
84 /*
85  * Maximum window size used for modular exponentiation. Default: 6
86  * Minimum value: 1. Maximum value: 6.
87  *
88  * Result is an array of ( 2 << MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
89  * for the sliding window calculation. (So 64 by default)
90  *
91  * Reduction in size, reduces speed.
92  */
93 #define MBEDTLS_MPI_WINDOW_SIZE 6
94 #endif /* !MBEDTLS_MPI_WINDOW_SIZE */
95 
96 #if !defined(MBEDTLS_MPI_MAX_SIZE)
97 /*
98  * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
99  * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
100  *
101  * Note: Calculations can temporarily result in larger MPIs. So the number
102  * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
103  */
104 #define MBEDTLS_MPI_MAX_SIZE 1024
105 #endif /* !MBEDTLS_MPI_MAX_SIZE */
106 
107 #define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE )
109 /*
110  * When reading from files with mbedtls_mpi_read_file() and writing to files with
111  * mbedtls_mpi_write_file() the buffer should have space
112  * for a (short) label, the MPI (in the provided radix), the newline
113  * characters and the '\0'.
114  *
115  * By default we assume at least a 10 char label, a minimum radix of 10
116  * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
117  * Autosized at compile time for at least a 10 char label, a minimum radix
118  * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
119  *
120  * This used to be statically sized to 1250 for a maximum of 4096 bit
121  * numbers (1234 decimal chars).
122  *
123  * Calculate using the formula:
124  * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
125  * LabelSize + 6
126  */
127 #define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
128 #define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
129 #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 )
130 
131 /*
132  * Define the base integer type, architecture-wise.
133  *
134  * 32 or 64-bit integer types can be forced regardless of the underlying
135  * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
136  * respectively and undefining MBEDTLS_HAVE_ASM.
137  *
138  * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
139  * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
140  */
141 #if !defined(MBEDTLS_HAVE_INT32)
142  #if defined(_MSC_VER) && defined(_M_AMD64)
143  /* Always choose 64-bit when using MSC */
144  #if !defined(MBEDTLS_HAVE_INT64)
145  #define MBEDTLS_HAVE_INT64
146  #endif /* !MBEDTLS_HAVE_INT64 */
147  typedef int64_t mbedtls_mpi_sint;
148  typedef uint64_t mbedtls_mpi_uint;
149  #elif defined(__GNUC__) && ( \
150  defined(__amd64__) || defined(__x86_64__) || \
151  defined(__ppc64__) || defined(__powerpc64__) || \
152  defined(__ia64__) || defined(__alpha__) || \
153  ( defined(__sparc__) && defined(__arch64__) ) || \
154  defined(__s390x__) || defined(__mips64) )
155  #if !defined(MBEDTLS_HAVE_INT64)
156  #define MBEDTLS_HAVE_INT64
157  #endif /* MBEDTLS_HAVE_INT64 */
158  typedef int64_t mbedtls_mpi_sint;
159  typedef uint64_t mbedtls_mpi_uint;
160  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
161  /* mbedtls_t_udbl defined as 128-bit unsigned int */
162  typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
163  #define MBEDTLS_HAVE_UDBL
164  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
165  #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
166  /*
167  * __ARMCC_VERSION is defined for both armcc and armclang and
168  * __aarch64__ is only defined by armclang when compiling 64-bit code
169  */
170  #if !defined(MBEDTLS_HAVE_INT64)
171  #define MBEDTLS_HAVE_INT64
172  #endif /* !MBEDTLS_HAVE_INT64 */
173  typedef int64_t mbedtls_mpi_sint;
174  typedef uint64_t mbedtls_mpi_uint;
175  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
176  /* mbedtls_t_udbl defined as 128-bit unsigned int */
177  typedef __uint128_t mbedtls_t_udbl;
178  #define MBEDTLS_HAVE_UDBL
179  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
180  #elif defined(MBEDTLS_HAVE_INT64)
181  /* Force 64-bit integers with unknown compiler */
182  typedef int64_t mbedtls_mpi_sint;
183  typedef uint64_t mbedtls_mpi_uint;
184  #endif
185 #endif /* !MBEDTLS_HAVE_INT32 */
186 
187 #if !defined(MBEDTLS_HAVE_INT64)
188  /* Default to 32-bit compilation */
189  #if !defined(MBEDTLS_HAVE_INT32)
190  #define MBEDTLS_HAVE_INT32
191  #endif /* !MBEDTLS_HAVE_INT32 */
194  #if !defined(MBEDTLS_NO_UDBL_DIVISION)
196  #define MBEDTLS_HAVE_UDBL
197  #endif /* !MBEDTLS_NO_UDBL_DIVISION */
198 #endif /* !MBEDTLS_HAVE_INT64 */
199 
200 #ifdef __cplusplus
201 extern "C" {
202 #endif
203 
207 typedef struct
208 {
209  int s;
210  size_t n;
212 }
214 
223 
230 
240 int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
241 
251 int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
252 
262 int mbedtls_mpi_copy( mbedtls_mpi *X, const mbedtls_mpi *Y );
263 
271 
289 int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
290 
308 int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
309 
320 
329 int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
330 
345 int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
346 
355 size_t mbedtls_mpi_lsb( const mbedtls_mpi *X );
356 
365 size_t mbedtls_mpi_bitlen( const mbedtls_mpi *X );
366 
372 size_t mbedtls_mpi_size( const mbedtls_mpi *X );
373 
383 int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
384 
401 int mbedtls_mpi_write_string( const mbedtls_mpi *X, int radix,
402  char *buf, size_t buflen, size_t *olen );
403 
404 #if defined(MBEDTLS_FS_IO)
405 
425 int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
426 
439 int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X, int radix, FILE *fout );
440 #endif /* MBEDTLS_FS_IO */
441 
452 int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf, size_t buflen );
453 
466 int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf, size_t buflen );
467 
477 int mbedtls_mpi_shift_l( mbedtls_mpi *X, size_t count );
478 
488 int mbedtls_mpi_shift_r( mbedtls_mpi *X, size_t count );
489 
500 int mbedtls_mpi_cmp_abs( const mbedtls_mpi *X, const mbedtls_mpi *Y );
501 
512 int mbedtls_mpi_cmp_mpi( const mbedtls_mpi *X, const mbedtls_mpi *Y );
513 
529 int mbedtls_mpi_lt_mpi_ct( const mbedtls_mpi *X, const mbedtls_mpi *Y,
530  unsigned *ret );
531 
543 
554 int mbedtls_mpi_add_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
555 
566 int mbedtls_mpi_sub_abs( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
567 
578 int mbedtls_mpi_add_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
579 
590 int mbedtls_mpi_sub_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
591 
603 
615 
626 int mbedtls_mpi_mul_mpi( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B );
627 
641 
657 
673 
686 int mbedtls_mpi_mod_mpi( mbedtls_mpi *R, const mbedtls_mpi *A, const mbedtls_mpi *B );
687 
701 
720 int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *E, const mbedtls_mpi *N, mbedtls_mpi *_RR );
721 
738  int (*f_rng)(void *, unsigned char *, size_t),
739  void *p_rng );
740 
751 int mbedtls_mpi_gcd( mbedtls_mpi *G, const mbedtls_mpi *A, const mbedtls_mpi *B );
752 
765 int mbedtls_mpi_inv_mod( mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *N );
766 
779  int (*f_rng)(void *, unsigned char *, size_t),
780  void *p_rng );
781 
796 int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int dh_flag,
797  int (*f_rng)(void *, unsigned char *, size_t),
798  void *p_rng );
799 
805 int mbedtls_mpi_self_test( int verbose );
806 
807 #ifdef __cplusplus
808 }
809 #endif
810 
811 #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.
#define R(b, x)
Definition: sha2.c:134
Definition: ehthrow.cxx:53
int mbedtls_mpi_sub_int(mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Signed subtraction: X = A - b.
#define Y(I)
size_t mbedtls_mpi_lsb(const mbedtls_mpi *X)
Return the number of zero-bits before the least significant '1' bit.
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLuint GLuint GLsizei count
Definition: gl.h:1545
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:192
size_t mbedtls_mpi_bitlen(const mbedtls_mpi *X)
Return the number of bits up to and including the most significant '1' bit'.
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|.
ULONG G
Definition: xcpt4u.c:549
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:195
Definition: ehthrow.cxx:92
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.
#define verbose
Definition: rosglue.h:36
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
#define __attribute__(x)
Definition: wpp_private.h:207
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:211
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.
GLdouble GLdouble z
Definition: glext.h:5874
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 >>= 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.
GLuint GLfloat * val
Definition: glext.h:7180
int mbedtls_mpi_mod_int(mbedtls_mpi_uint *r, const mbedtls_mpi *A, mbedtls_mpi_sint b)
Modulo: r = A mod b.
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLsizeiptr size
Definition: glext.h:5919
int mbedtls_mpi_shift_l(mbedtls_mpi *X, size_t count)
Left-shift: X <<= count.
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:193
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 <-> Y if swap is 1.
int ret
void mbedtls_mpi_init(mbedtls_mpi *X)
Initialize one MPI (make internal references valid) This just makes it ready to be set or freed,...
GLdouble s
Definition: gl.h:2039
GLenum mode
Definition: glext.h:6217
void mbedtls_mpi_swap(mbedtls_mpi *X, mbedtls_mpi *Y)
Swap the contents of X and Y.
INT32 int32_t
Definition: types.h:71
int mbedtls_mpi_add_mpi(mbedtls_mpi *X, const mbedtls_mpi *A, const mbedtls_mpi *B)
Signed addition: X = A + B.
static const WCHAR E[]
Definition: oid.c:1253
UINT64 uint64_t
Definition: types.h:77
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:207
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.
size_t n
Definition: bignum.h:210
UINT32 uint32_t
Definition: types.h:75
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.
GLfloat GLfloat p
Definition: glext.h:8902
int mbedtls_mpi_get_bit(const mbedtls_mpi *X, size_t pos)
Get a specific bit from X.
INT64 int64_t
Definition: types.h:72