blob: de03e3f92b0e56c14b63dc3aa1a993d27fdd596c [file] [log] [blame]
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01001/*
2 * Copyright (C) 2017 Denys Vlasenko
3 *
4 * Licensed under GPLv2, see file LICENSE in this source tree.
5 */
6/* The file is taken almost verbatim from matrixssl-3-7-2b-open/crypto/math/.
7 * Changes are flagged with ///bbox
8 */
9
Denys Vlasenko11d00962017-01-15 00:12:42 +010010/**
11 * @file pstm.h
12 * @version 33ef80f (HEAD, tag: MATRIXSSL-3-7-2-OPEN, tag: MATRIXSSL-3-7-2-COMM, origin/master, origin/HEAD, master)
13 *
14 * multiple-precision integer library.
15 */
16/*
17 * Copyright (c) 2013-2015 INSIDE Secure Corporation
18 * Copyright (c) PeerSec Networks, 2002-2011
19 * All Rights Reserved
20 *
21 * The latest version of this code is available at http://www.matrixssl.org
22 *
23 * This software is open source; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
27 *
28 * This General Public License does NOT permit incorporating this software
29 * into proprietary programs. If you are unable to comply with the GPL, a
30 * commercial license for this software may be purchased from INSIDE at
31 * http://www.insidesecure.com/eng/Company/Locations
32 *
33 * This program is distributed in WITHOUT ANY WARRANTY; without even the
34 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
35 * See the GNU General Public License for more details.
36 *
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software
39 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 * http://www.gnu.org/copyleft/gpl.html
41 */
42/******************************************************************************/
43
44#ifndef _h_PSTMATH
45#define _h_PSTMATH
46#ifndef DISABLE_PSTM
47
48/* Define this here to avoid including circular limits.h on some platforms */
49#ifndef CHAR_BIT
50#define CHAR_BIT 8
51#endif
52
53/******************************************************************************/
54/*
55 If native 64 bit integers are not supported, we do not support 32x32->64
56 in hardware, so we must set the 16 bit flag to produce 16x16->32 products.
57*/
58#ifndef HAVE_NATIVE_INT64
59 #define PSTM_16BIT
60#endif /* ! HAVE_NATIVE_INT64 */
61
62/******************************************************************************/
63/*
64 Some default configurations.
65
66 pstm_word should be the largest value the processor can hold as the product
67 of a multiplication. Most platforms support a 32x32->64 MAC instruction,
68 so 64bits is the default pstm_word size.
69 pstm_digit should be half the size of pstm_word
70 */
71#ifdef PSTM_8BIT
72/* 8-bit digits, 16-bit word products */
73 typedef unsigned char pstm_digit;
74 typedef unsigned short pstm_word;
75 #define DIGIT_BIT 8
76
77#elif defined(PSTM_16BIT)
78/* 16-bit digits, 32-bit word products */
79 typedef unsigned short pstm_digit;
80 typedef unsigned long pstm_word;
81 #define DIGIT_BIT 16
82
83#elif defined(PSTM_64BIT)
84/* 64-bit digits, 128-bit word products */
85 #ifndef __GNUC__
86 #error "64bit digits requires GCC"
87 #endif
88 typedef unsigned long pstm_digit;
89 typedef unsigned long pstm_word __attribute__ ((mode(TI)));
90 #define DIGIT_BIT 64
91
92#else
93/* This is the default case, 32-bit digits, 64-bit word products */
94 typedef uint32 pstm_digit;
95 typedef uint64 pstm_word;
96 #define DIGIT_BIT 32
97 #define PSTM_32BIT
98#endif /* digit and word size */
99
100#define PSTM_MASK (pstm_digit)(-1)
101#define PSTM_DIGIT_MAX PSTM_MASK
102
103/******************************************************************************/
104/*
105 equalities
106 */
107#define PSTM_LT -1 /* less than */
108#define PSTM_EQ 0 /* equal to */
109#define PSTM_GT 1 /* greater than */
110
111#define PSTM_ZPOS 0 /* positive integer */
112#define PSTM_NEG 1 /* negative */
113
114#define PSTM_OKAY PS_SUCCESS
115#define PSTM_MEM PS_MEM_FAIL
116
117/******************************************************************************/
118/*
119 Various build options
120 */
121#define PSTM_DEFAULT_INIT 64 /* default (64) digits of allocation */
122#define PSTM_MAX_SIZE 4096
123
124typedef struct {
125 int16 used, alloc, sign;
126 pstm_digit *dp;
127 psPool_t *pool;
128} pstm_int;
129
130/******************************************************************************/
131/*
132 Operations on large integers
133 */
134#define pstm_iszero(a) (((a)->used == 0) ? PS_TRUE : PS_FALSE)
135#define pstm_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? PS_TRUE : PS_FALSE)
136#define pstm_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? PS_TRUE : PS_FALSE)
137#define pstm_abs(a, b) { pstm_copy(a, b); (b)->sign = 0; }
138
139extern void pstm_set(pstm_int *a, pstm_digit b);
140
141extern void pstm_zero(pstm_int * a);
142
143extern int32 pstm_init(psPool_t *pool, pstm_int * a);
144
145extern int32 pstm_init_size(psPool_t *pool, pstm_int * a, uint32 size);
146
147extern int32 pstm_init_copy(psPool_t *pool, pstm_int * a, pstm_int * b,
148 int16 toSqr);
149
150extern int16 pstm_count_bits (pstm_int * a);
151
152extern int32 pstm_init_for_read_unsigned_bin(psPool_t *pool, pstm_int *a,
153 uint32 len);
154
155extern int32 pstm_read_unsigned_bin(pstm_int *a, unsigned char *b, int32 c);
156
157extern int32 pstm_unsigned_bin_size(pstm_int *a);
158
159extern int32 pstm_copy(pstm_int * a, pstm_int * b);
160
161extern void pstm_exch(pstm_int * a, pstm_int * b);
162
163extern void pstm_clear(pstm_int * a);
164
165extern void pstm_clear_multi(pstm_int *mp0, pstm_int *mp1, pstm_int *mp2,
166 pstm_int *mp3, pstm_int *mp4, pstm_int *mp5, pstm_int *mp6,
167 pstm_int *mp7);
168
169extern int32 pstm_grow(pstm_int * a, int16 size);
170
171extern void pstm_clamp(pstm_int * a);
172
173extern int32 pstm_cmp(pstm_int * a, pstm_int * b);
174
175extern int32 pstm_cmp_mag(pstm_int * a, pstm_int * b);
176
177extern void pstm_rshd(pstm_int *a, int16 x);
178
179extern int32 pstm_lshd(pstm_int * a, int16 b);
180
181extern int32 pstm_div(psPool_t *pool, pstm_int *a, pstm_int *b, pstm_int *c,
182 pstm_int *d);
183
184extern int32 pstm_div_2d(psPool_t *pool, pstm_int *a, int16 b, pstm_int *c,
185 pstm_int *d);
186
187extern int32 pstm_div_2(pstm_int * a, pstm_int * b);
188
189extern int32 s_pstm_sub(pstm_int *a, pstm_int *b, pstm_int *c);
190
191extern int32 pstm_sub(pstm_int *a, pstm_int *b, pstm_int *c);
192
193extern int32 pstm_sub_d(psPool_t *pool, pstm_int *a, pstm_digit b, pstm_int *c);
194
195extern int32 pstm_mul_2(pstm_int * a, pstm_int * b);
196
197extern int32 pstm_mod(psPool_t *pool, pstm_int *a, pstm_int *b, pstm_int *c);
198
199extern int32 pstm_mulmod(psPool_t *pool, pstm_int *a, pstm_int *b, pstm_int *c,
200 pstm_int *d);
201
202extern int32 pstm_exptmod(psPool_t *pool, pstm_int *G, pstm_int *X, pstm_int *P,
203 pstm_int *Y);
204
205extern int32 pstm_2expt(pstm_int *a, int16 b);
206
207extern int32 pstm_add(pstm_int *a, pstm_int *b, pstm_int *c);
208
209extern int32 pstm_to_unsigned_bin(psPool_t *pool, pstm_int *a,
210 unsigned char *b);
211
212extern int32 pstm_to_unsigned_bin_nr(psPool_t *pool, pstm_int *a,
213 unsigned char *b);
214
215extern int32 pstm_montgomery_setup(pstm_int *a, pstm_digit *rho);
216
217///bbox: pool unused
218#define pstm_montgomery_reduce(pool, a, m, mp, paD, paDlen) \
219 pstm_montgomery_reduce( a, m, mp, paD, paDlen)
220extern int32 pstm_montgomery_reduce(psPool_t *pool, pstm_int *a, pstm_int *m,
221 pstm_digit mp, pstm_digit *paD, uint32 paDlen);
222
223#define pstm_mul_comba(pool, A, B, C, paD, paDlen) \
224 pstm_mul_comba( A, B, C, paD, paDlen)
225extern int32 pstm_mul_comba(psPool_t *pool, pstm_int *A, pstm_int *B,
226 pstm_int *C, pstm_digit *paD, uint32 paDlen);
227
228///bbox: pool unused
229#define pstm_sqr_comba(pool, A, B, paD, paDlen) \
230 pstm_sqr_comba( A, B, paD, paDlen)
231extern int32 pstm_sqr_comba(psPool_t *pool, pstm_int *A, pstm_int *B,
232 pstm_digit *paD, uint32 paDlen);
233
234extern int32 pstm_cmp_d(pstm_int *a, pstm_digit b);
235
236extern int32 pstm_montgomery_calc_normalization(pstm_int *a, pstm_int *b);
237
238extern int32 pstm_mul_d(pstm_int *a, pstm_digit b, pstm_int *c);
239
240extern int32 pstm_invmod(psPool_t *pool, pstm_int * a, pstm_int * b,
241 pstm_int * c);
242
243#else /* DISABLE_PSTM */
244 typedef int32 pstm_int;
245#endif /* !DISABLE_PSTM */
246#endif /* _h_PSTMATH */
247