Skip to contentSkip to conversation

Bluetooth: Add self-tests through a dedicated module

PATCH New Johan Hedberg 1 participant 8 messages Dec 22, 2014

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Bluetooth: Add self-tests through a dedicated module

Hi,

This patch set re-introduces the self-tests that were already earlier attempted to be merge. The failure with the first attempt was that the tests rely on crypto subsystem features which may not be available when the Bluetooth subsystem gets loaded (using a subsystem priority instead of normal module prioirity). By having the tests in a separate module this prioirty conflict doesn't occur.

----------------------------------------------------------------
Johan Hedberg (6):
Bluetooth: Export SMP crypto functions for self-test module
Bluetooth: Export ECDH functions for self-test module
Bluetooth: Add skeleton for self-test module
Bluetooth: Add legacy SMP self-tests
Bluetooth: Add LE SC SMP self-tests
Bluetooth: Add ECDH self-tests

 net/bluetooth/Kconfig    |   6 +
 net/bluetooth/Makefile   |  15 +-
 net/bluetooth/ecc.c      |   2 +
 net/bluetooth/selftest.c | 494 ++++++++++++++++++++++++++++++++++++++++++++++
 net/bluetooth/smp.c      | 107 +++++-----
 net/bluetooth/smp.h      |  22 +++
 6 files changed, 590 insertions(+), 56 deletions(-)
 create mode 100644 net/bluetooth/selftest.c
#1 lore raw

PATCH 1/6 Bluetooth: Export SMP crypto functions for self-test module

From: Johan Hedberg <[email protected]>

In order to run self-tests on the SMP crypto functions from a dedicated self-test module the functions need to be exported to other modules. This patch exports the necessary functions from smp.c and also adds an extra bt_* prefix to them to avoid conflicts with any other kernel code.

---
 net/bluetooth/smp.c | 107 ++++++++++++++++++++++++++++------------------------
 net/bluetooth/smp.h |  22 +++++++++++
 2 files changed, 79 insertions(+), 50 deletions(-)
net/bluetooth/smp.c +57 -50
index b67749bb55bf..507a4138fd6a 100644
@@ -200,8 +200,8 @@ static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
 	return 0;
 }
 
-static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
-		  const u8 x[16], u8 z, u8 res[16])
+int bt_smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
+	      const u8 x[16], u8 z, u8 res[16])
 {
 	u8 m[65];
 	int err;
@@ -222,9 +222,10 @@ static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
 
 	return err;
 }
+EXPORT_SYMBOL(bt_smp_f4);
 
-static int smp_f5(struct crypto_hash *tfm_cmac, u8 w[32], u8 n1[16], u8 n2[16],
-		  u8 a1[7], u8 a2[7], u8 mackey[16], u8 ltk[16])
+int bt_smp_f5(struct crypto_hash *tfm_cmac, u8 w[32], u8 n1[16], u8 n2[16],
+	      u8 a1[7], u8 a2[7], u8 mackey[16], u8 ltk[16])
 {
 	/* The btle, salt and length "magic" values are as defined in
 	 * the SMP section of the Bluetooth core specification. In ASCII
@@ -274,11 +275,11 @@ static int smp_f5(struct crypto_hash *tfm_cmac, u8 w[32], u8 n1[16], u8 n2[16],
 
 	return 0;
 }
+EXPORT_SYMBOL(bt_smp_f5);
 
-static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16],
-		  const u8 n1[16], u8 n2[16], const u8 r[16],
-		  const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
-		  u8 res[16])
+int bt_smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16], const u8 n1[16],
+	      u8 n2[16], const u8 r[16], const u8 io_cap[3], const u8 a1[7],
+	      const u8 a2[7], u8 res[16])
 {
 	u8 m[65];
 	int err;
@@ -302,9 +303,10 @@ static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16],
 
 	return err;
 }
+EXPORT_SYMBOL(bt_smp_f6);
 
-static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
-		  const u8 x[16], const u8 y[16], u32 *val)
+int bt_smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
+	      const u8 x[16], const u8 y[16], u32 *val)
 {
 	u8 m[80], tmp[16];
 	int err;
@@ -328,9 +330,10 @@ static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
 
 	return 0;
 }
+EXPORT_SYMBOL(bt_smp_g2);
 
-static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16],
-		  const u8 key_id[4], u8 res[16])
+int bt_smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16], const u8 key_id[4],
+	      u8 res[16])
 {
 	int err;
 
@@ -344,6 +347,7 @@ static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16],
 
 	return err;
 }
+EXPORT_SYMBOL(bt_smp_h6);
 
 /* The following functions map to the legacy SMP crypto functions e, c1,
  * s1 and ah.
@@ -388,9 +392,9 @@ static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
 	return err;
 }
 
-static int smp_c1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
-		  const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
-		  const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
+int bt_smp_c1(struct crypto_blkcipher *tfm_aes, const u8 k[16], const u8 r[16],
+	      const u8 preq[7], const u8 pres[7], u8 _iat, const bdaddr_t *ia,
+	      u8 _rat, const bdaddr_t *ra, u8 res[16])
 {
 	u8 p1[16], p2[16];
 	int err;
@@ -428,9 +432,10 @@ static int smp_c1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
 
 	return err;
 }
+EXPORT_SYMBOL(bt_smp_c1);
 
-static int smp_s1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
-		  const u8 r1[16], const u8 r2[16], u8 _r[16])
+int bt_smp_s1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
+	      const u8 r1[16], const u8 r2[16], u8 _r[16])
 {
 	int err;
 
@@ -444,9 +449,10 @@ static int smp_s1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
 
 	return err;
 }
+EXPORT_SYMBOL(bt_smp_s1);
 
-static int smp_ah(struct crypto_blkcipher *tfm, const u8 irk[16],
-		  const u8 r[3], u8 res[3])
+int bt_smp_ah(struct crypto_blkcipher *tfm, const u8 irk[16], const u8 r[3],
+	      u8 res[3])
 {
 	u8 _res[16];
 	int err;
@@ -471,6 +477,7 @@ static int smp_ah(struct crypto_blkcipher *tfm, const u8 irk[16],
 
 	return 0;
 }
+EXPORT_SYMBOL(bt_smp_ah);
 
 bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
 		     const bdaddr_t *bdaddr)
@@ -487,7 +494,7 @@ bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
 
 	BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
 
-	err = smp_ah(tfm, irk, &bdaddr->b[3], hash);
+	err = bt_smp_ah(tfm, irk, &bdaddr->b[3], hash);
 	if (err)
 		return false;
 
@@ -510,7 +517,7 @@ int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
 	rpa->b[5] &= 0x3f;	/* Clear two most significant bits */
 	rpa->b[5] |= 0x40;	/* Set second most significant bit */
 
-	err = smp_ah(tfm, irk, &rpa->b[3], rpa->b);
+	err = bt_smp_ah(tfm, irk, &rpa->b[3], rpa->b);
 	if (err < 0)
 		return err;
 
@@ -866,10 +873,10 @@ static u8 smp_confirm(struct smp_chan *smp)
 
 	BT_DBG("conn %p", conn);
 
-	ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
-		     conn->hcon->init_addr_type, &conn->hcon->init_addr,
-		     conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
-		     cp.confirm_val);
+	ret = bt_smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
+			conn->hcon->init_addr_type, &conn->hcon->init_addr,
+			conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
+			cp.confirm_val);
 	if (ret)
 		return SMP_UNSPECIFIED;
 
@@ -897,9 +904,9 @@ static u8 smp_random(struct smp_chan *smp)
 
 	BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
 
-	ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
-		     hcon->init_addr_type, &hcon->init_addr,
-		     hcon->resp_addr_type, &hcon->resp_addr, confirm);
+	ret = bt_smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
+			hcon->init_addr_type, &hcon->init_addr,
+			hcon->resp_addr_type, &hcon->resp_addr, confirm);
 	if (ret)
 		return SMP_UNSPECIFIED;
 
@@ -913,7 +920,7 @@ static u8 smp_random(struct smp_chan *smp)
 		__le64 rand = 0;
 		__le16 ediv = 0;
 
-		smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
+		bt_smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
 
 		memset(stk + smp->enc_key_size, 0,
 		       SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
@@ -932,7 +939,7 @@ static u8 smp_random(struct smp_chan *smp)
 		smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
 			     smp->prnd);
 
-		smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
+		bt_smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
 
 		memset(stk + smp->enc_key_size, 0,
 		       SMP_MAX_ENC_KEY_SIZE - smp->enc_key_size);
@@ -1094,13 +1101,13 @@ static void sc_generate_link_key(struct smp_chan *smp)
 	if (!smp->link_key)
 		return;
 
-	if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
+	if (bt_smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
 		kfree(smp->link_key);
 		smp->link_key = NULL;
 		return;
 	}
 
-	if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
+	if (bt_smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
 		kfree(smp->link_key);
 		smp->link_key = NULL;
 		return;
@@ -1141,10 +1148,10 @@ static void sc_generate_ltk(struct smp_chan *smp)
 	if (key->type == HCI_LK_DEBUG_COMBINATION)
 		set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
 
-	if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
+	if (bt_smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
 		return;
 
-	if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
+	if (bt_smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
 		return;
 
 	sc_add_ltk(smp);
@@ -1337,7 +1344,7 @@ static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
 	a[6] = hcon->init_addr_type;
 	b[6] = hcon->resp_addr_type;
 
-	return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
+	return bt_smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
 }
 
 static void sc_dhkey_check(struct smp_chan *smp)
@@ -1370,8 +1377,8 @@ static void sc_dhkey_check(struct smp_chan *smp)
 	if (smp->method == REQ_OOB)
 		memcpy(r, smp->rr, 16);
 
-	smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
-	       local_addr, remote_addr, check.e);
+	bt_smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
+		  local_addr, remote_addr, check.e);
 
 	smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
 }
@@ -1388,8 +1395,8 @@ static u8 sc_passkey_send_confirm(struct smp_chan *smp)
 
 	get_random_bytes(smp->prnd, sizeof(smp->prnd));
 
-	if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
-		   cfm.confirm_val))
+	if (bt_smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
+		      r, cfm.confirm_val))
 		return SMP_UNSPECIFIED;
 
 	smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
@@ -1413,8 +1420,8 @@ static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
 		r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
 		r |= 0x80;
 
-		if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
-			   smp->rrnd, r, cfm))
+		if (bt_smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
+			      smp->rrnd, r, cfm))
 			return SMP_UNSPECIFIED;
 
 		if (memcmp(smp->pcnf, cfm, 16))
@@ -1974,8 +1981,8 @@ static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
 	if (hcon->out) {
 		u8 cfm[16];
 
-		err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
-			     smp->rrnd, 0, cfm);
+		err = bt_smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
+				smp->rrnd, 0, cfm);
 		if (err)
 			return SMP_UNSPECIFIED;
 
@@ -2001,7 +2008,7 @@ mackey_and_ltk:
 		return 0;
 	}
 
-	err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
+	err = bt_smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
 	if (err)
 		return SMP_UNSPECIFIED;
 
@@ -2464,8 +2471,8 @@ static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
 	}
 
 	if (smp->method == REQ_OOB) {
-		err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
-			     smp->rr, 0, cfm.confirm_val);
+		err = bt_smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
+				smp->rr, 0, cfm.confirm_val);
 		if (err)
 			return SMP_UNSPECIFIED;
 
@@ -2499,8 +2506,8 @@ static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
 	if (conn->hcon->out)
 		return 0;
 
-	err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
-		     0, cfm.confirm_val);
+	err = bt_smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk,
+			smp->prnd, 0, cfm.confirm_val);
 	if (err)
 		return SMP_UNSPECIFIED;
 
@@ -2545,8 +2552,8 @@ static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
 	if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
 		put_unaligned_le32(hcon->passkey_notify, r);
 
-	err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
-		     io_cap, remote_addr, local_addr, e);
+	err = bt_smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
+			io_cap, remote_addr, local_addr, e);
 	if (err)
 		return SMP_UNSPECIFIED;
 
net/bluetooth/smp.h +22 -0
index 3296bf42ae80..e66ff590d079 100644
@@ -23,6 +23,8 @@
 #ifndef __SMP_H
 #define __SMP_H
 
+#include <linux/crypto.h>
+
 struct smp_command_hdr {
 	__u8	code;
 } __packed;
@@ -192,4 +194,24 @@ int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa);
 int smp_register(struct hci_dev *hdev);
 void smp_unregister(struct hci_dev *hdev);
 
+/* Exported crypto functions for self-test module */
+int bt_smp_c1(struct crypto_blkcipher *tfm_aes, const u8 k[16], const u8 r[16],
+	      const u8 preq[7], const u8 pres[7], u8 _iat, const bdaddr_t *ia,
+	      u8 _rat, const bdaddr_t *ra, u8 res[16]);
+int bt_smp_s1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
+	      const u8 r1[16], const u8 r2[16], u8 _r[16]);
+int bt_smp_ah(struct crypto_blkcipher *tfm, const u8 irk[16], const u8 r[3],
+	      u8 res[3]);
+int bt_smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
+	      const u8 x[16], u8 z, u8 res[16]);
+int bt_smp_f5(struct crypto_hash *tfm_cmac, u8 w[32], u8 n1[16], u8 n2[16],
+	      u8 a1[7], u8 a2[7], u8 mackey[16], u8 ltk[16]);
+int bt_smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16], const u8 n1[16],
+	      u8 n2[16], const u8 r[16], const u8 io_cap[3], const u8 a1[7],
+	      const u8 a2[7], u8 res[16]);
+int bt_smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
+	      const u8 x[16], const u8 y[16], u32 *val);
+int bt_smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16], const u8 key_id[4],
+	      u8 res[16]);
+
 #endif /* __SMP_H */
Signature · 1 line
2.1.0
#2 lore raw

PATCH 2/6 Bluetooth: Export ECDH functions for self-test module

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

Similar to the SMP functions, this patch exports the two public ECDH functions so that a self-test module can access them.

---
 net/bluetooth/ecc.c | 2 ++
 1 file changed, 2 insertions(+)
net/bluetooth/ecc.c +2 -0
index e1709f8467ac..71d0a9c10775 100644
@@ -794,6 +794,7 @@ bool ecc_make_key(u8 public_key[64], u8 private_key[32])
 
 	return true;
 }
+EXPORT_SYMBOL(ecc_make_key);
 
 bool ecdh_shared_secret(const u8 public_key[64], const u8 private_key[32],
 		        u8 secret[32])
