summaryrefslogtreecommitdiff
path: root/tests
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 /tests
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 'tests')
-rw-r--r--tests/mpitests.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/tests/mpitests.c b/tests/mpitests.c
index cf828425..3b75ea79 100644
--- a/tests/mpitests.c
+++ b/tests/mpitests.c
@@ -88,6 +88,67 @@ unsigned char manyff[] = {
};
+static int
+test_const_and_immutable (void)
+{
+ gcry_mpi_t one, second_one;
+
+ one = gcry_mpi_set_ui (NULL, 1);
+ if (gcry_mpi_get_flag (one, GCRYMPI_FLAG_IMMUTABLE)
+ || gcry_mpi_get_flag (one, GCRYMPI_FLAG_CONST))
+ die ("immutable or const flag initially set\n");
+
+ second_one = gcry_mpi_copy (one);
+ if (gcry_mpi_get_flag (second_one, GCRYMPI_FLAG_IMMUTABLE))
+ die ("immutable flag set after copy\n");
+ if (gcry_mpi_get_flag (second_one, GCRYMPI_FLAG_CONST))
+ die ("const flag set after copy\n");
+ gcry_mpi_release (second_one);
+
+ gcry_mpi_set_flag (one, GCRYMPI_FLAG_IMMUTABLE);
+ if (!gcry_mpi_get_flag (one, GCRYMPI_FLAG_IMMUTABLE))
+ die ("failed to set immutable flag\n");
+ if (gcry_mpi_get_flag (one, GCRYMPI_FLAG_CONST))
+ die ("const flag unexpectly set\n");
+
+ second_one = gcry_mpi_copy (one);
+ if (gcry_mpi_get_flag (second_one, GCRYMPI_FLAG_IMMUTABLE))
+ die ("immutable flag not cleared after copy\n");
+ if (gcry_mpi_get_flag (second_one, GCRYMPI_FLAG_CONST))
+ die ("const flag unexpectly set after copy\n");
+ gcry_mpi_release (second_one);
+
+ gcry_mpi_clear_flag (one, GCRYMPI_FLAG_IMMUTABLE);
+ if (gcry_mpi_get_flag (one, GCRYMPI_FLAG_IMMUTABLE))
+ die ("failed to clear immutable flag\n");
+ if (gcry_mpi_get_flag (one, GCRYMPI_FLAG_CONST))
+ die ("const flag unexpectly set\n");
+
+ gcry_mpi_set_flag (one, GCRYMPI_FLAG_CONST);
+ if (!gcry_mpi_get_flag (one, GCRYMPI_FLAG_CONST))
+ die ("failed to set const flag\n");
+ if (!gcry_mpi_get_flag (one, GCRYMPI_FLAG_IMMUTABLE))
+ die ("failed to set immutable flag with const flag\n");
+
+ second_one = gcry_mpi_copy (one);
+ if (gcry_mpi_get_flag (second_one, GCRYMPI_FLAG_IMMUTABLE))
+ die ("immutable flag not cleared after copy\n");
+ if (gcry_mpi_get_flag (second_one, GCRYMPI_FLAG_CONST))
+ die ("const flag not cleared after copy\n");
+ gcry_mpi_release (second_one);
+
+ gcry_mpi_clear_flag (one, GCRYMPI_FLAG_IMMUTABLE);
+ if (!gcry_mpi_get_flag (one, GCRYMPI_FLAG_IMMUTABLE))
+ die ("clearing immutable flag not ignored for a constant MPI\n");
+ if (!gcry_mpi_get_flag (one, GCRYMPI_FLAG_CONST))
+ die ("const flag unexpectly cleared\n");
+
+ /* Due to the the constant flag the release below should be a NOP
+ and will leak memory. */
+ gcry_mpi_release (one);
+ return 1;
+}
+
static int
test_add (void)
@@ -292,6 +353,7 @@ main (int argc, char* argv[])
}
gcry_control(GCRYCTL_DISABLE_SECMEM);
+ test_const_and_immutable ();
test_add ();
test_sub ();
test_mul ();