1 /**
2  * @file rpa.c
3  * Resolvable Private Address Generation and Resolution
4  */
5 
6 /*
7  * Copyright (c) 2017 Nordic Semiconductor ASA
8  * Copyright (c) 2015-2016 Intel Corporation
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  */
12 
13 #include <zephyr/kernel.h>
14 #include <stddef.h>
15 #include <errno.h>
16 #include <string.h>
17 
18 #include "common/bt_str.h"
19 
20 #include <zephyr/bluetooth/crypto.h>
21 
22 #define LOG_LEVEL CONFIG_BT_RPA_LOG_LEVEL
23 #include <zephyr/logging/log.h>
24 LOG_MODULE_REGISTER(bt_rpa);
25 
26 #if defined(CONFIG_BT_CTLR) && defined(CONFIG_BT_HOST_CRYPTO)
27 #include "../controller/util/util.h"
28 #include "../controller/hal/ecb.h"
29 #endif /* defined(CONFIG_BT_CTLR) && defined(CONFIG_BT_HOST_CRYPTO) */
30 
31 #if defined(CONFIG_BT_PRIVACY) || defined(CONFIG_BT_CTLR_PRIVACY)
internal_rand(void * buf,size_t len)32 static int internal_rand(void *buf, size_t len)
33 {
34 /* Force using controller rand function. */
35 #if defined(CONFIG_BT_CTLR) && defined(CONFIG_BT_HOST_CRYPTO)
36 	return lll_csrand_get(buf, len);
37 #else
38 	return bt_rand(buf, len);
39 #endif
40 }
41 #endif /* defined(CONFIG_BT_PRIVACY) || defined(CONFIG_BT_CTLR_PRIVACY) */
42 
internal_encrypt_le(const uint8_t key[16],const uint8_t plaintext[16],uint8_t enc_data[16])43 static int internal_encrypt_le(const uint8_t key[16], const uint8_t plaintext[16],
44 			       uint8_t enc_data[16])
45 {
46 /* Force using controller encrypt function if supported. */
47 #if defined(CONFIG_BT_CTLR) && defined(CONFIG_BT_HOST_CRYPTO) && \
48     defined(CONFIG_BT_CTLR_LE_ENC)
49 	ecb_encrypt(key, plaintext, enc_data, NULL);
50 	return 0;
51 #else
52 	return bt_encrypt_le(key, plaintext, enc_data);
53 #endif
54 }
55 
ah(const uint8_t irk[16],const uint8_t r[3],uint8_t out[3])56 static int ah(const uint8_t irk[16], const uint8_t r[3], uint8_t out[3])
57 {
58 	uint8_t res[16];
59 	int err;
60 
61 	LOG_DBG("irk %s", bt_hex(irk, 16));
62 	LOG_DBG("r %s", bt_hex(r, 3));
63 
64 	/* r' = padding || r */
65 	memcpy(res, r, 3);
66 	(void)memset(res + 3, 0, 13);
67 
68 	err = internal_encrypt_le(irk, res, res);
69 	if (err) {
70 		return err;
71 	}
72 
73 	/* The output of the random address function ah is:
74 	 *      ah(h, r) = e(k, r') mod 2^24
75 	 * The output of the security function e is then truncated to 24 bits
76 	 * by taking the least significant 24 bits of the output of e as the
77 	 * result of ah.
78 	 */
79 	memcpy(out, res, 3);
80 
81 	return 0;
82 }
83 
84 #if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_CTLR_PRIVACY)
bt_rpa_irk_matches(const uint8_t irk[16],const bt_addr_t * addr)85 bool bt_rpa_irk_matches(const uint8_t irk[16], const bt_addr_t *addr)
86 {
87 	uint8_t hash[3];
88 	int err;
89 
90 	LOG_DBG("IRK %s bdaddr %s", bt_hex(irk, 16), bt_addr_str(addr));
91 
92 	err = ah(irk, addr->val + 3, hash);
93 	if (err) {
94 		return false;
95 	}
96 
97 	return !memcmp(addr->val, hash, 3);
98 }
99 #endif
100 
101 #if defined(CONFIG_BT_PRIVACY) || defined(CONFIG_BT_CTLR_PRIVACY)
bt_rpa_create(const uint8_t irk[16],bt_addr_t * rpa)102 int bt_rpa_create(const uint8_t irk[16], bt_addr_t *rpa)
103 {
104 	int err;
105 
106 	err = internal_rand(rpa->val + 3, 3);
107 	if (err) {
108 		return err;
109 	}
110 
111 	BT_ADDR_SET_RPA(rpa);
112 
113 	err = ah(irk, rpa->val + 3, rpa->val);
114 	if (err) {
115 		return err;
116 	}
117 
118 	LOG_DBG("Created RPA %s", bt_addr_str((bt_addr_t *)rpa->val));
119 
120 	return 0;
121 }
122 #else
bt_rpa_create(const uint8_t irk[16],bt_addr_t * rpa)123 int bt_rpa_create(const uint8_t irk[16], bt_addr_t *rpa)
124 {
125 	return -ENOTSUP;
126 }
127 #endif /* CONFIG_BT_PRIVACY */
128