blob: 4bfaa3d3ed289c338766dd34df8561521c0200d6 [file] [log] [blame]
Anderson Brigliaeb492e02011-06-09 18:50:40 -03001/*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation;
8
9 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
10 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
12 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
13 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
19 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
20 SOFTWARE IS DISCLAIMED.
21*/
22
Marcel Holtmann300acfde2014-12-31 14:43:16 -080023#include <linux/debugfs.h>
Gustavo Padovan8c520a52012-05-23 04:04:22 -030024#include <linux/crypto.h>
25#include <linux/scatterlist.h>
26#include <crypto/b128ops.h>
27
Anderson Brigliaeb492e02011-06-09 18:50:40 -030028#include <net/bluetooth/bluetooth.h>
29#include <net/bluetooth/hci_core.h>
30#include <net/bluetooth/l2cap.h>
Brian Gix2b64d152011-12-21 16:12:12 -080031#include <net/bluetooth/mgmt.h>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070032
Johan Hedberg3b191462014-06-06 10:50:15 +030033#include "ecc.h"
Marcel Holtmannac4b7232013-10-10 14:54:16 -070034#include "smp.h"
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030035
Johan Hedbergc7a3d572014-12-01 22:03:16 +020036/* Low-level debug macros to be used for stuff that we don't want
37 * accidentially in dmesg, i.e. the values of the various crypto keys
38 * and the inputs & outputs of crypto functions.
39 */
40#ifdef DEBUG
41#define SMP_DBG(fmt, ...) printk(KERN_DEBUG "%s: " fmt, __func__, \
42 ##__VA_ARGS__)
43#else
44#define SMP_DBG(fmt, ...) no_printk(KERN_DEBUG "%s: " fmt, __func__, \
45 ##__VA_ARGS__)
46#endif
47
Johan Hedbergb28b4942014-09-05 22:19:55 +030048#define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
Johan Hedbergb28b4942014-09-05 22:19:55 +030049
Johan Hedberg3b191462014-06-06 10:50:15 +030050/* Keys which are not distributed with Secure Connections */
51#define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
52
Marcel Holtmann17b02e62012-03-01 14:32:37 -080053#define SMP_TIMEOUT msecs_to_jiffies(30000)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -030054
Marcel Holtmannd7a5a112015-03-13 02:11:00 -070055#define AUTH_REQ_MASK(dev) (hci_dev_test_flag(dev, HCI_SC_ENABLED) ? \
Johan Hedberg0edb14d2014-05-26 13:29:28 +030056 0x1f : 0x07)
57#define KEY_DIST_MASK 0x07
Johan Hedberg065a13e2012-10-11 16:26:06 +020058
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +030059/* Maximum message length that can be passed to aes_cmac */
60#define CMAC_MSG_MAX 80
61
Johan Hedberg533e35d2014-06-16 19:25:18 +030062enum {
63 SMP_FLAG_TK_VALID,
64 SMP_FLAG_CFM_PENDING,
65 SMP_FLAG_MITM_AUTH,
66 SMP_FLAG_COMPLETE,
67 SMP_FLAG_INITIATOR,
Johan Hedberg65668772014-05-16 11:03:34 +030068 SMP_FLAG_SC,
Johan Hedbergd8f8edb2014-06-06 11:09:28 +030069 SMP_FLAG_REMOTE_PK,
Johan Hedbergaeb7d462014-05-31 18:52:28 +030070 SMP_FLAG_DEBUG_KEY,
Johan Hedberg38606f12014-06-25 11:10:28 +030071 SMP_FLAG_WAIT_USER,
Johan Hedbergd3e54a82014-06-04 11:07:40 +030072 SMP_FLAG_DHKEY_PENDING,
Johan Hedberg1a8bab42015-03-16 11:45:44 +020073 SMP_FLAG_REMOTE_OOB,
74 SMP_FLAG_LOCAL_OOB,
Johan Hedberg533e35d2014-06-16 19:25:18 +030075};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030076
Marcel Holtmann88a479d2015-03-16 01:10:19 -070077struct smp_dev {
Marcel Holtmann60a27d62015-03-16 01:10:22 -070078 /* Secure Connections OOB data */
79 u8 local_pk[64];
80 u8 local_sk[32];
Marcel Holtmannfb334fe2015-03-16 12:34:57 -070081 u8 local_rand[16];
Marcel Holtmann60a27d62015-03-16 01:10:22 -070082 bool debug_key;
83
Marcel Holtmann88a479d2015-03-16 01:10:19 -070084 struct crypto_blkcipher *tfm_aes;
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -070085 struct crypto_hash *tfm_cmac;
Marcel Holtmann88a479d2015-03-16 01:10:19 -070086};
87
Johan Hedberg4bc58f52014-05-20 09:45:47 +030088struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030089 struct l2cap_conn *conn;
90 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030091 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +030092
Johan Hedberg4bc58f52014-05-20 09:45:47 +030093 u8 preq[7]; /* SMP Pairing Request */
94 u8 prsp[7]; /* SMP Pairing Response */
95 u8 prnd[16]; /* SMP Pairing Random (local) */
96 u8 rrnd[16]; /* SMP Pairing Random (remote) */
97 u8 pcnf[16]; /* SMP Pairing Confirm */
98 u8 tk[16]; /* SMP Temporary Key */
Johan Hedberg882fafa2015-03-16 11:45:43 +020099 u8 rr[16]; /* Remote OOB ra/rb value */
100 u8 lr[16]; /* Local OOB ra/rb value */
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300101 u8 enc_key_size;
102 u8 remote_key_dist;
103 bdaddr_t id_addr;
104 u8 id_addr_type;
105 u8 irk[16];
106 struct smp_csrk *csrk;
107 struct smp_csrk *slave_csrk;
108 struct smp_ltk *ltk;
109 struct smp_ltk *slave_ltk;
110 struct smp_irk *remote_irk;
Johan Hedberg6a770832014-06-06 11:54:04 +0300111 u8 *link_key;
Johan Hedberg4a74d652014-05-20 09:45:50 +0300112 unsigned long flags;
Johan Hedberg783e0572014-05-31 18:48:26 +0300113 u8 method;
Johan Hedberg38606f12014-06-25 11:10:28 +0300114 u8 passkey_round;
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300115
Johan Hedberg3b191462014-06-06 10:50:15 +0300116 /* Secure Connections variables */
117 u8 local_pk[64];
118 u8 local_sk[32];
Johan Hedbergd8f8edb2014-06-06 11:09:28 +0300119 u8 remote_pk[64];
120 u8 dhkey[32];
Johan Hedberg760b0182014-06-06 11:44:05 +0300121 u8 mackey[16];
Johan Hedberg3b191462014-06-06 10:50:15 +0300122
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300123 struct crypto_blkcipher *tfm_aes;
Johan Hedberg407cecf2014-05-02 14:19:47 +0300124 struct crypto_hash *tfm_cmac;
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300125};
126
Johan Hedbergaeb7d462014-05-31 18:52:28 +0300127/* These debug key values are defined in the SMP section of the core
128 * specification. debug_pk is the public debug key and debug_sk the
129 * private debug key.
130 */
131static const u8 debug_pk[64] = {
132 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
133 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
134 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
135 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
136
137 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
138 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
139 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
140 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
141};
142
143static const u8 debug_sk[32] = {
144 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
145 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
146 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
147 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
148};
149
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300150static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300151{
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300152 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300153
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300154 for (i = 0; i < len; i++)
155 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300156}
157
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200158/* The following functions map to the LE SC SMP crypto functions
159 * AES-CMAC, f4, f5, f6, g2 and h6.
160 */
161
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300162static int aes_cmac(struct crypto_hash *tfm, const u8 k[16], const u8 *m,
163 size_t len, u8 mac[16])
164{
165 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
166 struct hash_desc desc;
167 struct scatterlist sg;
168 int err;
169
170 if (len > CMAC_MSG_MAX)
171 return -EFBIG;
172
173 if (!tfm) {
174 BT_ERR("tfm %p", tfm);
175 return -EINVAL;
176 }
177
178 desc.tfm = tfm;
179 desc.flags = 0;
180
181 crypto_hash_init(&desc);
182
183 /* Swap key and message from LSB to MSB */
184 swap_buf(k, tmp, 16);
185 swap_buf(m, msg_msb, len);
186
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200187 SMP_DBG("msg (len %zu) %*phN", len, (int) len, m);
188 SMP_DBG("key %16phN", k);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300189
190 err = crypto_hash_setkey(tfm, tmp, 16);
191 if (err) {
192 BT_ERR("cipher setkey failed: %d", err);
193 return err;
194 }
195
196 sg_init_one(&sg, msg_msb, len);
197
198 err = crypto_hash_update(&desc, &sg, len);
199 if (err) {
200 BT_ERR("Hash update error %d", err);
201 return err;
202 }
203
204 err = crypto_hash_final(&desc, mac_msb);
205 if (err) {
206 BT_ERR("Hash final error %d", err);
207 return err;
208 }
209
210 swap_buf(mac_msb, mac, 16);
211
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200212 SMP_DBG("mac %16phN", mac);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300213
214 return 0;
215}
216
217static int smp_f4(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
218 const u8 x[16], u8 z, u8 res[16])
219{
220 u8 m[65];
221 int err;
222
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200223 SMP_DBG("u %32phN", u);
224 SMP_DBG("v %32phN", v);
225 SMP_DBG("x %16phN z %02x", x, z);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300226
227 m[0] = z;
228 memcpy(m + 1, v, 32);
229 memcpy(m + 33, u, 32);
230
231 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
232 if (err)
233 return err;
234
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200235 SMP_DBG("res %16phN", res);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300236
237 return err;
238}
239
Johan Hedberg4da50de2014-12-29 12:04:10 +0200240static int smp_f5(struct crypto_hash *tfm_cmac, const u8 w[32],
241 const u8 n1[16], const u8 n2[16], const u8 a1[7],
242 const u8 a2[7], u8 mackey[16], u8 ltk[16])
Johan Hedberg760b0182014-06-06 11:44:05 +0300243{
244 /* The btle, salt and length "magic" values are as defined in
245 * the SMP section of the Bluetooth core specification. In ASCII
246 * the btle value ends up being 'btle'. The salt is just a
247 * random number whereas length is the value 256 in little
248 * endian format.
249 */
250 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
251 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
252 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
253 const u8 length[2] = { 0x00, 0x01 };
254 u8 m[53], t[16];
255 int err;
256
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200257 SMP_DBG("w %32phN", w);
258 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
259 SMP_DBG("a1 %7phN a2 %7phN", a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300260
261 err = aes_cmac(tfm_cmac, salt, w, 32, t);
262 if (err)
263 return err;
264
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200265 SMP_DBG("t %16phN", t);
Johan Hedberg760b0182014-06-06 11:44:05 +0300266
267 memcpy(m, length, 2);
268 memcpy(m + 2, a2, 7);
269 memcpy(m + 9, a1, 7);
270 memcpy(m + 16, n2, 16);
271 memcpy(m + 32, n1, 16);
272 memcpy(m + 48, btle, 4);
273
274 m[52] = 0; /* Counter */
275
276 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
277 if (err)
278 return err;
279
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200280 SMP_DBG("mackey %16phN", mackey);
Johan Hedberg760b0182014-06-06 11:44:05 +0300281
282 m[52] = 1; /* Counter */
283
284 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
285 if (err)
286 return err;
287
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200288 SMP_DBG("ltk %16phN", ltk);
Johan Hedberg760b0182014-06-06 11:44:05 +0300289
290 return 0;
291}
292
293static int smp_f6(struct crypto_hash *tfm_cmac, const u8 w[16],
Johan Hedberg4da50de2014-12-29 12:04:10 +0200294 const u8 n1[16], const u8 n2[16], const u8 r[16],
Johan Hedberg760b0182014-06-06 11:44:05 +0300295 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
296 u8 res[16])
297{
298 u8 m[65];
299 int err;
300
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200301 SMP_DBG("w %16phN", w);
302 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
303 SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300304
305 memcpy(m, a2, 7);
306 memcpy(m + 7, a1, 7);
307 memcpy(m + 14, io_cap, 3);
308 memcpy(m + 17, r, 16);
309 memcpy(m + 33, n2, 16);
310 memcpy(m + 49, n1, 16);
311
312 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
313 if (err)
314 return err;
315
Marcel Holtmann203de212014-12-31 20:01:22 -0800316 SMP_DBG("res %16phN", res);
Johan Hedberg760b0182014-06-06 11:44:05 +0300317
318 return err;
319}
320
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300321static int smp_g2(struct crypto_hash *tfm_cmac, const u8 u[32], const u8 v[32],
322 const u8 x[16], const u8 y[16], u32 *val)
323{
324 u8 m[80], tmp[16];
325 int err;
326
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200327 SMP_DBG("u %32phN", u);
328 SMP_DBG("v %32phN", v);
329 SMP_DBG("x %16phN y %16phN", x, y);
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300330
331 memcpy(m, y, 16);
332 memcpy(m + 16, v, 32);
333 memcpy(m + 48, u, 32);
334
335 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
336 if (err)
337 return err;
338
339 *val = get_unaligned_le32(tmp);
340 *val %= 1000000;
341
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200342 SMP_DBG("val %06u", *val);
Johan Hedberg191dc7f2014-06-06 11:39:49 +0300343
344 return 0;
345}
346
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200347static int smp_h6(struct crypto_hash *tfm_cmac, const u8 w[16],
348 const u8 key_id[4], u8 res[16])
349{
350 int err;
351
352 SMP_DBG("w %16phN key_id %4phN", w, key_id);
353
354 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
355 if (err)
356 return err;
357
358 SMP_DBG("res %16phN", res);
359
360 return err;
361}
362
363/* The following functions map to the legacy SMP crypto functions e, c1,
364 * s1 and ah.
365 */
366
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300367static int smp_e(struct crypto_blkcipher *tfm, const u8 *k, u8 *r)
368{
369 struct blkcipher_desc desc;
370 struct scatterlist sg;
Johan Hedberg943a7322014-03-18 12:58:24 +0200371 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200372 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300373
Johan Hedberg011c3912015-05-19 21:06:04 +0300374 SMP_DBG("k %16phN r %16phN", k, r);
375
Johan Hedberg7f376cd2014-12-03 16:07:13 +0200376 if (!tfm) {
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300377 BT_ERR("tfm %p", tfm);
378 return -EINVAL;
379 }
380
381 desc.tfm = tfm;
382 desc.flags = 0;
383
Johan Hedberg943a7322014-03-18 12:58:24 +0200384 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300385 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200386
387 err = crypto_blkcipher_setkey(tfm, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300388 if (err) {
389 BT_ERR("cipher setkey failed: %d", err);
390 return err;
391 }
392
Johan Hedberg943a7322014-03-18 12:58:24 +0200393 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300394 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200395
396 sg_init_one(&sg, data, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300397
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300398 err = crypto_blkcipher_encrypt(&desc, &sg, &sg, 16);
399 if (err)
400 BT_ERR("Encrypt data error %d", err);
401
Johan Hedberg943a7322014-03-18 12:58:24 +0200402 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300403 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200404
Johan Hedberg011c3912015-05-19 21:06:04 +0300405 SMP_DBG("r %16phN", r);
406
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300407 return err;
408}
409
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200410static int smp_c1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
411 const u8 r[16], const u8 preq[7], const u8 pres[7], u8 _iat,
412 const bdaddr_t *ia, u8 _rat, const bdaddr_t *ra, u8 res[16])
413{
414 u8 p1[16], p2[16];
415 int err;
416
Johan Hedberg011c3912015-05-19 21:06:04 +0300417 SMP_DBG("k %16phN r %16phN", k, r);
418 SMP_DBG("iat %u ia %6phN rat %u ra %6phN", _iat, ia, _rat, ra);
419 SMP_DBG("preq %7phN pres %7phN", preq, pres);
420
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200421 memset(p1, 0, 16);
422
423 /* p1 = pres || preq || _rat || _iat */
424 p1[0] = _iat;
425 p1[1] = _rat;
426 memcpy(p1 + 2, preq, 7);
427 memcpy(p1 + 9, pres, 7);
428
Johan Hedberg011c3912015-05-19 21:06:04 +0300429 SMP_DBG("p1 %16phN", p1);
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200430
431 /* res = r XOR p1 */
432 u128_xor((u128 *) res, (u128 *) r, (u128 *) p1);
433
434 /* res = e(k, res) */
435 err = smp_e(tfm_aes, k, res);
436 if (err) {
437 BT_ERR("Encrypt data error");
438 return err;
439 }
440
Johan Hedberg011c3912015-05-19 21:06:04 +0300441 /* p2 = padding || ia || ra */
442 memcpy(p2, ra, 6);
443 memcpy(p2 + 6, ia, 6);
444 memset(p2 + 12, 0, 4);
445
446 SMP_DBG("p2 %16phN", p2);
447
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200448 /* res = res XOR p2 */
449 u128_xor((u128 *) res, (u128 *) res, (u128 *) p2);
450
451 /* res = e(k, res) */
452 err = smp_e(tfm_aes, k, res);
453 if (err)
454 BT_ERR("Encrypt data error");
455
456 return err;
457}
458
459static int smp_s1(struct crypto_blkcipher *tfm_aes, const u8 k[16],
460 const u8 r1[16], const u8 r2[16], u8 _r[16])
Johan Hedberg6a770832014-06-06 11:54:04 +0300461{
462 int err;
463
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200464 /* Just least significant octets from r1 and r2 are considered */
465 memcpy(_r, r2, 8);
466 memcpy(_r + 8, r1, 8);
Johan Hedberg6a770832014-06-06 11:54:04 +0300467
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200468 err = smp_e(tfm_aes, k, _r);
Johan Hedberg6a770832014-06-06 11:54:04 +0300469 if (err)
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200470 BT_ERR("Encrypt data error");
Johan Hedberg6a770832014-06-06 11:54:04 +0300471
472 return err;
473}
474
Johan Hedbergcd082792014-12-02 13:37:41 +0200475static int smp_ah(struct crypto_blkcipher *tfm, const u8 irk[16],
476 const u8 r[3], u8 res[3])
Johan Hedberg60478052014-02-18 10:19:31 +0200477{
Johan Hedberg943a7322014-03-18 12:58:24 +0200478 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200479 int err;
480
481 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200482 memcpy(_res, r, 3);
483 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200484
Johan Hedberg943a7322014-03-18 12:58:24 +0200485 err = smp_e(tfm, irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200486 if (err) {
487 BT_ERR("Encrypt error");
488 return err;
489 }
490
491 /* The output of the random address function ah is:
492 * ah(h, r) = e(k, r') mod 2^24
493 * The output of the security function e is then truncated to 24 bits
494 * by taking the least significant 24 bits of the output of e as the
495 * result of ah.
496 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200497 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200498
499 return 0;
500}
501
Johan Hedbergcd082792014-12-02 13:37:41 +0200502bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
503 const bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200504{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300505 struct l2cap_chan *chan = hdev->smp_data;
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700506 struct smp_dev *smp;
Johan Hedberg60478052014-02-18 10:19:31 +0200507 u8 hash[3];
508 int err;
509
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300510 if (!chan || !chan->data)
511 return false;
512
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700513 smp = chan->data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300514
Johan Hedberg60478052014-02-18 10:19:31 +0200515 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
516
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700517 err = smp_ah(smp->tfm_aes, irk, &bdaddr->b[3], hash);
Johan Hedberg60478052014-02-18 10:19:31 +0200518 if (err)
519 return false;
520
521 return !memcmp(bdaddr->b, hash, 3);
522}
523
Johan Hedbergcd082792014-12-02 13:37:41 +0200524int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200525{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300526 struct l2cap_chan *chan = hdev->smp_data;
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700527 struct smp_dev *smp;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200528 int err;
529
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300530 if (!chan || !chan->data)
531 return -EOPNOTSUPP;
532
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700533 smp = chan->data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300534
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200535 get_random_bytes(&rpa->b[3], 3);
536
537 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
538 rpa->b[5] |= 0x40; /* Set second most significant bit */
539
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700540 err = smp_ah(smp->tfm_aes, irk, &rpa->b[3], rpa->b);
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200541 if (err < 0)
542 return err;
543
544 BT_DBG("RPA %pMR", rpa);
545
546 return 0;
547}
548
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700549int smp_generate_oob(struct hci_dev *hdev, u8 hash[16], u8 rand[16])
550{
551 struct l2cap_chan *chan = hdev->smp_data;
552 struct smp_dev *smp;
553 int err;
554
555 if (!chan || !chan->data)
556 return -EOPNOTSUPP;
557
558 smp = chan->data;
559
560 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
561 BT_DBG("Using debug keys");
562 memcpy(smp->local_pk, debug_pk, 64);
563 memcpy(smp->local_sk, debug_sk, 32);
564 smp->debug_key = true;
565 } else {
566 while (true) {
567 /* Generate local key pair for Secure Connections */
568 if (!ecc_make_key(smp->local_pk, smp->local_sk))
569 return -EIO;
570
571 /* This is unlikely, but we need to check that
572 * we didn't accidentially generate a debug key.
573 */
574 if (memcmp(smp->local_sk, debug_sk, 32))
575 break;
576 }
577 smp->debug_key = false;
578 }
579
580 SMP_DBG("OOB Public Key X: %32phN", smp->local_pk);
581 SMP_DBG("OOB Public Key Y: %32phN", smp->local_pk + 32);
582 SMP_DBG("OOB Private Key: %32phN", smp->local_sk);
583
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700584 get_random_bytes(smp->local_rand, 16);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700585
586 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->local_pk,
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700587 smp->local_rand, 0, hash);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700588 if (err < 0)
589 return err;
590
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700591 memcpy(rand, smp->local_rand, 16);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700592
593 return 0;
594}
595
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300596static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
597{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300598 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300599 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300600 struct kvec iv[2];
601 struct msghdr msg;
602
603 if (!chan)
604 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300605
606 BT_DBG("code 0x%2.2x", code);
607
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300608 iv[0].iov_base = &code;
609 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300610
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300611 iv[1].iov_base = data;
612 iv[1].iov_len = len;
613
614 memset(&msg, 0, sizeof(msg));
615
Al Viro17836392014-11-24 17:07:38 -0500616 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, iv, 2, 1 + len);
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300617
618 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300619
Johan Hedbergb68fda62014-08-11 22:06:40 +0300620 if (!chan->data)
621 return;
622
623 smp = chan->data;
624
625 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300626 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300627}
628
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300629static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800630{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300631 if (authreq & SMP_AUTH_MITM) {
632 if (authreq & SMP_AUTH_SC)
633 return BT_SECURITY_FIPS;
634 else
635 return BT_SECURITY_HIGH;
636 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800637 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300638 }
Brian Gix2b64d152011-12-21 16:12:12 -0800639}
640
641static __u8 seclevel_to_authreq(__u8 sec_level)
642{
643 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300644 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800645 case BT_SECURITY_HIGH:
646 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
647 case BT_SECURITY_MEDIUM:
648 return SMP_AUTH_BONDING;
649 default:
650 return SMP_AUTH_NONE;
651 }
652}
653
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300654static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700655 struct smp_cmd_pairing *req,
656 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300657{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300658 struct l2cap_chan *chan = conn->smp;
659 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200660 struct hci_conn *hcon = conn->hcon;
661 struct hci_dev *hdev = hcon->hdev;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100662 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300663
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700664 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700665 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
666 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300667 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800668 } else {
669 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300670 }
671
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700672 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergfd349c02014-02-18 10:19:36 +0200673 remote_dist |= SMP_DIST_ID_KEY;
674
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700675 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedberg863efaf2014-02-22 19:06:32 +0200676 local_dist |= SMP_DIST_ID_KEY;
677
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700678 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100679 (authreq & SMP_AUTH_SC)) {
680 struct oob_data *oob_data;
681 u8 bdaddr_type;
682
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700683 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300684 local_dist |= SMP_DIST_LINK_KEY;
685 remote_dist |= SMP_DIST_LINK_KEY;
686 }
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100687
688 if (hcon->dst_type == ADDR_LE_DEV_PUBLIC)
689 bdaddr_type = BDADDR_LE_PUBLIC;
690 else
691 bdaddr_type = BDADDR_LE_RANDOM;
692
693 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
694 bdaddr_type);
Marcel Holtmann4775a4e2015-01-31 00:15:52 -0800695 if (oob_data && oob_data->present) {
Johan Hedberg1a8bab42015-03-16 11:45:44 +0200696 set_bit(SMP_FLAG_REMOTE_OOB, &smp->flags);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100697 oob_flag = SMP_OOB_PRESENT;
Johan Hedberga29b0732014-10-28 15:17:05 +0100698 memcpy(smp->rr, oob_data->rand256, 16);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100699 memcpy(smp->pcnf, oob_data->hash256, 16);
Marcel Holtmannbc07cd62015-03-16 12:34:56 -0700700 SMP_DBG("OOB Remote Confirmation: %16phN", smp->pcnf);
701 SMP_DBG("OOB Remote Random: %16phN", smp->rr);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100702 }
703
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300704 } else {
705 authreq &= ~SMP_AUTH_SC;
706 }
707
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300708 if (rsp == NULL) {
709 req->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100710 req->oob_flag = oob_flag;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300711 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200712 req->init_key_dist = local_dist;
713 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300714 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200715
716 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300717 return;
718 }
719
720 rsp->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100721 rsp->oob_flag = oob_flag;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300722 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200723 rsp->init_key_dist = req->init_key_dist & remote_dist;
724 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300725 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200726
727 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300728}
729
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300730static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
731{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300732 struct l2cap_chan *chan = conn->smp;
733 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300734
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300735 if ((max_key_size > SMP_MAX_ENC_KEY_SIZE) ||
Marcel Holtmannf1560462013-10-13 05:43:25 -0700736 (max_key_size < SMP_MIN_ENC_KEY_SIZE))
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300737 return SMP_ENC_KEY_SIZE;
738
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300739 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300740
741 return 0;
742}
743
Johan Hedberg6f48e262014-08-11 22:06:44 +0300744static void smp_chan_destroy(struct l2cap_conn *conn)
745{
746 struct l2cap_chan *chan = conn->smp;
747 struct smp_chan *smp = chan->data;
Johan Hedberg923e2412014-12-03 12:43:39 +0200748 struct hci_conn *hcon = conn->hcon;
Johan Hedberg6f48e262014-08-11 22:06:44 +0300749 bool complete;
750
751 BUG_ON(!smp);
752
753 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300754
Johan Hedberg6f48e262014-08-11 22:06:44 +0300755 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
Johan Hedberg923e2412014-12-03 12:43:39 +0200756 mgmt_smp_complete(hcon, complete);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300757
Marcel Holtmann276812e2015-03-16 01:10:18 -0700758 kzfree(smp->csrk);
759 kzfree(smp->slave_csrk);
760 kzfree(smp->link_key);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300761
762 crypto_free_blkcipher(smp->tfm_aes);
Johan Hedberg407cecf2014-05-02 14:19:47 +0300763 crypto_free_hash(smp->tfm_cmac);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300764
Johan Hedberg923e2412014-12-03 12:43:39 +0200765 /* Ensure that we don't leave any debug key around if debug key
766 * support hasn't been explicitly enabled.
767 */
768 if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700769 !hci_dev_test_flag(hcon->hdev, HCI_KEEP_DEBUG_KEYS)) {
Johan Hedberg923e2412014-12-03 12:43:39 +0200770 list_del_rcu(&smp->ltk->list);
771 kfree_rcu(smp->ltk, rcu);
772 smp->ltk = NULL;
773 }
774
Johan Hedberg6f48e262014-08-11 22:06:44 +0300775 /* If pairing failed clean up any keys we might have */
776 if (!complete) {
777 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200778 list_del_rcu(&smp->ltk->list);
779 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300780 }
781
782 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200783 list_del_rcu(&smp->slave_ltk->list);
784 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300785 }
786
787 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200788 list_del_rcu(&smp->remote_irk->list);
789 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300790 }
791 }
792
793 chan->data = NULL;
Marcel Holtmann276812e2015-03-16 01:10:18 -0700794 kzfree(smp);
Johan Hedberg923e2412014-12-03 12:43:39 +0200795 hci_conn_drop(hcon);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300796}
797
Johan Hedberg84794e12013-11-06 11:24:57 +0200798static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800799{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200800 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300801 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200802
Johan Hedberg84794e12013-11-06 11:24:57 +0200803 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800804 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700805 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800806
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700807 clear_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags);
Johan Hedberge1e930f2014-09-08 17:09:49 -0700808 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300809
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300810 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300811 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800812}
813
Brian Gix2b64d152011-12-21 16:12:12 -0800814#define JUST_WORKS 0x00
815#define JUST_CFM 0x01
816#define REQ_PASSKEY 0x02
817#define CFM_PASSKEY 0x03
818#define REQ_OOB 0x04
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300819#define DSP_PASSKEY 0x05
Brian Gix2b64d152011-12-21 16:12:12 -0800820#define OVERLAP 0xFF
821
822static const u8 gen_method[5][5] = {
823 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
824 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
825 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
826 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
827 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
828};
829
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300830static const u8 sc_method[5][5] = {
831 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
832 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
833 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
834 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
835 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
836};
837
Johan Hedberg581370c2014-06-17 13:07:38 +0300838static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
839{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300840 /* If either side has unknown io_caps, use JUST_CFM (which gets
841 * converted later to JUST_WORKS if we're initiators.
842 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300843 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
844 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300845 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300846
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300847 if (test_bit(SMP_FLAG_SC, &smp->flags))
848 return sc_method[remote_io][local_io];
849
Johan Hedberg581370c2014-06-17 13:07:38 +0300850 return gen_method[remote_io][local_io];
851}
852
Brian Gix2b64d152011-12-21 16:12:12 -0800853static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
854 u8 local_io, u8 remote_io)
855{
856 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300857 struct l2cap_chan *chan = conn->smp;
858 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800859 u32 passkey = 0;
860 int ret = 0;
861
862 /* Initialize key for JUST WORKS */
863 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300864 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800865
866 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
867
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300868 /* If neither side wants MITM, either "just" confirm an incoming
869 * request or use just-works for outgoing ones. The JUST_CFM
870 * will be converted to JUST_WORKS if necessary later in this
871 * function. If either side has MITM look up the method from the
872 * table.
873 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300874 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg783e0572014-05-31 18:48:26 +0300875 smp->method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800876 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300877 smp->method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800878
Johan Hedberga82505c2014-03-24 14:39:07 +0200879 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg783e0572014-05-31 18:48:26 +0300880 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
881 &smp->flags))
882 smp->method = JUST_WORKS;
Johan Hedberga82505c2014-03-24 14:39:07 +0200883
Johan Hedberg02f3e252014-07-16 15:09:13 +0300884 /* Don't bother user space with no IO capabilities */
Johan Hedberg783e0572014-05-31 18:48:26 +0300885 if (smp->method == JUST_CFM &&
886 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
887 smp->method = JUST_WORKS;
Johan Hedberg02f3e252014-07-16 15:09:13 +0300888
Brian Gix2b64d152011-12-21 16:12:12 -0800889 /* If Just Works, Continue with Zero TK */
Johan Hedberg783e0572014-05-31 18:48:26 +0300890 if (smp->method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300891 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800892 return 0;
893 }
894
Johan Hedberg19c5ce92015-03-15 19:34:04 +0200895 /* If this function is used for SC -> legacy fallback we
896 * can only recover the just-works case.
897 */
898 if (test_bit(SMP_FLAG_SC, &smp->flags))
899 return -EINVAL;
900
Brian Gix2b64d152011-12-21 16:12:12 -0800901 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg783e0572014-05-31 18:48:26 +0300902 if (smp->method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300903 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300904 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
905 hcon->pending_sec_level = BT_SECURITY_HIGH;
906 }
Brian Gix2b64d152011-12-21 16:12:12 -0800907
908 /* If both devices have Keyoard-Display I/O, the master
909 * Confirms and the slave Enters the passkey.
910 */
Johan Hedberg783e0572014-05-31 18:48:26 +0300911 if (smp->method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300912 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedberg783e0572014-05-31 18:48:26 +0300913 smp->method = CFM_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800914 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300915 smp->method = REQ_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800916 }
917
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200918 /* Generate random passkey. */
Johan Hedberg783e0572014-05-31 18:48:26 +0300919 if (smp->method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200920 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800921 get_random_bytes(&passkey, sizeof(passkey));
922 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200923 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800924 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300925 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800926 }
927
Johan Hedberg783e0572014-05-31 18:48:26 +0300928 if (smp->method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700929 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200930 hcon->type, hcon->dst_type);
Johan Hedberg783e0572014-05-31 18:48:26 +0300931 else if (smp->method == JUST_CFM)
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200932 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
933 hcon->type, hcon->dst_type,
934 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800935 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200936 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200937 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200938 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800939
Brian Gix2b64d152011-12-21 16:12:12 -0800940 return ret;
941}
942
Johan Hedberg1cc61142014-05-20 09:45:52 +0300943static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300944{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300945 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300946 struct smp_cmd_pairing_confirm cp;
947 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300948
949 BT_DBG("conn %p", conn);
950
Johan Hedberge491eaf2014-10-25 21:15:37 +0200951 ret = smp_c1(smp->tfm_aes, smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200952 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200953 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
954 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300955 if (ret)
956 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300957
Johan Hedberg4a74d652014-05-20 09:45:50 +0300958 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800959
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300960 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
961
Johan Hedbergb28b4942014-09-05 22:19:55 +0300962 if (conn->hcon->out)
963 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
964 else
965 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
966
Johan Hedberg1cc61142014-05-20 09:45:52 +0300967 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300968}
969
Johan Hedberg861580a2014-05-20 09:45:51 +0300970static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300971{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300972 struct l2cap_conn *conn = smp->conn;
973 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300974 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300975 int ret;
976
Johan Hedbergec70f362014-06-27 14:23:04 +0300977 if (IS_ERR_OR_NULL(smp->tfm_aes))
Johan Hedberg861580a2014-05-20 09:45:51 +0300978 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300979
980 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
981
Johan Hedberge491eaf2014-10-25 21:15:37 +0200982 ret = smp_c1(smp->tfm_aes, smp->tk, smp->rrnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200983 hcon->init_addr_type, &hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200984 hcon->resp_addr_type, &hcon->resp_addr, confirm);
Johan Hedberg861580a2014-05-20 09:45:51 +0300985 if (ret)
986 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300987
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300988 if (memcmp(smp->pcnf, confirm, sizeof(smp->pcnf)) != 0) {
989 BT_ERR("Pairing failed (confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300990 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300991 }
992
993 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800994 u8 stk[16];
995 __le64 rand = 0;
996 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300997
Johan Hedberge491eaf2014-10-25 21:15:37 +0200998 smp_s1(smp->tfm_aes, smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300999
Johan Hedberg861580a2014-05-20 09:45:51 +03001000 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
1001 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001002
Johan Hedberg8b76ce32015-06-08 18:14:39 +03001003 hci_le_start_enc(hcon, ediv, rand, stk, smp->enc_key_size);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -03001004 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +03001005 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001006 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +03001007 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08001008 __le64 rand = 0;
1009 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001010
Johan Hedberg943a7322014-03-18 12:58:24 +02001011 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1012 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001013
Johan Hedberge491eaf2014-10-25 21:15:37 +02001014 smp_s1(smp->tfm_aes, smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001015
Johan Hedbergfff34902014-06-10 15:19:50 +03001016 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
1017 auth = 1;
1018 else
1019 auth = 0;
1020
Johan Hedberg7d5843b2014-06-16 19:25:15 +03001021 /* Even though there's no _SLAVE suffix this is the
1022 * slave STK we're adding for later lookup (the master
1023 * STK never needs to be stored).
1024 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001025 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +03001026 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001027 }
1028
Johan Hedberg861580a2014-05-20 09:45:51 +03001029 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001030}
1031
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001032static void smp_notify_keys(struct l2cap_conn *conn)
1033{
1034 struct l2cap_chan *chan = conn->smp;
1035 struct smp_chan *smp = chan->data;
1036 struct hci_conn *hcon = conn->hcon;
1037 struct hci_dev *hdev = hcon->hdev;
1038 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
1039 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
1040 bool persistent;
1041
1042 if (smp->remote_irk) {
1043 mgmt_new_irk(hdev, smp->remote_irk);
1044 /* Now that user space can be considered to know the
1045 * identity address track the connection based on it
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001046 * from now on (assuming this is an LE link).
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001047 */
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001048 if (hcon->type == LE_LINK) {
1049 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
1050 hcon->dst_type = smp->remote_irk->addr_type;
1051 queue_work(hdev->workqueue, &conn->id_addr_update_work);
1052 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001053
1054 /* When receiving an indentity resolving key for
1055 * a remote device that does not use a resolvable
1056 * private address, just remove the key so that
1057 * it is possible to use the controller white
1058 * list for scanning.
1059 *
1060 * Userspace will have been told to not store
1061 * this key at this point. So it is safe to
1062 * just remove it.
1063 */
1064 if (!bacmp(&smp->remote_irk->rpa, BDADDR_ANY)) {
Johan Hedbergadae20c2014-11-13 14:37:48 +02001065 list_del_rcu(&smp->remote_irk->list);
1066 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001067 smp->remote_irk = NULL;
1068 }
1069 }
1070
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001071 if (hcon->type == ACL_LINK) {
1072 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
1073 persistent = false;
1074 else
1075 persistent = !test_bit(HCI_CONN_FLUSH_KEY,
1076 &hcon->flags);
1077 } else {
1078 /* The LTKs and CSRKs should be persistent only if both sides
1079 * had the bonding bit set in their authentication requests.
1080 */
1081 persistent = !!((req->auth_req & rsp->auth_req) &
1082 SMP_AUTH_BONDING);
1083 }
1084
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001085
1086 if (smp->csrk) {
1087 smp->csrk->bdaddr_type = hcon->dst_type;
1088 bacpy(&smp->csrk->bdaddr, &hcon->dst);
1089 mgmt_new_csrk(hdev, smp->csrk, persistent);
1090 }
1091
1092 if (smp->slave_csrk) {
1093 smp->slave_csrk->bdaddr_type = hcon->dst_type;
1094 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
1095 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1096 }
1097
1098 if (smp->ltk) {
1099 smp->ltk->bdaddr_type = hcon->dst_type;
1100 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1101 mgmt_new_ltk(hdev, smp->ltk, persistent);
1102 }
1103
1104 if (smp->slave_ltk) {
1105 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1106 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1107 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1108 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001109
1110 if (smp->link_key) {
Johan Hedberge3befab2014-06-01 16:33:39 +03001111 struct link_key *key;
1112 u8 type;
1113
1114 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1115 type = HCI_LK_DEBUG_COMBINATION;
1116 else if (hcon->sec_level == BT_SECURITY_FIPS)
1117 type = HCI_LK_AUTH_COMBINATION_P256;
1118 else
1119 type = HCI_LK_UNAUTH_COMBINATION_P256;
1120
1121 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
1122 smp->link_key, type, 0, &persistent);
1123 if (key) {
1124 mgmt_new_link_key(hdev, key, persistent);
1125
1126 /* Don't keep debug keys around if the relevant
1127 * flag is not set.
1128 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001129 if (!hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS) &&
Johan Hedberge3befab2014-06-01 16:33:39 +03001130 key->type == HCI_LK_DEBUG_COMBINATION) {
1131 list_del_rcu(&key->list);
1132 kfree_rcu(key, rcu);
1133 }
1134 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001135 }
1136}
1137
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001138static void sc_add_ltk(struct smp_chan *smp)
1139{
1140 struct hci_conn *hcon = smp->conn->hcon;
1141 u8 key_type, auth;
1142
1143 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1144 key_type = SMP_LTK_P256_DEBUG;
1145 else
1146 key_type = SMP_LTK_P256;
1147
1148 if (hcon->pending_sec_level == BT_SECURITY_FIPS)
1149 auth = 1;
1150 else
1151 auth = 0;
1152
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001153 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1154 key_type, auth, smp->tk, smp->enc_key_size,
1155 0, 0);
1156}
1157
Johan Hedberg6a770832014-06-06 11:54:04 +03001158static void sc_generate_link_key(struct smp_chan *smp)
1159{
1160 /* These constants are as specified in the core specification.
1161 * In ASCII they spell out to 'tmp1' and 'lebr'.
1162 */
1163 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
1164 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
1165
1166 smp->link_key = kzalloc(16, GFP_KERNEL);
1167 if (!smp->link_key)
1168 return;
1169
1170 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
Marcel Holtmann276812e2015-03-16 01:10:18 -07001171 kzfree(smp->link_key);
Johan Hedberg6a770832014-06-06 11:54:04 +03001172 smp->link_key = NULL;
1173 return;
1174 }
1175
1176 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
Marcel Holtmann276812e2015-03-16 01:10:18 -07001177 kzfree(smp->link_key);
Johan Hedberg6a770832014-06-06 11:54:04 +03001178 smp->link_key = NULL;
1179 return;
1180 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001181}
1182
Johan Hedbergb28b4942014-09-05 22:19:55 +03001183static void smp_allow_key_dist(struct smp_chan *smp)
1184{
1185 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1186 * will be allowed in each PDU handler to ensure we receive
1187 * them in the correct order.
1188 */
1189 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
1190 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1191 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1192 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1193 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1194 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1195}
1196
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001197static void sc_generate_ltk(struct smp_chan *smp)
1198{
1199 /* These constants are as specified in the core specification.
1200 * In ASCII they spell out to 'tmp2' and 'brle'.
1201 */
1202 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
1203 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
1204 struct hci_conn *hcon = smp->conn->hcon;
1205 struct hci_dev *hdev = hcon->hdev;
1206 struct link_key *key;
1207
1208 key = hci_find_link_key(hdev, &hcon->dst);
1209 if (!key) {
1210 BT_ERR("%s No Link Key found to generate LTK", hdev->name);
1211 return;
1212 }
1213
1214 if (key->type == HCI_LK_DEBUG_COMBINATION)
1215 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1216
1217 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
1218 return;
1219
1220 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
1221 return;
1222
1223 sc_add_ltk(smp);
1224}
1225
Johan Hedbergd6268e82014-09-05 22:19:51 +03001226static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001227{
1228 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +03001229 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001230 struct hci_conn *hcon = conn->hcon;
1231 struct hci_dev *hdev = hcon->hdev;
1232 __u8 *keydist;
1233
1234 BT_DBG("conn %p", conn);
1235
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001236 rsp = (void *) &smp->prsp[1];
1237
1238 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001239 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1240 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001241 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001242 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001243
1244 req = (void *) &smp->preq[1];
1245
1246 if (hcon->out) {
1247 keydist = &rsp->init_key_dist;
1248 *keydist &= req->init_key_dist;
1249 } else {
1250 keydist = &rsp->resp_key_dist;
1251 *keydist &= req->resp_key_dist;
1252 }
1253
Johan Hedberg6a770832014-06-06 11:54:04 +03001254 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001255 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
Johan Hedberg6a770832014-06-06 11:54:04 +03001256 sc_generate_link_key(smp);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001257 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1258 sc_generate_ltk(smp);
Johan Hedberg6a770832014-06-06 11:54:04 +03001259
1260 /* Clear the keys which are generated but not distributed */
1261 *keydist &= ~SMP_SC_NO_DIST;
1262 }
1263
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001264 BT_DBG("keydist 0x%x", *keydist);
1265
1266 if (*keydist & SMP_DIST_ENC_KEY) {
1267 struct smp_cmd_encrypt_info enc;
1268 struct smp_cmd_master_ident ident;
1269 struct smp_ltk *ltk;
1270 u8 authenticated;
1271 __le16 ediv;
1272 __le64 rand;
1273
Johan Hedberg1fc62c52015-06-10 11:11:20 +03001274 /* Make sure we generate only the significant amount of
1275 * bytes based on the encryption key size, and set the rest
1276 * of the value to zeroes.
1277 */
1278 get_random_bytes(enc.ltk, smp->enc_key_size);
1279 memset(enc.ltk + smp->enc_key_size, 0,
1280 sizeof(enc.ltk) - smp->enc_key_size);
1281
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001282 get_random_bytes(&ediv, sizeof(ediv));
1283 get_random_bytes(&rand, sizeof(rand));
1284
1285 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1286
1287 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1288 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1289 SMP_LTK_SLAVE, authenticated, enc.ltk,
1290 smp->enc_key_size, ediv, rand);
1291 smp->slave_ltk = ltk;
1292
1293 ident.ediv = ediv;
1294 ident.rand = rand;
1295
1296 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1297
1298 *keydist &= ~SMP_DIST_ENC_KEY;
1299 }
1300
1301 if (*keydist & SMP_DIST_ID_KEY) {
1302 struct smp_cmd_ident_addr_info addrinfo;
1303 struct smp_cmd_ident_info idinfo;
1304
1305 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1306
1307 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1308
1309 /* The hci_conn contains the local identity address
1310 * after the connection has been established.
1311 *
1312 * This is true even when the connection has been
1313 * established using a resolvable random address.
1314 */
1315 bacpy(&addrinfo.bdaddr, &hcon->src);
1316 addrinfo.addr_type = hcon->src_type;
1317
1318 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1319 &addrinfo);
1320
1321 *keydist &= ~SMP_DIST_ID_KEY;
1322 }
1323
1324 if (*keydist & SMP_DIST_SIGN) {
1325 struct smp_cmd_sign_info sign;
1326 struct smp_csrk *csrk;
1327
1328 /* Generate a new random key */
1329 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1330
1331 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1332 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02001333 if (hcon->sec_level > BT_SECURITY_MEDIUM)
1334 csrk->type = MGMT_CSRK_LOCAL_AUTHENTICATED;
1335 else
1336 csrk->type = MGMT_CSRK_LOCAL_UNAUTHENTICATED;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001337 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1338 }
1339 smp->slave_csrk = csrk;
1340
1341 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1342
1343 *keydist &= ~SMP_DIST_SIGN;
1344 }
1345
1346 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001347 if (smp->remote_key_dist & KEY_DIST_MASK) {
1348 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001349 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001350 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001351
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001352 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1353 smp_notify_keys(conn);
1354
1355 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001356}
1357
Johan Hedbergb68fda62014-08-11 22:06:40 +03001358static void smp_timeout(struct work_struct *work)
1359{
1360 struct smp_chan *smp = container_of(work, struct smp_chan,
1361 security_timer.work);
1362 struct l2cap_conn *conn = smp->conn;
1363
1364 BT_DBG("conn %p", conn);
1365
Johan Hedberg1e91c292014-08-18 20:33:29 +03001366 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001367}
1368
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001369static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1370{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001371 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001372 struct smp_chan *smp;
1373
Marcel Holtmannf1560462013-10-13 05:43:25 -07001374 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001375 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001376 return NULL;
1377
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001378 smp->tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
1379 if (IS_ERR(smp->tfm_aes)) {
1380 BT_ERR("Unable to create ECB crypto context");
Marcel Holtmann276812e2015-03-16 01:10:18 -07001381 kzfree(smp);
Johan Hedberg6a7bd102014-06-27 14:23:03 +03001382 return NULL;
1383 }
1384
Johan Hedberg407cecf2014-05-02 14:19:47 +03001385 smp->tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
1386 if (IS_ERR(smp->tfm_cmac)) {
1387 BT_ERR("Unable to create CMAC crypto context");
1388 crypto_free_blkcipher(smp->tfm_aes);
Marcel Holtmann276812e2015-03-16 01:10:18 -07001389 kzfree(smp);
Johan Hedberg407cecf2014-05-02 14:19:47 +03001390 return NULL;
1391 }
1392
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001393 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001394 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001395
Johan Hedbergb28b4942014-09-05 22:19:55 +03001396 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1397
Johan Hedbergb68fda62014-08-11 22:06:40 +03001398 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1399
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001400 hci_conn_hold(conn->hcon);
1401
1402 return smp;
1403}
1404
Johan Hedberg760b0182014-06-06 11:44:05 +03001405static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1406{
1407 struct hci_conn *hcon = smp->conn->hcon;
1408 u8 *na, *nb, a[7], b[7];
1409
1410 if (hcon->out) {
1411 na = smp->prnd;
1412 nb = smp->rrnd;
1413 } else {
1414 na = smp->rrnd;
1415 nb = smp->prnd;
1416 }
1417
1418 memcpy(a, &hcon->init_addr, 6);
1419 memcpy(b, &hcon->resp_addr, 6);
1420 a[6] = hcon->init_addr_type;
1421 b[6] = hcon->resp_addr_type;
1422
1423 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1424}
1425
Johan Hedberg38606f12014-06-25 11:10:28 +03001426static void sc_dhkey_check(struct smp_chan *smp)
Johan Hedberg760b0182014-06-06 11:44:05 +03001427{
1428 struct hci_conn *hcon = smp->conn->hcon;
1429 struct smp_cmd_dhkey_check check;
1430 u8 a[7], b[7], *local_addr, *remote_addr;
1431 u8 io_cap[3], r[16];
1432
Johan Hedberg760b0182014-06-06 11:44:05 +03001433 memcpy(a, &hcon->init_addr, 6);
1434 memcpy(b, &hcon->resp_addr, 6);
1435 a[6] = hcon->init_addr_type;
1436 b[6] = hcon->resp_addr_type;
1437
1438 if (hcon->out) {
1439 local_addr = a;
1440 remote_addr = b;
1441 memcpy(io_cap, &smp->preq[1], 3);
1442 } else {
1443 local_addr = b;
1444 remote_addr = a;
1445 memcpy(io_cap, &smp->prsp[1], 3);
1446 }
1447
Johan Hedbergdddd3052014-06-01 15:38:09 +03001448 memset(r, 0, sizeof(r));
1449
1450 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
Johan Hedberg38606f12014-06-25 11:10:28 +03001451 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg760b0182014-06-06 11:44:05 +03001452
Johan Hedberga29b0732014-10-28 15:17:05 +01001453 if (smp->method == REQ_OOB)
1454 memcpy(r, smp->rr, 16);
1455
Johan Hedberg760b0182014-06-06 11:44:05 +03001456 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1457 local_addr, remote_addr, check.e);
1458
1459 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
Johan Hedbergdddd3052014-06-01 15:38:09 +03001460}
1461
Johan Hedberg38606f12014-06-25 11:10:28 +03001462static u8 sc_passkey_send_confirm(struct smp_chan *smp)
1463{
1464 struct l2cap_conn *conn = smp->conn;
1465 struct hci_conn *hcon = conn->hcon;
1466 struct smp_cmd_pairing_confirm cfm;
1467 u8 r;
1468
1469 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1470 r |= 0x80;
1471
1472 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1473
1474 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
1475 cfm.confirm_val))
1476 return SMP_UNSPECIFIED;
1477
1478 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1479
1480 return 0;
1481}
1482
1483static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
1484{
1485 struct l2cap_conn *conn = smp->conn;
1486 struct hci_conn *hcon = conn->hcon;
1487 struct hci_dev *hdev = hcon->hdev;
1488 u8 cfm[16], r;
1489
1490 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1491 if (smp->passkey_round >= 20)
1492 return 0;
1493
1494 switch (smp_op) {
1495 case SMP_CMD_PAIRING_RANDOM:
1496 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1497 r |= 0x80;
1498
1499 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1500 smp->rrnd, r, cfm))
1501 return SMP_UNSPECIFIED;
1502
1503 if (memcmp(smp->pcnf, cfm, 16))
1504 return SMP_CONFIRM_FAILED;
1505
1506 smp->passkey_round++;
1507
1508 if (smp->passkey_round == 20) {
1509 /* Generate MacKey and LTK */
1510 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
1511 return SMP_UNSPECIFIED;
1512 }
1513
1514 /* The round is only complete when the initiator
1515 * receives pairing random.
1516 */
1517 if (!hcon->out) {
1518 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1519 sizeof(smp->prnd), smp->prnd);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001520 if (smp->passkey_round == 20)
Johan Hedberg38606f12014-06-25 11:10:28 +03001521 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001522 else
Johan Hedberg38606f12014-06-25 11:10:28 +03001523 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Johan Hedberg38606f12014-06-25 11:10:28 +03001524 return 0;
1525 }
1526
1527 /* Start the next round */
1528 if (smp->passkey_round != 20)
1529 return sc_passkey_round(smp, 0);
1530
1531 /* Passkey rounds are complete - start DHKey Check */
1532 sc_dhkey_check(smp);
1533 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1534
1535 break;
1536
1537 case SMP_CMD_PAIRING_CONFIRM:
1538 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
1539 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1540 return 0;
1541 }
1542
1543 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1544
1545 if (hcon->out) {
1546 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1547 sizeof(smp->prnd), smp->prnd);
1548 return 0;
1549 }
1550
1551 return sc_passkey_send_confirm(smp);
1552
1553 case SMP_CMD_PUBLIC_KEY:
1554 default:
1555 /* Initiating device starts the round */
1556 if (!hcon->out)
1557 return 0;
1558
1559 BT_DBG("%s Starting passkey round %u", hdev->name,
1560 smp->passkey_round + 1);
1561
1562 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1563
1564 return sc_passkey_send_confirm(smp);
1565 }
1566
1567 return 0;
1568}
1569
Johan Hedbergdddd3052014-06-01 15:38:09 +03001570static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1571{
Johan Hedberg38606f12014-06-25 11:10:28 +03001572 struct l2cap_conn *conn = smp->conn;
1573 struct hci_conn *hcon = conn->hcon;
1574 u8 smp_op;
1575
1576 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1577
Johan Hedbergdddd3052014-06-01 15:38:09 +03001578 switch (mgmt_op) {
1579 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1580 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1581 return 0;
1582 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1583 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1584 return 0;
Johan Hedberg38606f12014-06-25 11:10:28 +03001585 case MGMT_OP_USER_PASSKEY_REPLY:
1586 hcon->passkey_notify = le32_to_cpu(passkey);
1587 smp->passkey_round = 0;
1588
1589 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
1590 smp_op = SMP_CMD_PAIRING_CONFIRM;
1591 else
1592 smp_op = 0;
1593
1594 if (sc_passkey_round(smp, smp_op))
1595 return -EIO;
1596
1597 return 0;
Johan Hedbergdddd3052014-06-01 15:38:09 +03001598 }
1599
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001600 /* Initiator sends DHKey check first */
1601 if (hcon->out) {
1602 sc_dhkey_check(smp);
1603 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1604 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
1605 sc_dhkey_check(smp);
1606 sc_add_ltk(smp);
1607 }
Johan Hedberg760b0182014-06-06 11:44:05 +03001608
1609 return 0;
1610}
1611
Brian Gix2b64d152011-12-21 16:12:12 -08001612int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1613{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001614 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001615 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001616 struct smp_chan *smp;
1617 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001618 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001619
1620 BT_DBG("");
1621
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001622 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001623 return -ENOTCONN;
1624
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001625 chan = conn->smp;
1626 if (!chan)
1627 return -ENOTCONN;
1628
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001629 l2cap_chan_lock(chan);
1630 if (!chan->data) {
1631 err = -ENOTCONN;
1632 goto unlock;
1633 }
1634
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001635 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001636
Johan Hedberg760b0182014-06-06 11:44:05 +03001637 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1638 err = sc_user_reply(smp, mgmt_op, passkey);
1639 goto unlock;
1640 }
1641
Brian Gix2b64d152011-12-21 16:12:12 -08001642 switch (mgmt_op) {
1643 case MGMT_OP_USER_PASSKEY_REPLY:
1644 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001645 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001646 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001647 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001648 /* Fall Through */
1649 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001650 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001651 break;
1652 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1653 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001654 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001655 err = 0;
1656 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001657 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001658 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001659 err = -EOPNOTSUPP;
1660 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001661 }
1662
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001663 err = 0;
1664
Brian Gix2b64d152011-12-21 16:12:12 -08001665 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001666 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1667 u8 rsp = smp_confirm(smp);
1668 if (rsp)
1669 smp_failure(conn, rsp);
1670 }
Brian Gix2b64d152011-12-21 16:12:12 -08001671
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001672unlock:
1673 l2cap_chan_unlock(chan);
1674 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001675}
1676
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001677static void build_bredr_pairing_cmd(struct smp_chan *smp,
1678 struct smp_cmd_pairing *req,
1679 struct smp_cmd_pairing *rsp)
1680{
1681 struct l2cap_conn *conn = smp->conn;
1682 struct hci_dev *hdev = conn->hcon->hdev;
1683 u8 local_dist = 0, remote_dist = 0;
1684
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001685 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001686 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1687 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1688 }
1689
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001690 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001691 remote_dist |= SMP_DIST_ID_KEY;
1692
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001693 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001694 local_dist |= SMP_DIST_ID_KEY;
1695
1696 if (!rsp) {
1697 memset(req, 0, sizeof(*req));
1698
1699 req->init_key_dist = local_dist;
1700 req->resp_key_dist = remote_dist;
1701 req->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1702
1703 smp->remote_key_dist = remote_dist;
1704
1705 return;
1706 }
1707
1708 memset(rsp, 0, sizeof(*rsp));
1709
1710 rsp->max_key_size = SMP_MAX_ENC_KEY_SIZE;
1711 rsp->init_key_dist = req->init_key_dist & remote_dist;
1712 rsp->resp_key_dist = req->resp_key_dist & local_dist;
1713
1714 smp->remote_key_dist = rsp->init_key_dist;
1715}
1716
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001717static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001718{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001719 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001720 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001721 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001722 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001723 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001724 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001725
1726 BT_DBG("conn %p", conn);
1727
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001728 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001729 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001730
Johan Hedberg40bef302014-07-16 11:42:27 +03001731 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001732 return SMP_CMD_NOTSUPP;
1733
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001734 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001735 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001736 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001737 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001738
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001739 if (!smp)
1740 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001741
Johan Hedbergc05b9332014-09-10 17:37:42 -07001742 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001743 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001744
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001745 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001746 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001747 return SMP_PAIRING_NOTSUPP;
1748
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001749 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001750 return SMP_AUTH_REQUIREMENTS;
1751
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001752 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1753 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001754 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001755
Johan Hedbergcb06d362015-03-16 21:12:34 +02001756 /* If the remote side's OOB flag is set it means it has
1757 * successfully received our local OOB data - therefore set the
1758 * flag to indicate that local OOB is in use.
1759 */
Johan Hedberg58428562015-03-16 11:45:45 +02001760 if (req->oob_flag == SMP_OOB_PRESENT)
1761 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1762
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001763 /* SMP over BR/EDR requires special treatment */
1764 if (conn->hcon->type == ACL_LINK) {
1765 /* We must have a BR/EDR SC link */
Marcel Holtmann08f63cc2014-12-07 16:19:12 +01001766 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07001767 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001768 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1769
1770 set_bit(SMP_FLAG_SC, &smp->flags);
1771
1772 build_bredr_pairing_cmd(smp, req, &rsp);
1773
1774 key_size = min(req->max_key_size, rsp.max_key_size);
1775 if (check_enc_key_size(conn, key_size))
1776 return SMP_ENC_KEY_SIZE;
1777
1778 /* Clear bits which are generated but not distributed */
1779 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1780
1781 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1782 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1783 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1784
1785 smp_distribute_keys(smp);
1786 return 0;
1787 }
1788
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001789 build_pairing_cmd(conn, req, &rsp, auth);
1790
1791 if (rsp.auth_req & SMP_AUTH_SC)
1792 set_bit(SMP_FLAG_SC, &smp->flags);
1793
Johan Hedberg5be5e272014-09-10 17:58:54 -07001794 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001795 sec_level = BT_SECURITY_MEDIUM;
1796 else
1797 sec_level = authreq_to_seclevel(auth);
1798
Johan Hedbergc7262e72014-06-17 13:07:37 +03001799 if (sec_level > conn->hcon->pending_sec_level)
1800 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001801
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001802 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001803 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1804 u8 method;
1805
1806 method = get_auth_method(smp, conn->hcon->io_capability,
1807 req->io_capability);
1808 if (method == JUST_WORKS || method == JUST_CFM)
1809 return SMP_AUTH_REQUIREMENTS;
1810 }
1811
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001812 key_size = min(req->max_key_size, rsp.max_key_size);
1813 if (check_enc_key_size(conn, key_size))
1814 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001815
Johan Hedberge84a6b12013-12-02 10:49:03 +02001816 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001817
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001818 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1819 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001820
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001821 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001822
1823 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1824
Johan Hedberg19c5ce92015-03-15 19:34:04 +02001825 /* Strictly speaking we shouldn't allow Pairing Confirm for the
1826 * SC case, however some implementations incorrectly copy RFU auth
1827 * req bits from our security request, which may create a false
1828 * positive SC enablement.
1829 */
1830 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1831
Johan Hedberg3b191462014-06-06 10:50:15 +03001832 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1833 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1834 /* Clear bits which are generated but not distributed */
1835 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1836 /* Wait for Public Key from Initiating Device */
1837 return 0;
Johan Hedberg3b191462014-06-06 10:50:15 +03001838 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001839
Brian Gix2b64d152011-12-21 16:12:12 -08001840 /* Request setup of TK */
1841 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1842 if (ret)
1843 return SMP_UNSPECIFIED;
1844
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001845 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001846}
1847
Johan Hedberg3b191462014-06-06 10:50:15 +03001848static u8 sc_send_public_key(struct smp_chan *smp)
1849{
Johan Hedberg70157ef2014-06-24 15:22:59 +03001850 struct hci_dev *hdev = smp->conn->hcon->hdev;
1851
Johan Hedberg3b191462014-06-06 10:50:15 +03001852 BT_DBG("");
1853
Johan Hedberg1a8bab42015-03-16 11:45:44 +02001854 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) {
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001855 struct l2cap_chan *chan = hdev->smp_data;
1856 struct smp_dev *smp_dev;
1857
1858 if (!chan || !chan->data)
1859 return SMP_UNSPECIFIED;
1860
1861 smp_dev = chan->data;
1862
1863 memcpy(smp->local_pk, smp_dev->local_pk, 64);
1864 memcpy(smp->local_sk, smp_dev->local_sk, 32);
Marcel Holtmannfb334fe2015-03-16 12:34:57 -07001865 memcpy(smp->lr, smp_dev->local_rand, 16);
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001866
1867 if (smp_dev->debug_key)
1868 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1869
1870 goto done;
1871 }
1872
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001873 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
Johan Hedberg70157ef2014-06-24 15:22:59 +03001874 BT_DBG("Using debug keys");
1875 memcpy(smp->local_pk, debug_pk, 64);
1876 memcpy(smp->local_sk, debug_sk, 32);
1877 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1878 } else {
1879 while (true) {
1880 /* Generate local key pair for Secure Connections */
1881 if (!ecc_make_key(smp->local_pk, smp->local_sk))
1882 return SMP_UNSPECIFIED;
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001883
Johan Hedberg70157ef2014-06-24 15:22:59 +03001884 /* This is unlikely, but we need to check that
1885 * we didn't accidentially generate a debug key.
1886 */
1887 if (memcmp(smp->local_sk, debug_sk, 32))
1888 break;
1889 }
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001890 }
Johan Hedberg3b191462014-06-06 10:50:15 +03001891
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001892done:
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001893 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
Marcel Holtmann8e4e2ee2015-03-16 01:10:25 -07001894 SMP_DBG("Local Public Key Y: %32phN", smp->local_pk + 32);
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001895 SMP_DBG("Local Private Key: %32phN", smp->local_sk);
Johan Hedberg3b191462014-06-06 10:50:15 +03001896
1897 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1898
1899 return 0;
1900}
1901
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001902static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001903{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001904 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001905 struct l2cap_chan *chan = conn->smp;
1906 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001907 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001908 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001909 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001910
1911 BT_DBG("conn %p", conn);
1912
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001913 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001914 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001915
Johan Hedberg40bef302014-07-16 11:42:27 +03001916 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001917 return SMP_CMD_NOTSUPP;
1918
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001919 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001920
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001921 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001922
1923 key_size = min(req->max_key_size, rsp->max_key_size);
1924 if (check_enc_key_size(conn, key_size))
1925 return SMP_ENC_KEY_SIZE;
1926
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001927 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001928
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001929 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001930 return SMP_AUTH_REQUIREMENTS;
1931
Johan Hedbergcb06d362015-03-16 21:12:34 +02001932 /* If the remote side's OOB flag is set it means it has
1933 * successfully received our local OOB data - therefore set the
1934 * flag to indicate that local OOB is in use.
1935 */
Johan Hedberg58428562015-03-16 11:45:45 +02001936 if (rsp->oob_flag == SMP_OOB_PRESENT)
1937 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1938
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001939 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1940 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1941
1942 /* Update remote key distribution in case the remote cleared
1943 * some bits that we had enabled in our request.
1944 */
1945 smp->remote_key_dist &= rsp->resp_key_dist;
1946
1947 /* For BR/EDR this means we're done and can start phase 3 */
1948 if (conn->hcon->type == ACL_LINK) {
1949 /* Clear bits which are generated but not distributed */
1950 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1951 smp_distribute_keys(smp);
1952 return 0;
1953 }
1954
Johan Hedberg65668772014-05-16 11:03:34 +03001955 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1956 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001957 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1958 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001959
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001960 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001961 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1962 u8 method;
1963
1964 method = get_auth_method(smp, req->io_capability,
1965 rsp->io_capability);
1966 if (method == JUST_WORKS || method == JUST_CFM)
1967 return SMP_AUTH_REQUIREMENTS;
1968 }
1969
Johan Hedberge84a6b12013-12-02 10:49:03 +02001970 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001971
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001972 /* Update remote key distribution in case the remote cleared
1973 * some bits that we had enabled in our request.
1974 */
1975 smp->remote_key_dist &= rsp->resp_key_dist;
1976
Johan Hedberg3b191462014-06-06 10:50:15 +03001977 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1978 /* Clear bits which are generated but not distributed */
1979 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1980 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1981 return sc_send_public_key(smp);
1982 }
1983
Johan Hedbergc05b9332014-09-10 17:37:42 -07001984 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08001985
Johan Hedberg476585e2012-06-06 18:54:15 +08001986 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08001987 if (ret)
1988 return SMP_UNSPECIFIED;
1989
Johan Hedberg4a74d652014-05-20 09:45:50 +03001990 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001991
1992 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03001993 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03001994 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001995
1996 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001997}
1998
Johan Hedbergdcee2b32014-06-06 11:36:38 +03001999static u8 sc_check_confirm(struct smp_chan *smp)
2000{
2001 struct l2cap_conn *conn = smp->conn;
2002
2003 BT_DBG("");
2004
Johan Hedberg38606f12014-06-25 11:10:28 +03002005 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2006 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
2007
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002008 if (conn->hcon->out) {
2009 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2010 smp->prnd);
2011 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2012 }
2013
2014 return 0;
2015}
2016
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002017/* Work-around for some implementations that incorrectly copy RFU bits
2018 * from our security request and thereby create the impression that
2019 * we're doing SC when in fact the remote doesn't support it.
2020 */
2021static int fixup_sc_false_positive(struct smp_chan *smp)
2022{
2023 struct l2cap_conn *conn = smp->conn;
2024 struct hci_conn *hcon = conn->hcon;
2025 struct hci_dev *hdev = hcon->hdev;
2026 struct smp_cmd_pairing *req, *rsp;
2027 u8 auth;
2028
2029 /* The issue is only observed when we're in slave role */
2030 if (hcon->out)
2031 return SMP_UNSPECIFIED;
2032
2033 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
2034 BT_ERR("Refusing SMP SC -> legacy fallback in SC-only mode");
2035 return SMP_UNSPECIFIED;
2036 }
2037
2038 BT_ERR("Trying to fall back to legacy SMP");
2039
2040 req = (void *) &smp->preq[1];
2041 rsp = (void *) &smp->prsp[1];
2042
2043 /* Rebuild key dist flags which may have been cleared for SC */
2044 smp->remote_key_dist = (req->init_key_dist & rsp->resp_key_dist);
2045
2046 auth = req->auth_req & AUTH_REQ_MASK(hdev);
2047
2048 if (tk_request(conn, 0, auth, rsp->io_capability, req->io_capability)) {
2049 BT_ERR("Failed to fall back to legacy SMP");
2050 return SMP_UNSPECIFIED;
2051 }
2052
2053 clear_bit(SMP_FLAG_SC, &smp->flags);
2054
2055 return 0;
2056}
2057
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002058static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002059{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002060 struct l2cap_chan *chan = conn->smp;
2061 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002062
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002063 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
2064
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002065 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002066 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002067
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002068 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
2069 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002070
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002071 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
2072 int ret;
2073
2074 /* Public Key exchange must happen before any other steps */
2075 if (test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
2076 return sc_check_confirm(smp);
2077
2078 BT_ERR("Unexpected SMP Pairing Confirm");
2079
2080 ret = fixup_sc_false_positive(smp);
2081 if (ret)
2082 return ret;
2083 }
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002084
Johan Hedbergb28b4942014-09-05 22:19:55 +03002085 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02002086 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2087 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002088 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2089 return 0;
2090 }
2091
2092 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03002093 return smp_confirm(smp);
Marcel Holtmann983f9812015-03-11 17:47:40 -07002094
2095 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002096
2097 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002098}
2099
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002100static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002101{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002102 struct l2cap_chan *chan = conn->smp;
2103 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002104 struct hci_conn *hcon = conn->hcon;
2105 u8 *pkax, *pkbx, *na, *nb;
2106 u32 passkey;
2107 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002108
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002109 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002110
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002111 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002112 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002113
Johan Hedberg943a7322014-03-18 12:58:24 +02002114 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002115 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002116
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002117 if (!test_bit(SMP_FLAG_SC, &smp->flags))
2118 return smp_random(smp);
2119
Johan Hedberg580039e2014-12-03 16:26:37 +02002120 if (hcon->out) {
2121 pkax = smp->local_pk;
2122 pkbx = smp->remote_pk;
2123 na = smp->prnd;
2124 nb = smp->rrnd;
2125 } else {
2126 pkax = smp->remote_pk;
2127 pkbx = smp->local_pk;
2128 na = smp->rrnd;
2129 nb = smp->prnd;
2130 }
2131
Johan Hedberga29b0732014-10-28 15:17:05 +01002132 if (smp->method == REQ_OOB) {
2133 if (!hcon->out)
2134 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2135 sizeof(smp->prnd), smp->prnd);
2136 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2137 goto mackey_and_ltk;
2138 }
2139
Johan Hedberg38606f12014-06-25 11:10:28 +03002140 /* Passkey entry has special treatment */
2141 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2142 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
2143
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002144 if (hcon->out) {
2145 u8 cfm[16];
2146
2147 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
2148 smp->rrnd, 0, cfm);
2149 if (err)
2150 return SMP_UNSPECIFIED;
2151
2152 if (memcmp(smp->pcnf, cfm, 16))
2153 return SMP_CONFIRM_FAILED;
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002154 } else {
2155 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2156 smp->prnd);
2157 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002158 }
2159
Johan Hedberga29b0732014-10-28 15:17:05 +01002160mackey_and_ltk:
Johan Hedberg760b0182014-06-06 11:44:05 +03002161 /* Generate MacKey and LTK */
2162 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
2163 if (err)
2164 return SMP_UNSPECIFIED;
2165
Johan Hedberga29b0732014-10-28 15:17:05 +01002166 if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
Johan Hedbergdddd3052014-06-01 15:38:09 +03002167 if (hcon->out) {
Johan Hedberg38606f12014-06-25 11:10:28 +03002168 sc_dhkey_check(smp);
Johan Hedbergdddd3052014-06-01 15:38:09 +03002169 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2170 }
2171 return 0;
2172 }
2173
Johan Hedberg38606f12014-06-25 11:10:28 +03002174 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002175 if (err)
2176 return SMP_UNSPECIFIED;
2177
Johan Hedberg38606f12014-06-25 11:10:28 +03002178 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
2179 hcon->dst_type, passkey, 0);
2180 if (err)
2181 return SMP_UNSPECIFIED;
2182
2183 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2184
Johan Hedberg191dc7f2014-06-06 11:39:49 +03002185 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002186}
2187
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002188static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002189{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002190 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002191 struct hci_conn *hcon = conn->hcon;
2192
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002193 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002194 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002195 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002196
Johan Hedberga6f78332014-09-10 17:37:45 -07002197 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002198 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08002199
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002200 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002201 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002202
Johan Hedberg8b76ce32015-06-08 18:14:39 +03002203 hci_le_start_enc(hcon, key->ediv, key->rand, key->val, key->enc_size);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002204 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002205
Johan Hedbergfe59a052014-07-01 19:14:12 +03002206 /* We never store STKs for master role, so clear this flag */
2207 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
2208
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002209 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002210}
Marcel Holtmannf1560462013-10-13 05:43:25 -07002211
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002212bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
2213 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03002214{
2215 if (sec_level == BT_SECURITY_LOW)
2216 return true;
2217
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002218 /* If we're encrypted with an STK but the caller prefers using
2219 * LTK claim insufficient security. This way we allow the
2220 * connection to be re-encrypted with an LTK, even if the LTK
2221 * provides the same level of security. Only exception is if we
2222 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002223 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002224 if (key_pref == SMP_USE_LTK &&
2225 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002226 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002227 return false;
2228
Johan Hedberg854f4722014-07-01 18:40:20 +03002229 if (hcon->sec_level >= sec_level)
2230 return true;
2231
2232 return false;
2233}
2234
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002235static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002236{
2237 struct smp_cmd_security_req *rp = (void *) skb->data;
2238 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002239 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002240 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002241 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07002242 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002243
2244 BT_DBG("conn %p", conn);
2245
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002246 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002247 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002248
Johan Hedberg40bef302014-07-16 11:42:27 +03002249 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02002250 return SMP_CMD_NOTSUPP;
2251
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002252 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07002253
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002254 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07002255 return SMP_AUTH_REQUIREMENTS;
2256
Johan Hedberg5be5e272014-09-10 17:58:54 -07002257 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07002258 sec_level = BT_SECURITY_MEDIUM;
2259 else
2260 sec_level = authreq_to_seclevel(auth);
2261
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002262 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Johan Hedberg854f4722014-07-01 18:40:20 +03002263 return 0;
2264
Johan Hedbergc7262e72014-06-17 13:07:37 +03002265 if (sec_level > hcon->pending_sec_level)
2266 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03002267
Johan Hedberg4dab7862012-06-07 14:58:37 +08002268 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002269 return 0;
2270
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002271 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03002272 if (!smp)
2273 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002274
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002275 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07002276 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03002277 return SMP_PAIRING_NOTSUPP;
2278
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002279 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002280
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002281 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07002282 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002283
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002284 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2285 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002286
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002287 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002288 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002289
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002290 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002291}
2292
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002293int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002294{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002295 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002296 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002297 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08002298 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002299 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002300
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002301 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2302
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002303 /* This may be NULL if there's an unexpected disconnection */
2304 if (!conn)
2305 return 1;
2306
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002307 chan = conn->smp;
2308
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002309 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002310 return 1;
2311
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002312 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002313 return 1;
2314
Johan Hedbergc7262e72014-06-17 13:07:37 +03002315 if (sec_level > hcon->pending_sec_level)
2316 hcon->pending_sec_level = sec_level;
2317
Johan Hedberg40bef302014-07-16 11:42:27 +03002318 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03002319 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2320 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002321
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002322 l2cap_chan_lock(chan);
2323
2324 /* If SMP is already in progress ignore this request */
2325 if (chan->data) {
2326 ret = 0;
2327 goto unlock;
2328 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002329
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002330 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002331 if (!smp) {
2332 ret = 1;
2333 goto unlock;
2334 }
Brian Gix2b64d152011-12-21 16:12:12 -08002335
2336 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002337
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002338 if (hci_dev_test_flag(hcon->hdev, HCI_SC_ENABLED))
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002339 authreq |= SMP_AUTH_SC;
2340
Johan Hedberg79897d22014-06-01 09:45:24 +03002341 /* Require MITM if IO Capability allows or the security level
2342 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02002343 */
Johan Hedberg79897d22014-06-01 09:45:24 +03002344 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03002345 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02002346 authreq |= SMP_AUTH_MITM;
2347
Johan Hedberg40bef302014-07-16 11:42:27 +03002348 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002349 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002350
Brian Gix2b64d152011-12-21 16:12:12 -08002351 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002352 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2353 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002354
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002355 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002356 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002357 } else {
2358 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08002359 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002360 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002361 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002362 }
2363
Johan Hedberg4a74d652014-05-20 09:45:50 +03002364 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002365 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02002366
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002367unlock:
2368 l2cap_chan_unlock(chan);
2369 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002370}
2371
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002372static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2373{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002374 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002375 struct l2cap_chan *chan = conn->smp;
2376 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002377
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002378 BT_DBG("conn %p", conn);
2379
2380 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002381 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002382
Johan Hedbergb28b4942014-09-05 22:19:55 +03002383 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002384
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002385 skb_pull(skb, sizeof(*rp));
2386
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002387 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002388
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002389 return 0;
2390}
2391
2392static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2393{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002394 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002395 struct l2cap_chan *chan = conn->smp;
2396 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002397 struct hci_dev *hdev = conn->hcon->hdev;
2398 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02002399 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002400 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002401
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002402 BT_DBG("conn %p", conn);
2403
2404 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002405 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002406
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002407 /* Mark the information as received */
2408 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2409
Johan Hedbergb28b4942014-09-05 22:19:55 +03002410 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2411 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07002412 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2413 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002414
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002415 skb_pull(skb, sizeof(*rp));
2416
Marcel Holtmannce39fb42013-10-13 02:23:39 -07002417 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03002418 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02002419 authenticated, smp->tk, smp->enc_key_size,
2420 rp->ediv, rp->rand);
2421 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002422 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03002423 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002424
2425 return 0;
2426}
2427
Johan Hedbergfd349c02014-02-18 10:19:36 +02002428static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2429{
2430 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002431 struct l2cap_chan *chan = conn->smp;
2432 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002433
2434 BT_DBG("");
2435
2436 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002437 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002438
Johan Hedbergb28b4942014-09-05 22:19:55 +03002439 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002440
Johan Hedbergfd349c02014-02-18 10:19:36 +02002441 skb_pull(skb, sizeof(*info));
2442
2443 memcpy(smp->irk, info->irk, 16);
2444
2445 return 0;
2446}
2447
2448static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2449 struct sk_buff *skb)
2450{
2451 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002452 struct l2cap_chan *chan = conn->smp;
2453 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002454 struct hci_conn *hcon = conn->hcon;
2455 bdaddr_t rpa;
2456
2457 BT_DBG("");
2458
2459 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002460 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002461
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002462 /* Mark the information as received */
2463 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2464
Johan Hedbergb28b4942014-09-05 22:19:55 +03002465 if (smp->remote_key_dist & SMP_DIST_SIGN)
2466 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2467
Johan Hedbergfd349c02014-02-18 10:19:36 +02002468 skb_pull(skb, sizeof(*info));
2469
Johan Hedberga9a58f82014-02-25 22:24:37 +02002470 /* Strictly speaking the Core Specification (4.1) allows sending
2471 * an empty address which would force us to rely on just the IRK
2472 * as "identity information". However, since such
2473 * implementations are not known of and in order to not over
2474 * complicate our implementation, simply pretend that we never
2475 * received an IRK for such a device.
Johan Hedberge12af482015-01-14 20:51:37 +02002476 *
2477 * The Identity Address must also be a Static Random or Public
2478 * Address, which hci_is_identity_address() checks for.
Johan Hedberga9a58f82014-02-25 22:24:37 +02002479 */
Johan Hedberge12af482015-01-14 20:51:37 +02002480 if (!bacmp(&info->bdaddr, BDADDR_ANY) ||
2481 !hci_is_identity_address(&info->bdaddr, info->addr_type)) {
Johan Hedberga9a58f82014-02-25 22:24:37 +02002482 BT_ERR("Ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03002483 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02002484 }
2485
Johan Hedbergfd349c02014-02-18 10:19:36 +02002486 bacpy(&smp->id_addr, &info->bdaddr);
2487 smp->id_addr_type = info->addr_type;
2488
2489 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2490 bacpy(&rpa, &hcon->dst);
2491 else
2492 bacpy(&rpa, BDADDR_ANY);
2493
Johan Hedberg23d0e122014-02-19 14:57:46 +02002494 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2495 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002496
Johan Hedberg31dd6242014-06-27 14:23:02 +03002497distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002498 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2499 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002500
2501 return 0;
2502}
2503
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002504static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2505{
2506 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002507 struct l2cap_chan *chan = conn->smp;
2508 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002509 struct smp_csrk *csrk;
2510
2511 BT_DBG("conn %p", conn);
2512
2513 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002514 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002515
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002516 /* Mark the information as received */
2517 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2518
2519 skb_pull(skb, sizeof(*rp));
2520
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002521 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2522 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02002523 if (conn->hcon->sec_level > BT_SECURITY_MEDIUM)
2524 csrk->type = MGMT_CSRK_REMOTE_AUTHENTICATED;
2525 else
2526 csrk->type = MGMT_CSRK_REMOTE_UNAUTHENTICATED;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002527 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2528 }
2529 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03002530 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002531
2532 return 0;
2533}
2534
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002535static u8 sc_select_method(struct smp_chan *smp)
2536{
2537 struct l2cap_conn *conn = smp->conn;
2538 struct hci_conn *hcon = conn->hcon;
2539 struct smp_cmd_pairing *local, *remote;
2540 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2541
Johan Hedberg1a8bab42015-03-16 11:45:44 +02002542 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags) ||
2543 test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags))
Johan Hedberga29b0732014-10-28 15:17:05 +01002544 return REQ_OOB;
2545
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002546 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2547 * which are needed as inputs to some crypto functions. To get
2548 * the "struct smp_cmd_pairing" from them we need to skip the
2549 * first byte which contains the opcode.
2550 */
2551 if (hcon->out) {
2552 local = (void *) &smp->preq[1];
2553 remote = (void *) &smp->prsp[1];
2554 } else {
2555 local = (void *) &smp->prsp[1];
2556 remote = (void *) &smp->preq[1];
2557 }
2558
2559 local_io = local->io_capability;
2560 remote_io = remote->io_capability;
2561
2562 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2563 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2564
2565 /* If either side wants MITM, look up the method from the table,
2566 * otherwise use JUST WORKS.
2567 */
2568 if (local_mitm || remote_mitm)
2569 method = get_auth_method(smp, local_io, remote_io);
2570 else
2571 method = JUST_WORKS;
2572
2573 /* Don't confirm locally initiated pairing attempts */
2574 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2575 method = JUST_WORKS;
2576
2577 return method;
2578}
2579
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002580static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2581{
2582 struct smp_cmd_public_key *key = (void *) skb->data;
2583 struct hci_conn *hcon = conn->hcon;
2584 struct l2cap_chan *chan = conn->smp;
2585 struct smp_chan *smp = chan->data;
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002586 struct hci_dev *hdev = hcon->hdev;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002587 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002588 int err;
2589
2590 BT_DBG("conn %p", conn);
2591
2592 if (skb->len < sizeof(*key))
2593 return SMP_INVALID_PARAMS;
2594
2595 memcpy(smp->remote_pk, key, 64);
2596
Johan Hedberga8ca6172015-03-16 18:12:57 +02002597 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags)) {
2598 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2599 smp->rr, 0, cfm.confirm_val);
2600 if (err)
2601 return SMP_UNSPECIFIED;
2602
2603 if (memcmp(cfm.confirm_val, smp->pcnf, 16))
2604 return SMP_CONFIRM_FAILED;
2605 }
2606
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002607 /* Non-initiating device sends its public key after receiving
2608 * the key from the initiating device.
2609 */
2610 if (!hcon->out) {
2611 err = sc_send_public_key(smp);
2612 if (err)
2613 return err;
2614 }
2615
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002616 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
Marcel Holtmanne0915262015-03-16 12:34:55 -07002617 SMP_DBG("Remote Public Key Y: %32phN", smp->remote_pk + 32);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002618
2619 if (!ecdh_shared_secret(smp->remote_pk, smp->local_sk, smp->dhkey))
2620 return SMP_UNSPECIFIED;
2621
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002622 SMP_DBG("DHKey %32phN", smp->dhkey);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002623
2624 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2625
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002626 smp->method = sc_select_method(smp);
2627
2628 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2629
2630 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2631 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2632 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2633 else
2634 hcon->pending_sec_level = BT_SECURITY_FIPS;
2635
Johan Hedbergaeb7d462014-05-31 18:52:28 +03002636 if (!memcmp(debug_pk, smp->remote_pk, 64))
2637 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2638
Johan Hedberg38606f12014-06-25 11:10:28 +03002639 if (smp->method == DSP_PASSKEY) {
2640 get_random_bytes(&hcon->passkey_notify,
2641 sizeof(hcon->passkey_notify));
2642 hcon->passkey_notify %= 1000000;
2643 hcon->passkey_entered = 0;
2644 smp->passkey_round = 0;
2645 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2646 hcon->dst_type,
2647 hcon->passkey_notify,
2648 hcon->passkey_entered))
2649 return SMP_UNSPECIFIED;
2650 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2651 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2652 }
2653
Johan Hedberg94ea7252015-03-16 11:45:46 +02002654 if (smp->method == REQ_OOB) {
Johan Hedberga29b0732014-10-28 15:17:05 +01002655 if (hcon->out)
2656 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2657 sizeof(smp->prnd), smp->prnd);
2658
2659 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2660
2661 return 0;
2662 }
2663
Johan Hedberg38606f12014-06-25 11:10:28 +03002664 if (hcon->out)
2665 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2666
2667 if (smp->method == REQ_PASSKEY) {
2668 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2669 hcon->dst_type))
2670 return SMP_UNSPECIFIED;
2671 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2672 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2673 return 0;
2674 }
2675
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002676 /* The Initiating device waits for the non-initiating device to
2677 * send the confirm value.
2678 */
2679 if (conn->hcon->out)
2680 return 0;
2681
2682 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2683 0, cfm.confirm_val);
2684 if (err)
2685 return SMP_UNSPECIFIED;
2686
2687 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2688 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2689
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002690 return 0;
2691}
2692
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002693static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2694{
2695 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2696 struct l2cap_chan *chan = conn->smp;
2697 struct hci_conn *hcon = conn->hcon;
2698 struct smp_chan *smp = chan->data;
2699 u8 a[7], b[7], *local_addr, *remote_addr;
2700 u8 io_cap[3], r[16], e[16];
2701 int err;
2702
2703 BT_DBG("conn %p", conn);
2704
2705 if (skb->len < sizeof(*check))
2706 return SMP_INVALID_PARAMS;
2707
2708 memcpy(a, &hcon->init_addr, 6);
2709 memcpy(b, &hcon->resp_addr, 6);
2710 a[6] = hcon->init_addr_type;
2711 b[6] = hcon->resp_addr_type;
2712
2713 if (hcon->out) {
2714 local_addr = a;
2715 remote_addr = b;
2716 memcpy(io_cap, &smp->prsp[1], 3);
2717 } else {
2718 local_addr = b;
2719 remote_addr = a;
2720 memcpy(io_cap, &smp->preq[1], 3);
2721 }
2722
2723 memset(r, 0, sizeof(r));
2724
Johan Hedberg38606f12014-06-25 11:10:28 +03002725 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2726 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg882fafa2015-03-16 11:45:43 +02002727 else if (smp->method == REQ_OOB)
2728 memcpy(r, smp->lr, 16);
Johan Hedberg38606f12014-06-25 11:10:28 +03002729
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002730 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2731 io_cap, remote_addr, local_addr, e);
2732 if (err)
2733 return SMP_UNSPECIFIED;
2734
2735 if (memcmp(check->e, e, 16))
2736 return SMP_DHKEY_CHECK_FAILED;
2737
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002738 if (!hcon->out) {
2739 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2740 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2741 return 0;
2742 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002743
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002744 /* Slave sends DHKey check as response to master */
2745 sc_dhkey_check(smp);
2746 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002747
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002748 sc_add_ltk(smp);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002749
2750 if (hcon->out) {
Johan Hedberg8b76ce32015-06-08 18:14:39 +03002751 hci_le_start_enc(hcon, 0, 0, smp->tk, smp->enc_key_size);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002752 hcon->enc_key_size = smp->enc_key_size;
2753 }
2754
2755 return 0;
2756}
2757
Johan Hedberg1408bb62014-06-04 22:45:57 +03002758static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2759 struct sk_buff *skb)
2760{
2761 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2762
2763 BT_DBG("value 0x%02x", kp->value);
2764
2765 return 0;
2766}
2767
Johan Hedberg4befb862014-08-11 22:06:38 +03002768static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002769{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002770 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002771 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002772 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002773 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002774 int err = 0;
2775
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002776 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07002777 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002778
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002779 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002780 reason = SMP_PAIRING_NOTSUPP;
2781 goto done;
2782 }
2783
Marcel Holtmann92381f52013-10-03 01:23:08 -07002784 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002785 skb_pull(skb, sizeof(code));
2786
Johan Hedbergb28b4942014-09-05 22:19:55 +03002787 smp = chan->data;
2788
2789 if (code > SMP_CMD_MAX)
2790 goto drop;
2791
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07002792 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03002793 goto drop;
2794
2795 /* If we don't have a context the only allowed commands are
2796 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002797 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03002798 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2799 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002800
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002801 switch (code) {
2802 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002803 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002804 break;
2805
2806 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02002807 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002808 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002809 break;
2810
2811 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002812 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002813 break;
2814
2815 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002816 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002817 break;
2818
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002819 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002820 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002821 break;
2822
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002823 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002824 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002825 break;
2826
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002827 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002828 reason = smp_cmd_encrypt_info(conn, skb);
2829 break;
2830
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002831 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002832 reason = smp_cmd_master_ident(conn, skb);
2833 break;
2834
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002835 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002836 reason = smp_cmd_ident_info(conn, skb);
2837 break;
2838
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002839 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002840 reason = smp_cmd_ident_addr_info(conn, skb);
2841 break;
2842
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002843 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002844 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002845 break;
2846
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002847 case SMP_CMD_PUBLIC_KEY:
2848 reason = smp_cmd_public_key(conn, skb);
2849 break;
2850
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002851 case SMP_CMD_DHKEY_CHECK:
2852 reason = smp_cmd_dhkey_check(conn, skb);
2853 break;
2854
Johan Hedberg1408bb62014-06-04 22:45:57 +03002855 case SMP_CMD_KEYPRESS_NOTIFY:
2856 reason = smp_cmd_keypress_notify(conn, skb);
2857 break;
2858
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002859 default:
2860 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002861 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002862 goto done;
2863 }
2864
2865done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002866 if (!err) {
2867 if (reason)
2868 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002869 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002870 }
2871
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002872 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002873
2874drop:
2875 BT_ERR("%s unexpected SMP command 0x%02x from %pMR", hcon->hdev->name,
2876 code, &hcon->dst);
2877 kfree_skb(skb);
2878 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002879}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002880
Johan Hedberg70db83c2014-08-08 09:37:16 +03002881static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2882{
2883 struct l2cap_conn *conn = chan->conn;
2884
2885 BT_DBG("chan %p", chan);
2886
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002887 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002888 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002889
Johan Hedberg70db83c2014-08-08 09:37:16 +03002890 conn->smp = NULL;
2891 l2cap_chan_put(chan);
2892}
2893
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002894static void bredr_pairing(struct l2cap_chan *chan)
2895{
2896 struct l2cap_conn *conn = chan->conn;
2897 struct hci_conn *hcon = conn->hcon;
2898 struct hci_dev *hdev = hcon->hdev;
2899 struct smp_cmd_pairing req;
2900 struct smp_chan *smp;
2901
2902 BT_DBG("chan %p", chan);
2903
2904 /* Only new pairings are interesting */
2905 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
2906 return;
2907
2908 /* Don't bother if we're not encrypted */
2909 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2910 return;
2911
2912 /* Only master may initiate SMP over BR/EDR */
2913 if (hcon->role != HCI_ROLE_MASTER)
2914 return;
2915
2916 /* Secure Connections support must be enabled */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002917 if (!hci_dev_test_flag(hdev, HCI_SC_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002918 return;
2919
2920 /* BR/EDR must use Secure Connections for SMP */
2921 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07002922 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002923 return;
2924
2925 /* If our LE support is not enabled don't do anything */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002926 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002927 return;
2928
2929 /* Don't bother if remote LE support is not enabled */
2930 if (!lmp_host_le_capable(hcon))
2931 return;
2932
2933 /* Remote must support SMP fixed chan for BR/EDR */
2934 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
2935 return;
2936
2937 /* Don't bother if SMP is already ongoing */
2938 if (chan->data)
2939 return;
2940
2941 smp = smp_chan_create(conn);
2942 if (!smp) {
2943 BT_ERR("%s unable to create SMP context for BR/EDR",
2944 hdev->name);
2945 return;
2946 }
2947
2948 set_bit(SMP_FLAG_SC, &smp->flags);
2949
2950 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
2951
2952 /* Prepare and send the BR/EDR SMP Pairing Request */
2953 build_bredr_pairing_cmd(smp, &req, NULL);
2954
2955 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2956 memcpy(&smp->preq[1], &req, sizeof(req));
2957
2958 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
2959 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
2960}
2961
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002962static void smp_resume_cb(struct l2cap_chan *chan)
2963{
Johan Hedbergb68fda62014-08-11 22:06:40 +03002964 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002965 struct l2cap_conn *conn = chan->conn;
2966 struct hci_conn *hcon = conn->hcon;
2967
2968 BT_DBG("chan %p", chan);
2969
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002970 if (hcon->type == ACL_LINK) {
2971 bredr_pairing(chan);
Johan Hedbergef8efe42014-08-13 15:12:32 +03002972 return;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002973 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03002974
Johan Hedberg86d14072014-08-11 22:06:43 +03002975 if (!smp)
2976 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03002977
Johan Hedberg84bc0db2014-09-05 22:19:49 +03002978 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2979 return;
2980
Johan Hedberg86d14072014-08-11 22:06:43 +03002981 cancel_delayed_work(&smp->security_timer);
2982
Johan Hedbergd6268e82014-09-05 22:19:51 +03002983 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03002984}
2985
Johan Hedberg70db83c2014-08-08 09:37:16 +03002986static void smp_ready_cb(struct l2cap_chan *chan)
2987{
2988 struct l2cap_conn *conn = chan->conn;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002989 struct hci_conn *hcon = conn->hcon;
Johan Hedberg70db83c2014-08-08 09:37:16 +03002990
2991 BT_DBG("chan %p", chan);
2992
2993 conn->smp = chan;
2994 l2cap_chan_hold(chan);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03002995
2996 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
2997 bredr_pairing(chan);
Johan Hedberg70db83c2014-08-08 09:37:16 +03002998}
2999
Johan Hedberg4befb862014-08-11 22:06:38 +03003000static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
3001{
3002 int err;
3003
3004 BT_DBG("chan %p", chan);
3005
3006 err = smp_sig_channel(chan, skb);
3007 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03003008 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03003009
Johan Hedbergb68fda62014-08-11 22:06:40 +03003010 if (smp)
3011 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03003012
Johan Hedberg1e91c292014-08-18 20:33:29 +03003013 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03003014 }
3015
3016 return err;
3017}
3018
Johan Hedberg70db83c2014-08-08 09:37:16 +03003019static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
3020 unsigned long hdr_len,
3021 unsigned long len, int nb)
3022{
3023 struct sk_buff *skb;
3024
3025 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
3026 if (!skb)
3027 return ERR_PTR(-ENOMEM);
3028
3029 skb->priority = HCI_PRIO_MAX;
Johan Hedberga4368ff2015-03-30 23:21:01 +03003030 bt_cb(skb)->l2cap.chan = chan;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003031
3032 return skb;
3033}
3034
3035static const struct l2cap_ops smp_chan_ops = {
3036 .name = "Security Manager",
3037 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03003038 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003039 .alloc_skb = smp_alloc_skb_cb,
3040 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003041 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003042
3043 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003044 .state_change = l2cap_chan_no_state_change,
3045 .close = l2cap_chan_no_close,
3046 .defer = l2cap_chan_no_defer,
3047 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003048 .set_shutdown = l2cap_chan_no_set_shutdown,
3049 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003050};
3051
3052static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
3053{
3054 struct l2cap_chan *chan;
3055
3056 BT_DBG("pchan %p", pchan);
3057
3058 chan = l2cap_chan_create();
3059 if (!chan)
3060 return NULL;
3061
3062 chan->chan_type = pchan->chan_type;
3063 chan->ops = &smp_chan_ops;
3064 chan->scid = pchan->scid;
3065 chan->dcid = chan->scid;
3066 chan->imtu = pchan->imtu;
3067 chan->omtu = pchan->omtu;
3068 chan->mode = pchan->mode;
3069
Johan Hedbergabe84902014-11-12 22:22:21 +02003070 /* Other L2CAP channels may request SMP routines in order to
3071 * change the security level. This means that the SMP channel
3072 * lock must be considered in its own category to avoid lockdep
3073 * warnings.
3074 */
3075 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
3076
Johan Hedberg70db83c2014-08-08 09:37:16 +03003077 BT_DBG("created chan %p", chan);
3078
3079 return chan;
3080}
3081
3082static const struct l2cap_ops smp_root_chan_ops = {
3083 .name = "Security Manager Root",
3084 .new_connection = smp_new_conn_cb,
3085
3086 /* None of these are implemented for the root channel */
3087 .close = l2cap_chan_no_close,
3088 .alloc_skb = l2cap_chan_no_alloc_skb,
3089 .recv = l2cap_chan_no_recv,
3090 .state_change = l2cap_chan_no_state_change,
3091 .teardown = l2cap_chan_no_teardown,
3092 .ready = l2cap_chan_no_ready,
3093 .defer = l2cap_chan_no_defer,
3094 .suspend = l2cap_chan_no_suspend,
3095 .resume = l2cap_chan_no_resume,
3096 .set_shutdown = l2cap_chan_no_set_shutdown,
3097 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003098};
3099
Johan Hedbergef8efe42014-08-13 15:12:32 +03003100static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003101{
Johan Hedberg70db83c2014-08-08 09:37:16 +03003102 struct l2cap_chan *chan;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003103 struct smp_dev *smp;
3104 struct crypto_blkcipher *tfm_aes;
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003105 struct crypto_hash *tfm_cmac;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003106
Johan Hedbergef8efe42014-08-13 15:12:32 +03003107 if (cid == L2CAP_CID_SMP_BREDR) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003108 smp = NULL;
Johan Hedbergef8efe42014-08-13 15:12:32 +03003109 goto create_chan;
3110 }
Johan Hedberg711eafe2014-08-08 09:32:52 +03003111
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003112 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
3113 if (!smp)
3114 return ERR_PTR(-ENOMEM);
3115
3116 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003117 if (IS_ERR(tfm_aes)) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003118 BT_ERR("Unable to create ECB crypto context");
3119 kzfree(smp);
Fengguang Wufe700772014-12-08 03:04:38 +08003120 return ERR_CAST(tfm_aes);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003121 }
3122
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003123 tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
3124 if (IS_ERR(tfm_cmac)) {
3125 BT_ERR("Unable to create CMAC crypto context");
3126 crypto_free_blkcipher(tfm_aes);
3127 kzfree(smp);
3128 return ERR_CAST(tfm_cmac);
3129 }
3130
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003131 smp->tfm_aes = tfm_aes;
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003132 smp->tfm_cmac = tfm_cmac;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003133
Johan Hedbergef8efe42014-08-13 15:12:32 +03003134create_chan:
Johan Hedberg70db83c2014-08-08 09:37:16 +03003135 chan = l2cap_chan_create();
3136 if (!chan) {
Marcel Holtmann63511f6d2015-03-17 11:38:24 -07003137 if (smp) {
3138 crypto_free_blkcipher(smp->tfm_aes);
3139 crypto_free_hash(smp->tfm_cmac);
3140 kzfree(smp);
3141 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003142 return ERR_PTR(-ENOMEM);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003143 }
3144
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003145 chan->data = smp;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003146
Johan Hedbergef8efe42014-08-13 15:12:32 +03003147 l2cap_add_scid(chan, cid);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003148
3149 l2cap_chan_set_defaults(chan);
3150
Marcel Holtmann157029b2015-01-14 15:43:09 -08003151 if (cid == L2CAP_CID_SMP) {
Johan Hedberg39e3e742015-02-20 13:48:24 +02003152 u8 bdaddr_type;
3153
3154 hci_copy_identity_address(hdev, &chan->src, &bdaddr_type);
3155
3156 if (bdaddr_type == ADDR_LE_DEV_PUBLIC)
Marcel Holtmann157029b2015-01-14 15:43:09 -08003157 chan->src_type = BDADDR_LE_PUBLIC;
Johan Hedberg39e3e742015-02-20 13:48:24 +02003158 else
3159 chan->src_type = BDADDR_LE_RANDOM;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003160 } else {
3161 bacpy(&chan->src, &hdev->bdaddr);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003162 chan->src_type = BDADDR_BREDR;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003163 }
3164
Johan Hedberg70db83c2014-08-08 09:37:16 +03003165 chan->state = BT_LISTEN;
3166 chan->mode = L2CAP_MODE_BASIC;
3167 chan->imtu = L2CAP_DEFAULT_MTU;
3168 chan->ops = &smp_root_chan_ops;
3169
Johan Hedbergabe84902014-11-12 22:22:21 +02003170 /* Set correct nesting level for a parent/listening channel */
3171 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
3172
Johan Hedbergef8efe42014-08-13 15:12:32 +03003173 return chan;
Johan Hedberg711eafe2014-08-08 09:32:52 +03003174}
3175
Johan Hedbergef8efe42014-08-13 15:12:32 +03003176static void smp_del_chan(struct l2cap_chan *chan)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003177{
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003178 struct smp_dev *smp;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003179
Johan Hedbergef8efe42014-08-13 15:12:32 +03003180 BT_DBG("chan %p", chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003181
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003182 smp = chan->data;
3183 if (smp) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003184 chan->data = NULL;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003185 if (smp->tfm_aes)
3186 crypto_free_blkcipher(smp->tfm_aes);
Marcel Holtmann6e2dc6d2015-03-16 01:10:21 -07003187 if (smp->tfm_cmac)
3188 crypto_free_hash(smp->tfm_cmac);
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003189 kzfree(smp);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003190 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03003191
Johan Hedberg70db83c2014-08-08 09:37:16 +03003192 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003193}
Johan Hedbergef8efe42014-08-13 15:12:32 +03003194
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003195static ssize_t force_bredr_smp_read(struct file *file,
3196 char __user *user_buf,
3197 size_t count, loff_t *ppos)
3198{
3199 struct hci_dev *hdev = file->private_data;
3200 char buf[3];
3201
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003202 buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP) ? 'Y': 'N';
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003203 buf[1] = '\n';
3204 buf[2] = '\0';
3205 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3206}
3207
3208static ssize_t force_bredr_smp_write(struct file *file,
3209 const char __user *user_buf,
3210 size_t count, loff_t *ppos)
3211{
3212 struct hci_dev *hdev = file->private_data;
3213 char buf[32];
3214 size_t buf_size = min(count, (sizeof(buf)-1));
3215 bool enable;
3216
3217 if (copy_from_user(buf, user_buf, buf_size))
3218 return -EFAULT;
3219
3220 buf[buf_size] = '\0';
3221 if (strtobool(buf, &enable))
3222 return -EINVAL;
3223
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003224 if (enable == hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003225 return -EALREADY;
3226
3227 if (enable) {
3228 struct l2cap_chan *chan;
3229
3230 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3231 if (IS_ERR(chan))
3232 return PTR_ERR(chan);
3233
3234 hdev->smp_bredr_data = chan;
3235 } else {
3236 struct l2cap_chan *chan;
3237
3238 chan = hdev->smp_bredr_data;
3239 hdev->smp_bredr_data = NULL;
3240 smp_del_chan(chan);
3241 }
3242
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003243 hci_dev_change_flag(hdev, HCI_FORCE_BREDR_SMP);
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003244
3245 return count;
3246}
3247
3248static const struct file_operations force_bredr_smp_fops = {
3249 .open = simple_open,
3250 .read = force_bredr_smp_read,
3251 .write = force_bredr_smp_write,
3252 .llseek = default_llseek,
3253};
3254
Johan Hedbergef8efe42014-08-13 15:12:32 +03003255int smp_register(struct hci_dev *hdev)
3256{
3257 struct l2cap_chan *chan;
3258
3259 BT_DBG("%s", hdev->name);
3260
Marcel Holtmann7e7ec442015-01-14 15:43:10 -08003261 /* If the controller does not support Low Energy operation, then
3262 * there is also no need to register any SMP channel.
3263 */
3264 if (!lmp_le_capable(hdev))
3265 return 0;
3266
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003267 if (WARN_ON(hdev->smp_data)) {
3268 chan = hdev->smp_data;
3269 hdev->smp_data = NULL;
3270 smp_del_chan(chan);
3271 }
3272
Johan Hedbergef8efe42014-08-13 15:12:32 +03003273 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
3274 if (IS_ERR(chan))
3275 return PTR_ERR(chan);
3276
3277 hdev->smp_data = chan;
3278
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003279 /* If the controller does not support BR/EDR Secure Connections
3280 * feature, then the BR/EDR SMP channel shall not be present.
3281 *
3282 * To test this with Bluetooth 4.0 controllers, create a debugfs
3283 * switch that allows forcing BR/EDR SMP support and accepting
3284 * cross-transport pairing on non-AES encrypted connections.
3285 */
3286 if (!lmp_sc_capable(hdev)) {
3287 debugfs_create_file("force_bredr_smp", 0644, hdev->debugfs,
3288 hdev, &force_bredr_smp_fops);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003289 return 0;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003290 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003291
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003292 if (WARN_ON(hdev->smp_bredr_data)) {
3293 chan = hdev->smp_bredr_data;
3294 hdev->smp_bredr_data = NULL;
3295 smp_del_chan(chan);
3296 }
3297
Johan Hedbergef8efe42014-08-13 15:12:32 +03003298 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3299 if (IS_ERR(chan)) {
3300 int err = PTR_ERR(chan);
3301 chan = hdev->smp_data;
3302 hdev->smp_data = NULL;
3303 smp_del_chan(chan);
3304 return err;
3305 }
3306
3307 hdev->smp_bredr_data = chan;
3308
3309 return 0;
3310}
3311
3312void smp_unregister(struct hci_dev *hdev)
3313{
3314 struct l2cap_chan *chan;
3315
3316 if (hdev->smp_bredr_data) {
3317 chan = hdev->smp_bredr_data;
3318 hdev->smp_bredr_data = NULL;
3319 smp_del_chan(chan);
3320 }
3321
3322 if (hdev->smp_data) {
3323 chan = hdev->smp_data;
3324 hdev->smp_data = NULL;
3325 smp_del_chan(chan);
3326 }
3327}
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003328
3329#if IS_ENABLED(CONFIG_BT_SELFTEST_SMP)
3330
Johan Hedbergcfc41982014-12-30 09:50:40 +02003331static int __init test_ah(struct crypto_blkcipher *tfm_aes)
3332{
3333 const u8 irk[16] = {
3334 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3335 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3336 const u8 r[3] = { 0x94, 0x81, 0x70 };
3337 const u8 exp[3] = { 0xaa, 0xfb, 0x0d };
3338 u8 res[3];
3339 int err;
3340
3341 err = smp_ah(tfm_aes, irk, r, res);
3342 if (err)
3343 return err;
3344
3345 if (memcmp(res, exp, 3))
3346 return -EINVAL;
3347
3348 return 0;
3349}
3350
3351static int __init test_c1(struct crypto_blkcipher *tfm_aes)
3352{
3353 const u8 k[16] = {
3354 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3355 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3356 const u8 r[16] = {
3357 0xe0, 0x2e, 0x70, 0xc6, 0x4e, 0x27, 0x88, 0x63,
3358 0x0e, 0x6f, 0xad, 0x56, 0x21, 0xd5, 0x83, 0x57 };
3359 const u8 preq[7] = { 0x01, 0x01, 0x00, 0x00, 0x10, 0x07, 0x07 };
3360 const u8 pres[7] = { 0x02, 0x03, 0x00, 0x00, 0x08, 0x00, 0x05 };
3361 const u8 _iat = 0x01;
3362 const u8 _rat = 0x00;
3363 const bdaddr_t ra = { { 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1 } };
3364 const bdaddr_t ia = { { 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1 } };
3365 const u8 exp[16] = {
3366 0x86, 0x3b, 0xf1, 0xbe, 0xc5, 0x4d, 0xa7, 0xd2,
3367 0xea, 0x88, 0x89, 0x87, 0xef, 0x3f, 0x1e, 0x1e };
3368 u8 res[16];
3369 int err;
3370
3371 err = smp_c1(tfm_aes, k, r, preq, pres, _iat, &ia, _rat, &ra, res);
3372 if (err)
3373 return err;
3374
3375 if (memcmp(res, exp, 16))
3376 return -EINVAL;
3377
3378 return 0;
3379}
3380
3381static int __init test_s1(struct crypto_blkcipher *tfm_aes)
3382{
3383 const u8 k[16] = {
3384 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3385 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3386 const u8 r1[16] = {
3387 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };
3388 const u8 r2[16] = {
3389 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99 };
3390 const u8 exp[16] = {
3391 0x62, 0xa0, 0x6d, 0x79, 0xae, 0x16, 0x42, 0x5b,
3392 0x9b, 0xf4, 0xb0, 0xe8, 0xf0, 0xe1, 0x1f, 0x9a };
3393 u8 res[16];
3394 int err;
3395
3396 err = smp_s1(tfm_aes, k, r1, r2, res);
3397 if (err)
3398 return err;
3399
3400 if (memcmp(res, exp, 16))
3401 return -EINVAL;
3402
3403 return 0;
3404}
3405
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003406static int __init test_f4(struct crypto_hash *tfm_cmac)
3407{
3408 const u8 u[32] = {
3409 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3410 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3411 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3412 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3413 const u8 v[32] = {
3414 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3415 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3416 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3417 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3418 const u8 x[16] = {
3419 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3420 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3421 const u8 z = 0x00;
3422 const u8 exp[16] = {
3423 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
3424 0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
3425 u8 res[16];
3426 int err;
3427
3428 err = smp_f4(tfm_cmac, u, v, x, z, res);
3429 if (err)
3430 return err;
3431
3432 if (memcmp(res, exp, 16))
3433 return -EINVAL;
3434
3435 return 0;
3436}
3437
3438static int __init test_f5(struct crypto_hash *tfm_cmac)
3439{
3440 const u8 w[32] = {
3441 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
3442 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
3443 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3444 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3445 const u8 n1[16] = {
3446 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3447 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3448 const u8 n2[16] = {
3449 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3450 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3451 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3452 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3453 const u8 exp_ltk[16] = {
3454 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98,
3455 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69 };
3456 const u8 exp_mackey[16] = {
3457 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3458 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3459 u8 mackey[16], ltk[16];
3460 int err;
3461
3462 err = smp_f5(tfm_cmac, w, n1, n2, a1, a2, mackey, ltk);
3463 if (err)
3464 return err;
3465
3466 if (memcmp(mackey, exp_mackey, 16))
3467 return -EINVAL;
3468
3469 if (memcmp(ltk, exp_ltk, 16))
3470 return -EINVAL;
3471
3472 return 0;
3473}
3474
3475static int __init test_f6(struct crypto_hash *tfm_cmac)
3476{
3477 const u8 w[16] = {
3478 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3479 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3480 const u8 n1[16] = {
3481 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3482 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3483 const u8 n2[16] = {
3484 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3485 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3486 const u8 r[16] = {
3487 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
3488 0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
3489 const u8 io_cap[3] = { 0x02, 0x01, 0x01 };
3490 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3491 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3492 const u8 exp[16] = {
3493 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
3494 0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
3495 u8 res[16];
3496 int err;
3497
3498 err = smp_f6(tfm_cmac, w, n1, n2, r, io_cap, a1, a2, res);
3499 if (err)
3500 return err;
3501
3502 if (memcmp(res, exp, 16))
3503 return -EINVAL;
3504
3505 return 0;
3506}
3507
3508static int __init test_g2(struct crypto_hash *tfm_cmac)
3509{
3510 const u8 u[32] = {
3511 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3512 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3513 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3514 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3515 const u8 v[32] = {
3516 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3517 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3518 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3519 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3520 const u8 x[16] = {
3521 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3522 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3523 const u8 y[16] = {
3524 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3525 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3526 const u32 exp_val = 0x2f9ed5ba % 1000000;
3527 u32 val;
3528 int err;
3529
3530 err = smp_g2(tfm_cmac, u, v, x, y, &val);
3531 if (err)
3532 return err;
3533
3534 if (val != exp_val)
3535 return -EINVAL;
3536
3537 return 0;
3538}
3539
3540static int __init test_h6(struct crypto_hash *tfm_cmac)
3541{
3542 const u8 w[16] = {
3543 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3544 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3545 const u8 key_id[4] = { 0x72, 0x62, 0x65, 0x6c };
3546 const u8 exp[16] = {
3547 0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8,
3548 0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d };
3549 u8 res[16];
3550 int err;
3551
3552 err = smp_h6(tfm_cmac, w, key_id, res);
3553 if (err)
3554 return err;
3555
3556 if (memcmp(res, exp, 16))
3557 return -EINVAL;
3558
3559 return 0;
3560}
3561
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003562static char test_smp_buffer[32];
3563
3564static ssize_t test_smp_read(struct file *file, char __user *user_buf,
3565 size_t count, loff_t *ppos)
3566{
3567 return simple_read_from_buffer(user_buf, count, ppos, test_smp_buffer,
3568 strlen(test_smp_buffer));
3569}
3570
3571static const struct file_operations test_smp_fops = {
3572 .open = simple_open,
3573 .read = test_smp_read,
3574 .llseek = default_llseek,
3575};
3576
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003577static int __init run_selftests(struct crypto_blkcipher *tfm_aes,
3578 struct crypto_hash *tfm_cmac)
3579{
Marcel Holtmann255047b2014-12-30 00:11:20 -08003580 ktime_t calltime, delta, rettime;
3581 unsigned long long duration;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003582 int err;
3583
Marcel Holtmann255047b2014-12-30 00:11:20 -08003584 calltime = ktime_get();
3585
Johan Hedbergcfc41982014-12-30 09:50:40 +02003586 err = test_ah(tfm_aes);
3587 if (err) {
3588 BT_ERR("smp_ah test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003589 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003590 }
3591
3592 err = test_c1(tfm_aes);
3593 if (err) {
3594 BT_ERR("smp_c1 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003595 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003596 }
3597
3598 err = test_s1(tfm_aes);
3599 if (err) {
3600 BT_ERR("smp_s1 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003601 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003602 }
3603
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003604 err = test_f4(tfm_cmac);
3605 if (err) {
3606 BT_ERR("smp_f4 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003607 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003608 }
3609
3610 err = test_f5(tfm_cmac);
3611 if (err) {
3612 BT_ERR("smp_f5 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003613 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003614 }
3615
3616 err = test_f6(tfm_cmac);
3617 if (err) {
3618 BT_ERR("smp_f6 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003619 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003620 }
3621
3622 err = test_g2(tfm_cmac);
3623 if (err) {
3624 BT_ERR("smp_g2 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003625 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003626 }
3627
3628 err = test_h6(tfm_cmac);
3629 if (err) {
3630 BT_ERR("smp_h6 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003631 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003632 }
3633
Marcel Holtmann255047b2014-12-30 00:11:20 -08003634 rettime = ktime_get();
3635 delta = ktime_sub(rettime, calltime);
3636 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
3637
Marcel Holtmann5ced2462015-01-12 23:09:48 -08003638 BT_INFO("SMP test passed in %llu usecs", duration);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003639
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003640done:
3641 if (!err)
3642 snprintf(test_smp_buffer, sizeof(test_smp_buffer),
3643 "PASS (%llu usecs)\n", duration);
3644 else
3645 snprintf(test_smp_buffer, sizeof(test_smp_buffer), "FAIL\n");
3646
3647 debugfs_create_file("selftest_smp", 0444, bt_debugfs, NULL,
3648 &test_smp_fops);
3649
3650 return err;
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003651}
3652
3653int __init bt_selftest_smp(void)
3654{
3655 struct crypto_blkcipher *tfm_aes;
3656 struct crypto_hash *tfm_cmac;
3657 int err;
3658
3659 tfm_aes = crypto_alloc_blkcipher("ecb(aes)", 0, CRYPTO_ALG_ASYNC);
3660 if (IS_ERR(tfm_aes)) {
3661 BT_ERR("Unable to create ECB crypto context");
3662 return PTR_ERR(tfm_aes);
3663 }
3664
3665 tfm_cmac = crypto_alloc_hash("cmac(aes)", 0, CRYPTO_ALG_ASYNC);
3666 if (IS_ERR(tfm_cmac)) {
3667 BT_ERR("Unable to create CMAC crypto context");
3668 crypto_free_blkcipher(tfm_aes);
3669 return PTR_ERR(tfm_cmac);
3670 }
3671
3672 err = run_selftests(tfm_aes, tfm_cmac);
3673
3674 crypto_free_hash(tfm_cmac);
3675 crypto_free_blkcipher(tfm_aes);
3676
3677 return err;
3678}
3679
3680#endif