From b84dff15db04301e1858e0f80ca1c3bb874b88cc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Niels=20M=C3=B6ller?= <nisse@lysator.liu.se>
Date: Sun, 9 Apr 2017 15:48:01 +0200
Subject: [PATCH] Define accessor functions to get address of ecc curve
 structs.

---
 ChangeLog                |  8 ++++++++
 ecc-192.c                |  6 +++++-
 ecc-224.c                |  7 ++++++-
 ecc-256.c                |  7 ++++++-
 ecc-384.c                |  7 ++++++-
 ecc-521.c                |  6 +++++-
 ecc-curve.h              | 25 ++++++++++++++++++++-----
 ecc-internal.h           |  6 ++++++
 examples/ecc-benchmark.c | 10 +++++-----
 testsuite/testutils.c    | 10 +++++-----
 10 files changed, 72 insertions(+), 20 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 8dd78c91..9a407deb 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,13 @@
 2017-04-09  Niels Möller  <nisse@lysator.liu.se>
 
+	* ecc-curve.h (nettle_get_secp_192r1, nettle_get_secp_224r1)
+	(nettle_get_secp_256r1, nettle_get_secp_384r1)
+	(nettle_get_secp_521r1): New functions, returning a pointer to
+	corresponding structure.
+	(nettle_secp_192r1, nettle_secp_224r1, nettle_secp_256r1)
+	(nettle_secp_384r1, nettle_secp_521r1): Redefined as macros,
+	calling the corresponding function.
+
 	* nettle-meta.h (nettle_ciphers, nettle_aeads, nettle_armors): New
 	macros, analogous to below change to nettle_hashes.
 
diff --git a/ecc-192.c b/ecc-192.c
index 5c52b043..4f428113 100644
--- a/ecc-192.c
+++ b/ecc-192.c
@@ -110,7 +110,7 @@ ecc_192_modp (const struct ecc_modulo *m UNUSED, mp_limb_t *rp)
 #define ecc_192_modp ecc_mod
 #endif
 
