Bluetooth: SMP: Add local random number generation support
The SMP procedures require us to establish a local random number. Use the HCI_LE_Rand command to generate the necessary 16 bytes (the command needs to be called twice since it only returns 8 bytes at a time). Store this number in the SMP context. To help debugging with various crypto related values add a local h() helper function for printk/BT_DBG to do hexadecimal conversion. Change-Id: Id3e8793a77ac635023b3cc3a94e8679170455870 Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
parent
653378d031
commit
a648bbe9a1
3 changed files with 101 additions and 6 deletions
|
@ -238,6 +238,12 @@ struct bt_hci_rp_le_encrypt {
|
|||
uint8_t enc_data[16];
|
||||
} PACK_STRUCT;
|
||||
|
||||
#define BT_HCI_OP_LE_RAND BT_OP(BT_OGF_LE, 0x0018)
|
||||
struct bt_hci_rp_le_rand {
|
||||
uint8_t status;
|
||||
uint8_t rand[8];
|
||||
} PACK_STRUCT;
|
||||
|
||||
/* Event definitions */
|
||||
|
||||
#define BT_HCI_EVT_DISCONN_COMPLETE 0x05
|
||||
|
|
|
@ -49,6 +49,8 @@ struct bt_conn_att {
|
|||
|
||||
/* SMP channel specific context */
|
||||
struct bt_conn_smp {
|
||||
/* Local random number */
|
||||
uint8_t prnd[16];
|
||||
};
|
||||
|
||||
struct bt_conn {
|
||||
|
|
|
@ -35,6 +35,9 @@
|
|||
|
||||
#include <nanokernel.h>
|
||||
#include <stddef.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <misc/util.h>
|
||||
|
||||
#include <bluetooth/hci.h>
|
||||
#include <bluetooth/bluetooth.h>
|
||||
|
@ -44,11 +47,71 @@
|
|||
#include "l2cap.h"
|
||||
#include "smp.h"
|
||||
|
||||
#if !defined(CONFIG_BLUETOOTH_DEBUG_SMP)
|
||||
#if defined(CONFIG_BLUETOOTH_DEBUG_SMP)
|
||||
|
||||
/* Helper for printk parameters to convert from binary to hex.
|
||||
* We declare multiple buffers so the helper can be used multiple times
|
||||
* in a single printk call.
|
||||
*/
|
||||
static const char *h(const void *buf, size_t len)
|
||||
{
|
||||
static const char hex[] = "0123456789abcdef";
|
||||
static char hexbufs[4][129];
|
||||
static uint8_t curbuf;
|
||||
const uint8_t *b = buf;
|
||||
char *str;
|
||||
int i;
|
||||
|
||||
str = hexbufs[curbuf++];
|
||||
curbuf %= ARRAY_SIZE(hexbufs);
|
||||
|
||||
len = min(len, (sizeof(hexbufs[0]) - 1) / 2);
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
str[i * 2] = hex[b[i] >> 4];
|
||||
str[i * 2 + 1] = hex[b[i] & 0xf];
|
||||
}
|
||||
|
||||
str[i * 2] = '\0';
|
||||
|
||||
return str;
|
||||
}
|
||||
#else
|
||||
#undef BT_DBG
|
||||
#define BT_DBG(fmt, ...)
|
||||
#endif
|
||||
|
||||
static int le_rand(uint8_t rand[16])
|
||||
{
|
||||
struct bt_hci_rp_le_rand *rp;
|
||||
struct bt_buf *rsp;
|
||||
int err;
|
||||
|
||||
/* First 8 bytes */
|
||||
err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_RAND, NULL, &rsp);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
rp = (void *)rsp->data;
|
||||
memcpy(rand, rp->rand, 8);
|
||||
bt_buf_put(rsp);
|
||||
|
||||
/* Second 8 bytes */
|
||||
err = bt_hci_cmd_send_sync(BT_HCI_OP_LE_RAND, NULL, &rsp);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
rp = (void *)rsp->data;
|
||||
memcpy(rand + 8, rp->rand, 8);
|
||||
bt_buf_put(rsp);
|
||||
|
||||
BT_DBG("rand %s\n", h(rand, 16));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct bt_buf *bt_smp_create_pdu(struct bt_conn *conn, uint8_t op, size_t len)
|
||||
{
|
||||
struct bt_smp_hdr *hdr;
|
||||
|
@ -79,26 +142,50 @@ static void send_err_rsp(struct bt_conn *conn, uint8_t reason)
|
|||
bt_conn_send(conn, buf);
|
||||
}
|
||||
|
||||
static int smp_init(struct bt_conn_smp *smp)
|
||||
{
|
||||
/* Initialize SMP context */
|
||||
memset(smp, 0, sizeof(*smp));
|
||||
|
||||
/* Generate local random number */
|
||||
if (le_rand(smp->prnd)) {
|
||||
return BT_SMP_ERR_UNSPECIFIED;
|
||||
}
|
||||
|
||||
BT_DBG("prnd %s\n", h(smp->prnd, 16));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int smp_pairing_req(struct bt_conn *conn, struct bt_buf *buf)
|
||||
{
|
||||
struct bt_smp_pairing *req = (void *)buf->data;
|
||||
struct bt_smp_pairing *rsp;
|
||||
struct bt_buf *rsp_buf;
|
||||
struct bt_conn_smp *smp = &conn->smp;
|
||||
int ret;
|
||||
|
||||
BT_DBG("\n");
|
||||
|
||||
if (buf->len != sizeof(*req))
|
||||
if (buf->len != sizeof(*req)) {
|
||||
return BT_SMP_ERR_INVALID_PARAMS;
|
||||
|
||||
rsp_buf = bt_smp_create_pdu(conn, BT_SMP_CMD_PAIRING_RSP, sizeof(*rsp));
|
||||
if (!rsp_buf)
|
||||
return BT_SMP_ERR_UNSPECIFIED;
|
||||
}
|
||||
|
||||
if ((req->max_key_size > BT_SMP_MAX_ENC_KEY_SIZE) ||
|
||||
(req->max_key_size < BT_SMP_MIN_ENC_KEY_SIZE)) {
|
||||
return BT_SMP_ERR_ENC_KEY_SIZE;
|
||||
}
|
||||
|
||||
ret = smp_init(smp);
|
||||
if (ret) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
rsp_buf = bt_smp_create_pdu(conn, BT_SMP_CMD_PAIRING_RSP, sizeof(*rsp));
|
||||
if (!rsp_buf) {
|
||||
return BT_SMP_ERR_UNSPECIFIED;
|
||||
}
|
||||
|
||||
rsp = bt_buf_add(rsp_buf, sizeof(*rsp));
|
||||
|
||||
/* For JustWorks pairing simplify rsp parameters.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue