mpi-internal.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* mpi-internal.h - Internal to the Multi Precision Integers
  2. * Copyright (C) 1994, 1996 Free Software Foundation, Inc.
  3. * Copyright (C) 1998, 2000 Free Software Foundation, Inc.
  4. *
  5. * This file is part of GnuPG.
  6. *
  7. * GnuPG is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * GnuPG is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. *
  21. * Note: This code is heavily based on the GNU MP Library.
  22. * Actually it's the same code with only minor changes in the
  23. * way the data is stored; this is to support the abstraction
  24. * of an optional secure memory allocation which may be used
  25. * to avoid revealing of sensitive data due to paging etc.
  26. * The GNU MP Library itself is published under the LGPL;
  27. * however I decided to publish this code under the plain GPL.
  28. */
  29. #ifndef G10_MPI_INTERNAL_H
  30. #define G10_MPI_INTERNAL_H
  31. #include <linux/module.h>
  32. #include <linux/kernel.h>
  33. #include <linux/slab.h>
  34. #include <linux/string.h>
  35. #include <linux/mpi.h>
  36. #include <linux/errno.h>
  37. #define log_debug printk
  38. #define log_bug printk
  39. #define assert(x) \
  40. do { \
  41. if (!x) \
  42. log_bug("failed assertion\n"); \
  43. } while (0);
  44. /* If KARATSUBA_THRESHOLD is not already defined, define it to a
  45. * value which is good on most machines. */
  46. /* tested 4, 16, 32 and 64, where 16 gave the best performance when
  47. * checking a 768 and a 1024 bit ElGamal signature.
  48. * (wk 22.12.97) */
  49. #ifndef KARATSUBA_THRESHOLD
  50. #define KARATSUBA_THRESHOLD 16
  51. #endif
  52. /* The code can't handle KARATSUBA_THRESHOLD smaller than 2. */
  53. #if KARATSUBA_THRESHOLD < 2
  54. #undef KARATSUBA_THRESHOLD
  55. #define KARATSUBA_THRESHOLD 2
  56. #endif
  57. typedef mpi_limb_t *mpi_ptr_t; /* pointer to a limb */
  58. typedef int mpi_size_t; /* (must be a signed type) */
  59. #define ABS(x) (x >= 0 ? x : -x)
  60. #define MIN(l, o) ((l) < (o) ? (l) : (o))
  61. #define MAX(h, i) ((h) > (i) ? (h) : (i))
  62. static inline int RESIZE_IF_NEEDED(MPI a, unsigned b)
  63. {
  64. if (a->alloced < b)
  65. return mpi_resize(a, b);
  66. return 0;
  67. }
  68. /* Copy N limbs from S to D. */
  69. #define MPN_COPY(d, s, n) \
  70. do { \
  71. mpi_size_t _i; \
  72. for (_i = 0; _i < (n); _i++) \
  73. (d)[_i] = (s)[_i]; \
  74. } while (0)
  75. #define MPN_COPY_INCR(d, s, n) \
  76. do { \
  77. mpi_size_t _i; \
  78. for (_i = 0; _i < (n); _i++) \
  79. (d)[_i] = (d)[_i]; \
  80. } while (0)
  81. #define MPN_COPY_DECR(d, s, n) \
  82. do { \
  83. mpi_size_t _i; \
  84. for (_i = (n)-1; _i >= 0; _i--) \
  85. (d)[_i] = (s)[_i]; \
  86. } while (0)
  87. /* Zero N limbs at D */
  88. #define MPN_ZERO(d, n) \
  89. do { \
  90. int _i; \
  91. for (_i = 0; _i < (n); _i++) \
  92. (d)[_i] = 0; \
  93. } while (0)
  94. #define MPN_NORMALIZE(d, n) \
  95. do { \
  96. while ((n) > 0) { \
  97. if ((d)[(n)-1]) \
  98. break; \
  99. (n)--; \
  100. } \
  101. } while (0)
  102. #define MPN_NORMALIZE_NOT_ZERO(d, n) \
  103. do { \
  104. for (;;) { \
  105. if ((d)[(n)-1]) \
  106. break; \
  107. (n)--; \
  108. } \
  109. } while (0)
  110. #define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
  111. do { \
  112. if ((size) < KARATSUBA_THRESHOLD) \
  113. mul_n_basecase(prodp, up, vp, size); \
  114. else \
  115. mul_n(prodp, up, vp, size, tspace); \
  116. } while (0);
  117. /* Divide the two-limb number in (NH,,NL) by D, with DI being the largest
  118. * limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB).
  119. * If this would yield overflow, DI should be the largest possible number
  120. * (i.e., only ones). For correct operation, the most significant bit of D
  121. * has to be set. Put the quotient in Q and the remainder in R.
  122. */
  123. #define UDIV_QRNND_PREINV(q, r, nh, nl, d, di) \
  124. do { \
  125. mpi_limb_t _q, _ql, _r; \
  126. mpi_limb_t _xh, _xl; \
  127. umul_ppmm(_q, _ql, (nh), (di)); \
  128. _q += (nh); /* DI is 2**BITS_PER_MPI_LIMB too small */ \
  129. umul_ppmm(_xh, _xl, _q, (d)); \
  130. sub_ddmmss(_xh, _r, (nh), (nl), _xh, _xl); \
  131. if (_xh) { \
  132. sub_ddmmss(_xh, _r, _xh, _r, 0, (d)); \
  133. _q++; \
  134. if (_xh) { \
  135. sub_ddmmss(_xh, _r, _xh, _r, 0, (d)); \
  136. _q++; \
  137. } \
  138. } \
  139. if (_r >= (d)) { \
  140. _r -= (d); \
  141. _q++; \
  142. } \
  143. (r) = _r; \
  144. (q) = _q; \
  145. } while (0)
  146. /*-- mpiutil.c --*/
  147. mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs);
  148. void mpi_free_limb_space(mpi_ptr_t a);
  149. void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs);
  150. /*-- mpi-bit.c --*/
  151. void mpi_rshift_limbs(MPI a, unsigned int count);
  152. int mpi_lshift_limbs(MPI a, unsigned int count);
  153. /*-- mpihelp-add.c --*/
  154. mpi_limb_t mpihelp_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  155. mpi_size_t s1_size, mpi_limb_t s2_limb);
  156. mpi_limb_t mpihelp_add_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  157. mpi_ptr_t s2_ptr, mpi_size_t size);
  158. mpi_limb_t mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
  159. mpi_ptr_t s2_ptr, mpi_size_t s2_size);
  160. /*-- mpihelp-sub.c --*/
  161. mpi_limb_t mpihelp_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  162. mpi_size_t s1_size, mpi_limb_t s2_limb);
  163. mpi_limb_t mpihelp_sub_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  164. mpi_ptr_t s2_ptr, mpi_size_t size);
  165. mpi_limb_t mpihelp_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
  166. mpi_ptr_t s2_ptr, mpi_size_t s2_size);
  167. /*-- mpihelp-cmp.c --*/
  168. int mpihelp_cmp(mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size);
  169. /*-- mpihelp-mul.c --*/
  170. struct karatsuba_ctx {
  171. struct karatsuba_ctx *next;
  172. mpi_ptr_t tspace;
  173. mpi_size_t tspace_size;
  174. mpi_ptr_t tp;
  175. mpi_size_t tp_size;
  176. };
  177. void mpihelp_release_karatsuba_ctx(struct karatsuba_ctx *ctx);
  178. mpi_limb_t mpihelp_addmul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  179. mpi_size_t s1_size, mpi_limb_t s2_limb);
  180. mpi_limb_t mpihelp_submul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  181. mpi_size_t s1_size, mpi_limb_t s2_limb);
  182. int mpihelp_mul_n(mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp, mpi_size_t size);
  183. int mpihelp_mul(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
  184. mpi_ptr_t vp, mpi_size_t vsize, mpi_limb_t *_result);
  185. void mpih_sqr_n_basecase(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size);
  186. void mpih_sqr_n(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size,
  187. mpi_ptr_t tspace);
  188. int mpihelp_mul_karatsuba_case(mpi_ptr_t prodp,
  189. mpi_ptr_t up, mpi_size_t usize,
  190. mpi_ptr_t vp, mpi_size_t vsize,
  191. struct karatsuba_ctx *ctx);
  192. /*-- mpihelp-mul_1.c (or xxx/cpu/ *.S) --*/
  193. mpi_limb_t mpihelp_mul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  194. mpi_size_t s1_size, mpi_limb_t s2_limb);
  195. /*-- mpihelp-div.c --*/
  196. mpi_limb_t mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
  197. mpi_limb_t divisor_limb);
  198. mpi_limb_t mpihelp_divrem(mpi_ptr_t qp, mpi_size_t qextra_limbs,
  199. mpi_ptr_t np, mpi_size_t nsize,
  200. mpi_ptr_t dp, mpi_size_t dsize);
  201. mpi_limb_t mpihelp_divmod_1(mpi_ptr_t quot_ptr,
  202. mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
  203. mpi_limb_t divisor_limb);
  204. /*-- mpihelp-shift.c --*/
  205. mpi_limb_t mpihelp_lshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
  206. unsigned cnt);
  207. mpi_limb_t mpihelp_rshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
  208. unsigned cnt);
  209. /* Define stuff for longlong.h. */
  210. #define W_TYPE_SIZE BITS_PER_MPI_LIMB
  211. typedef mpi_limb_t UWtype;
  212. typedef unsigned int UHWtype;
  213. #if defined(__GNUC__)
  214. typedef unsigned int UQItype __attribute__ ((mode(QI)));
  215. typedef int SItype __attribute__ ((mode(SI)));
  216. typedef unsigned int USItype __attribute__ ((mode(SI)));
  217. typedef int DItype __attribute__ ((mode(DI)));
  218. typedef unsigned int UDItype __attribute__ ((mode(DI)));
  219. #else
  220. typedef unsigned char UQItype;
  221. typedef long SItype;
  222. typedef unsigned long USItype;
  223. #endif
  224. #ifdef __GNUC__
  225. #include "mpi-inline.h"
  226. #endif
  227. #endif /*G10_MPI_INTERNAL_H */