-const struct ecc_curve nettle_secp_192r1 =
+const struct ecc_curve _nettle_secp_192r1 =
 {
   {
     192,
@@ -172,3 +172,7 @@ const struct ecc_curve nettle_secp_192r1 =
   ecc_table
 };
 
+const struct ecc_curve *nettle_get_secp_192r1(void)
+{
+  return &_nettle_secp_192r1;
+}
diff --git a/ecc-224.c b/ecc-224.c
index cdb42197..5962e1b8 100644
--- a/ecc-224.c
+++ b/ecc-224.c
@@ -62,7 +62,7 @@ ecc_224_modp (const struct ecc_modulo *m, mp_limb_t *rp);
 # error Configuration error
 #endif
 
-const struct ecc_curve nettle_secp_224r1 =
+const struct ecc_curve _nettle_secp_224r1 =
 {
   {
     224,
@@ -123,3 +123,8 @@ const struct ecc_curve nettle_secp_224r1 =
   ecc_unit,
   ecc_table
 };
+
+const struct ecc_curve *nettle_get_secp_224r1(void)
+{
+  return &_nettle_secp_224r1;
+}
diff --git a/ecc-256.c b/ecc-256.c
index e757985c..7eed2835 100644
--- a/ecc-256.c
+++ b/ecc-256.c
@@ -239,7 +239,7 @@ ecc_256_modq (const struct ecc_modulo *q, mp_limb_t *rp)
 #error Unsupported parameters
 #endif
 
-const struct ecc_curve nettle_secp_256r1 =
+const struct ecc_curve _nettle_secp_256r1 =
 {
   {
     256,
@@ -300,3 +300,8 @@ const struct ecc_curve nettle_secp_256r1 =
   ecc_unit,
   ecc_table
 };
+
+const struct ecc_curve *nettle_get_secp_256r1(void)
+{
+  return &_nettle_secp_256r1;
+}
diff --git a/ecc-384.c b/ecc-384.c
index a393c61f..94b8af91 100644
--- a/ecc-384.c
+++ b/ecc-384.c
@@ -147,7 +147,7 @@ ecc_384_modp (const struct ecc_modulo *p, mp_limb_t *rp)
 #define ecc_384_modp ecc_mod
 #endif
   
-const struct ecc_curve nettle_secp_384r1 =
+const struct ecc_curve _nettle_secp_384r1 =
 {
   {
     384,
@@ -208,3 +208,8 @@ const struct ecc_curve nettle_secp_384r1 =
   ecc_unit,
   ecc_table
 };
+
+const struct ecc_curve *nettle_get_secp_384r1(void)
+{
+  return &_nettle_secp_384r1;
+}
diff --git a/ecc-521.c b/ecc-521.c
index 1a08f209..52a018dd 100644
--- a/ecc-521.c
+++ b/ecc-521.c
@@ -75,7 +75,7 @@ ecc_521_modp (const struct ecc_modulo *m UNUSED, mp_limb_t *rp)
 }
 #endif
 
-const struct ecc_curve nettle_secp_521r1 =
+const struct ecc_curve _nettle_secp_521r1 =
 {
   {
     521,
@@ -137,3 +137,7 @@ const struct ecc_curve nettle_secp_521r1 =
   ecc_table
 };
 
+const struct ecc_curve *nettle_get_secp_521r1(void)
+{
+  return &_nettle_secp_521r1;
+}
diff --git a/ecc-curve.h b/ecc-curve.h
index 574c9f2e..f3fd63e4 100644
--- a/ecc-curve.h
+++ b/ecc-curve.h
@@ -41,11 +41,26 @@ extern "C" {
 /* The contents of this struct is internal. */
 struct ecc_curve;
 
-extern const struct ecc_curve nettle_secp_192r1;
-extern const struct ecc_curve nettle_secp_224r1;
-extern const struct ecc_curve nettle_secp_256r1;
-extern const struct ecc_curve nettle_secp_384r1;
-extern const struct ecc_curve nettle_secp_521r1;
+#ifdef __GNUC__
+#define NETTLE_PURE __attribute__((pure))
+#else
+#define NETTLE_PURE
+#endif
+
+const struct ecc_curve * NETTLE_PURE nettle_get_secp_192r1(void);
+const struct ecc_curve * NETTLE_PURE nettle_get_secp_224r1(void);
+const struct ecc_curve * NETTLE_PURE nettle_get_secp_256r1(void);
+const struct ecc_curve * NETTLE_PURE nettle_get_secp_384r1(void);
+const struct ecc_curve * NETTLE_PURE nettle_get_secp_521r1(void);
+
+#undef NETTLE_PURE
+
+/* For backwards compatibility */
+#define nettle_secp_192r1 (*nettle_get_secp_192r1())
+#define nettle_secp_224r1 (*nettle_get_secp_224r1())
+#define nettle_secp_256r1 (*nettle_get_secp_256r1())
+#define nettle_secp_384r1 (*nettle_get_secp_384r1())
+#define nettle_secp_521r1 (*nettle_get_secp_521r1())
 
 #ifdef __cplusplus
 }
diff --git a/ecc-internal.h b/ecc-internal.h
index ce1e34fb..94fc218b 100644
--- a/ecc-internal.h
+++ b/ecc-internal.h
@@ -73,6 +73,12 @@
 #define sec_modinv _nettle_sec_modinv
 #define curve25519_eh_to_x _nettle_curve25519_eh_to_x
 
+extern const struct ecc_curve _nettle_secp_192r1;
+extern const struct ecc_curve _nettle_secp_224r1;
+extern const struct ecc_curve _nettle_secp_256r1;
+extern const struct ecc_curve _nettle_secp_384r1;
+extern const struct ecc_curve _nettle_secp_521r1;
+
 /* Keep this structure internal for now. It's misnamed (since it's
    really implementing the equivalent twisted Edwards curve, with
    different coordinates). And we're not quite ready to provide
diff --git a/examples/ecc-benchmark.c b/examples/ecc-benchmark.c
index 8e5e0953..ea0be173 100644
--- a/examples/ecc-benchmark.c
+++ b/examples/ecc-benchmark.c
@@ -330,12 +330,12 @@ bench_curve (const struct ecc_curve *ecc)
 }
 
 const struct ecc_curve * const curves[] = {
-  &nettle_secp_192r1,
-  &nettle_secp_224r1,
+  &_nettle_secp_192r1,
+  &_nettle_secp_224r1,
   &_nettle_curve25519,
-  &nettle_secp_256r1,
-  &nettle_secp_384r1,
-  &nettle_secp_521r1,
+  &_nettle_secp_256r1,
+  &_nettle_secp_384r1,
+  &_nettle_secp_521r1,
 };
 
 #define numberof(x)  (sizeof (x) / sizeof ((x)[0]))
diff --git a/testsuite/testutils.c b/testsuite/testutils.c
index 6f897617..7a23a46d 100644
--- a/testsuite/testutils.c
+++ b/testsuite/testutils.c
@@ -1212,11 +1212,11 @@ test_dsa_key(const struct dsa_params *params,
 }
 
 const struct ecc_curve * const ecc_curves[] = {
-  &nettle_secp_192r1,
-  &nettle_secp_224r1,
-  &nettle_secp_256r1,
-  &nettle_secp_384r1,
-  &nettle_secp_521r1,
+  &_nettle_secp_192r1,
+  &_nettle_secp_224r1,
+  &_nettle_secp_256r1,
+  &_nettle_secp_384r1,
+  &_nettle_secp_521r1,
   &_nettle_curve25519,
   NULL
 };
-- 
GitLab