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(
28 const bluetooth::hci::Octet16& k,
29 const bluetooth::hci::Octet16& r,
30 const uint8_t* pres,
31 const uint8_t* preq,
32 const uint8_t iat,
33 const uint8_t* ia,
34 const uint8_t rat,
35 const uint8_t* ra);
36 bluetooth::hci::Octet16 s1(
37 const bluetooth::hci::Octet16& k,
38 const bluetooth::hci::Octet16& r1,
39 const bluetooth::hci::Octet16& r2);
40
41 bluetooth::hci::Octet16 aes_128(
42 const bluetooth::hci::Octet16& key, const bluetooth::hci::Octet16& message);
43 bluetooth::hci::Octet16 aes_cmac(
44 const bluetooth::hci::Octet16& key, const uint8_t* message, uint16_t length);
45 bluetooth::hci::Octet16 f4(
46 const uint8_t* u, const uint8_t* v, const bluetooth::hci::Octet16& x, uint8_t z);
47 void f5(
48 const uint8_t* w,
49 const bluetooth::hci::Octet16& n1,
50 const bluetooth::hci::Octet16& n2,
51 uint8_t* a1,
52 uint8_t* a2,
53 bluetooth::hci::Octet16* mac_key,
54 bluetooth::hci::Octet16* ltk);
55 bluetooth::hci::Octet16 f6(
56 const bluetooth::hci::Octet16& w,
57 const bluetooth::hci::Octet16& n1,
58 const bluetooth::hci::Octet16& n2,
59 const bluetooth::hci::Octet16& r,
60 uint8_t* iocap,
61 uint8_t* a1,
62 uint8_t* a2);
63 bluetooth::hci::Octet16 h6(const bluetooth::hci::Octet16& w, std::array<uint8_t, 4> keyid);
64 bluetooth::hci::Octet16 h7(const bluetooth::hci::Octet16& salt, const bluetooth::hci::Octet16& w);
65 uint32_t g2(
66 const uint8_t* u,
67 const uint8_t* v,
68 const bluetooth::hci::Octet16& x,
69 const bluetooth::hci::Octet16& y);
70 bluetooth::hci::Octet16 ltk_to_link_key(const bluetooth::hci::Octet16& ltk, bool use_h7);
71 bluetooth::hci::Octet16 link_key_to_ltk(const bluetooth::hci::Octet16& link_key, bool use_h7);
72
73 // |tlen| - lenth of mac desired
74 // |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)75 inline void aes_cmac(
76 const bluetooth::hci::Octet16& key,
77 const uint8_t* message,
78 uint16_t length,
79 uint16_t tlen,
80 uint8_t* p_signature) {
81 bluetooth::hci::Octet16 signature = aes_cmac(key, message, length);
82
83 uint8_t* p_mac = signature.data() + (bluetooth::hci::kOctet16Length - tlen);
84 memcpy(p_signature, p_mac, tlen);
85 }
86
aes_cmac(const bluetooth::hci::Octet16 & key,const bluetooth::hci::Octet16 & message)87 inline bluetooth::hci::Octet16 aes_cmac(
88 const bluetooth::hci::Octet16& key, const bluetooth::hci::Octet16& message) {
89 return aes_cmac(key, message.data(), message.size());
90 }
91
92 } // namespace crypto_toolbox
93