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 namespace bluetooth {
24 namespace crypto_toolbox {
25 
26 constexpr int OCTET16_LEN = 16;
27 using Octet16 = std::array<uint8_t, OCTET16_LEN>;
28 
29 Octet16 c1(
30     const Octet16& k,
31     const Octet16& r,
32     const uint8_t* pres,
33     const uint8_t* preq,
34     const uint8_t iat,
35     const uint8_t* ia,
36     const uint8_t rat,
37     const uint8_t* ra);
38 Octet16 s1(const Octet16& k, const Octet16& r1, const Octet16& r2);
39 
40 extern Octet16 aes_128(const Octet16& key, const Octet16& message);
41 extern Octet16 aes_cmac(const Octet16& key, const uint8_t* message, uint16_t length);
42 extern Octet16 f4(uint8_t* u, uint8_t* v, const Octet16& x, uint8_t z);
43 extern void f5(
44     uint8_t* w, const Octet16& n1, const Octet16& n2, uint8_t* a1, uint8_t* a2, Octet16* mac_key, Octet16* ltk);
45 extern Octet16 f6(
46     const Octet16& w, const Octet16& n1, const Octet16& n2, const Octet16& r, uint8_t* iocap, uint8_t* a1, uint8_t* a2);
47 extern Octet16 h6(const Octet16& w, std::array<uint8_t, 4> keyid);
48 extern Octet16 h7(const Octet16& salt, const Octet16& w);
49 extern uint32_t g2(uint8_t* u, uint8_t* v, const Octet16& x, const Octet16& y);
50 extern Octet16 ltk_to_link_key(const Octet16& ltk, bool use_h7);
51 extern Octet16 link_key_to_ltk(const Octet16& link_key, bool use_h7);
52 
53 /* This function computes AES_128(key, message). |key| must be 128bit.
54  * |message| can be at most 16 bytes long, it's length in bytes is given in
55  * |length| */
aes_128(const Octet16 & key,const uint8_t * message,const uint8_t length)56 inline Octet16 aes_128(const Octet16& key, const uint8_t* message, const uint8_t length) {
57   // CHECK(length <= OCTET16_LEN) << "you tried aes_128 more than 16 bytes!";
58   Octet16 msg{0};
59   std::copy(message, message + length, msg.begin());
60   return aes_128(key, msg);
61 }
62 
63 // |tlen| - lenth of mac desired
64 // |p_signature| - data pointer to where signed data to be stored, tlen long.
aes_cmac(const Octet16 & key,const uint8_t * message,uint16_t length,uint16_t tlen,uint8_t * p_signature)65 inline void aes_cmac(const Octet16& key, const uint8_t* message, uint16_t length, uint16_t tlen, uint8_t* p_signature) {
66   Octet16 signature = aes_cmac(key, message, length);
67 
68   uint8_t* p_mac = signature.data() + (OCTET16_LEN - tlen);
69   memcpy(p_signature, p_mac, tlen);
70 }
71 
aes_cmac(const Octet16 & key,const Octet16 & message)72 inline Octet16 aes_cmac(const Octet16& key, const Octet16& message) {
73   return aes_cmac(key, message.data(), message.size());
74 }
75 
76 }  // namespace crypto_toolbox
77 }  // namespace bluetooth
78