1 /*
2  * Copyright (c) 2019 Bose Corporation
3  * Copyright (c) 2021-2022 Nordic Semiconductor ASA
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <stdint.h>
9 
10 #include <zephyr/bluetooth/audio/csip.h>
11 #include <zephyr/types.h>
12 
13 #define BT_CSIP_CRYPTO_KEY_SIZE   16
14 #define BT_CSIP_CRYPTO_SALT_SIZE  16
15 #define BT_CSIP_CRYPTO_PRAND_SIZE 3
16 #define BT_CSIP_CRYPTO_HASH_SIZE  3
17 
18 /**
19  * @brief Private Set Unique identifier hash function sih.
20  *
21  * The RSI hash function sih is used to generate a hash value that is
22  * used in RSIs - Used by the Coordinated Set Identification service and
23  * profile.
24  *
25  * @param sirk  16 byte LS byte first SIRK
26  * @param r     3 byte LS byte first random value
27  * @param out   3 byte LS byte first output buffer
28  * @return int 0 on success, any other value indicates a failure.
29  */
30 int bt_csip_sih(const uint8_t sirk[BT_CSIP_SIRK_SIZE], uint8_t r[BT_CSIP_CRYPTO_PRAND_SIZE],
31 		uint8_t out[BT_CSIP_CRYPTO_HASH_SIZE]);
32 
33 /**
34  * @brief SIRK encryption function sef
35  *
36  * The SIRK encryption function sef is used by the server to encrypt the SIRK
37  * with a key K. The value of K depends on the transport on which the pairing
38  * between the client and the server was performed.
39  *
40  * If the pairing was performed on BR/EDR, K is equal to the Link Key shared by
41  * the server and the client.
42  *    K = Link Key.
43  *
44  * If the pairing was performed on LE, the 64 LSBs of K correspond to the 64
45  * LSBs of the IRK that the server sent to the client during the Phase 3
46  * (Transport Specific Key Distribution) of the pairing procedure (see Volume 3,
47  * Part H, Section 2.1 in [2]), and the 64 MSBs of K correspond to the 64 MSBs
48  * of the LTK shared by the server and client. That is,
49  *    K = LTK_64-127 || IRK_0-63
50  *
51  * @param k         16-byte key.
52  * @param sirk      The unencrypted SIRK.
53  * @param out_sirk  The encrypted SIRK.
54  * @return int 0 on success, any other value indicates a failure.
55  */
56 int bt_csip_sef(const uint8_t k[BT_CSIP_CRYPTO_KEY_SIZE], const uint8_t sirk[BT_CSIP_SIRK_SIZE],
57 		uint8_t out_sirk[BT_CSIP_SIRK_SIZE]);
58 
59 /**
60  * @brief SIRK decryption function sdf
61  *
62  * The SIRK decryption function sdf is used by the client to decrypt the SIRK
63  * with a key K. The value of K depends on the transport on which the pairing
64  * between the client and the server was performed.
65  *
66  * If the pairing was performed on BR/EDR, K is equal to the Link Key shared by
67  * the server and the client.
68  *    K = Link Key.
69  *
70  * If the pairing was performed on LE, the 64 LSBs of K correspond to the 64
71  * LSBs of the IRK that the server sent to the client during the Phase 3
72  * (Transport Specific Key Distribution) of the pairing procedure (see Volume 3,
73  * Part H, Section 2.1 in [2]), and the 64 MSBs of K correspond to the 64 MSBs
74  * of the LTK shared by the server and client. That is,
75  *    K = LTK_64-127 || IRK_0-63
76  *
77  * @param k         16-byte key.
78  * @param sirk      The encrypted SIRK.
79  * @param out_sirk  The decrypted SIRK.
80  * @return int 0 on success, any other value indicates a failure.
81  */
82 int bt_csip_sdf(const uint8_t k[BT_CSIP_CRYPTO_KEY_SIZE], const uint8_t enc_sirk[BT_CSIP_SIRK_SIZE],
83 		uint8_t out_sirk[BT_CSIP_SIRK_SIZE]);
84