summaryrefslogtreecommitdiff
path: root/src/mpi.h
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2013-03-13 15:08:33 +0100
committerWerner Koch <wk@gnupg.org>2013-03-13 15:08:33 +0100
commite005629bd7bebb3e13945645c6e1230b44ab16a2 (patch)
tree46dc8a478d794e103ee4e70ec5c0832902d1bcba /src/mpi.h
parent1fecae98ee7e0fa49b29f98efa6817ca121ed98a (diff)
downloadlibgcrypt-e005629bd7bebb3e13945645c6e1230b44ab16a2.tar.gz
Add GCRYMPI_FLAG_CONST and make use constants.
* src/gcrypt.h.in (GCRYMPI_FLAG_CONST): New. * src/mpi.h (mpi_is_const, mpi_const): New. (enum gcry_mpi_constants, MPI_NUMBER_OF_CONSTANTS): New. * mpi/mpiutil.c (_gcry_mpi_init): New. (constants): New. (_gcry_mpi_free): Do not release a constant flagged MPI. (gcry_mpi_copy): Clear the const and immutable flags. (gcry_mpi_set_flag, gcry_mpi_clear_flag, gcry_mpi_get_flag): Support GCRYMPI_FLAG_CONST. (_gcry_mpi_const): New. * src/global.c (global_init): Call _gcry_mpi_init. * mpi/ec.c (mpi_ec_ctx_s): Remove fields one, two, three, four, and eight. Change all users to call mpi_const() instead. * src/mpiutils.c (gcry_mpi_set_opaque): Check the immutable flag. -- Allocating the trivial constants newly for every EC context is a waste of memory and cpu cycles. We instead provide a simple mechanism to internally support such constants. Using a new flag in THE API also allows to mark an arbitrary MPI as constant. The drawback of the constants is the their memory will never be deallocated. However, that is what constants are about.
Diffstat (limited to 'src/mpi.h')
-rw-r--r--src/mpi.h22
1 files changed, 21 insertions, 1 deletions
diff --git a/src/mpi.h b/src/mpi.h
index 93ad8897..9c22141b 100644
--- a/src/mpi.h
+++ b/src/mpi.h
@@ -70,7 +70,8 @@ struct gcry_mpi
for opaque MPIs to store the length. */
unsigned int flags; /* Bit 0: Array to be allocated in secure memory space.*/
/* Bit 2: The limb is a pointer to some m_alloced data.*/
- /* Bit 4: Const MPI - the MPI may not be modified. */
+ /* Bit 4: Immutable MPI - the MPI may not be modified. */
+ /* Bit 5: Constant MPI - the MPI will not be freed. */
mpi_limb_t *d; /* Array with the limbs */
};
@@ -108,6 +109,7 @@ struct gcry_mpi
void _gcry_mpi_immutable_failed (void);
#define mpi_immutable_failed() _gcry_mpi_immutable_failed ()
+#define mpi_is_const(a) ((a) && ((a)->flags&32))
#define mpi_is_immutable(a) ((a) && ((a)->flags&16))
#define mpi_is_opaque(a) ((a) && ((a)->flags&4))
#define mpi_is_secure(a) ((a) && ((a)->flags&1))
@@ -122,6 +124,7 @@ void _gcry_mpi_immutable_failed (void);
#define mpi_swap(a,b) _gcry_mpi_swap ((a),(b))
#define mpi_new(n) _gcry_mpi_new ((n))
#define mpi_snew(n) _gcry_mpi_snew ((n))
+#define mpi_const(n) _gcry_mpi_const ((n))
void _gcry_mpi_clear( gcry_mpi_t a );
gcry_mpi_t _gcry_mpi_alloc_like( gcry_mpi_t a );
@@ -132,6 +135,23 @@ void _gcry_mpi_swap( gcry_mpi_t a, gcry_mpi_t b);
gcry_mpi_t _gcry_mpi_new (unsigned int nbits);
gcry_mpi_t _gcry_mpi_snew (unsigned int nbits);
+/* Constants used to return constant MPIs. See _gcry_mpi_init if you
+ want to add more constants. */
+#define MPI_NUMBER_OF_CONSTANTS 6
+enum gcry_mpi_constants
+ {
+ MPI_C_ZERO,
+ MPI_C_ONE,
+ MPI_C_TWO,
+ MPI_C_THREE,
+ MPI_C_FOUR,
+ MPI_C_EIGHT
+ };
+
+
+gcry_mpi_t _gcry_mpi_const (enum gcry_mpi_constants no);
+
+
/*-- mpicoder.c --*/
void _gcry_log_mpidump( const char *text, gcry_mpi_t a );
u32 _gcry_mpi_get_keyid( gcry_mpi_t a, u32 *keyid );