blob: aa714adb1ccfc46f6e2f879f23df18f07cbd67f0 [file] [log] [blame]
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef GARNET_DRIVERS_BLUETOOTH_LIB_SM_UTIL_H_
#define GARNET_DRIVERS_BLUETOOTH_LIB_SM_UTIL_H_
#include "garnet/drivers/bluetooth/lib/common/device_address.h"
#include "garnet/drivers/bluetooth/lib/common/uint128.h"
#include "garnet/drivers/bluetooth/lib/sm/smp.h"
namespace btlib {
namespace sm {
namespace util {
// Returns a string representation of a given pairing method.
std::string PairingMethodToString(PairingMethod method);
// Returns a string representation of a given IOCapability.
std::string IOCapabilityToString(IOCapability capability);
// Used to select the key generation method as described in Vol 3, Part H,
// 2.3.5.1 based on local and peer authentication parameters:
// - |secure_connections|: True if Secure Connections pairing is used. False
// means Legacy Pairing.
// - |local_oob|: Local OOB auth data is available.
// - |peer_oob|: Peer OOB auth data is available.
// - |mitm_required|: True means at least one of the devices requires MITM
// protection.
// - |local_ioc|, |peer_ioc|: Local and peer IO capabilities.
// - |local_initiator|: True means that the local device is the initiator and
// |local_ioc| represents the initiator's I/O capabilities.
PairingMethod SelectPairingMethod(bool secure_connections, bool local_oob,
bool peer_oob, bool mitm_required,
IOCapability local_ioc, IOCapability peer_ioc,
bool local_initiator);
// Implements the "Security Function 'e'" defined in Vol 3, Part H, 2.2.1.
void Encrypt(const common::UInt128& key, const common::UInt128& plaintext_data,
common::UInt128* out_encrypted_data);
// Implements the "Confirm Value Generation" or "c1" function for LE Legacy
// Pairing described in Vol 3, Part H, 2.2.3.
//
// |tk|: 128-bit TK value
// |rand|: 128-bit random number
// |preq|: 56-bit SMP "Pairing Request" PDU
// |pres|: 56-bit SMP "Pairing Response" PDU
// |initiator_addr|: Device address of the initiator used while establishing
// the connection.
// |responder_addr|: Device address of the responder used while establishing
// the connection.
//
// The generated confirm value will be returned in |out_confirm_value|.
void C1(const common::UInt128& tk, const common::UInt128& rand,
const common::ByteBuffer& preq, const common::ByteBuffer& pres,
const common::DeviceAddress& initiator_addr,
const common::DeviceAddress& responder_addr,
common::UInt128* out_confirm_value);
// Implements the "Key Generation Function s1" to generate the STK for LE Legacy
// Pairing described in Vol 3, Part H, 2.2.4.
//
// |tk|: 128-bit TK value
// |r1|: 128-bit random value generated by the responder.
// |r2|: 128-bit random value generated by the initiator.
void S1(const common::UInt128& tk, const common::UInt128& r1,
const common::UInt128& r2, common::UInt128* out_stk);
// Implements the "Random Address Hash Function ah" to resolve RPAs. Described
// in Vol 3, Part H, 222.
//
// |k|: 128-bit IRK value
// |r|: 24-bit random part of a RPA.
//
// Returns 24 bit hash value.
uint32_t Ah(const common::UInt128& k, uint32_t r);
// Returns true if the given |irk| can resolve the given |rpa| using the method
// described in Vol 6, Part B, 1.3.2.3.
bool IrkCanResolveRpa(const common::UInt128& irk,
const common::DeviceAddress& rpa);
// Generates a RPA using the given IRK based on the method described in Vol 6,
// Part B, 1.3.2.2.
common::DeviceAddress GenerateRpa(const common::UInt128& irk);
// Generates a static or non-resolvable private random device address.
common::DeviceAddress GenerateRandomAddress(bool is_static);
} // namespace util
} // namespace sm
} // namespace btlib
#endif // GARNET_DRIVERS_BLUETOOTH_LIB_SM_UTIL_H_