Linux Audio

Check our new training course

Loading...
v4.17
 
  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
 30#ifndef G10_MPI_INTERNAL_H
 31#define G10_MPI_INTERNAL_H
 32
 33#include <linux/module.h>
 34#include <linux/kernel.h>
 35#include <linux/slab.h>
 36#include <linux/string.h>
 37#include <linux/mpi.h>
 38#include <linux/errno.h>
 39
 40#define log_debug printk
 41#define log_bug printk
 42
 43#define assert(x) \
 44	do { \
 45		if (!x) \
 46			log_bug("failed assertion\n"); \
 47	} while (0);
 48
 49/* If KARATSUBA_THRESHOLD is not already defined, define it to a
 50 * value which is good on most machines.  */
 51
 52/* tested 4, 16, 32 and 64, where 16 gave the best performance when
 53 * checking a 768 and a 1024 bit ElGamal signature.
 54 * (wk 22.12.97) */
 55#ifndef KARATSUBA_THRESHOLD
 56#define KARATSUBA_THRESHOLD 16
 57#endif
 58
 59/* The code can't handle KARATSUBA_THRESHOLD smaller than 2.  */
 60#if KARATSUBA_THRESHOLD < 2
 61#undef KARATSUBA_THRESHOLD
 62#define KARATSUBA_THRESHOLD 2
 63#endif
 64
 65typedef mpi_limb_t *mpi_ptr_t;	/* pointer to a limb */
 66typedef int mpi_size_t;		/* (must be a signed type) */
 67
 68static inline int RESIZE_IF_NEEDED(MPI a, unsigned b)
 69{
 70	if (a->alloced < b)
 71		return mpi_resize(a, b);
 72	return 0;
 73}
 74
 75/* Copy N limbs from S to D.  */
 76#define MPN_COPY(d, s, n) \
 77	do {					\
 78		mpi_size_t _i;			\
 79		for (_i = 0; _i < (n); _i++)	\
 80			(d)[_i] = (s)[_i];	\
 81	} while (0)
 82
 83#define MPN_COPY_INCR(d, s, n) \
 84	do {					\
 85		mpi_size_t _i;			\
 86		for (_i = 0; _i < (n); _i++)	\
 87			(d)[_i] = (s)[_i];	\
 88	} while (0)
 89
 
 90#define MPN_COPY_DECR(d, s, n) \
 91	do {					\
 92		mpi_size_t _i;			\
 93		for (_i = (n)-1; _i >= 0; _i--) \
 94			(d)[_i] = (s)[_i];	\
 95	} while (0)
 96
 97/* Zero N limbs at D */
 98#define MPN_ZERO(d, n) \
 99	do {					\
100		int  _i;			\
101		for (_i = 0; _i < (n); _i++)	\
102			(d)[_i] = 0;		\
103	} while (0)
104
105#define MPN_NORMALIZE(d, n)  \
106	do {					\
107		while ((n) > 0) {		\
108			if ((d)[(n)-1])		\
109				break;		\
110			(n)--;			\
111		}				\
112	} while (0)
113
114#define MPN_NORMALIZE_NOT_ZERO(d, n) \
115	do {				\
116		for (;;) {		\
117			if ((d)[(n)-1])	\
118				break;	\
119			(n)--;		\
120		}			\
121	} while (0)
122
123#define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
124	do {							\
125		if ((size) < KARATSUBA_THRESHOLD)		\
126			mul_n_basecase(prodp, up, vp, size);	\
127		else						\
128			mul_n(prodp, up, vp, size, tspace);	\
129	} while (0);
130
131/* Divide the two-limb number in (NH,,NL) by D, with DI being the largest
132 * limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB).
133 * If this would yield overflow, DI should be the largest possible number
134 * (i.e., only ones).  For correct operation, the most significant bit of D
135 * has to be set.  Put the quotient in Q and the remainder in R.
136 */
137#define UDIV_QRNND_PREINV(q, r, nh, nl, d, di) \
138	do {								\
139		mpi_limb_t _q, _ql, _r;					\
 
140		mpi_limb_t _xh, _xl;					\
141		umul_ppmm(_q, _ql, (nh), (di));				\
142		_q += (nh);	/* DI is 2**BITS_PER_MPI_LIMB too small */ \
143		umul_ppmm(_xh, _xl, _q, (d));				\
144		sub_ddmmss(_xh, _r, (nh), (nl), _xh, _xl);		\
145		if (_xh) {						\
146			sub_ddmmss(_xh, _r, _xh, _r, 0, (d));		\
147			_q++;						\
148			if (_xh) {					\
149				sub_ddmmss(_xh, _r, _xh, _r, 0, (d));	\
150				_q++;					\
151			}						\
152		}							\
153		if (_r >= (d)) {					\
154			_r -= (d);					\
155			_q++;						\
156		}							\
157		(r) = _r;						\
158		(q) = _q;						\
159	} while (0)
160
 
