Skip to content

Commit 95be516

Browse files
kapi-noanangl
authored andcommitted
bluetooth: services: fast_pair: add new storage api for finding a key
Added a new fp_storage_account_key_find API to the internal header of the Fast Pair Storage module. This API allows the user to search for a specific Account Key that satisfies certain conditions. These conditions are defined via callback function. The new Fast Pair Storage API is now used in the Keys module. Signed-off-by: Kamil Piszczek <[email protected]>
1 parent c5610f9 commit 95be516

File tree

3 files changed

+85
-19
lines changed

3 files changed

+85
-19
lines changed

subsys/bluetooth/services/fast_pair/fp_keys.c

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ struct fp_procedure {
3838
uint8_t aes_key[FP_CRYPTO_ACCOUNT_KEY_LEN];
3939
};
4040

41+
struct fp_key_gen_account_key_check_context {
42+
struct bt_conn *conn;
43+
struct fp_keys_keygen_params *keygen_params;
44+
};
45+
4146
static bool user_pairing_mode = true;
4247
static struct fp_procedure fp_procedures[CONFIG_BT_MAX_CONN];
4348

@@ -155,35 +160,41 @@ static int key_gen_public_key(struct bt_conn *conn, struct fp_keys_keygen_params
155160
return err;
156161
}
157162

158-
static int key_gen_account_key(struct bt_conn *conn, struct fp_keys_keygen_params *keygen_params)
163+
static bool key_gen_account_key_check(const uint8_t *account_key, void *context)
159164
{
160165
int err;
166+
uint8_t req[FP_CRYPTO_AES128_BLOCK_LEN];
167+
struct fp_key_gen_account_key_check_context *ak_check_context = context;
168+
struct bt_conn *conn = ak_check_context->conn;
169+
struct fp_keys_keygen_params *keygen_params = ak_check_context->keygen_params;
161170
struct fp_procedure *proc = &fp_procedures[bt_conn_index(conn)];
162171

163-
uint8_t req[FP_CRYPTO_AES128_BLOCK_LEN];
164-
uint8_t ak[CONFIG_BT_FAST_PAIR_STORAGE_ACCOUNT_KEY_MAX][FP_CRYPTO_ACCOUNT_KEY_LEN];
165-
size_t ak_cnt = CONFIG_BT_FAST_PAIR_STORAGE_ACCOUNT_KEY_MAX;
172+
memcpy(proc->aes_key, account_key, FP_CRYPTO_ACCOUNT_KEY_LEN);
166173

167-
err = fp_storage_account_keys_get(ak, &ak_cnt);
174+
err = fp_keys_decrypt(conn, req, keygen_params->req_enc);
168175
if (err) {
169-
return err;
176+
return false;
170177
}
171178

172-
for (size_t i = 0; i < ak_cnt; i++) {
173-
memcpy(proc->aes_key, ak[i], FP_CRYPTO_ACCOUNT_KEY_LEN);
174-
175-
err = fp_keys_decrypt(conn, req, keygen_params->req_enc);
176-
if (!err) {
177-
err = keygen_params->req_validate_cb(conn, req, keygen_params->context);
178-
}
179-
180-
if (!err) {
181-
/* Key was found. */
182-
break;
183-
}
179+
err = keygen_params->req_validate_cb(conn, req, keygen_params->context);
180+
if (err) {
181+
return false;
184182
}
185183

186-
return err;
184+
return true;
185+
}
186+
187+
static int key_gen_account_key(struct bt_conn *conn, struct fp_keys_keygen_params *keygen_params)
188+
{
189+
struct fp_key_gen_account_key_check_context context = {
190+
.conn = conn,
191+
.keygen_params = keygen_params,
192+
};
193+
194+
/* This function call assigns the Account Key internally to the Fast Pair Keys
195+
* module. The assignment happens in the provided callback method.
196+
*/
197+
return fp_storage_account_key_find(NULL, key_gen_account_key_check, &context);
187198
}
188199

189200
int fp_keys_generate_key(struct bt_conn *conn, struct fp_keys_keygen_params *keygen_params)

subsys/bluetooth/services/fast_pair/fp_storage.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,33 @@ int fp_storage_account_keys_get(uint8_t buf[][FP_CRYPTO_ACCOUNT_KEY_LEN], size_t
239239
return 0;
240240
}
241241

242+
int fp_storage_account_key_find(uint8_t account_key[FP_CRYPTO_ACCOUNT_KEY_LEN],
243+
fp_storage_account_key_check_cb account_key_check_cb,
244+
void *context)
245+
{
246+
if (!atomic_get(&settings_loaded)) {
247+
return -ENODATA;
248+
}
249+
250+
if (!account_key_check_cb) {
251+
return -EINVAL;
252+
}
253+
254+
for (size_t i = 0; i < account_key_count; i++) {
255+
if (account_key_check_cb(account_key_list[i], context)) {
256+
if (account_key) {
257+
memcpy(account_key,
258+
account_key_list[i],
259+
FP_CRYPTO_ACCOUNT_KEY_LEN);
260+
}
261+
262+
return 0;
263+
}
264+
}
265+
266+
return -ESRCH;
267+
}
268+
242269
int fp_storage_account_key_save(const uint8_t *account_key)
243270
{
244271
if (!atomic_get(&settings_loaded)) {

subsys/bluetooth/services/fast_pair/include/fp_storage.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ extern "C" {
2121
#include <sys/types.h>
2222
#include "fp_crypto.h"
2323

24+
/**
25+
* @typedef fp_storage_account_key_check_cb
26+
* @brief Callback used to check if a given Account Key satisfies user-defined
27+
* conditions.
28+
*
29+
* @param[in] account_key 128-bit (16-byte) Account Key to be checked.
30+
* @param[in] context Pointer used to pass operation context.
31+
*
32+
* @return True if Account Key satisfies user-defined conditions.
33+
* False otherwise.
34+
*/
35+
typedef bool (*fp_storage_account_key_check_cb)(const uint8_t *account_key, void *context);
36+
2437
/** Save Account Key.
2538
*
2639
* @param[in] account_key 128-bit (16-byte) Account Key to be saved.
@@ -50,6 +63,21 @@ int fp_storage_account_key_count(void);
5063
*/
5164
int fp_storage_account_keys_get(uint8_t buf[][FP_CRYPTO_ACCOUNT_KEY_LEN], size_t *key_count);
5265

66+
/** Iterate over stored Account Keys to find a key that matches user-defined conditions.
67+
* If such a key is found, the iteration process stops and this function returns.
68+
*
69+
* @param[out] account_key Found Account Key. It is possible to pass NULL pointer if the found
70+
* key value is irrelevant.
71+
* @param[in] account_key_check_cb Callback for determining if a given Account Key satisfies
72+
* user-defined conditions.
73+
* @param[in] context Pointer used to pass operation context.
74+
*
75+
* @return 0 If the Account Key was found. Otherwise, a (negative) error code is returned.
76+
*/
77+
int fp_storage_account_key_find(uint8_t account_key[FP_CRYPTO_ACCOUNT_KEY_LEN],
78+
fp_storage_account_key_check_cb account_key_check_cb,
79+
void *context);
80+
5381
#ifdef __cplusplus
5482
}
5583
#endif

0 commit comments

Comments
 (0)