source: azure_iot_hub_f767zi/trunk/wolfssl-4.7.0/wolfssl/wolfcrypt/integer.h@ 464

Last change on this file since 464 was 464, checked in by coas-nagasima, 3 years ago

WolfSSLとAzure IoT SDKを更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-chdr;charset=UTF-8
File size: 14.7 KB
Line 
1/* integer.h
2 *
3 * Copyright (C) 2006-2020 wolfSSL Inc.
4 *
5 * This file is part of wolfSSL.
6 *
7 * wolfSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * wolfSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20 */
21
22
23/*
24 * Based on public domain LibTomMath 0.38 by Tom St Denis, tomstdenis@iahu.ca,
25 * http://math.libtomcrypt.com
26 */
27
28
29#ifndef WOLF_CRYPT_INTEGER_H
30#define WOLF_CRYPT_INTEGER_H
31
32/* may optionally use fast math instead, not yet supported on all platforms and
33 may not be faster on all
34*/
35#include <wolfssl/wolfcrypt/types.h> /* will set MP_xxBIT if not default */
36#if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
37 #include <wolfssl/wolfcrypt/sp_int.h>
38#elif defined(USE_FAST_MATH)
39 #include <wolfssl/wolfcrypt/tfm.h>
40#else
41
42#include <wolfssl/wolfcrypt/random.h>
43
44#ifndef CHAR_BIT
45 #if defined(WOLFSSL_LINUXKM)
46 #include <linux/limits.h>
47 #else
48 #include <limits.h>
49 #endif
50#endif
51
52#include <wolfssl/wolfcrypt/mpi_class.h>
53
54
55#ifdef __cplusplus
56extern "C" {
57
58/* C++ compilers don't like assigning void * to mp_digit * */
59#define OPT_CAST(x) (x *)
60
61#elif defined(_SH3)
62
63/* SuperH SH3 compiler doesn't like assigning voi* to mp_digit* */
64#define OPT_CAST(x) (x *)
65
66#else
67
68/* C on the other hand doesn't care */
69#define OPT_CAST(x)
70
71#endif /* __cplusplus */
72
73
74/* detect 64-bit mode if possible */
75#if (defined(__x86_64__) || defined(__aarch64__)) && !(defined (_MSC_VER) && defined(__clang__))
76 #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT))
77 #define MP_64BIT
78 #endif
79#endif
80/* if intel compiler doesn't provide 128 bit type don't turn on 64bit */
81#if defined(MP_64BIT) && defined(__INTEL_COMPILER) && !defined(HAVE___UINT128_T)
82 #undef MP_64BIT
83#endif
84
85
86/* allow user to define on mp_digit, mp_word, DIGIT_BIT types */
87#ifndef WOLFSSL_BIGINT_TYPES
88
89/* some default configurations.
90 *
91 * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
92 * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
93 *
94 * At the very least a mp_digit must be able to hold 7 bits
95 * [any size beyond that is ok provided it doesn't overflow the data type]
96 */
97#ifdef MP_8BIT
98 /* 8-bit */
99 typedef unsigned char mp_digit;
100 typedef unsigned short mp_word;
101 /* don't define DIGIT_BIT, so its calculated below */
102#elif defined(MP_16BIT)
103 /* 16-bit */
104 typedef unsigned int mp_digit;
105 typedef unsigned long mp_word;
106 /* don't define DIGIT_BIT, so its calculated below */
107#elif defined(NO_64BIT)
108 /* 32-bit forced to 16-bit */
109 typedef unsigned short mp_digit;
110 typedef unsigned int mp_word;
111 #define DIGIT_BIT 12
112#elif defined(MP_64BIT)
113 /* 64-bit */
114 /* for GCC only on supported platforms */
115 typedef unsigned long long mp_digit; /* 64 bit type, 128 uses mode(TI) */
116 typedef unsigned long mp_word __attribute__ ((mode(TI)));
117 #define DIGIT_BIT 60
118#else
119 /* 32-bit default case */
120
121 #if defined(_MSC_VER) || defined(__BORLANDC__)
122 typedef unsigned __int64 ulong64;
123 #else
124 typedef unsigned long long ulong64;
125 #endif
126
127 typedef unsigned int mp_digit; /* long could be 64 now, changed TAO */
128 typedef ulong64 mp_word;
129
130 #ifdef MP_31BIT
131 /* this is an extension that uses 31-bit digits */
132 #define DIGIT_BIT 31
133 #else
134 /* default case is 28-bit digits, defines MP_28BIT as a handy test macro */
135 #define DIGIT_BIT 28
136 #define MP_28BIT
137 #endif
138#endif
139
140#endif /* WOLFSSL_BIGINT_TYPES */
141
142/* otherwise the bits per digit is calculated automatically from the size of
143 a mp_digit */
144#ifndef DIGIT_BIT
145 #define DIGIT_BIT ((int)((CHAR_BIT * sizeof(mp_digit) - 1)))
146 /* bits per digit */
147#endif
148
149#define MP_DIGIT_BIT DIGIT_BIT
150#define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
151#define MP_DIGIT_MAX MP_MASK
152
153/* equalities */
154#define MP_LT -1 /* less than */
155#define MP_EQ 0 /* equal to */
156#define MP_GT 1 /* greater than */
157
158#define MP_ZPOS 0 /* positive integer */
159#define MP_NEG 1 /* negative */
160
161#define MP_OKAY 0 /* ok result */
162#define MP_MEM -2 /* out of mem */
163#define MP_VAL -3 /* invalid input */
164#define MP_NOT_INF -4 /* point not at infinity */
165#define MP_RANGE MP_NOT_INF
166
167#define MP_YES 1 /* yes response */
168#define MP_NO 0 /* no response */
169
170/* Primality generation flags */
171#define LTM_PRIME_BBS 0x0001 /* BBS style prime */
172#define LTM_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */
173#define LTM_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */
174
175typedef int mp_err;
176
177/* define this to use lower memory usage routines (exptmods mostly) */
178#define MP_LOW_MEM
179
180/* default precision */
181#ifndef MP_PREC
182 #ifndef MP_LOW_MEM
183 #define MP_PREC 32 /* default digits of precision */
184 #else
185 #define MP_PREC 1 /* default digits of precision */
186 #endif
187#endif
188
189/* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD -
190 BITS_PER_DIGIT*2) */
191#define MP_WARRAY ((mp_word)1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
192
193#ifdef HAVE_WOLF_BIGINT
194 /* raw big integer */
195 typedef struct WC_BIGINT {
196 byte* buf;
197 word32 len;
198 void* heap;
199 } WC_BIGINT;
200 #define WOLF_BIGINT_DEFINED
201#endif
202
203/* the mp_int structure */
204typedef struct mp_int {
205 int used, alloc, sign;
206 mp_digit *dp;
207
208#ifdef HAVE_WOLF_BIGINT
209 struct WC_BIGINT raw; /* unsigned binary (big endian) */
210#endif
211} mp_int;
212
213/* wolf big int and common functions */
214#include <wolfssl/wolfcrypt/wolfmath.h>
215
216
217/* callback for mp_prime_random, should fill dst with random bytes and return
218 how many read [up to len] */
219typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
220
221
222#define USED(m) ((m)->used)
223#define DIGIT(m,k) ((m)->dp[(k)])
224#define SIGN(m) ((m)->sign)
225
226
227/* ---> Basic Manipulations <--- */
228#define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
229#define mp_isone(a) \
230 (((((a)->used == 1)) && ((a)->dp[0] == 1u)) ? MP_YES : MP_NO)
231#define mp_iseven(a) \
232 (((a)->used > 0 && (((a)->dp[0] & 1u) == 0u)) ? MP_YES : MP_NO)
233#define mp_isodd(a) \
234 (((a)->used > 0 && (((a)->dp[0] & 1u) == 1u)) ? MP_YES : MP_NO)
235#define mp_isneg(a) (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO)
236#define mp_isword(a, w) \
237 ((((a)->used == 1) && ((a)->dp[0] == w)) || ((w == 0) && ((a)->used == 0)) \
238 ? MP_YES : MP_NO)
239
240/* number of primes */
241#ifdef MP_8BIT
242 #define PRIME_SIZE 31
243#else
244 #define PRIME_SIZE 256
245#endif
246
247#ifndef MAX_INVMOD_SZ
248 #if defined(WOLFSSL_MYSQL_COMPATIBLE)
249 #define MAX_INVMOD_SZ 8192
250 #else
251 #define MAX_INVMOD_SZ 4096
252 #endif
253#endif
254
255#define mp_prime_random(a, t, size, bbs, cb, dat) \
256 mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?LTM_PRIME_BBS:0, cb, dat)
257
258#define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
259#define mp_raw_size(mp) mp_signed_bin_size(mp)
260#define mp_toraw(mp, str) mp_to_signed_bin((mp), (str))
261#define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
262#define mp_mag_size(mp) mp_unsigned_bin_size(mp)
263#define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str))
264
265#define MP_RADIX_BIN 2
266#define MP_RADIX_OCT 8
267#define MP_RADIX_DEC 10
268#define MP_RADIX_HEX 16
269#define MP_RADIX_MAX 64
270
271#define mp_tobinary(M, S) mp_toradix((M), (S), MP_RADIX_BIN)
272#define mp_tooctal(M, S) mp_toradix((M), (S), MP_RADIX_OCT)
273#define mp_todecimal(M, S) mp_toradix((M), (S), MP_RADIX_DEC)
274#define mp_tohex(M, S) mp_toradix((M), (S), MP_RADIX_HEX)
275
276#define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
277
278#if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) || \
279 defined(WOLFSSL_DEBUG_MATH) || defined(DEBUG_WOLFSSL)
280extern const char *mp_s_rmap;
281#endif
282
283/* 6 functions needed by Rsa */
284MP_API int mp_init (mp_int * a);
285MP_API void mp_clear (mp_int * a);
286MP_API void mp_free (mp_int * a);
287MP_API void mp_forcezero(mp_int * a);
288MP_API int mp_unsigned_bin_size(mp_int * a);
289MP_API int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
290MP_API int mp_to_unsigned_bin_at_pos(int x, mp_int *t, unsigned char *b);
291MP_API int mp_to_unsigned_bin (mp_int * a, unsigned char *b);
292MP_API int mp_to_unsigned_bin_len(mp_int * a, unsigned char *b, int c);
293MP_API int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
294MP_API int mp_exptmod_ex (mp_int * G, mp_int * X, int digits, mp_int * P,
295 mp_int * Y);
296/* end functions needed by Rsa */
297
298/* functions added to support above needed, removed TOOM and KARATSUBA */
299MP_API int mp_count_bits (mp_int * a);
300MP_API int mp_leading_bit (mp_int * a);
301MP_API int mp_init_copy (mp_int * a, mp_int * b);
302MP_API int mp_copy (mp_int * a, mp_int * b);
303MP_API int mp_grow (mp_int * a, int size);
304MP_API int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d);
305MP_API void mp_zero (mp_int * a);
306MP_API void mp_clamp (mp_int * a);
307MP_API void mp_exch (mp_int * a, mp_int * b);
308MP_API int mp_cond_swap_ct (mp_int * a, mp_int * b, int c, int m);
309MP_API void mp_rshd (mp_int * a, int b);
310MP_API void mp_rshb (mp_int * a, int b);
311MP_API int mp_mod_2d (mp_int * a, int b, mp_int * c);
312MP_API int mp_mul_2d (mp_int * a, int b, mp_int * c);
313MP_API int mp_lshd (mp_int * a, int b);
314MP_API int mp_abs (mp_int * a, mp_int * b);
315MP_API int mp_invmod (mp_int * a, mp_int * b, mp_int * c);
316int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c);
317MP_API int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c);
318MP_API int mp_cmp_mag (mp_int * a, mp_int * b);
319MP_API int mp_cmp (mp_int * a, mp_int * b);
320MP_API int mp_cmp_d(mp_int * a, mp_digit b);
321MP_API int mp_set (mp_int * a, mp_digit b);
322MP_API int mp_is_bit_set (mp_int * a, mp_digit b);
323MP_API int mp_mod (mp_int * a, mp_int * b, mp_int * c);
324MP_API int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d);
325MP_API int mp_div_2(mp_int * a, mp_int * b);
326MP_API int mp_div_2_mod_ct (mp_int* a, mp_int* b, mp_int* c);
327MP_API int mp_add (mp_int * a, mp_int * b, mp_int * c);
328int s_mp_add (mp_int * a, mp_int * b, mp_int * c);
329int s_mp_sub (mp_int * a, mp_int * b, mp_int * c);
330MP_API int mp_sub (mp_int * a, mp_int * b, mp_int * c);
331MP_API int mp_reduce_is_2k_l(mp_int *a);
332MP_API int mp_reduce_is_2k(mp_int *a);
333MP_API int mp_dr_is_modulus(mp_int *a);
334MP_API int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y,
335 int);
336MP_API int mp_exptmod_base_2 (mp_int * X, mp_int * P, mp_int * Y);
337#define mp_exptmod_nct(G,X,P,Y) mp_exptmod_fast(G,X,P,Y,0)
338MP_API int mp_montgomery_setup (mp_int * n, mp_digit * rho);
339int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho);
340MP_API int mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho);
341#define mp_montgomery_reduce_ex(x, n, rho, ct) mp_montgomery_reduce (x, n, rho)
342MP_API void mp_dr_setup(mp_int *a, mp_digit *d);
343MP_API int mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k);
344MP_API int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
345int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
346int s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
347MP_API int mp_reduce_2k_setup_l(mp_int *a, mp_int *d);
348MP_API int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d);
349MP_API int mp_reduce (mp_int * x, mp_int * m, mp_int * mu);
350MP_API int mp_reduce_setup (mp_int * a, mp_int * b);
351int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
352MP_API int mp_montgomery_calc_normalization (mp_int * a, mp_int * b);
353int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
354int s_mp_sqr (mp_int * a, mp_int * b);
355int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
356int fast_s_mp_sqr (mp_int * a, mp_int * b);
357MP_API int mp_init_size (mp_int * a, int size);
358MP_API int mp_div_3 (mp_int * a, mp_int *c, mp_digit * d);
359MP_API int mp_mul_2(mp_int * a, mp_int * b);
360MP_API int mp_mul (mp_int * a, mp_int * b, mp_int * c);
361MP_API int mp_sqr (mp_int * a, mp_int * b);
362MP_API int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
363MP_API int mp_submod (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
364MP_API int mp_addmod (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
365MP_API int mp_submod_ct (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
366MP_API int mp_addmod_ct (mp_int* a, mp_int* b, mp_int* c, mp_int* d);
367MP_API int mp_mul_d (mp_int * a, mp_digit b, mp_int * c);
368MP_API int mp_2expt (mp_int * a, int b);
369MP_API int mp_set_bit (mp_int * a, int b);
370MP_API int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
371MP_API int mp_add_d (mp_int* a, mp_digit b, mp_int* c);
372MP_API int mp_set_int (mp_int * a, unsigned long b);
373MP_API int mp_sub_d (mp_int * a, mp_digit b, mp_int * c);
374/* end support added functions */
375
376/* added */
377MP_API int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e,
378 mp_int* f);
379MP_API int mp_toradix (mp_int *a, char *str, int radix);
380MP_API int mp_radix_size (mp_int * a, int radix, int *size);
381
382#ifdef WOLFSSL_DEBUG_MATH
383 MP_API void mp_dump(const char* desc, mp_int* a, byte verbose);
384#else
385 #define mp_dump(desc, a, verbose)
386#endif
387
388#if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN) || !defined(NO_RSA) || \
389 !defined(NO_DSA) || !defined(NO_DH)
390 MP_API int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c);
391#endif
392#if !defined(NO_DSA) || defined(HAVE_ECC)
393 MP_API int mp_read_radix(mp_int* a, const char* str, int radix);
394#endif
395
396#if defined(WOLFSSL_KEY_GEN) || !defined(NO_RSA) || !defined(NO_DSA) || !defined(NO_DH)
397 MP_API int mp_prime_is_prime (mp_int * a, int t, int *result);
398 MP_API int mp_prime_is_prime_ex (mp_int * a, int t, int *result, WC_RNG*);
399#endif /* WOLFSSL_KEY_GEN NO_RSA NO_DSA NO_DH */
400#ifdef WOLFSSL_KEY_GEN
401 MP_API int mp_gcd (mp_int * a, mp_int * b, mp_int * c);
402 MP_API int mp_lcm (mp_int * a, mp_int * b, mp_int * c);
403 MP_API int mp_rand_prime(mp_int* N, int len, WC_RNG* rng, void* heap);
404#endif
405
406MP_API int mp_cnt_lsb(mp_int *a);
407MP_API int mp_mod_d(mp_int* a, mp_digit b, mp_digit* c);
408
409
410#ifdef __cplusplus
411 }
412#endif
413
414
415#endif /* USE_FAST_MATH */
416
417#endif /* WOLF_CRYPT_INTEGER_H */
418
Note: See TracBrowser for help on using the repository browser.