161/*-- mpiutil.c --*/
162mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs);
163void mpi_free_limb_space(mpi_ptr_t a);
164void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs);
165
166/*-- mpi-bit.c --*/
167void mpi_rshift_limbs(MPI a, unsigned int count);
168int mpi_lshift_limbs(MPI a, unsigned int count);
169
170/*-- mpihelp-add.c --*/
171static inline mpi_limb_t mpihelp_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
172			 mpi_size_t s1_size, mpi_limb_t s2_limb);
173mpi_limb_t mpihelp_add_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
174			 mpi_ptr_t s2_ptr, mpi_size_t size);
175static inline mpi_limb_t mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
176		       mpi_ptr_t s2_ptr, mpi_size_t s2_size);
177
178/*-- mpihelp-sub.c --*/
179static inline mpi_limb_t mpihelp_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
180			 mpi_size_t s1_size, mpi_limb_t s2_limb);
181mpi_limb_t mpihelp_sub_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
182			 mpi_ptr_t s2_ptr, mpi_size_t size);
183static inline mpi_limb_t mpihelp_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
184		       mpi_ptr_t s2_ptr, mpi_size_t s2_size);
185
186/*-- mpihelp-cmp.c --*/
187int mpihelp_cmp(mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size);
188
189/*-- mpihelp-mul.c --*/
190
191struct karatsuba_ctx {
192	struct karatsuba_ctx *next;
193	mpi_ptr_t tspace;
194	mpi_size_t tspace_size;
195	mpi_ptr_t tp;
196	mpi_size_t tp_size;
197};
198
199void mpihelp_release_karatsuba_ctx(struct karatsuba_ctx *ctx);
200
201mpi_limb_t mpihelp_addmul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
202			    mpi_size_t s1_size, mpi_limb_t s2_limb);
203mpi_limb_t mpihelp_submul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
204			    mpi_size_t s1_size, mpi_limb_t s2_limb);
205int mpihelp_mul_n(mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp, mpi_size_t size);
206int mpihelp_mul(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
207		mpi_ptr_t vp, mpi_size_t vsize, mpi_limb_t *_result);
208void mpih_sqr_n_basecase(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size);
209void mpih_sqr_n(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size,
210		mpi_ptr_t tspace);
 
 
211
212int mpihelp_mul_karatsuba_case(mpi_ptr_t prodp,
213			       mpi_ptr_t up, mpi_size_t usize,
214			       mpi_ptr_t vp, mpi_size_t vsize,
215			       struct karatsuba_ctx *ctx);
216
217/*-- mpihelp-mul_1.c (or xxx/cpu/ *.S) --*/
218mpi_limb_t mpihelp_mul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
219			 mpi_size_t s1_size, mpi_limb_t s2_limb);
220
221/*-- mpihelp-div.c --*/
222mpi_limb_t mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
223			 mpi_limb_t divisor_limb);
224mpi_limb_t mpihelp_divrem(mpi_ptr_t qp, mpi_size_t qextra_limbs,
225			  mpi_ptr_t np, mpi_size_t nsize,
226			  mpi_ptr_t dp, mpi_size_t dsize);
227mpi_limb_t mpihelp_divmod_1(mpi_ptr_t quot_ptr,
228			    mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
229			    mpi_limb_t divisor_limb);
230
231/*-- mpihelp-shift.c --*/
232mpi_limb_t mpihelp_lshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
233			  unsigned cnt);
234mpi_limb_t mpihelp_rshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
235			  unsigned cnt);
236
237/* Define stuff for longlong.h.  */
238#define W_TYPE_SIZE BITS_PER_MPI_LIMB
239typedef mpi_limb_t UWtype;
240typedef unsigned int UHWtype;
241#if defined(__GNUC__)
242typedef unsigned int UQItype __attribute__ ((mode(QI)));
243typedef int SItype __attribute__ ((mode(SI)));
244typedef unsigned int USItype __attribute__ ((mode(SI)));
245typedef int DItype __attribute__ ((mode(DI)));
246typedef unsigned int UDItype __attribute__ ((mode(DI)));
247#else
248typedef unsigned char UQItype;
249typedef long SItype;
250typedef unsigned long USItype;
251#endif
252
253#ifdef __GNUC__
254#include "mpi-inline.h"
255#endif
256
257#endif /*G10_MPI_INTERNAL_H */
v5.14.15
  1/* SPDX-License-Identifier: GPL-2.0-or-later */
  2/* mpi-internal.h  -  Internal to the Multi Precision Integers
  3 *	Copyright (C) 1994, 1996 Free Software Foundation, Inc.
  4 *	Copyright (C) 1998, 2000 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
 17#ifndef G10_MPI_INTERNAL_H
 18#define G10_MPI_INTERNAL_H
 19
 20#include <linux/module.h>
 21#include <linux/kernel.h>
 22#include <linux/slab.h>
 23#include <linux/string.h>
 24#include <linux/mpi.h>
 25#include <linux/errno.h>
 26
 27#define log_debug printk
 28#define log_bug printk
 29
 30#define assert(x) \
 31	do { \
 32		if (!x) \
 33			log_bug("failed assertion\n"); \
 34	} while (0);
 35
 36/* If KARATSUBA_THRESHOLD is not already defined, define it to a
 37 * value which is good on most machines.  */
 38
 39/* tested 4, 16, 32 and 64, where 16 gave the best performance when
 40 * checking a 768 and a 1024 bit ElGamal signature.
 41 * (wk 22.12.97) */
 42#ifndef KARATSUBA_THRESHOLD
 43#define KARATSUBA_THRESHOLD 16
 44#endif
 45
 46/* The code can't handle KARATSUBA_THRESHOLD smaller than 2.  */
 47#if KARATSUBA_THRESHOLD < 2
 48#undef KARATSUBA_THRESHOLD
 49#define KARATSUBA_THRESHOLD 2
 50#endif
 51
 52typedef mpi_limb_t *mpi_ptr_t;	/* pointer to a limb */
 53typedef int mpi_size_t;		/* (must be a signed type) */
 54
 55#define RESIZE_IF_NEEDED(a, b)			\
 56	do {					\
 57		if ((a)->alloced < (b))		\
 58			mpi_resize((a), (b));	\
 59	} while (0)
 
 60
 61/* Copy N limbs from S to D.  */
 62#define MPN_COPY(d, s, n) \
 63	do {					\
 64		mpi_size_t _i;			\
 65		for (_i = 0; _i < (n); _i++)	\
 66			(d)[_i] = (s)[_i];	\
 67	} while (0)
 68
 69#define MPN_COPY_INCR(d, s, n)		\
 70	do {					\
 71		mpi_size_t _i;			\
 72		for (_i = 0; _i < (n); _i++)	\
 73			(d)[_i] = (s)[_i];	\
 74	} while (0)
 75
 76
 77#define MPN_COPY_DECR(d, s, n) \
 78	do {					\
 79		mpi_size_t _i;			\
 80		for (_i = (n)-1; _i >= 0; _i--) \
 81			(d)[_i] = (s)[_i];	\
 82	} while (0)
 83
 84/* Zero N limbs at D */
 85#define MPN_ZERO(d, n) \
 86	do {					\
 87		int  _i;			\
 88		for (_i = 0; _i < (n); _i++)	\
 89			(d)[_i] = 0;		\
 90	} while (0)
 91
 92#define MPN_NORMALIZE(d, n)  \
 93	do {					\
 94		while ((n) > 0) {		\
 95			if ((d)[(n)-1])		\
 96				break;		\
 97			(n)--;			\
 98		}				\
 99	} while (0)
