blob: c2d6f4efcfbc662a3788a51c631e0edabae716af [file] [log] [blame]
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +03001/* 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
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +030068/* 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
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +030076#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
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +0300100#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
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +0300108/*-- mpiutil.c --*/
109mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs);
110void mpi_free_limb_space(mpi_ptr_t a);
111void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs);
112
Arnd Bergmann9c6bd0c2016-02-26 13:46:27 +0100113static inline mpi_limb_t mpihelp_add_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +0300114 mpi_size_t s1_size, mpi_limb_t s2_limb);
115mpi_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);
Arnd Bergmann9c6bd0c2016-02-26 13:46:27 +0100117static inline mpi_limb_t mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +0300118 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
119
Arnd Bergmann9c6bd0c2016-02-26 13:46:27 +0100120static inline mpi_limb_t mpihelp_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr,
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +0300121 mpi_size_t s1_size, mpi_limb_t s2_limb);
122mpi_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);
Arnd Bergmann9c6bd0c2016-02-26 13:46:27 +0100124static inline mpi_limb_t mpihelp_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size,
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +0300125 mpi_ptr_t s2_ptr, mpi_size_t s2_size);
126
Vasily Averincbdc61a2018-06-07 17:10:51 -0700127/*-- mpih-cmp.c --*/
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +0300128int mpihelp_cmp(mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size);
129
Vasily Averincbdc61a2018-06-07 17:10:51 -0700130/*-- mpih-mul.c --*/
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +0300131
132struct 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
140void mpihelp_release_karatsuba_ctx(struct karatsuba_ctx *ctx);
141
142mpi_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);
144mpi_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);
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +0300146int 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);
148void mpih_sqr_n_basecase(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size);
149void mpih_sqr_n(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size,
150 mpi_ptr_t tspace);
151
152int 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
Vasily Averincbdc61a2018-06-07 17:10:51 -0700157/*-- generic_mpih-mul1.c --*/
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +0300158mpi_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
Vasily Averincbdc61a2018-06-07 17:10:51 -0700161/*-- mpih-div.c --*/
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +0300162mpi_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);
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +0300165
Vasily Averincbdc61a2018-06-07 17:10:51 -0700166/*-- generic_mpih-[lr]shift.c --*/
Dmitry Kasatkin5ce3e312011-08-31 14:05:16 +0300167mpi_limb_t mpihelp_lshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize,
168 unsigned cnt);
169mpi_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
174typedef mpi_limb_t UWtype;
175typedef unsigned int UHWtype;
176#if defined(__GNUC__)
177typedef unsigned int UQItype __attribute__ ((mode(QI)));
178typedef int SItype __attribute__ ((mode(SI)));
179typedef unsigned int USItype __attribute__ ((mode(SI)));
180typedef int DItype __attribute__ ((mode(DI)));
181typedef unsigned int UDItype __attribute__ ((mode(DI)));
182#else
183typedef unsigned char UQItype;
184typedef long SItype;
185typedef unsigned long USItype;
186#endif
187
188#ifdef __GNUC__
189#include "mpi-inline.h"
190#endif
191
192#endif /*G10_MPI_INTERNAL_H */