162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci   BlueZ - Bluetooth protocol stack for Linux
362306a36Sopenharmony_ci   Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci   This program is free software; you can redistribute it and/or modify
662306a36Sopenharmony_ci   it under the terms of the GNU General Public License version 2 as
762306a36Sopenharmony_ci   published by the Free Software Foundation;
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1062306a36Sopenharmony_ci   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1162306a36Sopenharmony_ci   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
1262306a36Sopenharmony_ci   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
1362306a36Sopenharmony_ci   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
1462306a36Sopenharmony_ci   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1562306a36Sopenharmony_ci   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1662306a36Sopenharmony_ci   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ci   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
1962306a36Sopenharmony_ci   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
2062306a36Sopenharmony_ci   SOFTWARE IS DISCLAIMED.
2162306a36Sopenharmony_ci*/
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci#ifndef __SMP_H
2462306a36Sopenharmony_ci#define __SMP_H
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_cistruct smp_command_hdr {
2762306a36Sopenharmony_ci	__u8	code;
2862306a36Sopenharmony_ci} __packed;
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci#define SMP_CMD_PAIRING_REQ	0x01
3162306a36Sopenharmony_ci#define SMP_CMD_PAIRING_RSP	0x02
3262306a36Sopenharmony_cistruct smp_cmd_pairing {
3362306a36Sopenharmony_ci	__u8	io_capability;
3462306a36Sopenharmony_ci	__u8	oob_flag;
3562306a36Sopenharmony_ci	__u8	auth_req;
3662306a36Sopenharmony_ci	__u8	max_key_size;
3762306a36Sopenharmony_ci	__u8	init_key_dist;
3862306a36Sopenharmony_ci	__u8	resp_key_dist;
3962306a36Sopenharmony_ci} __packed;
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#define SMP_IO_DISPLAY_ONLY	0x00
4262306a36Sopenharmony_ci#define SMP_IO_DISPLAY_YESNO	0x01
4362306a36Sopenharmony_ci#define SMP_IO_KEYBOARD_ONLY	0x02
4462306a36Sopenharmony_ci#define SMP_IO_NO_INPUT_OUTPUT	0x03
4562306a36Sopenharmony_ci#define SMP_IO_KEYBOARD_DISPLAY	0x04
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci#define SMP_OOB_NOT_PRESENT	0x00
4862306a36Sopenharmony_ci#define SMP_OOB_PRESENT		0x01
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci#define SMP_DIST_ENC_KEY	0x01
5162306a36Sopenharmony_ci#define SMP_DIST_ID_KEY		0x02
5262306a36Sopenharmony_ci#define SMP_DIST_SIGN		0x04
5362306a36Sopenharmony_ci#define SMP_DIST_LINK_KEY	0x08
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci#define SMP_AUTH_NONE		0x00
5662306a36Sopenharmony_ci#define SMP_AUTH_BONDING	0x01
5762306a36Sopenharmony_ci#define SMP_AUTH_MITM		0x04
5862306a36Sopenharmony_ci#define SMP_AUTH_SC		0x08
5962306a36Sopenharmony_ci#define SMP_AUTH_KEYPRESS	0x10
6062306a36Sopenharmony_ci#define SMP_AUTH_CT2		0x20
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci#define SMP_CMD_PAIRING_CONFIRM	0x03
6362306a36Sopenharmony_cistruct smp_cmd_pairing_confirm {
6462306a36Sopenharmony_ci	__u8	confirm_val[16];
6562306a36Sopenharmony_ci} __packed;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci#define SMP_CMD_PAIRING_RANDOM	0x04
6862306a36Sopenharmony_cistruct smp_cmd_pairing_random {
6962306a36Sopenharmony_ci	__u8	rand_val[16];
7062306a36Sopenharmony_ci} __packed;
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci#define SMP_CMD_PAIRING_FAIL	0x05
7362306a36Sopenharmony_cistruct smp_cmd_pairing_fail {
7462306a36Sopenharmony_ci	__u8	reason;
7562306a36Sopenharmony_ci} __packed;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci#define SMP_CMD_ENCRYPT_INFO	0x06
7862306a36Sopenharmony_cistruct smp_cmd_encrypt_info {
7962306a36Sopenharmony_ci	__u8	ltk[16];
8062306a36Sopenharmony_ci} __packed;
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci#define SMP_CMD_INITIATOR_IDENT	0x07
8362306a36Sopenharmony_cistruct smp_cmd_initiator_ident {
8462306a36Sopenharmony_ci	__le16	ediv;
8562306a36Sopenharmony_ci	__le64	rand;
8662306a36Sopenharmony_ci} __packed;
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci#define SMP_CMD_IDENT_INFO	0x08
8962306a36Sopenharmony_cistruct smp_cmd_ident_info {
9062306a36Sopenharmony_ci	__u8	irk[16];
9162306a36Sopenharmony_ci} __packed;
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci#define SMP_CMD_IDENT_ADDR_INFO	0x09
9462306a36Sopenharmony_cistruct smp_cmd_ident_addr_info {
9562306a36Sopenharmony_ci	__u8	addr_type;
9662306a36Sopenharmony_ci	bdaddr_t bdaddr;
9762306a36Sopenharmony_ci} __packed;
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci#define SMP_CMD_SIGN_INFO	0x0a
10062306a36Sopenharmony_cistruct smp_cmd_sign_info {
10162306a36Sopenharmony_ci	__u8	csrk[16];
10262306a36Sopenharmony_ci} __packed;
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci#define SMP_CMD_SECURITY_REQ	0x0b
10562306a36Sopenharmony_cistruct smp_cmd_security_req {
10662306a36Sopenharmony_ci	__u8	auth_req;
10762306a36Sopenharmony_ci} __packed;
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci#define SMP_CMD_PUBLIC_KEY	0x0c
11062306a36Sopenharmony_cistruct smp_cmd_public_key {
11162306a36Sopenharmony_ci	__u8	x[32];
11262306a36Sopenharmony_ci	__u8	y[32];
11362306a36Sopenharmony_ci} __packed;
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci#define SMP_CMD_DHKEY_CHECK	0x0d
11662306a36Sopenharmony_cistruct smp_cmd_dhkey_check {
11762306a36Sopenharmony_ci	__u8	e[16];
11862306a36Sopenharmony_ci} __packed;
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci#define SMP_CMD_KEYPRESS_NOTIFY	0x0e
12162306a36Sopenharmony_cistruct smp_cmd_keypress_notify {
12262306a36Sopenharmony_ci	__u8	value;
12362306a36Sopenharmony_ci} __packed;
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci#define SMP_CMD_MAX		0x0e
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ci#define SMP_PASSKEY_ENTRY_FAILED	0x01
12862306a36Sopenharmony_ci#define SMP_OOB_NOT_AVAIL		0x02
12962306a36Sopenharmony_ci#define SMP_AUTH_REQUIREMENTS		0x03
13062306a36Sopenharmony_ci#define SMP_CONFIRM_FAILED		0x04
13162306a36Sopenharmony_ci#define SMP_PAIRING_NOTSUPP		0x05
13262306a36Sopenharmony_ci#define SMP_ENC_KEY_SIZE		0x06
13362306a36Sopenharmony_ci#define SMP_CMD_NOTSUPP			0x07
13462306a36Sopenharmony_ci#define SMP_UNSPECIFIED			0x08
13562306a36Sopenharmony_ci#define SMP_REPEATED_ATTEMPTS		0x09
13662306a36Sopenharmony_ci#define SMP_INVALID_PARAMS		0x0a
13762306a36Sopenharmony_ci#define SMP_DHKEY_CHECK_FAILED		0x0b
13862306a36Sopenharmony_ci#define SMP_NUMERIC_COMP_FAILED		0x0c
13962306a36Sopenharmony_ci#define SMP_BREDR_PAIRING_IN_PROGRESS	0x0d
14062306a36Sopenharmony_ci#define SMP_CROSS_TRANSP_NOT_ALLOWED	0x0e
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci#define SMP_MIN_ENC_KEY_SIZE		7
14362306a36Sopenharmony_ci#define SMP_MAX_ENC_KEY_SIZE		16
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci/* LTK types used in internal storage (struct smp_ltk) */
14662306a36Sopenharmony_cienum {
14762306a36Sopenharmony_ci	SMP_STK,
14862306a36Sopenharmony_ci	SMP_LTK,
14962306a36Sopenharmony_ci	SMP_LTK_RESPONDER,
15062306a36Sopenharmony_ci	SMP_LTK_P256,
15162306a36Sopenharmony_ci	SMP_LTK_P256_DEBUG,
15262306a36Sopenharmony_ci};
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_cistatic inline bool smp_ltk_is_sc(struct smp_ltk *key)
15562306a36Sopenharmony_ci{
15662306a36Sopenharmony_ci	switch (key->type) {
15762306a36Sopenharmony_ci	case SMP_LTK_P256:
15862306a36Sopenharmony_ci	case SMP_LTK_P256_DEBUG:
15962306a36Sopenharmony_ci		return true;
16062306a36Sopenharmony_ci	}
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	return false;
16362306a36Sopenharmony_ci}
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_cistatic inline u8 smp_ltk_sec_level(struct smp_ltk *key)
16662306a36Sopenharmony_ci{
16762306a36Sopenharmony_ci	if (key->authenticated) {
16862306a36Sopenharmony_ci		if (smp_ltk_is_sc(key))
16962306a36Sopenharmony_ci			return BT_SECURITY_FIPS;
17062306a36Sopenharmony_ci		else
17162306a36Sopenharmony_ci			return BT_SECURITY_HIGH;
17262306a36Sopenharmony_ci	}
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ci	return BT_SECURITY_MEDIUM;
17562306a36Sopenharmony_ci}
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci/* Key preferences for smp_sufficient security */
17862306a36Sopenharmony_cienum smp_key_pref {
17962306a36Sopenharmony_ci	SMP_ALLOW_STK,
18062306a36Sopenharmony_ci	SMP_USE_LTK,
18162306a36Sopenharmony_ci};
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci/* SMP Commands */
18462306a36Sopenharmony_ciint smp_cancel_and_remove_pairing(struct hci_dev *hdev, bdaddr_t *bdaddr,
18562306a36Sopenharmony_ci				  u8 addr_type);
18662306a36Sopenharmony_cibool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
18762306a36Sopenharmony_ci			     enum smp_key_pref key_pref);
18862306a36Sopenharmony_ciint smp_conn_security(struct hci_conn *hcon, __u8 sec_level);
18962306a36Sopenharmony_ciint smp_user_confirm_reply(struct hci_conn *conn, u16 mgmt_op, __le32 passkey);
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_cibool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
19262306a36Sopenharmony_ci		     const bdaddr_t *bdaddr);
19362306a36Sopenharmony_ciint smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa);
19462306a36Sopenharmony_ciint smp_generate_oob(struct hci_dev *hdev, u8 hash[16], u8 rand[16]);
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ciint smp_force_bredr(struct hci_dev *hdev, bool enable);
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ciint smp_register(struct hci_dev *hdev);
19962306a36Sopenharmony_civoid smp_unregister(struct hci_dev *hdev);
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_BT_SELFTEST_SMP)
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ciint bt_selftest_smp(void);
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci#else
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_cistatic inline int bt_selftest_smp(void)
20862306a36Sopenharmony_ci{
20962306a36Sopenharmony_ci	return 0;
21062306a36Sopenharmony_ci}
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ci#endif
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci#endif /* __SMP_H */
215