Kleidos API Search

In development. It changes without notice.

Topic

Pairing & Numeric Comparison

Who may pair, connect and receive keystrokes, and the prompt that compares six digits on both screens.

Pairing is LE Secure Connections only, with numeric comparison and a 16-byte encryption key. The six digits are shown by the board and by the other side, and the owner's answer on the board is what accepts the link. The prompt is held in one atomic value, so the screen's core never reads half of one.

Which central the board keeps once it connects is a rule of its own: with the pairing window open only one that is not bonded, and otherwise only a bonded one, or the one the send chose.

See also
BLE Facade & HID Manager, BLE Stack Integration

Files

file src/ble/security/connect_filter.h
Which central the peripheral keeps once its link is up.
file src/ble/security/eviction_confirmation.h
Bond-eviction confirmation gate (full bond table — replace oldest?).
file src/ble/security/keystore_sweep.h
Which of the host's stored records outlive a host sync: the sweep that removes what no bond owns.
file src/ble/security/numeric_comparison.h
LE Secure Connections Numeric Comparison prompt state, bound to its link and its number.
file src/ble/security/repair_confirmation.h
Re-pairing confirmation gate: a bonded host pairs again only on the owner's Allow.
file src/ble/security/store_write_gate.h
What the host's keystore may keep: a key a numeric comparison confirmed, and no eviction.

Classes

struct kleidos::ble::security::ConnectKeystoreView
What the host's keystore says about a central that just connected. More about kleidos::ble::security::ConnectKeystoreView
struct kleidos::ble::security::sweep::KeptBond
A bond the keystore holds whole: its identity and the identity key its peer record carries. More about kleidos::ble::security::sweep::KeptBond
struct kleidos::ble::security::sweep::PeerDeviceRecord
One of NimBLE's peer device records, as the sweep reads it. More about kleidos::ble::security::sweep::PeerDeviceRecord
struct kleidos::ble::security::sweep::PeerRecordPlan
What planPeerRecords decided. More about kleidos::ble::security::sweep::PeerRecordPlan
struct kleidos::ble::security::nc::Ticket
The request an answer is for: the generation it was armed with and the digits it showed. More about kleidos::ble::security::nc::Ticket
struct kleidos::ble::security::nc::Snapshot
Coherent snapshot of the prompt state for lock-free polling. More about kleidos::ble::security::nc::Snapshot
struct kleidos::ble::security::nc::Answer
Which link an answer belongs to, once it was taken. More about kleidos::ble::security::nc::Answer
struct kleidos::ble::security::repair::Snapshot
Coherent snapshot of the prompt state for lock-free polling. More about kleidos::ble::security::repair::Snapshot
struct kleidos::ble::security::store_gate::KeyRecord
What a stored security record says about the key it holds. More about kleidos::ble::security::store_gate::KeyRecord
struct kleidos::ble::security::store_gate::PeerIdentity
A peer's identity address as the store keys it. More about kleidos::ble::security::store_gate::PeerIdentity
struct kleidos::ble::security::store_gate::IdentityFacts
What the adapter knows about the identity a security record is written under. More about kleidos::ble::security::store_gate::IdentityFacts
class kleidos::ble::security::store_gate::WriteGate
The write gate: decides each security and address record the store is asked to keep. More about kleidos::ble::security::store_gate::WriteGate

Enumerations

enum class kleidos::ble::security::ConnectAdmission : uint8_t { kleidos::ble::security::ConnectAdmission::Refused , kleidos::ble::security::ConnectAdmission::Pairing , kleidos::ble::security::ConnectAdmission::SavedHost }
Why the peripheral kept a link a central just made, or that it did not. More about kleidos::ble::security::ConnectAdmission
enum class kleidos::ble::security::OwnAddress : uint8_t { kleidos::ble::security::OwnAddress::Private , kleidos::ble::security::OwnAddress::Identity , kleidos::ble::security::OwnAddress::Refused }
Which own address the advertising may use once privacy was set up (finding C27). More about kleidos::ble::security::OwnAddress
enum class kleidos::ble::security::nc::Verdict : uint8_t { kleidos::ble::security::nc::Verdict::None , kleidos::ble::security::nc::Verdict::Accepted , kleidos::ble::security::nc::Verdict::Rejected }
How the last request was answered, by whichever surface answered it. More about kleidos::ble::security::nc::Verdict
enum class kleidos::ble::security::store_gate::PersistResult : uint8_t { kleidos::ble::security::store_gate::PersistResult::Stored, kleidos::ble::security::store_gate::PersistResult::NoRoom, kleidos::ble::security::store_gate::PersistResult::StoreError, kleidos::ble::security::store_gate::PersistResult::IdentityTaken, kleidos::ble::security::store_gate::PersistResult::IdentityUnproven, kleidos::ble::security::store_gate::PersistResult::BadIdentity }
What became of a confirmed pairing's security records in the store. More about kleidos::ble::security::store_gate::PersistResult
enum class kleidos::ble::security::store_gate::KeyVerdict : uint8_t { kleidos::ble::security::store_gate::KeyVerdict::Store, kleidos::ble::security::store_gate::KeyVerdict::Unconfirmed, kleidos::ble::security::store_gate::KeyVerdict::BadIdentity, kleidos::ble::security::store_gate::KeyVerdict::IdentityTaken, kleidos::ble::security::store_gate::KeyVerdict::IdentityUnproven }
The gate's verdict on one security record. More about kleidos::ble::security::store_gate::KeyVerdict
enum class kleidos::ble::security::store_gate::RepairClaim : uint8_t { kleidos::ble::security::store_gate::RepairClaim::None , kleidos::ble::security::store_gate::RepairClaim::Proven , kleidos::ble::security::store_gate::RepairClaim::Unproven }
Whether a re-pair the owner allowed claims the identity a peer record is written under. More about kleidos::ble::security::store_gate::RepairClaim
enum class kleidos::ble::security::store_gate::StoreEvent : uint8_t { kleidos::ble::security::store_gate::StoreEvent::Overflow , kleidos::ble::security::store_gate::StoreEvent::Full , kleidos::ble::security::store_gate::StoreEvent::Other }
The store events the host reports to its status callback. More about kleidos::ble::security::store_gate::StoreEvent
enum class kleidos::ble::security::store_gate::StoreRecord : uint8_t { kleidos::ble::security::store_gate::StoreRecord::DeviceKey, kleidos::ble::security::store_gate::StoreRecord::PeerKey, kleidos::ble::security::store_gate::StoreRecord::PeerAddress, kleidos::ble::security::store_gate::StoreRecord::Subscription, kleidos::ble::security::store_gate::StoreRecord::ClientFeatures, kleidos::ble::security::store_gate::StoreRecord::Other }
The kinds of record the host's store keeps, as the gate tells them apart. More about kleidos::ble::security::store_gate::StoreRecord

