raw
mpi-genesis             1 /* mpi.h  -  Multi Precision Integers
mpi-genesis 2 * Copyright (C) 1994, 1996, 1998, 1999,
mpi-genesis 3 * 2000, 2001 Free Software Foundation, Inc.
mpi-genesis 4 *
mpi-genesis 5 * This file is part of GNUPG.
mpi-genesis 6 *
mpi-genesis 7 * GNUPG is free software; you can redistribute it and/or modify
mpi-genesis 8 * it under the terms of the GNU General Public License as published by
mpi-genesis 9 * the Free Software Foundation; either version 3 of the License, or
mpi-genesis 10 * (at your option) any later version.
mpi-genesis 11 *
mpi-genesis 12 * GNUPG is distributed in the hope that it will be useful,
mpi-genesis 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mpi-genesis 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
mpi-genesis 15 * GNU General Public License for more details.
mpi-genesis 16 *
mpi-genesis 17 * You should have received a copy of the GNU General Public License
mpi-genesis 18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
mpi-genesis 19 *
mpi-genesis 20 * Note: This code is heavily based on the GNU MP Library.
mpi-genesis 21 * Actually it's the same code with only minor changes in the
mpi-genesis 22 * way the data is stored; this is to support the abstraction
mpi-genesis 23 * of an optional secure memory allocation which may be used
mpi-genesis 24 * to avoid revealing of sensitive data due to paging etc.
mpi-genesis 25 * The GNU MP Library itself is published under the LGPL;
mpi-genesis 26 * however I decided to publish this code under the plain GPL.
mpi-genesis 27 */
mpi-genesis 28
mpi-genesis 29 #ifndef G10_MPI_H
mpi-genesis 30 #define G10_MPI_H
mpi-genesis 31
mpi-genesis 32 #include <config.h>
mpi-genesis 33 #include <stdio.h>
mpi-genesis 34 #include "iobuf.h"
mpi-genesis 35 #include "types.h"
mpi-genesis 36 #include "memory.h"
mpi-genesis 37
mpi-genesis 38 #ifndef EXTERN_UNLESS_MAIN_MODULE
mpi-genesis 39 #if defined (__riscos__) && !defined (INCLUDED_BY_MAIN_MODULE)
mpi-genesis 40 #define EXTERN_UNLESS_MAIN_MODULE extern
mpi-genesis 41 #else
mpi-genesis 42 #define EXTERN_UNLESS_MAIN_MODULE
mpi-genesis 43 #endif
mpi-genesis 44 #endif
mpi-genesis 45
mpi-genesis 46 #define DBG_MPI mpi_debug_mode
mpi-genesis 47 EXTERN_UNLESS_MAIN_MODULE int mpi_debug_mode;
mpi-genesis 48
mpi-genesis 49
mpi-genesis 50 struct gcry_mpi;
mpi-genesis 51 typedef struct gcry_mpi *MPI;
mpi-genesis 52
mpi-genesis 53
mpi-genesis 54 /*-- mpiutil.c --*/
mpi-genesis 55
mpi-genesis 56 #ifdef M_DEBUG
mpi-genesis 57 #define mpi_alloc(n) mpi_debug_alloc((n), M_DBGINFO( __LINE__ ) )
mpi-genesis 58 #define mpi_alloc_secure(n) mpi_debug_alloc_secure((n), M_DBGINFO( __LINE__ ) )
mpi-genesis 59 #define mpi_alloc_like(n) mpi_debug_alloc_like((n), M_DBGINFO( __LINE__ ) )
mpi-genesis 60 #define mpi_free(a) mpi_debug_free((a), M_DBGINFO(__LINE__) )
mpi-genesis 61 #define mpi_resize(a,b) mpi_debug_resize((a),(b), M_DBGINFO(__LINE__) )
mpi-genesis 62 #define mpi_copy(a) mpi_debug_copy((a), M_DBGINFO(__LINE__) )
mpi-genesis 63 MPI mpi_debug_alloc( unsigned nlimbs, const char *info );
mpi-genesis 64 MPI mpi_debug_alloc_secure( unsigned nlimbs, const char *info );
mpi-genesis 65 MPI mpi_debug_alloc_like( MPI a, const char *info );
mpi-genesis 66 void mpi_debug_free( MPI a, const char *info );
mpi-genesis 67 void mpi_debug_resize( MPI a, unsigned nlimbs, const char *info );
mpi-genesis 68 MPI mpi_debug_copy( MPI a, const char *info );
mpi-genesis 69 #else
mpi-genesis 70 MPI mpi_alloc( unsigned nlimbs );
mpi-genesis 71 MPI mpi_alloc_secure( unsigned nlimbs );
mpi-genesis 72 MPI mpi_alloc_like( MPI a );
mpi-genesis 73 void mpi_free( MPI a );
mpi-genesis 74 void mpi_resize( MPI a, unsigned nlimbs );
mpi-genesis 75 MPI mpi_copy( MPI a );
mpi-genesis 76 #endif
mpi-genesis 77 #define mpi_is_opaque(a) ((a) && (mpi_get_flags (a)&4))
mpi-genesis 78 MPI mpi_set_opaque( MPI a, void *p, unsigned int len );
mpi-genesis 79 void *mpi_get_opaque( MPI a, unsigned int *len );
mpi-genesis 80 #define mpi_is_secure(a) ((a) && (mpi_get_flags (a)&1))
mpi-genesis 81 void mpi_set_secure( MPI a );
mpi-genesis 82 void mpi_clear( MPI a );
mpi-genesis 83 void mpi_set( MPI w, MPI u);
mpi-genesis 84 void mpi_set_ui( MPI w, ulong u);
mpi-genesis 85 MPI mpi_alloc_set_ui( unsigned long u);
mpi-genesis 86 void mpi_m_check( MPI a );
mpi-genesis 87 void mpi_swap( MPI a, MPI b);
mpi-genesis 88 int mpi_get_nlimbs (MPI a);
mpi-genesis 89 int mpi_is_neg (MPI a);
mpi-genesis 90 unsigned int mpi_nlimb_hint_from_nbytes (unsigned int nbytes);
mpi-genesis 91 unsigned int mpi_nlimb_hint_from_nbits (unsigned int nbits);
mpi-genesis 92 unsigned int mpi_get_flags (MPI a);
mpi-genesis 93
mpi-genesis 94 /*-- mpicoder.c --*/
mpi-genesis 95 int mpi_write( IOBUF out, MPI a );
mpi-genesis 96 #ifdef M_DEBUG
mpi-genesis 97 #define mpi_read(a,b,c) mpi_debug_read((a),(b),(c), M_DBGINFO( __LINE__ ) )
mpi-genesis 98 MPI mpi_debug_read(IOBUF inp, unsigned *nread, int secure, const char *info);
mpi-genesis 99 #else
mpi-genesis 100 MPI mpi_read(IOBUF inp, unsigned *nread, int secure);
mpi-genesis 101 #endif
mpi-genesis 102 MPI mpi_read_from_buffer(byte *buffer, unsigned *ret_nread, int secure);
mpi-genesis 103 int mpi_fromstr(MPI val, const char *str);
mpi-genesis 104 int mpi_print( FILE *fp, MPI a, int mode );
mpi-genesis 105 void g10_log_mpidump( const char *text, MPI a );
mpi-genesis 106 u32 mpi_get_keyid( MPI a, u32 *keyid );
mpi-genesis 107 byte *mpi_get_buffer( MPI a, unsigned *nbytes, int *sign );
mpi-genesis 108 byte *mpi_get_secure_buffer( MPI a, unsigned *nbytes, int *sign );
mpi-genesis 109 void mpi_set_buffer( MPI a, const byte *buffer, unsigned nbytes, int sign );
mpi-genesis 110
mpi-genesis 111 #define log_mpidump g10_log_mpidump
mpi-genesis 112
mpi-genesis 113 /*-- mpi-add.c --*/
mpi-genesis 114 void mpi_add_ui(MPI w, MPI u, ulong v );
mpi-genesis 115 void mpi_add(MPI w, MPI u, MPI v);
mpi-genesis 116 void mpi_addm(MPI w, MPI u, MPI v, MPI m);
mpi-genesis 117 void mpi_sub_ui(MPI w, MPI u, ulong v );
mpi-genesis 118 void mpi_sub( MPI w, MPI u, MPI v);
mpi-genesis 119 void mpi_subm( MPI w, MPI u, MPI v, MPI m);
mpi-genesis 120
mpi-genesis 121 /*-- mpi-mul.c --*/
mpi-genesis 122 void mpi_mul_ui(MPI w, MPI u, ulong v );
mpi-genesis 123 void mpi_mul_2exp( MPI w, MPI u, ulong cnt);
mpi-genesis 124 void mpi_mul( MPI w, MPI u, MPI v);
mpi-genesis 125 void mpi_mulm( MPI w, MPI u, MPI v, MPI m);
mpi-genesis 126
mpi-genesis 127 /*-- mpi-div.c --*/
mpi-genesis 128 ulong mpi_fdiv_r_ui( MPI rem, MPI dividend, ulong divisor );
mpi-genesis 129 void mpi_fdiv_r( MPI rem, MPI dividend, MPI divisor );
mpi-genesis 130 void mpi_fdiv_q( MPI quot, MPI dividend, MPI divisor );
mpi-genesis 131 void mpi_fdiv_qr( MPI quot, MPI rem, MPI dividend, MPI divisor );
mpi-genesis 132 void mpi_tdiv_r( MPI rem, MPI num, MPI den);
mpi-genesis 133 void mpi_tdiv_qr( MPI quot, MPI rem, MPI num, MPI den);
mpi-genesis 134 void mpi_tdiv_q_2exp( MPI w, MPI u, unsigned count );
mpi-genesis 135 int mpi_divisible_ui(MPI dividend, ulong divisor );
mpi-genesis 136
mpi-genesis 137 /*-- mpi-gcd.c --*/
mpi-genesis 138 int mpi_gcd( MPI g, MPI a, MPI b );
mpi-genesis 139
mpi-genesis 140 /*-- mpi-pow.c --*/
mpi-genesis 141 void mpi_pow( MPI w, MPI u, MPI v);
mpi-genesis 142 void mpi_powm( MPI res, MPI base, MPI exponent, MPI mod);
mpi-genesis 143
mpi-genesis 144 /*-- mpi-mpow.c --*/
mpi-genesis 145 void mpi_mulpowm( MPI res, MPI *basearray, MPI *exparray, MPI mod);
mpi-genesis 146
mpi-genesis 147 /*-- mpi-cmp.c --*/
mpi-genesis 148 int mpi_cmp_ui( MPI u, ulong v );
mpi-genesis 149 int mpi_cmp( MPI u, MPI v );
mpi-genesis 150
mpi-genesis 151 /*-- mpi-scan.c --*/
mpi-genesis 152 int mpi_getbyte( MPI a, unsigned idx );
mpi-genesis 153 void mpi_putbyte( MPI a, unsigned idx, int value );
mpi-genesis 154 unsigned mpi_trailing_zeros( MPI a );
mpi-genesis 155
mpi-genesis 156 /*-- mpi-bit.c --*/
mpi-genesis 157 void mpi_normalize( MPI a );
mpi-genesis 158 unsigned mpi_get_nbits( MPI a );
mpi-genesis 159 int mpi_test_bit( MPI a, unsigned n );
mpi-genesis 160 void mpi_set_bit( MPI a, unsigned n );
mpi-genesis 161 void mpi_set_highbit( MPI a, unsigned n );
mpi-genesis 162 void mpi_clear_highbit( MPI a, unsigned n );
mpi-genesis 163 void mpi_clear_bit( MPI a, unsigned n );
mpi-genesis 164 void mpi_rshift( MPI x, MPI a, unsigned n );
mpi-genesis 165
mpi-genesis 166 /*-- mpi-inv.c --*/
mpi-genesis 167 void mpi_invm( MPI x, MPI u, MPI v );
mpi-genesis 168
mpi-genesis 169 #endif /*G10_MPI_H*/