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_CRYPTO) && defined(CONFIG_BT_HOST_CRYPTO)
27 #include "../controller/util/util.h"
28 #include "../controller/hal/ecb.h"
29 #endif /* CONFIG_BT_CTLR_CRYPTO && 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_CRYPTO) && defined(CONFIG_BT_HOST_CRYPTO)
36 return lll_csrand_get(buf, len);
37 #else /* !CONFIG_BT_CTLR_CRYPTO || !CONFIG_BT_HOST_CRYPTO */
38 return bt_rand(buf, len);
39 #endif /* !CONFIG_BT_CTLR_CRYPTO || !CONFIG_BT_HOST_CRYPTO */
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_CRYPTO) && defined(CONFIG_BT_HOST_CRYPTO)
48 ecb_encrypt(key, plaintext, enc_data, NULL);
49 return 0;
50 #else /* !CONFIG_BT_CTLR_CRYPTO || !CONFIG_BT_HOST_CRYPTO */
51 return bt_encrypt_le(key, plaintext, enc_data);
52 #endif /* !CONFIG_BT_CTLR_CRYPTO || !CONFIG_BT_HOST_CRYPTO */
53 }
54
ah(const uint8_t irk[16],const uint8_t r[3],uint8_t out[3])55 static int ah(const uint8_t irk[16], const uint8_t r[3], uint8_t out[3])
56 {
57 uint8_t res[16];
58 int err;
59
60 LOG_DBG("irk %s", bt_hex(irk, 16));
61 LOG_DBG("r %s", bt_hex(r, 3));
62
63 /* r' = padding || r */
64 memcpy(res, r, 3);
65 (void)memset(res + 3, 0, 13);
66
67 err = internal_encrypt_le(irk, res, res);
68 if (err) {
69 return err;
70 }
71
72 /* The output of the random address function ah is:
73 * ah(h, r) = e(k, r') mod 2^24
74 * The output of the security function e is then truncated to 24 bits
75 * by taking the least significant 24 bits of the output of e as the
76 * result of ah.
77 */
78 memcpy(out, res, 3);
79
80 return 0;
81 }
82
83 #if defined(CONFIG_BT_SMP) || defined(CONFIG_BT_CTLR_PRIVACY)
bt_rpa_irk_matches(const uint8_t irk[16],const bt_addr_t * addr)84 bool bt_rpa_irk_matches(const uint8_t irk[16], const bt_addr_t *addr)
85 {
86 uint8_t hash[3];
87 int err;
88
89 LOG_DBG("IRK %s bdaddr %s", bt_hex(irk, 16), bt_addr_str(addr));
90
91 err = ah(irk, addr->val + 3, hash);
92 if (err) {
93 return false;
94 }
95
96 return !memcmp(addr->val, hash, 3);
97 }
98 #endif
99
100 #if defined(CONFIG_BT_PRIVACY) || defined(CONFIG_BT_CTLR_PRIVACY)
bt_rpa_create(const uint8_t irk[16],bt_addr_t * rpa)101 int bt_rpa_create(const uint8_t irk[16], bt_addr_t *rpa)
102 {
103 int err;
104
105 err = internal_rand(rpa->val + 3, 3);
106 if (err) {
107 return err;
108 }
109
110 BT_ADDR_SET_RPA(rpa);
111
112 err = ah(irk, rpa->val + 3, rpa->val);
113 if (err) {
114 return err;
115 }
116
117 LOG_DBG("Created RPA %s", bt_addr_str((bt_addr_t *)rpa->val));
118
119 return 0;
120 }
121 #else
bt_rpa_create(const uint8_t irk[16],bt_addr_t * rpa)122 int bt_rpa_create(const uint8_t irk[16], bt_addr_t *rpa)
123 {
124 return -ENOTSUP;
125 }
126 #endif /* CONFIG_BT_PRIVACY */
127