source: UsbWattMeter/trunk/wolfssl-3.7.0/wolfssl/wolfcrypt/integer.h@ 164

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

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-chdr
File size: 11.1 KB
Line 
1/* integer.h
2 *
3 * Copyright (C) 2006-2015 wolfSSL Inc.
4 *
5 * This file is part of wolfSSL. (formerly known as CyaSSL)
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-1301, USA
20 */
21
22/*
23 * Based on public domain LibTomMath 0.38 by Tom St Denis, tomstdenis@iahu.ca,
24 * http://math.libtomcrypt.com
25 */
26
27
28#ifndef WOLF_CRYPT_INTEGER_H
29#define WOLF_CRYPT_INTEGER_H
30
31/* may optionally use fast math instead, not yet supported on all platforms and
32 may not be faster on all
33*/
34#include <wolfssl/wolfcrypt/types.h> /* will set MP_xxBIT if not default */
35#ifdef USE_FAST_MATH
36 #include <wolfssl/wolfcrypt/tfm.h>
37#else
38
39#include <wolfssl/wolfcrypt/random.h>
40
41#ifndef CHAR_BIT
42 #include <limits.h>
43#endif
44
45#include <wolfssl/wolfcrypt/mpi_class.h>
46
47#ifndef MIN
48 #define MIN(x,y) ((x)<(y)?(x):(y))
49#endif
50
51#ifndef MAX
52 #define MAX(x,y) ((x)>(y)?(x):(y))
53#endif
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#else
62
63/* C on the other hand doesn't care */
64#define OPT_CAST(x)
65
66#endif
67
68
69/* detect 64-bit mode if possible */
70#if defined(__x86_64__)
71 #if !(defined(MP_64BIT) && defined(MP_16BIT) && defined(MP_8BIT))
72 #define MP_64BIT
73 #endif
74#endif
75/* if intel compiler doesn't provide 128 bit type don't turn on 64bit */
76#if defined(MP_64BIT) && defined(__INTEL_COMPILER) && !defined(HAVE___UINT128_T)
77 #undef MP_64BIT
78#endif
79
80
81/* allow user to define on mp_digit, mp_word, DIGIT_BIT types */
82#ifndef WOLFSSL_BIGINT_TYPES
83
84/* some default configurations.
85 *
86 * A "mp_digit" must be able to hold DIGIT_BIT + 1 bits
87 * A "mp_word" must be able to hold 2*DIGIT_BIT + 1 bits
88 *
89 * At the very least a mp_digit must be able to hold 7 bits
90 * [any size beyond that is ok provided it doesn't overflow the data type]
91 */
92#ifdef MP_8BIT
93 typedef unsigned char mp_digit;
94 typedef unsigned short mp_word;
95#elif defined(MP_16BIT) || defined(NO_64BIT)
96 typedef unsigned short mp_digit;
97 typedef unsigned int mp_word;
98#elif defined(MP_64BIT)
99 /* for GCC only on supported platforms */
100 typedef unsigned long long mp_digit; /* 64 bit type, 128 uses mode(TI) */
101 typedef unsigned long mp_word __attribute__ ((mode(TI)));
102
103 #define DIGIT_BIT 60
104#else
105 /* this is the default case, 28-bit digits */
106
107 #if defined(_MSC_VER) || defined(__BORLANDC__)
108 typedef unsigned __int64 ulong64;
109 #else
110 typedef unsigned long long ulong64;
111 #endif
112
113 typedef unsigned int mp_digit; /* long could be 64 now, changed TAO */
114 typedef ulong64 mp_word;
115
116#ifdef MP_31BIT
117 /* this is an extension that uses 31-bit digits */
118 #define DIGIT_BIT 31
119#else
120 /* default case is 28-bit digits, defines MP_28BIT as a handy test macro */
121 #define DIGIT_BIT 28
122 #define MP_28BIT
123#endif
124#endif
125
126#endif /* WOLFSSL_BIGINT_TYPES */
127
128/* otherwise the bits per digit is calculated automatically from the size of
129 a mp_digit */
130#ifndef DIGIT_BIT
131 #define DIGIT_BIT ((int)((CHAR_BIT * sizeof(mp_digit) - 1)))
132 /* bits per digit */
133#endif
134
135#define MP_DIGIT_BIT DIGIT_BIT
136#define MP_MASK ((((mp_digit)1)<<((mp_digit)DIGIT_BIT))-((mp_digit)1))
137#define MP_DIGIT_MAX MP_MASK
138
139/* equalities */
140#define MP_LT -1 /* less than */
141#define MP_EQ 0 /* equal to */
142#define MP_GT 1 /* greater than */
143
144#define MP_ZPOS 0 /* positive integer */
145#define MP_NEG 1 /* negative */
146
147#define MP_OKAY 0 /* ok result */
148#define MP_MEM -2 /* out of mem */
149#define MP_VAL -3 /* invalid input */
150#define MP_NOT_INF -4 /* point not at infinity */
151#define MP_RANGE MP_NOT_INF
152
153#define MP_YES 1 /* yes response */
154#define MP_NO 0 /* no response */
155
156/* Primality generation flags */
157#define LTM_PRIME_BBS 0x0001 /* BBS style prime */
158#define LTM_PRIME_SAFE 0x0002 /* Safe prime (p-1)/2 == prime */
159#define LTM_PRIME_2MSB_ON 0x0008 /* force 2nd MSB to 1 */
160
161typedef int mp_err;
162
163/* define this to use lower memory usage routines (exptmods mostly) */
164#define MP_LOW_MEM
165
166/* default precision */
167#ifndef MP_PREC
168 #ifndef MP_LOW_MEM
169 #define MP_PREC 32 /* default digits of precision */
170 #else
171 #define MP_PREC 1 /* default digits of precision */
172 #endif
173#endif
174
175/* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD -
176 BITS_PER_DIGIT*2) */
177#define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1))
178
179/* the infamous mp_int structure */
180typedef struct {
181 int used, alloc, sign;
182 mp_digit *dp;
183} mp_int;
184
185/* callback for mp_prime_random, should fill dst with random bytes and return
186 how many read [upto len] */
187typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
188
189
190#define USED(m) ((m)->used)
191#define DIGIT(m,k) ((m)->dp[(k)])
192#define SIGN(m) ((m)->sign)
193
194
195/* ---> Basic Manipulations <--- */
196#define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
197#define mp_iseven(a) \
198 (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO)
199#define mp_isodd(a) \
200 (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO)
201
202
203/* number of primes */
204#ifdef MP_8BIT
205 #define PRIME_SIZE 31
206#else
207 #define PRIME_SIZE 256
208#endif
209
210#define mp_prime_random(a, t, size, bbs, cb, dat) \
211 mp_prime_random_ex(a, t, ((size) * 8) + 1, (bbs==1)?LTM_PRIME_BBS:0, cb, dat)
212
213#define mp_read_raw(mp, str, len) mp_read_signed_bin((mp), (str), (len))
214#define mp_raw_size(mp) mp_signed_bin_size(mp)
215#define mp_toraw(mp, str) mp_to_signed_bin((mp), (str))
216#define mp_read_mag(mp, str, len) mp_read_unsigned_bin((mp), (str), (len))
217#define mp_mag_size(mp) mp_unsigned_bin_size(mp)
218#define mp_tomag(mp, str) mp_to_unsigned_bin((mp), (str))
219
220#define mp_tobinary(M, S) mp_toradix((M), (S), 2)
221#define mp_tooctal(M, S) mp_toradix((M), (S), 8)
222#define mp_todecimal(M, S) mp_toradix((M), (S), 10)
223#define mp_tohex(M, S) mp_toradix((M), (S), 16)
224
225#define s_mp_mul(a, b, c) s_mp_mul_digs(a, b, c, (a)->used + (b)->used + 1)
226
227extern const char *mp_s_rmap;
228
229/* 6 functions needed by Rsa */
230int mp_init (mp_int * a);
231void mp_clear (mp_int * a);
232int mp_unsigned_bin_size(mp_int * a);
233int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c);
234int mp_to_unsigned_bin (mp_int * a, unsigned char *b);
235int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y);
236/* end functions needed by Rsa */
237
238/* functions added to support above needed, removed TOOM and KARATSUBA */
239int mp_count_bits (mp_int * a);
240int mp_leading_bit (mp_int * a);
241int mp_init_copy (mp_int * a, mp_int * b);
242int mp_copy (mp_int * a, mp_int * b);
243int mp_grow (mp_int * a, int size);
244int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d);
245void mp_zero (mp_int * a);
246void mp_clamp (mp_int * a);
247void mp_exch (mp_int * a, mp_int * b);
248void mp_rshd (mp_int * a, int b);
249void mp_rshb (mp_int * a, int b);
250int mp_mod_2d (mp_int * a, int b, mp_int * c);
251int mp_mul_2d (mp_int * a, int b, mp_int * c);
252int mp_lshd (mp_int * a, int b);
253int mp_abs (mp_int * a, mp_int * b);
254int mp_invmod (mp_int * a, mp_int * b, mp_int * c);
255int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c);
256int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c);
257int mp_cmp_mag (mp_int * a, mp_int * b);
258int mp_cmp (mp_int * a, mp_int * b);
259int mp_cmp_d(mp_int * a, mp_digit b);
260void mp_set (mp_int * a, mp_digit b);
261int mp_is_bit_set (mp_int * a, mp_digit b);
262int mp_mod (mp_int * a, mp_int * b, mp_int * c);
263int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d);
264int mp_div_2(mp_int * a, mp_int * b);
265int mp_add (mp_int * a, mp_int * b, mp_int * c);
266int s_mp_add (mp_int * a, mp_int * b, mp_int * c);
267int s_mp_sub (mp_int * a, mp_int * b, mp_int * c);
268int mp_sub (mp_int * a, mp_int * b, mp_int * c);
269int mp_reduce_is_2k_l(mp_int *a);
270int mp_reduce_is_2k(mp_int *a);
271int mp_dr_is_modulus(mp_int *a);
272int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int);
273int mp_montgomery_setup (mp_int * n, mp_digit * rho);
274int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho);
275int mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho);
276void mp_dr_setup(mp_int *a, mp_digit *d);
277int mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k);
278int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d);
279int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
280int s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
281int mp_reduce_2k_setup_l(mp_int *a, mp_int *d);
282int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d);
283int mp_reduce (mp_int * x, mp_int * m, mp_int * mu);
284int mp_reduce_setup (mp_int * a, mp_int * b);
285int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
286int mp_montgomery_calc_normalization (mp_int * a, mp_int * b);
287int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
288int s_mp_sqr (mp_int * a, mp_int * b);
289int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs);
290int fast_s_mp_sqr (mp_int * a, mp_int * b);
291int mp_init_size (mp_int * a, int size);
292int mp_div_3 (mp_int * a, mp_int *c, mp_digit * d);
293int mp_mul_2(mp_int * a, mp_int * b);
294int mp_mul (mp_int * a, mp_int * b, mp_int * c);
295int mp_sqr (mp_int * a, mp_int * b);
296int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d);
297int mp_mul_d (mp_int * a, mp_digit b, mp_int * c);
298int mp_2expt (mp_int * a, int b);
299int mp_set_bit (mp_int * a, int b);
300int mp_reduce_2k_setup(mp_int *a, mp_digit *d);
301int mp_add_d (mp_int* a, mp_digit b, mp_int* c);
302int mp_set_int (mp_int * a, unsigned long b);
303int mp_sub_d (mp_int * a, mp_digit b, mp_int * c);
304/* end support added functions */
305
306/* added */
307int mp_init_multi(mp_int* a, mp_int* b, mp_int* c, mp_int* d, mp_int* e,
308 mp_int* f);
309int mp_toradix (mp_int *a, char *str, int radix);
310int mp_radix_size (mp_int * a, int radix, int *size);
311
312#if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
313 int mp_sqrmod(mp_int* a, mp_int* b, mp_int* c);
314#endif
315#if defined(HAVE_ECC) || defined(WOLFSSL_KEY_GEN)
316 int mp_read_radix(mp_int* a, const char* str, int radix);
317#endif
318
319#ifdef WOLFSSL_KEY_GEN
320 int mp_prime_is_prime (mp_int * a, int t, int *result);
321 int mp_gcd (mp_int * a, mp_int * b, mp_int * c);
322 int mp_lcm (mp_int * a, mp_int * b, mp_int * c);
323 int mp_rand_prime(mp_int* N, int len, WC_RNG* rng, void* heap);
324#endif
325
326int mp_cnt_lsb(mp_int *a);
327int mp_mod_d(mp_int* a, mp_digit b, mp_digit* c);
328
329#ifdef __cplusplus
330 }
331#endif
332
333
334#endif /* USE_FAST_MATH */
335
336#endif /* WOLF_CRYPT_INTEGER_H */
337
Note: See TracBrowser for help on using the repository browser.