From bdb7dc21bd81290f088ff1db29ea5496b335bb1f Mon Sep 17 00:00:00 2001 From: jsing Date: Sat, 20 Jun 2015 13:26:08 +0000 Subject: [PATCH] Provide EC_curve_nid2nist() and EC_curve_nist2nid(). From OpenSSL. Rides libcrypto bump. ok miod@ (a while ago) --- lib/libcrypto/ec/ec.h | 4 ++- lib/libcrypto/ec/ec_curve.c | 55 ++++++++++++++++++++++++++++- lib/libssl/src/crypto/ec/ec.h | 4 ++- lib/libssl/src/crypto/ec/ec_curve.c | 55 ++++++++++++++++++++++++++++- 4 files changed, 114 insertions(+), 4 deletions(-) diff --git a/lib/libcrypto/ec/ec.h b/lib/libcrypto/ec/ec.h index db0b99a8e9d..3b409ff92d1 100644 --- a/lib/libcrypto/ec/ec.h +++ b/lib/libcrypto/ec/ec.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ec.h,v 1.9 2014/06/12 15:49:29 deraadt Exp $ */ +/* $OpenBSD: ec.h,v 1.10 2015/06/20 13:26:08 jsing Exp $ */ /* * Originally written by Bodo Moeller for the OpenSSL project. */ @@ -395,6 +395,8 @@ typedef struct { * are filled with the data of the first nitems internal groups */ size_t EC_get_builtin_curves(EC_builtin_curve *r, size_t nitems); +const char *EC_curve_nid2nist(int nid); +int EC_curve_nist2nid(const char *name); /********************************************************************/ /* EC_POINT functions */ diff --git a/lib/libcrypto/ec/ec_curve.c b/lib/libcrypto/ec/ec_curve.c index d913867b760..9278e5593b4 100644 --- a/lib/libcrypto/ec/ec_curve.c +++ b/lib/libcrypto/ec/ec_curve.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ec_curve.c,v 1.11 2015/02/09 01:12:03 doug Exp $ */ +/* $OpenBSD: ec_curve.c,v 1.12 2015/06/20 13:26:08 jsing Exp $ */ /* * Written by Nils Larsch for the OpenSSL project. */ @@ -69,6 +69,8 @@ * */ +#include + #include #include "ec_lcl.h" @@ -3285,3 +3287,54 @@ EC_get_builtin_curves(EC_builtin_curve * r, size_t nitems) return curve_list_length; } + +/* + * Functions to translate between common NIST curve names and NIDs. + */ + +typedef struct { + const char *name; /* NIST Name of curve */ + int nid; /* Curve NID */ +} EC_NIST_NAME; + +static EC_NIST_NAME nist_curves[] = { + { "B-163", NID_sect163r2 }, + { "B-233", NID_sect233r1 }, + { "B-283", NID_sect283r1 }, + { "B-409", NID_sect409r1 }, + { "B-571", NID_sect571r1 }, + { "K-163", NID_sect163k1 }, + { "K-233", NID_sect233k1 }, + { "K-283", NID_sect283k1 }, + { "K-409", NID_sect409k1 }, + { "K-571", NID_sect571k1 }, + { "P-192", NID_X9_62_prime192v1 }, + { "P-224", NID_secp224r1 }, + { "P-256", NID_X9_62_prime256v1 }, + { "P-384", NID_secp384r1 }, + { "P-521", NID_secp521r1 } +}; + +const char * +EC_curve_nid2nist(int nid) +{ + size_t i; + + for (i = 0; i < sizeof(nist_curves) / sizeof(EC_NIST_NAME); i++) { + if (nist_curves[i].nid == nid) + return (nist_curves[i].name); + } + return (NULL); +} + +int +EC_curve_nist2nid(const char *name) +{ + size_t i; + + for (i = 0; i < sizeof(nist_curves) / sizeof(EC_NIST_NAME); i++) { + if (!strcmp(nist_curves[i].name, name)) + return (nist_curves[i].nid); + } + return (NID_undef); +} diff --git a/lib/libssl/src/crypto/ec/ec.h b/lib/libssl/src/crypto/ec/ec.h index db0b99a8e9d..3b409ff92d1 100644 --- a/lib/libssl/src/crypto/ec/ec.h +++ b/lib/libssl/src/crypto/ec/ec.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ec.h,v 1.9 2014/06/12 15:49:29 deraadt Exp $ */ +/* $OpenBSD: ec.h,v 1.10 2015/06/20 13:26:08 jsing Exp $ */ /* * Originally written by Bodo Moeller for the OpenSSL project. */ @@ -395,6 +395,8 @@ typedef struct { * are filled with the data of the first nitems internal groups */ size_t EC_get_builtin_curves(EC_builtin_curve *r, size_t nitems); +const char *EC_curve_nid2nist(int nid); +int EC_curve_nist2nid(const char *name); /********************************************************************/ /* EC_POINT functions */ diff --git a/lib/libssl/src/crypto/ec/ec_curve.c b/lib/libssl/src/crypto/ec/ec_curve.c index d913867b760..9278e5593b4 100644 --- a/lib/libssl/src/crypto/ec/ec_curve.c +++ b/lib/libssl/src/crypto/ec/ec_curve.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ec_curve.c,v 1.11 2015/02/09 01:12:03 doug Exp $ */ +/* $OpenBSD: ec_curve.c,v 1.12 2015/06/20 13:26:08 jsing Exp $ */ /* * Written by Nils Larsch for the OpenSSL project. */ @@ -69,6 +69,8 @@ * */ +#include + #include #include "ec_lcl.h" @@ -3285,3 +3287,54 @@ EC_get_builtin_curves(EC_builtin_curve * r, size_t nitems) return curve_list_length; } + +/* + * Functions to translate between common NIST curve names and NIDs. + */ + +typedef struct { + const char *name; /* NIST Name of curve */ + int nid; /* Curve NID */ +} EC_NIST_NAME; + +static EC_NIST_NAME nist_curves[] = { + { "B-163", NID_sect163r2 }, + { "B-233", NID_sect233r1 }, + { "B-283", NID_sect283r1 }, + { "B-409", NID_sect409r1 }, + { "B-571", NID_sect571r1 }, + { "K-163", NID_sect163k1 }, + { "K-233", NID_sect233k1 }, + { "K-283", NID_sect283k1 }, + { "K-409", NID_sect409k1 }, + { "K-571", NID_sect571k1 }, + { "P-192", NID_X9_62_prime192v1 }, + { "P-224", NID_secp224r1 }, + { "P-256", NID_X9_62_prime256v1 }, + { "P-384", NID_secp384r1 }, + { "P-521", NID_secp521r1 } +}; + +const char * +EC_curve_nid2nist(int nid) +{ + size_t i; + + for (i = 0; i < sizeof(nist_curves) / sizeof(EC_NIST_NAME); i++) { + if (nist_curves[i].nid == nid) + return (nist_curves[i].name); + } + return (NULL); +} + +int +EC_curve_nist2nid(const char *name) +{ + size_t i; + + for (i = 0; i < sizeof(nist_curves) / sizeof(EC_NIST_NAME); i++) { + if (!strcmp(nist_curves[i].name, name)) + return (nist_curves[i].nid); + } + return (NID_undef); +} -- 2.20.1