public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
From: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
To: linux-bluetooth@vger.kernel.org
Subject: [RFC v2 10/12] tools: Add support to generate RSI using SIRK
Date: Tue,  7 Mar 2023 14:24:20 -0800	[thread overview]
Message-ID: <20230307222422.2608483-10-luiz.dentz@gmail.com> (raw)
In-Reply-To: <20230307222422.2608483-1-luiz.dentz@gmail.com>

From: Sathish Narasimman <sathish.narasimman@intel.com>

The patch helps to generate Resolvable set identifier adv data.
which can be used as ADV data during advertisement.
It will be used to identify the device as part of setmember for
Coordinated set identification profile.
Example:
$<path to advtest/>advtest -i "761FAE703ED681F0C50B34155B6434FB"
SIRK: 761FAE703ED681F0C50B34155B6434FB
  RSI:  0x71 0xcb 0xbc 0x7e 0x01 0x84
    Random: bccb71
    Hash:   84017e
---
 tools/advtest.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 78 insertions(+), 2 deletions(-)

diff --git a/tools/advtest.c b/tools/advtest.c
index de036e783325..9ef69ed5124a 100644
--- a/tools/advtest.c
+++ b/tools/advtest.c
@@ -13,6 +13,13 @@
 #include <config.h>
 #endif
 
+#include <stdlib.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
 #include <getopt.h>
 
 #include "lib/bluetooth.h"
@@ -32,6 +39,9 @@
 			"\xe1\x23\x99\xc1\xca\x9a\xc3\x31"
 #define SCAN_IRK	"\xfa\x73\x09\x11\x3f\x03\x37\x0f" \
 			"\xf4\xf9\x93\x1e\xf9\xa3\x63\xa6"
+#ifndef MIN
+#define MIN(x, y) ((x) < (y) ? (x) : (y))
+#endif
 
 static struct mgmt *mgmt;
 static uint16_t index1 = MGMT_INDEX_NONE;
