mpi-internal.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. /* Copy N limbs from S to D. */
  60. #define MPN_COPY(d, s, n) \
  61. do { \
  62. mpi_size_t _i; \
  63. for (_i = 0; _i < (n); _i++) \
  64. (d)[_i] = (s)[_i]; \
  65. } while (0)
  66. #define MPN_COPY_DECR(d, s, n) \
  67. do { \
  68. mpi_size_t _i; \
  69. for (_i = (n)-1; _i >= 0; _i--) \
  70. (d)[_i] = (s)[_i]; \
  71. } while (0)
  72. /* Zero N limbs at D */
  73. #define MPN_ZERO(d, n) \
  74. do { \
  75. int _i; \
  76. for (_i = 0; _i < (n); _i++) \
  77. (d)[_i] = 0; \
  78. } while (0)
  79. #define MPN_NORMALIZE(d, n) \
  80. do { \
  81. while ((n) > 0) { \
  82. if ((d)[(n)-1]) \
  83. break; \
  84. (n)--; \
  85. } \
  86. } while (0)
  87. #define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
  88. do { \
  89. if ((size) < KARATSUBA_THRESHOLD) \
  90. mul_n_basecase(prodp, up, vp, size); \
  91. else \
  92. mul_n(prodp, up, vp, size, tspace); \
  93. } while (0);
  94. /*-- mpiutil.c --*/
  95. mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs);
  96. void mpi_free_limb_space(mpi_ptr_t a);
  97. void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs);
  98. static inline mpi_limb_t mpihelp_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  99. mpi_size_t s1_size, mpi_limb_t s2_limb);
  100. mpi_limb_t mpihelp_add_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  101. mpi_ptr_t s2_ptr, mpi_size_t size);
  102. static inline mpi_limb_t mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
  103. mpi_ptr_t s2_ptr, mpi_size_t s2_size);
  104. static inline mpi_limb_t mpihelp_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  105. mpi_size_t s1_size, mpi_limb_t s2_limb);
  106. mpi_limb_t mpihelp_sub_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  107. mpi_ptr_t s2_ptr, mpi_size_t size);
  108. static inline mpi_limb_t mpihelp_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
  109. mpi_ptr_t s2_ptr, mpi_size_t s2_size);
  110. /*-- mpih-cmp.c --*/
  111. int mpihelp_cmp(mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size);
  112. /*-- mpih-mul.c --*/
  113. struct karatsuba_ctx {
  114. struct karatsuba_ctx *next;
  115. mpi_ptr_t tspace;
  116. mpi_size_t tspace_size;
  117. mpi_ptr_t tp;
  118. mpi_size_t tp_size;
  119. };
  120. void mpihelp_release_karatsuba_ctx(struct karatsuba_ctx *ctx);
  121. mpi_limb_t mpihelp_addmul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  122. mpi_size_t s1_size, mpi_limb_t s2_limb);
  123. mpi_limb_t mpihelp_submul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  124. mpi_size_t s1_size, mpi_limb_t s2_limb);
  125. int mpihelp_mul(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
  126. mpi_ptr_t vp, mpi_size_t vsize, mpi_limb_t *_result);
  127. void mpih_sqr_n_basecase(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size);
  128. void mpih_sqr_n(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size,
  129. mpi_ptr_t tspace);
  130. int mpihelp_mul_karatsuba_case(mpi_ptr_t prodp,
  131. mpi_ptr_t up, mpi_size_t usize,
  132. mpi_ptr_t vp, mpi_size_t vsize,
  133. struct karatsuba_ctx *ctx);
  134. /*-- generic_mpih-mul1.c --*/
  135. mpi_limb_t mpihelp_mul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
  136. mpi_size_t s1_size, mpi_limb_t s2_limb);
  137. /*-- mpih-div.c --*/
  138. mpi_limb_t mpihelp_divrem(mpi_ptr_t qp, mpi_size_t qextra_limbs,
  139. mpi_ptr_t np, mpi_size_t nsize,
  140. mpi_ptr_t dp, mpi_size_t dsize);
  141. /*-- generic_mpih-[lr]shift.c --*/
  142. mpi_limb_t mpihelp_lshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
  143. unsigned cnt);
  144. mpi_limb_t mpihelp_rshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
  145. unsigned cnt);
  146. /* Define stuff for longlong.h. */
  147. #define W_TYPE_SIZE BITS_PER_MPI_LIMB
  148. typedef mpi_limb_t UWtype;
  149. typedef unsigned int UHWtype;
  150. #if defined(__GNUC__)
  151. typedef unsigned int UQItype __attribute__ ((mode(QI)));
  152. typedef int SItype __attribute__ ((mode(SI)));
  153. typedef unsigned int USItype __attribute__ ((mode(SI)));
  154. typedef int DItype __attribute__ ((mode(DI)));
  155. typedef unsigned int UDItype __attribute__ ((mode(DI)));
  156. #else
  157. typedef unsigned char UQItype;
  158. typedef long SItype;
  159. typedef unsigned long USItype;
  160. #endif
  161. #ifdef __GNUC__
  162. #include "mpi-inline.h"
  163. #endif
  164. #endif /*G10_MPI_INTERNAL_H */