1 /**
2  * Copyright (c) 2018 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #ifndef BLE_CRYPT_IF_H
20 #define BLE_CRYPT_IF_H
21 
22 #include <stdbool.h>
23 #include <stdint.h>
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 void BLECrypt_if_encrypt_packet(uint8_t packet_first_header_byte, // First byte of packet header
30     const uint8_t* unecrypted_payload,      // Packet payload to be encrypted
31     uint8_t* encrypted_payload,  //encrypted payload (and MIC if generate_mic==1)
32     int length,        //including MIC length if ( generate_mic == 1 ) ; [ just  the length in the packet header ]
33     bool generate_mic, //we have MIC, or not
34     const uint8_t* sk, // Session key (16 bytes, BIG-ENDIAN)
35     const uint8_t* nonce   // CCM Nonce (NONCE_LEN bytes, little-endian)
36 );
37 
38 void BLECrypt_if_decrypt_packet(uint8_t packet_1st_header_byte, // First byte of packet header (or just LLID and RFU (RFU=0 for BLE v4.x) - other bits are ignored)
39     const uint8_t* encrypted_packet_payload,      //as received from the air (including a MIC if has_mic)
40     uint8_t* decrypted_packet_payload,
41     int length,       //including MIC lenght if (has_mic == 1) ; [ just  the length in the packet header ]
42     bool has_mic,
43     const uint8_t* sk,      // Session key (16 bytes, BIG-ENDIAN)
44     const uint8_t* nonce,   // CCM Nonce (NONCE_LEN bytes, little-endian)
45     uint8_t *mic_error       /*was there a mic error in the packet (only if has_mic==1)*/
46 );
47 
48 void BLECrypt_if_aes_128(
49     // Inputs
50     const uint8_t *key_be,                          // Key (KEY_LEN bytes, big-endian)
51     const uint8_t *plaintext_data_be,               // Plaintext data (KEY_LEN bytes, big-endian)
52     // Outputs (the pointers themselves are inputs and must point to large enough areas)
53     uint8_t *encrypted_data_be);                    // Plaintext data (KEY_LEN bytes, big-endian)
54 
55 void BLECrypt_if_aes_ecb(
56     // Inputs
57     const uint8_t *key_be,            // Key (KEY_LEN bytes, big-endian)
58     size_t key_size,                  // Key size in bits
59     const uint8_t *plaintext_data_be, // Plaintext data (128bits/16Bytes, big-endian)
60     // Outputs (the pointers themselves are inputs and must point to large enough areas)
61     uint8_t *encrypted_data_be);
62 
63 void BLECrypt_if_encrypt_packet_v3(uint8_t *adata,
64     int alen,
65     int mlen,
66     int maclen,
67     int noncelen,
68     const uint8_t *mdata,
69     const uint8_t *sk,
70     const uint8_t *ccm_nonce,
71     uint8_t *encrypted_packet_payload_and_mac);
72 
73 int BLECrypt_if_decrypt_packet_v3(uint8_t *adata,
74     int alen,
75     int mlen,
76     int maclen,
77     int noncelen,
78     const uint8_t *mdata_and_mac,
79     const uint8_t *sk,
80     const uint8_t *ccm_nonce,
81     int no_mac,
82     uint8_t *decrypted_packet_payload);
83 #ifdef __cplusplus
84 }
85 #endif
86 
87 #endif
88