Functions

constexpr ConnectAdmissionkleidos::ble::security::connectAdmission (bool pairingWindowOpen, const ConnectKeystoreView &keystore)
Whether, and why, the peripheral keeps a link a central just made.
constexpr boolkleidos::ble::security::admitConnection (bool pairingWindowOpen, const ConnectKeystoreView &keystore)
Whether the peripheral keeps a link a central just made.
constexpr boolkleidos::ble::security::encryptionDeadlineApplies (ConnectAdmission admission)
Whether a link gets the encryption-start deadline at its connection (finding C17).
constexpr boolkleidos::ble::security::restoredKeyAdmitted (bool freshPairing, bool forgotten)
Whether a link that just encrypted may read as a host's.
constexpr boolkleidos::ble::security::sendReachesNoHost (bool hostMayPair, bool targetSet, bool targetKeyless, uint8_t listed, uint8_t keyless)
Whether a send can reach no host at all, so it ends at once.
constexpr boolkleidos::ble::security::typingTargetAllows (bool targetSet, bool repairTargetKept, bool peerIsTarget)
Whether a send may type into the live, authenticated link.
voidkleidos::ble::security::eviction::resetForTest ()
Native-only: clear state between tests.
constexpr boolkleidos::ble::security::sweep::sameIrk (const std::array< uint8_t, kIrkBytes > &a, const std::array< uint8_t, kIrkBytes > &b)
Whether two identity keys are equal, read in constant time.
constexpr std::array< uint8_t, kIrkBytes >kleidos::ble::security::sweep::swapIrkOrder (std::span< const uint8_t, kIrkBytes > key)
Turn an identity key between the keystore's byte order and a peer device record's.
constexpr boolkleidos::ble::security::sweep::recordOrphaned (const std::array< uint8_t, 6 > &bytes, std::span< const KeptBond > kept)
Whether a stored record under bytes belongs to no kept bond.
constexpr PeerRecordPlankleidos::ble::security::sweep::planPeerRecords (std::span< const PeerDeviceRecord > records, std::span< const KeptBond > kept, std::span< bool > keep, std::span< bool > needs)
Decide which peer device records stay, and which kept bonds need one.
constexpr boolkleidos::ble::security::reportLinkSecure (bool encrypted, bool authenticated, bool bonded, uint8_t keySize)
True when the live link satisfies the HOGP security bar.
constexpr boolkleidos::ble::security::securityStartFailed (int rc)
Whether the security the peripheral asks for at connection failed to start.
constexpr boolkleidos::ble::security::encryptionDeadlineMissed (bool armed, bool reached, bool encrypted, bool repairHoldsLink)
Whether a link that had the encryption-start deadline is ended for missing it.
constexpr uint64_tkleidos::ble::security::packEncryptionDeadline (uint16_t handle, uint32_t atMs)
One link's encryption-start deadline, packed into one word.
constexpr uint16_tkleidos::ble::security::encryptionDeadlineHandle (uint64_t packed)
The link a packed encryption-start deadline belongs to.
constexpr uint32_tkleidos::ble::security::encryptionDeadlineAtMs (uint64_t packed)
When a packed encryption-start deadline runs out.
constexpr OwnAddresskleidos::ble::security::ownAddressAfterPrivacySetup (bool privacyOn, bool failClosed)
The own address the advertising uses after the privacy setup.
Verdictkleidos::ble::security::nc::lastVerdict ()
How the last request armed was answered.
Answerkleidos::ble::security::nc::answer (const Ticket &request, Verdict verdict)
Take the owner's answer to request, if it is still the request pending.
voidkleidos::ble::security::nc::resetForTest ()
Native-only: clear state between tests.
voidkleidos::ble::security::repair::resetForTest ()
Native-only: clear state between tests.
constexpr boolkleidos::ble::security::store_gate::keyConfirmed (const KeyRecord &record)
True when record is a key this firmware's pairing can produce.
constexpr boolkleidos::ble::security::store_gate::identityShapeValid (const PeerIdentity &peer, std::span< const std::array< uint8_t, 6 > > own)
True when peer is an identity a host can have.
constexpr boolkleidos::ble::security::store_gate::bondRecordsConfirmed (bool ownConfirmed, bool peerFound, bool peerConfirmed)
Whether a bond in the keystore holds a key a confirmed pairing left whole.
constexpr boolkleidos::ble::security::store_gate::unstoredDropsKeys (PersistResult result)
Whether the records an unstored pairing may have left are deleted.
constexpr boolkleidos::ble::security::store_gate::recordNeedsConfirmedKey (StoreRecord record)
Whether a record of record's kind is kept only for an identity with a confirmed key.
constexpr uint8_tkleidos::ble::security::store_gate::identityTypeOf (uint8_t addrType)
The identity address type a record's address type names.
constexpr boolkleidos::ble::security::store_gate::storeEventProceeds (StoreEvent event)
Whether the host may go on after a store event.

Variables

