1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #pragma once
16 #include <pw_bluetooth/hci_commands.emb.h>
17 
18 #include <cstdint>
19 
20 #include "pw_bluetooth_sapphire/internal/host/common/uint128.h"
21 
22 namespace bt::hci_spec {
23 
24 // Represents a key used to encrypt a link.
25 class LinkKey final {
26  public:
LinkKey()27   LinkKey() : rand_(0), ediv_(0) { value_.fill(0); }
LinkKey(const UInt128 & value,uint64_t rand,uint16_t ediv)28   LinkKey(const UInt128& value, uint64_t rand, uint16_t ediv)
29       : value_(value), rand_(rand), ediv_(ediv) {}
30 
31   // 128-bit BR/EDR link key, LE Long Term Key, or LE Short Term key.
value()32   const UInt128& value() const { return value_; }
33 
34   // Encrypted diversifier and random values used to identify the LTK. These
35   // values are set to 0 for the LE Legacy STK, LE Secure Connections LTK, and
36   // BR/EDR Link Key.
rand()37   uint64_t rand() const { return rand_; }
ediv()38   uint16_t ediv() const { return ediv_; }
39 
40   bool operator!=(const LinkKey& other) const { return !(*this == other); }
41   bool operator==(const LinkKey& other) const {
42     return value() == other.value() && rand() == other.rand() &&
43            ediv() == other.ediv();
44   }
45 
view()46   auto view() { return pw::bluetooth::emboss::MakeLinkKeyView(&value_); }
47 
48  private:
49   UInt128 value_;
50   uint64_t rand_;
51   uint16_t ediv_;
52 };
53 
54 }  // namespace bt::hci_spec
55