@@ -814,3 +815,4 @@ bool ecdh_shared_secret(const u8 public_key[64], const u8 private_key[32],
 
 	return !ecc_point_is_zero(&product);
 }
+EXPORT_SYMBOL(ecdh_shared_secret);
Signature · 1 line
2.1.0
#3 lore raw

PATCH 3/6 Bluetooth: Add skeleton for self-test module

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

This patch adds a basic skeleton for a new self-test module. The module will be used to implement various self-tests for the bluetooth subsystem. The first user will be SMP, for which it is of particular importance that this is as a separate module to avoid loading priority conflicts with the crypto subsystem and the rest of the Bluetooth subsystem.

---
 net/bluetooth/Kconfig    |  6 ++++++
 net/bluetooth/Makefile   | 15 +++++++++------
 net/bluetooth/selftest.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 65 insertions(+), 6 deletions(-)
 create mode 100644 net/bluetooth/selftest.c
net/bluetooth/Kconfig +6 -0
index 29bcafc41adf..be8f68f02d48 100644
@@ -64,4 +64,10 @@ config BT_6LOWPAN
 	help
 	  IPv6 compression over Bluetooth Low Energy.
 
+config BT_SELFTEST
+	bool "Run self-tests on boot"
+	depends on BT && DEBUG_KERNEL
+	help
+		Run self-tests during boot. Currently limited to SMP.
+
 source "drivers/bluetooth/Kconfig"
net/bluetooth/Makefile +9 -6
index 364cff1ad4b1..9765a68925b3 100644
@@ -2,15 +2,18 @@
 # Makefile for the Linux Bluetooth subsystem.
 #
 
-obj-$(CONFIG_BT)	+= bluetooth.o
-obj-$(CONFIG_BT_RFCOMM)	+= rfcomm/
-obj-$(CONFIG_BT_BNEP)	+= bnep/
-obj-$(CONFIG_BT_CMTP)	+= cmtp/
-obj-$(CONFIG_BT_HIDP)	+= hidp/
-obj-$(CONFIG_BT_6LOWPAN) += bluetooth_6lowpan.o
+obj-$(CONFIG_BT)		+= bluetooth.o
+obj-$(CONFIG_BT_RFCOMM)		+= rfcomm/
+obj-$(CONFIG_BT_BNEP)		+= bnep/
+obj-$(CONFIG_BT_CMTP)		+= cmtp/
+obj-$(CONFIG_BT_HIDP)		+= hidp/
+obj-$(CONFIG_BT_6LOWPAN)	+= bluetooth_6lowpan.o
+obj-$(CONFIG_BT_SELFTEST)	+= bluetooth_selftest.o
 
 bluetooth_6lowpan-y := 6lowpan.o
 
+bluetooth_selftest-y := selftest.o
+
 bluetooth-y := af_bluetooth.o hci_core.o hci_conn.o hci_event.o mgmt.o \
 	hci_sock.o hci_sysfs.o l2cap_core.o l2cap_sock.o smp.o sco.o lib.o \
 	a2mp.o amp.o ecc.o hci_request.o hci_debugfs.o
net/bluetooth/selftest.c new file +50 -0

50 lines not on this page · raw message

Signature · 1 line
2.1.0
#4 lore raw

PATCH v2 3/6 Bluetooth: Add skeleton for self-test module

From: Johan Hedberg <[email protected]>

This patch adds a basic skeleton for a new self-test module. The module will be used to implement various self-tests for the bluetooth subsystem. The first user will be SMP, for which it is of particular importance that this is as a separate module to avoid loading priority conflicts with the crypto subsystem and the rest of the Bluetooth subsystem.

---
v2: Use tristate instead of bool in Kconfig, depend on BT_LE and improve description.

 net/bluetooth/Kconfig    |  8 ++++++++
 net/bluetooth/Makefile   | 15 +++++++++------
 net/bluetooth/selftest.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 67 insertions(+), 6 deletions(-)
 create mode 100644 net/bluetooth/selftest.c
net/bluetooth/Kconfig +8 -0
index 29bcafc41adf..bdb30c7ac5d8 100644
@@ -64,4 +64,12 @@ config BT_6LOWPAN
 	help
 	  IPv6 compression over Bluetooth Low Energy.
 
+config BT_SELFTEST
+	tristate "Run self-tests on boot"
+	depends on BT_LE && DEBUG_KERNEL
+	help
+	  Self-tests for Bluetooth subsystem. Currently limited to SMP. This would
+	  only be of interest to developers hacking on code that's covered by the
+	  self-tests.
+
 source "drivers/bluetooth/Kconfig"
net/bluetooth/Makefile +9 -6
index 364cff1ad4b1..9765a68925b3 100644
@@ -2,15 +2,18 @@
 # Makefile for the Linux Bluetooth subsystem.
 #
 
