1
0
mirror of git://git.gnupg.org/gnupg.git synced 2024-12-23 10:29:58 +01:00
gnupg/include/mpi.h

201 lines
7.0 KiB
C
Raw Normal View History

1997-11-18 14:06:00 +00:00
/* mpi.h - Multi Precision Integers
* Copyright (C) 1994, 1996, 1998, 1999,
* 2000, 2001 Free Software Foundation, Inc.
1997-11-18 14:06:00 +00:00
*
1998-06-11 07:16:50 +00:00
* This file is part of GNUPG.
1997-11-18 14:06:00 +00:00
*
1998-06-11 07:16:50 +00:00
* GNUPG is free software; you can redistribute it and/or modify
1997-11-18 14:06:00 +00:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
1998-06-11 07:16:50 +00:00
* GNUPG is distributed in the hope that it will be useful,
1997-11-18 14:06:00 +00:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
1997-11-26 22:02:28 +00:00
*
* Note: This code is heavily based on the GNU MP Library.
* Actually it's the same code with only minor changes in the
* way the data is stored; this is to support the abstraction
* of an optional secure memory allocation which may be used
* to avoid revealing of sensitive data due to paging etc.
* The GNU MP Library itself is published under the LGPL;
* however I decided to publish this code under the plain GPL.
1997-11-18 14:06:00 +00:00
*/
#ifndef G10_MPI_H
#define G10_MPI_H
#include <config.h>
1997-11-18 14:06:00 +00:00
#include <stdio.h>
#include "iobuf.h"
#include "types.h"
#include "memory.h"
#if BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_INT
typedef unsigned int mpi_limb_t;
typedef signed int mpi_limb_signed_t;
#elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG
typedef unsigned long int mpi_limb_t;
typedef signed long int mpi_limb_signed_t;
#elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_LONG_LONG
typedef unsigned long long int mpi_limb_t;
typedef signed long long int mpi_limb_signed_t;
#elif BYTES_PER_MPI_LIMB == SIZEOF_UNSIGNED_SHORT
typedef unsigned short int mpi_limb_t;
typedef signed short int mpi_limb_signed_t;
#else
#error BYTES_PER_MPI_LIMB does not match any C type
#endif
#define BITS_PER_MPI_LIMB (8*BYTES_PER_MPI_LIMB)
1997-11-18 14:06:00 +00:00
#ifndef EXTERN_UNLESS_MAIN_MODULE
#if defined (__riscos__) && !defined (INCLUDED_BY_MAIN_MODULE)
#define EXTERN_UNLESS_MAIN_MODULE extern
#else
#define EXTERN_UNLESS_MAIN_MODULE
#endif
#endif
1997-11-18 14:06:00 +00:00
#define DBG_MPI mpi_debug_mode
EXTERN_UNLESS_MAIN_MODULE int mpi_debug_mode;
1997-11-18 14:06:00 +00:00
struct gcry_mpi {
1997-11-18 14:06:00 +00:00
int alloced; /* array size (# of allocated limbs) */
int nlimbs; /* number of valid limbs */
1997-12-09 12:46:23 +00:00
int nbits; /* the real number of valid bits (info only) */
1997-11-18 14:06:00 +00:00
int sign; /* indicates a negative number */
1998-06-16 15:13:28 +00:00
unsigned flags; /* bit 0: array must be allocated in secure memory space */
/* bit 1: the mpi is encrypted */
1998-08-05 16:51:59 +00:00
/* bit 2: the limb is a pointer to some m_alloced data */
1997-11-18 14:06:00 +00:00
mpi_limb_t *d; /* array with the limbs */
};
typedef struct gcry_mpi *MPI;
1997-11-18 14:06:00 +00:00
#define MPI_NULL NULL
1997-12-09 12:46:23 +00:00
#define mpi_get_nlimbs(a) ((a)->nlimbs)
#define mpi_get_nbit_info(a) ((a)->nbits)
1998-06-16 15:13:28 +00:00
#define mpi_set_nbit_info(a,b) ((a)->nbits = (b))
1997-12-09 12:46:23 +00:00
#define mpi_is_neg(a) ((a)->sign)
1997-11-18 14:06:00 +00:00
/*-- mpiutil.c --*/
#ifdef M_DEBUG
#define mpi_alloc(n) mpi_debug_alloc((n), M_DBGINFO( __LINE__ ) )
#define mpi_alloc_secure(n) mpi_debug_alloc_secure((n), M_DBGINFO( __LINE__ ) )
#define mpi_free(a) mpi_debug_free((a), M_DBGINFO(__LINE__) )
#define mpi_resize(a,b) mpi_debug_resize((a),(b), M_DBGINFO(__LINE__) )
#define mpi_copy(a) mpi_debug_copy((a), M_DBGINFO(__LINE__) )
MPI mpi_debug_alloc( unsigned nlimbs, const char *info );
MPI mpi_debug_alloc_secure( unsigned nlimbs, const char *info );
void mpi_debug_free( MPI a, const char *info );
void mpi_debug_resize( MPI a, unsigned nlimbs, const char *info );
MPI mpi_debug_copy( MPI a, const char *info );
#else
MPI mpi_alloc( unsigned nlimbs );
MPI mpi_alloc_secure( unsigned nlimbs );
void mpi_free( MPI a );
void mpi_resize( MPI a, unsigned nlimbs );
MPI mpi_copy( MPI a );
#endif
1998-08-05 16:51:59 +00:00
#define mpi_is_opaque(a) ((a) && ((a)->flags&4))
MPI mpi_set_opaque( MPI a, void *p, int len );
void *mpi_get_opaque( MPI a, int *len );
1998-06-16 15:13:28 +00:00
#define mpi_is_protected(a) ((a) && ((a)->flags&2))
#define mpi_set_protect_flag(a) ((a)->flags |= 2)
#define mpi_clear_protect_flag(a) ((a)->flags &= ~2)
#define mpi_is_secure(a) ((a) && ((a)->flags&1))
1998-02-02 14:36:06 +00:00
void mpi_set_secure( MPI a );
1997-11-18 14:06:00 +00:00
void mpi_clear( MPI a );
MPI mpi_alloc_like( MPI a );
1997-11-18 14:06:00 +00:00
void mpi_set( MPI w, MPI u);
void mpi_set_ui( MPI w, ulong u);
MPI mpi_alloc_set_ui( unsigned long u);
void mpi_m_check( MPI a );
void mpi_swap( MPI a, MPI b);
/*-- mpicoder.c --*/
1997-12-09 12:46:23 +00:00
int mpi_write( IOBUF out, MPI a );
1997-11-18 14:06:00 +00:00
#ifdef M_DEBUG
1997-12-09 12:46:23 +00:00
#define mpi_read(a,b,c) mpi_debug_read((a),(b),(c), M_DBGINFO( __LINE__ ) )
MPI mpi_debug_read(IOBUF inp, unsigned *nread, int secure, const char *info);
1997-11-18 14:06:00 +00:00
#else
1997-12-09 12:46:23 +00:00
MPI mpi_read(IOBUF inp, unsigned *nread, int secure);
1997-11-18 14:06:00 +00:00
#endif
1998-08-05 16:51:59 +00:00
MPI mpi_read_from_buffer(byte *buffer, unsigned *ret_nread, int secure);
1997-11-18 14:06:00 +00:00
int mpi_fromstr(MPI val, const char *str);
int mpi_print( FILE *fp, MPI a, int mode );
void g10_log_mpidump( const char *text, MPI a );
1997-11-18 14:06:00 +00:00
u32 mpi_get_keyid( MPI a, u32 *keyid );
1997-12-09 12:46:23 +00:00
byte *mpi_get_buffer( MPI a, unsigned *nbytes, int *sign );
1998-04-09 11:19:09 +00:00
byte *mpi_get_secure_buffer( MPI a, unsigned *nbytes, int *sign );
1997-12-09 12:46:23 +00:00
void mpi_set_buffer( MPI a, const byte *buffer, unsigned nbytes, int sign );
1997-11-18 14:06:00 +00:00
#define log_mpidump g10_log_mpidump
1997-11-18 14:06:00 +00:00
/*-- mpi-add.c --*/
void mpi_add_ui(MPI w, MPI u, ulong v );
void mpi_add(MPI w, MPI u, MPI v);
1997-11-24 22:24:04 +00:00
void mpi_addm(MPI w, MPI u, MPI v, MPI m);
1997-11-18 14:06:00 +00:00
void mpi_sub_ui(MPI w, MPI u, ulong v );
void mpi_sub( MPI w, MPI u, MPI v);
1997-11-24 22:24:04 +00:00
void mpi_subm( MPI w, MPI u, MPI v, MPI m);
1997-11-18 14:06:00 +00:00
/*-- mpi-mul.c --*/
void mpi_mul_ui(MPI w, MPI u, ulong v );
void mpi_mul_2exp( MPI w, MPI u, ulong cnt);
void mpi_mul( MPI w, MPI u, MPI v);
1997-11-24 22:24:04 +00:00
void mpi_mulm( MPI w, MPI u, MPI v, MPI m);
1997-11-18 14:06:00 +00:00
/*-- mpi-div.c --*/
ulong mpi_fdiv_r_ui( MPI rem, MPI dividend, ulong divisor );
void mpi_fdiv_r( MPI rem, MPI dividend, MPI divisor );
void mpi_fdiv_q( MPI quot, MPI dividend, MPI divisor );
void mpi_fdiv_qr( MPI quot, MPI rem, MPI dividend, MPI divisor );
void mpi_tdiv_r( MPI rem, MPI num, MPI den);
void mpi_tdiv_qr( MPI quot, MPI rem, MPI num, MPI den);
1997-11-27 11:44:13 +00:00
void mpi_tdiv_q_2exp( MPI w, MPI u, unsigned count );
1997-11-18 14:06:00 +00:00
int mpi_divisible_ui(MPI dividend, ulong divisor );
/*-- mpi-gcd.c --*/
int mpi_gcd( MPI g, MPI a, MPI b );
/*-- mpi-pow.c --*/
void mpi_pow( MPI w, MPI u, MPI v);
void mpi_powm( MPI res, MPI base, MPI exp, MPI mod);
/*-- mpi-mpow.c --*/
void mpi_mulpowm( MPI res, MPI *basearray, MPI *exparray, MPI mod);
1997-11-18 14:06:00 +00:00
/*-- mpi-cmp.c --*/
int mpi_cmp_ui( MPI u, ulong v );
int mpi_cmp( MPI u, MPI v );
/*-- mpi-scan.c --*/
int mpi_getbyte( MPI a, unsigned idx );
void mpi_putbyte( MPI a, unsigned idx, int value );
1997-11-27 11:44:13 +00:00
unsigned mpi_trailing_zeros( MPI a );
1997-11-18 14:06:00 +00:00
/*-- mpi-bit.c --*/
void mpi_normalize( MPI a );
1997-11-18 14:06:00 +00:00
unsigned mpi_get_nbits( MPI a );
int mpi_test_bit( MPI a, unsigned n );
void mpi_set_bit( MPI a, unsigned n );
void mpi_set_highbit( MPI a, unsigned n );
1998-03-19 15:27:29 +00:00
void mpi_clear_highbit( MPI a, unsigned n );
1997-11-18 14:06:00 +00:00
void mpi_clear_bit( MPI a, unsigned n );
1997-11-19 13:12:23 +00:00
void mpi_rshift( MPI x, MPI a, unsigned n );
1997-11-18 14:06:00 +00:00
/*-- mpi-inv.c --*/
1997-11-24 22:24:04 +00:00
void mpi_invm( MPI x, MPI u, MPI v );
1997-11-18 14:06:00 +00:00
#endif /*G10_MPI_H*/