18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci   BlueZ - Bluetooth protocol stack for Linux
38c2ecf20Sopenharmony_ci   Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci   This program is free software; you can redistribute it and/or modify
68c2ecf20Sopenharmony_ci   it under the terms of the GNU General Public License version 2 as
78c2ecf20Sopenharmony_ci   published by the Free Software Foundation;
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
108c2ecf20Sopenharmony_ci   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
118c2ecf20Sopenharmony_ci   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
128c2ecf20Sopenharmony_ci   IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
138c2ecf20Sopenharmony_ci   CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
148c2ecf20Sopenharmony_ci   WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
158c2ecf20Sopenharmony_ci   ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
168c2ecf20Sopenharmony_ci   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci   ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
198c2ecf20Sopenharmony_ci   COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
208c2ecf20Sopenharmony_ci   SOFTWARE IS DISCLAIMED.
218c2ecf20Sopenharmony_ci*/
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#ifndef __SMP_H
248c2ecf20Sopenharmony_ci#define __SMP_H
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistruct smp_command_hdr {
278c2ecf20Sopenharmony_ci	__u8	code;
288c2ecf20Sopenharmony_ci} __packed;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define SMP_CMD_PAIRING_REQ	0x01
318c2ecf20Sopenharmony_ci#define SMP_CMD_PAIRING_RSP	0x02
328c2ecf20Sopenharmony_cistruct smp_cmd_pairing {
338c2ecf20Sopenharmony_ci	__u8	io_capability;
348c2ecf20Sopenharmony_ci	__u8	oob_flag;
358c2ecf20Sopenharmony_ci	__u8	auth_req;
368c2ecf20Sopenharmony_ci	__u8	max_key_size;
378c2ecf20Sopenharmony_ci	__u8	init_key_dist;
388c2ecf20Sopenharmony_ci	__u8	resp_key_dist;
398c2ecf20Sopenharmony_ci} __packed;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define SMP_IO_DISPLAY_ONLY	0x00
428c2ecf20Sopenharmony_ci#define SMP_IO_DISPLAY_YESNO	0x01
438c2ecf20Sopenharmony_ci#define SMP_IO_KEYBOARD_ONLY	0x02
448c2ecf20Sopenharmony_ci#define SMP_IO_NO_INPUT_OUTPUT	0x03
458c2ecf20Sopenharmony_ci#define SMP_IO_KEYBOARD_DISPLAY	0x04
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci#define SMP_OOB_NOT_PRESENT	0x00
488c2ecf20Sopenharmony_ci#define SMP_OOB_PRESENT		0x01
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define SMP_DIST_ENC_KEY	0x01
518c2ecf20Sopenharmony_ci#define SMP_DIST_ID_KEY		0x02
528c2ecf20Sopenharmony_ci#define SMP_DIST_SIGN		0x04
538c2ecf20Sopenharmony_ci#define SMP_DIST_LINK_KEY	0x08
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#define SMP_AUTH_NONE		0x00
568c2ecf20Sopenharmony_ci#define SMP_AUTH_BONDING	0x01
578c2ecf20Sopenharmony_ci#define SMP_AUTH_MITM		0x04
588c2ecf20Sopenharmony_ci#define SMP_AUTH_SC		0x08
598c2ecf20Sopenharmony_ci#define SMP_AUTH_KEYPRESS	0x10
608c2ecf20Sopenharmony_ci#define SMP_AUTH_CT2		0x20
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define SMP_CMD_PAIRING_CONFIRM	0x03
638c2ecf20Sopenharmony_cistruct smp_cmd_pairing_confirm {
648c2ecf20Sopenharmony_ci	__u8	confirm_val[16];
658c2ecf20Sopenharmony_ci} __packed;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#define SMP_CMD_PAIRING_RANDOM	0x04
688c2ecf20Sopenharmony_cistruct smp_cmd_pairing_random {
698c2ecf20Sopenharmony_ci	__u8	rand_val[16];
708c2ecf20Sopenharmony_ci} __packed;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#define SMP_CMD_PAIRING_FAIL	0x05
738c2ecf20Sopenharmony_cistruct smp_cmd_pairing_fail {
748c2ecf20Sopenharmony_ci	__u8	reason;
758c2ecf20Sopenharmony_ci} __packed;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define SMP_CMD_ENCRYPT_INFO	0x06
788c2ecf20Sopenharmony_cistruct smp_cmd_encrypt_info {
798c2ecf20Sopenharmony_ci	__u8	ltk[16];
808c2ecf20Sopenharmony_ci} __packed;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define SMP_CMD_INITIATOR_IDENT	0x07
838c2ecf20Sopenharmony_cistruct smp_cmd_initiator_ident {
848c2ecf20Sopenharmony_ci	__le16	ediv;
858c2ecf20Sopenharmony_ci	__le64	rand;
868c2ecf20Sopenharmony_ci} __packed;
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci#define SMP_CMD_IDENT_INFO	0x08
898c2ecf20Sopenharmony_cistruct smp_cmd_ident_info {
908c2ecf20Sopenharmony_ci	__u8	irk[16];
918c2ecf20Sopenharmony_ci} __packed;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci#define SMP_CMD_IDENT_ADDR_INFO	0x09
948c2ecf20Sopenharmony_cistruct smp_cmd_ident_addr_info {
958c2ecf20Sopenharmony_ci	__u8	addr_type;
968c2ecf20Sopenharmony_ci	bdaddr_t bdaddr;
978c2ecf20Sopenharmony_ci} __packed;
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci#define SMP_CMD_SIGN_INFO	0x0a
1008c2ecf20Sopenharmony_cistruct smp_cmd_sign_info {
1018c2ecf20Sopenharmony_ci	__u8	csrk[16];
1028c2ecf20Sopenharmony_ci} __packed;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci#define SMP_CMD_SECURITY_REQ	0x0b
1058c2ecf20Sopenharmony_cistruct smp_cmd_security_req {
1068c2ecf20Sopenharmony_ci	__u8	auth_req;
1078c2ecf20Sopenharmony_ci} __packed;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci#define SMP_CMD_PUBLIC_KEY	0x0c
1108c2ecf20Sopenharmony_cistruct smp_cmd_public_key {
1118c2ecf20Sopenharmony_ci	__u8	x[32];
1128c2ecf20Sopenharmony_ci	__u8	y[32];
1138c2ecf20Sopenharmony_ci} __packed;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci#define SMP_CMD_DHKEY_CHECK	0x0d
1168c2ecf20Sopenharmony_cistruct smp_cmd_dhkey_check {
1178c2ecf20Sopenharmony_ci	__u8	e[16];
1188c2ecf20Sopenharmony_ci} __packed;
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci#define SMP_CMD_KEYPRESS_NOTIFY	0x0e
1218c2ecf20Sopenharmony_cistruct smp_cmd_keypress_notify {
1228c2ecf20Sopenharmony_ci	__u8	value;
1238c2ecf20Sopenharmony_ci} __packed;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci#define SMP_CMD_MAX		0x0e
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci#define SMP_PASSKEY_ENTRY_FAILED	0x01
1288c2ecf20Sopenharmony_ci#define SMP_OOB_NOT_AVAIL		0x02
1298c2ecf20Sopenharmony_ci#define SMP_AUTH_REQUIREMENTS		0x03
1308c2ecf20Sopenharmony_ci#define SMP_CONFIRM_FAILED		0x04
1318c2ecf20Sopenharmony_ci#define SMP_PAIRING_NOTSUPP		0x05
1328c2ecf20Sopenharmony_ci#define SMP_ENC_KEY_SIZE		0x06
1338c2ecf20Sopenharmony_ci#define SMP_CMD_NOTSUPP			0x07
1348c2ecf20Sopenharmony_ci#define SMP_UNSPECIFIED			0x08
1358c2ecf20Sopenharmony_ci#define SMP_REPEATED_ATTEMPTS		0x09
1368c2ecf20Sopenharmony_ci#define SMP_INVALID_PARAMS		0x0a
1378c2ecf20Sopenharmony_ci#define SMP_DHKEY_CHECK_FAILED		0x0b
1388c2ecf20Sopenharmony_ci#define SMP_NUMERIC_COMP_FAILED		0x0c
1398c2ecf20Sopenharmony_ci#define SMP_BREDR_PAIRING_IN_PROGRESS	0x0d
1408c2ecf20Sopenharmony_ci#define SMP_CROSS_TRANSP_NOT_ALLOWED	0x0e
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci#define SMP_MIN_ENC_KEY_SIZE		7
1438c2ecf20Sopenharmony_ci#define SMP_MAX_ENC_KEY_SIZE		16
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci/* LTK types used in internal storage (struct smp_ltk) */
1468c2ecf20Sopenharmony_cienum {
1478c2ecf20Sopenharmony_ci	SMP_STK,
1488c2ecf20Sopenharmony_ci	SMP_LTK,
1498c2ecf20Sopenharmony_ci	SMP_LTK_RESPONDER,
1508c2ecf20Sopenharmony_ci	SMP_LTK_P256,
1518c2ecf20Sopenharmony_ci	SMP_LTK_P256_DEBUG,
1528c2ecf20Sopenharmony_ci};
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_cistatic inline bool smp_ltk_is_sc(struct smp_ltk *key)
1558c2ecf20Sopenharmony_ci{
1568c2ecf20Sopenharmony_ci	switch (key->type) {
1578c2ecf20Sopenharmony_ci	case SMP_LTK_P256:
1588c2ecf20Sopenharmony_ci	case SMP_LTK_P256_DEBUG:
1598c2ecf20Sopenharmony_ci		return true;
1608c2ecf20Sopenharmony_ci	}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	return false;
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic inline u8 smp_ltk_sec_level(struct smp_ltk *key)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	if (key->authenticated) {
1688c2ecf20Sopenharmony_ci		if (smp_ltk_is_sc(key))
1698c2ecf20Sopenharmony_ci			return BT_SECURITY_FIPS;
1708c2ecf20Sopenharmony_ci		else
1718c2ecf20Sopenharmony_ci			return BT_SECURITY_HIGH;
1728c2ecf20Sopenharmony_ci	}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	return BT_SECURITY_MEDIUM;
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci/* Key preferences for smp_sufficient security */
1788c2ecf20Sopenharmony_cienum smp_key_pref {
1798c2ecf20Sopenharmony_ci	SMP_ALLOW_STK,
1808c2ecf20Sopenharmony_ci	SMP_USE_LTK,
1818c2ecf20Sopenharmony_ci};
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci/* SMP Commands */
1848c2ecf20Sopenharmony_ciint smp_cancel_and_remove_pairing(struct hci_dev *hdev, bdaddr_t *bdaddr,
1858c2ecf20Sopenharmony_ci				  u8 addr_type);
1868c2ecf20Sopenharmony_cibool smp_sufficient_security(struct hci_conn *hcon, u8 sec_level,
1878c2ecf20Sopenharmony_ci			     enum smp_key_pref key_pref);
1888c2ecf20Sopenharmony_ciint smp_conn_security(struct hci_conn *hcon, __u8 sec_level);
1898c2ecf20Sopenharmony_ciint smp_user_confirm_reply(struct hci_conn *conn, u16 mgmt_op, __le32 passkey);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_cibool smp_irk_matches(struct hci_dev *hdev, const u8 irk[16],
1928c2ecf20Sopenharmony_ci		     const bdaddr_t *bdaddr);
1938c2ecf20Sopenharmony_ciint smp_generate_rpa(struct hci_dev *hdev, const u8 irk[16], bdaddr_t *rpa);
1948c2ecf20Sopenharmony_ciint smp_generate_oob(struct hci_dev *hdev, u8 hash[16], u8 rand[16]);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ciint smp_register(struct hci_dev *hdev);
1978c2ecf20Sopenharmony_civoid smp_unregister(struct hci_dev *hdev);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci#if IS_ENABLED(CONFIG_BT_SELFTEST_SMP)
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ciint bt_selftest_smp(void);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci#else
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_cistatic inline int bt_selftest_smp(void)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	return 0;
2088c2ecf20Sopenharmony_ci}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci#endif
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci#endif /* __SMP_H */
213