blob: bc7a0119a11d34e4f6bf78e3184708010659c1b5 [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/.
Denys Vlasenko6b1b0042017-01-19 15:51:00 +01007 * Changes are flagged with //bbox
Denys Vlasenko3f8ecd92017-01-15 14:16:51 +01008 */
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 {
Denys Vlasenko229d3c42017-04-03 21:53:29 +0200125 int used, alloc, sign; //bbox: was int16
Denys Vlasenko11d00962017-01-15 00:12:42 +0100126 pstm_digit *dp;
Denys Vlasenko6b1b0042017-01-19 15:51:00 +0100127//bbox psPool_t *pool;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100128} 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
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100139//made static:extern void pstm_set(pstm_int *a, pstm_digit b);
Denys Vlasenko11d00962017-01-15 00:12:42 +0100140
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100141//made static:extern void pstm_zero(pstm_int * a);
Denys Vlasenko11d00962017-01-15 00:12:42 +0100142
Denys Vlasenko6b1b0042017-01-19 15:51:00 +0100143//bbox: pool unused
144#define pstm_init(pool, a) \
145 pstm_init( a)
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100146//made static:extern int32 pstm_init(psPool_t *pool, pstm_int * a);
Denys Vlasenko11d00962017-01-15 00:12:42 +0100147
Denys Vlasenko6b1b0042017-01-19 15:51:00 +0100148//bbox: pool unused
149#define pstm_init_size(pool, a, size) \
150 pstm_init_size( a, size)
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100151extern int32 pstm_init_size(psPool_t *pool, pstm_int * a, uint32 size) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100152
Denys Vlasenko6b1b0042017-01-19 15:51:00 +0100153//bbox: pool unused
154#define pstm_init_copy(pool, a, b, toSqr) \
155 pstm_init_copy( a, b, toSqr)
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100156//made static:extern int32 pstm_init_copy(psPool_t *pool, pstm_int * a, pstm_int * b,
157//made static: int toSqr); //bbox: was int16 toSqr
Denys Vlasenko11d00962017-01-15 00:12:42 +0100158
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100159//made static:extern int pstm_count_bits (pstm_int * a) FAST_FUNC; //bbox: was returning int16
Denys Vlasenko11d00962017-01-15 00:12:42 +0100160
Denys Vlasenko6b1b0042017-01-19 15:51:00 +0100161//bbox: pool unused
162#define pstm_init_for_read_unsigned_bin(pool, a, len) \
163 pstm_init_for_read_unsigned_bin( a, len)
Denys Vlasenko11d00962017-01-15 00:12:42 +0100164extern int32 pstm_init_for_read_unsigned_bin(psPool_t *pool, pstm_int *a,
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100165 uint32 len) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100166
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100167extern int32 pstm_read_unsigned_bin(pstm_int *a, unsigned char *b, int32 c) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100168
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100169extern int32 pstm_unsigned_bin_size(pstm_int *a) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100170
171extern int32 pstm_copy(pstm_int * a, pstm_int * b);
172
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100173//made static:extern void pstm_exch(pstm_int * a, pstm_int * b);
Denys Vlasenko11d00962017-01-15 00:12:42 +0100174
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100175extern void pstm_clear(pstm_int * a) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100176
177extern void pstm_clear_multi(pstm_int *mp0, pstm_int *mp1, pstm_int *mp2,
178 pstm_int *mp3, pstm_int *mp4, pstm_int *mp5, pstm_int *mp6,
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100179 pstm_int *mp7) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100180
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100181extern int32 pstm_grow(pstm_int * a, int size) FAST_FUNC; //bbox: was int16 size
Denys Vlasenko11d00962017-01-15 00:12:42 +0100182
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100183extern void pstm_clamp(pstm_int * a) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100184
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100185extern int32 pstm_cmp(pstm_int * a, pstm_int * b) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100186
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100187extern int32 pstm_cmp_mag(pstm_int * a, pstm_int * b) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100188
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100189//made static:extern void pstm_rshd(pstm_int *a, int x); //bbox: was int16 x
Denys Vlasenko11d00962017-01-15 00:12:42 +0100190
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100191//made static:extern int32 pstm_lshd(pstm_int * a, int b); //bbox: was int16 b
Denys Vlasenko11d00962017-01-15 00:12:42 +0100192
Denys Vlasenko6b1b0042017-01-19 15:51:00 +0100193//bbox: pool unused
194#define pstm_div(pool, a, b, c, d) \
195 pstm_div( a, b, c, d)
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100196//made static:extern int32 pstm_div(psPool_t *pool, pstm_int *a, pstm_int *b, pstm_int *c,
197//made static: pstm_int *d);
Denys Vlasenko11d00962017-01-15 00:12:42 +0100198
Denys Vlasenko6b1b0042017-01-19 15:51:00 +0100199//bbox: pool unused
200#define pstm_div_2d(pool, a, b, c, d) \
201 pstm_div_2d( a, b, c, d)
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100202//made static:extern int32 pstm_div_2d(psPool_t *pool, pstm_int *a, int b, pstm_int *c,
203//made static: pstm_int *d); //bbox: was int16 b
Denys Vlasenko11d00962017-01-15 00:12:42 +0100204
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100205extern int32 pstm_div_2(pstm_int * a, pstm_int * b) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100206
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100207extern int32 s_pstm_sub(pstm_int *a, pstm_int *b, pstm_int *c) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100208
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100209extern int32 pstm_sub(pstm_int *a, pstm_int *b, pstm_int *c) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100210
Denys Vlasenko6b1b0042017-01-19 15:51:00 +0100211//bbox: pool unused
212#define pstm_sub_d(pool, a, b, c) \
213 pstm_sub_d( a, b, c)
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100214extern int32 pstm_sub_d(psPool_t *pool, pstm_int *a, pstm_digit b, pstm_int *c) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100215
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100216extern int32 pstm_mul_2(pstm_int * a, pstm_int * b) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100217
Denys Vlasenko6b1b0042017-01-19 15:51:00 +0100218//bbox: pool unused
219#define pstm_mod(pool, a, b, c) \
220 pstm_mod( a, b, c)
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100221//made static:extern int32 pstm_mod(psPool_t *pool, pstm_int *a, pstm_int *b, pstm_int *c);
Denys Vlasenko11d00962017-01-15 00:12:42 +0100222
Denys Vlasenko6b1b0042017-01-19 15:51:00 +0100223//bbox: pool unused
224#define pstm_mulmod(pool, a, b, c, d) \
225 pstm_mulmod( a, b, c, d)
Denys Vlasenko11d00962017-01-15 00:12:42 +0100226extern int32 pstm_mulmod(psPool_t *pool, pstm_int *a, pstm_int *b, pstm_int *c,
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100227 pstm_int *d) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100228
Denys Vlasenko6b1b0042017-01-19 15:51:00 +0100229//bbox: pool unused
230#define pstm_exptmod(pool, G, X, P, Y) \
231 pstm_exptmod( G, X, P, Y)
Denys Vlasenko11d00962017-01-15 00:12:42 +0100232extern int32 pstm_exptmod(psPool_t *pool, pstm_int *G, pstm_int *X, pstm_int *P,
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100233 pstm_int *Y) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100234
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100235//made static:extern int32 pstm_2expt(pstm_int *a, int b); //bbox: was int16 b
Denys Vlasenko11d00962017-01-15 00:12:42 +0100236
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100237extern int32 pstm_add(pstm_int *a, pstm_int *b, pstm_int *c) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100238
Denys Vlasenko6b1b0042017-01-19 15:51:00 +0100239//bbox: pool unused
240#define pstm_to_unsigned_bin(pool, a, b) \
241 pstm_to_unsigned_bin( a, b)
Denys Vlasenko11d00962017-01-15 00:12:42 +0100242extern int32 pstm_to_unsigned_bin(psPool_t *pool, pstm_int *a,
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100243 unsigned char *b) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100244
Denys Vlasenko6b1b0042017-01-19 15:51:00 +0100245//bbox: pool unused
246#define pstm_to_unsigned_bin_nr(pool, a, b) \
247 pstm_to_unsigned_bin_nr( a, b)
Denys Vlasenko11d00962017-01-15 00:12:42 +0100248extern int32 pstm_to_unsigned_bin_nr(psPool_t *pool, pstm_int *a,
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100249 unsigned char *b) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100250
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100251//made static:extern int32 pstm_montgomery_setup(pstm_int *a, pstm_digit *rho);
Denys Vlasenko11d00962017-01-15 00:12:42 +0100252
Denys Vlasenko6b1b0042017-01-19 15:51:00 +0100253//bbox: pool unused
Denys Vlasenko11d00962017-01-15 00:12:42 +0100254#define pstm_montgomery_reduce(pool, a, m, mp, paD, paDlen) \
255 pstm_montgomery_reduce( a, m, mp, paD, paDlen)
256extern int32 pstm_montgomery_reduce(psPool_t *pool, pstm_int *a, pstm_int *m,
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100257 pstm_digit mp, pstm_digit *paD, uint32 paDlen) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100258
259#define pstm_mul_comba(pool, A, B, C, paD, paDlen) \
260 pstm_mul_comba( A, B, C, paD, paDlen)
261extern int32 pstm_mul_comba(psPool_t *pool, pstm_int *A, pstm_int *B,
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100262 pstm_int *C, pstm_digit *paD, uint32 paDlen) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100263
Denys Vlasenko6b1b0042017-01-19 15:51:00 +0100264//bbox: pool unused
Denys Vlasenko11d00962017-01-15 00:12:42 +0100265#define pstm_sqr_comba(pool, A, B, paD, paDlen) \
266 pstm_sqr_comba( A, B, paD, paDlen)
267extern int32 pstm_sqr_comba(psPool_t *pool, pstm_int *A, pstm_int *B,
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100268 pstm_digit *paD, uint32 paDlen) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100269
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100270//made static:extern int32 pstm_cmp_d(pstm_int *a, pstm_digit b);
Denys Vlasenko11d00962017-01-15 00:12:42 +0100271
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100272//made static:extern int32 pstm_montgomery_calc_normalization(pstm_int *a, pstm_int *b);
Denys Vlasenko11d00962017-01-15 00:12:42 +0100273
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100274//made static:extern int32 pstm_mul_d(pstm_int *a, pstm_digit b, pstm_int *c);
Denys Vlasenko11d00962017-01-15 00:12:42 +0100275
Denys Vlasenko6b1b0042017-01-19 15:51:00 +0100276//bbox: pool unused
277#define pstm_invmod(pool, a, b, c) \
278 pstm_invmod( a, b, c)
Denys Vlasenko11d00962017-01-15 00:12:42 +0100279extern int32 pstm_invmod(psPool_t *pool, pstm_int * a, pstm_int * b,
Denys Vlasenko37bdd8f2019-01-01 15:40:43 +0100280 pstm_int * c) FAST_FUNC;
Denys Vlasenko11d00962017-01-15 00:12:42 +0100281
282#else /* DISABLE_PSTM */
283 typedef int32 pstm_int;
284#endif /* !DISABLE_PSTM */
285#endif /* _h_PSTMATH */
286