blob: 26e8cfad22b8ed9e209e5edfe4b7fd773785b715 [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/scatterlist.h>
Andy Lutomirskia4770e12016-06-26 14:55:23 -070025#include <linux/crypto.h>
Ard Biesheuvel28a220a2019-07-02 21:41:41 +020026#include <crypto/aes.h>
Jason A. Donenfeld329d8232017-06-10 04:59:11 +020027#include <crypto/algapi.h>
Gustavo Padovan8c520a52012-05-23 04:04:22 -030028#include <crypto/b128ops.h>
Herbert Xu71af2f62016-01-24 21:18:30 +080029#include <crypto/hash.h>
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +030030#include <crypto/kpp.h>
Gustavo Padovan8c520a52012-05-23 04:04:22 -030031
Anderson Brigliaeb492e02011-06-09 18:50:40 -030032#include <net/bluetooth/bluetooth.h>
33#include <net/bluetooth/hci_core.h>
34#include <net/bluetooth/l2cap.h>
Brian Gix2b64d152011-12-21 16:12:12 -080035#include <net/bluetooth/mgmt.h>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070036
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +010037#include "ecdh_helper.h"
Marcel Holtmannac4b7232013-10-10 14:54:16 -070038#include "smp.h"
Anderson Brigliad22ef0b2011-06-09 18:50:44 -030039
Johan Hedberg2fd36552015-06-11 13:52:26 +030040#define SMP_DEV(hdev) \
41 ((struct smp_dev *)((struct l2cap_chan *)((hdev)->smp_data))->data)
42
Johan Hedbergc7a3d572014-12-01 22:03:16 +020043/* Low-level debug macros to be used for stuff that we don't want
44 * accidentially in dmesg, i.e. the values of the various crypto keys
45 * and the inputs & outputs of crypto functions.
46 */
47#ifdef DEBUG
48#define SMP_DBG(fmt, ...) printk(KERN_DEBUG "%s: " fmt, __func__, \
49 ##__VA_ARGS__)
50#else
51#define SMP_DBG(fmt, ...) no_printk(KERN_DEBUG "%s: " fmt, __func__, \
52 ##__VA_ARGS__)
53#endif
54
Johan Hedbergb28b4942014-09-05 22:19:55 +030055#define SMP_ALLOW_CMD(smp, code) set_bit(code, &smp->allow_cmd)
Johan Hedbergb28b4942014-09-05 22:19:55 +030056
Johan Hedberg3b191462014-06-06 10:50:15 +030057/* Keys which are not distributed with Secure Connections */
58#define SMP_SC_NO_DIST (SMP_DIST_ENC_KEY | SMP_DIST_LINK_KEY);
59
Marcel Holtmann17b02e62012-03-01 14:32:37 -080060#define SMP_TIMEOUT msecs_to_jiffies(30000)
Vinicius Costa Gomes5d3de7d2011-06-14 13:37:41 -030061
Marcel Holtmannd7a5a112015-03-13 02:11:00 -070062#define AUTH_REQ_MASK(dev) (hci_dev_test_flag(dev, HCI_SC_ENABLED) ? \
Johan Hedberga62da6f2016-12-08 08:32:54 +020063 0x3f : 0x07)
Johan Hedberg0edb14d2014-05-26 13:29:28 +030064#define KEY_DIST_MASK 0x07
Johan Hedberg065a13e2012-10-11 16:26:06 +020065
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +030066/* Maximum message length that can be passed to aes_cmac */
67#define CMAC_MSG_MAX 80
68
Johan Hedberg533e35d2014-06-16 19:25:18 +030069enum {
70 SMP_FLAG_TK_VALID,
71 SMP_FLAG_CFM_PENDING,
72 SMP_FLAG_MITM_AUTH,
73 SMP_FLAG_COMPLETE,
74 SMP_FLAG_INITIATOR,
Johan Hedberg65668772014-05-16 11:03:34 +030075 SMP_FLAG_SC,
Johan Hedbergd8f8edb2014-06-06 11:09:28 +030076 SMP_FLAG_REMOTE_PK,
Johan Hedbergaeb7d462014-05-31 18:52:28 +030077 SMP_FLAG_DEBUG_KEY,
Johan Hedberg38606f12014-06-25 11:10:28 +030078 SMP_FLAG_WAIT_USER,
Johan Hedbergd3e54a82014-06-04 11:07:40 +030079 SMP_FLAG_DHKEY_PENDING,
Johan Hedberg1a8bab42015-03-16 11:45:44 +020080 SMP_FLAG_REMOTE_OOB,
81 SMP_FLAG_LOCAL_OOB,
Johan Hedberga62da6f2016-12-08 08:32:54 +020082 SMP_FLAG_CT2,
Johan Hedberg533e35d2014-06-16 19:25:18 +030083};
Johan Hedberg4bc58f52014-05-20 09:45:47 +030084
Marcel Holtmann88a479d2015-03-16 01:10:19 -070085struct smp_dev {
Marcel Holtmann60a27d62015-03-16 01:10:22 -070086 /* Secure Connections OOB data */
Johan Hedberg94f14e42018-09-11 14:10:12 +030087 bool local_oob;
Marcel Holtmann60a27d62015-03-16 01:10:22 -070088 u8 local_pk[64];
Marcel Holtmannfb334fe2015-03-16 12:34:57 -070089 u8 local_rand[16];
Marcel Holtmann60a27d62015-03-16 01:10:22 -070090 bool debug_key;
91
Herbert Xu71af2f62016-01-24 21:18:30 +080092 struct crypto_shash *tfm_cmac;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +030093 struct crypto_kpp *tfm_ecdh;
Marcel Holtmann88a479d2015-03-16 01:10:19 -070094};
95
Johan Hedberg4bc58f52014-05-20 09:45:47 +030096struct smp_chan {
Johan Hedbergb68fda62014-08-11 22:06:40 +030097 struct l2cap_conn *conn;
98 struct delayed_work security_timer;
Johan Hedbergb28b4942014-09-05 22:19:55 +030099 unsigned long allow_cmd; /* Bitmask of allowed commands */
Johan Hedbergb68fda62014-08-11 22:06:40 +0300100
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300101 u8 preq[7]; /* SMP Pairing Request */
102 u8 prsp[7]; /* SMP Pairing Response */
103 u8 prnd[16]; /* SMP Pairing Random (local) */
104 u8 rrnd[16]; /* SMP Pairing Random (remote) */
105 u8 pcnf[16]; /* SMP Pairing Confirm */
106 u8 tk[16]; /* SMP Temporary Key */
Johan Hedberg882fafa2015-03-16 11:45:43 +0200107 u8 rr[16]; /* Remote OOB ra/rb value */
108 u8 lr[16]; /* Local OOB ra/rb value */
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300109 u8 enc_key_size;
110 u8 remote_key_dist;
111 bdaddr_t id_addr;
112 u8 id_addr_type;
113 u8 irk[16];
114 struct smp_csrk *csrk;
115 struct smp_csrk *slave_csrk;
116 struct smp_ltk *ltk;
117 struct smp_ltk *slave_ltk;
118 struct smp_irk *remote_irk;
Johan Hedberg6a770832014-06-06 11:54:04 +0300119 u8 *link_key;
Johan Hedberg4a74d652014-05-20 09:45:50 +0300120 unsigned long flags;
Johan Hedberg783e0572014-05-31 18:48:26 +0300121 u8 method;
Johan Hedberg38606f12014-06-25 11:10:28 +0300122 u8 passkey_round;
Johan Hedberg6a7bd102014-06-27 14:23:03 +0300123
Johan Hedberg3b191462014-06-06 10:50:15 +0300124 /* Secure Connections variables */
125 u8 local_pk[64];
Johan Hedbergd8f8edb2014-06-06 11:09:28 +0300126 u8 remote_pk[64];
127 u8 dhkey[32];
Johan Hedberg760b0182014-06-06 11:44:05 +0300128 u8 mackey[16];
Johan Hedberg3b191462014-06-06 10:50:15 +0300129
Herbert Xu71af2f62016-01-24 21:18:30 +0800130 struct crypto_shash *tfm_cmac;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +0300131 struct crypto_kpp *tfm_ecdh;
Johan Hedberg4bc58f52014-05-20 09:45:47 +0300132};
133
Johan Hedbergaeb7d462014-05-31 18:52:28 +0300134/* These debug key values are defined in the SMP section of the core
135 * specification. debug_pk is the public debug key and debug_sk the
136 * private debug key.
137 */
138static const u8 debug_pk[64] = {
139 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
140 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
141 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
142 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
143
144 0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
145 0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
146 0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
147 0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc,
148};
149
150static const u8 debug_sk[32] = {
151 0xbd, 0x1a, 0x3c, 0xcd, 0xa6, 0xb8, 0x99, 0x58,
152 0x99, 0xb7, 0x40, 0xeb, 0x7b, 0x60, 0xff, 0x4a,
153 0x50, 0x3f, 0x10, 0xd2, 0xe3, 0xb3, 0xc9, 0x74,
154 0x38, 0x5f, 0xc5, 0xa3, 0xd4, 0xf6, 0x49, 0x3f,
155};
156
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300157static inline void swap_buf(const u8 *src, u8 *dst, size_t len)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300158{
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300159 size_t i;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300160
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300161 for (i = 0; i < len; i++)
162 dst[len - 1 - i] = src[i];
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300163}
164
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200165/* The following functions map to the LE SC SMP crypto functions
166 * AES-CMAC, f4, f5, f6, g2 and h6.
167 */
168
Herbert Xu71af2f62016-01-24 21:18:30 +0800169static int aes_cmac(struct crypto_shash *tfm, const u8 k[16], const u8 *m,
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300170 size_t len, u8 mac[16])
171{
172 uint8_t tmp[16], mac_msb[16], msg_msb[CMAC_MSG_MAX];
Herbert Xu71af2f62016-01-24 21:18:30 +0800173 SHASH_DESC_ON_STACK(desc, tfm);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300174 int err;
175
176 if (len > CMAC_MSG_MAX)
177 return -EFBIG;
178
179 if (!tfm) {
180 BT_ERR("tfm %p", tfm);
181 return -EINVAL;
182 }
183
Herbert Xu71af2f62016-01-24 21:18:30 +0800184 desc->tfm = tfm;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300185
186 /* Swap key and message from LSB to MSB */
187 swap_buf(k, tmp, 16);
188 swap_buf(m, msg_msb, len);
189
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200190 SMP_DBG("msg (len %zu) %*phN", len, (int) len, m);
191 SMP_DBG("key %16phN", k);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300192
Herbert Xu71af2f62016-01-24 21:18:30 +0800193 err = crypto_shash_setkey(tfm, tmp, 16);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300194 if (err) {
195 BT_ERR("cipher setkey failed: %d", err);
196 return err;
197 }
198
Herbert Xu71af2f62016-01-24 21:18:30 +0800199 err = crypto_shash_digest(desc, msg_msb, len, mac_msb);
200 shash_desc_zero(desc);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300201 if (err) {
Herbert Xu71af2f62016-01-24 21:18:30 +0800202 BT_ERR("Hash computation error %d", err);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300203 return err;
204 }
205
206 swap_buf(mac_msb, mac, 16);
207
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200208 SMP_DBG("mac %16phN", mac);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300209
210 return 0;
211}
212
Herbert Xu71af2f62016-01-24 21:18:30 +0800213static int smp_f4(struct crypto_shash *tfm_cmac, const u8 u[32],
214 const u8 v[32], const u8 x[16], u8 z, u8 res[16])
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300215{
216 u8 m[65];
217 int err;
218
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200219 SMP_DBG("u %32phN", u);
220 SMP_DBG("v %32phN", v);
221 SMP_DBG("x %16phN z %02x", x, z);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300222
223 m[0] = z;
224 memcpy(m + 1, v, 32);
225 memcpy(m + 33, u, 32);
226
227 err = aes_cmac(tfm_cmac, x, m, sizeof(m), res);
228 if (err)
229 return err;
230
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200231 SMP_DBG("res %16phN", res);
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +0300232
233 return err;
234}
235
Herbert Xu71af2f62016-01-24 21:18:30 +0800236static int smp_f5(struct crypto_shash *tfm_cmac, const u8 w[32],
Johan Hedberg4da50de2014-12-29 12:04:10 +0200237 const u8 n1[16], const u8 n2[16], const u8 a1[7],
238 const u8 a2[7], u8 mackey[16], u8 ltk[16])
Johan Hedberg760b0182014-06-06 11:44:05 +0300239{
240 /* The btle, salt and length "magic" values are as defined in
241 * the SMP section of the Bluetooth core specification. In ASCII
242 * the btle value ends up being 'btle'. The salt is just a
243 * random number whereas length is the value 256 in little
244 * endian format.
245 */
246 const u8 btle[4] = { 0x65, 0x6c, 0x74, 0x62 };
247 const u8 salt[16] = { 0xbe, 0x83, 0x60, 0x5a, 0xdb, 0x0b, 0x37, 0x60,
248 0x38, 0xa5, 0xf5, 0xaa, 0x91, 0x83, 0x88, 0x6c };
249 const u8 length[2] = { 0x00, 0x01 };
250 u8 m[53], t[16];
251 int err;
252
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200253 SMP_DBG("w %32phN", w);
254 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
255 SMP_DBG("a1 %7phN a2 %7phN", a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300256
257 err = aes_cmac(tfm_cmac, salt, w, 32, t);
258 if (err)
259 return err;
260
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200261 SMP_DBG("t %16phN", t);
Johan Hedberg760b0182014-06-06 11:44:05 +0300262
263 memcpy(m, length, 2);
264 memcpy(m + 2, a2, 7);
265 memcpy(m + 9, a1, 7);
266 memcpy(m + 16, n2, 16);
267 memcpy(m + 32, n1, 16);
268 memcpy(m + 48, btle, 4);
269
270 m[52] = 0; /* Counter */
271
272 err = aes_cmac(tfm_cmac, t, m, sizeof(m), mackey);
273 if (err)
274 return err;
275
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200276 SMP_DBG("mackey %16phN", mackey);
Johan Hedberg760b0182014-06-06 11:44:05 +0300277
278 m[52] = 1; /* Counter */
279
280 err = aes_cmac(tfm_cmac, t, m, sizeof(m), ltk);
281 if (err)
282 return err;
283
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200284 SMP_DBG("ltk %16phN", ltk);
Johan Hedberg760b0182014-06-06 11:44:05 +0300285
286 return 0;
287}
288
Herbert Xu71af2f62016-01-24 21:18:30 +0800289static int smp_f6(struct crypto_shash *tfm_cmac, const u8 w[16],
Johan Hedberg4da50de2014-12-29 12:04:10 +0200290 const u8 n1[16], const u8 n2[16], const u8 r[16],
Johan Hedberg760b0182014-06-06 11:44:05 +0300291 const u8 io_cap[3], const u8 a1[7], const u8 a2[7],
292 u8 res[16])
293{
294 u8 m[65];
295 int err;
296
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200297 SMP_DBG("w %16phN", w);
298 SMP_DBG("n1 %16phN n2 %16phN", n1, n2);
299 SMP_DBG("r %16phN io_cap %3phN a1 %7phN a2 %7phN", r, io_cap, a1, a2);
Johan Hedberg760b0182014-06-06 11:44:05 +0300300
301 memcpy(m, a2, 7);
302 memcpy(m + 7, a1, 7);
303 memcpy(m + 14, io_cap, 3);
304 memcpy(m + 17, r, 16);
305 memcpy(m + 33, n2, 16);
306 memcpy(m + 49, n1, 16);
307
308 err = aes_cmac(tfm_cmac, w, m, sizeof(m), res);
309 if (err)
310 return err;
311
Marcel Holtmann203de212014-12-31 20:01:22 -0800312 SMP_DBG("res %16phN", res);
Johan Hedberg760b0182014-06-06 11:44:05 +0300313
314 return err;
315}
316
Herbert Xu71af2f62016-01-24 21:18:30 +0800317static int smp_g2(struct crypto_shash *tfm_cmac, const u8 u[32], const u8 v[32],
Johan Hedberg191dc7fe22014-06-06 11:39:49 +0300318 const u8 x[16], const u8 y[16], u32 *val)
319{
320 u8 m[80], tmp[16];
321 int err;
322
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200323 SMP_DBG("u %32phN", u);
324 SMP_DBG("v %32phN", v);
325 SMP_DBG("x %16phN y %16phN", x, y);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +0300326
327 memcpy(m, y, 16);
328 memcpy(m + 16, v, 32);
329 memcpy(m + 48, u, 32);
330
331 err = aes_cmac(tfm_cmac, x, m, sizeof(m), tmp);
332 if (err)
333 return err;
334
335 *val = get_unaligned_le32(tmp);
336 *val %= 1000000;
337
Johan Hedbergc7a3d572014-12-01 22:03:16 +0200338 SMP_DBG("val %06u", *val);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +0300339
340 return 0;
341}
342
Herbert Xu71af2f62016-01-24 21:18:30 +0800343static int smp_h6(struct crypto_shash *tfm_cmac, const u8 w[16],
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200344 const u8 key_id[4], u8 res[16])
345{
346 int err;
347
348 SMP_DBG("w %16phN key_id %4phN", w, key_id);
349
350 err = aes_cmac(tfm_cmac, w, key_id, 4, res);
351 if (err)
352 return err;
353
354 SMP_DBG("res %16phN", res);
355
356 return err;
357}
358
Johan Hedberga62da6f2016-12-08 08:32:54 +0200359static int smp_h7(struct crypto_shash *tfm_cmac, const u8 w[16],
360 const u8 salt[16], u8 res[16])
361{
362 int err;
363
364 SMP_DBG("w %16phN salt %16phN", w, salt);
365
366 err = aes_cmac(tfm_cmac, salt, w, 16, res);
367 if (err)
368 return err;
369
370 SMP_DBG("res %16phN", res);
371
372 return err;
373}
374
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200375/* The following functions map to the legacy SMP crypto functions e, c1,
376 * s1 and ah.
377 */
378
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200379static int smp_e(const u8 *k, u8 *r)
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300380{
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200381 struct crypto_aes_ctx ctx;
Johan Hedberg943a7322014-03-18 12:58:24 +0200382 uint8_t tmp[16], data[16];
Johan Hedberg201a5922013-12-02 10:49:04 +0200383 int err;
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300384
Johan Hedberg011c3912015-05-19 21:06:04 +0300385 SMP_DBG("k %16phN r %16phN", k, r);
386
Johan Hedberg943a7322014-03-18 12:58:24 +0200387 /* The most significant octet of key corresponds to k[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300388 swap_buf(k, tmp, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200389
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200390 err = aes_expandkey(&ctx, tmp, 16);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300391 if (err) {
392 BT_ERR("cipher setkey failed: %d", err);
393 return err;
394 }
395
Johan Hedberg943a7322014-03-18 12:58:24 +0200396 /* Most significant octet of plaintextData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300397 swap_buf(r, data, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200398
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200399 aes_encrypt(&ctx, data, data);
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300400
Johan Hedberg943a7322014-03-18 12:58:24 +0200401 /* Most significant octet of encryptedData corresponds to data[0] */
Johan Hedberg8a2936f2014-06-16 19:25:19 +0300402 swap_buf(data, r, 16);
Johan Hedberg943a7322014-03-18 12:58:24 +0200403
Johan Hedberg011c3912015-05-19 21:06:04 +0300404 SMP_DBG("r %16phN", r);
405
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200406 memzero_explicit(&ctx, sizeof (ctx));
Anderson Brigliad22ef0b2011-06-09 18:50:44 -0300407 return err;
408}
409
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200410static int smp_c1(const u8 k[16],
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200411 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) */
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200435 err = smp_e(k, res);
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200436 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) */
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200452 err = smp_e(k, res);
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200453 if (err)
454 BT_ERR("Encrypt data error");
455
456 return err;
457}
458
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200459static int smp_s1(const u8 k[16],
Johan Hedberg06edf8d2014-12-02 13:39:23 +0200460 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
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200468 err = smp_e(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
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200475static int smp_ah(const u8 irk[16], const u8 r[3], u8 res[3])
Johan Hedberg60478052014-02-18 10:19:31 +0200476{
Johan Hedberg943a7322014-03-18 12:58:24 +0200477 u8 _res[16];
Johan Hedberg60478052014-02-18 10:19:31 +0200478 int err;
479
480 /* r' = padding || r */
Johan Hedberg943a7322014-03-18 12:58:24 +0200481 memcpy(_res, r, 3);
482 memset(_res + 3, 0, 13);
Johan Hedberg60478052014-02-18 10:19:31 +0200483
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200484 err = smp_e(irk, _res);
Johan Hedberg60478052014-02-18 10:19:31 +0200485 if (err) {
486 BT_ERR("Encrypt error");
487 return err;
488 }
489
490 /* The output of the random address function ah is:
Marcel Holtmannc5080d42015-09-04 17:08:18 +0200491 * ah(k, r) = e(k, r') mod 2^24
Johan Hedberg60478052014-02-18 10:19:31 +0200492 * The output of the security function e is then truncated to 24 bits
493 * by taking the least significant 24 bits of the output of e as the
494 * result of ah.
495 */
Johan Hedberg943a7322014-03-18 12:58:24 +0200496 memcpy(res, _res, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200497
498 return 0;
499}
500
Johan Hedbergcd082792014-12-02 13:37:41 +0200501bool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
502 const bdaddr_t *bdaddr)
Johan Hedberg60478052014-02-18 10:19:31 +0200503{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300504 struct l2cap_chan *chan = hdev->smp_data;
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700505 struct smp_dev *smp;
Johan Hedberg60478052014-02-18 10:19:31 +0200506 u8 hash[3];
507 int err;
508
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300509 if (!chan || !chan->data)
510 return false;
511
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700512 smp = chan->data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300513
Johan Hedberg60478052014-02-18 10:19:31 +0200514 BT_DBG("RPA %pMR IRK %*phN", bdaddr, 16, irk);
515
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200516 err = smp_ah(irk, &bdaddr->b[3], hash);
Johan Hedberg60478052014-02-18 10:19:31 +0200517 if (err)
518 return false;
519
Jason A. Donenfeld329d8232017-06-10 04:59:11 +0200520 return !crypto_memneq(bdaddr->b, hash, 3);
Johan Hedberg60478052014-02-18 10:19:31 +0200521}
522
Johan Hedbergcd082792014-12-02 13:37:41 +0200523int smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa)
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200524{
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300525 struct l2cap_chan *chan = hdev->smp_data;
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700526 struct smp_dev *smp;
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200527 int err;
528
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300529 if (!chan || !chan->data)
530 return -EOPNOTSUPP;
531
Marcel Holtmann88a479d2015-03-16 01:10:19 -0700532 smp = chan->data;
Johan Hedbergdefce9e2014-08-08 09:37:17 +0300533
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200534 get_random_bytes(&rpa->b[3], 3);
535
536 rpa->b[5] &= 0x3f; /* Clear two most significant bits */
537 rpa->b[5] |= 0x40; /* Set second most significant bit */
538
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200539 err = smp_ah(irk, &rpa->b[3], rpa->b);
Johan Hedbergb1e2b3a2014-02-23 19:42:19 +0200540 if (err < 0)
541 return err;
542
543 BT_DBG("RPA %pMR", rpa);
544
545 return 0;
546}
547
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700548int smp_generate_oob(struct hci_dev *hdev, u8 hash[16], u8 rand[16])
549{
550 struct l2cap_chan *chan = hdev->smp_data;
551 struct smp_dev *smp;
552 int err;
553
554 if (!chan || !chan->data)
555 return -EOPNOTSUPP;
556
557 smp = chan->data;
558
559 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
560 BT_DBG("Using debug keys");
Tudor Ambarusc0153b02017-09-28 17:14:55 +0300561 err = set_ecdh_privkey(smp->tfm_ecdh, debug_sk);
562 if (err)
563 return err;
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700564 memcpy(smp->local_pk, debug_pk, 64);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700565 smp->debug_key = true;
566 } else {
567 while (true) {
Tudor Ambarusc0153b02017-09-28 17:14:55 +0300568 /* Generate key pair for Secure Connections */
569 err = generate_ecdh_keys(smp->tfm_ecdh, smp->local_pk);
Tudor Ambarusa2976412017-09-28 17:14:52 +0300570 if (err)
571 return err;
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700572
573 /* This is unlikely, but we need to check that
574 * we didn't accidentially generate a debug key.
575 */
Tudor Ambarusc0153b02017-09-28 17:14:55 +0300576 if (crypto_memneq(smp->local_pk, debug_pk, 64))
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700577 break;
578 }
579 smp->debug_key = false;
580 }
581
582 SMP_DBG("OOB Public Key X: %32phN", smp->local_pk);
583 SMP_DBG("OOB Public Key Y: %32phN", smp->local_pk + 32);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700584
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700585 get_random_bytes(smp->local_rand, 16);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700586
587 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->local_pk,
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700588 smp->local_rand, 0, hash);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700589 if (err < 0)
590 return err;
591
Marcel Holtmannfb334fe2015-03-16 12:34:57 -0700592 memcpy(rand, smp->local_rand, 16);
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700593
Johan Hedberg94f14e42018-09-11 14:10:12 +0300594 smp->local_oob = true;
595
Marcel Holtmann60a27d62015-03-16 01:10:22 -0700596 return 0;
597}
598
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300599static void smp_send_cmd(struct l2cap_conn *conn, u8 code, u16 len, void *data)
600{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300601 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300602 struct smp_chan *smp;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300603 struct kvec iv[2];
604 struct msghdr msg;
605
606 if (!chan)
607 return;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300608
609 BT_DBG("code 0x%2.2x", code);
610
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300611 iv[0].iov_base = &code;
612 iv[0].iov_len = 1;
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300613
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300614 iv[1].iov_base = data;
615 iv[1].iov_len = len;
616
617 memset(&msg, 0, sizeof(msg));
618
David Howellsaa563d72018-10-20 00:57:56 +0100619 iov_iter_kvec(&msg.msg_iter, WRITE, iv, 2, 1 + len);
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300620
621 l2cap_chan_send(chan, &msg, 1 + len);
Vinicius Costa Gomese2dcd112011-08-19 21:06:50 -0300622
Johan Hedbergb68fda62014-08-11 22:06:40 +0300623 if (!chan->data)
624 return;
625
626 smp = chan->data;
627
628 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg1b0921d2014-09-05 22:19:48 +0300629 schedule_delayed_work(&smp->security_timer, SMP_TIMEOUT);
Anderson Brigliaeb492e02011-06-09 18:50:40 -0300630}
631
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300632static u8 authreq_to_seclevel(u8 authreq)
Brian Gix2b64d152011-12-21 16:12:12 -0800633{
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300634 if (authreq & SMP_AUTH_MITM) {
635 if (authreq & SMP_AUTH_SC)
636 return BT_SECURITY_FIPS;
637 else
638 return BT_SECURITY_HIGH;
639 } else {
Brian Gix2b64d152011-12-21 16:12:12 -0800640 return BT_SECURITY_MEDIUM;
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300641 }
Brian Gix2b64d152011-12-21 16:12:12 -0800642}
643
644static __u8 seclevel_to_authreq(__u8 sec_level)
645{
646 switch (sec_level) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +0300647 case BT_SECURITY_FIPS:
Brian Gix2b64d152011-12-21 16:12:12 -0800648 case BT_SECURITY_HIGH:
649 return SMP_AUTH_MITM | SMP_AUTH_BONDING;
650 case BT_SECURITY_MEDIUM:
651 return SMP_AUTH_BONDING;
652 default:
653 return SMP_AUTH_NONE;
654 }
655}
656
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300657static void build_pairing_cmd(struct l2cap_conn *conn,
Marcel Holtmannf1560462013-10-13 05:43:25 -0700658 struct smp_cmd_pairing *req,
659 struct smp_cmd_pairing *rsp, __u8 authreq)
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300660{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300661 struct l2cap_chan *chan = conn->smp;
662 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200663 struct hci_conn *hcon = conn->hcon;
664 struct hci_dev *hdev = hcon->hdev;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100665 u8 local_dist = 0, remote_dist = 0, oob_flag = SMP_OOB_NOT_PRESENT;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300666
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700667 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700668 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
669 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300670 authreq |= SMP_AUTH_BONDING;
Brian Gix2b64d152011-12-21 16:12:12 -0800671 } else {
672 authreq &= ~SMP_AUTH_BONDING;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300673 }
674
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700675 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergfd349c02014-02-18 10:19:36 +0200676 remote_dist |= SMP_DIST_ID_KEY;
677
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700678 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedberg863efaf2014-02-22 19:06:32 +0200679 local_dist |= SMP_DIST_ID_KEY;
680
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700681 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100682 (authreq & SMP_AUTH_SC)) {
683 struct oob_data *oob_data;
684 u8 bdaddr_type;
685
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700686 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300687 local_dist |= SMP_DIST_LINK_KEY;
688 remote_dist |= SMP_DIST_LINK_KEY;
689 }
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100690
691 if (hcon->dst_type == ADDR_LE_DEV_PUBLIC)
692 bdaddr_type = BDADDR_LE_PUBLIC;
693 else
694 bdaddr_type = BDADDR_LE_RANDOM;
695
696 oob_data = hci_find_remote_oob_data(hdev, &hcon->dst,
697 bdaddr_type);
Marcel Holtmann4775a4e2015-01-31 00:15:52 -0800698 if (oob_data && oob_data->present) {
Johan Hedberg1a8bab42015-03-16 11:45:44 +0200699 set_bit(SMP_FLAG_REMOTE_OOB, &smp->flags);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100700 oob_flag = SMP_OOB_PRESENT;
Johan Hedberga29b0732014-10-28 15:17:05 +0100701 memcpy(smp->rr, oob_data->rand256, 16);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100702 memcpy(smp->pcnf, oob_data->hash256, 16);
Marcel Holtmannbc07cd62015-03-16 12:34:56 -0700703 SMP_DBG("OOB Remote Confirmation: %16phN", smp->pcnf);
704 SMP_DBG("OOB Remote Random: %16phN", smp->rr);
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100705 }
706
Johan Hedbergdf8e1a42014-06-06 10:39:56 +0300707 } else {
708 authreq &= ~SMP_AUTH_SC;
709 }
710
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300711 if (rsp == NULL) {
712 req->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100713 req->oob_flag = oob_flag;
Matias Karhumaa30d65e02018-09-28 21:54:30 +0300714 req->max_key_size = hdev->le_max_key_size;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200715 req->init_key_dist = local_dist;
716 req->resp_key_dist = remote_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300717 req->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200718
719 smp->remote_key_dist = remote_dist;
Vinicius Costa Gomes54790f72011-07-07 18:59:38 -0300720 return;
721 }
722
723 rsp->io_capability = conn->hcon->io_capability;
Johan Hedberg02b05bd2014-10-26 21:19:10 +0100724 rsp->oob_flag = oob_flag;
Matias Karhumaa30d65e02018-09-28 21:54:30 +0300725 rsp->max_key_size = hdev->le_max_key_size;
Johan Hedbergfd349c02014-02-18 10:19:36 +0200726 rsp->init_key_dist = req->init_key_dist & remote_dist;
727 rsp->resp_key_dist = req->resp_key_dist & local_dist;
Johan Hedberg0edb14d2014-05-26 13:29:28 +0300728 rsp->auth_req = (authreq & AUTH_REQ_MASK(hdev));
Johan Hedbergfd349c02014-02-18 10:19:36 +0200729
730 smp->remote_key_dist = rsp->init_key_dist;
Vinicius Costa Gomesb8e66ea2011-06-09 18:50:52 -0300731}
732
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300733static u8 check_enc_key_size(struct l2cap_conn *conn, __u8 max_key_size)
734{
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300735 struct l2cap_chan *chan = conn->smp;
Johan Hedberg2fd36552015-06-11 13:52:26 +0300736 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300737 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -0300738
Matias Karhumaa30d65e02018-09-28 21:54:30 +0300739 if (max_key_size > hdev->le_max_key_size ||
Johan Hedberg2fd36552015-06-11 13:52:26 +0300740 max_key_size < SMP_MIN_ENC_KEY_SIZE)
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300741 return SMP_ENC_KEY_SIZE;
742
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -0300743 smp->enc_key_size = max_key_size;
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -0300744
745 return 0;
746}
747
Johan Hedberg6f48e262014-08-11 22:06:44 +0300748static void smp_chan_destroy(struct l2cap_conn *conn)
749{
750 struct l2cap_chan *chan = conn->smp;
751 struct smp_chan *smp = chan->data;
Johan Hedberg923e2412014-12-03 12:43:39 +0200752 struct hci_conn *hcon = conn->hcon;
Johan Hedberg6f48e262014-08-11 22:06:44 +0300753 bool complete;
754
755 BUG_ON(!smp);
756
757 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300758
Johan Hedberg6f48e262014-08-11 22:06:44 +0300759 complete = test_bit(SMP_FLAG_COMPLETE, &smp->flags);
Johan Hedberg923e2412014-12-03 12:43:39 +0200760 mgmt_smp_complete(hcon, complete);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300761
Marcel Holtmann276812e2015-03-16 01:10:18 -0700762 kzfree(smp->csrk);
763 kzfree(smp->slave_csrk);
764 kzfree(smp->link_key);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300765
Herbert Xu71af2f62016-01-24 21:18:30 +0800766 crypto_free_shash(smp->tfm_cmac);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +0300767 crypto_free_kpp(smp->tfm_ecdh);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300768
Johan Hedberg923e2412014-12-03 12:43:39 +0200769 /* Ensure that we don't leave any debug key around if debug key
770 * support hasn't been explicitly enabled.
771 */
772 if (smp->ltk && smp->ltk->type == SMP_LTK_P256_DEBUG &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700773 !hci_dev_test_flag(hcon->hdev, HCI_KEEP_DEBUG_KEYS)) {
Johan Hedberg923e2412014-12-03 12:43:39 +0200774 list_del_rcu(&smp->ltk->list);
775 kfree_rcu(smp->ltk, rcu);
776 smp->ltk = NULL;
777 }
778
Johan Hedberg6f48e262014-08-11 22:06:44 +0300779 /* If pairing failed clean up any keys we might have */
780 if (!complete) {
781 if (smp->ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200782 list_del_rcu(&smp->ltk->list);
783 kfree_rcu(smp->ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300784 }
785
786 if (smp->slave_ltk) {
Johan Hedberg970d0f12014-11-13 14:37:47 +0200787 list_del_rcu(&smp->slave_ltk->list);
788 kfree_rcu(smp->slave_ltk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300789 }
790
791 if (smp->remote_irk) {
Johan Hedbergadae20c2014-11-13 14:37:48 +0200792 list_del_rcu(&smp->remote_irk->list);
793 kfree_rcu(smp->remote_irk, rcu);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300794 }
795 }
796
797 chan->data = NULL;
Marcel Holtmann276812e2015-03-16 01:10:18 -0700798 kzfree(smp);
Johan Hedberg923e2412014-12-03 12:43:39 +0200799 hci_conn_drop(hcon);
Johan Hedberg6f48e262014-08-11 22:06:44 +0300800}
801
Johan Hedberg84794e12013-11-06 11:24:57 +0200802static void smp_failure(struct l2cap_conn *conn, u8 reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800803{
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200804 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb68fda62014-08-11 22:06:40 +0300805 struct l2cap_chan *chan = conn->smp;
Johan Hedbergbab73cb2012-02-09 16:07:29 +0200806
Johan Hedberg84794e12013-11-06 11:24:57 +0200807 if (reason)
Brian Gix4f957a72011-11-23 08:28:36 -0800808 smp_send_cmd(conn, SMP_CMD_PAIRING_FAIL, sizeof(reason),
Marcel Holtmannf1560462013-10-13 05:43:25 -0700809 &reason);
Brian Gix4f957a72011-11-23 08:28:36 -0800810
Johan Hedberge1e930f2014-09-08 17:09:49 -0700811 mgmt_auth_failed(hcon, HCI_ERROR_AUTH_FAILURE);
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300812
Johan Hedbergfc75cc82014-09-05 22:19:52 +0300813 if (chan->data)
Vinicius Costa Gomesf1c09c02012-02-01 18:27:56 -0300814 smp_chan_destroy(conn);
Brian Gix4f957a72011-11-23 08:28:36 -0800815}
816
Brian Gix2b64d152011-12-21 16:12:12 -0800817#define JUST_WORKS 0x00
818#define JUST_CFM 0x01
819#define REQ_PASSKEY 0x02
820#define CFM_PASSKEY 0x03
821#define REQ_OOB 0x04
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300822#define DSP_PASSKEY 0x05
Brian Gix2b64d152011-12-21 16:12:12 -0800823#define OVERLAP 0xFF
824
825static const u8 gen_method[5][5] = {
826 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
827 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
828 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
829 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
830 { CFM_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, OVERLAP },
831};
832
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300833static const u8 sc_method[5][5] = {
834 { JUST_WORKS, JUST_CFM, REQ_PASSKEY, JUST_WORKS, REQ_PASSKEY },
835 { JUST_WORKS, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
836 { DSP_PASSKEY, DSP_PASSKEY, REQ_PASSKEY, JUST_WORKS, DSP_PASSKEY },
837 { JUST_WORKS, JUST_CFM, JUST_WORKS, JUST_WORKS, JUST_CFM },
838 { DSP_PASSKEY, CFM_PASSKEY, REQ_PASSKEY, JUST_WORKS, CFM_PASSKEY },
839};
840
Johan Hedberg581370c2014-06-17 13:07:38 +0300841static u8 get_auth_method(struct smp_chan *smp, u8 local_io, u8 remote_io)
842{
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300843 /* If either side has unknown io_caps, use JUST_CFM (which gets
844 * converted later to JUST_WORKS if we're initiators.
845 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300846 if (local_io > SMP_IO_KEYBOARD_DISPLAY ||
847 remote_io > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300848 return JUST_CFM;
Johan Hedberg581370c2014-06-17 13:07:38 +0300849
Johan Hedberg5e3d3d92014-05-31 18:51:02 +0300850 if (test_bit(SMP_FLAG_SC, &smp->flags))
851 return sc_method[remote_io][local_io];
852
Johan Hedberg581370c2014-06-17 13:07:38 +0300853 return gen_method[remote_io][local_io];
854}
855
Brian Gix2b64d152011-12-21 16:12:12 -0800856static int tk_request(struct l2cap_conn *conn, u8 remote_oob, u8 auth,
857 u8 local_io, u8 remote_io)
858{
859 struct hci_conn *hcon = conn->hcon;
Johan Hedberg5d88cc72014-08-08 09:37:18 +0300860 struct l2cap_chan *chan = conn->smp;
861 struct smp_chan *smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -0800862 u32 passkey = 0;
863 int ret = 0;
864
865 /* Initialize key for JUST WORKS */
866 memset(smp->tk, 0, sizeof(smp->tk));
Johan Hedberg4a74d652014-05-20 09:45:50 +0300867 clear_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800868
869 BT_DBG("tk_request: auth:%d lcl:%d rem:%d", auth, local_io, remote_io);
870
Johan Hedberg2bcd4002014-07-09 19:18:09 +0300871 /* If neither side wants MITM, either "just" confirm an incoming
872 * request or use just-works for outgoing ones. The JUST_CFM
873 * will be converted to JUST_WORKS if necessary later in this
874 * function. If either side has MITM look up the method from the
875 * table.
876 */
Johan Hedberg581370c2014-06-17 13:07:38 +0300877 if (!(auth & SMP_AUTH_MITM))
Johan Hedberg783e0572014-05-31 18:48:26 +0300878 smp->method = JUST_CFM;
Brian Gix2b64d152011-12-21 16:12:12 -0800879 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300880 smp->method = get_auth_method(smp, local_io, remote_io);
Brian Gix2b64d152011-12-21 16:12:12 -0800881
Johan Hedberga82505c2014-03-24 14:39:07 +0200882 /* Don't confirm locally initiated pairing attempts */
Johan Hedberg783e0572014-05-31 18:48:26 +0300883 if (smp->method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR,
884 &smp->flags))
885 smp->method = JUST_WORKS;
Johan Hedberga82505c2014-03-24 14:39:07 +0200886
Johan Hedberg02f3e252014-07-16 15:09:13 +0300887 /* Don't bother user space with no IO capabilities */
Johan Hedberg783e0572014-05-31 18:48:26 +0300888 if (smp->method == JUST_CFM &&
889 hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
890 smp->method = JUST_WORKS;
Johan Hedberg02f3e252014-07-16 15:09:13 +0300891
Brian Gix2b64d152011-12-21 16:12:12 -0800892 /* If Just Works, Continue with Zero TK */
Johan Hedberg783e0572014-05-31 18:48:26 +0300893 if (smp->method == JUST_WORKS) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300894 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800895 return 0;
896 }
897
Johan Hedberg19c5ce92015-03-15 19:34:04 +0200898 /* If this function is used for SC -> legacy fallback we
899 * can only recover the just-works case.
900 */
901 if (test_bit(SMP_FLAG_SC, &smp->flags))
902 return -EINVAL;
903
Brian Gix2b64d152011-12-21 16:12:12 -0800904 /* Not Just Works/Confirm results in MITM Authentication */
Johan Hedberg783e0572014-05-31 18:48:26 +0300905 if (smp->method != JUST_CFM) {
Johan Hedberg4a74d652014-05-20 09:45:50 +0300906 set_bit(SMP_FLAG_MITM_AUTH, &smp->flags);
Johan Hedberg5eb596f2014-09-18 11:26:32 +0300907 if (hcon->pending_sec_level < BT_SECURITY_HIGH)
908 hcon->pending_sec_level = BT_SECURITY_HIGH;
909 }
Brian Gix2b64d152011-12-21 16:12:12 -0800910
911 /* If both devices have Keyoard-Display I/O, the master
912 * Confirms and the slave Enters the passkey.
913 */
Johan Hedberg783e0572014-05-31 18:48:26 +0300914 if (smp->method == OVERLAP) {
Johan Hedberg40bef302014-07-16 11:42:27 +0300915 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedberg783e0572014-05-31 18:48:26 +0300916 smp->method = CFM_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800917 else
Johan Hedberg783e0572014-05-31 18:48:26 +0300918 smp->method = REQ_PASSKEY;
Brian Gix2b64d152011-12-21 16:12:12 -0800919 }
920
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200921 /* Generate random passkey. */
Johan Hedberg783e0572014-05-31 18:48:26 +0300922 if (smp->method == CFM_PASSKEY) {
Johan Hedberg943a7322014-03-18 12:58:24 +0200923 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -0800924 get_random_bytes(&passkey, sizeof(passkey));
925 passkey %= 1000000;
Johan Hedberg943a7322014-03-18 12:58:24 +0200926 put_unaligned_le32(passkey, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -0800927 BT_DBG("PassKey: %d", passkey);
Johan Hedberg4a74d652014-05-20 09:45:50 +0300928 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800929 }
930
Johan Hedberg783e0572014-05-31 18:48:26 +0300931 if (smp->method == REQ_PASSKEY)
Marcel Holtmannce39fb42013-10-13 02:23:39 -0700932 ret = mgmt_user_passkey_request(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200933 hcon->type, hcon->dst_type);
Johan Hedberg783e0572014-05-31 18:48:26 +0300934 else if (smp->method == JUST_CFM)
Johan Hedberg4eb65e62014-03-24 14:39:05 +0200935 ret = mgmt_user_confirm_request(hcon->hdev, &hcon->dst,
936 hcon->type, hcon->dst_type,
937 passkey, 1);
Brian Gix2b64d152011-12-21 16:12:12 -0800938 else
Johan Hedberg01ad34d2014-03-19 14:14:53 +0200939 ret = mgmt_user_passkey_notify(hcon->hdev, &hcon->dst,
Johan Hedberg272d90d2012-02-09 15:26:12 +0200940 hcon->type, hcon->dst_type,
Johan Hedberg39adbff2014-03-20 08:18:14 +0200941 passkey, 0);
Brian Gix2b64d152011-12-21 16:12:12 -0800942
Brian Gix2b64d152011-12-21 16:12:12 -0800943 return ret;
944}
945
Johan Hedberg1cc61142014-05-20 09:45:52 +0300946static u8 smp_confirm(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300947{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300948 struct l2cap_conn *conn = smp->conn;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300949 struct smp_cmd_pairing_confirm cp;
950 int ret;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300951
952 BT_DBG("conn %p", conn);
953
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200954 ret = smp_c1(smp->tk, smp->prnd, smp->preq, smp->prsp,
Johan Hedbergb1cd5fd2014-02-28 12:54:17 +0200955 conn->hcon->init_addr_type, &conn->hcon->init_addr,
Johan Hedberg943a7322014-03-18 12:58:24 +0200956 conn->hcon->resp_addr_type, &conn->hcon->resp_addr,
957 cp.confirm_val);
Johan Hedberg1cc61142014-05-20 09:45:52 +0300958 if (ret)
959 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300960
Johan Hedberg4a74d652014-05-20 09:45:50 +0300961 clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -0800962
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300963 smp_send_cmd(smp->conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cp), &cp);
964
Johan Hedbergb28b4942014-09-05 22:19:55 +0300965 if (conn->hcon->out)
966 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
967 else
968 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
969
Johan Hedberg1cc61142014-05-20 09:45:52 +0300970 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300971}
972
Johan Hedberg861580a2014-05-20 09:45:51 +0300973static u8 smp_random(struct smp_chan *smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300974{
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300975 struct l2cap_conn *conn = smp->conn;
976 struct hci_conn *hcon = conn->hcon;
Johan Hedberg861580a2014-05-20 09:45:51 +0300977 u8 confirm[16];
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300978 int ret;
979
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300980 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
981
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200982 ret = smp_c1(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
Jason A. Donenfeld329d8232017-06-10 04:59:11 +0200988 if (crypto_memneq(smp->pcnf, confirm, sizeof(smp->pcnf))) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100989 bt_dev_err(hcon->hdev, "pairing failed "
990 "(confirmation values mismatch)");
Johan Hedberg861580a2014-05-20 09:45:51 +0300991 return SMP_CONFIRM_FAILED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300992 }
993
994 if (hcon->out) {
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -0800995 u8 stk[16];
996 __le64 rand = 0;
997 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -0300998
Ard Biesheuvel28a220a2019-07-02 21:41:41 +0200999 smp_s1(smp->tk, smp->rrnd, smp->prnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001000
Johan Hedberg861580a2014-05-20 09:45:51 +03001001 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
1002 return SMP_UNSPECIFIED;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001003
Johan Hedberg8b76ce32015-06-08 18:14:39 +03001004 hci_le_start_enc(hcon, ediv, rand, stk, smp->enc_key_size);
Vinicius Costa Gomesf7aa6112012-01-30 19:29:12 -03001005 hcon->enc_key_size = smp->enc_key_size;
Johan Hedbergfe59a052014-07-01 19:14:12 +03001006 set_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001007 } else {
Johan Hedbergfff34902014-06-10 15:19:50 +03001008 u8 stk[16], auth;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08001009 __le64 rand = 0;
1010 __le16 ediv = 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001011
Johan Hedberg943a7322014-03-18 12:58:24 +02001012 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
1013 smp->prnd);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001014
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02001015 smp_s1(smp->tk, smp->prnd, smp->rrnd, stk);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001016
Johan Hedbergfff34902014-06-10 15:19:50 +03001017 if (hcon->pending_sec_level == BT_SECURITY_HIGH)
1018 auth = 1;
1019 else
1020 auth = 0;
1021
Johan Hedberg7d5843b2014-06-16 19:25:15 +03001022 /* Even though there's no _SLAVE suffix this is the
1023 * slave STK we're adding for later lookup (the master
1024 * STK never needs to be stored).
1025 */
Marcel Holtmannce39fb42013-10-13 02:23:39 -07001026 hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
Johan Hedberg2ceba532014-06-16 19:25:16 +03001027 SMP_STK, auth, stk, smp->enc_key_size, ediv, rand);
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001028 }
1029
Johan Hedberg861580a2014-05-20 09:45:51 +03001030 return 0;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001031}
1032
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001033static void smp_notify_keys(struct l2cap_conn *conn)
1034{
1035 struct l2cap_chan *chan = conn->smp;
1036 struct smp_chan *smp = chan->data;
1037 struct hci_conn *hcon = conn->hcon;
1038 struct hci_dev *hdev = hcon->hdev;
1039 struct smp_cmd_pairing *req = (void *) &smp->preq[1];
1040 struct smp_cmd_pairing *rsp = (void *) &smp->prsp[1];
1041 bool persistent;
1042
Johan Hedbergcad20c22015-10-12 13:36:19 +02001043 if (hcon->type == ACL_LINK) {
1044 if (hcon->key_type == HCI_LK_DEBUG_COMBINATION)
1045 persistent = false;
1046 else
1047 persistent = !test_bit(HCI_CONN_FLUSH_KEY,
1048 &hcon->flags);
1049 } else {
1050 /* The LTKs, IRKs and CSRKs should be persistent only if
1051 * both sides had the bonding bit set in their
1052 * authentication requests.
1053 */
1054 persistent = !!((req->auth_req & rsp->auth_req) &
1055 SMP_AUTH_BONDING);
1056 }
1057
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001058 if (smp->remote_irk) {
Johan Hedbergcad20c22015-10-12 13:36:19 +02001059 mgmt_new_irk(hdev, smp->remote_irk, persistent);
1060
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001061 /* Now that user space can be considered to know the
1062 * identity address track the connection based on it
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001063 * from now on (assuming this is an LE link).
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001064 */
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001065 if (hcon->type == LE_LINK) {
1066 bacpy(&hcon->dst, &smp->remote_irk->bdaddr);
1067 hcon->dst_type = smp->remote_irk->addr_type;
1068 queue_work(hdev->workqueue, &conn->id_addr_update_work);
1069 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001070 }
1071
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001072 if (smp->csrk) {
1073 smp->csrk->bdaddr_type = hcon->dst_type;
1074 bacpy(&smp->csrk->bdaddr, &hcon->dst);
1075 mgmt_new_csrk(hdev, smp->csrk, persistent);
1076 }
1077
1078 if (smp->slave_csrk) {
1079 smp->slave_csrk->bdaddr_type = hcon->dst_type;
1080 bacpy(&smp->slave_csrk->bdaddr, &hcon->dst);
1081 mgmt_new_csrk(hdev, smp->slave_csrk, persistent);
1082 }
1083
1084 if (smp->ltk) {
1085 smp->ltk->bdaddr_type = hcon->dst_type;
1086 bacpy(&smp->ltk->bdaddr, &hcon->dst);
1087 mgmt_new_ltk(hdev, smp->ltk, persistent);
1088 }
1089
1090 if (smp->slave_ltk) {
1091 smp->slave_ltk->bdaddr_type = hcon->dst_type;
1092 bacpy(&smp->slave_ltk->bdaddr, &hcon->dst);
1093 mgmt_new_ltk(hdev, smp->slave_ltk, persistent);
1094 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001095
1096 if (smp->link_key) {
Johan Hedberge3befab2014-06-01 16:33:39 +03001097 struct link_key *key;
1098 u8 type;
1099
1100 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1101 type = HCI_LK_DEBUG_COMBINATION;
1102 else if (hcon->sec_level == BT_SECURITY_FIPS)
1103 type = HCI_LK_AUTH_COMBINATION_P256;
1104 else
1105 type = HCI_LK_UNAUTH_COMBINATION_P256;
1106
1107 key = hci_add_link_key(hdev, smp->conn->hcon, &hcon->dst,
1108 smp->link_key, type, 0, &persistent);
1109 if (key) {
1110 mgmt_new_link_key(hdev, key, persistent);
1111
1112 /* Don't keep debug keys around if the relevant
1113 * flag is not set.
1114 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001115 if (!hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS) &&
Johan Hedberge3befab2014-06-01 16:33:39 +03001116 key->type == HCI_LK_DEBUG_COMBINATION) {
1117 list_del_rcu(&key->list);
1118 kfree_rcu(key, rcu);
1119 }
1120 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001121 }
1122}
1123
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001124static void sc_add_ltk(struct smp_chan *smp)
1125{
1126 struct hci_conn *hcon = smp->conn->hcon;
1127 u8 key_type, auth;
1128
1129 if (test_bit(SMP_FLAG_DEBUG_KEY, &smp->flags))
1130 key_type = SMP_LTK_P256_DEBUG;
1131 else
1132 key_type = SMP_LTK_P256;
1133
1134 if (hcon->pending_sec_level == BT_SECURITY_FIPS)
1135 auth = 1;
1136 else
1137 auth = 0;
1138
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001139 smp->ltk = hci_add_ltk(hcon->hdev, &hcon->dst, hcon->dst_type,
1140 key_type, auth, smp->tk, smp->enc_key_size,
1141 0, 0);
1142}
1143
Johan Hedberg6a770832014-06-06 11:54:04 +03001144static void sc_generate_link_key(struct smp_chan *smp)
1145{
Johan Hedberga62da6f2016-12-08 08:32:54 +02001146 /* From core spec. Spells out in ASCII as 'lebr'. */
Johan Hedberg6a770832014-06-06 11:54:04 +03001147 const u8 lebr[4] = { 0x72, 0x62, 0x65, 0x6c };
1148
1149 smp->link_key = kzalloc(16, GFP_KERNEL);
1150 if (!smp->link_key)
1151 return;
1152
Johan Hedberga62da6f2016-12-08 08:32:54 +02001153 if (test_bit(SMP_FLAG_CT2, &smp->flags)) {
1154 /* SALT = 0x00000000000000000000000000000000746D7031 */
1155 const u8 salt[16] = { 0x31, 0x70, 0x6d, 0x74 };
1156
1157 if (smp_h7(smp->tfm_cmac, smp->tk, salt, smp->link_key)) {
1158 kzfree(smp->link_key);
1159 smp->link_key = NULL;
1160 return;
1161 }
1162 } else {
1163 /* From core spec. Spells out in ASCII as 'tmp1'. */
1164 const u8 tmp1[4] = { 0x31, 0x70, 0x6d, 0x74 };
1165
1166 if (smp_h6(smp->tfm_cmac, smp->tk, tmp1, smp->link_key)) {
1167 kzfree(smp->link_key);
1168 smp->link_key = NULL;
1169 return;
1170 }
Johan Hedberg6a770832014-06-06 11:54:04 +03001171 }
1172
1173 if (smp_h6(smp->tfm_cmac, smp->link_key, lebr, smp->link_key)) {
Marcel Holtmann276812e2015-03-16 01:10:18 -07001174 kzfree(smp->link_key);
Johan Hedberg6a770832014-06-06 11:54:04 +03001175 smp->link_key = NULL;
1176 return;
1177 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001178}
1179
Johan Hedbergb28b4942014-09-05 22:19:55 +03001180static void smp_allow_key_dist(struct smp_chan *smp)
1181{
1182 /* Allow the first expected phase 3 PDU. The rest of the PDUs
1183 * will be allowed in each PDU handler to ensure we receive
1184 * them in the correct order.
1185 */
1186 if (smp->remote_key_dist & SMP_DIST_ENC_KEY)
1187 SMP_ALLOW_CMD(smp, SMP_CMD_ENCRYPT_INFO);
1188 else if (smp->remote_key_dist & SMP_DIST_ID_KEY)
1189 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
1190 else if (smp->remote_key_dist & SMP_DIST_SIGN)
1191 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
1192}
1193
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001194static void sc_generate_ltk(struct smp_chan *smp)
1195{
Johan Hedberga62da6f2016-12-08 08:32:54 +02001196 /* From core spec. Spells out in ASCII as 'brle'. */
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001197 const u8 brle[4] = { 0x65, 0x6c, 0x72, 0x62 };
1198 struct hci_conn *hcon = smp->conn->hcon;
1199 struct hci_dev *hdev = hcon->hdev;
1200 struct link_key *key;
1201
1202 key = hci_find_link_key(hdev, &hcon->dst);
1203 if (!key) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001204 bt_dev_err(hdev, "no Link Key found to generate LTK");
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001205 return;
1206 }
1207
1208 if (key->type == HCI_LK_DEBUG_COMBINATION)
1209 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1210
Johan Hedberga62da6f2016-12-08 08:32:54 +02001211 if (test_bit(SMP_FLAG_CT2, &smp->flags)) {
1212 /* SALT = 0x00000000000000000000000000000000746D7032 */
1213 const u8 salt[16] = { 0x32, 0x70, 0x6d, 0x74 };
1214
1215 if (smp_h7(smp->tfm_cmac, key->val, salt, smp->tk))
1216 return;
1217 } else {
1218 /* From core spec. Spells out in ASCII as 'tmp2'. */
1219 const u8 tmp2[4] = { 0x32, 0x70, 0x6d, 0x74 };
1220
1221 if (smp_h6(smp->tfm_cmac, key->val, tmp2, smp->tk))
1222 return;
1223 }
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001224
1225 if (smp_h6(smp->tfm_cmac, smp->tk, brle, smp->tk))
1226 return;
1227
1228 sc_add_ltk(smp);
1229}
1230
Johan Hedbergd6268e82014-09-05 22:19:51 +03001231static void smp_distribute_keys(struct smp_chan *smp)
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001232{
1233 struct smp_cmd_pairing *req, *rsp;
Johan Hedberg86d14072014-08-11 22:06:43 +03001234 struct l2cap_conn *conn = smp->conn;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001235 struct hci_conn *hcon = conn->hcon;
1236 struct hci_dev *hdev = hcon->hdev;
1237 __u8 *keydist;
1238
1239 BT_DBG("conn %p", conn);
1240
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001241 rsp = (void *) &smp->prsp[1];
1242
1243 /* The responder sends its keys first */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001244 if (hcon->out && (smp->remote_key_dist & KEY_DIST_MASK)) {
1245 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001246 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001247 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001248
1249 req = (void *) &smp->preq[1];
1250
1251 if (hcon->out) {
1252 keydist = &rsp->init_key_dist;
1253 *keydist &= req->init_key_dist;
1254 } else {
1255 keydist = &rsp->resp_key_dist;
1256 *keydist &= req->resp_key_dist;
1257 }
1258
Johan Hedberg6a770832014-06-06 11:54:04 +03001259 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001260 if (hcon->type == LE_LINK && (*keydist & SMP_DIST_LINK_KEY))
Johan Hedberg6a770832014-06-06 11:54:04 +03001261 sc_generate_link_key(smp);
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001262 if (hcon->type == ACL_LINK && (*keydist & SMP_DIST_ENC_KEY))
1263 sc_generate_ltk(smp);
Johan Hedberg6a770832014-06-06 11:54:04 +03001264
1265 /* Clear the keys which are generated but not distributed */
1266 *keydist &= ~SMP_SC_NO_DIST;
1267 }
1268
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001269 BT_DBG("keydist 0x%x", *keydist);
1270
1271 if (*keydist & SMP_DIST_ENC_KEY) {
1272 struct smp_cmd_encrypt_info enc;
1273 struct smp_cmd_master_ident ident;
1274 struct smp_ltk *ltk;
1275 u8 authenticated;
1276 __le16 ediv;
1277 __le64 rand;
1278
Johan Hedberg1fc62c52015-06-10 11:11:20 +03001279 /* Make sure we generate only the significant amount of
1280 * bytes based on the encryption key size, and set the rest
1281 * of the value to zeroes.
1282 */
1283 get_random_bytes(enc.ltk, smp->enc_key_size);
1284 memset(enc.ltk + smp->enc_key_size, 0,
1285 sizeof(enc.ltk) - smp->enc_key_size);
1286
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001287 get_random_bytes(&ediv, sizeof(ediv));
1288 get_random_bytes(&rand, sizeof(rand));
1289
1290 smp_send_cmd(conn, SMP_CMD_ENCRYPT_INFO, sizeof(enc), &enc);
1291
1292 authenticated = hcon->sec_level == BT_SECURITY_HIGH;
1293 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type,
1294 SMP_LTK_SLAVE, authenticated, enc.ltk,
1295 smp->enc_key_size, ediv, rand);
1296 smp->slave_ltk = ltk;
1297
1298 ident.ediv = ediv;
1299 ident.rand = rand;
1300
1301 smp_send_cmd(conn, SMP_CMD_MASTER_IDENT, sizeof(ident), &ident);
1302
1303 *keydist &= ~SMP_DIST_ENC_KEY;
1304 }
1305
1306 if (*keydist & SMP_DIST_ID_KEY) {
1307 struct smp_cmd_ident_addr_info addrinfo;
1308 struct smp_cmd_ident_info idinfo;
1309
1310 memcpy(idinfo.irk, hdev->irk, sizeof(idinfo.irk));
1311
1312 smp_send_cmd(conn, SMP_CMD_IDENT_INFO, sizeof(idinfo), &idinfo);
1313
1314 /* The hci_conn contains the local identity address
1315 * after the connection has been established.
1316 *
1317 * This is true even when the connection has been
1318 * established using a resolvable random address.
1319 */
1320 bacpy(&addrinfo.bdaddr, &hcon->src);
1321 addrinfo.addr_type = hcon->src_type;
1322
1323 smp_send_cmd(conn, SMP_CMD_IDENT_ADDR_INFO, sizeof(addrinfo),
1324 &addrinfo);
1325
1326 *keydist &= ~SMP_DIST_ID_KEY;
1327 }
1328
1329 if (*keydist & SMP_DIST_SIGN) {
1330 struct smp_cmd_sign_info sign;
1331 struct smp_csrk *csrk;
1332
1333 /* Generate a new random key */
1334 get_random_bytes(sign.csrk, sizeof(sign.csrk));
1335
1336 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
1337 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02001338 if (hcon->sec_level > BT_SECURITY_MEDIUM)
1339 csrk->type = MGMT_CSRK_LOCAL_AUTHENTICATED;
1340 else
1341 csrk->type = MGMT_CSRK_LOCAL_UNAUTHENTICATED;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001342 memcpy(csrk->val, sign.csrk, sizeof(csrk->val));
1343 }
1344 smp->slave_csrk = csrk;
1345
1346 smp_send_cmd(conn, SMP_CMD_SIGN_INFO, sizeof(sign), &sign);
1347
1348 *keydist &= ~SMP_DIST_SIGN;
1349 }
1350
1351 /* If there are still keys to be received wait for them */
Johan Hedbergb28b4942014-09-05 22:19:55 +03001352 if (smp->remote_key_dist & KEY_DIST_MASK) {
1353 smp_allow_key_dist(smp);
Johan Hedberg86d14072014-08-11 22:06:43 +03001354 return;
Johan Hedbergb28b4942014-09-05 22:19:55 +03001355 }
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001356
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001357 set_bit(SMP_FLAG_COMPLETE, &smp->flags);
1358 smp_notify_keys(conn);
1359
1360 smp_chan_destroy(conn);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03001361}
1362
Johan Hedbergb68fda62014-08-11 22:06:40 +03001363static void smp_timeout(struct work_struct *work)
1364{
1365 struct smp_chan *smp = container_of(work, struct smp_chan,
1366 security_timer.work);
1367 struct l2cap_conn *conn = smp->conn;
1368
1369 BT_DBG("conn %p", conn);
1370
Johan Hedberg1e91c292014-08-18 20:33:29 +03001371 hci_disconnect(conn->hcon, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedbergb68fda62014-08-11 22:06:40 +03001372}
1373
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001374static struct smp_chan *smp_chan_create(struct l2cap_conn *conn)
1375{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001376 struct l2cap_chan *chan = conn->smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001377 struct smp_chan *smp;
1378
Marcel Holtmannf1560462013-10-13 05:43:25 -07001379 smp = kzalloc(sizeof(*smp), GFP_ATOMIC);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001380 if (!smp)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001381 return NULL;
1382
Herbert Xu71af2f62016-01-24 21:18:30 +08001383 smp->tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0);
Johan Hedberg407cecf2014-05-02 14:19:47 +03001384 if (IS_ERR(smp->tfm_cmac)) {
1385 BT_ERR("Unable to create CMAC crypto context");
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02001386 goto zfree_smp;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03001387 }
1388
1389 smp->tfm_ecdh = crypto_alloc_kpp("ecdh", CRYPTO_ALG_INTERNAL, 0);
1390 if (IS_ERR(smp->tfm_ecdh)) {
1391 BT_ERR("Unable to create ECDH crypto context");
1392 goto free_shash;
Johan Hedberg407cecf2014-05-02 14:19:47 +03001393 }
1394
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001395 smp->conn = conn;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001396 chan->data = smp;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001397
Johan Hedbergb28b4942014-09-05 22:19:55 +03001398 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_FAIL);
1399
Johan Hedbergb68fda62014-08-11 22:06:40 +03001400 INIT_DELAYED_WORK(&smp->security_timer, smp_timeout);
1401
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001402 hci_conn_hold(conn->hcon);
1403
1404 return smp;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03001405
1406free_shash:
1407 crypto_free_shash(smp->tfm_cmac);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03001408zfree_smp:
1409 kzfree(smp);
1410 return NULL;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001411}
1412
Johan Hedberg760b0182014-06-06 11:44:05 +03001413static int sc_mackey_and_ltk(struct smp_chan *smp, u8 mackey[16], u8 ltk[16])
1414{
1415 struct hci_conn *hcon = smp->conn->hcon;
1416 u8 *na, *nb, a[7], b[7];
1417
1418 if (hcon->out) {
1419 na = smp->prnd;
1420 nb = smp->rrnd;
1421 } else {
1422 na = smp->rrnd;
1423 nb = smp->prnd;
1424 }
1425
1426 memcpy(a, &hcon->init_addr, 6);
1427 memcpy(b, &hcon->resp_addr, 6);
1428 a[6] = hcon->init_addr_type;
1429 b[6] = hcon->resp_addr_type;
1430
1431 return smp_f5(smp->tfm_cmac, smp->dhkey, na, nb, a, b, mackey, ltk);
1432}
1433
Johan Hedberg38606f12014-06-25 11:10:28 +03001434static void sc_dhkey_check(struct smp_chan *smp)
Johan Hedberg760b0182014-06-06 11:44:05 +03001435{
1436 struct hci_conn *hcon = smp->conn->hcon;
1437 struct smp_cmd_dhkey_check check;
1438 u8 a[7], b[7], *local_addr, *remote_addr;
1439 u8 io_cap[3], r[16];
1440
Johan Hedberg760b0182014-06-06 11:44:05 +03001441 memcpy(a, &hcon->init_addr, 6);
1442 memcpy(b, &hcon->resp_addr, 6);
1443 a[6] = hcon->init_addr_type;
1444 b[6] = hcon->resp_addr_type;
1445
1446 if (hcon->out) {
1447 local_addr = a;
1448 remote_addr = b;
1449 memcpy(io_cap, &smp->preq[1], 3);
1450 } else {
1451 local_addr = b;
1452 remote_addr = a;
1453 memcpy(io_cap, &smp->prsp[1], 3);
1454 }
1455
Johan Hedbergdddd3052014-06-01 15:38:09 +03001456 memset(r, 0, sizeof(r));
1457
1458 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
Johan Hedberg38606f12014-06-25 11:10:28 +03001459 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg760b0182014-06-06 11:44:05 +03001460
Johan Hedberga29b0732014-10-28 15:17:05 +01001461 if (smp->method == REQ_OOB)
1462 memcpy(r, smp->rr, 16);
1463
Johan Hedberg760b0182014-06-06 11:44:05 +03001464 smp_f6(smp->tfm_cmac, smp->mackey, smp->prnd, smp->rrnd, r, io_cap,
1465 local_addr, remote_addr, check.e);
1466
1467 smp_send_cmd(smp->conn, SMP_CMD_DHKEY_CHECK, sizeof(check), &check);
Johan Hedbergdddd3052014-06-01 15:38:09 +03001468}
1469
Johan Hedberg38606f12014-06-25 11:10:28 +03001470static u8 sc_passkey_send_confirm(struct smp_chan *smp)
1471{
1472 struct l2cap_conn *conn = smp->conn;
1473 struct hci_conn *hcon = conn->hcon;
1474 struct smp_cmd_pairing_confirm cfm;
1475 u8 r;
1476
1477 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1478 r |= 0x80;
1479
1480 get_random_bytes(smp->prnd, sizeof(smp->prnd));
1481
1482 if (smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd, r,
1483 cfm.confirm_val))
1484 return SMP_UNSPECIFIED;
1485
1486 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
1487
1488 return 0;
1489}
1490
1491static u8 sc_passkey_round(struct smp_chan *smp, u8 smp_op)
1492{
1493 struct l2cap_conn *conn = smp->conn;
1494 struct hci_conn *hcon = conn->hcon;
1495 struct hci_dev *hdev = hcon->hdev;
1496 u8 cfm[16], r;
1497
1498 /* Ignore the PDU if we've already done 20 rounds (0 - 19) */
1499 if (smp->passkey_round >= 20)
1500 return 0;
1501
1502 switch (smp_op) {
1503 case SMP_CMD_PAIRING_RANDOM:
1504 r = ((hcon->passkey_notify >> smp->passkey_round) & 0x01);
1505 r |= 0x80;
1506
1507 if (smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
1508 smp->rrnd, r, cfm))
1509 return SMP_UNSPECIFIED;
1510
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02001511 if (crypto_memneq(smp->pcnf, cfm, 16))
Johan Hedberg38606f12014-06-25 11:10:28 +03001512 return SMP_CONFIRM_FAILED;
1513
1514 smp->passkey_round++;
1515
1516 if (smp->passkey_round == 20) {
1517 /* Generate MacKey and LTK */
1518 if (sc_mackey_and_ltk(smp, smp->mackey, smp->tk))
1519 return SMP_UNSPECIFIED;
1520 }
1521
1522 /* The round is only complete when the initiator
1523 * receives pairing random.
1524 */
1525 if (!hcon->out) {
1526 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1527 sizeof(smp->prnd), smp->prnd);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001528 if (smp->passkey_round == 20)
Johan Hedberg38606f12014-06-25 11:10:28 +03001529 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001530 else
Johan Hedberg38606f12014-06-25 11:10:28 +03001531 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
Johan Hedberg38606f12014-06-25 11:10:28 +03001532 return 0;
1533 }
1534
1535 /* Start the next round */
1536 if (smp->passkey_round != 20)
1537 return sc_passkey_round(smp, 0);
1538
1539 /* Passkey rounds are complete - start DHKey Check */
1540 sc_dhkey_check(smp);
1541 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1542
1543 break;
1544
1545 case SMP_CMD_PAIRING_CONFIRM:
1546 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
1547 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
1548 return 0;
1549 }
1550
1551 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
1552
1553 if (hcon->out) {
1554 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
1555 sizeof(smp->prnd), smp->prnd);
1556 return 0;
1557 }
1558
1559 return sc_passkey_send_confirm(smp);
1560
1561 case SMP_CMD_PUBLIC_KEY:
1562 default:
1563 /* Initiating device starts the round */
1564 if (!hcon->out)
1565 return 0;
1566
1567 BT_DBG("%s Starting passkey round %u", hdev->name,
1568 smp->passkey_round + 1);
1569
1570 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1571
1572 return sc_passkey_send_confirm(smp);
1573 }
1574
1575 return 0;
1576}
1577
Johan Hedbergdddd3052014-06-01 15:38:09 +03001578static int sc_user_reply(struct smp_chan *smp, u16 mgmt_op, __le32 passkey)
1579{
Johan Hedberg38606f12014-06-25 11:10:28 +03001580 struct l2cap_conn *conn = smp->conn;
1581 struct hci_conn *hcon = conn->hcon;
1582 u8 smp_op;
1583
1584 clear_bit(SMP_FLAG_WAIT_USER, &smp->flags);
1585
Johan Hedbergdddd3052014-06-01 15:38:09 +03001586 switch (mgmt_op) {
1587 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1588 smp_failure(smp->conn, SMP_PASSKEY_ENTRY_FAILED);
1589 return 0;
1590 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
1591 smp_failure(smp->conn, SMP_NUMERIC_COMP_FAILED);
1592 return 0;
Johan Hedberg38606f12014-06-25 11:10:28 +03001593 case MGMT_OP_USER_PASSKEY_REPLY:
1594 hcon->passkey_notify = le32_to_cpu(passkey);
1595 smp->passkey_round = 0;
1596
1597 if (test_and_clear_bit(SMP_FLAG_CFM_PENDING, &smp->flags))
1598 smp_op = SMP_CMD_PAIRING_CONFIRM;
1599 else
1600 smp_op = 0;
1601
1602 if (sc_passkey_round(smp, smp_op))
1603 return -EIO;
1604
1605 return 0;
Johan Hedbergdddd3052014-06-01 15:38:09 +03001606 }
1607
Johan Hedbergd3e54a82014-06-04 11:07:40 +03001608 /* Initiator sends DHKey check first */
1609 if (hcon->out) {
1610 sc_dhkey_check(smp);
1611 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
1612 } else if (test_and_clear_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags)) {
1613 sc_dhkey_check(smp);
1614 sc_add_ltk(smp);
1615 }
Johan Hedberg760b0182014-06-06 11:44:05 +03001616
1617 return 0;
1618}
1619
Brian Gix2b64d152011-12-21 16:12:12 -08001620int smp_user_confirm_reply(struct hci_conn *hcon, u16 mgmt_op, __le32 passkey)
1621{
Johan Hedbergb10e8012014-06-27 14:23:07 +03001622 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001623 struct l2cap_chan *chan;
Brian Gix2b64d152011-12-21 16:12:12 -08001624 struct smp_chan *smp;
1625 u32 value;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001626 int err;
Brian Gix2b64d152011-12-21 16:12:12 -08001627
1628 BT_DBG("");
1629
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001630 if (!conn)
Brian Gix2b64d152011-12-21 16:12:12 -08001631 return -ENOTCONN;
1632
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001633 chan = conn->smp;
1634 if (!chan)
1635 return -ENOTCONN;
1636
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001637 l2cap_chan_lock(chan);
1638 if (!chan->data) {
1639 err = -ENOTCONN;
1640 goto unlock;
1641 }
1642
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001643 smp = chan->data;
Brian Gix2b64d152011-12-21 16:12:12 -08001644
Johan Hedberg760b0182014-06-06 11:44:05 +03001645 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1646 err = sc_user_reply(smp, mgmt_op, passkey);
1647 goto unlock;
1648 }
1649
Brian Gix2b64d152011-12-21 16:12:12 -08001650 switch (mgmt_op) {
1651 case MGMT_OP_USER_PASSKEY_REPLY:
1652 value = le32_to_cpu(passkey);
Johan Hedberg943a7322014-03-18 12:58:24 +02001653 memset(smp->tk, 0, sizeof(smp->tk));
Brian Gix2b64d152011-12-21 16:12:12 -08001654 BT_DBG("PassKey: %d", value);
Johan Hedberg943a7322014-03-18 12:58:24 +02001655 put_unaligned_le32(value, smp->tk);
Brian Gix2b64d152011-12-21 16:12:12 -08001656 /* Fall Through */
1657 case MGMT_OP_USER_CONFIRM_REPLY:
Johan Hedberg4a74d652014-05-20 09:45:50 +03001658 set_bit(SMP_FLAG_TK_VALID, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08001659 break;
1660 case MGMT_OP_USER_PASSKEY_NEG_REPLY:
1661 case MGMT_OP_USER_CONFIRM_NEG_REPLY:
Johan Hedberg84794e12013-11-06 11:24:57 +02001662 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001663 err = 0;
1664 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001665 default:
Johan Hedberg84794e12013-11-06 11:24:57 +02001666 smp_failure(conn, SMP_PASSKEY_ENTRY_FAILED);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001667 err = -EOPNOTSUPP;
1668 goto unlock;
Brian Gix2b64d152011-12-21 16:12:12 -08001669 }
1670
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001671 err = 0;
1672
Brian Gix2b64d152011-12-21 16:12:12 -08001673 /* If it is our turn to send Pairing Confirm, do so now */
Johan Hedberg1cc61142014-05-20 09:45:52 +03001674 if (test_bit(SMP_FLAG_CFM_PENDING, &smp->flags)) {
1675 u8 rsp = smp_confirm(smp);
1676 if (rsp)
1677 smp_failure(conn, rsp);
1678 }
Brian Gix2b64d152011-12-21 16:12:12 -08001679
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001680unlock:
1681 l2cap_chan_unlock(chan);
1682 return err;
Brian Gix2b64d152011-12-21 16:12:12 -08001683}
1684
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001685static void build_bredr_pairing_cmd(struct smp_chan *smp,
1686 struct smp_cmd_pairing *req,
1687 struct smp_cmd_pairing *rsp)
1688{
1689 struct l2cap_conn *conn = smp->conn;
1690 struct hci_dev *hdev = conn->hcon->hdev;
1691 u8 local_dist = 0, remote_dist = 0;
1692
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001693 if (hci_dev_test_flag(hdev, HCI_BONDABLE)) {
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001694 local_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1695 remote_dist = SMP_DIST_ENC_KEY | SMP_DIST_SIGN;
1696 }
1697
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001698 if (hci_dev_test_flag(hdev, HCI_RPA_RESOLVING))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001699 remote_dist |= SMP_DIST_ID_KEY;
1700
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001701 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001702 local_dist |= SMP_DIST_ID_KEY;
1703
1704 if (!rsp) {
1705 memset(req, 0, sizeof(*req));
1706
Johan Hedberga62da6f2016-12-08 08:32:54 +02001707 req->auth_req = SMP_AUTH_CT2;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001708 req->init_key_dist = local_dist;
1709 req->resp_key_dist = remote_dist;
Johan Hedberge3f6a252015-06-11 13:52:30 +03001710 req->max_key_size = conn->hcon->enc_key_size;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001711
1712 smp->remote_key_dist = remote_dist;
1713
1714 return;
1715 }
1716
1717 memset(rsp, 0, sizeof(*rsp));
1718
Johan Hedberga62da6f2016-12-08 08:32:54 +02001719 rsp->auth_req = SMP_AUTH_CT2;
Johan Hedberge3f6a252015-06-11 13:52:30 +03001720 rsp->max_key_size = conn->hcon->enc_key_size;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001721 rsp->init_key_dist = req->init_key_dist & remote_dist;
1722 rsp->resp_key_dist = req->resp_key_dist & local_dist;
1723
1724 smp->remote_key_dist = rsp->init_key_dist;
1725}
1726
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001727static u8 smp_cmd_pairing_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001728{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001729 struct smp_cmd_pairing rsp, *req = (void *) skb->data;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001730 struct l2cap_chan *chan = conn->smp;
Johan Hedbergb3c64102014-07-10 11:02:07 +03001731 struct hci_dev *hdev = conn->hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001732 struct smp_chan *smp;
Johan Hedbergc7262e72014-06-17 13:07:37 +03001733 u8 key_size, auth, sec_level;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001734 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001735
1736 BT_DBG("conn %p", conn);
1737
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001738 if (skb->len < sizeof(*req))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001739 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001740
Johan Hedberg40bef302014-07-16 11:42:27 +03001741 if (conn->hcon->role != HCI_ROLE_SLAVE)
Brian Gix2b64d152011-12-21 16:12:12 -08001742 return SMP_CMD_NOTSUPP;
1743
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001744 if (!chan->data)
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001745 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03001746 else
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001747 smp = chan->data;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001748
Andrei Emeltchenkod08fd0e2012-07-19 17:03:43 +03001749 if (!smp)
1750 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03001751
Johan Hedbergc05b9332014-09-10 17:37:42 -07001752 /* We didn't start the pairing, so match remote */
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001753 auth = req->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001754
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001755 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07001756 (auth & SMP_AUTH_BONDING))
Johan Hedbergb3c64102014-07-10 11:02:07 +03001757 return SMP_PAIRING_NOTSUPP;
1758
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001759 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001760 return SMP_AUTH_REQUIREMENTS;
1761
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001762 smp->preq[0] = SMP_CMD_PAIRING_REQ;
1763 memcpy(&smp->preq[1], req, sizeof(*req));
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001764 skb_pull(skb, sizeof(*req));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001765
Johan Hedbergcb06d362015-03-16 21:12:34 +02001766 /* If the remote side's OOB flag is set it means it has
1767 * successfully received our local OOB data - therefore set the
1768 * flag to indicate that local OOB is in use.
1769 */
Johan Hedberg94f14e42018-09-11 14:10:12 +03001770 if (req->oob_flag == SMP_OOB_PRESENT && SMP_DEV(hdev)->local_oob)
Johan Hedberg58428562015-03-16 11:45:45 +02001771 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1772
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001773 /* SMP over BR/EDR requires special treatment */
1774 if (conn->hcon->type == ACL_LINK) {
1775 /* We must have a BR/EDR SC link */
Marcel Holtmann08f63cc2014-12-07 16:19:12 +01001776 if (!test_bit(HCI_CONN_AES_CCM, &conn->hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07001777 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001778 return SMP_CROSS_TRANSP_NOT_ALLOWED;
1779
1780 set_bit(SMP_FLAG_SC, &smp->flags);
1781
1782 build_bredr_pairing_cmd(smp, req, &rsp);
1783
Johan Hedberga62da6f2016-12-08 08:32:54 +02001784 if (req->auth_req & SMP_AUTH_CT2)
1785 set_bit(SMP_FLAG_CT2, &smp->flags);
1786
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001787 key_size = min(req->max_key_size, rsp.max_key_size);
1788 if (check_enc_key_size(conn, key_size))
1789 return SMP_ENC_KEY_SIZE;
1790
1791 /* Clear bits which are generated but not distributed */
1792 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1793
1794 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1795 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
1796 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
1797
1798 smp_distribute_keys(smp);
1799 return 0;
1800 }
1801
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001802 build_pairing_cmd(conn, req, &rsp, auth);
1803
Johan Hedberga62da6f2016-12-08 08:32:54 +02001804 if (rsp.auth_req & SMP_AUTH_SC) {
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03001805 set_bit(SMP_FLAG_SC, &smp->flags);
1806
Johan Hedberga62da6f2016-12-08 08:32:54 +02001807 if (rsp.auth_req & SMP_AUTH_CT2)
1808 set_bit(SMP_FLAG_CT2, &smp->flags);
1809 }
1810
Johan Hedberg5be5e272014-09-10 17:58:54 -07001811 if (conn->hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07001812 sec_level = BT_SECURITY_MEDIUM;
1813 else
1814 sec_level = authreq_to_seclevel(auth);
1815
Johan Hedbergc7262e72014-06-17 13:07:37 +03001816 if (sec_level > conn->hcon->pending_sec_level)
1817 conn->hcon->pending_sec_level = sec_level;
Ido Yarivfdde0a22012-03-05 20:09:38 +02001818
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001819 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001820 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1821 u8 method;
1822
1823 method = get_auth_method(smp, conn->hcon->io_capability,
1824 req->io_capability);
1825 if (method == JUST_WORKS || method == JUST_CFM)
1826 return SMP_AUTH_REQUIREMENTS;
1827 }
1828
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001829 key_size = min(req->max_key_size, rsp.max_key_size);
1830 if (check_enc_key_size(conn, key_size))
1831 return SMP_ENC_KEY_SIZE;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001832
Johan Hedberge84a6b12013-12-02 10:49:03 +02001833 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03001834
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001835 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1836 memcpy(&smp->prsp[1], &rsp, sizeof(rsp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03001837
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001838 smp_send_cmd(conn, SMP_CMD_PAIRING_RSP, sizeof(rsp), &rsp);
Johan Hedberg3b191462014-06-06 10:50:15 +03001839
1840 clear_bit(SMP_FLAG_INITIATOR, &smp->flags);
1841
Johan Hedberg19c5ce92015-03-15 19:34:04 +02001842 /* Strictly speaking we shouldn't allow Pairing Confirm for the
1843 * SC case, however some implementations incorrectly copy RFU auth
1844 * req bits from our security request, which may create a false
1845 * positive SC enablement.
1846 */
1847 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
1848
Johan Hedberg3b191462014-06-06 10:50:15 +03001849 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1850 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
1851 /* Clear bits which are generated but not distributed */
1852 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1853 /* Wait for Public Key from Initiating Device */
1854 return 0;
Johan Hedberg3b191462014-06-06 10:50:15 +03001855 }
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001856
Brian Gix2b64d152011-12-21 16:12:12 -08001857 /* Request setup of TK */
1858 ret = tk_request(conn, 0, auth, rsp.io_capability, req->io_capability);
1859 if (ret)
1860 return SMP_UNSPECIFIED;
1861
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001862 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001863}
1864
Johan Hedberg3b191462014-06-06 10:50:15 +03001865static u8 sc_send_public_key(struct smp_chan *smp)
1866{
Johan Hedberg70157ef2014-06-24 15:22:59 +03001867 struct hci_dev *hdev = smp->conn->hcon->hdev;
1868
Johan Hedberg3b191462014-06-06 10:50:15 +03001869 BT_DBG("");
1870
Johan Hedberg1a8bab42015-03-16 11:45:44 +02001871 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) {
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001872 struct l2cap_chan *chan = hdev->smp_data;
1873 struct smp_dev *smp_dev;
1874
1875 if (!chan || !chan->data)
1876 return SMP_UNSPECIFIED;
1877
1878 smp_dev = chan->data;
1879
1880 memcpy(smp->local_pk, smp_dev->local_pk, 64);
Marcel Holtmannfb334fe2015-03-16 12:34:57 -07001881 memcpy(smp->lr, smp_dev->local_rand, 16);
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001882
1883 if (smp_dev->debug_key)
1884 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1885
1886 goto done;
1887 }
1888
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001889 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS)) {
Johan Hedberg70157ef2014-06-24 15:22:59 +03001890 BT_DBG("Using debug keys");
Tudor Ambarusc0153b02017-09-28 17:14:55 +03001891 if (set_ecdh_privkey(smp->tfm_ecdh, debug_sk))
1892 return SMP_UNSPECIFIED;
Johan Hedberg70157ef2014-06-24 15:22:59 +03001893 memcpy(smp->local_pk, debug_pk, 64);
Johan Hedberg70157ef2014-06-24 15:22:59 +03001894 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
1895 } else {
1896 while (true) {
Tudor Ambarusc0153b02017-09-28 17:14:55 +03001897 /* Generate key pair for Secure Connections */
1898 if (generate_ecdh_keys(smp->tfm_ecdh, smp->local_pk))
Johan Hedberg70157ef2014-06-24 15:22:59 +03001899 return SMP_UNSPECIFIED;
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001900
Johan Hedberg70157ef2014-06-24 15:22:59 +03001901 /* This is unlikely, but we need to check that
1902 * we didn't accidentially generate a debug key.
1903 */
Tudor Ambarusc0153b02017-09-28 17:14:55 +03001904 if (crypto_memneq(smp->local_pk, debug_pk, 64))
Johan Hedberg70157ef2014-06-24 15:22:59 +03001905 break;
1906 }
Johan Hedberg6c0dcc52014-06-06 15:33:30 +03001907 }
Johan Hedberg3b191462014-06-06 10:50:15 +03001908
Marcel Holtmann33d0c032015-03-16 01:10:24 -07001909done:
Johan Hedbergc7a3d572014-12-01 22:03:16 +02001910 SMP_DBG("Local Public Key X: %32phN", smp->local_pk);
Marcel Holtmann8e4e2ee2015-03-16 01:10:25 -07001911 SMP_DBG("Local Public Key Y: %32phN", smp->local_pk + 32);
Johan Hedberg3b191462014-06-06 10:50:15 +03001912
1913 smp_send_cmd(smp->conn, SMP_CMD_PUBLIC_KEY, 64, smp->local_pk);
1914
1915 return 0;
1916}
1917
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001918static u8 smp_cmd_pairing_rsp(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001919{
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001920 struct smp_cmd_pairing *req, *rsp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03001921 struct l2cap_chan *chan = conn->smp;
1922 struct smp_chan *smp = chan->data;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001923 struct hci_dev *hdev = conn->hcon->hdev;
Johan Hedberg3a7dbfb2014-09-10 17:37:41 -07001924 u8 key_size, auth;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001925 int ret;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03001926
1927 BT_DBG("conn %p", conn);
1928
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001929 if (skb->len < sizeof(*rsp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03001930 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02001931
Johan Hedberg40bef302014-07-16 11:42:27 +03001932 if (conn->hcon->role != HCI_ROLE_MASTER)
Brian Gix2b64d152011-12-21 16:12:12 -08001933 return SMP_CMD_NOTSUPP;
1934
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001935 skb_pull(skb, sizeof(*rsp));
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03001936
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03001937 req = (void *) &smp->preq[1];
Vinicius Costa Gomes3158c502011-06-14 13:37:42 -03001938
1939 key_size = min(req->max_key_size, rsp->max_key_size);
1940 if (check_enc_key_size(conn, key_size))
1941 return SMP_ENC_KEY_SIZE;
1942
Johan Hedberg0edb14d2014-05-26 13:29:28 +03001943 auth = rsp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07001944
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001945 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07001946 return SMP_AUTH_REQUIREMENTS;
1947
Johan Hedbergcb06d362015-03-16 21:12:34 +02001948 /* If the remote side's OOB flag is set it means it has
1949 * successfully received our local OOB data - therefore set the
1950 * flag to indicate that local OOB is in use.
1951 */
Johan Hedberg94f14e42018-09-11 14:10:12 +03001952 if (rsp->oob_flag == SMP_OOB_PRESENT && SMP_DEV(hdev)->local_oob)
Johan Hedberg58428562015-03-16 11:45:45 +02001953 set_bit(SMP_FLAG_LOCAL_OOB, &smp->flags);
1954
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001955 smp->prsp[0] = SMP_CMD_PAIRING_RSP;
1956 memcpy(&smp->prsp[1], rsp, sizeof(*rsp));
1957
1958 /* Update remote key distribution in case the remote cleared
1959 * some bits that we had enabled in our request.
1960 */
1961 smp->remote_key_dist &= rsp->resp_key_dist;
1962
Johan Hedberga62da6f2016-12-08 08:32:54 +02001963 if ((req->auth_req & SMP_AUTH_CT2) && (auth & SMP_AUTH_CT2))
1964 set_bit(SMP_FLAG_CT2, &smp->flags);
1965
Johan Hedbergb5ae3442014-08-14 12:34:26 +03001966 /* For BR/EDR this means we're done and can start phase 3 */
1967 if (conn->hcon->type == ACL_LINK) {
1968 /* Clear bits which are generated but not distributed */
1969 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1970 smp_distribute_keys(smp);
1971 return 0;
1972 }
1973
Johan Hedberg65668772014-05-16 11:03:34 +03001974 if ((req->auth_req & SMP_AUTH_SC) && (auth & SMP_AUTH_SC))
1975 set_bit(SMP_FLAG_SC, &smp->flags);
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03001976 else if (conn->hcon->pending_sec_level > BT_SECURITY_HIGH)
1977 conn->hcon->pending_sec_level = BT_SECURITY_HIGH;
Johan Hedberg65668772014-05-16 11:03:34 +03001978
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001979 /* If we need MITM check that it can be achieved */
Johan Hedberg2ed8f652014-06-17 13:07:39 +03001980 if (conn->hcon->pending_sec_level >= BT_SECURITY_HIGH) {
1981 u8 method;
1982
1983 method = get_auth_method(smp, req->io_capability,
1984 rsp->io_capability);
1985 if (method == JUST_WORKS || method == JUST_CFM)
1986 return SMP_AUTH_REQUIREMENTS;
1987 }
1988
Johan Hedberge84a6b12013-12-02 10:49:03 +02001989 get_random_bytes(smp->prnd, sizeof(smp->prnd));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03001990
Johan Hedbergfdcc4be2014-03-14 10:53:50 +02001991 /* Update remote key distribution in case the remote cleared
1992 * some bits that we had enabled in our request.
1993 */
1994 smp->remote_key_dist &= rsp->resp_key_dist;
1995
Johan Hedberg3b191462014-06-06 10:50:15 +03001996 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
1997 /* Clear bits which are generated but not distributed */
1998 smp->remote_key_dist &= ~SMP_SC_NO_DIST;
1999 SMP_ALLOW_CMD(smp, SMP_CMD_PUBLIC_KEY);
2000 return sc_send_public_key(smp);
2001 }
2002
Johan Hedbergc05b9332014-09-10 17:37:42 -07002003 auth |= req->auth_req;
Brian Gix2b64d152011-12-21 16:12:12 -08002004
Johan Hedberg476585e2012-06-06 18:54:15 +08002005 ret = tk_request(conn, 0, auth, req->io_capability, rsp->io_capability);
Brian Gix2b64d152011-12-21 16:12:12 -08002006 if (ret)
2007 return SMP_UNSPECIFIED;
2008
Johan Hedberg4a74d652014-05-20 09:45:50 +03002009 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Brian Gix2b64d152011-12-21 16:12:12 -08002010
2011 /* Can't compose response until we have been confirmed */
Johan Hedberg4a74d652014-05-20 09:45:50 +03002012 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03002013 return smp_confirm(smp);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002014
2015 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002016}
2017
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002018static u8 sc_check_confirm(struct smp_chan *smp)
2019{
2020 struct l2cap_conn *conn = smp->conn;
2021
2022 BT_DBG("");
2023
Johan Hedberg38606f12014-06-25 11:10:28 +03002024 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2025 return sc_passkey_round(smp, SMP_CMD_PAIRING_CONFIRM);
2026
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002027 if (conn->hcon->out) {
2028 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2029 smp->prnd);
2030 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2031 }
2032
2033 return 0;
2034}
2035
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002036/* Work-around for some implementations that incorrectly copy RFU bits
2037 * from our security request and thereby create the impression that
2038 * we're doing SC when in fact the remote doesn't support it.
2039 */
2040static int fixup_sc_false_positive(struct smp_chan *smp)
2041{
2042 struct l2cap_conn *conn = smp->conn;
2043 struct hci_conn *hcon = conn->hcon;
2044 struct hci_dev *hdev = hcon->hdev;
2045 struct smp_cmd_pairing *req, *rsp;
2046 u8 auth;
2047
2048 /* The issue is only observed when we're in slave role */
2049 if (hcon->out)
2050 return SMP_UNSPECIFIED;
2051
2052 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002053 bt_dev_err(hdev, "refusing legacy fallback in SC-only mode");
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002054 return SMP_UNSPECIFIED;
2055 }
2056
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002057 bt_dev_err(hdev, "trying to fall back to legacy SMP");
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002058
2059 req = (void *) &smp->preq[1];
2060 rsp = (void *) &smp->prsp[1];
2061
2062 /* Rebuild key dist flags which may have been cleared for SC */
2063 smp->remote_key_dist = (req->init_key_dist & rsp->resp_key_dist);
2064
2065 auth = req->auth_req & AUTH_REQ_MASK(hdev);
2066
2067 if (tk_request(conn, 0, auth, rsp->io_capability, req->io_capability)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002068 bt_dev_err(hdev, "failed to fall back to legacy SMP");
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002069 return SMP_UNSPECIFIED;
2070 }
2071
2072 clear_bit(SMP_FLAG_SC, &smp->flags);
2073
2074 return 0;
2075}
2076
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002077static u8 smp_cmd_pairing_confirm(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002078{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002079 struct l2cap_chan *chan = conn->smp;
2080 struct smp_chan *smp = chan->data;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002081
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002082 BT_DBG("conn %p %s", conn, conn->hcon->out ? "master" : "slave");
2083
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002084 if (skb->len < sizeof(smp->pcnf))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002085 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002086
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002087 memcpy(smp->pcnf, skb->data, sizeof(smp->pcnf));
2088 skb_pull(skb, sizeof(smp->pcnf));
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002089
Johan Hedberg19c5ce92015-03-15 19:34:04 +02002090 if (test_bit(SMP_FLAG_SC, &smp->flags)) {
2091 int ret;
2092
2093 /* Public Key exchange must happen before any other steps */
2094 if (test_bit(SMP_FLAG_REMOTE_PK, &smp->flags))
2095 return sc_check_confirm(smp);
2096
2097 BT_ERR("Unexpected SMP Pairing Confirm");
2098
2099 ret = fixup_sc_false_positive(smp);
2100 if (ret)
2101 return ret;
2102 }
Johan Hedbergdcee2b32014-06-06 11:36:38 +03002103
Johan Hedbergb28b4942014-09-05 22:19:55 +03002104 if (conn->hcon->out) {
Johan Hedberg943a7322014-03-18 12:58:24 +02002105 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2106 smp->prnd);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002107 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2108 return 0;
2109 }
2110
2111 if (test_bit(SMP_FLAG_TK_VALID, &smp->flags))
Johan Hedberg1cc61142014-05-20 09:45:52 +03002112 return smp_confirm(smp);
Marcel Holtmann983f9812015-03-11 17:47:40 -07002113
2114 set_bit(SMP_FLAG_CFM_PENDING, &smp->flags);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002115
2116 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002117}
2118
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002119static u8 smp_cmd_pairing_random(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002120{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002121 struct l2cap_chan *chan = conn->smp;
2122 struct smp_chan *smp = chan->data;
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002123 struct hci_conn *hcon = conn->hcon;
2124 u8 *pkax, *pkbx, *na, *nb;
2125 u32 passkey;
2126 int err;
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002127
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002128 BT_DBG("conn %p", conn);
Anderson Briglia7d24ddc2011-06-09 18:50:46 -03002129
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002130 if (skb->len < sizeof(smp->rrnd))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002131 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002132
Johan Hedberg943a7322014-03-18 12:58:24 +02002133 memcpy(smp->rrnd, skb->data, sizeof(smp->rrnd));
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002134 skb_pull(skb, sizeof(smp->rrnd));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002135
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002136 if (!test_bit(SMP_FLAG_SC, &smp->flags))
2137 return smp_random(smp);
2138
Johan Hedberg580039e2014-12-03 16:26:37 +02002139 if (hcon->out) {
2140 pkax = smp->local_pk;
2141 pkbx = smp->remote_pk;
2142 na = smp->prnd;
2143 nb = smp->rrnd;
2144 } else {
2145 pkax = smp->remote_pk;
2146 pkbx = smp->local_pk;
2147 na = smp->rrnd;
2148 nb = smp->prnd;
2149 }
2150
Johan Hedberga29b0732014-10-28 15:17:05 +01002151 if (smp->method == REQ_OOB) {
2152 if (!hcon->out)
2153 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2154 sizeof(smp->prnd), smp->prnd);
2155 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2156 goto mackey_and_ltk;
2157 }
2158
Johan Hedberg38606f12014-06-25 11:10:28 +03002159 /* Passkey entry has special treatment */
2160 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2161 return sc_passkey_round(smp, SMP_CMD_PAIRING_RANDOM);
2162
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002163 if (hcon->out) {
2164 u8 cfm[16];
2165
2166 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->local_pk,
2167 smp->rrnd, 0, cfm);
2168 if (err)
2169 return SMP_UNSPECIFIED;
2170
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02002171 if (crypto_memneq(smp->pcnf, cfm, 16))
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002172 return SMP_CONFIRM_FAILED;
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002173 } else {
2174 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM, sizeof(smp->prnd),
2175 smp->prnd);
2176 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002177 }
2178
Johan Hedberga29b0732014-10-28 15:17:05 +01002179mackey_and_ltk:
Johan Hedberg760b0182014-06-06 11:44:05 +03002180 /* Generate MacKey and LTK */
2181 err = sc_mackey_and_ltk(smp, smp->mackey, smp->tk);
2182 if (err)
2183 return SMP_UNSPECIFIED;
2184
Johan Hedberga29b0732014-10-28 15:17:05 +01002185 if (smp->method == JUST_WORKS || smp->method == REQ_OOB) {
Johan Hedbergdddd3052014-06-01 15:38:09 +03002186 if (hcon->out) {
Johan Hedberg38606f12014-06-25 11:10:28 +03002187 sc_dhkey_check(smp);
Johan Hedbergdddd3052014-06-01 15:38:09 +03002188 SMP_ALLOW_CMD(smp, SMP_CMD_DHKEY_CHECK);
2189 }
2190 return 0;
2191 }
2192
Johan Hedberg38606f12014-06-25 11:10:28 +03002193 err = smp_g2(smp->tfm_cmac, pkax, pkbx, na, nb, &passkey);
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002194 if (err)
2195 return SMP_UNSPECIFIED;
2196
Johan Hedberg38606f12014-06-25 11:10:28 +03002197 err = mgmt_user_confirm_request(hcon->hdev, &hcon->dst, hcon->type,
2198 hcon->dst_type, passkey, 0);
2199 if (err)
2200 return SMP_UNSPECIFIED;
2201
2202 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2203
Johan Hedberg191dc7fe22014-06-06 11:39:49 +03002204 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002205}
2206
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002207static bool smp_ltk_encrypt(struct l2cap_conn *conn, u8 sec_level)
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002208{
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002209 struct smp_ltk *key;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002210 struct hci_conn *hcon = conn->hcon;
2211
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002212 key = hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role);
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002213 if (!key)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002214 return false;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002215
Johan Hedberga6f78332014-09-10 17:37:45 -07002216 if (smp_ltk_sec_level(key) < sec_level)
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002217 return false;
Johan Hedberg4dab7862012-06-07 14:58:37 +08002218
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002219 if (test_and_set_bit(HCI_CONN_ENCRYPT_PEND, &hcon->flags))
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002220 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002221
Johan Hedberg8b76ce32015-06-08 18:14:39 +03002222 hci_le_start_enc(hcon, key->ediv, key->rand, key->val, key->enc_size);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002223 hcon->enc_key_size = key->enc_size;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002224
Johan Hedbergfe59a052014-07-01 19:14:12 +03002225 /* We never store STKs for master role, so clear this flag */
2226 clear_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags);
2227
Marcel Holtmannf81cd822014-07-01 10:59:24 +02002228 return true;
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002229}
Marcel Holtmannf1560462013-10-13 05:43:25 -07002230
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002231bool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
2232 enum smp_key_pref key_pref)
Johan Hedberg854f4722014-07-01 18:40:20 +03002233{
2234 if (sec_level == BT_SECURITY_LOW)
2235 return true;
2236
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002237 /* If we're encrypted with an STK but the caller prefers using
2238 * LTK claim insufficient security. This way we allow the
2239 * connection to be re-encrypted with an LTK, even if the LTK
2240 * provides the same level of security. Only exception is if we
2241 * don't have an LTK (e.g. because of key distribution bits).
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002242 */
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002243 if (key_pref == SMP_USE_LTK &&
2244 test_bit(HCI_CONN_STK_ENCRYPT, &hcon->flags) &&
Johan Hedbergf3a73d92014-05-29 15:02:59 +03002245 hci_find_ltk(hcon->hdev, &hcon->dst, hcon->dst_type, hcon->role))
Johan Hedberg9ab65d602014-07-01 19:14:13 +03002246 return false;
2247
Johan Hedberg854f4722014-07-01 18:40:20 +03002248 if (hcon->sec_level >= sec_level)
2249 return true;
2250
2251 return false;
2252}
2253
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002254static u8 smp_cmd_security_req(struct l2cap_conn *conn, struct sk_buff *skb)
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002255{
2256 struct smp_cmd_security_req *rp = (void *) skb->data;
2257 struct smp_cmd_pairing cp;
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002258 struct hci_conn *hcon = conn->hcon;
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002259 struct hci_dev *hdev = hcon->hdev;
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002260 struct smp_chan *smp;
Johan Hedbergc05b9332014-09-10 17:37:42 -07002261 u8 sec_level, auth;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002262
2263 BT_DBG("conn %p", conn);
2264
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002265 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002266 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002267
Johan Hedberg40bef302014-07-16 11:42:27 +03002268 if (hcon->role != HCI_ROLE_MASTER)
Johan Hedberg86ca9ea2013-11-05 11:30:39 +02002269 return SMP_CMD_NOTSUPP;
2270
Johan Hedberg0edb14d2014-05-26 13:29:28 +03002271 auth = rp->auth_req & AUTH_REQ_MASK(hdev);
Johan Hedbergc05b9332014-09-10 17:37:42 -07002272
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002273 if (hci_dev_test_flag(hdev, HCI_SC_ONLY) && !(auth & SMP_AUTH_SC))
Johan Hedberg903b71c2014-09-08 16:59:18 -07002274 return SMP_AUTH_REQUIREMENTS;
2275
Johan Hedberg5be5e272014-09-10 17:58:54 -07002276 if (hcon->io_capability == HCI_IO_NO_INPUT_OUTPUT)
Johan Hedberg1afc2a12014-09-10 17:37:44 -07002277 sec_level = BT_SECURITY_MEDIUM;
2278 else
2279 sec_level = authreq_to_seclevel(auth);
2280
Szymon Janc64e759f2018-02-26 15:41:53 +01002281 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK)) {
2282 /* If link is already encrypted with sufficient security we
2283 * still need refresh encryption as per Core Spec 5.0 Vol 3,
2284 * Part H 2.4.6
2285 */
2286 smp_ltk_encrypt(conn, hcon->sec_level);
Johan Hedberg854f4722014-07-01 18:40:20 +03002287 return 0;
Szymon Janc64e759f2018-02-26 15:41:53 +01002288 }
Johan Hedberg854f4722014-07-01 18:40:20 +03002289
Johan Hedbergc7262e72014-06-17 13:07:37 +03002290 if (sec_level > hcon->pending_sec_level)
2291 hcon->pending_sec_level = sec_level;
Vinicius Costa Gomesfeb45eb2011-08-25 20:02:35 -03002292
Johan Hedberg4dab7862012-06-07 14:58:37 +08002293 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
Vinicius Costa Gomes988c5992011-08-25 20:02:28 -03002294 return 0;
2295
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002296 smp = smp_chan_create(conn);
Johan Hedbergc29d2442014-06-16 19:25:14 +03002297 if (!smp)
2298 return SMP_UNSPECIFIED;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002299
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002300 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedbergc05b9332014-09-10 17:37:42 -07002301 (auth & SMP_AUTH_BONDING))
Johan Hedberg616d55b2014-07-29 14:18:48 +03002302 return SMP_PAIRING_NOTSUPP;
2303
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002304 skb_pull(skb, sizeof(*rp));
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002305
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002306 memset(&cp, 0, sizeof(cp));
Johan Hedbergc05b9332014-09-10 17:37:42 -07002307 build_pairing_cmd(conn, &cp, NULL, auth);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002308
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002309 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2310 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002311
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002312 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002313 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002314
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002315 return 0;
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002316}
2317
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002318int smp_conn_security(struct hci_conn *hcon, __u8 sec_level)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002319{
Vinicius Costa Gomescc110922012-08-23 21:32:43 -03002320 struct l2cap_conn *conn = hcon->l2cap_data;
Johan Hedbergc68b7f12014-09-06 06:59:10 +03002321 struct l2cap_chan *chan;
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002322 struct smp_chan *smp;
Brian Gix2b64d152011-12-21 16:12:12 -08002323 __u8 authreq;
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002324 int ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002325
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002326 BT_DBG("conn %p hcon %p level 0x%2.2x", conn, hcon, sec_level);
2327
Johan Hedberg0a66cf22014-03-24 14:39:03 +02002328 /* This may be NULL if there's an unexpected disconnection */
2329 if (!conn)
2330 return 1;
2331
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002332 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED))
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002333 return 1;
2334
Johan Hedberg35dc6f82014-11-13 10:55:18 +02002335 if (smp_sufficient_security(hcon, sec_level, SMP_USE_LTK))
Vinicius Costa Gomesf1cb9af2011-01-26 21:42:57 -03002336 return 1;
2337
Johan Hedbergc7262e72014-06-17 13:07:37 +03002338 if (sec_level > hcon->pending_sec_level)
2339 hcon->pending_sec_level = sec_level;
2340
Johan Hedberg40bef302014-07-16 11:42:27 +03002341 if (hcon->role == HCI_ROLE_MASTER)
Johan Hedbergc7262e72014-06-17 13:07:37 +03002342 if (smp_ltk_encrypt(conn, hcon->pending_sec_level))
2343 return 0;
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002344
Johan Hedbergd8949aa2015-09-04 12:22:46 +03002345 chan = conn->smp;
2346 if (!chan) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002347 bt_dev_err(hcon->hdev, "security requested but not available");
Johan Hedbergd8949aa2015-09-04 12:22:46 +03002348 return 1;
2349 }
2350
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002351 l2cap_chan_lock(chan);
2352
2353 /* If SMP is already in progress ignore this request */
2354 if (chan->data) {
2355 ret = 0;
2356 goto unlock;
2357 }
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002358
Vinicius Costa Gomes8aab4752011-09-05 14:31:31 -03002359 smp = smp_chan_create(conn);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002360 if (!smp) {
2361 ret = 1;
2362 goto unlock;
2363 }
Brian Gix2b64d152011-12-21 16:12:12 -08002364
2365 authreq = seclevel_to_authreq(sec_level);
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002366
Johan Hedberga62da6f2016-12-08 08:32:54 +02002367 if (hci_dev_test_flag(hcon->hdev, HCI_SC_ENABLED)) {
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002368 authreq |= SMP_AUTH_SC;
Johan Hedberga62da6f2016-12-08 08:32:54 +02002369 if (hci_dev_test_flag(hcon->hdev, HCI_SSP_ENABLED))
2370 authreq |= SMP_AUTH_CT2;
2371 }
Johan Hedbergd2eb9e12014-05-16 10:59:06 +03002372
Johan Hedberg79897d22014-06-01 09:45:24 +03002373 /* Require MITM if IO Capability allows or the security level
2374 * requires it.
Johan Hedberg2e233642014-03-18 15:42:30 +02002375 */
Johan Hedberg79897d22014-06-01 09:45:24 +03002376 if (hcon->io_capability != HCI_IO_NO_INPUT_OUTPUT ||
Johan Hedbergc7262e72014-06-17 13:07:37 +03002377 hcon->pending_sec_level > BT_SECURITY_MEDIUM)
Johan Hedberg2e233642014-03-18 15:42:30 +02002378 authreq |= SMP_AUTH_MITM;
2379
Johan Hedberg40bef302014-07-16 11:42:27 +03002380 if (hcon->role == HCI_ROLE_MASTER) {
Vinicius Costa Gomesd26a2342011-08-19 21:06:51 -03002381 struct smp_cmd_pairing cp;
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002382
Brian Gix2b64d152011-12-21 16:12:12 -08002383 build_pairing_cmd(conn, &cp, NULL, authreq);
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002384 smp->preq[0] = SMP_CMD_PAIRING_REQ;
2385 memcpy(&smp->preq[1], &cp, sizeof(cp));
Anderson Brigliaf01ead32011-06-09 18:50:45 -03002386
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002387 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002388 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002389 } else {
2390 struct smp_cmd_security_req cp;
Brian Gix2b64d152011-12-21 16:12:12 -08002391 cp.auth_req = authreq;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002392 smp_send_cmd(conn, SMP_CMD_SECURITY_REQ, sizeof(cp), &cp);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002393 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_REQ);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002394 }
2395
Johan Hedberg4a74d652014-05-20 09:45:50 +03002396 set_bit(SMP_FLAG_INITIATOR, &smp->flags);
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002397 ret = 0;
Johan Hedbergedca7922014-03-24 15:54:11 +02002398
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002399unlock:
2400 l2cap_chan_unlock(chan);
2401 return ret;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002402}
2403
Matias Karhumaacb28c302018-09-26 09:13:46 +03002404int smp_cancel_and_remove_pairing(struct hci_dev *hdev, bdaddr_t *bdaddr,
2405 u8 addr_type)
Johan Hedbergc81d5552015-10-22 09:38:35 +03002406{
Matias Karhumaacb28c302018-09-26 09:13:46 +03002407 struct hci_conn *hcon;
2408 struct l2cap_conn *conn;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002409 struct l2cap_chan *chan;
2410 struct smp_chan *smp;
Matias Karhumaacb28c302018-09-26 09:13:46 +03002411 int err;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002412
Matias Karhumaacb28c302018-09-26 09:13:46 +03002413 err = hci_remove_ltk(hdev, bdaddr, addr_type);
2414 hci_remove_irk(hdev, bdaddr, addr_type);
2415
2416 hcon = hci_conn_hash_lookup_le(hdev, bdaddr, addr_type);
2417 if (!hcon)
2418 goto done;
2419
2420 conn = hcon->l2cap_data;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002421 if (!conn)
Matias Karhumaacb28c302018-09-26 09:13:46 +03002422 goto done;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002423
2424 chan = conn->smp;
2425 if (!chan)
Matias Karhumaacb28c302018-09-26 09:13:46 +03002426 goto done;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002427
2428 l2cap_chan_lock(chan);
2429
2430 smp = chan->data;
2431 if (smp) {
Matias Karhumaacb28c302018-09-26 09:13:46 +03002432 /* Set keys to NULL to make sure smp_failure() does not try to
2433 * remove and free already invalidated rcu list entries. */
2434 smp->ltk = NULL;
2435 smp->slave_ltk = NULL;
2436 smp->remote_irk = NULL;
2437
Johan Hedbergc81d5552015-10-22 09:38:35 +03002438 if (test_bit(SMP_FLAG_COMPLETE, &smp->flags))
2439 smp_failure(conn, 0);
2440 else
2441 smp_failure(conn, SMP_UNSPECIFIED);
Matias Karhumaacb28c302018-09-26 09:13:46 +03002442 err = 0;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002443 }
2444
2445 l2cap_chan_unlock(chan);
Matias Karhumaacb28c302018-09-26 09:13:46 +03002446
2447done:
2448 return err;
Johan Hedbergc81d5552015-10-22 09:38:35 +03002449}
2450
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002451static int smp_cmd_encrypt_info(struct l2cap_conn *conn, struct sk_buff *skb)
2452{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002453 struct smp_cmd_encrypt_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002454 struct l2cap_chan *chan = conn->smp;
2455 struct smp_chan *smp = chan->data;
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002456
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002457 BT_DBG("conn %p", conn);
2458
2459 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002460 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002461
Johan Hedbergb28b4942014-09-05 22:19:55 +03002462 SMP_ALLOW_CMD(smp, SMP_CMD_MASTER_IDENT);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002463
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002464 skb_pull(skb, sizeof(*rp));
2465
Vinicius Costa Gomes1c1def02011-09-05 14:31:30 -03002466 memcpy(smp->tk, rp->ltk, sizeof(smp->tk));
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002467
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002468 return 0;
2469}
2470
2471static int smp_cmd_master_ident(struct l2cap_conn *conn, struct sk_buff *skb)
2472{
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002473 struct smp_cmd_master_ident *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002474 struct l2cap_chan *chan = conn->smp;
2475 struct smp_chan *smp = chan->data;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002476 struct hci_dev *hdev = conn->hcon->hdev;
2477 struct hci_conn *hcon = conn->hcon;
Johan Hedberg23d0e122014-02-19 14:57:46 +02002478 struct smp_ltk *ltk;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03002479 u8 authenticated;
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002480
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002481 BT_DBG("conn %p", conn);
2482
2483 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002484 return SMP_INVALID_PARAMS;
Johan Hedbergc46b98b2014-02-18 10:19:29 +02002485
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002486 /* Mark the information as received */
2487 smp->remote_key_dist &= ~SMP_DIST_ENC_KEY;
2488
Johan Hedbergb28b4942014-09-05 22:19:55 +03002489 if (smp->remote_key_dist & SMP_DIST_ID_KEY)
2490 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_INFO);
Johan Hedberg196332f2014-09-09 16:21:46 -07002491 else if (smp->remote_key_dist & SMP_DIST_SIGN)
2492 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002493
Vinicius Costa Gomes16b90832011-07-07 18:59:39 -03002494 skb_pull(skb, sizeof(*rp));
2495
Marcel Holtmannce39fb42013-10-13 02:23:39 -07002496 authenticated = (hcon->sec_level == BT_SECURITY_HIGH);
Johan Hedberg2ceba532014-06-16 19:25:16 +03002497 ltk = hci_add_ltk(hdev, &hcon->dst, hcon->dst_type, SMP_LTK,
Johan Hedberg23d0e122014-02-19 14:57:46 +02002498 authenticated, smp->tk, smp->enc_key_size,
2499 rp->ediv, rp->rand);
2500 smp->ltk = ltk;
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002501 if (!(smp->remote_key_dist & KEY_DIST_MASK))
Johan Hedbergd6268e82014-09-05 22:19:51 +03002502 smp_distribute_keys(smp);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002503
2504 return 0;
2505}
2506
Johan Hedbergfd349c02014-02-18 10:19:36 +02002507static int smp_cmd_ident_info(struct l2cap_conn *conn, struct sk_buff *skb)
2508{
2509 struct smp_cmd_ident_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002510 struct l2cap_chan *chan = conn->smp;
2511 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002512
2513 BT_DBG("");
2514
2515 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002516 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002517
Johan Hedbergb28b4942014-09-05 22:19:55 +03002518 SMP_ALLOW_CMD(smp, SMP_CMD_IDENT_ADDR_INFO);
Johan Hedberg6131ddc2014-02-18 10:19:37 +02002519
Johan Hedbergfd349c02014-02-18 10:19:36 +02002520 skb_pull(skb, sizeof(*info));
2521
2522 memcpy(smp->irk, info->irk, 16);
2523
2524 return 0;
2525}
2526
2527static int smp_cmd_ident_addr_info(struct l2cap_conn *conn,
2528 struct sk_buff *skb)
2529{
2530 struct smp_cmd_ident_addr_info *info = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002531 struct l2cap_chan *chan = conn->smp;
2532 struct smp_chan *smp = chan->data;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002533 struct hci_conn *hcon = conn->hcon;
2534 bdaddr_t rpa;
2535
2536 BT_DBG("");
2537
2538 if (skb->len < sizeof(*info))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002539 return SMP_INVALID_PARAMS;
Johan Hedbergfd349c02014-02-18 10:19:36 +02002540
Johan Hedberg9747a9f2014-02-26 23:33:43 +02002541 /* Mark the information as received */
2542 smp->remote_key_dist &= ~SMP_DIST_ID_KEY;
2543
Johan Hedbergb28b4942014-09-05 22:19:55 +03002544 if (smp->remote_key_dist & SMP_DIST_SIGN)
2545 SMP_ALLOW_CMD(smp, SMP_CMD_SIGN_INFO);
2546
Johan Hedbergfd349c02014-02-18 10:19:36 +02002547 skb_pull(skb, sizeof(*info));
2548
Johan Hedberga9a58f82014-02-25 22:24:37 +02002549 /* Strictly speaking the Core Specification (4.1) allows sending
2550 * an empty address which would force us to rely on just the IRK
2551 * as "identity information". However, since such
2552 * implementations are not known of and in order to not over
2553 * complicate our implementation, simply pretend that we never
2554 * received an IRK for such a device.
Johan Hedberge12af482015-01-14 20:51:37 +02002555 *
2556 * The Identity Address must also be a Static Random or Public
2557 * Address, which hci_is_identity_address() checks for.
Johan Hedberga9a58f82014-02-25 22:24:37 +02002558 */
Johan Hedberge12af482015-01-14 20:51:37 +02002559 if (!bacmp(&info->bdaddr, BDADDR_ANY) ||
2560 !hci_is_identity_address(&info->bdaddr, info->addr_type)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002561 bt_dev_err(hcon->hdev, "ignoring IRK with no identity address");
Johan Hedberg31dd6242014-06-27 14:23:02 +03002562 goto distribute;
Johan Hedberga9a58f82014-02-25 22:24:37 +02002563 }
2564
Szymon Janc1d87b882019-06-19 00:47:47 +02002565 /* Drop IRK if peer is using identity address during pairing but is
2566 * providing different address as identity information.
2567 *
2568 * Microsoft Surface Precision Mouse is known to have this bug.
2569 */
2570 if (hci_is_identity_address(&hcon->dst, hcon->dst_type) &&
2571 (bacmp(&info->bdaddr, &hcon->dst) ||
2572 info->addr_type != hcon->dst_type)) {
2573 bt_dev_err(hcon->hdev,
2574 "ignoring IRK with invalid identity address");
2575 goto distribute;
2576 }
2577
Johan Hedbergfd349c02014-02-18 10:19:36 +02002578 bacpy(&smp->id_addr, &info->bdaddr);
2579 smp->id_addr_type = info->addr_type;
2580
2581 if (hci_bdaddr_is_rpa(&hcon->dst, hcon->dst_type))
2582 bacpy(&rpa, &hcon->dst);
2583 else
2584 bacpy(&rpa, BDADDR_ANY);
2585
Johan Hedberg23d0e122014-02-19 14:57:46 +02002586 smp->remote_irk = hci_add_irk(conn->hcon->hdev, &smp->id_addr,
2587 smp->id_addr_type, smp->irk, &rpa);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002588
Johan Hedberg31dd6242014-06-27 14:23:02 +03002589distribute:
Johan Hedbergc6e81e92014-09-05 22:19:54 +03002590 if (!(smp->remote_key_dist & KEY_DIST_MASK))
2591 smp_distribute_keys(smp);
Johan Hedbergfd349c02014-02-18 10:19:36 +02002592
2593 return 0;
2594}
2595
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002596static int smp_cmd_sign_info(struct l2cap_conn *conn, struct sk_buff *skb)
2597{
2598 struct smp_cmd_sign_info *rp = (void *) skb->data;
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002599 struct l2cap_chan *chan = conn->smp;
2600 struct smp_chan *smp = chan->data;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002601 struct smp_csrk *csrk;
2602
2603 BT_DBG("conn %p", conn);
2604
2605 if (skb->len < sizeof(*rp))
Johan Hedberg38e4a912014-05-08 14:19:11 +03002606 return SMP_INVALID_PARAMS;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002607
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002608 /* Mark the information as received */
2609 smp->remote_key_dist &= ~SMP_DIST_SIGN;
2610
2611 skb_pull(skb, sizeof(*rp));
2612
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002613 csrk = kzalloc(sizeof(*csrk), GFP_KERNEL);
2614 if (csrk) {
Johan Hedberg4cd39282015-02-27 10:11:13 +02002615 if (conn->hcon->sec_level > BT_SECURITY_MEDIUM)
2616 csrk->type = MGMT_CSRK_REMOTE_AUTHENTICATED;
2617 else
2618 csrk->type = MGMT_CSRK_REMOTE_UNAUTHENTICATED;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002619 memcpy(csrk->val, rp->csrk, sizeof(csrk->val));
2620 }
2621 smp->csrk = csrk;
Johan Hedbergd6268e82014-09-05 22:19:51 +03002622 smp_distribute_keys(smp);
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002623
2624 return 0;
2625}
2626
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002627static u8 sc_select_method(struct smp_chan *smp)
2628{
2629 struct l2cap_conn *conn = smp->conn;
2630 struct hci_conn *hcon = conn->hcon;
2631 struct smp_cmd_pairing *local, *remote;
2632 u8 local_mitm, remote_mitm, local_io, remote_io, method;
2633
Johan Hedberg1a8bab42015-03-16 11:45:44 +02002634 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags) ||
2635 test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags))
Johan Hedberga29b0732014-10-28 15:17:05 +01002636 return REQ_OOB;
2637
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002638 /* The preq/prsp contain the raw Pairing Request/Response PDUs
2639 * which are needed as inputs to some crypto functions. To get
2640 * the "struct smp_cmd_pairing" from them we need to skip the
2641 * first byte which contains the opcode.
2642 */
2643 if (hcon->out) {
2644 local = (void *) &smp->preq[1];
2645 remote = (void *) &smp->prsp[1];
2646 } else {
2647 local = (void *) &smp->prsp[1];
2648 remote = (void *) &smp->preq[1];
2649 }
2650
2651 local_io = local->io_capability;
2652 remote_io = remote->io_capability;
2653
2654 local_mitm = (local->auth_req & SMP_AUTH_MITM);
2655 remote_mitm = (remote->auth_req & SMP_AUTH_MITM);
2656
2657 /* If either side wants MITM, look up the method from the table,
2658 * otherwise use JUST WORKS.
2659 */
2660 if (local_mitm || remote_mitm)
2661 method = get_auth_method(smp, local_io, remote_io);
2662 else
2663 method = JUST_WORKS;
2664
2665 /* Don't confirm locally initiated pairing attempts */
2666 if (method == JUST_CFM && test_bit(SMP_FLAG_INITIATOR, &smp->flags))
2667 method = JUST_WORKS;
2668
2669 return method;
2670}
2671
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002672static int smp_cmd_public_key(struct l2cap_conn *conn, struct sk_buff *skb)
2673{
2674 struct smp_cmd_public_key *key = (void *) skb->data;
2675 struct hci_conn *hcon = conn->hcon;
2676 struct l2cap_chan *chan = conn->smp;
2677 struct smp_chan *smp = chan->data;
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002678 struct hci_dev *hdev = hcon->hdev;
Tudor Ambarusc0153b02017-09-28 17:14:55 +03002679 struct crypto_kpp *tfm_ecdh;
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002680 struct smp_cmd_pairing_confirm cfm;
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002681 int err;
2682
2683 BT_DBG("conn %p", conn);
2684
2685 if (skb->len < sizeof(*key))
2686 return SMP_INVALID_PARAMS;
2687
2688 memcpy(smp->remote_pk, key, 64);
2689
Johan Hedberga8ca6172015-03-16 18:12:57 +02002690 if (test_bit(SMP_FLAG_REMOTE_OOB, &smp->flags)) {
2691 err = smp_f4(smp->tfm_cmac, smp->remote_pk, smp->remote_pk,
2692 smp->rr, 0, cfm.confirm_val);
2693 if (err)
2694 return SMP_UNSPECIFIED;
2695
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02002696 if (crypto_memneq(cfm.confirm_val, smp->pcnf, 16))
Johan Hedberga8ca6172015-03-16 18:12:57 +02002697 return SMP_CONFIRM_FAILED;
2698 }
2699
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002700 /* Non-initiating device sends its public key after receiving
2701 * the key from the initiating device.
2702 */
2703 if (!hcon->out) {
2704 err = sc_send_public_key(smp);
2705 if (err)
2706 return err;
2707 }
2708
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002709 SMP_DBG("Remote Public Key X: %32phN", smp->remote_pk);
Marcel Holtmanne0915262015-03-16 12:34:55 -07002710 SMP_DBG("Remote Public Key Y: %32phN", smp->remote_pk + 32);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002711
Tudor Ambarusc0153b02017-09-28 17:14:55 +03002712 /* Compute the shared secret on the same crypto tfm on which the private
2713 * key was set/generated.
2714 */
2715 if (test_bit(SMP_FLAG_LOCAL_OOB, &smp->flags)) {
Matias Karhumaa4ba51752018-09-11 14:10:13 +03002716 struct l2cap_chan *hchan = hdev->smp_data;
2717 struct smp_dev *smp_dev;
2718
2719 if (!hchan || !hchan->data)
2720 return SMP_UNSPECIFIED;
2721
2722 smp_dev = hchan->data;
Tudor Ambarusc0153b02017-09-28 17:14:55 +03002723
2724 tfm_ecdh = smp_dev->tfm_ecdh;
2725 } else {
2726 tfm_ecdh = smp->tfm_ecdh;
2727 }
2728
2729 if (compute_ecdh_secret(tfm_ecdh, smp->remote_pk, smp->dhkey))
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002730 return SMP_UNSPECIFIED;
2731
Johan Hedbergc7a3d572014-12-01 22:03:16 +02002732 SMP_DBG("DHKey %32phN", smp->dhkey);
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002733
2734 set_bit(SMP_FLAG_REMOTE_PK, &smp->flags);
2735
Johan Hedberg5e3d3d92014-05-31 18:51:02 +03002736 smp->method = sc_select_method(smp);
2737
2738 BT_DBG("%s selected method 0x%02x", hdev->name, smp->method);
2739
2740 /* JUST_WORKS and JUST_CFM result in an unauthenticated key */
2741 if (smp->method == JUST_WORKS || smp->method == JUST_CFM)
2742 hcon->pending_sec_level = BT_SECURITY_MEDIUM;
2743 else
2744 hcon->pending_sec_level = BT_SECURITY_FIPS;
2745
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02002746 if (!crypto_memneq(debug_pk, smp->remote_pk, 64))
Johan Hedbergaeb7d462014-05-31 18:52:28 +03002747 set_bit(SMP_FLAG_DEBUG_KEY, &smp->flags);
2748
Johan Hedberg38606f12014-06-25 11:10:28 +03002749 if (smp->method == DSP_PASSKEY) {
2750 get_random_bytes(&hcon->passkey_notify,
2751 sizeof(hcon->passkey_notify));
2752 hcon->passkey_notify %= 1000000;
2753 hcon->passkey_entered = 0;
2754 smp->passkey_round = 0;
2755 if (mgmt_user_passkey_notify(hdev, &hcon->dst, hcon->type,
2756 hcon->dst_type,
2757 hcon->passkey_notify,
2758 hcon->passkey_entered))
2759 return SMP_UNSPECIFIED;
2760 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2761 return sc_passkey_round(smp, SMP_CMD_PUBLIC_KEY);
2762 }
2763
Johan Hedberg94ea7252015-03-16 11:45:46 +02002764 if (smp->method == REQ_OOB) {
Johan Hedberga29b0732014-10-28 15:17:05 +01002765 if (hcon->out)
2766 smp_send_cmd(conn, SMP_CMD_PAIRING_RANDOM,
2767 sizeof(smp->prnd), smp->prnd);
2768
2769 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2770
2771 return 0;
2772 }
2773
Johan Hedberg38606f12014-06-25 11:10:28 +03002774 if (hcon->out)
2775 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2776
2777 if (smp->method == REQ_PASSKEY) {
2778 if (mgmt_user_passkey_request(hdev, &hcon->dst, hcon->type,
2779 hcon->dst_type))
2780 return SMP_UNSPECIFIED;
2781 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_CONFIRM);
2782 set_bit(SMP_FLAG_WAIT_USER, &smp->flags);
2783 return 0;
2784 }
2785
Johan Hedbergcbbbe3e2014-06-06 11:30:08 +03002786 /* The Initiating device waits for the non-initiating device to
2787 * send the confirm value.
2788 */
2789 if (conn->hcon->out)
2790 return 0;
2791
2792 err = smp_f4(smp->tfm_cmac, smp->local_pk, smp->remote_pk, smp->prnd,
2793 0, cfm.confirm_val);
2794 if (err)
2795 return SMP_UNSPECIFIED;
2796
2797 smp_send_cmd(conn, SMP_CMD_PAIRING_CONFIRM, sizeof(cfm), &cfm);
2798 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RANDOM);
2799
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002800 return 0;
2801}
2802
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002803static int smp_cmd_dhkey_check(struct l2cap_conn *conn, struct sk_buff *skb)
2804{
2805 struct smp_cmd_dhkey_check *check = (void *) skb->data;
2806 struct l2cap_chan *chan = conn->smp;
2807 struct hci_conn *hcon = conn->hcon;
2808 struct smp_chan *smp = chan->data;
2809 u8 a[7], b[7], *local_addr, *remote_addr;
2810 u8 io_cap[3], r[16], e[16];
2811 int err;
2812
2813 BT_DBG("conn %p", conn);
2814
2815 if (skb->len < sizeof(*check))
2816 return SMP_INVALID_PARAMS;
2817
2818 memcpy(a, &hcon->init_addr, 6);
2819 memcpy(b, &hcon->resp_addr, 6);
2820 a[6] = hcon->init_addr_type;
2821 b[6] = hcon->resp_addr_type;
2822
2823 if (hcon->out) {
2824 local_addr = a;
2825 remote_addr = b;
2826 memcpy(io_cap, &smp->prsp[1], 3);
2827 } else {
2828 local_addr = b;
2829 remote_addr = a;
2830 memcpy(io_cap, &smp->preq[1], 3);
2831 }
2832
2833 memset(r, 0, sizeof(r));
2834
Johan Hedberg38606f12014-06-25 11:10:28 +03002835 if (smp->method == REQ_PASSKEY || smp->method == DSP_PASSKEY)
2836 put_unaligned_le32(hcon->passkey_notify, r);
Johan Hedberg882fafa2015-03-16 11:45:43 +02002837 else if (smp->method == REQ_OOB)
2838 memcpy(r, smp->lr, 16);
Johan Hedberg38606f12014-06-25 11:10:28 +03002839
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002840 err = smp_f6(smp->tfm_cmac, smp->mackey, smp->rrnd, smp->prnd, r,
2841 io_cap, remote_addr, local_addr, e);
2842 if (err)
2843 return SMP_UNSPECIFIED;
2844
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02002845 if (crypto_memneq(check->e, e, 16))
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002846 return SMP_DHKEY_CHECK_FAILED;
2847
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002848 if (!hcon->out) {
2849 if (test_bit(SMP_FLAG_WAIT_USER, &smp->flags)) {
2850 set_bit(SMP_FLAG_DHKEY_PENDING, &smp->flags);
2851 return 0;
2852 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002853
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002854 /* Slave sends DHKey check as response to master */
2855 sc_dhkey_check(smp);
2856 }
Johan Hedbergd378a2d2014-05-31 18:53:36 +03002857
Johan Hedbergd3e54a82014-06-04 11:07:40 +03002858 sc_add_ltk(smp);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002859
2860 if (hcon->out) {
Johan Hedberg8b76ce32015-06-08 18:14:39 +03002861 hci_le_start_enc(hcon, 0, 0, smp->tk, smp->enc_key_size);
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002862 hcon->enc_key_size = smp->enc_key_size;
2863 }
2864
2865 return 0;
2866}
2867
Johan Hedberg1408bb62014-06-04 22:45:57 +03002868static int smp_cmd_keypress_notify(struct l2cap_conn *conn,
2869 struct sk_buff *skb)
2870{
2871 struct smp_cmd_keypress_notify *kp = (void *) skb->data;
2872
2873 BT_DBG("value 0x%02x", kp->value);
2874
2875 return 0;
2876}
2877
Johan Hedberg4befb862014-08-11 22:06:38 +03002878static int smp_sig_channel(struct l2cap_chan *chan, struct sk_buff *skb)
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002879{
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002880 struct l2cap_conn *conn = chan->conn;
Marcel Holtmann7b9899d2013-10-03 00:00:57 -07002881 struct hci_conn *hcon = conn->hcon;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002882 struct smp_chan *smp;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002883 __u8 code, reason;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002884 int err = 0;
2885
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002886 if (skb->len < 1)
Marcel Holtmann92381f52013-10-03 01:23:08 -07002887 return -EILSEQ;
Marcel Holtmann92381f52013-10-03 01:23:08 -07002888
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002889 if (!hci_dev_test_flag(hcon->hdev, HCI_LE_ENABLED)) {
Andre Guedes2e65c9d2011-06-30 19:20:56 -03002890 reason = SMP_PAIRING_NOTSUPP;
2891 goto done;
2892 }
2893
Marcel Holtmann92381f52013-10-03 01:23:08 -07002894 code = skb->data[0];
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002895 skb_pull(skb, sizeof(code));
2896
Johan Hedbergb28b4942014-09-05 22:19:55 +03002897 smp = chan->data;
2898
2899 if (code > SMP_CMD_MAX)
2900 goto drop;
2901
Johan Hedberg24bd0bd2014-09-10 17:37:43 -07002902 if (smp && !test_and_clear_bit(code, &smp->allow_cmd))
Johan Hedbergb28b4942014-09-05 22:19:55 +03002903 goto drop;
2904
2905 /* If we don't have a context the only allowed commands are
2906 * pairing request and security request.
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002907 */
Johan Hedbergb28b4942014-09-05 22:19:55 +03002908 if (!smp && code != SMP_CMD_PAIRING_REQ && code != SMP_CMD_SECURITY_REQ)
2909 goto drop;
Johan Hedberg8cf9fa12013-01-29 10:44:23 -06002910
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002911 switch (code) {
2912 case SMP_CMD_PAIRING_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002913 reason = smp_cmd_pairing_req(conn, skb);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002914 break;
2915
2916 case SMP_CMD_PAIRING_FAIL:
Johan Hedberg84794e12013-11-06 11:24:57 +02002917 smp_failure(conn, 0);
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002918 err = -EPERM;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002919 break;
2920
2921 case SMP_CMD_PAIRING_RSP:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002922 reason = smp_cmd_pairing_rsp(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002923 break;
2924
2925 case SMP_CMD_SECURITY_REQ:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002926 reason = smp_cmd_security_req(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002927 break;
2928
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002929 case SMP_CMD_PAIRING_CONFIRM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002930 reason = smp_cmd_pairing_confirm(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002931 break;
2932
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002933 case SMP_CMD_PAIRING_RANDOM:
Vinicius Costa Gomesda85e5e2011-06-09 18:50:53 -03002934 reason = smp_cmd_pairing_random(conn, skb);
Anderson Briglia88ba43b2011-06-09 18:50:42 -03002935 break;
2936
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002937 case SMP_CMD_ENCRYPT_INFO:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002938 reason = smp_cmd_encrypt_info(conn, skb);
2939 break;
2940
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002941 case SMP_CMD_MASTER_IDENT:
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002942 reason = smp_cmd_master_ident(conn, skb);
2943 break;
2944
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002945 case SMP_CMD_IDENT_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002946 reason = smp_cmd_ident_info(conn, skb);
2947 break;
2948
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002949 case SMP_CMD_IDENT_ADDR_INFO:
Johan Hedbergfd349c02014-02-18 10:19:36 +02002950 reason = smp_cmd_ident_addr_info(conn, skb);
2951 break;
2952
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002953 case SMP_CMD_SIGN_INFO:
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07002954 reason = smp_cmd_sign_info(conn, skb);
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002955 break;
2956
Johan Hedbergd8f8edb2014-06-06 11:09:28 +03002957 case SMP_CMD_PUBLIC_KEY:
2958 reason = smp_cmd_public_key(conn, skb);
2959 break;
2960
Johan Hedberg6433a9a2014-06-06 11:47:30 +03002961 case SMP_CMD_DHKEY_CHECK:
2962 reason = smp_cmd_dhkey_check(conn, skb);
2963 break;
2964
Johan Hedberg1408bb62014-06-04 22:45:57 +03002965 case SMP_CMD_KEYPRESS_NOTIFY:
2966 reason = smp_cmd_keypress_notify(conn, skb);
2967 break;
2968
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002969 default:
2970 BT_DBG("Unknown command code 0x%2.2x", code);
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002971 reason = SMP_CMD_NOTSUPP;
Vinicius Costa Gomes3a0259b2011-06-09 18:50:43 -03002972 goto done;
2973 }
2974
2975done:
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002976 if (!err) {
2977 if (reason)
2978 smp_failure(conn, reason);
Johan Hedberg8ae9b982014-08-11 22:06:39 +03002979 kfree_skb(skb);
Johan Hedberg9b7b18e2014-08-18 20:33:31 +03002980 }
2981
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002982 return err;
Johan Hedbergb28b4942014-09-05 22:19:55 +03002983
2984drop:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002985 bt_dev_err(hcon->hdev, "unexpected SMP command 0x%02x from %pMR",
2986 code, &hcon->dst);
Johan Hedbergb28b4942014-09-05 22:19:55 +03002987 kfree_skb(skb);
2988 return 0;
Anderson Brigliaeb492e02011-06-09 18:50:40 -03002989}
Vinicius Costa Gomes7034b912011-07-07 18:59:34 -03002990
Johan Hedberg70db83c2014-08-08 09:37:16 +03002991static void smp_teardown_cb(struct l2cap_chan *chan, int err)
2992{
2993 struct l2cap_conn *conn = chan->conn;
2994
2995 BT_DBG("chan %p", chan);
2996
Johan Hedbergfc75cc82014-09-05 22:19:52 +03002997 if (chan->data)
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002998 smp_chan_destroy(conn);
Johan Hedberg5d88cc72014-08-08 09:37:18 +03002999
Johan Hedberg70db83c2014-08-08 09:37:16 +03003000 conn->smp = NULL;
3001 l2cap_chan_put(chan);
3002}
3003
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003004static void bredr_pairing(struct l2cap_chan *chan)
3005{
3006 struct l2cap_conn *conn = chan->conn;
3007 struct hci_conn *hcon = conn->hcon;
3008 struct hci_dev *hdev = hcon->hdev;
3009 struct smp_cmd_pairing req;
3010 struct smp_chan *smp;
3011
3012 BT_DBG("chan %p", chan);
3013
3014 /* Only new pairings are interesting */
3015 if (!test_bit(HCI_CONN_NEW_LINK_KEY, &hcon->flags))
3016 return;
3017
3018 /* Don't bother if we're not encrypted */
3019 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
3020 return;
3021
3022 /* Only master may initiate SMP over BR/EDR */
3023 if (hcon->role != HCI_ROLE_MASTER)
3024 return;
3025
3026 /* Secure Connections support must be enabled */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003027 if (!hci_dev_test_flag(hdev, HCI_SC_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003028 return;
3029
3030 /* BR/EDR must use Secure Connections for SMP */
3031 if (!test_bit(HCI_CONN_AES_CCM, &hcon->flags) &&
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003032 !hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003033 return;
3034
3035 /* If our LE support is not enabled don't do anything */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003036 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003037 return;
3038
3039 /* Don't bother if remote LE support is not enabled */
3040 if (!lmp_host_le_capable(hcon))
3041 return;
3042
3043 /* Remote must support SMP fixed chan for BR/EDR */
3044 if (!(conn->remote_fixed_chan & L2CAP_FC_SMP_BREDR))
3045 return;
3046
3047 /* Don't bother if SMP is already ongoing */
3048 if (chan->data)
3049 return;
3050
3051 smp = smp_chan_create(conn);
3052 if (!smp) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003053 bt_dev_err(hdev, "unable to create SMP context for BR/EDR");
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003054 return;
3055 }
3056
3057 set_bit(SMP_FLAG_SC, &smp->flags);
3058
3059 BT_DBG("%s starting SMP over BR/EDR", hdev->name);
3060
3061 /* Prepare and send the BR/EDR SMP Pairing Request */
3062 build_bredr_pairing_cmd(smp, &req, NULL);
3063
3064 smp->preq[0] = SMP_CMD_PAIRING_REQ;
3065 memcpy(&smp->preq[1], &req, sizeof(req));
3066
3067 smp_send_cmd(conn, SMP_CMD_PAIRING_REQ, sizeof(req), &req);
3068 SMP_ALLOW_CMD(smp, SMP_CMD_PAIRING_RSP);
3069}
3070
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003071static void smp_resume_cb(struct l2cap_chan *chan)
3072{
Johan Hedbergb68fda62014-08-11 22:06:40 +03003073 struct smp_chan *smp = chan->data;
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003074 struct l2cap_conn *conn = chan->conn;
3075 struct hci_conn *hcon = conn->hcon;
3076
3077 BT_DBG("chan %p", chan);
3078
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003079 if (hcon->type == ACL_LINK) {
3080 bredr_pairing(chan);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003081 return;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003082 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003083
Johan Hedberg86d14072014-08-11 22:06:43 +03003084 if (!smp)
3085 return;
Johan Hedbergb68fda62014-08-11 22:06:40 +03003086
Johan Hedberg84bc0db2014-09-05 22:19:49 +03003087 if (!test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
3088 return;
3089
Johan Hedberg86d14072014-08-11 22:06:43 +03003090 cancel_delayed_work(&smp->security_timer);
3091
Johan Hedbergd6268e82014-09-05 22:19:51 +03003092 smp_distribute_keys(smp);
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003093}
3094
Johan Hedberg70db83c2014-08-08 09:37:16 +03003095static void smp_ready_cb(struct l2cap_chan *chan)
3096{
3097 struct l2cap_conn *conn = chan->conn;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003098 struct hci_conn *hcon = conn->hcon;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003099
3100 BT_DBG("chan %p", chan);
3101
Johan Hedberg78837462015-11-11 21:47:12 +02003102 /* No need to call l2cap_chan_hold() here since we already own
3103 * the reference taken in smp_new_conn_cb(). This is just the
3104 * first time that we tie it to a specific pointer. The code in
3105 * l2cap_core.c ensures that there's no risk this function wont
3106 * get called if smp_new_conn_cb was previously called.
3107 */
Johan Hedberg70db83c2014-08-08 09:37:16 +03003108 conn->smp = chan;
Johan Hedbergb5ae3442014-08-14 12:34:26 +03003109
3110 if (hcon->type == ACL_LINK && test_bit(HCI_CONN_ENCRYPT, &hcon->flags))
3111 bredr_pairing(chan);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003112}
3113
Johan Hedberg4befb862014-08-11 22:06:38 +03003114static int smp_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
3115{
3116 int err;
3117
3118 BT_DBG("chan %p", chan);
3119
3120 err = smp_sig_channel(chan, skb);
3121 if (err) {
Johan Hedbergb68fda62014-08-11 22:06:40 +03003122 struct smp_chan *smp = chan->data;
Johan Hedberg4befb862014-08-11 22:06:38 +03003123
Johan Hedbergb68fda62014-08-11 22:06:40 +03003124 if (smp)
3125 cancel_delayed_work_sync(&smp->security_timer);
Johan Hedberg4befb862014-08-11 22:06:38 +03003126
Johan Hedberg1e91c292014-08-18 20:33:29 +03003127 hci_disconnect(chan->conn->hcon, HCI_ERROR_AUTH_FAILURE);
Johan Hedberg4befb862014-08-11 22:06:38 +03003128 }
3129
3130 return err;
3131}
3132
Johan Hedberg70db83c2014-08-08 09:37:16 +03003133static struct sk_buff *smp_alloc_skb_cb(struct l2cap_chan *chan,
3134 unsigned long hdr_len,
3135 unsigned long len, int nb)
3136{
3137 struct sk_buff *skb;
3138
3139 skb = bt_skb_alloc(hdr_len + len, GFP_KERNEL);
3140 if (!skb)
3141 return ERR_PTR(-ENOMEM);
3142
3143 skb->priority = HCI_PRIO_MAX;
Johan Hedberga4368ff2015-03-30 23:21:01 +03003144 bt_cb(skb)->l2cap.chan = chan;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003145
3146 return skb;
3147}
3148
3149static const struct l2cap_ops smp_chan_ops = {
3150 .name = "Security Manager",
3151 .ready = smp_ready_cb,
Johan Hedberg5d88cc72014-08-08 09:37:18 +03003152 .recv = smp_recv_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003153 .alloc_skb = smp_alloc_skb_cb,
3154 .teardown = smp_teardown_cb,
Johan Hedberg44f1a7a2014-08-11 22:06:36 +03003155 .resume = smp_resume_cb,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003156
3157 .new_connection = l2cap_chan_no_new_connection,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003158 .state_change = l2cap_chan_no_state_change,
3159 .close = l2cap_chan_no_close,
3160 .defer = l2cap_chan_no_defer,
3161 .suspend = l2cap_chan_no_suspend,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003162 .set_shutdown = l2cap_chan_no_set_shutdown,
3163 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003164};
3165
3166static inline struct l2cap_chan *smp_new_conn_cb(struct l2cap_chan *pchan)
3167{
3168 struct l2cap_chan *chan;
3169
3170 BT_DBG("pchan %p", pchan);
3171
3172 chan = l2cap_chan_create();
3173 if (!chan)
3174 return NULL;
3175
3176 chan->chan_type = pchan->chan_type;
3177 chan->ops = &smp_chan_ops;
3178 chan->scid = pchan->scid;
3179 chan->dcid = chan->scid;
3180 chan->imtu = pchan->imtu;
3181 chan->omtu = pchan->omtu;
3182 chan->mode = pchan->mode;
3183
Johan Hedbergabe84902014-11-12 22:22:21 +02003184 /* Other L2CAP channels may request SMP routines in order to
3185 * change the security level. This means that the SMP channel
3186 * lock must be considered in its own category to avoid lockdep
3187 * warnings.
3188 */
3189 atomic_set(&chan->nesting, L2CAP_NESTING_SMP);
3190
Johan Hedberg70db83c2014-08-08 09:37:16 +03003191 BT_DBG("created chan %p", chan);
3192
3193 return chan;
3194}
3195
3196static const struct l2cap_ops smp_root_chan_ops = {
3197 .name = "Security Manager Root",
3198 .new_connection = smp_new_conn_cb,
3199
3200 /* None of these are implemented for the root channel */
3201 .close = l2cap_chan_no_close,
3202 .alloc_skb = l2cap_chan_no_alloc_skb,
3203 .recv = l2cap_chan_no_recv,
3204 .state_change = l2cap_chan_no_state_change,
3205 .teardown = l2cap_chan_no_teardown,
3206 .ready = l2cap_chan_no_ready,
3207 .defer = l2cap_chan_no_defer,
3208 .suspend = l2cap_chan_no_suspend,
3209 .resume = l2cap_chan_no_resume,
3210 .set_shutdown = l2cap_chan_no_set_shutdown,
3211 .get_sndtimeo = l2cap_chan_no_get_sndtimeo,
Johan Hedberg70db83c2014-08-08 09:37:16 +03003212};
3213
Johan Hedbergef8efe42014-08-13 15:12:32 +03003214static struct l2cap_chan *smp_add_cid(struct hci_dev *hdev, u16 cid)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003215{
Johan Hedberg70db83c2014-08-08 09:37:16 +03003216 struct l2cap_chan *chan;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003217 struct smp_dev *smp;
Herbert Xu71af2f62016-01-24 21:18:30 +08003218 struct crypto_shash *tfm_cmac;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003219 struct crypto_kpp *tfm_ecdh;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003220
Johan Hedbergef8efe42014-08-13 15:12:32 +03003221 if (cid == L2CAP_CID_SMP_BREDR) {
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003222 smp = NULL;
Johan Hedbergef8efe42014-08-13 15:12:32 +03003223 goto create_chan;
3224 }
Johan Hedberg711eafe2014-08-08 09:32:52 +03003225
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003226 smp = kzalloc(sizeof(*smp), GFP_KERNEL);
3227 if (!smp)
3228 return ERR_PTR(-ENOMEM);
3229
Herbert Xu71af2f62016-01-24 21:18:30 +08003230 tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0);
Marcel Holtmann6e2dc6d12015-03-16 01:10:21 -07003231 if (IS_ERR(tfm_cmac)) {
3232 BT_ERR("Unable to create CMAC crypto context");
Marcel Holtmann6e2dc6d12015-03-16 01:10:21 -07003233 kzfree(smp);
3234 return ERR_CAST(tfm_cmac);
3235 }
3236
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003237 tfm_ecdh = crypto_alloc_kpp("ecdh", CRYPTO_ALG_INTERNAL, 0);
3238 if (IS_ERR(tfm_ecdh)) {
3239 BT_ERR("Unable to create ECDH crypto context");
3240 crypto_free_shash(tfm_cmac);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003241 kzfree(smp);
3242 return ERR_CAST(tfm_ecdh);
3243 }
3244
Johan Hedberg94f14e42018-09-11 14:10:12 +03003245 smp->local_oob = false;
Marcel Holtmann6e2dc6d12015-03-16 01:10:21 -07003246 smp->tfm_cmac = tfm_cmac;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003247 smp->tfm_ecdh = tfm_ecdh;
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003248
Johan Hedbergef8efe42014-08-13 15:12:32 +03003249create_chan:
Johan Hedberg70db83c2014-08-08 09:37:16 +03003250 chan = l2cap_chan_create();
3251 if (!chan) {
Marcel Holtmann63511f6d2015-03-17 11:38:24 -07003252 if (smp) {
Herbert Xu71af2f62016-01-24 21:18:30 +08003253 crypto_free_shash(smp->tfm_cmac);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003254 crypto_free_kpp(smp->tfm_ecdh);
Marcel Holtmann63511f6d2015-03-17 11:38:24 -07003255 kzfree(smp);
3256 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003257 return ERR_PTR(-ENOMEM);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003258 }
3259
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003260 chan->data = smp;
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003261
Johan Hedbergef8efe42014-08-13 15:12:32 +03003262 l2cap_add_scid(chan, cid);
Johan Hedberg70db83c2014-08-08 09:37:16 +03003263
3264 l2cap_chan_set_defaults(chan);
3265
Marcel Holtmann157029b2015-01-14 15:43:09 -08003266 if (cid == L2CAP_CID_SMP) {
Johan Hedberg39e3e742015-02-20 13:48:24 +02003267 u8 bdaddr_type;
3268
3269 hci_copy_identity_address(hdev, &chan->src, &bdaddr_type);
3270
3271 if (bdaddr_type == ADDR_LE_DEV_PUBLIC)
Marcel Holtmann157029b2015-01-14 15:43:09 -08003272 chan->src_type = BDADDR_LE_PUBLIC;
Johan Hedberg39e3e742015-02-20 13:48:24 +02003273 else
3274 chan->src_type = BDADDR_LE_RANDOM;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003275 } else {
3276 bacpy(&chan->src, &hdev->bdaddr);
Johan Hedbergef8efe42014-08-13 15:12:32 +03003277 chan->src_type = BDADDR_BREDR;
Marcel Holtmann157029b2015-01-14 15:43:09 -08003278 }
3279
Johan Hedberg70db83c2014-08-08 09:37:16 +03003280 chan->state = BT_LISTEN;
3281 chan->mode = L2CAP_MODE_BASIC;
3282 chan->imtu = L2CAP_DEFAULT_MTU;
3283 chan->ops = &smp_root_chan_ops;
3284
Johan Hedbergabe84902014-11-12 22:22:21 +02003285 /* Set correct nesting level for a parent/listening channel */
3286 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
3287
Johan Hedbergef8efe42014-08-13 15:12:32 +03003288 return chan;
Johan Hedberg711eafe2014-08-08 09:32:52 +03003289}
3290
Johan Hedbergef8efe42014-08-13 15:12:32 +03003291static void smp_del_chan(struct l2cap_chan *chan)
Johan Hedberg711eafe2014-08-08 09:32:52 +03003292{
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003293 struct smp_dev *smp;
Johan Hedberg70db83c2014-08-08 09:37:16 +03003294
Johan Hedbergef8efe42014-08-13 15:12:32 +03003295 BT_DBG("chan %p", chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003296
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003297 smp = chan->data;
3298 if (smp) {
Johan Hedbergdefce9e2014-08-08 09:37:17 +03003299 chan->data = NULL;
Herbert Xu71af2f62016-01-24 21:18:30 +08003300 crypto_free_shash(smp->tfm_cmac);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003301 crypto_free_kpp(smp->tfm_ecdh);
Marcel Holtmann88a479d2015-03-16 01:10:19 -07003302 kzfree(smp);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003303 }
Johan Hedberg70db83c2014-08-08 09:37:16 +03003304
Johan Hedberg70db83c2014-08-08 09:37:16 +03003305 l2cap_chan_put(chan);
Johan Hedberg711eafe2014-08-08 09:32:52 +03003306}
Johan Hedbergef8efe42014-08-13 15:12:32 +03003307
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003308static ssize_t force_bredr_smp_read(struct file *file,
3309 char __user *user_buf,
3310 size_t count, loff_t *ppos)
3311{
3312 struct hci_dev *hdev = file->private_data;
3313 char buf[3];
3314
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003315 buf[0] = hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP) ? 'Y': 'N';
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003316 buf[1] = '\n';
3317 buf[2] = '\0';
3318 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3319}
3320
3321static ssize_t force_bredr_smp_write(struct file *file,
3322 const char __user *user_buf,
3323 size_t count, loff_t *ppos)
3324{
3325 struct hci_dev *hdev = file->private_data;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003326 bool enable;
Andy Shevchenko3bf5e972018-05-29 16:33:48 +03003327 int err;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003328
Andy Shevchenko3bf5e972018-05-29 16:33:48 +03003329 err = kstrtobool_from_user(user_buf, count, &enable);
3330 if (err)
3331 return err;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003332
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003333 if (enable == hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003334 return -EALREADY;
3335
3336 if (enable) {
3337 struct l2cap_chan *chan;
3338
3339 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3340 if (IS_ERR(chan))
3341 return PTR_ERR(chan);
3342
3343 hdev->smp_bredr_data = chan;
3344 } else {
3345 struct l2cap_chan *chan;
3346
3347 chan = hdev->smp_bredr_data;
3348 hdev->smp_bredr_data = NULL;
3349 smp_del_chan(chan);
3350 }
3351
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -07003352 hci_dev_change_flag(hdev, HCI_FORCE_BREDR_SMP);
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003353
3354 return count;
3355}
3356
3357static const struct file_operations force_bredr_smp_fops = {
3358 .open = simple_open,
3359 .read = force_bredr_smp_read,
3360 .write = force_bredr_smp_write,
3361 .llseek = default_llseek,
3362};
3363
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003364static ssize_t le_min_key_size_read(struct file *file,
3365 char __user *user_buf,
3366 size_t count, loff_t *ppos)
3367{
3368 struct hci_dev *hdev = file->private_data;
3369 char buf[4];
3370
Matias Karhumaa30d65e02018-09-28 21:54:30 +03003371 snprintf(buf, sizeof(buf), "%2u\n", hdev->le_min_key_size);
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003372
3373 return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
3374}
3375
3376static ssize_t le_min_key_size_write(struct file *file,
3377 const char __user *user_buf,
3378 size_t count, loff_t *ppos)
3379{
3380 struct hci_dev *hdev = file->private_data;
3381 char buf[32];
3382 size_t buf_size = min(count, (sizeof(buf) - 1));
3383 u8 key_size;
3384
3385 if (copy_from_user(buf, user_buf, buf_size))
3386 return -EFAULT;
3387
3388 buf[buf_size] = '\0';
3389
3390 sscanf(buf, "%hhu", &key_size);
3391
Matias Karhumaa30d65e02018-09-28 21:54:30 +03003392 if (key_size > hdev->le_max_key_size ||
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003393 key_size < SMP_MIN_ENC_KEY_SIZE)
3394 return -EINVAL;
3395
Matias Karhumaa30d65e02018-09-28 21:54:30 +03003396 hdev->le_min_key_size = key_size;
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003397
3398 return count;
3399}
3400
3401static const struct file_operations le_min_key_size_fops = {
3402 .open = simple_open,
3403 .read = le_min_key_size_read,
3404 .write = le_min_key_size_write,
3405 .llseek = default_llseek,
3406};
3407
Johan Hedberg2fd36552015-06-11 13:52:26 +03003408static ssize_t le_max_key_size_read(struct file *file,
3409 char __user *user_buf,
3410 size_t count, loff_t *ppos)
3411{
3412 struct hci_dev *hdev = file->private_data;
3413 char buf[4];
3414
Matias Karhumaa30d65e02018-09-28 21:54:30 +03003415 snprintf(buf, sizeof(buf), "%2u\n", hdev->le_max_key_size);
Johan Hedberg2fd36552015-06-11 13:52:26 +03003416
3417 return simple_read_from_buffer(user_buf, count, ppos, buf, strlen(buf));
3418}
3419
3420static ssize_t le_max_key_size_write(struct file *file,
3421 const char __user *user_buf,
3422 size_t count, loff_t *ppos)
3423{
3424 struct hci_dev *hdev = file->private_data;
3425 char buf[32];
3426 size_t buf_size = min(count, (sizeof(buf) - 1));
3427 u8 key_size;
3428
3429 if (copy_from_user(buf, user_buf, buf_size))
3430 return -EFAULT;
3431
3432 buf[buf_size] = '\0';
3433
3434 sscanf(buf, "%hhu", &key_size);
3435
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003436 if (key_size > SMP_MAX_ENC_KEY_SIZE ||
Matias Karhumaa30d65e02018-09-28 21:54:30 +03003437 key_size < hdev->le_min_key_size)
Johan Hedberg2fd36552015-06-11 13:52:26 +03003438 return -EINVAL;
3439
Matias Karhumaa30d65e02018-09-28 21:54:30 +03003440 hdev->le_max_key_size = key_size;
Johan Hedberg2fd36552015-06-11 13:52:26 +03003441
3442 return count;
3443}
3444
3445static const struct file_operations le_max_key_size_fops = {
3446 .open = simple_open,
3447 .read = le_max_key_size_read,
3448 .write = le_max_key_size_write,
3449 .llseek = default_llseek,
3450};
3451
Johan Hedbergef8efe42014-08-13 15:12:32 +03003452int smp_register(struct hci_dev *hdev)
3453{
3454 struct l2cap_chan *chan;
3455
3456 BT_DBG("%s", hdev->name);
3457
Marcel Holtmann7e7ec442015-01-14 15:43:10 -08003458 /* If the controller does not support Low Energy operation, then
3459 * there is also no need to register any SMP channel.
3460 */
3461 if (!lmp_le_capable(hdev))
3462 return 0;
3463
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003464 if (WARN_ON(hdev->smp_data)) {
3465 chan = hdev->smp_data;
3466 hdev->smp_data = NULL;
3467 smp_del_chan(chan);
3468 }
3469
Johan Hedbergef8efe42014-08-13 15:12:32 +03003470 chan = smp_add_cid(hdev, L2CAP_CID_SMP);
3471 if (IS_ERR(chan))
3472 return PTR_ERR(chan);
3473
3474 hdev->smp_data = chan;
3475
Johan Hedbergb1f663c2015-06-11 13:52:27 +03003476 debugfs_create_file("le_min_key_size", 0644, hdev->debugfs, hdev,
3477 &le_min_key_size_fops);
Johan Hedberg2fd36552015-06-11 13:52:26 +03003478 debugfs_create_file("le_max_key_size", 0644, hdev->debugfs, hdev,
3479 &le_max_key_size_fops);
3480
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003481 /* If the controller does not support BR/EDR Secure Connections
3482 * feature, then the BR/EDR SMP channel shall not be present.
3483 *
3484 * To test this with Bluetooth 4.0 controllers, create a debugfs
3485 * switch that allows forcing BR/EDR SMP support and accepting
3486 * cross-transport pairing on non-AES encrypted connections.
3487 */
3488 if (!lmp_sc_capable(hdev)) {
3489 debugfs_create_file("force_bredr_smp", 0644, hdev->debugfs,
3490 hdev, &force_bredr_smp_fops);
Szymon Janc83ebb9e2016-09-09 20:24:40 +02003491
3492 /* Flag can be already set here (due to power toggle) */
3493 if (!hci_dev_test_flag(hdev, HCI_FORCE_BREDR_SMP))
3494 return 0;
Marcel Holtmann300acfde2014-12-31 14:43:16 -08003495 }
Johan Hedbergef8efe42014-08-13 15:12:32 +03003496
Marcel Holtmann2b8df322015-01-15 08:04:21 -08003497 if (WARN_ON(hdev->smp_bredr_data)) {
3498 chan = hdev->smp_bredr_data;
3499 hdev->smp_bredr_data = NULL;
3500 smp_del_chan(chan);
3501 }
3502
Johan Hedbergef8efe42014-08-13 15:12:32 +03003503 chan = smp_add_cid(hdev, L2CAP_CID_SMP_BREDR);
3504 if (IS_ERR(chan)) {
3505 int err = PTR_ERR(chan);
3506 chan = hdev->smp_data;
3507 hdev->smp_data = NULL;
3508 smp_del_chan(chan);
3509 return err;
3510 }
3511
3512 hdev->smp_bredr_data = chan;
3513
3514 return 0;
3515}
3516
3517void smp_unregister(struct hci_dev *hdev)
3518{
3519 struct l2cap_chan *chan;
3520
3521 if (hdev->smp_bredr_data) {
3522 chan = hdev->smp_bredr_data;
3523 hdev->smp_bredr_data = NULL;
3524 smp_del_chan(chan);
3525 }
3526
3527 if (hdev->smp_data) {
3528 chan = hdev->smp_data;
3529 hdev->smp_data = NULL;
3530 smp_del_chan(chan);
3531 }
3532}
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003533
3534#if IS_ENABLED(CONFIG_BT_SELFTEST_SMP)
3535
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003536static int __init test_debug_key(struct crypto_kpp *tfm_ecdh)
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003537{
Tudor Ambarusc0153b02017-09-28 17:14:55 +03003538 u8 pk[64];
Tudor Ambarusa2976412017-09-28 17:14:52 +03003539 int err;
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003540
Tudor Ambarusc0153b02017-09-28 17:14:55 +03003541 err = set_ecdh_privkey(tfm_ecdh, debug_sk);
Tudor Ambarusa2976412017-09-28 17:14:52 +03003542 if (err)
3543 return err;
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003544
Tudor Ambarusc0153b02017-09-28 17:14:55 +03003545 err = generate_ecdh_public_key(tfm_ecdh, pk);
3546 if (err)
3547 return err;
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003548
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003549 if (crypto_memneq(pk, debug_pk, 64))
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003550 return -EINVAL;
3551
3552 return 0;
3553}
3554
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003555static int __init test_ah(void)
Johan Hedbergcfc41982014-12-30 09:50:40 +02003556{
3557 const u8 irk[16] = {
3558 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3559 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3560 const u8 r[3] = { 0x94, 0x81, 0x70 };
3561 const u8 exp[3] = { 0xaa, 0xfb, 0x0d };
3562 u8 res[3];
3563 int err;
3564
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003565 err = smp_ah(irk, r, res);
Johan Hedbergcfc41982014-12-30 09:50:40 +02003566 if (err)
3567 return err;
3568
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003569 if (crypto_memneq(res, exp, 3))
Johan Hedbergcfc41982014-12-30 09:50:40 +02003570 return -EINVAL;
3571
3572 return 0;
3573}
3574
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003575static int __init test_c1(void)
Johan Hedbergcfc41982014-12-30 09:50:40 +02003576{
3577 const u8 k[16] = {
3578 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3579 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3580 const u8 r[16] = {
3581 0xe0, 0x2e, 0x70, 0xc6, 0x4e, 0x27, 0x88, 0x63,
3582 0x0e, 0x6f, 0xad, 0x56, 0x21, 0xd5, 0x83, 0x57 };
3583 const u8 preq[7] = { 0x01, 0x01, 0x00, 0x00, 0x10, 0x07, 0x07 };
3584 const u8 pres[7] = { 0x02, 0x03, 0x00, 0x00, 0x08, 0x00, 0x05 };
3585 const u8 _iat = 0x01;
3586 const u8 _rat = 0x00;
3587 const bdaddr_t ra = { { 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1 } };
3588 const bdaddr_t ia = { { 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1 } };
3589 const u8 exp[16] = {
3590 0x86, 0x3b, 0xf1, 0xbe, 0xc5, 0x4d, 0xa7, 0xd2,
3591 0xea, 0x88, 0x89, 0x87, 0xef, 0x3f, 0x1e, 0x1e };
3592 u8 res[16];
3593 int err;
3594
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003595 err = smp_c1(k, r, preq, pres, _iat, &ia, _rat, &ra, res);
Johan Hedbergcfc41982014-12-30 09:50:40 +02003596 if (err)
3597 return err;
3598
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003599 if (crypto_memneq(res, exp, 16))
Johan Hedbergcfc41982014-12-30 09:50:40 +02003600 return -EINVAL;
3601
3602 return 0;
3603}
3604
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003605static int __init test_s1(void)
Johan Hedbergcfc41982014-12-30 09:50:40 +02003606{
3607 const u8 k[16] = {
3608 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3609 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
3610 const u8 r1[16] = {
3611 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 };
3612 const u8 r2[16] = {
3613 0x00, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99 };
3614 const u8 exp[16] = {
3615 0x62, 0xa0, 0x6d, 0x79, 0xae, 0x16, 0x42, 0x5b,
3616 0x9b, 0xf4, 0xb0, 0xe8, 0xf0, 0xe1, 0x1f, 0x9a };
3617 u8 res[16];
3618 int err;
3619
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003620 err = smp_s1(k, r1, r2, res);
Johan Hedbergcfc41982014-12-30 09:50:40 +02003621 if (err)
3622 return err;
3623
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003624 if (crypto_memneq(res, exp, 16))
Johan Hedbergcfc41982014-12-30 09:50:40 +02003625 return -EINVAL;
3626
3627 return 0;
3628}
3629
Herbert Xu71af2f62016-01-24 21:18:30 +08003630static int __init test_f4(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003631{
3632 const u8 u[32] = {
3633 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3634 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3635 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3636 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3637 const u8 v[32] = {
3638 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3639 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3640 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3641 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3642 const u8 x[16] = {
3643 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3644 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3645 const u8 z = 0x00;
3646 const u8 exp[16] = {
3647 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
3648 0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
3649 u8 res[16];
3650 int err;
3651
3652 err = smp_f4(tfm_cmac, u, v, x, z, res);
3653 if (err)
3654 return err;
3655
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003656 if (crypto_memneq(res, exp, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003657 return -EINVAL;
3658
3659 return 0;
3660}
3661
Herbert Xu71af2f62016-01-24 21:18:30 +08003662static int __init test_f5(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003663{
3664 const u8 w[32] = {
3665 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
3666 0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
3667 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3668 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3669 const u8 n1[16] = {
3670 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3671 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3672 const u8 n2[16] = {
3673 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3674 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3675 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3676 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3677 const u8 exp_ltk[16] = {
3678 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98,
3679 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69 };
3680 const u8 exp_mackey[16] = {
3681 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3682 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3683 u8 mackey[16], ltk[16];
3684 int err;
3685
3686 err = smp_f5(tfm_cmac, w, n1, n2, a1, a2, mackey, ltk);
3687 if (err)
3688 return err;
3689
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003690 if (crypto_memneq(mackey, exp_mackey, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003691 return -EINVAL;
3692
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003693 if (crypto_memneq(ltk, exp_ltk, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003694 return -EINVAL;
3695
3696 return 0;
3697}
3698
Herbert Xu71af2f62016-01-24 21:18:30 +08003699static int __init test_f6(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003700{
3701 const u8 w[16] = {
3702 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
3703 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
3704 const u8 n1[16] = {
3705 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3706 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3707 const u8 n2[16] = {
3708 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3709 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3710 const u8 r[16] = {
3711 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
3712 0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
3713 const u8 io_cap[3] = { 0x02, 0x01, 0x01 };
3714 const u8 a1[7] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56, 0x00 };
3715 const u8 a2[7] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7, 0x00 };
3716 const u8 exp[16] = {
3717 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
3718 0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
3719 u8 res[16];
3720 int err;
3721
3722 err = smp_f6(tfm_cmac, w, n1, n2, r, io_cap, a1, a2, res);
3723 if (err)
3724 return err;
3725
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003726 if (crypto_memneq(res, exp, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003727 return -EINVAL;
3728
3729 return 0;
3730}
3731
Herbert Xu71af2f62016-01-24 21:18:30 +08003732static int __init test_g2(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003733{
3734 const u8 u[32] = {
3735 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
3736 0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
3737 0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
3738 0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
3739 const u8 v[32] = {
3740 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
3741 0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
3742 0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
3743 0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
3744 const u8 x[16] = {
3745 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
3746 0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
3747 const u8 y[16] = {
3748 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
3749 0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
3750 const u32 exp_val = 0x2f9ed5ba % 1000000;
3751 u32 val;
3752 int err;
3753
3754 err = smp_g2(tfm_cmac, u, v, x, y, &val);
3755 if (err)
3756 return err;
3757
3758 if (val != exp_val)
3759 return -EINVAL;
3760
3761 return 0;
3762}
3763
Herbert Xu71af2f62016-01-24 21:18:30 +08003764static int __init test_h6(struct crypto_shash *tfm_cmac)
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003765{
3766 const u8 w[16] = {
3767 0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
3768 0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
3769 const u8 key_id[4] = { 0x72, 0x62, 0x65, 0x6c };
3770 const u8 exp[16] = {
3771 0x99, 0x63, 0xb1, 0x80, 0xe2, 0xa9, 0xd3, 0xe8,
3772 0x1c, 0xc9, 0x6d, 0xe7, 0x02, 0xe1, 0x9a, 0x2d };
3773 u8 res[16];
3774 int err;
3775
3776 err = smp_h6(tfm_cmac, w, key_id, res);
3777 if (err)
3778 return err;
3779
Jason A. Donenfeld329d8232017-06-10 04:59:11 +02003780 if (crypto_memneq(res, exp, 16))
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003781 return -EINVAL;
3782
3783 return 0;
3784}
3785
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003786static char test_smp_buffer[32];
3787
3788static ssize_t test_smp_read(struct file *file, char __user *user_buf,
3789 size_t count, loff_t *ppos)
3790{
3791 return simple_read_from_buffer(user_buf, count, ppos, test_smp_buffer,
3792 strlen(test_smp_buffer));
3793}
3794
3795static const struct file_operations test_smp_fops = {
3796 .open = simple_open,
3797 .read = test_smp_read,
3798 .llseek = default_llseek,
3799};
3800
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003801static int __init run_selftests(struct crypto_shash *tfm_cmac,
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003802 struct crypto_kpp *tfm_ecdh)
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003803{
Marcel Holtmann255047b2014-12-30 00:11:20 -08003804 ktime_t calltime, delta, rettime;
3805 unsigned long long duration;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003806 int err;
3807
Marcel Holtmann255047b2014-12-30 00:11:20 -08003808 calltime = ktime_get();
3809
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003810 err = test_debug_key(tfm_ecdh);
Marcel Holtmann71653eb2017-04-30 06:51:41 -07003811 if (err) {
3812 BT_ERR("debug_key test failed");
3813 goto done;
3814 }
3815
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003816 err = test_ah();
Johan Hedbergcfc41982014-12-30 09:50:40 +02003817 if (err) {
3818 BT_ERR("smp_ah test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003819 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003820 }
3821
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003822 err = test_c1();
Johan Hedbergcfc41982014-12-30 09:50:40 +02003823 if (err) {
3824 BT_ERR("smp_c1 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003825 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003826 }
3827
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003828 err = test_s1();
Johan Hedbergcfc41982014-12-30 09:50:40 +02003829 if (err) {
3830 BT_ERR("smp_s1 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003831 goto done;
Johan Hedbergcfc41982014-12-30 09:50:40 +02003832 }
3833
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003834 err = test_f4(tfm_cmac);
3835 if (err) {
3836 BT_ERR("smp_f4 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003837 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003838 }
3839
3840 err = test_f5(tfm_cmac);
3841 if (err) {
3842 BT_ERR("smp_f5 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003843 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003844 }
3845
3846 err = test_f6(tfm_cmac);
3847 if (err) {
3848 BT_ERR("smp_f6 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003849 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003850 }
3851
3852 err = test_g2(tfm_cmac);
3853 if (err) {
3854 BT_ERR("smp_g2 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003855 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003856 }
3857
3858 err = test_h6(tfm_cmac);
3859 if (err) {
3860 BT_ERR("smp_h6 test failed");
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003861 goto done;
Johan Hedbergfb2969a2014-12-30 09:50:41 +02003862 }
3863
Marcel Holtmann255047b2014-12-30 00:11:20 -08003864 rettime = ktime_get();
3865 delta = ktime_sub(rettime, calltime);
3866 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
3867
Marcel Holtmann5ced2462015-01-12 23:09:48 -08003868 BT_INFO("SMP test passed in %llu usecs", duration);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003869
Marcel Holtmann64dd3742015-04-01 12:52:13 -07003870done:
3871 if (!err)
3872 snprintf(test_smp_buffer, sizeof(test_smp_buffer),
3873 "PASS (%llu usecs)\n", duration);
3874 else
3875 snprintf(test_smp_buffer, sizeof(test_smp_buffer), "FAIL\n");
3876
3877 debugfs_create_file("selftest_smp", 0444, bt_debugfs, NULL,
3878 &test_smp_fops);
3879
3880 return err;
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003881}
3882
3883int __init bt_selftest_smp(void)
3884{
Herbert Xu71af2f62016-01-24 21:18:30 +08003885 struct crypto_shash *tfm_cmac;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003886 struct crypto_kpp *tfm_ecdh;
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003887 int err;
3888
Eric Biggers3d234b32018-11-14 12:21:11 -08003889 tfm_cmac = crypto_alloc_shash("cmac(aes)", 0, 0);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003890 if (IS_ERR(tfm_cmac)) {
3891 BT_ERR("Unable to create CMAC crypto context");
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003892 return PTR_ERR(tfm_cmac);
3893 }
3894
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003895 tfm_ecdh = crypto_alloc_kpp("ecdh", CRYPTO_ALG_INTERNAL, 0);
3896 if (IS_ERR(tfm_ecdh)) {
3897 BT_ERR("Unable to create ECDH crypto context");
3898 crypto_free_shash(tfm_cmac);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003899 return PTR_ERR(tfm_ecdh);
3900 }
3901
Ard Biesheuvel28a220a2019-07-02 21:41:41 +02003902 err = run_selftests(tfm_cmac, tfm_ecdh);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003903
Herbert Xu71af2f62016-01-24 21:18:30 +08003904 crypto_free_shash(tfm_cmac);
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +03003905 crypto_free_kpp(tfm_ecdh);
Johan Hedberg0a2b0f02014-12-30 09:50:39 +02003906
3907 return err;
3908}
3909
3910#endif