-obj-$(CONFIG_BT)	+= bluetooth.o
-obj-$(CONFIG_BT_RFCOMM)	+= rfcomm/
-obj-$(CONFIG_BT_BNEP)	+= bnep/
-obj-$(CONFIG_BT_CMTP)	+= cmtp/
-obj-$(CONFIG_BT_HIDP)	+= hidp/
-obj-$(CONFIG_BT_6LOWPAN) += bluetooth_6lowpan.o
+obj-$(CONFIG_BT)		+= bluetooth.o
+obj-$(CONFIG_BT_RFCOMM)		+= rfcomm/
+obj-$(CONFIG_BT_BNEP)		+= bnep/
+obj-$(CONFIG_BT_CMTP)		+= cmtp/
+obj-$(CONFIG_BT_HIDP)		+= hidp/
+obj-$(CONFIG_BT_6LOWPAN)	+= bluetooth_6lowpan.o
+obj-$(CONFIG_BT_SELFTEST)	+= bluetooth_selftest.o
 
 bluetooth_6lowpan-y := 6lowpan.o
 
+bluetooth_selftest-y := selftest.o
+
 bluetooth-y := af_bluetooth.o hci_core.o hci_conn.o hci_event.o mgmt.o \
 	hci_sock.o hci_sysfs.o l2cap_core.o l2cap_sock.o smp.o sco.o lib.o \
 	a2mp.o amp.o ecc.o hci_request.o hci_debugfs.o
net/bluetooth/selftest.c new file +50 -0

50 lines not on this page · raw message

Signature · 1 line
2.1.0
#5 lore raw

PATCH 4/6 Bluetooth: Add legacy SMP self-tests

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

This patch implements self-tests for the legacy (non-Secure Connection) SMP crypto functions. The sample data has been taken from the core specification.

---
 net/bluetooth/selftest.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 121 insertions(+)
net/bluetooth/selftest.c +121 -0
index 394b05427ad1..0816680c6037 100644
@@ -30,10 +30,131 @@
 
 #define VERSION "0.1"
 
