Linux bluetooth development
 help / color / mirror / Atom feed
From: Brian Gix <brian.gix@intel.com>
To: Marcel Holtmann <marcel@holtmann.org>,
	Luiz Augusto von Dentz <luiz.dentz@gmail.com>,
	linux-bluetooth@vger.kernel.org
Cc: Brian Gix <brian.gix@intel.com>
Subject: [PATCH BlueZ v2 1/1] shared/ecc: Allow pre-composed Private Keys
Date: Fri, 27 Apr 2018 10:10:49 -0700	[thread overview]
Message-ID: <20180427171049.2250-2-brian.gix@intel.com> (raw)
In-Reply-To: <20180427171049.2250-1-brian.gix@intel.com>

Add function ecc_make_public_key() which takes a const private_key
and generates a validated public_key to complete the pair.

Non-ephemeral key sets are required to support Bluetooth Mesh.
---
 src/shared/ecc.c | 42 +++++++++++++++++++++++++++++-------------
 src/shared/ecc.h | 17 ++++++++++++++---
 2 files changed, 43 insertions(+), 16 deletions(-)

diff --git a/src/shared/ecc.c b/src/shared/ecc.c
index eb2cc005d..47f229ca9 100644
--- a/src/shared/ecc.c
+++ b/src/shared/ecc.c
@@ -856,33 +856,49 @@ static void ecc_native2bytes(const uint64_t native[NUM_ECC_DIGITS],
 	}
 }
 
-bool ecc_make_key(uint8_t public_key[64], uint8_t private_key[32])
+bool ecc_make_public_key(const uint8_t private_key[32], uint8_t public_key[64])
 {
 	struct ecc_point pk;
 	uint64_t priv[NUM_ECC_DIGITS];
-	unsigned tries = 0;
 
-	do {
-		if (!get_random_number(priv) || (tries++ >= MAX_TRIES))
-			return false;
+	ecc_bytes2native(private_key, priv);
 
-		if (vli_is_zero(priv))
-			continue;
+	if (vli_is_zero(priv))
+		return false;
 
-		/* Make sure the private key is in the range [1, n-1]. */
-		if (vli_cmp(curve_n, priv) != 1)
-			continue;
+	/* Make sure the private key is in the range [1, n-1]. */
+	if (vli_cmp(curve_n, priv) != 1)
+		return false;
+
+	ecc_point_mult(&pk, &curve_g, priv, NULL, vli_num_bits(priv));
 
-		ecc_point_mult(&pk, &curve_g, priv, NULL, vli_num_bits(priv));
-	} while (ecc_point_is_zero(&pk));
+	if (ecc_point_is_zero(&pk))
+		return false;
 
-	ecc_native2bytes(priv, private_key);
 	ecc_native2bytes(pk.x, public_key);
 	ecc_native2bytes(pk.y, &public_key[32]);
 
 	return true;
 }
 
+bool ecc_make_key(uint8_t public_key[64], uint8_t private_key[32])
+{
+	uint64_t priv[NUM_ECC_DIGITS];
+	unsigned tries = 0;
+	bool result = false;
+
+	for (tries = 0; !result && tries < MAX_TRIES; tries++) {
+		if (!get_random_number(priv))
+			continue;
+
+		ecc_native2bytes(priv, private_key);
+
+		result = ecc_make_public_key(private_key, public_key);
+	}
+
+	return result;
+}
+
 bool ecc_valid_public_key(const uint8_t public_key[64])
 {
 	struct ecc_point pk;
diff --git a/src/shared/ecc.h b/src/shared/ecc.h
index f89f2b0c3..a88e735c7 100644
--- a/src/shared/ecc.h
+++ b/src/shared/ecc.h
@@ -27,14 +27,25 @@
 #include <stdbool.h>
 #include <stdint.h>
 
+/* Create a public key from a private key.
+ *
+ * Outputs:
+ *	private_key - Const private key
+ *	public_key  - Will be filled in with the public key.
+ *
+ * Returns true if the public key was generated successfully, false
+ * if an error occurred. The keys are with the LSB first.
+ */
+bool ecc_make_public_key(const uint8_t private_key[32], uint8_t public_key[64]);
+
 /* Create a public/private key pair.
  *
  * Outputs:
  *	public_key  - Will be filled in with the public key.
- *	private_Key - Will be filled in with the private key.
+ *	private_key - Will be filled in with the private key.
  *
  * Returns true if the key pair was generated successfully, false
- * if an error occurred. They keys are with the LSB first.
+ * if an error occurred. The keys are with the LSB first.
  */
 bool ecc_make_key(uint8_t public_key[64], uint8_t private_key[32]);
 
@@ -54,7 +65,7 @@ bool ecc_valid_public_key(const uint8_t public_key[64]);
  *
  * Inputs:
  *	public_key  - The public key of the remote party.
- *	private_Key - Your private key.
+ *	private_key - Your private key.
  *
  * Outputs:
  *	secret - Will be filled in with the shared secret value.
-- 
2.14.3

  reply	other threads:[~2018-04-27 17:10 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-04-27 17:10 [PATCH BlueZ v2 0/1] Allow Non-Ephemeral ECC Key Sets Brian Gix
2018-04-27 17:10 ` Brian Gix [this message]
2018-04-30  8:46   ` [PATCH BlueZ v2 1/1] shared/ecc: Allow pre-composed Private Keys Marcel Holtmann
2018-04-30 15:47     ` Gix, Brian
2018-04-30  7:19 ` [PATCH BlueZ v2 0/1] Allow Non-Ephemeral ECC Key Sets Luiz Augusto von Dentz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180427171049.2250-2-brian.gix@intel.com \
    --to=brian.gix@intel.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=luiz.dentz@gmail.com \
    --cc=marcel@holtmann.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox