1 /*
2  * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3  */
4 
5 #ifndef _MESH_HAL_SEC_H_
6 #define _MESH_HAL_SEC_H_
7 
8 #include <stdint.h>
9 
10 typedef void (*bt_mesh_dh_key_cb_t)(const uint8_t key[32]);
11 
12 /*  @brief Container for public key callback */
13 struct bt_mesh_pub_key_cb {
14     /** @brief Callback type for Public Key generation.
15          *
16          *  Used to notify of the local public key or that the local key is not
17          *  available (either because of a failure to read it or because it is
18          *  being regenerated).
19          *
20          *  @param key The local public key, or NULL in case of no key.
21          */
22     void (*func)(const uint8_t key[64]);
23 
24     struct bt_mesh_pub_key_cb *_next;
25 };
26 
27 /** @brief Generate random data.
28  *
29  *  A random number generation helper which utilizes the Bluetooth
30  *  controller's own RNG.
31  *
32  *  @param buf Buffer to insert the random data
33  *  @param len Length of random data to generate
34  *
35  *  @return Zero on success or error code otherwise, positive in case
36  *  of protocol error or negative (POSIX) in case of stack internal error
37  */
38 int bt_mesh_rand(void *buf, size_t len);
39 
40 /** @brief AES-128 ECB
41  *
42  *  A function used to perform AES-128 ECB encryption.
43  *  See Core Spec V4.2, Vol 3, Part H, Section 2.2.1.
44  *
45  *  @param key AES key
46  *  @param plaintext The data before encryption
47  *  @param enc_data The data after encryption
48  *
49  *  @return 0 on sucess, otherwise negative number
50  */
51 int bt_mesh_aes_encrypt(const uint8_t key[16], const uint8_t plaintext[16], uint8_t enc_data[16]);
52 
53 int bt_mesh_aes_decrypt(const uint8_t key[16], const uint8_t enc_data[16], uint8_t dec_data[16]);
54 
55 /*  @brief Get the current Public Key.
56  *
57  *  Get the current ECC Public Key.
58  *
59  *  @return Current key, or NULL if not available.
60  */
61 const uint8_t *bt_mesh_pub_key_get(void);
62 
63 /*  @brief Calculate a DH Key from a remote Public Key.
64  *
65  *  Calculate a DH Key from the remote Public Key.
66  *
67  *  @param remote_pk Remote Public Key.
68  *  @param cb Callback to notify the calculated key.
69  *
70  *  @return Zero on success or negative error code otherwise
71  */
72 int bt_mesh_dh_key_gen(const uint8_t remote_pk[64], bt_mesh_dh_key_cb_t cb);
73 
74 /*  @brief Generate a new Public Key.
75  *
76  *  Generate a new ECC Public Key. The callback will persist even after the
77  *  key has been generated, and will be used to notify of new generation
78  *  processes (NULL as key).
79  *
80  *  @param cb Callback to notify the new key, or NULL to request an update
81  *            without registering any new callback.
82  *
83  *  @return Zero on success or negative error code otherwise
84  */
85 int bt_mesh_pub_key_gen(struct bt_mesh_pub_key_cb *cb);
86 
87 #endif /* _MESH_HAL_SEC_H_ */
88