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 cmp_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 *cmp_pub_key);
46 
47 /*  @brief Check if public key is valid.
48  *
49  *  Verify that the public key is valid, e.g. that its coordinates lie on the eliptic curve.
50  *
51  *  @param key The public key to validate.
52  *
53  *  @return True if the public key is valid.
54  */
55 bool bt_pub_key_is_valid(const uint8_t key[BT_PUB_KEY_LEN]);
56 
57 /*  @brief Generate a new Public Key.
58  *
59  *  Generate a new ECC Public Key. Provided cb must persists until callback
60  *  is called. Callee adds the callback structure to a linked list. Registering
61  *  multiple callbacks requires multiple calls to bt_pub_key_gen() and separate
62  *  callback structures. This method cannot be called directly from result
63  *  callback. After calling all the registered callbacks the linked list
64  *  is cleared.
65  *
66  *  @param cb Callback to notify the new key.
67  *
68  *  @return Zero on success or negative error code otherwise
69  */
70 int bt_pub_key_gen(struct bt_pub_key_cb *cb);
71 
72 /*  @brief Cleanup public key callbacks when HCI is disrupted.
73  *
74  *  Clear the pub_key_cb_slist and clear the BT_DEV_PUB_KEY_BUSY flag.
75  */
76 void bt_pub_key_hci_disrupted(void);
77 
78 /*  @brief Get the current Public Key.
79  *
80  *  Get the current ECC Public Key.
81  *
82  *  @return Current key, or NULL if not available.
83  */
84 const uint8_t *bt_pub_key_get(void);
85 
86 /*  @typedef bt_dh_key_cb_t
87  *  @brief Callback type for DH Key calculation.
88  *
89  *  Used to notify of the calculated DH Key.
90  *
91  *  @param key The DH Key, or NULL in case of failure.
92  */
93 typedef void (*bt_dh_key_cb_t)(const uint8_t key[BT_DH_KEY_LEN]);
94 
95 /*  @brief Calculate a DH Key from a remote Public Key.
96  *
97  *  Calculate a DH Key from the remote Public Key.
98  *
99  *  @param remote_pk Remote Public Key.
100  *  @param cb Callback to notify the calculated key.
101  *
102  *  @return Zero on success or negative error code otherwise
103  */
104 int bt_dh_key_gen(const uint8_t remote_pk[BT_PUB_KEY_LEN], bt_dh_key_cb_t cb);
105