constexpr std::size_tkleidos::ble::security::sweep::kIrkBytes
Bytes of an identity resolving key.
constexpr uint8_tkleidos::ble::security::kMinLinkKeyBytes
constexpr intkleidos::ble::security::kSecurityAlreadyRunning
constexpr uint32_tkleidos::ble::security::kEncryptionStartDeadlineMs
constexpr uint64_tkleidos::ble::security::kNoEncryptionDeadline
constexpr uint16_tkleidos::ble::security::nc::kNoConnHandle
The connection handle no link has (NimBLE's BLE_HS_CONN_HANDLE_NONE).
constexpr uint32_tkleidos::ble::security::repair::kPromptCooldownMs
How long, after a prompt that ended without an Allow, no new prompt is raised.
constexpr uint8_tkleidos::ble::security::store_gate::kIdentityPublic
Address type of a public identity.
constexpr uint8_tkleidos::ble::security::store_gate::kIdentityRandom
Address type of a random (static) identity.
constexpr uint8_tkleidos::ble::security::store_gate::kIdentityRandomBit
The bit of an address type that says the identity is a random one.

Eviction confirmation: prompt lifecycle (Core 1 auth-drain)

voidkleidos::ble::security::eviction::armPrompt (const uint8_t addr[6], uint8_t addrType)
Arm an eviction prompt for a candidate identity (current timestamp).
voidkleidos::ble::security::eviction::armPromptAt (const uint8_t addr[6], uint8_t addrType, uint32_t requestMs)
Arm an eviction prompt with an explicit request timestamp.
voidkleidos::ble::security::eviction::clear ()
Discard any pending eviction prompt (deny).

Eviction confirmation: state queries (any task)

boolkleidos::ble::security::eviction::pending ()
True while a bond-eviction decision is awaited.
boolkleidos::ble::security::eviction::candidate (uint8_t outAddr[6], uint8_t &outType)
Read the candidate identity awaiting an eviction decision.

Eviction confirmation: timeout helpers

boolkleidos::ble::security::eviction::timedOut (uint32_t timeoutMs)
True if the prompt aged out relative to the current time.
boolkleidos::ble::security::eviction::timedOutAt (uint32_t timeoutMs, uint32_t nowMs)
Timeout check at an explicit nowMs.

Numeric comparison: prompt lifecycle (Core 0)

voidkleidos::ble::security::nc::armRequest (uint16_t connHandle, uint32_t passkey)
Arm a Numeric Comparison prompt for connHandle with the current timestamp.
voidkleidos::ble::security::nc::armRequestAt (uint16_t connHandle, uint32_t passkey, uint32_t requestMs)
Arm a Numeric Comparison prompt with an explicit request timestamp.
boolkleidos::ble::security::nc::withdraw (uint16_t connHandle)
Withdraw the request connHandle raised: its link ended or its encryption failed.
voidkleidos::ble::security::nc::clear ()
Withdraw any pending request (the host reset or the stack stopped).

Numeric comparison: state queries (any task)

boolkleidos::ble::security::nc::pending ()
True while a Numeric Comparison decision is awaited.
uint32_tkleidos::ble::security::nc::passkey ()
Current 6-digit passkey, or 0 if no prompt is active.
Snapshotkleidos::ble::security::nc::snapshot ()
Coherent copy of the prompt state.
Ticketkleidos::ble::security::nc::ticket ()
The request pending now, as an answer must name it.
boolkleidos::ble::security::nc::pendingFor (const Ticket &request)
True while request is the request pending now.
uint16_tkleidos::ble::security::nc::linkOf (const Ticket &request)
The link request was armed for, while it is the last request armed.
boolkleidos::ble::security::nc::armedFor (uint16_t connHandle, uint16_t generation)
True while the last request armed is generation generation on link connHandle.
Verdictkleidos::ble::security::nc::verdictFor (const Ticket &request)
How request was answered.

Numeric comparison: timeout helpers

boolkleidos::ble::security::nc::timedOut (uint32_t timeoutMs)
True if the prompt aged out relative to the current time.
boolkleidos::ble::security::nc::timedOutAt (uint32_t timeoutMs, uint32_t nowMs)
Timeout check at an explicit nowMs.
Ticketkleidos::ble::security::nc::timedOutTicket (uint32_t timeoutMs)
The request that aged out, read in the same load as its age.
Ticketkleidos::ble::security::nc::timedOutTicketAt (uint32_t timeoutMs, uint32_t nowMs)
timedOutTicket at an explicit nowMs.

Re-pair confirmation: prompt lifecycle (Core 0)

boolkleidos::ble::security::repair::armPrompt (bool encryptedLink)
Arm a re-pair confirmation prompt with the current timestamp.
boolkleidos::ble::security::repair::armPromptAt (uint32_t requestMs, bool encryptedLink)
Arm a re-pair confirmation prompt with an explicit request timestamp.
boolkleidos::ble::security::repair::mayPromptAt (uint32_t nowMs)
Whether a prompt may be raised at nowMs.
voidkleidos::ble::security::repair::clear ()
Discard any pending prompt and any armed approval (deny).
voidkleidos::ble::security::repair::linkDropped ()
The link the prompt stood for dropped.

Re-pair confirmation: state queries (any task)

boolkleidos::ble::security::repair::pending ()
True while a re-pair confirmation decision is awaited.
Snapshotkleidos::ble::security::repair::snapshot ()
Coherent copy of the prompt state.

Re-pair confirmation: timeout helpers

boolkleidos::ble::security::repair::timedOut (uint32_t timeoutMs)
True if the prompt aged out relative to the current time.
boolkleidos::ble::security::repair::timedOutAt (uint32_t timeoutMs, uint32_t nowMs)
Timeout check at an explicit nowMs.

Re-pair confirmation: decision (any task)

voidkleidos::ble::security::repair::approve ()
Approve the re-pair: clear the prompt and arm the approval.
voidkleidos::ble::security::repair::decline ()
Decline the re-pair: clear the prompt and any armed grant.
voidkleidos::ble::security::repair::declineAt (uint32_t nowMs)
Decline at an explicit time: as decline, the cooldown from nowMs.

Back to the top

Enumeration Type Documentation

ConnectAdmission #

enum class kleidos::ble::security::ConnectAdmission : uint8_t

Why the peripheral kept a link a central just made, or that it did not.

Enumerator
Refused 

Terminate it.

Pairing 

Kept for a pairing: the window is open, or nobody is bonded or listed.

SavedHost 

Kept as a bonded host's identity (the chosen one when a send chose one).

KeyVerdict #

enum class kleidos::ble::security::store_gate::KeyVerdict : uint8_t

The gate's verdict on one security record.

Enumerator
Store 

Keep it.

Unconfirmed 

No comparison confirmed the key.

BadIdentity 

The identity is not one a host can have.

IdentityTaken 

The store holds another pairing's key under the same six bytes.

IdentityUnproven 

The re-pairing host's bytes without its identity key.

OwnAddress #

enum class kleidos::ble::security::OwnAddress : uint8_t

Which own address the advertising may use once privacy was set up (finding C27).

Enumerator
Private 

Privacy is on: a resolvable private address.

Identity 

Privacy failed: the identity address, on a build that allows it.

Refused 

Privacy failed on a build that must not show its identity: no advertising.

PersistResult #

enum class kleidos::ble::security::store_gate::PersistResult : uint8_t

What became of a confirmed pairing's security records in the store.

Enumerator
Stored 

Every record the gate admitted was kept (or none was written).

NoRoom 

The store was out of room for one of them.

StoreError 

The store failed one of them for another reason.

IdentityTaken 

Its identity is a saved host's: refused, nothing of it stored.

IdentityUnproven 

It claimed the re-pairing host's identity without that host's identity key: its peer record refused, its device record deleted.

BadIdentity 

Its identity is no host's (all zeros, own, private): nothing stored.

RepairClaim #

enum class kleidos::ble::security::store_gate::RepairClaim : uint8_t

Whether a re-pair the owner allowed claims the identity a peer record is written under.

Enumerator
None 

No re-pair waits for these bytes (or the record is the device's own).

Proven 

The record carries the identity key the old bond held.

Unproven 

The bytes are the re-pairing host's, the identity key is not.

StoreEvent #

enum class kleidos::ble::security::store_gate::StoreEvent : uint8_t

The store events the host reports to its status callback.

Enumerator
Overflow 

A write did not fit; the host asks for room to retry it.

Full 

A pairing is about to start with no free keystore slot.

Other 

Any other event code.

StoreRecord #

enum class kleidos::ble::security::store_gate::StoreRecord : uint8_t

The kinds of record the host's store keeps, as the gate tells them apart.

Enumerator
DeviceKey 

The device's security record (BLE_STORE_OBJ_TYPE_OUR_SEC).

PeerKey 

The peer's security record (BLE_STORE_OBJ_TYPE_PEER_SEC).

PeerAddress 

The private address mapped to the identity (PEER_ADDR).

Subscription 

A characteristic subscription (BLE_STORE_OBJ_TYPE_CCCD).

ClientFeatures 

The client's supported features (BLE_STORE_OBJ_TYPE_CSFC).

Other 

Any other record (the device's own identity key, for one).

Verdict #

enum class kleidos::ble::security::nc::Verdict : uint8_t

How the last request was answered, by whichever surface answered it.

Enumerator
None 

Not answered: still pending, or it lapsed, was withdrawn or dropped unanswered.

Accepted 

The digits were confirmed.

Rejected 

The comparison was refused.

Back to the top

Function Documentation

admitConnection() #

bool kleidos::ble::security::admitConnection(bool pairingWindowOpen, const ConnectKeystoreView & keystore)
nodiscardconstexpr

Whether the peripheral keeps a link a central just made.

Parameters
NameDirectionMeaning
pairingWindowOpeninThe "add a host" pairing window is open.
keystoreinWhat the keystore says about the central.
Return values
ValueMeaning
trueKeep the link (connectAdmission is not Refused).
falseTerminate it.

answer() #

Answer kleidos::ble::security::nc::answer(const Ticket & request, Verdict verdict)

Take the owner's answer to request, if it is still the request pending.

The answer is taken in one step with the check, so two surfaces answering at once, or a request replaced while its answer travels, never answer a request the owner did not see.

Parameters
NameDirectionMeaning
requestinThe request the answer was given to.
verdictinVerdict::Accepted or Verdict::Rejected.
Returns
Whether it was taken, and the link to inject it into.

approve() #

void kleidos::ble::security::repair::approve()

Approve the re-pair: clear the prompt and arm the approval.

The approval stands while the host's old bond is deleted, its link dropped and its fresh pairing awaited; clear ends it (that host's fresh pairing completed, the session stopped), and no other host's link does. It keeps the time the prompt was armed (Snapshot::requestMs), as a drop of the link does, so the approval's age reads from the request.

armedFor() #

bool kleidos::ble::security::nc::armedFor(uint16_t connHandle, uint16_t generation)

True while the last request armed is generation generation on link connHandle.

The check an answer is injected under: a link that reused a handle and reached its own comparison armed a newer generation, so an answer taken for the earlier request never reaches it.

Parameters
NameDirectionMeaning
connHandleinThe link the answer is for.
generationinThe generation the answer was taken for.
Return values
ValueMeaning
trueNo request was armed since that one.
falseA newer request was armed, on that link or another.

armPrompt() [1/2] #

void kleidos::ble::security::eviction::armPrompt(const uint8_t addr[6], uint8_t addrType)

Arm an eviction prompt for a candidate identity (current timestamp).

Parameters
NameDirectionMeaning
addrin6-byte candidate (new host) identity address.
addrTypein0 = public, 1 = random.

armPrompt() [2/2] #

bool kleidos::ble::security::repair::armPrompt(bool encryptedLink)

Arm a re-pair confirmation prompt with the current timestamp.

Parameters
NameMeaning
encryptedLinkThe request came over an encrypted link.
Return values
ValueMeaning
trueThe prompt is armed.
falseA prompt ended without an Allow less than kPromptCooldownMs ago: nothing is armed, and the request is refused with no prompt.

armPromptAt() [1/2] #

void kleidos::ble::security::eviction::armPromptAt(const uint8_t addr[6], uint8_t addrType, uint32_t requestMs)

Arm an eviction prompt with an explicit request timestamp.

Parameters
NameDirectionMeaning
addrin6-byte candidate identity address.
addrTypein0 = public, 1 = random.
requestMsinMonotonic milliseconds at which the request was received.

armPromptAt() [2/2] #

bool kleidos::ble::security::repair::armPromptAt(uint32_t requestMs, bool encryptedLink)

Arm a re-pair confirmation prompt with an explicit request timestamp.

Parameters
NameMeaning
requestMsMonotonic milliseconds at which the request was received.
encryptedLinkThe request came over an encrypted link.
Return values
ValueMeaning
trueThe prompt is armed.
falseThe cooldown after the last prompt that ended without an Allow still runs at requestMs: nothing is armed.

armRequest() #

void kleidos::ble::security::nc::armRequest(uint16_t connHandle, uint32_t passkey)

Arm a Numeric Comparison prompt for connHandle with the current timestamp.

Parameters
NameMeaning
connHandleThe connection that raised the comparison.
passkey6-digit comparison value provided by NimBLE.

armRequestAt() #

void kleidos::ble::security::nc::armRequestAt(uint16_t connHandle, uint32_t passkey, uint32_t requestMs)

Arm a Numeric Comparison prompt with an explicit request timestamp.

A request armed while another is pending replaces it: the earlier one is no longer pending, and no answer given to it is taken.

Parameters
NameMeaning
connHandleThe connection that raised the comparison.
passkey6-digit comparison value provided by NimBLE.
requestMsMonotonic milliseconds at which the request was received.

bondRecordsConfirmed() #

bool kleidos::ble::security::store_gate::bondRecordsConfirmed(bool ownConfirmed, bool peerFound, bool peerConfirmed)
nodiscardconstexpr

Whether a bond in the keystore holds a key a confirmed pairing left whole.

NimBLE lists its bonds from the device's own records, and the device record alone carries the link key a reconnect encrypts with. A bond is a host's only when the peer's record is stored beside it and both hold a confirmed key: a device record whose peer record never arrived (refused, not stored, or lost with the power between the two writes) is no host's, and the next host sync deletes it instead of adopting it.

Parameters
NameDirectionMeaning
ownConfirmedinThe device's record holds a confirmed key.
peerFoundinThe peer's record is stored.
peerConfirmedinThe peer's record holds a confirmed key.
Returns
true only when all three hold.

candidate() #

bool kleidos::ble::security::eviction::candidate(uint8_t outAddr[6], uint8_t & outType)

Read the candidate identity awaiting an eviction decision.

Parameters
NameDirectionMeaning
outAddrout6-byte candidate identity address (written on success).
outTypeoutCandidate address type (0 = public, 1 = random).
Return values
ValueMeaning
trueA prompt is armed; outAddr / outType are populated.
falseNo prompt is armed; the outputs are untouched.

clear() #

void kleidos::ble::security::nc::clear()

Withdraw any pending request (the host reset or the stack stopped).

The verdict of a request already answered stays readable.

connectAdmission() #

ConnectAdmission kleidos::ble::security::connectAdmission(bool pairingWindowOpen, const ConnectKeystoreView & keystore)
nodiscardconstexpr

Whether, and why, the peripheral keeps a link a central just made.

Parameters
NameDirectionMeaning
pairingWindowOpeninThe "add a host" pairing window is open.
keystoreinWhat the keystore says about the central.
Return values
ValueMeaning
ConnectAdmission::PairingA stranger while the window is open, anyone while the window is open and the keystore could not be read, and anyone while nobody is bonded (no key in the keystore and no host in the table): a pairing is what the link is for.
ConnectAdmission::SavedHostA bonded host (the chosen one, when a send chose one) while the window is closed: it holds a stored key.
ConnectAdmission::RefusedAnyone else: a forgotten host, and an unreadable keystore with the window closed, included.

decline() #

void kleidos::ble::security::repair::decline()

Decline the re-pair: clear the prompt and any armed grant.

A prompt it ends starts the cooldown (kPromptCooldownMs).

declineAt() #

void kleidos::ble::security::repair::declineAt(uint32_t nowMs)

Decline at an explicit time: as decline, the cooldown from nowMs.

Parameters
NameMeaning
nowMsMonotonic milliseconds.

encryptionDeadlineApplies() #

bool kleidos::ble::security::encryptionDeadlineApplies(ConnectAdmission admission)
nodiscardconstexpr

Whether a link gets the encryption-start deadline at its connection (finding C17).

A link kept as a saved host claims a bonded identity, and a bonded host encrypts with its stored key as soon as it connects: a central replaying that identity cannot, and is ended at the deadline. A pairing cannot be raised on such a link: a bonded identity's Pairing Request is NimBLE's repeat pairing, which the re-pair gate (E4) always refuses, so its link waits only on the owner's re-pair prompt, which the deadline leaves alone. A link kept for a pairing gets no deadline: its pairing waits on the owner's numeric comparison, bounded by NimBLE's security timer and the session's own.

Parameters
NameDirectionMeaning
admissioninWhat connectAdmission decided for the link.
Return values
ValueMeaning
trueArm the deadline.
falseNo deadline: a pairing may run on this link (or it is refused).

encryptionDeadlineAtMs() #

uint32_t kleidos::ble::security::encryptionDeadlineAtMs(uint64_t packed)
nodiscardconstexpr

When a packed encryption-start deadline runs out.

Parameters
NameMeaning
packedA word from packEncryptionDeadline.
Returns
Its clock::millis32 time, never 0.

encryptionDeadlineHandle() #

uint16_t kleidos::ble::security::encryptionDeadlineHandle(uint64_t packed)
nodiscardconstexpr

The link a packed encryption-start deadline belongs to.

Parameters
NameMeaning
packedA word from packEncryptionDeadline.
Returns
Its connection handle.

encryptionDeadlineMissed() #

bool kleidos::ble::security::encryptionDeadlineMissed(bool armed, bool reached, bool encrypted, bool repairHoldsLink)
nodiscardconstexpr

Whether a link that had the encryption-start deadline is ended for missing it.

A bonded host whose key the keystore lacks asks to pair again, and the owner's re-pair prompt (E4) then holds that link under its own timeout; the deadline leaves that link to the prompt. An Allow ends the prompt on the UI task before the BLE stack task drops the link for it (dropRepairLink, 0x13), so the link stays the re-pair's until then too: its drop always follows the opening of the pairing window the asking host comes back through, never a deadline's 0x05 in between.

Parameters
NameMeaning
armedThe link has the deadline.
reachedThe deadline has passed.
encryptedIts encryption completed.
repairHoldsLinkA re-pair prompt the owner sees was raised on this link, or the owner allowed it and the link's drop for it has not run yet.
Return values
ValueMeaning
trueEnd the link.
falseLeave it.

identityShapeValid() #

bool kleidos::ble::security::store_gate::identityShapeValid(const PeerIdentity & peer, std::span< const std::array< uint8_t, 6 > > own)
nodiscardconstexpr

True when peer is an identity a host can have.

Refused: a type other than public or random, the all-zero and the all-one address, any of the device's own addresses (whatever the type), and a random address that is not a static one (the two top bits of the most significant byte set, the 46 random bits neither all zero nor all one). A private address there means the peer distributed no identity, and a key stored under it could never be found again.

Parameters
NameDirectionMeaning
peerinThe identity a record is about to be stored under.
owninThe device's own addresses (public and random), as NimBLE keeps them; an all-zero entry stands for one it does not have.
Returns
true when the identity may hold a key.

identityTypeOf() #

uint8_t kleidos::ble::security::store_gate::identityTypeOf(uint8_t addrType)
nodiscardconstexpr

The identity address type a record's address type names.

NimBLE writes some records with the connection's identity type (2 for a public identity, 3 for a random one) and stores the keys with 0 and 1; the low bit is the identity's type in both spellings.

Parameters
NameDirectionMeaning
addrTypeinThe type a record carries.
Returns
0 for a public identity, 1 for a random one.

keyConfirmed() #

bool kleidos::ble::security::store_gate::keyConfirmed(const KeyRecord & record)
nodiscardconstexpr

True when record is a key this firmware's pairing can produce.

Parameters
NameDirectionMeaning
recordinThe record about to be stored.
Returns
true only for an authenticated Secure Connections long-term key of at least kMinLinkKeyBytes.

lastVerdict() #

Verdict kleidos::ble::security::nc::lastVerdict()

How the last request armed was answered.

Kept past the answer (the request is no longer pending) until the next request is armed, so a prompt that closed because another surface answered reads which answer stands.

Returns
The last request's verdict, Verdict::None while it is pending or when it ended unanswered.

linkDropped() #

void kleidos::ble::security::repair::linkDropped()

The link the prompt stood for dropped.

A pending prompt goes with it: it stands for that link only, and a new peer must raise (and the owner answer) a fresh one. An approval stays: dropping the link is what it asked for, and the host's fresh pairing is still awaited.

linkOf() #

uint16_t kleidos::ble::security::nc::linkOf(const Ticket & request)

The link request was armed for, while it is the last request armed.

Parameters
NameDirectionMeaning
requestinThe request a dialog showed.
Returns
Its connection handle, or kNoConnHandle once a newer request was armed (or for an invalid ticket).

mayPromptAt() #

bool kleidos::ble::security::repair::mayPromptAt(uint32_t nowMs)

Whether a prompt may be raised at nowMs.

Parameters
NameMeaning
nowMsMonotonic milliseconds.
Return values
ValueMeaning
trueNo cooldown runs.
falseA prompt ended without an Allow less than kPromptCooldownMs ago.

ownAddressAfterPrivacySetup() #

OwnAddress kleidos::ble::security::ownAddressAfterPrivacySetup(bool privacyOn, bool failClosed)
nodiscardconstexpr

The own address the advertising uses after the privacy setup.

An identity address is the same on every advertisement, so anyone in range can follow the device. A shipping build (_release, _secure) never falls back to it: it does not advertise at all, and the session fails. An open build keeps the fallback, so a bench can still reach a board whose controller refused privacy.

Parameters
NameMeaning
privacyOnThe privacy setup succeeded.
failClosedThe build may not fall back to the identity address.
Returns
The address the advertising takes, or OwnAddress::Refused.

packEncryptionDeadline() #

uint64_t kleidos::ble::security::packEncryptionDeadline(uint16_t handle, uint32_t atMs)
nodiscardconstexpr

One link's encryption-start deadline, packed into one word.

The handle and the time travel together, so a reader never pairs one link's time with another link's handle: a link that ends and a new one that connects between two separate reads cannot inherit an expired deadline.

Parameters
NameMeaning
handleThe link's connection handle.
atMsWhen its time runs out (clock::millis32 time); 0 is taken as 1.
Returns
The packed word, never kNoEncryptionDeadline.

passkey() #

uint32_t kleidos::ble::security::nc::passkey()

Current 6-digit passkey, or 0 if no prompt is active.

Returns
The passkey value, or 0 when not in Numeric Comparison.

pending() [1/3] #

bool kleidos::ble::security::eviction::pending()

True while a bond-eviction decision is awaited.

Return values
ValueMeaning
trueA prompt has been armed but not yet resolved.
falseNo prompt is currently armed.

pending() [2/3] #

bool kleidos::ble::security::nc::pending()

True while a Numeric Comparison decision is awaited.

Return values
ValueMeaning
trueA prompt has been armed but not yet resolved.
falseNo prompt is currently armed.

pending() [3/3] #

bool kleidos::ble::security::repair::pending()

True while a re-pair confirmation decision is awaited.

Return values
ValueMeaning
trueA prompt has been armed but not yet resolved.
falseNo prompt is currently armed.

pendingFor() #

bool kleidos::ble::security::nc::pendingFor(const Ticket & request)

True while request is the request pending now.

Parameters
NameDirectionMeaning
requestinThe request a dialog shows.
Return values
ValueMeaning
trueIt is pending: the dialog still asks.
falseIt was answered, withdrawn, or a newer request replaced it.

planPeerRecords() #

PeerRecordPlan kleidos::ble::security::sweep::planPeerRecords(std::span< const PeerDeviceRecord > records, std::span< const KeptBond > kept, std::span< bool > keep, std::span< bool > needs)
nodiscardconstexpr

Decide which peer device records stay, and which kept bonds need one.

A record stays when it carries an identity and an identity key, a kept bond has that identity, that bond's peer record holds the same key, and no earlier record already stays for that bond. Every other record is removed. A kept bond with an identity key and no record that stays is marked as needing one.

Parameters
NameDirectionMeaning
recordsinThe peer device records, in the list's order.
keptinThe bonds the keystore holds whole.
keepoutOne flag per record: true when it stays (sized as records).
needsoutOne flag per kept bond: true when it needs a record (sized as kept).
Returns
How many records go and how many bonds need one.

recordNeedsConfirmedKey() #

bool kleidos::ble::security::store_gate::recordNeedsConfirmedKey(StoreRecord record)
nodiscardconstexpr

Whether a record of record's kind is kept only for an identity with a confirmed key.

A peer's address record, its subscriptions and its supported features belong to a bonded host. NimBLE writes the last two for every link that bonded, whatever the association model, right after the encryption change (ble_gatts_bonding_established): a Just Works stranger the adapter refuses leaves its supported-features record, and a subscription to Service Changed made in the clear, under an identity with no key, which no forget and no reconcile would ever find (finding C5). Each is kept only when a confirmed key is stored for its identity and the pairing writing it did not just have its key refused (WriteGate::admitBoundRecord).

Parameters
NameDirectionMeaning
recordinThe record's kind.
Return values
ValueMeaning
trueIts identity must hold a confirmed key.
falseIt is a security record (judged by WriteGate::decideKey) or the device's own.

recordOrphaned() #

bool kleidos::ble::security::sweep::recordOrphaned(const std::array< uint8_t, 6 > & bytes, std::span< const KeptBond > kept)
nodiscardconstexpr

Whether a stored record under bytes belongs to no kept bond.

Parameters
NameDirectionMeaning
bytesinThe six bytes of the identity the record is stored under.
keptinThe bonds the keystore holds whole.
Return values
ValueMeaning
trueNo kept bond has these bytes: the record is deleted.
falseA kept bond owns it.

reportLinkSecure() #

bool kleidos::ble::security::reportLinkSecure(bool encrypted, bool authenticated, bool bonded, uint8_t keySize)
nodiscardconstexpr

True when the live link satisfies the HOGP security bar.

Fail-closed: any field that falls short makes the whole predicate false, so callers that gate credential keystrokes on it never emit over a link that has lost encryption/authentication.

Parameters
NameMeaning
encryptedLink-layer encryption is active.
authenticatedPairing was MITM-authenticated (Numeric Comparison).
bondedA bond (LTK/IRK) is stored for the peer.
keySizeNegotiated encryption-key length in bytes.
Returns
true only if encrypted && authenticated && bonded && keySize >= kMinLinkKeyBytes.

restoredKeyAdmitted() #

bool kleidos::ble::security::restoredKeyAdmitted(bool freshPairing, bool forgotten)
nodiscardconstexpr

Whether a link that just encrypted may read as a host's.

A link that paired fresh (a numeric comparison ran on it) carries a key the owner just confirmed. A link that restored a stored key is a forgotten host's when the table keeps a tombstone for its identity: the key was to be deleted and the delete has not taken, so the link is dropped before it reads as authenticated (finding C9).

Parameters
NameDirectionMeaning
freshPairinginThe link paired here.
forgotteninThe table holds a tombstone for the link's identity.
Returns
true when the link may read as authenticated.

sameIrk() #

bool kleidos::ble::security::sweep::sameIrk(const std::array< uint8_t, kIrkBytes > & a, const std::array< uint8_t, kIrkBytes > & b)
nodiscardconstexpr

Whether two identity keys are equal, read in constant time.

Parameters
NameDirectionMeaning
ainOne key.
binThe other.
Returns
true when every byte matches.

securityStartFailed() #

bool kleidos::ble::security::securityStartFailed(int rc)
nodiscardconstexpr

Whether the security the peripheral asks for at connection failed to start.

The peripheral asks for security when the connection event reaches it (ble_gap_security_initiate). NimBLE delivers that event only once it has read the central's version and features, and runs the SMP exchange in the meantime: a central that pairs as soon as it connects (BlueZ's pair, some Windows stacks) has started its pairing before the event, and the request returns kSecurityAlreadyRunning. That pairing is the one asked for and goes on under the same rules: Secure Connections only, the MITM bit, the numeric comparison, and an unauthenticated result refused at the encryption change. Any other non-zero code means no security can start on the link, which is then ended.

Parameters
NameMeaning
rcThe code ble_gap_security_initiate() returned.
Return values
ValueMeaning
trueIt failed: the link is ended.
falseSecurity started, or a procedure already runs on the link.

sendReachesNoHost() #

bool kleidos::ble::security::sendReachesNoHost(bool hostMayPair, bool targetSet, bool targetKeyless, uint8_t listed, uint8_t keyless)
nodiscardconstexpr

Whether a send can reach no host at all, so it ends at once.

A host whose row the table lists but whose key the keystore lost (a keyless row: an image before the write gate, a forget whose row a power loss brought back, a key dropped on the bench) can never connect: the link check needs its key. A send for it waited the whole session and timed out with no word (findings C10 and C44). It ends at once, and the owner is told the host must be paired again.

Never while a host may still pair: a re-pair the owner allowed has deleted the asking host's old bond and row (and cleared the target) before the host comes back to pair fresh, and the pairing window admits a new host. Ending the send there would cut the re-pair short and lose that host's row.

Parameters
NameDirectionMeaning
hostMayPairinA re-pair the owner allowed is waiting for its host, or the pairing window is open.
targetSetinThe send chose one host.
targetKeylessinThat host's row has no key.
listedinHosts the table lists.
keylessinOf them, rows with no key.
Return values
ValueMeaning
trueNo listed host the send may reach has a key.
falseA host may still connect or pair (or the table lists none: the first pairing).

snapshot() [1/2] #

Snapshot kleidos::ble::security::nc::snapshot()

Coherent copy of the prompt state.

Returns
Snapshot of the current NC state.

snapshot() [2/2] #

Snapshot kleidos::ble::security::repair::snapshot()

Coherent copy of the prompt state.

Returns
Snapshot of the current re-pair state (pending, approved, requestMs).

storeEventProceeds() #

bool kleidos::ble::security::store_gate::storeEventProceeds(StoreEvent event)
nodiscardconstexpr

Whether the host may go on after a store event.

Parameters
NameDirectionMeaning
eventinThe event the host reported.
Return values
ValueMeaning
trueGo on: a "full" warning before a pairing.
falseRefuse: an overflow is never answered by evicting a bond.

swapIrkOrder() #

std::array< uint8_t, kIrkBytes > kleidos::ble::security::sweep::swapIrkOrder(std::span< const uint8_t, kIrkBytes > key)
nodiscardconstexpr

Turn an identity key between the keystore's byte order and a peer device record's.

The security manager stores a distributed key in the keystore as it came (ble_sm_fill_store_value copies it) and into the peer device record byte-reversed (ble_sm_persist_keys copies it with swap_buf). The turn is its own inverse, so one call reads a record's key into the keystore's order and another writes a kept bond's key into a record.

Parameters
NameDirectionMeaning
keyinThe key in one order.
Returns
The same key in the other.

ticket() #

Ticket kleidos::ble::security::nc::ticket()

The request pending now, as an answer must name it.

Returns
Its generation and digits, or an invalid ticket when none is pending.

timedOut() [1/3] #

bool kleidos::ble::security::eviction::timedOut(uint32_t timeoutMs)

True if the prompt aged out relative to the current time.

Parameters
NameMeaning
timeoutMsMaximum prompt lifetime in milliseconds.
Return values
ValueMeaning
trueThe elapsed time since the request exceeds timeoutMs.
falseThe prompt is still within timeoutMs, or none is armed.

timedOut() [2/3] #

bool kleidos::ble::security::nc::timedOut(uint32_t timeoutMs)

True if the prompt aged out relative to the current time.

Parameters
NameMeaning
timeoutMsMaximum prompt lifetime in milliseconds.
Return values
ValueMeaning
trueThe elapsed time since the request exceeds timeoutMs.
falseThe prompt is still within timeoutMs, or no prompt is armed.

timedOut() [3/3] #

bool kleidos::ble::security::repair::timedOut(uint32_t timeoutMs)

True if the prompt aged out relative to the current time.

Parameters
NameMeaning
timeoutMsMaximum prompt lifetime in milliseconds.
Return values
ValueMeaning
trueThe elapsed time since the request exceeds timeoutMs.
falseThe prompt is still within timeoutMs, or no prompt is armed.

timedOutAt() [1/3] #

bool kleidos::ble::security::eviction::timedOutAt(uint32_t timeoutMs, uint32_t nowMs)

Timeout check at an explicit nowMs.

Parameters
NameMeaning
timeoutMsMaximum prompt lifetime in milliseconds.
nowMsCurrent monotonic timestamp in milliseconds.
Return values
ValueMeaning
true(nowMs - requestMs) exceeds timeoutMs.
falseThe prompt is still within timeoutMs, or none is armed.

timedOutAt() [2/3] #

bool kleidos::ble::security::nc::timedOutAt(uint32_t timeoutMs, uint32_t nowMs)

Timeout check at an explicit nowMs.

Parameters
NameMeaning
timeoutMsMaximum prompt lifetime in milliseconds.
nowMsCurrent monotonic timestamp in milliseconds.
Return values
ValueMeaning
true(nowMs - requestMs) exceeds timeoutMs.
falseThe prompt is still within timeoutMs, or no prompt is armed.

timedOutAt() [3/3] #

bool kleidos::ble::security::repair::timedOutAt(uint32_t timeoutMs, uint32_t nowMs)

Timeout check at an explicit nowMs.

Parameters
NameMeaning
timeoutMsMaximum prompt lifetime in milliseconds.
nowMsCurrent monotonic timestamp in milliseconds.
Return values
ValueMeaning
true(nowMs - requestMs) exceeds timeoutMs.
falseThe prompt is still within timeoutMs, or no prompt is armed.

timedOutTicket() #

Ticket kleidos::ble::security::nc::timedOutTicket(uint32_t timeoutMs)

The request that aged out, read in the same load as its age.

Parameters
NameMeaning
timeoutMsMaximum prompt lifetime in milliseconds.
Returns
Its ticket, to reject that request and no later one; an invalid ticket when nothing pending aged out.

timedOutTicketAt() #

Ticket kleidos::ble::security::nc::timedOutTicketAt(uint32_t timeoutMs, uint32_t nowMs)

timedOutTicket at an explicit nowMs.

Parameters
NameMeaning
timeoutMsMaximum prompt lifetime in milliseconds.
nowMsCurrent monotonic timestamp in milliseconds.
Returns
The ticket of the pending request older than timeoutMs, or an invalid one.

typingTargetAllows() #

bool kleidos::ble::security::typingTargetAllows(bool targetSet, bool repairTargetKept, bool peerIsTarget)
nodiscardconstexpr

Whether a send may type into the live, authenticated link.

With a target chosen, only that host's link; with none, any bonded host's. While a re-pair the owner allowed keeps the target for its host (BondStore::repairTargetKept), nothing: a central paired meanwhile, even one the owner confirmed, is never typed to before the asking host is back (finding C4).

Parameters
NameDirectionMeaning
targetSetinA target slot is chosen.
repairTargetKeptinA re-pair holds the target for its host.
peerIsTargetinThe live, authenticated link is the target's.
Returns
true when typing may start.

unstoredDropsKeys() #

bool kleidos::ble::security::store_gate::unstoredDropsKeys(PersistResult result)
nodiscardconstexpr

Whether the records an unstored pairing may have left are deleted.

A pairing refused for an identity that is a saved host's, or that no host can have, stored nothing: deleting by its bytes would delete the saved host's keys (or an unlisted replace-oldest candidate's). A pairing the store ran out of room for, failed, or refused at its peer record after storing its device record left records of its own.

Parameters
NameDirectionMeaning
resultinThe pairing's persist result.
Returns
true when its records are deleted by its identity's bytes.

verdictFor() #

Verdict kleidos::ble::security::nc::verdictFor(const Ticket & request)

How request was answered.

Parameters
NameDirectionMeaning
requestinThe request a dialog showed.
Returns
Its verdict while it is the last request armed; Verdict::None while it is pending, when it ended unanswered, or once a newer request replaced it.

withdraw() #

bool kleidos::ble::security::nc::withdraw(uint16_t connHandle)

Withdraw the request connHandle raised: its link ended or its encryption failed.

A request another link raised is left as it is, and so is the verdict of a request already answered.

Parameters
NameMeaning
connHandleThe link that went.
Return values
ValueMeaning
trueThe pending request was that link's and is withdrawn.
falseNothing was pending for that link.

Back to the top

Variable Documentation

kEncryptionStartDeadlineMs #

uint32_t kleidos::ble::security::kEncryptionStartDeadlineMs
inlineconstexpr

How long a link kept as a saved host has to start its encryption, from its connection event (finding C17). Such a link claims a bonded identity (ConnectAdmission::SavedHost, encryptionDeadlineApplies in connect_filter.h), and a bonded host encrypts with its stored key as soon as it connects; a central that replays a bonded host's address cannot, and would otherwise hold the device's one link until NimBLE's own 30 s security timer ends it.

kMinLinkKeyBytes #

uint8_t kleidos::ble::security::kMinLinkKeyBytes
inlineconstexpr

Minimum negotiated encryption-key length (bytes) for LE Secure Connections. A shorter key indicates a KNOB/BLUFFS-style downgrade and is rejected.

kNoEncryptionDeadline #

uint64_t kleidos::ble::security::kNoEncryptionDeadline
inlineconstexpr

No encryption-start deadline is armed (the packed word of packEncryptionDeadline is never this value).

kSecurityAlreadyRunning #

int kleidos::ble::security::kSecurityAlreadyRunning
inlineconstexpr

The code ble_gap_security_initiate() returns when a security procedure (a pairing, or the encryption with a stored key) already runs on the link: NimBLE's BLE_HS_EALREADY. The raw adapter checks the two agree.

Back to the top