| Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 1 | /* mpi.h  -  Multi Precision Integers | 
 | 2 |  *	Copyright (C) 1994, 1996, 1998, 1999, | 
 | 3 |  *                    2000, 2001 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_H | 
 | 31 | #define G10_MPI_H | 
 | 32 |  | 
 | 33 | #include <linux/types.h> | 
 | 34 |  | 
 | 35 | /* DSI defines */ | 
 | 36 |  | 
 | 37 | #define SHA1_DIGEST_LENGTH   20 | 
 | 38 |  | 
 | 39 | /*end of DSI defines */ | 
 | 40 |  | 
 | 41 | #define BYTES_PER_MPI_LIMB	(BITS_PER_LONG / 8) | 
 | 42 | #define BITS_PER_MPI_LIMB	BITS_PER_LONG | 
 | 43 |  | 
 | 44 | typedef unsigned long int mpi_limb_t; | 
 | 45 | typedef signed long int mpi_limb_signed_t; | 
 | 46 |  | 
 | 47 | struct gcry_mpi { | 
 | 48 | 	int alloced;		/* array size (# of allocated limbs) */ | 
 | 49 | 	int nlimbs;		/* number of valid limbs */ | 
 | 50 | 	int nbits;		/* the real number of valid bits (info only) */ | 
 | 51 | 	int sign;		/* indicates a negative number */ | 
 | 52 | 	unsigned flags;		/* bit 0: array must be allocated in secure memory space */ | 
 | 53 | 	/* bit 1: not used */ | 
 | 54 | 	/* bit 2: the limb is a pointer to some m_alloced data */ | 
 | 55 | 	mpi_limb_t *d;		/* array with the limbs */ | 
 | 56 | }; | 
 | 57 |  | 
 | 58 | typedef struct gcry_mpi *MPI; | 
 | 59 |  | 
