diff options
author | Werner Koch <wk@gnupg.org> | 2016-01-28 17:33:51 +0100 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2016-01-28 17:34:17 +0100 |
commit | 2cf2ca7bb9741ac86e8aa92d8f03b1c5f5938897 (patch) | |
tree | aa44736804100ba41f1a1b925134dc548a969677 | |
parent | 191c2e4fe2dc0e00f61aa44e011a9596887e6ce1 (diff) | |
download | libgcrypt-2cf2ca7bb9741ac86e8aa92d8f03b1c5f5938897.tar.gz |
ecc: New API function gcry_mpi_ec_decode_point.
* mpi/ec.c (_gcry_mpi_ec_decode_point): New.
* cipher/ecc-common.h: Move two prototypes to ...
* src/ec-context.h: here.
* src/gcrypt.h.in (gcry_mpi_ec_decode_point): New.
* src/libgcrypt.def (gcry_mpi_ec_decode_point): New.
* src/libgcrypt.vers (gcry_mpi_ec_decode_point): New.
* src/visibility.c (gcry_mpi_ec_decode_point): New.
* src/visibility.h: Add new function.
--
This new function make the use of the gcry_mpi_ec_curve_point function
possible in many contexts. Here is a code snippet which could be used
in gpg to check a point:
static gpg_error_t
check_point (PKT_public_key *pk, gcry_mpi_t m_point)
{
gpg_error_t err;
char *curve;
gcry_ctx_t gctx = NULL;
gcry_mpi_point_t point = NULL;
/* Get the curve name from the first OpenPGP key parameter. */
curve = openpgp_oid_to_str (pk->pkey[0]);
if (!curve)
{
err = gpg_error_from_syserror ();
goto leave;
}
point = gcry_mpi_point_new (0);
if (!point)
{
err = gpg_error_from_syserror ();
goto leave;
}
err = gcry_mpi_ec_new (&gctx, NULL, curve);
if (err)
goto leave;
err = gcry_mpi_ec_decode_point (point, m_point, gctx);
if (err)
goto leave;
if (!gcry_mpi_ec_curve_point (point, gctx))
err = gpg_error (GPG_ERR_BAD_DATA);
leave:
gcry_ctx_release (gctx);
gcry_mpi_point_release (point);
xfree (curve);
return err;
}
Signed-off-by: Werner Koch <wk@gnupg.org>
-rw-r--r-- | NEWS | 1 | ||||
-rw-r--r-- | cipher/ecc-common.h | 9 | ||||
-rw-r--r-- | doc/gcrypt.texi | 13 | ||||
-rw-r--r-- | mpi/ec.c | 21 | ||||
-rw-r--r-- | src/ec-context.h | 12 | ||||
-rw-r--r-- | src/gcrypt.h.in | 4 | ||||
-rw-r--r-- | src/libgcrypt.def | 2 | ||||
-rw-r--r-- | src/libgcrypt.vers | 2 | ||||
-rw-r--r-- | src/mpi.h | 2 | ||||
-rw-r--r-- | src/visibility.c | 9 | ||||
-rw-r--r-- | src/visibility.h | 2 |
11 files changed, 69 insertions, 8 deletions
@@ -36,6 +36,7 @@ Noteworthy changes in version 1.7.0 (unreleased) gcry_cipher_set_sbox NEW macro. GCRY_MD_GOSTR3411_CP NEW. gcry_mpi_ec_sub NEW. + gcry_mpi_ec_decode_point NEW. GCRY_CIPHER_MODE_OCB NEW. GCRYCTL_SET_TAGLEN NEW. gcry_cipher_final NEW macro. diff --git a/cipher/ecc-common.h b/cipher/ecc-common.h index 4e528af8..748e6dba 100644 --- a/cipher/ecc-common.h +++ b/cipher/ecc-common.h @@ -89,13 +89,10 @@ elliptic_curve_t _gcry_ecc_curve_copy (elliptic_curve_t E); const char *_gcry_ecc_model2str (enum gcry_mpi_ec_models model); const char *_gcry_ecc_dialect2str (enum ecc_dialects dialect); gcry_mpi_t _gcry_ecc_ec2os (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t p); -gcry_err_code_t _gcry_ecc_os2ec (mpi_point_t result, gcry_mpi_t value); mpi_point_t _gcry_ecc_compute_public (mpi_point_t Q, mpi_ec_t ec, mpi_point_t G, gcry_mpi_t d); -gpg_err_code_t _gcry_ecc_mont_decodepoint (gcry_mpi_t pk, mpi_ec_t ctx, - mpi_point_t result); /*-- ecc.c --*/ @@ -116,10 +113,8 @@ gpg_err_code_t _gcry_ecc_eddsa_encodepoint (mpi_point_t point, mpi_ec_t ctx, unsigned int *r_buflen); gpg_err_code_t _gcry_ecc_eddsa_ensure_compact (gcry_mpi_t value, unsigned int nbits); -gpg_err_code_t _gcry_ecc_eddsa_decodepoint (gcry_mpi_t pk, mpi_ec_t ctx, - mpi_point_t result, - unsigned char **r_encpk, - unsigned int *r_encpklen); + + gpg_err_code_t _gcry_ecc_eddsa_compute_h_d (unsigned char **r_digest, gcry_mpi_t d, mpi_ec_t ec); diff --git a/doc/gcrypt.texi b/doc/gcrypt.texi index 39c7c9f5..bbaaac62 100644 --- a/doc/gcrypt.texi +++ b/doc/gcrypt.texi @@ -4882,6 +4882,19 @@ Valid names are the point parameters of an elliptic curve (@pxref{ecc_keyparam}). @end deftypefun +@deftypefun gpg_err_code_t gcry_mpi_ec_decode_point ( @ + @w{mpi_point_t @var{result}}, @w{gcry_mpi_t @var{value}}, @ + @w{gcry_ctx_t @var{ctx}}) + +Decode the point given as an MPI in @var{value} and store at +@var{result}. To decide which encoding is used the function takes a +context @var{ctx} which can be created with @code{gcry_mpi_ec_new}. +If @code{NULL} is given for the context the function assumes a 0x04 +prefixed uncompressed encoding. On error an error code is returned +and @var{result} might be changed. +@end deftypefun + + @deftypefun int gcry_mpi_ec_get_affine ( @ @w{gcry_mpi_t @var{x}}, @w{gcry_mpi_t @var{y}}, @ @w{gcry_mpi_point_t @var{point}}, @w{gcry_ctx_t @var{ctx}}) @@ -589,6 +589,27 @@ _gcry_mpi_ec_set_point (const char *name, gcry_mpi_point_t newvalue, } +/* Given an encoded point in the MPI VALUE and a context EC, decode + * the point according to the context and store it in RESULT. On + * error an error code is return but RESULT might have been changed. + * If no context is given the function tries to decode VALUE by + * assuming a 0x04 prefixed uncompressed encoding. */ +gpg_err_code_t +_gcry_mpi_ec_decode_point (mpi_point_t result, gcry_mpi_t value, mpi_ec_t ec) +{ + gcry_err_code_t rc; + + if (ec && ec->dialect == ECC_DIALECT_ED25519) + rc = _gcry_ecc_eddsa_decodepoint (value, ec, result, NULL, NULL); + else if (ec && ec->model == MPI_EC_MONTGOMERY) + rc = _gcry_ecc_mont_decodepoint (value, ec, result); + else + rc = _gcry_ecc_os2ec (result, value); + + return rc; +} + + /* Compute the affine coordinates from the projective coordinates in POINT. Set them into X and Y. If one coordinate is not required, X or Y may be passed as NULL. CTX is the usual context. Returns: 0 diff --git a/src/ec-context.h b/src/ec-context.h index c8f2ad0c..d74fb695 100644 --- a/src/ec-context.h +++ b/src/ec-context.h @@ -81,5 +81,17 @@ gpg_err_code_t _gcry_ecc_set_mpi (const char *name, gpg_err_code_t _gcry_ecc_set_point (const char *name, gcry_mpi_point_t newvalue, mpi_ec_t ec); +/*-- cipher/ecc-misc.c --*/ +gcry_err_code_t _gcry_ecc_os2ec (mpi_point_t result, gcry_mpi_t value); +gpg_err_code_t _gcry_ecc_mont_decodepoint (gcry_mpi_t pk, mpi_ec_t ctx, + mpi_point_t result); + +/*-- cipher/ecc-eddsa.c --*/ +gpg_err_code_t _gcry_ecc_eddsa_decodepoint (gcry_mpi_t pk, mpi_ec_t ctx, + mpi_point_t result, + unsigned char **r_encpk, + unsigned int *r_encpklen); + + #endif /*GCRY_EC_CONTEXT_H*/ diff --git a/src/gcrypt.h.in b/src/gcrypt.h.in index 93b1f43f..f48f04fb 100644 --- a/src/gcrypt.h.in +++ b/src/gcrypt.h.in @@ -694,6 +694,10 @@ gpg_error_t gcry_mpi_ec_set_mpi (const char *name, gcry_mpi_t newvalue, gpg_error_t gcry_mpi_ec_set_point (const char *name, gcry_mpi_point_t newvalue, gcry_ctx_t ctx); +/* Decode and store VALUE into RESULT. */ +gpg_error_t gcry_mpi_ec_decode_point (gcry_mpi_point_t result, + gcry_mpi_t value, gcry_ctx_t ctx); + /* Store the affine coordinates of POINT into X and Y. */ int gcry_mpi_ec_get_affine (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_point_t point, gcry_ctx_t ctx); diff --git a/src/libgcrypt.def b/src/libgcrypt.def index f3e074bd..067cb843 100644 --- a/src/libgcrypt.def +++ b/src/libgcrypt.def @@ -280,4 +280,6 @@ EXPORTS gcry_md_extract @245 + gcry_mpi_ec_decode_point @246 + ;; end of file with public symbols for Windows. diff --git a/src/libgcrypt.vers b/src/libgcrypt.vers index 5b3d4192..785b8ed2 100644 --- a/src/libgcrypt.vers +++ b/src/libgcrypt.vers @@ -106,7 +106,7 @@ GCRYPT_1.6 { gcry_mpi_ec_set_mpi; gcry_mpi_ec_set_point; gcry_mpi_ec_get_affine; gcry_mpi_ec_dup; gcry_mpi_ec_add; gcry_mpi_ec_sub; gcry_mpi_ec_mul; - gcry_mpi_ec_curve_point; + gcry_mpi_ec_curve_point; gcry_mpi_ec_decode_point; gcry_log_debug; gcry_log_debughex; gcry_log_debugmpi; gcry_log_debugpnt; gcry_log_debugsxp; @@ -307,6 +307,8 @@ gpg_err_code_t _gcry_mpi_ec_set_mpi (const char *name, gcry_mpi_t newvalue, gpg_err_code_t _gcry_mpi_ec_set_point (const char *name, gcry_mpi_point_t newvalue, gcry_ctx_t ctx); +gpg_err_code_t _gcry_mpi_ec_decode_point (mpi_point_t result, + gcry_mpi_t value, mpi_ec_t ec); /*-- ecc-curves.c --*/ gpg_err_code_t _gcry_mpi_ec_new (gcry_ctx_t *r_ctx, diff --git a/src/visibility.c b/src/visibility.c index 23a2705a..3abbd379 100644 --- a/src/visibility.c +++ b/src/visibility.c @@ -544,6 +544,15 @@ gcry_mpi_ec_set_point (const char *name, gcry_mpi_point_t newvalue, return gpg_error (_gcry_mpi_ec_set_point (name, newvalue, ctx)); } +gpg_error_t +gcry_mpi_ec_decode_point (gcry_mpi_point_t result, gcry_mpi_t value, + gcry_ctx_t ctx) +{ + return gpg_error (_gcry_mpi_ec_decode_point + (result, value, + ctx? _gcry_ctx_get_pointer (ctx, CONTEXT_TYPE_EC) : NULL)); +} + int gcry_mpi_ec_get_affine (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_point_t point, gcry_ctx_t ctx) diff --git a/src/visibility.h b/src/visibility.h index bb25de01..7ecd75e3 100644 --- a/src/visibility.h +++ b/src/visibility.h @@ -222,6 +222,7 @@ MARK_VISIBLEX (gcry_mpi_ec_add) MARK_VISIBLEX (gcry_mpi_ec_sub) MARK_VISIBLEX (gcry_mpi_ec_curve_point) MARK_VISIBLEX (gcry_mpi_ec_dup) +MARK_VISIBLEX (gcry_mpi_ec_decode_point) MARK_VISIBLEX (gcry_mpi_ec_get_affine) MARK_VISIBLEX (gcry_mpi_ec_mul) MARK_VISIBLEX (gcry_mpi_ec_new) @@ -492,6 +493,7 @@ MARK_VISIBLEX (_gcry_mpi_get_const) #define gcry_mpi_ec_sub _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_ec_curve_point _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_ec_dup _gcry_USE_THE_UNDERSCORED_FUNCTION +#define gcry_mpi_ec_decode_point _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_ec_get_affine _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_ec_get_mpi _gcry_USE_THE_UNDERSCORED_FUNCTION #define gcry_mpi_ec_get_point _gcry_USE_THE_UNDERSCORED_FUNCTION |