1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #pragma once
18
19 #include <array>
20 #include <cstdint>
21 #include <cstring>
22
23 #include "hci/octets.h"
24
25 namespace crypto_toolbox {
26
27 bluetooth::hci::Octet16 c1(const bluetooth::hci::Octet16& k, const bluetooth::hci::Octet16& r,
28 const uint8_t* pres, const uint8_t* preq, const uint8_t iat,
29 const uint8_t* ia, const uint8_t rat, const uint8_t* ra);
30 bluetooth::hci::Octet16 s1(const bluetooth::hci::Octet16& k, const bluetooth::hci::Octet16& r1,
31 const bluetooth::hci::Octet16& r2);
32
33 bluetooth::hci::Octet16 aes_128(const bluetooth::hci::Octet16& key,
34 const bluetooth::hci::Octet16& message);
35 bluetooth::hci::Octet16 aes_cmac(const bluetooth::hci::Octet16& key, const uint8_t* message,
36 uint16_t length);
37 bluetooth::hci::Octet16 f4(const uint8_t* u, const uint8_t* v, const bluetooth::hci::Octet16& x,
38 uint8_t z);
39 void f5(const uint8_t* w, const bluetooth::hci::Octet16& n1, const bluetooth::hci::Octet16& n2,
40 uint8_t* a1, uint8_t* a2, bluetooth::hci::Octet16* mac_key, bluetooth::hci::Octet16* ltk);
41 bluetooth::hci::Octet16 f6(const bluetooth::hci::Octet16& w, const bluetooth::hci::Octet16& n1,
42 const bluetooth::hci::Octet16& n2, const bluetooth::hci::Octet16& r,
43 uint8_t* iocap, uint8_t* a1, uint8_t* a2);
44 bluetooth::hci::Octet16 h6(const bluetooth::hci::Octet16& w, std::array<uint8_t, 4> keyid);
45 bluetooth::hci::Octet16 h7(const bluetooth::hci::Octet16& salt, const bluetooth::hci::Octet16& w);
46 uint32_t g2(const uint8_t* u, const uint8_t* v, const bluetooth::hci::Octet16& x,
47 const bluetooth::hci::Octet16& y);
48 bluetooth::hci::Octet16 ltk_to_link_key(const bluetooth::hci::Octet16& ltk, bool use_h7);
49 bluetooth::hci::Octet16 link_key_to_ltk(const bluetooth::hci::Octet16& link_key, bool use_h7);
50
51 // |tlen| - length of mac desired
52 // |p_signature| - data pointer to where signed data to be stored, tlen long.
aes_cmac(const bluetooth::hci::Octet16 & key,const uint8_t * message,uint16_t length,uint16_t tlen,uint8_t * p_signature)53 inline void aes_cmac(const bluetooth::hci::Octet16& key, const uint8_t* message, uint16_t length,
54 uint16_t tlen, uint8_t* p_signature) {
55 bluetooth::hci::Octet16 signature = aes_cmac(key, message, length);
56
57 uint8_t* p_mac = signature.data() + (bluetooth::hci::kOctet16Length - tlen);
58 memcpy(p_signature, p_mac, tlen);
59 }
60
aes_cmac(const bluetooth::hci::Octet16 & key,const bluetooth::hci::Octet16 & message)61 inline bluetooth::hci::Octet16 aes_cmac(const bluetooth::hci::Octet16& key,
62 const bluetooth::hci::Octet16& message) {
63 return aes_cmac(key, message.data(), message.size());
64 }
65
66 } // namespace crypto_toolbox
67