1 /* 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 10 /** 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 124 typedef struct { 125 int used, alloc, sign; //bbox: was int16 126 pstm_digit *dp; 127 //bbox 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 139 //made static:extern void pstm_set(pstm_int *a, pstm_digit b); 140 141 //made static:extern void pstm_zero(pstm_int * a); 142 143 //bbox: pool unused 144 #define pstm_init(pool, a) \ 145 pstm_init( a) 146 //made static:extern int32 pstm_init(psPool_t *pool, pstm_int * a); 147 148 //bbox: pool unused 149 #define pstm_init_size(pool, a, size) \ 150 pstm_init_size( a, size) 151 extern int32 pstm_init_size(psPool_t *pool, pstm_int * a, uint32 size) FAST_FUNC; 152 153 //bbox: pool unused 154 #define pstm_init_copy(pool, a, b, toSqr) \ 155 pstm_init_copy( a, b, toSqr) 156 //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 158 159 //made static:extern int pstm_count_bits (pstm_int * a) FAST_FUNC; //bbox: was returning int16 160 161 //bbox: pool unused 162 #define pstm_init_for_read_unsigned_bin(pool, a, len) \ 163 pstm_init_for_read_unsigned_bin( a, len) 164 extern int32 pstm_init_for_read_unsigned_bin(psPool_t *pool, pstm_int *a, 165 uint32 len) FAST_FUNC; 166 167 extern int32 pstm_read_unsigned_bin(pstm_int *a, unsigned char *b, int32 c) FAST_FUNC; 168 169 extern int32 pstm_unsigned_bin_size(pstm_int *a) FAST_FUNC; 170 171 extern int32 pstm_copy(pstm_int * a, pstm_int * b); 172 173 //made static:extern void pstm_exch(pstm_int * a, pstm_int * b); 174 175 extern void pstm_clear(pstm_int * a) FAST_FUNC; 176 177 extern 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, 179 pstm_int *mp7) FAST_FUNC; 180 181 extern int32 pstm_grow(pstm_int * a, int size) FAST_FUNC; //bbox: was int16 size 182 183 extern void pstm_clamp(pstm_int * a) FAST_FUNC; 184 185 extern int32 pstm_cmp(pstm_int * a, pstm_int * b) FAST_FUNC; 186 187 extern int32 pstm_cmp_mag(pstm_int * a, pstm_int * b) FAST_FUNC; 188 189 //made static:extern void pstm_rshd(pstm_int *a, int x); //bbox: was int16 x 190 191 //made static:extern int32 pstm_lshd(pstm_int * a, int b); //bbox: was int16 b 192 193 //bbox: pool unused 194 #define pstm_div(pool, a, b, c, d) \ 195 pstm_div( a, b, c, d) 196 //made static:extern int32 pstm_div(psPool_t *pool, pstm_int *a, pstm_int *b, pstm_int *c, 197 //made static: pstm_int *d); 198 199 //bbox: pool unused 200 #define pstm_div_2d(pool, a, b, c, d) \ 201 pstm_div_2d( a, b, c, d) 202 //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 204 205 extern int32 pstm_div_2(pstm_int * a, pstm_int * b) FAST_FUNC; 206 207 extern int32 s_pstm_sub(pstm_int *a, pstm_int *b, pstm_int *c) FAST_FUNC; 208 209 extern int32 pstm_sub(pstm_int *a, pstm_int *b, pstm_int *c) FAST_FUNC; 210 211 //bbox: pool unused 212 #define pstm_sub_d(pool, a, b, c) \ 213 pstm_sub_d( a, b, c) 214 extern int32 pstm_sub_d(psPool_t *pool, pstm_int *a, pstm_digit b, pstm_int *c) FAST_FUNC; 215 216 extern int32 pstm_mul_2(pstm_int * a, pstm_int * b) FAST_FUNC; 217 218 //bbox: pool unused 219 #define pstm_mod(pool, a, b, c) \ 220 pstm_mod( a, b, c) 221 //made static:extern int32 pstm_mod(psPool_t *pool, pstm_int *a, pstm_int *b, pstm_int *c); 222 223 //bbox: pool unused 224 #define pstm_mulmod(pool, a, b, c, d) \ 225 pstm_mulmod( a, b, c, d) 226 extern int32 pstm_mulmod(psPool_t *pool, pstm_int *a, pstm_int *b, pstm_int *c, 227 pstm_int *d) FAST_FUNC; 228 229 //bbox: pool unused 230 #define pstm_exptmod(pool, G, X, P, Y) \ 231 pstm_exptmod( G, X, P, Y) 232 extern int32 pstm_exptmod(psPool_t *pool, pstm_int *G, pstm_int *X, pstm_int *P, 233 pstm_int *Y) FAST_FUNC; 234 235 //made static:extern int32 pstm_2expt(pstm_int *a, int b); //bbox: was int16 b 236 237 extern int32 pstm_add(pstm_int *a, pstm_int *b, pstm_int *c) FAST_FUNC; 238 239 //bbox: pool unused 240 #define pstm_to_unsigned_bin(pool, a, b) \ 241 pstm_to_unsigned_bin( a, b) 242 extern int32 pstm_to_unsigned_bin(psPool_t *pool, pstm_int *a, 243 unsigned char *b) FAST_FUNC; 244 245 //bbox: pool unused 246 #define pstm_to_unsigned_bin_nr(pool, a, b) \ 247 pstm_to_unsigned_bin_nr( a, b) 248 extern int32 pstm_to_unsigned_bin_nr(psPool_t *pool, pstm_int *a, 249 unsigned char *b) FAST_FUNC; 250 251 //made static:extern int32 pstm_montgomery_setup(pstm_int *a, pstm_digit *rho); 252 253 //bbox: pool unused 254 #define pstm_montgomery_reduce(pool, a, m, mp, paD, paDlen) \ 255 pstm_montgomery_reduce( a, m, mp, paD, paDlen) 256 extern int32 pstm_montgomery_reduce(psPool_t *pool, pstm_int *a, pstm_int *m, 257 pstm_digit mp, pstm_digit *paD, uint32 paDlen) FAST_FUNC; 258 259 #define pstm_mul_comba(pool, A, B, C, paD, paDlen) \ 260 pstm_mul_comba( A, B, C, paD, paDlen) 261 extern int32 pstm_mul_comba(psPool_t *pool, pstm_int *A, pstm_int *B, 262 pstm_int *C, pstm_digit *paD, uint32 paDlen) FAST_FUNC; 263 264 //bbox: pool unused 265 #define pstm_sqr_comba(pool, A, B, paD, paDlen) \ 266 pstm_sqr_comba( A, B, paD, paDlen) 267 extern int32 pstm_sqr_comba(psPool_t *pool, pstm_int *A, pstm_int *B, 268 pstm_digit *paD, uint32 paDlen) FAST_FUNC; 269 270 //made static:extern int32 pstm_cmp_d(pstm_int *a, pstm_digit b); 271 272 //made static:extern int32 pstm_montgomery_calc_normalization(pstm_int *a, pstm_int *b); 273 274 //made static:extern int32 pstm_mul_d(pstm_int *a, pstm_digit b, pstm_int *c); 275 276 //bbox: pool unused 277 #define pstm_invmod(pool, a, b, c) \ 278 pstm_invmod( a, b, c) 279 extern int32 pstm_invmod(psPool_t *pool, pstm_int * a, pstm_int * b, 280 pstm_int * c) FAST_FUNC; 281 282 #else /* DISABLE_PSTM */ 283 typedef int32 pstm_int; 284 #endif /* !DISABLE_PSTM */ 285 #endif /* _h_PSTMATH */ 286