Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v3.5 261 lines 8.1 kB view raw
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 68#define ABS(x) (x >= 0 ? x : -x) 69#define MIN(l, o) ((l) < (o) ? (l) : (o)) 70#define MAX(h, i) ((h) > (i) ? (h) : (i)) 71 72static inline int RESIZE_IF_NEEDED(MPI a, unsigned b) 73{ 74 if (a->alloced < b) 75 return mpi_resize(a, b); 76 return 0; 77} 78 79/* Copy N limbs from S to D. */ 80#define MPN_COPY(d, s, n) \ 81 do { \ 82 mpi_size_t _i; \ 83 for (_i = 0; _i < (n); _i++) \ 84 (d)[_i] = (s)[_i]; \ 85 } while (0) 86 87#define MPN_COPY_INCR(d, s, n) \ 88 do { \ 89 mpi_size_t _i; \ 90 for (_i = 0; _i < (n); _i++) \ 91 (d)[_i] = (d)[_i]; \ 92 } while (0) 93 94#define MPN_COPY_DECR(d, s, n) \ 95 do { \ 96 mpi_size_t _i; \ 97 for (_i = (n)-1; _i >= 0; _i--) \ 98 (d)[_i] = (s)[_i]; \ 99 } while (0) 100 101/* Zero N limbs at D */ 102#define MPN_ZERO(d, n) \ 103 do { \ 104 int _i; \ 105 for (_i = 0; _i < (n); _i++) \ 106 (d)[_i] = 0; \ 107 } while (0) 108 109#define MPN_NORMALIZE(d, n) \ 110 do { \ 111 while ((n) > 0) { \ 112 if ((d)[(n)-1]) \ 113 break; \ 114 (n)--; \ 115 } \ 116 } while (0) 117 118#define MPN_NORMALIZE_NOT_ZERO(d, n) \ 119 do { \ 120 for (;;) { \ 121 if ((d)[(n)-1]) \ 122 break; \ 123 (n)--; \ 124 } \ 125 } while (0) 126 127#define MPN_MUL_N_RECURSE(prodp, up, vp, size, tspace) \ 128 do { \ 129 if ((size) < KARATSUBA_THRESHOLD) \ 130 mul_n_basecase(prodp, up, vp, size); \ 131 else \ 132 mul_n(prodp, up, vp, size, tspace); \ 133 } while (0); 134 135/* Divide the two-limb number in (NH,,NL) by D, with DI being the largest 136 * limb not larger than (2**(2*BITS_PER_MP_LIMB))/D - (2**BITS_PER_MP_LIMB). 137 * If this would yield overflow, DI should be the largest possible number 138 * (i.e., only ones). For correct operation, the most significant bit of D 139 * has to be set. Put the quotient in Q and the remainder in R. 140 */ 141#define UDIV_QRNND_PREINV(q, r, nh, nl, d, di) \ 142 do { \ 143 mpi_limb_t _q, _ql, _r; \ 144 mpi_limb_t _xh, _xl; \ 145 umul_ppmm(_q, _ql, (nh), (di)); \ 146 _q += (nh); /* DI is 2**BITS_PER_MPI_LIMB too small */ \ 147 umul_ppmm(_xh, _xl, _q, (d)); \ 148 sub_ddmmss(_xh, _r, (nh), (nl), _xh, _xl); \ 149 if (_xh) { \ 150 sub_ddmmss(_xh, _r, _xh, _r, 0, (d)); \ 151 _q++; \ 152 if (_xh) { \ 153 sub_ddmmss(_xh, _r, _xh, _r, 0, (d)); \ 154 _q++; \ 155 } \ 156 } \ 157 if (_r >= (d)) { \ 158 _r -= (d); \ 159 _q++; \ 160 } \ 161 (r) = _r; \ 162 (q) = _q; \ 163 } while (0) 164 165/*-- mpiutil.c --*/ 166mpi_ptr_t mpi_alloc_limb_space(unsigned nlimbs); 167void mpi_free_limb_space(mpi_ptr_t a); 168void mpi_assign_limb_space(MPI a, mpi_ptr_t ap, unsigned nlimbs); 169 170/*-- mpi-bit.c --*/ 171void mpi_rshift_limbs(MPI a, unsigned int count); 172int mpi_lshift_limbs(MPI a, unsigned int count); 173 174/*-- mpihelp-add.c --*/ 175mpi_limb_t mpihelp_add_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_add_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, 178 mpi_ptr_t s2_ptr, mpi_size_t size); 179mpi_limb_t mpihelp_add(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size, 180 mpi_ptr_t s2_ptr, mpi_size_t s2_size); 181 182/*-- mpihelp-sub.c --*/ 183mpi_limb_t mpihelp_sub_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, 184 mpi_size_t s1_size, mpi_limb_t s2_limb); 185mpi_limb_t mpihelp_sub_n(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, 186 mpi_ptr_t s2_ptr, mpi_size_t size); 187mpi_limb_t mpihelp_sub(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, mpi_size_t s1_size, 188 mpi_ptr_t s2_ptr, mpi_size_t s2_size); 189 190/*-- mpihelp-cmp.c --*/ 191int mpihelp_cmp(mpi_ptr_t op1_ptr, mpi_ptr_t op2_ptr, mpi_size_t size); 192 193/*-- mpihelp-mul.c --*/ 194 195struct karatsuba_ctx { 196 struct karatsuba_ctx *next; 197 mpi_ptr_t tspace; 198 mpi_size_t tspace_size; 199 mpi_ptr_t tp; 200 mpi_size_t tp_size; 201}; 202 203void mpihelp_release_karatsuba_ctx(struct karatsuba_ctx *ctx); 204 205mpi_limb_t mpihelp_addmul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, 206 mpi_size_t s1_size, mpi_limb_t s2_limb); 207mpi_limb_t mpihelp_submul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, 208 mpi_size_t s1_size, mpi_limb_t s2_limb); 209int mpihelp_mul_n(mpi_ptr_t prodp, mpi_ptr_t up, mpi_ptr_t vp, mpi_size_t size); 210int mpihelp_mul(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t usize, 211 mpi_ptr_t vp, mpi_size_t vsize, mpi_limb_t *_result); 212void mpih_sqr_n_basecase(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size); 213void mpih_sqr_n(mpi_ptr_t prodp, mpi_ptr_t up, mpi_size_t size, 214 mpi_ptr_t tspace); 215 216int mpihelp_mul_karatsuba_case(mpi_ptr_t prodp, 217 mpi_ptr_t up, mpi_size_t usize, 218 mpi_ptr_t vp, mpi_size_t vsize, 219 struct karatsuba_ctx *ctx); 220 221/*-- mpihelp-mul_1.c (or xxx/cpu/ *.S) --*/ 222mpi_limb_t mpihelp_mul_1(mpi_ptr_t res_ptr, mpi_ptr_t s1_ptr, 223 mpi_size_t s1_size, mpi_limb_t s2_limb); 224 225/*-- mpihelp-div.c --*/ 226mpi_limb_t mpihelp_mod_1(mpi_ptr_t dividend_ptr, mpi_size_t dividend_size, 227 mpi_limb_t divisor_limb); 228mpi_limb_t mpihelp_divrem(mpi_ptr_t qp, mpi_size_t qextra_limbs, 229 mpi_ptr_t np, mpi_size_t nsize, 230 mpi_ptr_t dp, mpi_size_t dsize); 231mpi_limb_t mpihelp_divmod_1(mpi_ptr_t quot_ptr, 232 mpi_ptr_t dividend_ptr, mpi_size_t dividend_size, 233 mpi_limb_t divisor_limb); 234 235/*-- mpihelp-shift.c --*/ 236mpi_limb_t mpihelp_lshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize, 237 unsigned cnt); 238mpi_limb_t mpihelp_rshift(mpi_ptr_t wp, mpi_ptr_t up, mpi_size_t usize, 239 unsigned cnt); 240 241/* Define stuff for longlong.h. */ 242#define W_TYPE_SIZE BITS_PER_MPI_LIMB 243typedef mpi_limb_t UWtype; 244typedef unsigned int UHWtype; 245#if defined(__GNUC__) 246typedef unsigned int UQItype __attribute__ ((mode(QI))); 247typedef int SItype __attribute__ ((mode(SI))); 248typedef unsigned int USItype __attribute__ ((mode(SI))); 249typedef int DItype __attribute__ ((mode(DI))); 250typedef unsigned int UDItype __attribute__ ((mode(DI))); 251#else 252typedef unsigned char UQItype; 253typedef long SItype; 254typedef unsigned long USItype; 255#endif 256 257#ifdef __GNUC__ 258#include "mpi-inline.h" 259#endif 260 261#endif /*G10_MPI_INTERNAL_H */