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 
65 typedef mpi_limb_t *mpi_ptr_t;	/* pointer to a limb */
66 typedef int mpi_size_t;		/* (must be a signed type) */
67 
68 /* Copy N limbs from S to D.  */
69 #define MPN_COPY(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 #define MPN_COPY_DECR(d, s, n) \
77 	do {					\
78 		mpi_size_t _i;			\
79 		for (_i = (n)-1; _i >= 0; _i--) \
80 			(d)[_i] = (s)[_i];	\
81 	} while (0)
82 
83 /* Zero N limbs at D */
84 #define MPN_ZERO(d, n) \
85 	do {					\
86 		int  _i;			\
87 		for (_i = 0; _i < (n); _i++)	\
88 			(d)[_i] = 0;		\
89 	} while (0)
90 
91 #define MPN_NORMALIZE(d, n)  \
92 	do {					\
93 		while ((n) > 0) {		\
94 			if ((d)[(n)-1])		\
95 				break;		\
96 			(n)--;			\
97 		}				\
98 	} while (0)
99 
100 #define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \
101 	do {							\
102 		if ((size) < KARATSUBA_THRESHOLD)		\
103 			mul_n_basecase(prodp, up, vp, size);	\
104 		else						\
105 			mul_n(prodp, up, vp, size, tspace);	\
106 	} while (0);
107 
108 /*-- mpiutil.c --*/
109 mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs);
110 void mpi_free_limb_space(mpi_ptr_t a);
111 void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs);
112 
113 static inline mpi_limb_t mpihelp_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
114 			 mpi_size_t s1_size, mpi_limb_t s2_limb);
115 mpi_limb_t mpihelp_add_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
116 			 mpi_ptr_t s2_ptr, mpi_size_t size);
117 static inline mpi_limb_t mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
118 		       mpi_ptr_t s2_ptr, mpi_size_t s2_size);
119 
120 static inline mpi_limb_t mpihelp_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
121 			 mpi_size_t s1_size, mpi_limb_t s2_limb);
122 mpi_limb_t mpihelp_sub_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
123 			 mpi_ptr_t s2_ptr, mpi_size_t size);
124 static inline mpi_limb_t mpihelp_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
125 		       mpi_ptr_t s2_ptr, mpi_size_t s2_size);
126 
127 /*-- mpih-cmp.c --*/
128 int mpihelp_cmp(mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size);
129 
130 /*-- mpih-mul.c --*/
131 
132 struct karatsuba_ctx {
133 	struct karatsuba_ctx *next;
134 	mpi_ptr_t tspace;
135 	mpi_size_t tspace_size;
136 	mpi_ptr_t tp;
137 	mpi_size_t tp_size;
138 };
139 
140 void mpihelp_release_karatsuba_ctx(struct karatsuba_ctx *ctx);
141 
142 mpi_limb_t mpihelp_addmul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
143 			    mpi_size_t s1_size, mpi_limb_t s2_limb);
144 mpi_limb_t mpihelp_submul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
145 			    mpi_size_t s1_size, mpi_limb_t s2_limb);
146 int mpihelp_mul(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize,
147 		mpi_ptr_t vp, mpi_size_t vsize, mpi_limb_t *_result);
148 void mpih_sqr_n_basecase(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size);
149 void mpih_sqr_n(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size,
150 		mpi_ptr_t tspace);
151 
152 int mpihelp_mul_karatsuba_case(mpi_ptr_t prodp,
153 			       mpi_ptr_t up, mpi_size_t usize,
154 			       mpi_ptr_t vp, mpi_size_t vsize,
155 			       struct karatsuba_ctx *ctx);
156 
157 /*-- generic_mpih-mul1.c --*/
158 mpi_limb_t mpihelp_mul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
159 			 mpi_size_t s1_size, mpi_limb_t s2_limb);
160 
161 /*-- mpih-div.c --*/
162 mpi_limb_t mpihelp_divrem(mpi_ptr_t qp, mpi_size_t qextra_limbs,
163 			  mpi_ptr_t np, mpi_size_t nsize,
164 			  mpi_ptr_t dp, mpi_size_t dsize);
165 
166 /*-- generic_mpih-[lr]shift.c --*/
167 mpi_limb_t mpihelp_lshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
168 			  unsigned cnt);
169 mpi_limb_t mpihelp_rshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
170 			  unsigned cnt);
171 
172 /* Define stuff for longlong.h.  */
173 #define W_TYPE_SIZE BITS_PER_MPI_LIMB
174 typedef mpi_limb_t UWtype;
175 typedef unsigned int UHWtype;
176 #if defined(__GNUC__)
177 typedef unsigned int UQItype __attribute__ ((mode(QI)));
178 typedef int SItype __attribute__ ((mode(SI)));
179 typedef unsigned int USItype __attribute__ ((mode(SI)));
180 typedef int DItype __attribute__ ((mode(DI)));
181 typedef unsigned int UDItype __attribute__ ((mode(DI)));
182 #else
183 typedef unsigned char UQItype;
184 typedef long SItype;
185 typedef unsigned long USItype;
186 #endif
187 
188 #ifdef __GNUC__
189 #include "mpi-inline.h"
190 #endif
191 
192 #endif /*G10_MPI_INTERNAL_H */
193