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.
6 * This file is part of GNUPG.
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.
20 #include <linux/types.h>
21 #include <linux/scatterlist.h>
23 #define BYTES_PER_MPI_LIMB (BITS_PER_LONG / 8)
24 #define BITS_PER_MPI_LIMB BITS_PER_LONG
26 typedef unsigned long int mpi_limb_t;
27 typedef signed long int mpi_limb_signed_t;
30 int alloced; /* array size (# of allocated limbs) */
31 int nlimbs; /* number of valid limbs */
32 int nbits; /* the real number of valid bits (info only) */
33 int sign; /* indicates a negative number */
34 unsigned flags; /* bit 0: array must be allocated in secure memory space */
36 /* bit 2: the limb is a pointer to some m_alloced data */
37 mpi_limb_t *d; /* array with the limbs */
40 typedef struct gcry_mpi *MPI;
42 #define mpi_get_nlimbs(a) ((a)->nlimbs)
43 #define mpi_has_sign(a) ((a)->sign)
46 MPI mpi_alloc(unsigned nlimbs);
47 void mpi_clear(MPI a);
49 int mpi_resize(MPI a, unsigned nlimbs);
51 static inline MPI mpi_new(unsigned int nbits)
53 return mpi_alloc((nbits + BITS_PER_MPI_LIMB - 1) / BITS_PER_MPI_LIMB);
57 MPI mpi_alloc_like(MPI a);
58 void mpi_snatch(MPI w, MPI u);
59 MPI mpi_set(MPI w, MPI u);
60 MPI mpi_set_ui(MPI w, unsigned long u);
61 MPI mpi_alloc_set_ui(unsigned long u);
62 void mpi_swap_cond(MPI a, MPI b, unsigned long swap);
64 /* Constants used to return constant MPIs. See mpi_init if you
65 * want to add more constants.
67 #define MPI_NUMBER_OF_CONSTANTS 6
68 enum gcry_mpi_constants {
77 MPI mpi_const(enum gcry_mpi_constants no);
81 /* Different formats of external big integer representation. */
82 enum gcry_mpi_format {
84 GCRYMPI_FMT_STD = 1, /* Twos complement stored without length. */
85 GCRYMPI_FMT_PGP = 2, /* As used by OpenPGP (unsigned only). */
86 GCRYMPI_FMT_SSH = 3, /* As used by SSH (like STD but with length). */
87 GCRYMPI_FMT_HEX = 4, /* Hex format. */
88 GCRYMPI_FMT_USG = 5, /* Like STD but unsigned. */
89 GCRYMPI_FMT_OPAQUE = 8 /* Opaque format (some functions only). */
92 MPI mpi_read_raw_data(const void *xbuffer, size_t nbytes);
93 MPI mpi_read_from_buffer(const void *buffer, unsigned *ret_nread);
94 int mpi_fromstr(MPI val, const char *str);
95 MPI mpi_scanval(const char *string);
96 MPI mpi_read_raw_from_sgl(struct scatterlist *sgl, unsigned int len);
97 void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign);
98 int mpi_read_buffer(MPI a, uint8_t *buf, unsigned buf_len, unsigned *nbytes,
100 int mpi_write_to_sgl(MPI a, struct scatterlist *sg, unsigned nbytes,
102 int mpi_print(enum gcry_mpi_format format, unsigned char *buffer,
103 size_t buflen, size_t *nwritten, MPI a);
106 void mpi_mod(MPI rem, MPI dividend, MPI divisor);
108 /* Context used with Barrett reduction. */
109 struct barrett_ctx_s;
110 typedef struct barrett_ctx_s *mpi_barrett_t;
112 mpi_barrett_t mpi_barrett_init(MPI m, int copy);
113 void mpi_barrett_free(mpi_barrett_t ctx);
114 void mpi_mod_barrett(MPI r, MPI x, mpi_barrett_t ctx);
115 void mpi_mul_barrett(MPI w, MPI u, MPI v, mpi_barrett_t ctx);
118 int mpi_powm(MPI res, MPI base, MPI exp, MPI mod);
121 int mpi_cmp_ui(MPI u, ulong v);
122 int mpi_cmp(MPI u, MPI v);
123 int mpi_cmpabs(MPI u, MPI v);
125 /*-- mpi-sub-ui.c --*/
126 int mpi_sub_ui(MPI w, MPI u, unsigned long vval);
129 void mpi_normalize(MPI a);
130 unsigned mpi_get_nbits(MPI a);
131 int mpi_test_bit(MPI a, unsigned int n);
132 void mpi_set_bit(MPI a, unsigned int n);
133 void mpi_set_highbit(MPI a, unsigned int n);
134 void mpi_clear_highbit(MPI a, unsigned int n);
135 void mpi_clear_bit(MPI a, unsigned int n);
136 void mpi_rshift_limbs(MPI a, unsigned int count);
137 void mpi_rshift(MPI x, MPI a, unsigned int n);
138 void mpi_lshift_limbs(MPI a, unsigned int count);
139 void mpi_lshift(MPI x, MPI a, unsigned int n);
142 void mpi_add_ui(MPI w, MPI u, unsigned long v);
143 void mpi_add(MPI w, MPI u, MPI v);
144 void mpi_sub(MPI w, MPI u, MPI v);
145 void mpi_addm(MPI w, MPI u, MPI v, MPI m);
146 void mpi_subm(MPI w, MPI u, MPI v, MPI m);
149 void mpi_mul(MPI w, MPI u, MPI v);
150 void mpi_mulm(MPI w, MPI u, MPI v, MPI m);
153 void mpi_tdiv_r(MPI rem, MPI num, MPI den);
154 void mpi_fdiv_r(MPI rem, MPI dividend, MPI divisor);
155 void mpi_fdiv_q(MPI quot, MPI dividend, MPI divisor);
158 int mpi_invm(MPI x, MPI a, MPI n);
160 /* inline functions */
163 * mpi_get_size() - returns max size required to store the number
165 * @a: A multi precision integer for which we want to allocate a bufer
167 * Return: size required to store the number
169 static inline unsigned int mpi_get_size(MPI a)
171 return a->nlimbs * BYTES_PER_MPI_LIMB;
173 #endif /*G10_MPI_H */