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