| Dmitry Kasatkin | 5ce3e31 | 2011-08-31 14:05:16 +0300 | [diff] [blame] | 60 | #define mpi_get_nlimbs(a)     ((a)->nlimbs) | 
 | 61 | #define mpi_is_neg(a)	      ((a)->sign) | 
 | 62 |  | 
 | 63 | /*-- mpiutil.c --*/ | 
 | 64 | MPI mpi_alloc(unsigned nlimbs); | 
 | 65 | MPI mpi_alloc_secure(unsigned nlimbs); | 
 | 66 | MPI mpi_alloc_like(MPI a); | 
 | 67 | void mpi_free(MPI a); | 
 | 68 | int mpi_resize(MPI a, unsigned nlimbs); | 
 | 69 | int mpi_copy(MPI *copy, const MPI a); | 
 | 70 | void mpi_clear(MPI a); | 
 | 71 | int mpi_set(MPI w, MPI u); | 
 | 72 | int mpi_set_ui(MPI w, ulong u); | 
 | 73 | MPI mpi_alloc_set_ui(unsigned long u); | 
 | 74 | void mpi_m_check(MPI a); | 
 | 75 | void mpi_swap(MPI a, MPI b); | 
 | 76 |  | 
 | 77 | /*-- mpicoder.c --*/ | 
 | 78 | MPI do_encode_md(const void *sha_buffer, unsigned nbits); | 
 | 79 | MPI mpi_read_from_buffer(const void *buffer, unsigned *ret_nread); | 
 | 80 | int mpi_fromstr(MPI val, const char *str); | 
 | 81 | u32 mpi_get_keyid(MPI a, u32 *keyid); | 
 | 82 | void *mpi_get_buffer(MPI a, unsigned *nbytes, int *sign); | 
 | 83 | void *mpi_get_secure_buffer(MPI a, unsigned *nbytes, int *sign); | 
 | 84 | int mpi_set_buffer(MPI a, const void *buffer, unsigned nbytes, int sign); | 
 | 85 |  | 
 | 86 | #define log_mpidump g10_log_mpidump | 
 | 87 |  | 
 | 88 | /*-- mpi-add.c --*/ | 
 | 89 | int mpi_add_ui(MPI w, MPI u, ulong v); | 
 | 90 | int mpi_add(MPI w, MPI u, MPI v); | 
 | 91 | int mpi_addm(MPI w, MPI u, MPI v, MPI m); | 
 | 92 | int mpi_sub_ui(MPI w, MPI u, ulong v); | 
 | 93 | int mpi_sub(MPI w, MPI u, MPI v); | 
 | 94 | int mpi_subm(MPI w, MPI u, MPI v, MPI m); | 
 | 95 |  | 
 | 96 | /*-- mpi-mul.c --*/ | 
 | 97 | int mpi_mul_ui(MPI w, MPI u, ulong v); | 
 | 98 | int mpi_mul_2exp(MPI w, MPI u, ulong cnt); | 
 | 99 | int mpi_mul(MPI w, MPI u, MPI v); | 
 | 100 | int mpi_mulm(MPI w, MPI u, MPI v, MPI m); | 
 | 101 |  | 
 | 102 | /*-- mpi-div.c --*/ | 
 | 103 | ulong mpi_fdiv_r_ui(MPI rem, MPI dividend, ulong divisor); | 
 | 104 | int mpi_fdiv_r(MPI rem, MPI dividend, MPI divisor); | 
 | 105 | int mpi_fdiv_q(MPI quot, MPI dividend, MPI divisor); | 
 | 106 | int mpi_fdiv_qr(MPI quot, MPI rem, MPI dividend, MPI divisor); | 
 | 107 | int mpi_tdiv_r(MPI rem, MPI num, MPI den); | 
 | 108 | int mpi_tdiv_qr(MPI quot, MPI rem, MPI num, MPI den); | 
 | 109 | int mpi_tdiv_q_2exp(MPI w, MPI u, unsigned count); | 
 | 110 | int mpi_divisible_ui(const MPI dividend, ulong divisor); | 
 | 111 |  | 
 | 112 | /*-- mpi-gcd.c --*/ | 
 | 113 | int mpi_gcd(MPI g, const MPI a, const MPI b); | 
 | 114 |  | 
 | 115 | /*-- mpi-pow.c --*/ | 
 | 116 | int mpi_pow(MPI w, MPI u, MPI v); | 
 | 117 | int mpi_powm(MPI res, MPI base, MPI exp, MPI mod); | 
 | 118 |  | 
 | 119 | /*-- mpi-mpow.c --*/ | 
 | 120 | int mpi_mulpowm(MPI res, MPI *basearray, MPI *exparray, MPI mod); | 
 | 121 |  | 
 | 122 | /*-- mpi-cmp.c --*/ | 
 | 123 | int mpi_cmp_ui(MPI u, ulong v); | 
 | 124 | int mpi_cmp(MPI u, MPI v); | 
 | 125 |  | 
 | 126 | /*-- mpi-scan.c --*/ | 
 | 127 | int mpi_getbyte(MPI a, unsigned idx); | 
 | 128 | void mpi_putbyte(MPI a, unsigned idx, int value); | 
 | 129 | unsigned mpi_trailing_zeros(MPI a); | 
 | 130 |  | 
 | 131 | /*-- mpi-bit.c --*/ | 
 | 132 | void mpi_normalize(MPI a); | 
 | 133 | unsigned mpi_get_nbits(MPI a); | 
 | 134 | int mpi_test_bit(MPI a, unsigned n); | 
 | 135 | int mpi_set_bit(MPI a, unsigned n); | 
 | 136 | int mpi_set_highbit(MPI a, unsigned n); | 
 | 137 | void mpi_clear_highbit(MPI a, unsigned n); | 
 | 138 | void mpi_clear_bit(MPI a, unsigned n); | 
 | 139 | int mpi_rshift(MPI x, MPI a, unsigned n); | 
 | 140 |  | 
 | 141 | /*-- mpi-inv.c --*/ | 
 | 142 | int mpi_invm(MPI x, MPI u, MPI v); | 
 | 143 |  | 
 | 144 | #endif /*G10_MPI_H */ |