100
 
 
 
 
 
 
 
 
 
101#define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
102	do {							\
103		if ((size) < KARATSUBA_THRESHOLD)		\
104			mul_n_basecase(prodp, up, vp, size);	\
105		else						\
106			mul_n(prodp, up, vp, size, tspace);	\
107	} while (0);
108
109/* Divide the two-limb number in (NH,,NL) by D, with DI being the largest
110 * limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB).
111 * If this would yield overflow, DI should be the largest possible number
112 * (i.e., only ones).  For correct operation, the most significant bit of D
113 * has to be set.  Put the quotient in Q and the remainder in R.
114 */
115#define UDIV_QRNND_PREINV(q, r, nh, nl, d, di)				\
116	do {								\
117		mpi_limb_t _ql __maybe_unused;				\
118		mpi_limb_t _q, _r;					\
119		mpi_limb_t _xh, _xl;					\
120		umul_ppmm(_q, _ql, (nh), (di));				\
121		_q += (nh);	/* DI is 2**BITS_PER_MPI_LIMB too small */ \
122		umul_ppmm(_xh, _xl, _q, (d));				\
123		sub_ddmmss(_xh, _r, (nh), (nl), _xh, _xl);		\
124		if (_xh) {						\
125			sub_ddmmss(_xh, _r, _xh, _r, 0, (d));		\
126			_q++;						\
127			if (_xh) {					\
128				sub_ddmmss(_xh, _r, _xh, _r, 0, (d));	\
129				_q++;					\
130			}						\
131		}							\
132		if (_r >= (d)) {					\
133			_r -= (d);					\
134			_q++;						\
135		}							\
136		(r) = _r;						\
137		(q) = _q;						\
138	} while (0)
139
140
141/*-- mpiutil.c --*/
142mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs);
143void mpi_free_limb_space(mpi_ptr_t a);
144void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs);
145
 
 
 
 
 
