summaryrefslogtreecommitdiff
path: root/mpi
diff options
context:
space:
mode:
authorWerner Koch <wk@gnupg.org>2013-03-08 22:10:23 +0100
committerWerner Koch <wk@gnupg.org>2013-03-08 22:13:52 +0100
commit8ac9e756d3ca545a9b97e61ad3d42fc2e877d788 (patch)
tree9c371da68f09dca2379381529f9d7810957260b2 /mpi
parent7cce620acddac2df024ca421ed3abc32a88f3738 (diff)
downloadlibgcrypt-8ac9e756d3ca545a9b97e61ad3d42fc2e877d788.tar.gz
mpi: Add an API for EC math.
* src/context.c, src/context.h: New. * src/Makefile.am (libgcrypt_la_SOURCES): Add new files. * src/gcrypt.h.in (struct gcry_context, gcry_ctx_t): New types. (gcry_ctx_release): New prototype. (gcry_mpi_ec_p_new, gcry_mpi_ec_get_affine, gcry_mpi_ec_dup) (gcry_mpi_ec_add, gcry_mpi_ec_mul): New prototypes. * mpi/ec.c: Include errno.h and context.h. (_gcry_mpi_ec_init): Rename to .. (ec_p_init): this, make static, remove allocation and add arg CTX. (_gcry_mpi_ec_p_internal_new): New; to replace _gcry_mpi_ec_init. Change all callers to use this func. (_gcry_mpi_ec_free): Factor code out to .. (ec_deinit): New func. (gcry_mpi_ec_p_new): New. * src/visibility.c: Include context.h and mpi.h. (gcry_mpi_ec_p_new, gcry_mpi_ec_get_affine, gcry_mpi_ec_dup) (gcry_mpi_ec_add, gcry_mpi_ec_mul) (gcry_ctx_release): New wrapper functions. * src/visibility.h: Mark new wrapper functions visible. * src/libgcrypt.def, src/libgcrypt.vers: Add new symbols. * tests/t-mpi-point.c (print_mpi, hex2mpi, cmp_mpihex): New. (context_alloc): New. (make_point, basic_ec_math): New. -- This part finishes the basic API to do EC math. It provides a wrapper around all internal functions. tests/t-mpi-point.c may be useful as sample code. Eventually we will add function to retrieve curve parameters etc.
Diffstat (limited to 'mpi')
-rw-r--r--mpi/ec.c84
1 files changed, 66 insertions, 18 deletions
diff --git a/mpi/ec.c b/mpi/ec.c
index bb9bea45..e85ec04d 100644
--- a/mpi/ec.c
+++ b/mpi/ec.c
@@ -21,10 +21,12 @@
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
+#include <errno.h>
#include "mpi-internal.h"
#include "longlong.h"
#include "g10lib.h"
+#include "context.h"
#define point_init(a) _gcry_mpi_point_init ((a))
@@ -348,16 +350,13 @@ ec_invm (gcry_mpi_t x, gcry_mpi_t a, mpi_ec_t ctx)
-/* This function returns a new context for elliptic curve based on the
- field GF(p). P is the prime specifying thuis field, A is the first
- coefficient.
-
- This context needs to be released using _gcry_mpi_ec_free. */
-mpi_ec_t
-_gcry_mpi_ec_init (gcry_mpi_t p, gcry_mpi_t a)
+/* This function initialized a context for elliptic curve based on the
+ field GF(p). P is the prime specifying this field, A is the first
+ coefficient. CTX is expected to be zeroized. */
+static void
+ec_p_init (mpi_ec_t ctx, gcry_mpi_t p, gcry_mpi_t a)
{
int i;
- mpi_ec_t ctx;
gcry_mpi_t tmp;
mpi_normalize (p);
@@ -367,8 +366,6 @@ _gcry_mpi_ec_init (gcry_mpi_t p, gcry_mpi_t a)
a < p
*/
- ctx = gcry_xcalloc (1, sizeof *ctx);
-
ctx->p = mpi_copy (p);
ctx->a = mpi_copy (a);
@@ -408,18 +405,15 @@ _gcry_mpi_ec_init (gcry_mpi_t p, gcry_mpi_t a)
/* ctx->s[i] = mpi_new (384); */
/* ctx->c = mpi_new (384*2); */
/* } */
-
- return ctx;
}
-void
-_gcry_mpi_ec_free (mpi_ec_t ctx)
+
+static void
+ec_deinit (void *opaque)
{
+ mpi_ec_t ctx = opaque;
int i;
- if (!ctx)
- return;
-
mpi_free (ctx->p);
mpi_free (ctx->a);
@@ -446,8 +440,62 @@ _gcry_mpi_ec_free (mpi_ec_t ctx)
/* mpi_free (ctx->s[i]); */
/* mpi_free (ctx->c); */
/* } */
+}
+
- gcry_free (ctx);
+/* This function returns a new context for elliptic curve based on the
+ field GF(p). P is the prime specifying this field, A is the first
+ coefficient. This function is only used within Libgcrypt and not
+ part of the public API.
+
+ This context needs to be released using _gcry_mpi_ec_free. */
+mpi_ec_t
+_gcry_mpi_ec_p_internal_new (gcry_mpi_t p, gcry_mpi_t a)
+{
+ mpi_ec_t ctx;
+
+ ctx = gcry_xcalloc (1, sizeof *ctx);
+ ec_p_init (ctx, p, a);
+
+ return ctx;
+}
+
+
+void
+_gcry_mpi_ec_free (mpi_ec_t ctx)
+{
+ if (ctx)
+ {
+ ec_deinit (ctx);
+ gcry_free (ctx);
+ }
+}
+
+
+/* This function returns a new context for elliptic curve operations
+ based on the field GF(p). P is the prime specifying this field, A
+ is the first coefficient. This function is part of the public API.
+ On error this function returns NULL and sets ERRNO.
+ The context needs to be released using gcry_ctx_release. */
+gcry_ctx_t
+gcry_mpi_ec_p_new (gcry_mpi_t p, gcry_mpi_t a)
+{
+ gcry_ctx_t ctx;
+ mpi_ec_t ec;
+
+ if (!p || !a || !mpi_cmp_ui (a, 0))
+ {
+ gpg_err_set_errno (EINVAL);
+ return NULL;
+ }
+
+ ctx = _gcry_ctx_alloc (CONTEXT_TYPE_EC, sizeof *ec, ec_deinit);
+ if (!ctx)
+ return NULL;
+ ec = _gcry_ctx_get_pointer (ctx, CONTEXT_TYPE_EC);
+ ec_p_init (ec, p, a);
+
+ return ctx;
}