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