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 <lib/fit/function.h> 17 18 #include "pw_bluetooth_sapphire/internal/host/common/uint256.h" 19 #include "pw_bluetooth_sapphire/internal/host/hci/connection.h" 20 #include "pw_bluetooth_sapphire/internal/host/sm/error.h" 21 #include "pw_bluetooth_sapphire/internal/host/sm/smp.h" 22 #include "pw_bluetooth_sapphire/internal/host/sm/types.h" 23 24 namespace bt::sm { 25 26 // Pure abstract interface to be implemented by classes that execute 27 // Authentication Stage 1 of Phase 2 of SMP Secure Connections. The owning class 28 // will use the abstract OnPairingConfirm and OnPairingRandom methods of this 29 // class to handle any inbound Pairing Confirm / Random values. Concrete Stage 1 30 // classes are responsible for tracking the state associated with their Pairing 31 // Method. See spec V5.0 Vol. 3 Part H 2.3.5.6.2-4 (aka "the spec section") for 32 // more details. 33 class ScStage1 { 34 public: 35 // This object contains the values generated/exchanged during Stage 1 which 36 // are needed to finish SC pairing. An ScStage1 returns an |Output| through 37 // its callback upon successful completion. 38 struct Output { 39 bool operator==(const Output& other) const { 40 return initiator_r == other.initiator_r && 41 responder_r == other.responder_r && 42 initiator_rand == other.initiator_rand && 43 responder_rand == other.responder_rand; 44 } 45 46 // `ra` in the spec, associated with the initiator. Used to generate DHKey 47 // check E in SC Phase 2 Stage 2. Meaning depends on the pairing method 48 // used, see "the spec section" for details. 49 UInt128 initiator_r; 50 51 // `rb` in the spec, associated with the responder. Used to generate DHKey 52 // check E in SC Phase 2 Stage 2. Meaning depends on the pairing method 53 // used, see "the spec section" for details. 54 UInt128 responder_r; 55 56 // 'Na' in the spec; the Pairing Random value sent by the initiator. Used 57 // during SC Phase 2 Stage 2 to generate the MacKey/LTK and the DHKey check 58 // E values. 59 UInt128 initiator_rand; 60 61 // 'Na' in the spec; the Pairing Random value sent by the initiator. Used 62 // during SC Phase 2 Stage 2 to generate the MacKey/LTK and the DHKey check 63 // E values. 64 UInt128 responder_rand; 65 }; 66 67 // Used by Stage 1 classes to notify their owning class that they have 68 // finished. A successful Stage 1 notifies its owner with `Output`, or that it 69 // has failed due to `ErrorCode`. 70 using Stage1CompleteCallback = 71 fit::function<void(fit::result<ErrorCode, Output>)>; 72 73 virtual void Run() = 0; 74 virtual void OnPairingConfirm(PairingConfirmValue) = 0; 75 virtual void OnPairingRandom(PairingRandomValue) = 0; 76 virtual ~ScStage1() = default; 77 }; 78 79 } // namespace bt::sm 80