@@ -43,13 +53,73 @@ static struct bt_hci *scan_dev;
 
 static void print_rpa(const uint8_t addr[6])
 {
-	printf("  Address:  %02x:%02x:%02x:%02x:%02x:%02x\n",
+	printf("  RSI:\t0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
 					addr[5], addr[4], addr[3],
 					addr[2], addr[1], addr[0]);
 	printf("    Random: %02x%02x%02x\n", addr[3], addr[4], addr[5]);
 	printf("    Hash:   %02x%02x%02x\n", addr[0], addr[1], addr[2]);
 }
 
+static size_t hex2bin(const char *hexstr, uint8_t *buf, size_t buflen)
+{
+	size_t i, len;
+
+	len = MIN((strlen(hexstr) / 2), buflen);
+	memset(buf, 0, len);
+
+	for (i = 0; i < len; i++)
+		if (sscanf(hexstr + (i * 2), "%02hhX", &buf[i]) != 1)
+			continue;
+
+
+	return len;
+}
+
+static bool get_random_bytes(void *buf, size_t num_bytes)
+{
+	ssize_t len;
+	int fd;
+
+	fd = open("/dev/urandom", O_RDONLY);
+	if (fd < 0)
+		return false;
+
+	len = read(fd, buf, num_bytes);
+
+	close(fd);
+
+	if (len < 0)
+		return false;
+
+	return true;
+}
+
+static void generate_rsi(char *val)
+{
+	uint8_t sirk[16], hash[3];
+	uint8_t  rsi[6] = {0};
+
+	hex2bin(val, sirk, sizeof(sirk));
+
+	get_random_bytes(&rsi[3], 3);
+
+	rsi[5] &= 0x3f; /* Clear 2 msb */
+	rsi[5] |= 0x40; /* Set 2nd msb */
+
+	crypto = bt_crypto_new();
+	if (!crypto) {
+		fprintf(stderr, "Failed to open crypto interface\n");
+		mainloop_exit_failure();
+		return;
+	}
+
+	bt_crypto_ah(crypto, sirk, rsi + 3, hash);
+	memcpy(rsi, hash, 3);
+
+	print_rpa(rsi);
+}
+
+
 static void scan_le_adv_report(const void *data, uint8_t size,
 							void *user_data)
 {
@@ -351,9 +421,11 @@ static void usage(void)
 	printf("\tadvtest [options]\n");
 	printf("options:\n"
 		"\t-h, --help             Show help options\n");
+	printf(" \t-i  <128bit SIRK>,     Generate RSI ADV Data\n");
 }
 
 static const struct option main_options[] = {
+	{ "hash",   no_argument,       NULL, 'i' },
 	{ "version",   no_argument,       NULL, 'v' },
 	{ "help",      no_argument,       NULL, 'h' },
 	{ }
@@ -366,11 +438,15 @@ int main(int argc ,char *argv[])
 	for (;;) {
 		int opt;
 
-		opt = getopt_long(argc, argv, "vh", main_options, NULL);
+		opt = getopt_long(argc, argv, "i:vh", main_options, NULL);
 		if (opt < 0)
 			break;
 
 		switch (opt) {
+		case 'i':
+			printf("SIRK: %s\n", optarg);
+			generate_rsi(optarg);
+			return EXIT_SUCCESS;
 		case 'v':
 			printf("%s\n", VERSION);
 			return EXIT_SUCCESS;
-- 
2.39.2


  parent reply	other threads:[~2023-03-07 22:26 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-07 22:24 [RFC v2 01/12] shared/crypto: Add bt_crypto_sirk Luiz Augusto von Dentz
2023-03-07 22:24 ` [RFC v2 02/12] shared/ad: Add RSI data type Luiz Augusto von Dentz
2023-03-07 22:24 ` [RFC v2 03/12] doc: Add set-api Luiz Augusto von Dentz
2023-03-07 22:24 ` [RFC v2 04/12] device-api: Add Set property Luiz Augusto von Dentz
2023-03-07 22:24 ` [RFC v2 05/12] core: Add initial implementation of DeviceSet interface Luiz Augusto von Dentz
2023-03-07 22:24 ` [RFC v2 06/12] core: Check if device has RSI Luiz Augusto von Dentz
2023-03-07 22:24 ` [RFC v2 07/12] main.conf: Add CSIP profile configurable options Luiz Augusto von Dentz
2023-03-07 22:24 ` [RFC v2 08/12] shared/csip: Add initial code for handling CSIP Luiz Augusto von Dentz
2023-03-07 22:24 ` [RFC v2 09/12] profiles: Add initial code for csip plugin Luiz Augusto von Dentz
2023-03-07 22:24 ` Luiz Augusto von Dentz [this message]
2023-03-07 22:24 ` [RFC v2 11/12] client: Add support for DeviceSet proxy Luiz Augusto von Dentz
2023-03-07 22:24 ` [RFC v2 12/12] client: Use AdvertisingFlags when available Luiz Augusto von Dentz
2023-03-08  2:12 ` [RFC,v2,01/12] shared/crypto: Add bt_crypto_sirk bluez.test.bot
2023-03-11  0:40 ` [RFC v2 01/12] " patchwork-bot+bluetooth
2023-03-13  5:36   ` Luiz Augusto von Dentz
2023-03-13 23:29     ` Pauli Virtanen
2023-03-14  0:18       ` Luiz Augusto von Dentz
2023-03-14  0:57         ` Pauli Virtanen
2023-04-06  0:16           ` Luiz Augusto von Dentz
2023-04-06 18:08             ` Pauli Virtanen
2023-04-06 20:14               ` Luiz Augusto von Dentz
2023-04-13 18:48                 ` Luiz Augusto von Dentz
2023-04-13 21:14                   ` Pauli Virtanen
2023-04-14 21:56                     ` Luiz Augusto von Dentz
2023-04-15 14:57                       ` Pauli Virtanen
2023-04-13 20:11                 ` Pauli Virtanen

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=20230307222422.2608483-10-luiz.dentz@gmail.com \
    --to=luiz.dentz@gmail.com \
    --cc=linux-bluetooth@vger.kernel.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