146static inline mpi_limb_t mpihelp_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
147			 mpi_size_t s1_size, mpi_limb_t s2_limb);
148mpi_limb_t mpihelp_add_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
149			 mpi_ptr_t s2_ptr, mpi_size_t size);
150static inline mpi_limb_t mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
151		       mpi_ptr_t s2_ptr, mpi_size_t s2_size);
152
 
153static inline mpi_limb_t mpihelp_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
154			 mpi_size_t s1_size, mpi_limb_t s2_limb);
155mpi_limb_t mpihelp_sub_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
156			 mpi_ptr_t s2_ptr, mpi_size_t size);
157static inline mpi_limb_t mpihelp_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
158		       mpi_ptr_t s2_ptr, mpi_size_t s2_size);
159
160/*-- mpih-cmp.c --*/
161int mpihelp_cmp(mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size);
162
163/*-- mpih-mul.c --*/
164
165struct karatsuba_ctx {
166	struct karatsuba_ctx *next;
167	mpi_ptr_t tspace;
168	mpi_size_t tspace_size;
169	mpi_ptr_t tp;
170	mpi_size_t tp_size;
171};
172
173void mpihelp_release_karatsuba_ctx(struct karatsuba_ctx *ctx);
174
175mpi_limb_t mpihelp_addmul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
176			    mpi_size_t s1_size, mpi_limb_t s2_limb);
177mpi_limb_t mpihelp_submul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
178			    mpi_size_t s1_size, mpi_limb_t s2_limb);
 
179int mpihelp_mul(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
180		mpi_ptr_t vp, mpi_size_t vsize, mpi_limb_t *_result);
181void mpih_sqr_n_basecase(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size);
182void mpih_sqr_n(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size,
183		mpi_ptr_t tspace);
184void mpihelp_mul_n(mpi_ptr_t prodp,
185		mpi_ptr_t up, mpi_ptr_t vp, mpi_size_t size);
186
187int mpihelp_mul_karatsuba_case(mpi_ptr_t prodp,
188			       mpi_ptr_t up, mpi_size_t usize,
189			       mpi_ptr_t vp, mpi_size_t vsize,
190			       struct karatsuba_ctx *ctx);
191
192/*-- generic_mpih-mul1.c --*/
193mpi_limb_t mpihelp_mul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
194			 mpi_size_t s1_size, mpi_limb_t s2_limb);
195
196/*-- mpih-div.c --*/
197mpi_limb_t mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
198			 mpi_limb_t divisor_limb);
199mpi_limb_t mpihelp_divrem(mpi_ptr_t qp, mpi_size_t qextra_limbs,
200			  mpi_ptr_t np, mpi_size_t nsize,
201			  mpi_ptr_t dp, mpi_size_t dsize);
202mpi_limb_t mpihelp_divmod_1(mpi_ptr_t quot_ptr,
203			    mpi_ptr_t dividend_ptr, mpi_size_t dividend_size,
204			    mpi_limb_t divisor_limb);
205
206/*-- generic_mpih-[lr]shift.c --*/
207mpi_limb_t mpihelp_lshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
208			  unsigned cnt);
209mpi_limb_t mpihelp_rshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
210			  unsigned cnt);
211
212/* Define stuff for longlong.h.  */
213#define W_TYPE_SIZE BITS_PER_MPI_LIMB
214typedef mpi_limb_t UWtype;
215typedef unsigned int UHWtype;
216#if defined(__GNUC__)
217typedef unsigned int UQItype __attribute__ ((mode(QI)));
218typedef int SItype __attribute__ ((mode(SI)));
219typedef unsigned int USItype __attribute__ ((mode(SI)));
220typedef int DItype __attribute__ ((mode(DI)));
221typedef unsigned int UDItype __attribute__ ((mode(DI)));
222#else
223typedef unsigned char UQItype;
224typedef long SItype;
225typedef unsigned long USItype;
226#endif
227
228#ifdef __GNUC__
229#include "mpi-inline.h"
230#endif
231
232#endif /*G10_MPI_INTERNAL_H */