ReactOS 0.4.15-dev-7889-g76290a6
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) \
75 do \
76 { \
77 if( ( ret = (f) ) != 0 ) \
78 goto cleanup; \
79 } while( 0 )
80
81/*
82 * Maximum size MPIs are allowed to grow to in number of limbs.
83 */
84#define MBEDTLS_MPI_MAX_LIMBS 10000
85
86#if !defined(MBEDTLS_MPI_WINDOW_SIZE)
87/*
88 * Maximum window size used for modular exponentiation. Default: 6
89 * Minimum value: 1. Maximum value: 6.
90 *
91 * Result is an array of ( 2 ** MBEDTLS_MPI_WINDOW_SIZE ) MPIs used
92 * for the sliding window calculation. (So 64 by default)
93 *
94 * Reduction in size, reduces speed.
95 */
96#define MBEDTLS_MPI_WINDOW_SIZE 6
97#endif /* !MBEDTLS_MPI_WINDOW_SIZE */
98
99#if !defined(MBEDTLS_MPI_MAX_SIZE)
100/*
101 * Maximum size of MPIs allowed in bits and bytes for user-MPIs.
102 * ( Default: 512 bytes => 4096 bits, Maximum tested: 2048 bytes => 16384 bits )
103 *
104 * Note: Calculations can temporarily result in larger MPIs. So the number
105 * of limbs required (MBEDTLS_MPI_MAX_LIMBS) is higher.
106 */
107#define MBEDTLS_MPI_MAX_SIZE 1024
108#endif /* !MBEDTLS_MPI_MAX_SIZE */
109
110#define MBEDTLS_MPI_MAX_BITS ( 8 * MBEDTLS_MPI_MAX_SIZE )
112/*
113 * When reading from files with mbedtls_mpi_read_file() and writing to files with
114 * mbedtls_mpi_write_file() the buffer should have space
115 * for a (short) label, the MPI (in the provided radix), the newline
116 * characters and the '\0'.
117 *
118 * By default we assume at least a 10 char label, a minimum radix of 10
119 * (decimal) and a maximum of 4096 bit numbers (1234 decimal chars).
120 * Autosized at compile time for at least a 10 char label, a minimum radix
121 * of 10 (decimal) for a number of MBEDTLS_MPI_MAX_BITS size.
122 *
123 * This used to be statically sized to 1250 for a maximum of 4096 bit
124 * numbers (1234 decimal chars).
125 *
126 * Calculate using the formula:
127 * MBEDTLS_MPI_RW_BUFFER_SIZE = ceil(MBEDTLS_MPI_MAX_BITS / ln(10) * ln(2)) +
128 * LabelSize + 6
129 */
130#define MBEDTLS_MPI_MAX_BITS_SCALE100 ( 100 * MBEDTLS_MPI_MAX_BITS )
131#define MBEDTLS_LN_2_DIV_LN_10_SCALE100 332
132#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 )
133
134/*
135 * Define the base integer type, architecture-wise.
136 *
137 * 32 or 64-bit integer types can be forced regardless of the underlying
138 * architecture by defining MBEDTLS_HAVE_INT32 or MBEDTLS_HAVE_INT64
139 * respectively and undefining MBEDTLS_HAVE_ASM.
140 *
141 * Double-width integers (e.g. 128-bit in 64-bit architectures) can be
142 * disabled by defining MBEDTLS_NO_UDBL_DIVISION.
143 */
144#if !defined(MBEDTLS_HAVE_INT32)
145 #if defined(_MSC_VER) && defined(_M_AMD64)
146 /* Always choose 64-bit when using MSC */
147 #if !defined(MBEDTLS_HAVE_INT64)
148 #define MBEDTLS_HAVE_INT64
149 #endif /* !MBEDTLS_HAVE_INT64 */
152 #elif defined(__GNUC__) && ( \
153 defined(__amd64__) || defined(__x86_64__) || \
154 defined(__ppc64__) || defined(__powerpc64__) || \
155 defined(__ia64__) || defined(__alpha__) || \
156 ( defined(__sparc__) && defined(__arch64__) ) || \
157 defined(__s390x__) || defined(__mips64) )
158 #if !defined(MBEDTLS_HAVE_INT64)
159 #define MBEDTLS_HAVE_INT64
160 #endif /* MBEDTLS_HAVE_INT64 */
163 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
164 /* mbedtls_t_udbl defined as 128-bit unsigned int */
165 typedef unsigned int mbedtls_t_udbl __attribute__((mode(TI)));
166 #define MBEDTLS_HAVE_UDBL
167 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
168 #elif defined(__ARMCC_VERSION) && defined(__aarch64__)
169 /*
170 * __ARMCC_VERSION is defined for both armcc and armclang and
171 * __aarch64__ is only defined by armclang when compiling 64-bit code
172 */
173 #if !defined(MBEDTLS_HAVE_INT64)
174 #define MBEDTLS_HAVE_INT64
175 #endif /* !MBEDTLS_HAVE_INT64 */
178 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
179 /* mbedtls_t_udbl defined as 128-bit unsigned int */
180 typedef __uint128_t mbedtls_t_udbl;
181 #define MBEDTLS_HAVE_UDBL
182 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
183 #elif defined(MBEDTLS_HAVE_INT64)
184 /* Force 64-bit integers with unknown compiler */
187 #endif
188#endif /* !MBEDTLS_HAVE_INT32 */
189
190#if !defined(MBEDTLS_HAVE_INT64)
191 /* Default to 32-bit compilation */
192 #if !defined(MBEDTLS_HAVE_INT32)
193 #define MBEDTLS_HAVE_INT32
194 #endif /* !MBEDTLS_HAVE_INT32 */
197 #if !defined(MBEDTLS_NO_UDBL_DIVISION)
199 #define MBEDTLS_HAVE_UDBL
200 #endif /* !MBEDTLS_NO_UDBL_DIVISION */
201#endif /* !MBEDTLS_HAVE_INT64 */
202
203#ifdef __cplusplus
204extern "C" {
205#endif
206
210typedef struct mbedtls_mpi
211{
212 int s;
213 size_t n;
215}
217
227
236
250int mbedtls_mpi_grow( mbedtls_mpi *X, size_t nblimbs );
251
267int mbedtls_mpi_shrink( mbedtls_mpi *X, size_t nblimbs );
268
283
291
316int mbedtls_mpi_safe_cond_assign( mbedtls_mpi *X, const mbedtls_mpi *Y, unsigned char assign );
317
341int mbedtls_mpi_safe_cond_swap( mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign );
342
354
365int mbedtls_mpi_get_bit( const mbedtls_mpi *X, size_t pos );
366
382int mbedtls_mpi_set_bit( mbedtls_mpi *X, size_t pos, unsigned char val );
383
397
411
426
437int mbedtls_mpi_read_string( mbedtls_mpi *X, int radix, const char *s );
438
462 char *buf, size_t buflen, size_t *olen );
463
464#if defined(MBEDTLS_FS_IO)
486int mbedtls_mpi_read_file( mbedtls_mpi *X, int radix, FILE *fin );
487
503int mbedtls_mpi_write_file( const char *p, const mbedtls_mpi *X,
504 int radix, FILE *fout );
505#endif /* MBEDTLS_FS_IO */
506
519int mbedtls_mpi_read_binary( mbedtls_mpi *X, const unsigned char *buf,
520 size_t buflen );
521
536int mbedtls_mpi_write_binary( const mbedtls_mpi *X, unsigned char *buf,
537 size_t buflen );
538
550
562
574
586
603 unsigned *ret );
604
616
629 const mbedtls_mpi *B );
630
644 const mbedtls_mpi *B );
645
658 const mbedtls_mpi *B );
659
672 const mbedtls_mpi *B );
673
687
702
716 const mbedtls_mpi *B );
717
733
753 const mbedtls_mpi *B );
754
775
794 const mbedtls_mpi *B );
795
814
842 const mbedtls_mpi *E, const mbedtls_mpi *N,
843 mbedtls_mpi *_RR );
844
863 int (*f_rng)(void *, unsigned char *, size_t),
864 void *p_rng );
865
878 const mbedtls_mpi *B );
879
897 const mbedtls_mpi *N );
898
899#if !defined(MBEDTLS_DEPRECATED_REMOVED)
900#if defined(MBEDTLS_DEPRECATED_WARNING)
901#define MBEDTLS_DEPRECATED __attribute__((deprecated))
902#else
903#define MBEDTLS_DEPRECATED
904#endif
925 int (*f_rng)(void *, unsigned char *, size_t),
926 void *p_rng );
927#undef MBEDTLS_DEPRECATED
928#endif /* !MBEDTLS_DEPRECATED_REMOVED */
929
957int mbedtls_mpi_is_prime_ext( const mbedtls_mpi *X, int rounds,
958 int (*f_rng)(void *, unsigned char *, size_t),
959 void *p_rng );
966typedef enum {
970
990int mbedtls_mpi_gen_prime( mbedtls_mpi *X, size_t nbits, int flags,
991 int (*f_rng)(void *, unsigned char *, size_t),
992 void *p_rng );
993
994#if defined(MBEDTLS_SELF_TEST)
995
1001int mbedtls_mpi_self_test( int verbose );
1002
1003#endif /* MBEDTLS_SELF_TEST */
1004
1005#ifdef __cplusplus
1006}
1007#endif
1008
1009#endif /* bignum.h */
#define N
Definition: crc32.c:57
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_grow(mbedtls_mpi *X, size_t nblimbs)
Enlarge an MPI to the specified number of limbs.
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_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:966
@ MBEDTLS_MPI_GEN_PRIME_FLAG_DH
Definition: bignum.h:967
@ MBEDTLS_MPI_GEN_PRIME_FLAG_LOW_ERR
Definition: bignum.h:968
size_t mbedtls_mpi_size(const mbedtls_mpi *X)
Return the total size of an MPI value in bytes.
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.
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_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.
int32_t mbedtls_mpi_sint
Definition: bignum.h:195
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.
int mbedtls_mpi_safe_cond_swap(mbedtls_mpi *X, mbedtls_mpi *Y, unsigned char assign)
Perform a safe conditional swap which doesn't reveal whether the condition was true or not.
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:903
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_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_write_binary(const mbedtls_mpi *X, unsigned char *buf, size_t buflen)
Export an MPI into unsigned big endian binary data of fixed size.
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|.
uint32_t mbedtls_mpi_uint
Definition: bignum.h:196
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:198
#define G(r, i, a, b, c, d)
Definition: blake2b-ref.c:117
Definition: ehthrow.cxx:93
Definition: ehthrow.cxx:54
INT32 int32_t
Definition: types.h:71
UINT32 uint32_t
Definition: types.h:75
UINT64 uint64_t
Definition: types.h:77
INT64 int64_t
Definition: types.h:72
#define __attribute__(x)
Definition: wpp_private.h:207
#define Y(I)
static const WCHAR E[]
Definition: oid.c:1253
GLdouble s
Definition: gl.h:2039
GLuint GLuint GLsizei count
Definition: gl.h:1545
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
GLsizeiptr size
Definition: glext.h:5919
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLenum mode
Definition: glext.h:6217
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLbitfield flags
Definition: glext.h:7161
GLuint GLfloat * val
Definition: glext.h:7180
GLfloat GLfloat p
Definition: glext.h:8902
GLdouble GLdouble z
Definition: glext.h:5874
#define verbose
Definition: rosglue.h:36
#define R(b, x)
Definition: sha2.c:134
MPI structure.
Definition: bignum.h:211
size_t n
Definition: bignum.h:213
mbedtls_mpi_uint * p
Definition: bignum.h:214
int ret