1 /* ecc.h - ECDH helpers */
2 
3 /*
4  * Copyright (c) 2016 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 /** Key size used in Bluetooth's ECC domain. */
10 #define BT_ECC_KEY_SIZE            32
11 /** Length of a Bluetooth ECC public key coordinate. */
12 #define BT_PUB_KEY_COORD_LEN       (BT_ECC_KEY_SIZE)
13 /** Length of a Bluetooth ECC public key. */
14 #define BT_PUB_KEY_LEN             (2 * (BT_PUB_KEY_COORD_LEN))
15 /** Length of a Bluetooth ECC private key. */
16 #define BT_PRIV_KEY_LEN            (BT_ECC_KEY_SIZE)
17 /** Length of a Bluetooth Diffie-Hellman key. */
18 #define BT_DH_KEY_LEN              (BT_ECC_KEY_SIZE)
19 
20 /*  @brief Container for public key callback */
21 struct bt_pub_key_cb {
22 	/** @brief Callback type for Public Key generation.
23 	 *
24 	 *  Used to notify of the local public key or that the local key is not
25 	 *  available (either because of a failure to read it or because it is
26 	 *  being regenerated).
27 	 *
28 	 *  @param key The local public key, or NULL in case of no key.
29 	 */
30 	void (*func)(const uint8_t key[BT_PUB_KEY_LEN]);
31 
32 	/* Internal */
33 	sys_snode_t node;
34 };
35 
36 /*  @brief Check if public key is equal to the debug public key.
37  *
38  *  Compare the Public key to the Bluetooth specification defined debug public
39  *  key.
40  *
41  *  @param pub_key The public key to compare.
42  *
43  *  @return True if the public key is the debug public key.
44  */
45 bool bt_pub_key_is_debug(uint8_t *pub_key);
46 
47 /*  @brief Generate a new Public Key.
48  *
49  *  Generate a new ECC Public Key. Provided cb must persists until callback
50  *  is called. Callee adds the callback structure to a linked list. Registering
51  *  multiple callbacks requires multiple calls to bt_pub_key_gen() and separate
52  *  callback structures. This method cannot be called directly from result
53  *  callback. After calling all the registered callbacks the linked list
54  *  is cleared.
55  *
56  *  @param cb Callback to notify the new key.
57  *
58  *  @return Zero on success or negative error code otherwise
59  */
60 int bt_pub_key_gen(struct bt_pub_key_cb *cb);
61 
62 /*  @brief Cleanup public key callbacks when HCI is disrupted.
63  *
64  *  Clear the pub_key_cb_slist and clear the BT_DEV_PUB_KEY_BUSY flag.
65  */
66 void bt_pub_key_hci_disrupted(void);
67 
68 /*  @brief Get the current Public Key.
69  *
70  *  Get the current ECC Public Key.
71  *
72  *  @return Current key, or NULL if not available.
73  */
74 const uint8_t *bt_pub_key_get(void);
75 
76 /*  @typedef bt_dh_key_cb_t
77  *  @brief Callback type for DH Key calculation.
78  *
79  *  Used to notify of the calculated DH Key.
80  *
81  *  @param key The DH Key, or NULL in case of failure.
82  */
83 typedef void (*bt_dh_key_cb_t)(const uint8_t key[BT_DH_KEY_LEN]);
84 
85 /*  @brief Calculate a DH Key from a remote Public Key.
86  *
87  *  Calculate a DH Key from the remote Public Key.
88  *
89  *  @param remote_pk Remote Public Key.
90  *  @param cb Callback to notify the calculated key.
91  *
92  *  @return Zero on success or negative error code otherwise
93  */
94 int bt_dh_key_gen(const uint8_t remote_pk[BT_PUB_KEY_LEN], bt_dh_key_cb_t cb);
95