mpi-internal.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. static inline int RESIZE_IF_NEEDED(MPI a, unsigned b)
  60. {
  61. if (a->alloced < b)
  62. return mpi_resize(a, b);
  63. return 0;
  64. }
  65. /* Copy N limbs from S to D. */
  66. #define MPN_COPY(d, s, n) \
  67. do { \
  68. mpi_size_t _i; \
  69. for (_i = 0; _i < (n); _i++) \
  70. (d)[_i] = (s)[_i]; \
  71. } while (0)
  72. #define MPN_COPY_INCR(d, s, n) \
  73. do { \
  74. mpi_size_t _i; \
  75. for (_i = 0; _i < (n); _i++) \
  76. (d)[_i] = (s)[_i]; \
  77. } while (0)
  78. #define MPN_COPY_DECR(d, s, n) \
  79. do { \
  80. mpi_size_t _i; \
  81. for (_i = (n)-1; _i >= 0; _i--) \
  82. (d)[_i] = (s)[_i]; \
  83. } while (0)
  84. /* Zero N limbs at D */
  85. #define MPN_ZERO(d, n) \
  86. do { \
  87. int _i; \
  88. for (_i = 0; _i < (n); _i++) \
  89. (d)[_i] = 0; \
  90. } while (0)
  91. #define MPN_NORMALIZE(d, n) \
  92. do { \
  93. while ((n) > 0) { \
  94. if ((d)[(n)-1]) \
  95. break; \
  96. (n)--; \
  97. } \
  98. } while (0)
  99. #define MPN_NORMALIZE_NOT_ZERO(d, n) \
  100. do { \
  101. for (;;) { \
  102. if ((d)[(n)-1]) \
  103. break; \
  104. (n)--; \
  105. } \
  106. } while (0)
  107. #define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
  108. do { \
  109. if ((size) < KARATSUBA_THRESHOLD) \
  110. mul_n_basecase(prodp, up, vp, size); \
  111. else \
  112. mul_n(prodp, up, vp, size, tspace); \
  113. } while (0);
  114. /* Divide the two-limb number in (NH,,NL) by D, with DI being the largest
  115. * limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB).
  116. * If this would yield overflow, DI should be the largest possible number
  117. * (i.e., only ones). For correct operation, the most significant bit of D
  118. * has to be set. Put the quotient in Q and the remainder in R.
  119. */
  120. #define UDIV_QRNND_PREINV(q, r, nh, nl, d, di) \
  121. do { \
  122. mpi_limb_t _q, _ql, _r; \
  123. mpi_limb_t _xh, _xl; \
  124. umul_ppmm(_q, _ql, (nh), (di)); \
  125. _q += (nh); /* DI is 2**BITS_PER_MPI_LIMB too small */ \
  126. umul_ppmm(_xh, _xl, _q, (d)); \
  127. sub_ddmmss(_xh, _r, (nh), (nl), _xh, _xl); \
  128. if (_xh) { \
  129. sub_ddmmss(_xh, _r, _xh, _r, 0, (d)); \
  130. _q++; \
  131. if (_xh) { \
  132. sub_ddmmss(_xh, _r, _xh, _r, 0, (d)); \
  133. _q++; \
  134. } \
  135. } \
  136. if (_r >= (d)) { \
  137. _r -= (d); \
  138. _q++; \
  139. } \
  140. (r) = _r; \
  141. (q) = _q; \
  142. } while (0)
  143. /*-- mpiutil.c --*/
  144. mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs);
  145. void mpi_free_limb_space(mpi_ptr_t a);
  146. void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs);
  147. /*-- mpi-bit.c --*/
  148. void mpi_rshift_limbs(MPI a, unsigned int count);
  149. int mpi_lshift_limbs(MPI a, unsigned int count);
  150. /*-- mpihelp-add.c --*/
  151. static inline mpi_limb_t mpihelp_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  152. mpi_size_t s1_size, mpi_limb_t s2_limb);
  153. mpi_limb_t mpihelp_add_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  154. mpi_ptr_t s2_ptr, mpi_size_t size);
  155. static inline mpi_limb_t mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
  156. mpi_ptr_t s2_ptr, mpi_size_t s2_size);
  157. /*-- mpihelp-sub.c --*/
  158. static inline mpi_limb_t mpihelp_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  159. mpi_size_t s1_size, mpi_limb_t s2_limb);
  160. mpi_limb_t mpihelp_sub_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  161. mpi_ptr_t s2_ptr, mpi_size_t size);
  162. static inline mpi_limb_t mpihelp_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
  163. mpi_ptr_t s2_ptr, mpi_size_t s2_size);
  164. /*-- mpihelp-cmp.c --*/
  165. int mpihelp_cmp(mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size);
  166. /*-- mpihelp-mul.c --*/
  167. struct karatsuba_ctx {
  168. struct karatsuba_ctx *next;
  169. mpi_ptr_t tspace;
  170. mpi_size_t tspace_size;
  171. mpi_ptr_t tp;
  172. mpi_size_t tp_size;
  173. };
  174. void mpihelp_release_karatsuba_ctx(struct karatsuba_ctx *ctx);
  175. mpi_limb_t mpihelp_addmul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  176. mpi_size_t s1_size, mpi_limb_t s2_limb);
  177. mpi_limb_t mpihelp_submul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  178. mpi_size_t s1_size, mpi_limb_t s2_limb);
  179. int mpihelp_mul_n(mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp, mpi_size_t size);
  180. int mpihelp_mul(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
  181. mpi_ptr_t vp, mpi_size_t vsize, mpi_limb_t *_result);
  182. void mpih_sqr_n_basecase(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size);
  183. void mpih_sqr_n(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size,
  184. mpi_ptr_t tspace);
  185. int mpihelp_mul_karatsuba_case(mpi_ptr_t prodp,
  186. mpi_ptr_t up, mpi_size_t usize,
  187. mpi_ptr_t vp, mpi_size_t vsize,
  188. struct karatsuba_ctx *ctx);
  189. /*-- mpihelp-mul_1.c (or xxx/cpu/ *.S) --*/
  190. mpi_limb_t mpihelp_mul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  191. mpi_size_t s1_size, mpi_limb_t s2_limb);
  192. /*-- mpihelp-div.c --*/
  193. mpi_limb_t mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
  194. mpi_limb_t divisor_limb);
  195. mpi_limb_t mpihelp_divrem(mpi_ptr_t qp, mpi_size_t qextra_limbs,
  196. mpi_ptr_t np, mpi_size_t nsize,
  197. mpi_ptr_t dp, mpi_size_t dsize);
  198. mpi_limb_t mpihelp_divmod_1(mpi_ptr_t quot_ptr,
  199. mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
  200. mpi_limb_t divisor_limb);
  201. /*-- mpihelp-shift.c --*/
  202. mpi_limb_t mpihelp_lshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
  203. unsigned cnt);
  204. mpi_limb_t mpihelp_rshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
  205. unsigned cnt);
  206. /* Define stuff for longlong.h. */
  207. #define W_TYPE_SIZE BITS_PER_MPI_LIMB
  208. typedef mpi_limb_t UWtype;
  209. typedef unsigned int UHWtype;
  210. #if defined(__GNUC__)
  211. typedef unsigned int UQItype __attribute__ ((mode(QI)));
  212. typedef int SItype __attribute__ ((mode(SI)));
  213. typedef unsigned int USItype __attribute__ ((mode(SI)));
  214. typedef int DItype __attribute__ ((mode(DI)));
  215. typedef unsigned int UDItype __attribute__ ((mode(DI)));
  216. #else
  217. typedef unsigned char UQItype;
  218. typedef long SItype;
  219. typedef unsigned long USItype;
  220. #endif
  221. #ifdef __GNUC__
  222. #include "mpi-inline.h"
  223. #endif
  224. #endif /*G10_MPI_INTERNAL_H */