mpi.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* mpi.h - Multi Precision Integers
  3. * Copyright (C) 1994, 1996, 1998, 1999,
  4. * 2000, 2001 Free Software Foundation, Inc.
  5. *
  6. * This file is part of GNUPG.
  7. *
  8. * Note: This code is heavily based on the GNU MP Library.
  9. * Actually it's the same code with only minor changes in the
  10. * way the data is stored; this is to support the abstraction
  11. * of an optional secure memory allocation which may be used
  12. * to avoid revealing of sensitive data due to paging etc.
  13. * The GNU MP Library itself is published under the LGPL;
  14. * however I decided to publish this code under the plain GPL.
  15. */
  16. #ifndef G10_MPI_H
  17. #define G10_MPI_H
  18. #include <linux/types.h>
  19. #include <linux/scatterlist.h>
  20. #define BYTES_PER_MPI_LIMB (BITS_PER_LONG / 8)
  21. #define BITS_PER_MPI_LIMB BITS_PER_LONG
  22. typedef unsigned long int mpi_limb_t;
  23. typedef signed long int mpi_limb_signed_t;
  24. struct gcry_mpi {
  25. int alloced; /* array size (# of allocated limbs) */
  26. int nlimbs; /* number of valid limbs */
  27. int nbits; /* the real number of valid bits (info only) */
  28. int sign; /* indicates a negative number */
  29. unsigned flags; /* bit 0: array must be allocated in secure memory space */
  30. /* bit 1: not used */
  31. /* bit 2: the limb is a pointer to some m_alloced data */
  32. mpi_limb_t *d; /* array with the limbs */
  33. };
  34. typedef struct gcry_mpi *MPI;
  35. #define mpi_get_nlimbs(a) ((a)->nlimbs)
  36. #define mpi_has_sign(a) ((a)->sign)
  37. /*-- mpiutil.c --*/
  38. MPI mpi_alloc(unsigned nlimbs);
  39. void mpi_clear(MPI a);
  40. void mpi_free(MPI a);
  41. int mpi_resize(MPI a, unsigned nlimbs);
  42. static inline MPI mpi_new(unsigned int nbits)
  43. {
  44. return mpi_alloc((nbits + BITS_PER_MPI_LIMB - 1) / BITS_PER_MPI_LIMB);
  45. }
  46. MPI mpi_copy(MPI a);
  47. MPI mpi_alloc_like(MPI a);
  48. void mpi_snatch(MPI w, MPI u);
  49. MPI mpi_set(MPI w, MPI u);
  50. MPI mpi_set_ui(MPI w, unsigned long u);
  51. MPI mpi_alloc_set_ui(unsigned long u);
  52. void mpi_swap_cond(MPI a, MPI b, unsigned long swap);
  53. /* Constants used to return constant MPIs. See mpi_init if you
  54. * want to add more constants.
  55. */
  56. #define MPI_NUMBER_OF_CONSTANTS 6
  57. enum gcry_mpi_constants {
  58. MPI_C_ZERO,
  59. MPI_C_ONE,
  60. MPI_C_TWO,
  61. MPI_C_THREE,
  62. MPI_C_FOUR,
  63. MPI_C_EIGHT
  64. };
  65. MPI mpi_const(enum gcry_mpi_constants no);
  66. /*-- mpicoder.c --*/
  67. /* Different formats of external big integer representation. */
  68. enum gcry_mpi_format {
  69. GCRYMPI_FMT_NONE = 0,
  70. GCRYMPI_FMT_STD = 1, /* Twos complement stored without length. */
  71. GCRYMPI_FMT_PGP = 2, /* As used by OpenPGP (unsigned only). */
  72. GCRYMPI_FMT_SSH = 3, /* As used by SSH (like STD but with length). */
  73. GCRYMPI_FMT_HEX = 4, /* Hex format. */
  74. GCRYMPI_FMT_USG = 5, /* Like STD but unsigned. */
  75. GCRYMPI_FMT_OPAQUE = 8 /* Opaque format (some functions only). */
  76. };
  77. MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes);
  78. MPI mpi_read_from_buffer(const void *buffer, unsigned *ret_nread);
  79. int mpi_fromstr(MPI val, const char *str);
  80. MPI mpi_scanval(const char *string);
  81. MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int len);
  82. void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign);
  83. int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
  84. int *sign);
  85. int mpi_write_to_sgl(MPI a, struct scatterlist *sg, unsigned nbytes,
  86. int *sign);
  87. int mpi_print(enum gcry_mpi_format format, unsigned char *buffer,
  88. size_t buflen, size_t *nwritten, MPI a);
  89. /*-- mpi-mod.c --*/
  90. void mpi_mod(MPI rem, MPI dividend, MPI divisor);
  91. /* Context used with Barrett reduction. */
  92. struct barrett_ctx_s;
  93. typedef struct barrett_ctx_s *mpi_barrett_t;
  94. mpi_barrett_t mpi_barrett_init(MPI m, int copy);
  95. void mpi_barrett_free(mpi_barrett_t ctx);
  96. void mpi_mod_barrett(MPI r, MPI x, mpi_barrett_t ctx);
  97. void mpi_mul_barrett(MPI w, MPI u, MPI v, mpi_barrett_t ctx);
  98. /*-- mpi-pow.c --*/
  99. int mpi_powm(MPI res, MPI base, MPI exp, MPI mod);
  100. /*-- mpi-cmp.c --*/
  101. int mpi_cmp_ui(MPI u, ulong v);
  102. int mpi_cmp(MPI u, MPI v);
  103. int mpi_cmpabs(MPI u, MPI v);
  104. /*-- mpi-sub-ui.c --*/
  105. int mpi_sub_ui(MPI w, MPI u, unsigned long vval);
  106. /*-- mpi-bit.c --*/
  107. void mpi_normalize(MPI a);
  108. unsigned mpi_get_nbits(MPI a);
  109. int mpi_test_bit(MPI a, unsigned int n);
  110. void mpi_set_bit(MPI a, unsigned int n);
  111. void mpi_set_highbit(MPI a, unsigned int n);
  112. void mpi_clear_highbit(MPI a, unsigned int n);
  113. void mpi_clear_bit(MPI a, unsigned int n);
  114. void mpi_rshift_limbs(MPI a, unsigned int count);
  115. void mpi_rshift(MPI x, MPI a, unsigned int n);
  116. void mpi_lshift_limbs(MPI a, unsigned int count);
  117. void mpi_lshift(MPI x, MPI a, unsigned int n);
  118. /*-- mpi-add.c --*/
  119. void mpi_add_ui(MPI w, MPI u, unsigned long v);
  120. void mpi_add(MPI w, MPI u, MPI v);
  121. void mpi_sub(MPI w, MPI u, MPI v);
  122. void mpi_addm(MPI w, MPI u, MPI v, MPI m);
  123. void mpi_subm(MPI w, MPI u, MPI v, MPI m);
  124. /*-- mpi-mul.c --*/
  125. void mpi_mul(MPI w, MPI u, MPI v);
  126. void mpi_mulm(MPI w, MPI u, MPI v, MPI m);
  127. /*-- mpi-div.c --*/
  128. void mpi_tdiv_r(MPI rem, MPI num, MPI den);
  129. void mpi_fdiv_r(MPI rem, MPI dividend, MPI divisor);
  130. void mpi_fdiv_q(MPI quot, MPI dividend, MPI divisor);
  131. /*-- mpi-inv.c --*/
  132. int mpi_invm(MPI x, MPI a, MPI n);
  133. /*-- ec.c --*/
  134. /* Object to represent a point in projective coordinates */
  135. struct gcry_mpi_point {
  136. MPI x;
  137. MPI y;
  138. MPI z;
  139. };
  140. typedef struct gcry_mpi_point *MPI_POINT;
  141. /* Models describing an elliptic curve */
  142. enum gcry_mpi_ec_models {
  143. /* The Short Weierstrass equation is
  144. * y^2 = x^3 + ax + b
  145. */
  146. MPI_EC_WEIERSTRASS = 0,
  147. /* The Montgomery equation is
  148. * by^2 = x^3 + ax^2 + x
  149. */
  150. MPI_EC_MONTGOMERY,
  151. /* The Twisted Edwards equation is
  152. * ax^2 + y^2 = 1 + bx^2y^2
  153. * Note that we use 'b' instead of the commonly used 'd'.
  154. */
  155. MPI_EC_EDWARDS
  156. };
  157. /* Dialects used with elliptic curves */
  158. enum ecc_dialects {
  159. ECC_DIALECT_STANDARD = 0,
  160. ECC_DIALECT_ED25519,
  161. ECC_DIALECT_SAFECURVE
  162. };
  163. /* This context is used with all our EC functions. */
  164. struct mpi_ec_ctx {
  165. enum gcry_mpi_ec_models model; /* The model describing this curve. */
  166. enum ecc_dialects dialect; /* The ECC dialect used with the curve. */
  167. int flags; /* Public key flags (not always used). */
  168. unsigned int nbits; /* Number of bits. */
  169. /* Domain parameters. Note that they may not all be set and if set
  170. * the MPIs may be flagged as constant.
  171. */
  172. MPI p; /* Prime specifying the field GF(p). */
  173. MPI a; /* First coefficient of the Weierstrass equation. */
  174. MPI b; /* Second coefficient of the Weierstrass equation. */
  175. MPI_POINT G; /* Base point (generator). */
  176. MPI n; /* Order of G. */
  177. unsigned int h; /* Cofactor. */
  178. /* The actual key. May not be set. */
  179. MPI_POINT Q; /* Public key. */
  180. MPI d; /* Private key. */
  181. const char *name; /* Name of the curve. */
  182. /* This structure is private to mpi/ec.c! */
  183. struct {
  184. struct {
  185. unsigned int a_is_pminus3:1;
  186. unsigned int two_inv_p:1;
  187. } valid; /* Flags to help setting the helper vars below. */
  188. int a_is_pminus3; /* True if A = P - 3. */
  189. MPI two_inv_p;
  190. mpi_barrett_t p_barrett;
  191. /* Scratch variables. */
  192. MPI scratch[11];
  193. /* Helper for fast reduction. */
  194. /* int nist_nbits; /\* If this is a NIST curve, the # of bits. *\/ */
  195. /* MPI s[10]; */
  196. /* MPI c; */
  197. } t;
  198. /* Curve specific computation routines for the field. */
  199. void (*addm)(MPI w, MPI u, MPI v, struct mpi_ec_ctx *ctx);
  200. void (*subm)(MPI w, MPI u, MPI v, struct mpi_ec_ctx *ec);
  201. void (*mulm)(MPI w, MPI u, MPI v, struct mpi_ec_ctx *ctx);
  202. void (*pow2)(MPI w, const MPI b, struct mpi_ec_ctx *ctx);
  203. void (*mul2)(MPI w, MPI u, struct mpi_ec_ctx *ctx);
  204. };
  205. void mpi_ec_init(struct mpi_ec_ctx *ctx, enum gcry_mpi_ec_models model,
  206. enum ecc_dialects dialect,
  207. int flags, MPI p, MPI a, MPI b);
  208. void mpi_ec_deinit(struct mpi_ec_ctx *ctx);
  209. MPI_POINT mpi_point_new(unsigned int nbits);
  210. void mpi_point_release(MPI_POINT p);
  211. void mpi_point_init(MPI_POINT p);
  212. void mpi_point_free_parts(MPI_POINT p);
  213. int mpi_ec_get_affine(MPI x, MPI y, MPI_POINT point, struct mpi_ec_ctx *ctx);
  214. void mpi_ec_add_points(MPI_POINT result,
  215. MPI_POINT p1, MPI_POINT p2,
  216. struct mpi_ec_ctx *ctx);
  217. void mpi_ec_mul_point(MPI_POINT result,
  218. MPI scalar, MPI_POINT point,
  219. struct mpi_ec_ctx *ctx);
  220. int mpi_ec_curve_point(MPI_POINT point, struct mpi_ec_ctx *ctx);
  221. /* inline functions */
  222. /**
  223. * mpi_get_size() - returns max size required to store the number
  224. *
  225. * @a: A multi precision integer for which we want to allocate a buffer
  226. *
  227. * Return: size required to store the number
  228. */
  229. static inline unsigned int mpi_get_size(MPI a)
  230. {
  231. return a->nlimbs * BYTES_PER_MPI_LIMB;
  232. }
  233. #endif /*G10_MPI_H */