mpi.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* mpi.h - Multi Precision Integers
  2. * Copyright (C) 1994, 1996, 1998, 1999,
  3. * 2000, 2001 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_H
  30. #define G10_MPI_H
  31. #include <linux/types.h>
  32. #include <linux/scatterlist.h>
  33. /* DSI defines */
  34. #define SHA1_DIGEST_LENGTH 20
  35. /*end of DSI defines */
  36. #define BYTES_PER_MPI_LIMB (BITS_PER_LONG / 8)
  37. #define BITS_PER_MPI_LIMB BITS_PER_LONG
  38. typedef unsigned long int mpi_limb_t;
  39. typedef signed long int mpi_limb_signed_t;
  40. struct gcry_mpi {
  41. int alloced; /* array size (# of allocated limbs) */
  42. int nlimbs; /* number of valid limbs */
  43. int nbits; /* the real number of valid bits (info only) */
  44. int sign; /* indicates a negative number */
  45. unsigned flags; /* bit 0: array must be allocated in secure memory space */
  46. /* bit 1: not used */
  47. /* bit 2: the limb is a pointer to some m_alloced data */
  48. mpi_limb_t *d; /* array with the limbs */
  49. };
  50. typedef struct gcry_mpi *MPI;
  51. #define mpi_get_nlimbs(a) ((a)->nlimbs)
  52. #define mpi_is_neg(a) ((a)->sign)
  53. /*-- mpiutil.c --*/
  54. MPI mpi_alloc(unsigned nlimbs);
  55. MPI mpi_alloc_secure(unsigned nlimbs);
  56. MPI mpi_alloc_like(MPI a);
  57. void mpi_free(MPI a);
  58. int mpi_resize(MPI a, unsigned nlimbs);
  59. int mpi_copy(MPI *copy, const MPI a);
  60. void mpi_clear(MPI a);
  61. int mpi_set(MPI w, MPI u);
  62. int mpi_set_ui(MPI w, ulong u);
  63. MPI mpi_alloc_set_ui(unsigned long u);
  64. void mpi_m_check(MPI a);
  65. void mpi_swap(MPI a, MPI b);
  66. /*-- mpicoder.c --*/
  67. MPI do_encode_md(const void *sha_buffer, unsigned nbits);
  68. MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes);
  69. MPI mpi_read_from_buffer(const void *buffer, unsigned *ret_nread);
  70. MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int len);
  71. int mpi_fromstr(MPI val, const char *str);
  72. u32 mpi_get_keyid(MPI a, u32 *keyid);
  73. void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign);
  74. int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
  75. int *sign);
  76. void *mpi_get_secure_buffer(MPI a, unsigned *nbytes, int *sign);
  77. int mpi_set_buffer(MPI a, const void *buffer, unsigned nbytes, int sign);
  78. int mpi_write_to_sgl(MPI a, struct scatterlist *sg, unsigned *nbytes,
  79. int *sign);
  80. #define log_mpidump g10_log_mpidump
  81. /*-- mpi-add.c --*/
  82. int mpi_add_ui(MPI w, MPI u, ulong v);
  83. int mpi_add(MPI w, MPI u, MPI v);
  84. int mpi_addm(MPI w, MPI u, MPI v, MPI m);
  85. int mpi_sub_ui(MPI w, MPI u, ulong v);
  86. int mpi_sub(MPI w, MPI u, MPI v);
  87. int mpi_subm(MPI w, MPI u, MPI v, MPI m);
  88. /*-- mpi-mul.c --*/
  89. int mpi_mul_ui(MPI w, MPI u, ulong v);
  90. int mpi_mul_2exp(MPI w, MPI u, ulong cnt);
  91. int mpi_mul(MPI w, MPI u, MPI v);
  92. int mpi_mulm(MPI w, MPI u, MPI v, MPI m);
  93. /*-- mpi-div.c --*/
  94. ulong mpi_fdiv_r_ui(MPI rem, MPI dividend, ulong divisor);
  95. int mpi_fdiv_r(MPI rem, MPI dividend, MPI divisor);
  96. int mpi_fdiv_q(MPI quot, MPI dividend, MPI divisor);
  97. int mpi_fdiv_qr(MPI quot, MPI rem, MPI dividend, MPI divisor);
  98. int mpi_tdiv_r(MPI rem, MPI num, MPI den);
  99. int mpi_tdiv_qr(MPI quot, MPI rem, MPI num, MPI den);
  100. int mpi_tdiv_q_2exp(MPI w, MPI u, unsigned count);
  101. int mpi_divisible_ui(const MPI dividend, ulong divisor);
  102. /*-- mpi-gcd.c --*/
  103. int mpi_gcd(MPI g, const MPI a, const MPI b);
  104. /*-- mpi-pow.c --*/
  105. int mpi_pow(MPI w, MPI u, MPI v);
  106. int mpi_powm(MPI res, MPI base, MPI exp, MPI mod);
  107. /*-- mpi-mpow.c --*/
  108. int mpi_mulpowm(MPI res, MPI *basearray, MPI *exparray, MPI mod);
  109. /*-- mpi-cmp.c --*/
  110. int mpi_cmp_ui(MPI u, ulong v);
  111. int mpi_cmp(MPI u, MPI v);
  112. /*-- mpi-scan.c --*/
  113. int mpi_getbyte(MPI a, unsigned idx);
  114. void mpi_putbyte(MPI a, unsigned idx, int value);
  115. unsigned mpi_trailing_zeros(MPI a);
  116. /*-- mpi-bit.c --*/
  117. void mpi_normalize(MPI a);
  118. unsigned mpi_get_nbits(MPI a);
  119. int mpi_test_bit(MPI a, unsigned n);
  120. int mpi_set_bit(MPI a, unsigned n);
  121. int mpi_set_highbit(MPI a, unsigned n);
  122. void mpi_clear_highbit(MPI a, unsigned n);
  123. void mpi_clear_bit(MPI a, unsigned n);
  124. int mpi_rshift(MPI x, MPI a, unsigned n);
  125. /*-- mpi-inv.c --*/
  126. int mpi_invm(MPI x, MPI u, MPI v);
  127. /* inline functions */
  128. /**
  129. * mpi_get_size() - returns max size required to store the number
  130. *
  131. * @a: A multi precision integer for which we want to allocate a bufer
  132. *
  133. * Return: size required to store the number
  134. */
  135. static inline unsigned int mpi_get_size(MPI a)
  136. {
  137. return a->nlimbs * BYTES_PER_MPI_LIMB;
  138. }
  139. #endif /*G10_MPI_H */