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 <stddef.h>
9 #include <zephyr/types.h>
10 
11 #include <zephyr/bluetooth/audio/csip.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_SET_SIRK_SIZE],
31 		uint8_t r[BT_CSIP_CRYPTO_PRAND_SIZE],
32 		uint8_t out[BT_CSIP_CRYPTO_HASH_SIZE]);
33 
34 /**
35  * @brief SIRK encryption function sef
36  *
37  * The SIRK encryption function sef is used by the server to encrypt the SIRK
38  * with a key K. The value of K depends on the transport on which the pairing
39  * between the client and the server was performed.
40  *
41  * If the pairing was performed on BR/EDR, K is equal to the Link Key shared by
42  * the server and the client.
43  *    K = Link Key.
44  *
45  * If the pairing was performed on LE, the 64 LSBs of K correspond to the 64
46  * LSBs of the IRK that the server sent to the client during the Phase 3
47  * (Transport Specific Key Distribution) of the pairing procedure (see Volume 3,
48  * Part H, Section 2.1 in [2]), and the 64 MSBs of K correspond to the 64 MSBs
49  * of the LTK shared by the server and client. That is,
50  *    K = LTK_64-127 || IRK_0-63
51  *
52  * @param k         16-byte key.
53  * @param sirk      The unencrypted SIRK.
54  * @param out_sirk  The encrypted SIRK.
55  * @return int 0 on success, any other value indicates a failure.
56  */
57 int bt_csip_sef(const uint8_t k[BT_CSIP_CRYPTO_KEY_SIZE],
58 		const uint8_t sirk[BT_CSIP_SET_SIRK_SIZE],
59 		uint8_t out_sirk[BT_CSIP_SET_SIRK_SIZE]);
60 
61 /**
62  * @brief SIRK decryption function sdf
63  *
64  * The SIRK decryption function sdf is used by the client to decrypt the SIRK
65  * with a key K. The value of K depends on the transport on which the pairing
66  * between the client and the server was performed.
67  *
68  * If the pairing was performed on BR/EDR, K is equal to the Link Key shared by
69  * the server and the client.
70  *    K = Link Key.
71  *
72  * If the pairing was performed on LE, the 64 LSBs of K correspond to the 64
73  * LSBs of the IRK that the server sent to the client during the Phase 3
74  * (Transport Specific Key Distribution) of the pairing procedure (see Volume 3,
75  * Part H, Section 2.1 in [2]), and the 64 MSBs of K correspond to the 64 MSBs
76  * of the LTK shared by the server and client. That is,
77  *    K = LTK_64-127 || IRK_0-63
78  *
79  * @param k         16-byte key.
80  * @param sirk      The encrypted SIRK.
81  * @param out_sirk  The decrypted SIRK.
82  * @return int 0 on success, any other value indicates a failure.
83  */
84 int bt_csip_sdf(const uint8_t k[BT_CSIP_CRYPTO_KEY_SIZE],
85 		const uint8_t enc_sirk[BT_CSIP_SET_SIRK_SIZE],
86 		uint8_t out_sirk[BT_CSIP_SET_SIRK_SIZE]);
87