1 /** @file
2  *  @brief Bluetooth subsystem crypto APIs.
3  */
4 
5 /*
6  * Copyright (c) 2017-2020 Nordic Semiconductor ASA
7  * Copyright (c) 2015-2017 Intel Corporation
8  *
9  * SPDX-License-Identifier: Apache-2.0
10  */
11 #ifndef ZEPHYR_INCLUDE_BLUETOOTH_CRYPTO_H_
12 #define ZEPHYR_INCLUDE_BLUETOOTH_CRYPTO_H_
13 
14 /**
15  * @brief Cryptography
16  * @defgroup bt_crypto Cryptography
17  * @ingroup bluetooth
18  * @{
19  */
20 
21 #include <stdbool.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
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_rand(void *buf, size_t len);
39 
40 /** @brief AES encrypt little-endian data.
41  *
42  *  An AES encrypt helper is used to request the Bluetooth controller's own
43  *  hardware to encrypt the plaintext using the key and returns the encrypted
44  *  data.
45  *
46  *  @param key 128 bit LS byte first key for the encryption of the plaintext
47  *  @param plaintext 128 bit LS byte first plaintext data block to be encrypted
48  *  @param enc_data 128 bit LS byte first encrypted data block
49  *
50  *  @return Zero on success or error code otherwise.
51  */
52 int bt_encrypt_le(const uint8_t key[16], const uint8_t plaintext[16],
53 		  uint8_t enc_data[16]);
54 
55 /** @brief AES encrypt big-endian data.
56  *
57  *  An AES encrypt helper is used to request the Bluetooth controller's own
58  *  hardware to encrypt the plaintext using the key and returns the encrypted
59  *  data.
60  *
61  *  @param key 128 bit MS byte first key for the encryption of the plaintext
62  *  @param plaintext 128 bit MS byte first plaintext data block to be encrypted
63  *  @param enc_data 128 bit MS byte first encrypted data block
64  *
65  *  @return Zero on success or error code otherwise.
66  */
67 int bt_encrypt_be(const uint8_t key[16], const uint8_t plaintext[16],
68 		  uint8_t enc_data[16]);
69 
70 
71 /** @brief Decrypt big-endian data with AES-CCM.
72  *
73  *  Decrypts and authorizes @c enc_data with AES-CCM, as described in
74  *  https://tools.ietf.org/html/rfc3610.
75  *
76  *  Assumes that the MIC follows directly after the encrypted data.
77  *
78  *  @param key       128 bit MS byte first key
79  *  @param nonce     13 byte MS byte first nonce
80  *  @param enc_data  Encrypted data
81  *  @param len       Length of the encrypted data
82  *  @param aad       Additional input data
83  *  @param aad_len   Additional input data length
84  *  @param plaintext Plaintext buffer to place result in
85  *  @param mic_size  Size of the trailing MIC (in bytes)
86  *
87  *  @retval 0        Successfully decrypted the data.
88  *  @retval -EINVAL  Invalid parameters.
89  *  @retval -EBADMSG Authentication failed.
90  */
91 int bt_ccm_decrypt(const uint8_t key[16], uint8_t nonce[13], const uint8_t *enc_data,
92 		   size_t len, const uint8_t *aad, size_t aad_len,
93 		   uint8_t *plaintext, size_t mic_size);
94 
95 
96 /** @brief Encrypt big-endian data with AES-CCM.
97  *
98  *  Encrypts and generates a MIC from @c plaintext with AES-CCM, as described in
99  *  https://tools.ietf.org/html/rfc3610.
100  *
101  *  Places the MIC directly after the encrypted data.
102  *
103  *  @param key       128 bit MS byte first key
104  *  @param nonce     13 byte MS byte first nonce
105  *  @param plaintext Plaintext buffer to encrypt
106  *  @param len       Length of the encrypted data
107  *  @param aad       Additional input data
108  *  @param aad_len   Additional input data length
109  *  @param enc_data  Buffer to place encrypted data in
110  *  @param mic_size  Size of the trailing MIC (in bytes)
111  *
112  *  @retval 0        Successfully encrypted the data.
113  *  @retval -EINVAL  Invalid parameters.
114  */
115 int bt_ccm_encrypt(const uint8_t key[16], uint8_t nonce[13],
116 		   const uint8_t *plaintext, size_t len, const uint8_t *aad,
117 		   size_t aad_len, uint8_t *enc_data, size_t mic_size);
118 
119 #ifdef __cplusplus
120 }
121 #endif
122 /**
123  * @}
124  */
125 
126 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_CRYPTO_H_ */
127