33 #define SMP_TIMEOUT msecs_to_jiffies(30000)
35 #define AUTH_REQ_MASK 0x07
37 static inline void swap128(
u8 src[16],
u8 dst[16])
40 for (i = 0; i < 16; i++)
44 static inline void swap56(
u8 src[7],
u8 dst[7])
47 for (i = 0; i < 7; i++)
56 unsigned char iv[128];
66 err = crypto_blkcipher_setkey(tfm, k, 16);
68 BT_ERR(
"cipher setkey failed: %d", err);
74 iv_len = crypto_blkcipher_ivsize(tfm);
77 crypto_blkcipher_set_iv(tfm, iv, iv_len);
80 err = crypto_blkcipher_encrypt(&
desc, &
sg, &
sg, 16);
82 BT_ERR(
"Encrypt data error %d", err);
88 u8 preq[7],
u8 pres[7],
u8 _iat, bdaddr_t *ia,
105 baswap((bdaddr_t *) (p2 + 4), ia);
106 baswap((bdaddr_t *) (p2 + 10), ra);
112 err = smp_e(tfm, k, res);
114 BT_ERR(
"Encrypt data error");
122 err = smp_e(tfm, k, res);
124 BT_ERR(
"Encrypt data error");
136 memcpy(_r + 8, r2 + 8, 8);
138 err = smp_e(tfm, k, _r);
140 BT_ERR(
"Encrypt data error");
145 static int smp_rand(
u8 *
buf)
179 static void smp_send_cmd(
struct l2cap_conn *conn,
u8 code,
u16 len,
void *data)
181 struct sk_buff *skb = smp_build_cmd(conn, code, len, data);
183 BT_DBG(
"code 0x%2.2x", code);
195 static __u8 authreq_to_seclevel(
__u8 authreq)
203 static __u8 seclevel_to_authreq(
__u8 sec_level)
215 static void build_pairing_cmd(
struct l2cap_conn *conn,
278 #define JUST_WORKS 0x00
279 #define JUST_CFM 0x01
280 #define REQ_PASSKEY 0x02
281 #define CFM_PASSKEY 0x03
285 static const u8 gen_method[5][5] = {
294 u8 local_io,
u8 remote_io)
306 BT_DBG(
"tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
316 method = gen_method[remote_io][local_io];
346 memset(key, 0,
sizeof(key));
350 swap128(key, smp->
tk);
351 BT_DBG(
"PassKey: %d", passkey);
390 conn->
src, conn->
hcon->dst_type, conn->
dst, res);
393 conn->
hcon->dst_type, conn->
dst, 0, conn->
src,
402 swap128(res,
cp.confirm_val);
408 smp_failure(conn, reason, 1);
420 if (IS_ERR_OR_NULL(tfm)) {
425 BT_DBG(
"conn %p %s", conn, conn->
hcon->out ?
"master" :
"slave");
438 swap128(res, confirm);
441 BT_ERR(
"Pairing failed (confirmation values mismatch)");
450 memset(rand, 0,
sizeof(rand));
453 smp_s1(tfm, smp->
tk, smp->
rrnd, smp->
prnd, key);
467 u8 stk[16], r[16], rand[8];
470 memset(rand, 0,
sizeof(rand));
473 swap128(smp->
prnd, r);
476 smp_s1(tfm, smp->
tk, smp->
prnd, smp->
rrnd, key);
490 smp_failure(conn, reason, 1);
508 hci_conn_hold(conn->
hcon);
520 crypto_free_blkcipher(smp->
tfm);
525 hci_conn_put(conn->
hcon);
545 memset(key, 0,
sizeof(key));
546 BT_DBG(
"PassKey: %d", value);
548 swap128(key, smp->
tk);
583 smp = smp_chan_create(conn);
595 if (req->
auth_req & SMP_AUTH_BONDING)
598 conn->
hcon->pending_sec_level = authreq_to_seclevel(auth);
600 build_pairing_cmd(conn, req, &rsp, auth);
603 if (check_enc_key_size(conn,
key_size))
606 ret = smp_rand(smp->
prnd);
638 req = (
void *) &smp->
preq[1];
641 if (check_enc_key_size(conn, key_size))
644 ret = smp_rand(smp->
prnd);
651 if ((req->
auth_req & SMP_AUTH_BONDING) &&
677 BT_DBG(
"conn %p %s", conn, conn->
hcon->out ?
"master" :
"slave");
682 if (conn->
hcon->out) {
685 swap128(smp->
prnd, random);
750 smp = smp_chan_create(conn);
771 BT_DBG(
"conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
783 if (smp_ltk_encrypt(conn, sec_level))
789 smp = smp_chan_create(conn);
793 authreq = seclevel_to_authreq(sec_level);
798 build_pairing_cmd(conn, &cp,
NULL, authreq);
864 reason = smp_cmd_pairing_req(conn, skb);
868 smp_failure(conn, skb->
data[0], 0);
874 reason = smp_cmd_pairing_rsp(conn, skb);
905 BT_DBG(
"Unknown command code 0x%2.2x", code);
914 smp_failure(conn, reason, 1);
926 BT_DBG(
"conn %p force %d", conn, force);
931 rsp = (
void *) &smp->
prsp[1];
937 req = (
void *) &smp->
preq[1];
948 BT_DBG(
"keydist 0x%x", *keydist);
972 *keydist &= ~SMP_DIST_ENC_KEY;
985 memset(&addrinfo, 0,
sizeof(addrinfo));
991 *keydist &= ~SMP_DIST_ID_KEY;
1002 *keydist &= ~SMP_DIST_SIGN;
1005 if (conn->
hcon->out || force) {