+static int __init test_ah(struct crypto_blkcipher *tfm_aes)
+{
+	u8 irk[16] = {	0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
+			0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
+	u8 r[3] = {	0x94, 0x81, 0x70 };
+	u8 exp[3] = {	0xaa, 0xfb, 0x0d };
+	u8 res[3];
+	int err;
+
+	err = bt_smp_ah(tfm_aes, irk, r, res);
+	if (err)
+		return err;
+
+	if (memcmp(res, exp, 3) != 0)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int __init test_c1(struct crypto_blkcipher *tfm_aes)
+{
+	u8 k[16] = {	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
+	u8 r[16] = {	0xe0, 0x2e, 0x70, 0xc6, 0x4e, 0x27, 0x88, 0x63,
+			0x0e, 0x6f, 0xad, 0x56, 0x21, 0xd5, 0x83, 0x57 };
+	u8 preq[7] = {	0x01, 0x01, 0x00, 0x00, 0x10, 0x07, 0x07 };
+	u8 pres[7] = {	0x02, 0x03, 0x00, 0x00, 0x08, 0x00, 0x05 };
+	u8 _iat =	0x01;
+	u8 _rat =	0x00;
+	bdaddr_t ra = { { 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1 } };
+	bdaddr_t ia = { { 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1 } };
+	u8 exp[16] = {	0x86, 0x3b, 0xf1, 0xbe, 0xc5, 0x4d, 0xa7, 0xd2,
+			0xea, 0x88, 0x89, 0x87, 0xef, 0x3f, 0x1e, 0x1e };
+	u8 res[16];
+	int err;
+
+	err = bt_smp_c1(tfm_aes, k, r, preq, pres, _iat, &ia, _rat, &ra, res);
+	if (err)
+		return err;
+
+	if (memcmp(res, exp, 16) != 0)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int __init test_s1(struct crypto_blkcipher *tfm_aes)
+{
+	u8 k[16] = {	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+			0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
+	u8 r1[16] = {	0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };
+	u8 r2[16] = {	0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99 };
+	u8 exp[16] = {	0x62, 0xa0, 0x6d, 0x79, 0xae, 0x16, 0x42, 0x5b,
+			0x9b, 0xf4, 0xb0, 0xe8, 0xf0, 0xe1, 0x1f, 0x9a };
+	u8 res[16];
+	int err;
+
+	err = bt_smp_s1(tfm_aes, k, r1, r2, res);
+	if (err)
+		return err;
+
+	if (memcmp(res, exp, 16) != 0)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int __init test_smp(void)
+{
+	struct crypto_blkcipher *tfm_aes;
+	struct crypto_hash *tfm_cmac;
+	int err;
+
+	tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
+	if (IS_ERR(tfm_aes)) {
+		BT_ERR("Unable to create ECB crypto context");
+		return PTR_ERR(tfm_aes);
+	}
+
+	tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
+	if (IS_ERR(tfm_cmac)) {
+		BT_ERR("Unable to create CMAC crypto context");
+		crypto_free_blkcipher(tfm_aes);
+		return PTR_ERR(tfm_cmac);
+	}
+
+	err = test_ah(tfm_aes);
+	if (err) {
+		BT_ERR("smp_ah test failed");
+		goto failed;
+	}
+
+	err = test_c1(tfm_aes);
+	if (err) {
+		BT_ERR("smp_c1 test failed");
+		goto failed;
+	}
+
+	err = test_s1(tfm_aes);
+	if (err) {
+		BT_ERR("smp_s1 test failed");
+		goto failed;
+	}
+
+failed:
+	crypto_free_hash(tfm_cmac);
+	crypto_free_blkcipher(tfm_aes);
+
+	return err;
+}
+
 static int __init selftest_init(void)
 {
+	int err;
+
 	BT_INFO("Bluetooth self-tests version %s", VERSION);
 
+	err = test_smp();
+	if (err) {
+		BT_ERR("Bluetooth SMP self-tests failed");
+		return err;
+	}
+
+	BT_INFO("Bluetooth self-tests succeeded");
+
 	return 0;
 }
 
Signature · 1 line
2.1.0
#6 lore raw

PATCH 5/6 Bluetooth: Add LE SC SMP self-tests

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

This patch adds self-tests for the SMP crypto functions that are part of the LE Secure Connections feature. The sample data has been taken from the core specification.

---
 net/bluetooth/selftest.c | 167 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 167 insertions(+)
net/bluetooth/selftest.c +167 -0
index 0816680c6037..a474f07a9167 100644
@@ -97,6 +97,143 @@ static int __init test_s1(struct crypto_blkcipher *tfm_aes)
 	return 0;
 }
 
+static int __init test_f4(struct crypto_hash *tfm_cmac)
+{
+	u8 u[32] = {	0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
+			0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
+			0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
+			0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
+	u8 v[32] = {	0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
+			0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
+			0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
+			0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
+	u8 x[16] = {	0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
+			0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
+	u8 z = 0x00;
+	u8 exp[16] = {	0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
+			0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
+	u8 res[16];
+	int err;
+
+	err = bt_smp_f4(tfm_cmac, u, v, x, z, res);
+	if (err)
+		return err;
+
+	if (memcmp(res, exp, 16) != 0)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int __init test_f5(struct crypto_hash *tfm_cmac)
+{
+	u8 w[32] = {	0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
+			0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
+			0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
+			0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
+	u8 n1[16] = {	0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
+			0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
+	u8 n2[16] = {	0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
+			0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
+	u8 a1[7] = {	0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
+	u8 a2[7] = {	0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
+	u8 exp_ltk[16] = {
+			0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98,
+			0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69 };
+	u8 exp_mackey[16] = {
+			0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
+			0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
+	u8 mackey[16], ltk[16];
+	int err;
+
+	err = bt_smp_f5(tfm_cmac, w, n1, n2, a1, a2, mackey, ltk);
+	if (err)
+		return err;
+
+	if (memcmp(mackey, exp_mackey, 16) != 0)
+		return -EINVAL;
+
+	if (memcmp(ltk, exp_ltk, 16) != 0)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int __init test_f6(struct crypto_hash *tfm_cmac)
+{
+	u8 w[16] = {	0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
+			0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
+	u8 n1[16] = {	0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
+			0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
+	u8 n2[16] = {	0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
+			0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
+	u8 r[16] = {	0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
+			0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
+	u8 io_cap[3] = { 0x02, 0x01, 0x01 };
+	u8 a1[7] = {	0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
+	u8 a2[7] = {	0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
+	u8 exp[16] = {	0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
+			0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
+	u8 res[16];
+	int err;
+
+	err = bt_smp_f6(tfm_cmac, w, n1, n2, r, io_cap, a1, a2, res);
+	if (err)
+		return err;
+
+	if (memcmp(res, exp, 16) != 0)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int __init test_g2(struct crypto_hash *tfm_cmac)
+{
+	u8 u[32] = {	0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
+			0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
+			0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
+			0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
+	u8 v[32] = {	0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
+			0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
+			0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
+			0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
+	u8 x[16] = {	0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
+			0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
+	u8 y[16] = {	0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
+			0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
+	u32 val, exp_val = 0x2f9ed5ba % 1000000;
+	int err;
+
+	err = bt_smp_g2(tfm_cmac, u, v, x, y, &val);
+	if (err)
+		return err;
+
+	if (val != exp_val)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int __init test_h6(struct crypto_hash *tfm_cmac)
+{
+	u8 w[16] = {	0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
+			0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
+	u8 key_id[4] = { 0x72, 0x62, 0x65, 0x6c };
+	u8 exp[16] = {	0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8,
+			0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d };
+	u8 res[16];
+	int err;
+
+	err = bt_smp_h6(tfm_cmac, w, key_id, res);
+	if (err)
+		return err;
+
+	if (memcmp(res, exp, 16) != 0)
+		return -EINVAL;
+
+	return 0;
+}
+
 static int __init test_smp(void)
 {
 	struct crypto_blkcipher *tfm_aes;
@@ -134,6 +271,36 @@ static int __init test_smp(void)
 		goto failed;
 	}
 
+	err = test_f4(tfm_cmac);
+	if (err) {
+		BT_ERR("smp_f4 test failed");
+		goto failed;
+	}
+
+	err = test_f5(tfm_cmac);
+	if (err) {
+		BT_ERR("smp_f5 test failed");
+		goto failed;
+	}
+
+	err = test_f6(tfm_cmac);
+	if (err) {
+		BT_ERR("smp_f6 test failed");
+		goto failed;
+	}
+
+	err = test_g2(tfm_cmac);
+	if (err) {
+		BT_ERR("smp_g2 test failed");
+		goto failed;
+	}
+
+	err = test_h6(tfm_cmac);
+	if (err) {
+		BT_ERR("smp_h6 test failed");
+		goto failed;
+	}
+
 failed:
 	crypto_free_hash(tfm_cmac);
 	crypto_free_blkcipher(tfm_aes);
Signature · 1 line
2.1.0
#7 lore raw

PATCH 6/6 Bluetooth: Add ECDH self-tests

In reply to Johan Hedberg

From: Johan Hedberg <[email protected]>

This patch adds basic self-tests for the ECDH implementation used by the LE Secure Connections feature of SMP.

---
 net/bluetooth/selftest.c | 156 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 156 insertions(+)
net/bluetooth/selftest.c +156 -0
index a474f07a9167..d5ca26e082c0 100644
@@ -27,6 +27,7 @@
 #include <net/bluetooth/hci_core.h>
 
 #include "smp.h"
+#include "ecc.h"
 
 #define VERSION "0.1"
 
@@ -234,6 +235,155 @@ static int __init test_h6(struct crypto_hash *tfm_cmac)
 	return 0;
 }
 
+static const u8 __initconst priv_a_1[32] = {
+	0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
+	0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
+	0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
+	0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
+};
+static const u8 __initconst priv_b_1[32] = {
+	0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
+	0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
+	0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
+	0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55,
+};
+static const u8 __initconst pub_a_1[64] = {
+	0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
+	0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
+	0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
+	0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
+
+	0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
+	0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
+	0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
+	0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
+};
+static const u8 __initconst pub_b_1[64] = {
+	0x90, 0xa1, 0xaa, 0x2f, 0xb2, 0x77, 0x90, 0x55,
+	0x9f, 0xa6, 0x15, 0x86, 0xfd, 0x8a, 0xb5, 0x47,
+	0x00, 0x4c, 0x9e, 0xf1, 0x84, 0x22, 0x59, 0x09,
+	0x96, 0x1d, 0xaf, 0x1f, 0xf0, 0xf0, 0xa1, 0x1e,
+
+	0x4a, 0x21, 0xb1, 0x15, 0xf9, 0xaf, 0x89, 0x5f,
+	0x76, 0x36, 0x8e, 0xe2, 0x30, 0x11, 0x2d, 0x47,
+	0x60, 0x51, 0xb8, 0x9a, 0x3a, 0x70, 0x56, 0x73,
+	0x37, 0xad, 0x9d, 0x42, 0x3e, 0xf3, 0x55, 0x4c,
+};
+static const u8 __initconst dhkey_1[32] = {
+	0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
+	0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
+	0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
+	0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec,
+};
+
+static const u8 __initconst priv_a_2[32] = {
+	0x63, 0x76, 0x45, 0xd0, 0xf7, 0x73, 0xac, 0xb7,
+	0xff, 0xdd, 0x03, 0x72, 0xb9, 0x72, 0x85, 0xb4,
+	0x41, 0xb6, 0x5d, 0x0c, 0x5d, 0x54, 0x84, 0x60,
+	0x1a, 0xa3, 0x9a, 0x3c, 0x69, 0x16, 0xa5, 0x06,
+};
+static const u8 __initconst priv_b_2[32] = {
+	0xba, 0x30, 0x55, 0x50, 0x19, 0xa2, 0xca, 0xa3,
+	0xa5, 0x29, 0x08, 0xc6, 0xb5, 0x03, 0x88, 0x7e,
+	0x03, 0x2b, 0x50, 0x73, 0xd4, 0x2e, 0x50, 0x97,
+	0x64, 0xcd, 0x72, 0x0d, 0x67, 0xa0, 0x9a, 0x52,
+};
+static const u8 __initconst pub_a_2[64] = {
+	0xdd, 0x78, 0x5c, 0x74, 0x03, 0x9b, 0x7e, 0x98,
+	0xcb, 0x94, 0x87, 0x4a, 0xad, 0xfa, 0xf8, 0xd5,
+	0x43, 0x3e, 0x5c, 0xaf, 0xea, 0xb5, 0x4c, 0xf4,
+	0x9e, 0x80, 0x79, 0x57, 0x7b, 0xa4, 0x31, 0x2c,
+
+	0x4f, 0x5d, 0x71, 0x43, 0x77, 0x43, 0xf8, 0xea,
+	0xd4, 0x3e, 0xbd, 0x17, 0x91, 0x10, 0x21, 0xd0,
+	0x1f, 0x87, 0x43, 0x8e, 0x40, 0xe2, 0x52, 0xcd,
+	0xbe, 0xdf, 0x98, 0x38, 0x18, 0x12, 0x95, 0x91,
+};
+static const u8 __initconst pub_b_2[64] = {
+	0xcc, 0x00, 0x65, 0xe1, 0xf5, 0x6c, 0x0d, 0xcf,
+	0xec, 0x96, 0x47, 0x20, 0x66, 0xc9, 0xdb, 0x84,
+	0x81, 0x75, 0xa8, 0x4d, 0xc0, 0xdf, 0xc7, 0x9d,
+	0x1b, 0x3f, 0x3d, 0xf2, 0x3f, 0xe4, 0x65, 0xf4,
+
+	0x79, 0xb2, 0xec, 0xd8, 0xca, 0x55, 0xa1, 0xa8,
+	0x43, 0x4d, 0x6b, 0xca, 0x10, 0xb0, 0xc2, 0x01,
+	0xc2, 0x33, 0x4e, 0x16, 0x24, 0xc4, 0xef, 0xee,
+	0x99, 0xd8, 0xbb, 0xbc, 0x48, 0xd0, 0x01, 0x02,
+};
+static const u8 __initconst dhkey_2[32] = {
+	0x69, 0xeb, 0x21, 0x32, 0xf2, 0xc6, 0x05, 0x41,
+	0x60, 0x19, 0xcd, 0x5e, 0x94, 0xe1, 0xe6, 0x5f,
+	0x33, 0x07, 0xe3, 0x38, 0x4b, 0x68, 0xe5, 0x62,
+	0x3f, 0x88, 0x6d, 0x2f, 0x3a, 0x84, 0x85, 0xab,
+};
+
+static const u8 __initconst priv_a_3[32] = {
+	0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
+	0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
+	0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
+	0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
+};
+static const u8 __initconst pub_a_3[64] = {
+	0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
+	0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
+	0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
+	0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
+
+	0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
+	0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
+	0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
+	0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
+};
+static const u8 __initconst dhkey_3[32] = {
+	0x2d, 0xab, 0x00, 0x48, 0xcb, 0xb3, 0x7b, 0xda,
+	0x55, 0x7b, 0x8b, 0x72, 0xa8, 0x57, 0x87, 0xc3,
+	0x87, 0x27, 0x99, 0x32, 0xfc, 0x79, 0x5f, 0xae,
+	0x7c, 0x1c, 0xf9, 0x49, 0xe6, 0xd7, 0xaa, 0x70,
+};
+
+static int __init test_ecdh_sample(const u8 priv_a[32], const u8 priv_b[32],
+				   const u8 pub_a[64], const u8 pub_b[64],
+				   const u8 dhkey[32])
+{
+	u8 dhkey_a[32], dhkey_b[32];
+
+	ecdh_shared_secret(pub_b, priv_a, dhkey_a);
+	ecdh_shared_secret(pub_a, priv_b, dhkey_b);
+
+	if (memcmp(dhkey_a, dhkey, 32) != 0)
+		return -EINVAL;
+
+	if (memcmp(dhkey_b, dhkey, 32) != 0)
+		return -EINVAL;
+
+	return 0;
+}
+
+static int __init test_ecdh(void)
+{
+	int err;
+
+	err = test_ecdh_sample(priv_a_1, priv_b_1, pub_a_1, pub_b_1, dhkey_1);
+	if (err) {
+		BT_ERR("ECDH sample 1 failed");
+		return err;
+	}
+
+	err = test_ecdh_sample(priv_a_2, priv_b_2, pub_a_2, pub_b_2, dhkey_2);
+	if (err) {
+		BT_ERR("ECDH sample 2 failed");
+		return err;
+	}
+
+	err = test_ecdh_sample(priv_a_3, priv_a_3, pub_a_3, pub_a_3, dhkey_3);
+	if (err) {
+		BT_ERR("ECDH sample 3 failed");
+		return err;
+	}
+
+	return 0;
+}
+
 static int __init test_smp(void)
 {
 	struct crypto_blkcipher *tfm_aes;
@@ -301,6 +451,12 @@ static int __init test_smp(void)
 		goto failed;
 	}
 
+	err = test_ecdh();
+	if (err) {
+		BT_ERR("ECDH test failed");
+		goto failed;
+	}
+
 failed:
 	crypto_free_hash(tfm_cmac);
 	crypto_free_blkcipher(tfm_aes);
Signature · 1 line
2.1.0