tls_pstm.h 9.6 KB

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