git clone https://garage44.eu/jeroen/pinnacle.gitcartopolis-agent · 2026-11-30T16:30:39Z
feat(bt): pair by a passkey shown on one side and typed on the other LE Secure Connections passkey entry (Vol 3 Part H 2.3.5.6.3): where one side has a keyboard and the other a display, the display shows a six-digit passkey and its person types it on the keyboard; twenty rounds each commit to a bit of it, so a device in the middle cannot fake it any more than a compared number. The method follows the spec's table for every pair of IO capabilities, either side showing or both typing. The host says Event::Passkey (show it, or ask for it) and takes Host::enter_passkey; confirm(no) cancels. A keyboard asking for a code used to be refused as not supported.
bt/src/host.rs | 32 ++++++
bt/src/lib.rs | 3 +-
bt/src/smp.rs | 270 ++++++++++++++++++++++++++++++++++++++++++-------
bt/src/tests.rs | 141 +++++++++++++++++++++++++-
programs/src/bin/bt.rs | 2 +
5 files changed, 408 insertions(+), 40 deletions(-)
diff --git a/bt/src/host.rs b/bt/src/host.rs
index fdb323d..909e179 100644
--- a/bt/src/host.rs
+++ b/bt/src/host.rs
@@ -82,6 +82,15 @@ pub enum Event {
peer: Address,
code: Option<u32>,
},
+ /// Pairing by passkey: show `passkey` to this device's person, to be
+ /// typed on the other device (a keyboard), or, with `None`, ask them to
+ /// type the one the other device shows ([`Host::enter_passkey`]).
+ /// [`Host::confirm`] with no cancels either.
+ Passkey {
+ connection: u16,
+ peer: Address,
+ passkey: Option<u32>,
+ },
/// Paired, the link encrypted with the new key: the bond to keep.
Paired {
connection: u16,
@@ -482,6 +491,19 @@ impl Host {
self.smp_outputs(connection, out);
}
+ /// The passkey the person typed, asked for by [`Event::Passkey`].
+ pub fn enter_passkey(&mut self, connection: u16, passkey: u32) {
+ let now = self.now;
+ let Some(out) = self
+ .connection(connection)
+ .and_then(|c| c.pairing.as_mut())
+ .map(|p| p.enter(passkey, now))
+ else {
+ return;
+ };
+ self.smp_outputs(connection, out);
+ }
+
/// The person's answer to [`Event::Confirm`].
pub fn confirm(&mut self, connection: u16, yes: bool) {
let now = self.now;
@@ -523,6 +545,16 @@ impl Host {
peer,
code,
}),
+ smp::Output::Show(passkey) => self.events.push_back(Event::Passkey {
+ connection,
+ peer,
+ passkey: Some(passkey),
+ }),
+ smp::Output::Type => self.events.push_back(Event::Passkey {
+ connection,
+ peer,
+ passkey: None,
+ }),
smp::Output::Done { key, compared } => {
if let Some(c) = self.connection(connection) {
c.paired = Some((key, compared));
diff --git a/bt/src/lib.rs b/bt/src/lib.rs
index 879e44c..4b9505d 100644
--- a/bt/src/lib.rs
+++ b/bt/src/lib.rs
@@ -13,7 +13,8 @@
//! GATT server (a table of services this device offers: its name, its
//! make, and the Pinnacle card for contacts nearby). It pairs by LE
//! Secure Connections ([`smp`]: a six-digit number compared on both
-//! screens, or "just works" with a device that has none), after which the
+//! screens, a passkey shown on one and typed on the other where one is a
+//! keyboard, or "just works" with a device that has neither), after which the
//! controllers encrypt the link, and a bonded device's link is encrypted
//! again on every connection with the key kept ([`Bond`]); the two hand
//! each other their identity as they pair, so a paired device that moves to
diff --git a/bt/src/smp.rs b/bt/src/smp.rs
index 38d9914..e047e8b 100644
--- a/bt/src/smp.rs
+++ b/bt/src/smp.rs
@@ -4,8 +4,12 @@
//! long-term key the controllers encrypt the link with; with a display on
//! both sides each shows a six-digit number and its person says whether
//! they match (numeric comparison), which a device in the middle cannot
-//! fake; with none on one side the pairing is "just works", private from
-//! listeners but not proof against a device in the middle.
+//! fake; where one side has a keyboard and the other a display (a keyboard
+//! pairing with a phone), one shows a six-digit passkey and its person types
+//! it on the other (passkey entry), proved a bit a round over twenty rounds
+//! so a device in the middle cannot fake that either; with nothing to show
+//! or type on one side the pairing is "just works", private from listeners
+//! but not proof against a device in the middle.
//!
//! Once the link is encrypted the two hand each other their identity
//! (Vol 3 Part H 3.6.4): an identity resolving key (IRK) and the address
@@ -15,8 +19,8 @@
//! ([`resolves`]).
//!
//! What is not here: legacy pairing (before Bluetooth 4.2: its key is
-//! found from a recorded pairing, so it is refused), a passkey typed on
-//! one side (a keyboard pairing), out-of-band data, and signing keys.
+//! found from a recorded pairing, so it is refused), out-of-band data, and
+//! signing keys.
//!
//! Bytes are as they go over the air, least significant first, the
//! functions too (Linux's convention): `cmac` turns them round for AES.
@@ -37,6 +41,7 @@ pub const IDENTITY_INFORMATION: u8 = 0x08;
pub const IDENTITY_ADDRESS_INFORMATION: u8 = 0x09;
// Why a pairing failed.
+pub const PASSKEY_ENTRY_FAILED: u8 = 0x01;
pub const AUTHENTICATION_REQUIREMENTS: u8 = 0x03;
pub const CONFIRM_VALUE_FAILED: u8 = 0x04;
pub const NOT_SUPPORTED: u8 = 0x05;
@@ -70,8 +75,10 @@ pub const TIMEOUT_MS: u64 = 30_000;
pub fn reason_text(reason: u8) -> &'static str {
match reason {
AUTHENTICATION_REQUIREMENTS => "it only pairs the old, unsafe way",
- CONFIRM_VALUE_FAILED | DHKEY_CHECK_FAILED => "the keys did not match",
- NOT_SUPPORTED => "it wants a code typed, which is not done yet",
+ PASSKEY_ENTRY_FAILED => "the code was not typed",
+ CONFIRM_VALUE_FAILED => "the code typed was not the one shown, or the keys did not match",
+ DHKEY_CHECK_FAILED => "the keys did not match",
+ NOT_SUPPORTED => "it cannot pair",
NUMERIC_COMPARISON_FAILED => "the numbers were not confirmed",
TIMED_OUT => "no answer in time",
_ => "it refused",
@@ -184,8 +191,15 @@ pub enum Output {
/// (numeric comparison), or, with `None`, may it pair at all (just
/// works, the peer asking). [`Pairing::confirm`] answers.
Ask(Option<u32>),
+ /// Passkey entry: show this passkey to this device's person, to be
+ /// typed on the other device. [`Pairing::confirm`] with no cancels.
+ Show(u32),
+ /// Passkey entry: this device's person is to type the passkey the
+ /// other device shows. [`Pairing::enter`] answers.
+ Type,
/// Agreed: the link's long-term key, and whether the person compared
- /// numbers (proof against a device in the middle). The link is to be
+ /// numbers or typed a passkey (proof against a device in the middle,
+ /// which would have shown another number). The link is to be
/// encrypted with it, then [`Pairing::encrypted`] called.
Done {
key: [u8; 16],
@@ -239,7 +253,16 @@ pub struct Pairing {
nonce: [u8; 16],
peer_nonce: [u8; 16],
peer_confirm: [u8; 16],
- numeric: bool,
+ method: Method,
+ /// Passkey entry: the passkey, once shown or typed; the round (of
+ /// twenty, a bit of it each); the key each round's fresh nonce is
+ /// drawn with; a passkey drawn for showing; and the peer's confirm for
+ /// this round, held by a responder until its person has typed.
+ passkey: Option<u32>,
+ round: u8,
+ nonce_key: [u8; 16],
+ drawn: u32,
+ held: bool,
/// The initiator's address and the responder's: six bytes and a type.
a: [u8; 7],
b: [u8; 7],
@@ -301,6 +324,10 @@ impl Pairing {
}
let mut nonce = [0u8; 16];
random(&mut nonce);
+ let mut nonce_key = [0u8; 16];
+ random(&mut nonce_key);
+ let mut drawn = [0u8; 4];
+ random(&mut drawn);
let (a, b) = if initiator { (own, peer) } else { (peer, own) };
Pairing {
initiator,
@@ -314,7 +341,12 @@ impl Pairing {
nonce,
peer_nonce: [0; 16],
peer_confirm: [0; 16],
- numeric: false,
+ method: Method::JustWorks,
+ passkey: None,
+ round: 0,
+ nonce_key,
+ drawn: u32::from_le_bytes(drawn),
+ held: false,
a,
b,
mac_key: [0; 16],
@@ -396,13 +428,7 @@ impl Pairing {
p.take_keys = request[5] & ID_KEY;
p.send_keys = request[6] & p.own_keys();
p.response = Self::features(PAIRING_RESPONSE, io, p.take_keys, p.send_keys);
- match method(&p.request, &p.response) {
- Some(numeric) => p.numeric = numeric,
- None => {
- let out = p.failed(NOT_SUPPORTED);
- return (p, out);
- }
- }
+ p.method = method(&p.request, &p.response);
p.state = State::PublicKey;
let out = alloc::vec![Output::Send(p.response.to_vec())];
(p, out)
@@ -414,7 +440,97 @@ impl Pairing {
/// Whether it waits on this device's person.
pub fn asking(&self) -> bool {
- self.state == State::Person && self.asked
+ self.state == State::Person && self.asked || self.typing()
+ }
+
+ /// Whether this device's person is to type the passkey, and has not.
+ fn typing(&self) -> bool {
+ self.passkey.is_none()
+ && self.state != State::Over
+ && matches!(self.method, Method::Passkey { .. })
+ && !self.shows()
+ }
+
+ /// Passkey entry: whether this side shows the passkey (else its
+ /// person types it).
+ fn shows(&self) -> bool {
+ match self.method {
+ Method::Passkey {
+ initiator_shows,
+ responder_shows,
+ } => {
+ if self.initiator {
+ initiator_shows
+ } else {
+ responder_shows
+ }
+ }
+ _ => false,
+ }
+ }
+
+ /// Passkey entry, once the public keys are across: the passkey shown,
+ /// or the person asked to type it; the initiator then sends its first
+ /// round's confirm as soon as it has the passkey.
+ fn start_passkey(&mut self) -> Vec<Output> {
+ let mut out = Vec::new();
+ if self.shows() {
+ let passkey = self.drawn % 1_000_000;
+ self.passkey = Some(passkey);
+ out.push(Output::Show(passkey));
+ } else {
+ out.push(Output::Type);
+ }
+ self.state = State::Confirm;
+ if self.initiator && self.passkey.is_some() {
+ out.push(Output::Send(self.round_confirm()));
+ }
+ out
+ }
+
+ /// This side's confirm for the round: a fresh nonce, committed to with
+ /// the round's bit of the passkey (Cai = f4(PKax, PKbx, Nai, 0x80|rai),
+ /// Cbi likewise from the responder's side).
+ fn round_confirm(&mut self) -> Vec<u8> {
+ self.nonce = cmac(&self.nonce_key, &[self.round]);
+ let confirm = f4(x(&self.own), x(&self.peer), &self.nonce, self.round_bit());
+ let mut c = alloc::vec![PAIRING_CONFIRM];
+ c.extend_from_slice(&confirm);
+ c
+ }
+
+ /// The round's z for f4: 0x80 and the passkey's bit, the lowest first.
+ fn round_bit(&self) -> u8 {
+ 0x80 | (self.passkey.unwrap_or(0) >> self.round & 1) as u8
+ }
+
+ /// Passkey entry: whether the peer's nonce opens its confirm for this
+ /// round, which it can only with the same bit of the passkey.
+ fn round_checks(&self) -> bool {
+ f4(x(&self.peer), x(&self.own), &self.peer_nonce, self.round_bit()) == self.peer_confirm
+ }
+
+ /// The person typed the passkey the other device shows
+ /// ([`Output::Type`]).
+ pub fn enter(&mut self, passkey: u32, now: u64) -> Vec<Output> {
+ if !self.typing() || passkey >= 1_000_000 {
+ return Vec::new();
+ }
+ self.at = now;
+ self.passkey = Some(passkey);
+ if self.state != State::Confirm {
+ return Vec::new();
+ }
+ if self.initiator {
+ alloc::vec![Output::Send(self.round_confirm())]
+ } else if self.held {
+ // The initiator's first confirm came while the person typed.
+ self.held = false;
+ self.state = State::Random;
+ alloc::vec![Output::Send(self.round_confirm())]
+ } else {
+ Vec::new()
+ }
}
fn failed(&mut self, reason: u8) -> Vec<Output> {
@@ -466,10 +582,7 @@ impl Pairing {
}
self.send_keys = response[5];
self.take_keys = response[6];
- match method(&self.request, &self.response) {
- Some(numeric) => self.numeric = numeric,
- None => return self.failed(NOT_SUPPORTED),
- }
+ self.method = method(&self.request, &self.response);
self.state = State::PublicKey;
alloc::vec![Output::Send(self.public_key_pdu())]
}
@@ -478,9 +591,17 @@ impl Pairing {
return self.failed(DHKEY_CHECK_FAILED);
};
self.dhkey = dhkey;
+ let passkey = matches!(self.method, Method::Passkey { .. });
if self.initiator {
+ if passkey {
+ return self.start_passkey();
+ }
self.state = State::Confirm;
Vec::new()
+ } else if passkey {
+ let mut out = alloc::vec![Output::Send(self.public_key_pdu())];
+ out.extend(self.start_passkey());
+ out
} else {
// Cb = f4(PKbx, PKax, Nb, 0).
let confirm = f4(x(&self.own), x(&self.peer), &self.nonce, 0);
@@ -490,11 +611,52 @@ impl Pairing {
alloc::vec![Output::Send(self.public_key_pdu()), Output::Send(c)]
}
}
- (State::Confirm, PAIRING_CONFIRM, true) if body.len() == 16 => {
+ // The initiator's own confirm went first in passkey entry, so
+ // not before its person typed.
+ (State::Confirm, PAIRING_CONFIRM, true) if body.len() == 16 && !self.typing() => {
self.peer_confirm.copy_from_slice(body);
self.state = State::Random;
alloc::vec![Output::Send(self.random_pdu())]
}
+ // Passkey entry: the initiator's confirm for a round, answered
+ // with ours once the passkey is typed.
+ (State::Confirm, PAIRING_CONFIRM, false) if body.len() == 16 && !self.held => {
+ self.peer_confirm.copy_from_slice(body);
+ if self.passkey.is_none() {
+ self.held = true;
+ return Vec::new();
+ }
+ self.state = State::Random;
+ alloc::vec![Output::Send(self.round_confirm())]
+ }
+ (State::Random, PAIRING_RANDOM, _)
+ if body.len() == 16 && matches!(self.method, Method::Passkey { .. }) =>
+ {
+ self.peer_nonce.copy_from_slice(body);
+ if !self.round_checks() {
+ return self.failed(CONFIRM_VALUE_FAILED);
+ }
+ let mut out = Vec::new();
+ if !self.initiator {
+ out.push(Output::Send(self.random_pdu()));
+ }
+ self.round += 1;
+ if self.round < 20 {
+ self.state = State::Confirm;
+ if self.initiator {
+ out.push(Output::Send(self.round_confirm()));
+ }
+ return out;
+ }
+ // Twenty rounds: the last nonces are Na and Nb.
+ let (na, nb) = self.nonces();
+ (self.mac_key, self.key) = f5(&self.dhkey, &na, &nb, &self.a, &self.b);
+ if self.initiator {
+ out.push(Output::Send(self.check_pdu()));
+ }
+ self.state = State::Check;
+ out
+ }
(State::Random, PAIRING_RANDOM, _) if body.len() == 16 => {
self.peer_nonce.copy_from_slice(body);
let (na, nb) = self.nonces();
@@ -512,7 +674,7 @@ impl Pairing {
} else {
(x(&self.peer), x(&self.own))
};
- if self.numeric {
+ if self.method == Method::Numeric {
out.push(Output::Ask(Some(g2(pkax, pkbx, &na, &nb))));
self.asked = true;
self.state = State::Person;
@@ -560,8 +722,12 @@ impl Pairing {
}
}
- /// This device's person answered [`Output::Ask`].
+ /// This device's person answered [`Output::Ask`], or with no cancelled
+ /// a passkey shown or asked for.
pub fn confirm(&mut self, yes: bool, now: u64) -> Vec<Output> {
+ if !yes && matches!(self.method, Method::Passkey { .. }) && self.state != State::Over {
+ return self.failed(PASSKEY_ENTRY_FAILED);
+ }
if self.state != State::Person {
return Vec::new();
}
@@ -585,7 +751,7 @@ impl Pairing {
/// Checks the peer's DHKey check; the responder then sends its own.
fn finish(&mut self, check: [u8; 16]) -> Vec<Output> {
let (na, nb) = self.nonces();
- let r = [0u8; 16];
+ let r = self.r();
let expected = if self.initiator {
// Eb = f6(MacKey, Nb, Na, 0, IOcapB, B, A).
f6(
@@ -619,7 +785,7 @@ impl Pairing {
self.private = [0; 32];
out.push(Output::Done {
key: self.key,
- compared: self.numeric,
+ compared: self.method != Method::JustWorks,
});
out
}
@@ -664,6 +830,15 @@ impl Pairing {
out
}
+ /// R for the DHKey checks: the passkey in passkey entry, else zero.
+ fn r(&self) -> [u8; 16] {
+ let mut r = [0u8; 16];
+ if matches!(self.method, Method::Passkey { .. }) {
+ r[..4].copy_from_slice(&self.passkey.unwrap_or(0).to_le_bytes());
+ }
+ r
+ }
+
/// (Na, Nb): the initiator's nonce and the responder's.
fn nonces(&self) -> ([u8; 16], [u8; 16]) {
if self.initiator {
@@ -688,7 +863,7 @@ impl Pairing {
/// This side's DHKey check: Ea as the initiator, Eb as the responder.
fn check_pdu(&self) -> Vec<u8> {
let (na, nb) = self.nonces();
- let r = [0u8; 16];
+ let r = self.r();
let e = if self.initiator {
f6(
&self.mac_key,
@@ -740,24 +915,43 @@ fn io_cap(features: &[u8; 7]) -> [u8; 3] {
[features[1], features[2], features[3]]
}
-/// How two devices pair (Vol 3 Part H 2.3.5.1, Secure Connections):
-/// `Some(true)` numeric comparison, `Some(false)` just works, `None` a
-/// passkey typed, which is not done here.
-fn method(request: &[u8; 7], response: &[u8; 7]) -> Option<bool> {
+/// How two devices pair.
+#[derive(Clone, Copy, PartialEq, Eq, Debug)]
+enum Method {
+ JustWorks,
+ Numeric,
+ /// Which sides show the passkey; a side that does not types it (with
+ /// a keyboard on both, both people type one they chose).
+ Passkey {
+ initiator_shows: bool,
+ responder_shows: bool,
+ },
+}
+
+/// How two devices pair, from what each can show and take (Vol 3 Part H
+/// 2.3.5.1, the table for Secure Connections).
+fn method(request: &[u8; 7], response: &[u8; 7]) -> Method {
if (request[3] | response[3]) & MITM == 0 {
- return Some(false);
+ return Method::JustWorks;
}
let (i, r) = (request[1], response[1]);
let display = |io: u8| io == DISPLAY_YES_NO || io == KEYBOARD_DISPLAY;
if i == NO_INPUT_NO_OUTPUT || r == NO_INPUT_NO_OUTPUT {
- return Some(false);
+ return Method::JustWorks;
}
if display(i) && display(r) {
- return Some(true);
+ return Method::Numeric;
+ }
+ // A keyboard types; one with a display too types only beside a display
+ // without buttons, and otherwise shows.
+ let initiator_types = i == KEYBOARD_ONLY || i == KEYBOARD_DISPLAY && r == DISPLAY_ONLY;
+ let responder_types = r == KEYBOARD_ONLY || r == KEYBOARD_DISPLAY && i == DISPLAY_ONLY;
+ if !initiator_types && !responder_types {
+ // Displays only, nothing to type on.
+ return Method::JustWorks;
}
- if i == KEYBOARD_ONLY || r == KEYBOARD_ONLY || i == KEYBOARD_DISPLAY || r == KEYBOARD_DISPLAY {
- return None;
+ Method::Passkey {
+ initiator_shows: !initiator_types,
+ responder_shows: !responder_types,
}
- // A display only, beside one that cannot type.
- Some(false)
}
diff --git a/bt/src/tests.rs b/bt/src/tests.rs
index 60a187c..0e8de78 100644
--- a/bt/src/tests.rs
+++ b/bt/src/tests.rs
@@ -738,6 +738,144 @@ fn just_works_with_a_device_that_has_no_display() {
assert!(w.hosts[0].encrypted(ca));
}
+fn passkey(events: &[Event]) -> Option<Option<u32>> {
+ events.iter().find_map(|e| match e {
+ Event::Passkey { passkey, .. } => Some(*passkey),
+ _ => None,
+ })
+}
+
+/// A phone pairs with a keyboard: the phone shows a passkey, the keyboard's
+/// person types it, and the two pair as safely as by comparing numbers.
+#[test]
+fn a_keyboard_pairs_by_the_passkey_typed_on_it() {
+ let mut w = pairing_world([smp::DISPLAY_YES_NO, smp::KEYBOARD_ONLY]);
+ let (ca, cb) = connect(&mut w);
+ w.hosts[0].pair(ca);
+ w.settle();
+ let (ea, eb) = (w.events(0), w.events(1));
+ let Some(Some(shown)) = passkey(&ea) else {
+ panic!("the phone should show a passkey: {ea:?}");
+ };
+ assert!(shown < 1_000_000);
+ assert_eq!(passkey(&eb), Some(None), "the keyboard asks for it: {eb:?}");
+ assert_eq!(code(&ea), None, "nothing to compare");
+ assert!(!w.hosts[0].encrypted(ca));
+ w.hosts[1].enter_passkey(cb, shown);
+ w.settle();
+ let (ea, eb) = (w.events(0), w.events(1));
+ for (events, peer) in [(&ea, B), (&eb, A)] {
+ assert!(
+ has(
+ events,
+ |e| matches!(e, Event::Paired { bond, .. } if bond.address == peer && bond.compared)
+ ),
+ "{events:?}"
+ );
+ }
+ assert!(w.hosts[0].encrypted(ca) && w.hosts[1].encrypted(cb));
+ assert_eq!(w.hosts[0].bonds()[0].key, w.hosts[1].bonds()[0].key);
+}
+
+#[test]
+fn a_wrong_passkey_pairs_nothing() {
+ let mut w = pairing_world([smp::DISPLAY_YES_NO, smp::KEYBOARD_ONLY]);
+ let (ca, cb) = connect(&mut w);
+ w.hosts[0].pair(ca);
+ w.settle();
+ let Some(Some(shown)) = passkey(&w.events(0)) else {
+ panic!("no passkey shown");
+ };
+ w.events(1);
+ // One digit off: a bit of it differs, and that round's confirm fails.
+ w.hosts[1].enter_passkey(cb, (shown + 1) % 1_000_000);
+ w.settle();
+ for i in 0..2 {
+ let events = w.events(i);
+ assert!(
+ has(&events, |e| matches!(
+ e,
+ Event::PairingFailed {
+ reason: smp::CONFIRM_VALUE_FAILED,
+ ..
+ }
+ )),
+ "{events:?}"
+ );
+ assert!(w.hosts[i].bonds().is_empty());
+ }
+ assert!(!w.hosts[0].encrypted(ca));
+}
+
+#[test]
+fn a_passkey_shown_can_be_cancelled() {
+ let mut w = pairing_world([smp::DISPLAY_YES_NO, smp::KEYBOARD_ONLY]);
+ let (ca, _) = connect(&mut w);
+ w.hosts[0].pair(ca);
+ w.settle();
+ w.events(0);
+ w.events(1);
+ w.hosts[0].confirm(ca, false);
+ w.settle();
+ assert!(has(&w.events(1), |e| matches!(
+ e,
+ Event::PairingFailed {
+ reason: smp::PASSKEY_ENTRY_FAILED,
+ ..
+ }
+ )));
+}
+
+/// Who shows and who types, the other way round: a keyboard that starts
+/// the pairing types what a display shows; two keyboards both type one
+/// number their people chose.
+#[test]
+fn passkey_entry_either_way_round() {
+ let (a, b) = (smp::address_bytes(A.0, 0), smp::address_bytes(B.0, 0));
+ for (io_a, io_b) in [
+ (smp::KEYBOARD_ONLY, smp::DISPLAY_ONLY),
+ (smp::KEYBOARD_ONLY, smp::KEYBOARD_ONLY),
+ (smp::KEYBOARD_DISPLAY, smp::KEYBOARD_ONLY),
+ ] {
+ let (mut first, start) = smp::Pairing::start(io_a, a, b, None, &mut counter(1), 0);
+ let smp::Output::Send(request) = &start[0] else {
+ panic!()
+ };
+ let (mut second, out) = smp::Pairing::answer(io_b, b, a, None, request, &mut counter(2), 0);
+ let (saw_a, saw_b) = pump(&mut first, &mut second, Vec::new(), out);
+ let shown = |saw: &[smp::Output]| {
+ saw.iter().find_map(|o| match o {
+ smp::Output::Show(p) => Some(Some(*p)),
+ smp::Output::Type => Some(None),
+ _ => None,
+ })
+ };
+ let (Some(pa), Some(pb)) = (shown(&saw_a), shown(&saw_b)) else {
+ panic!("{io_a} {io_b}: both asked: {saw_a:?} {saw_b:?}");
+ };
+ let passkey = pa.or(pb).unwrap_or(123_456);
+ let mut from_a = Vec::new();
+ let mut from_b = Vec::new();
+ if pa.is_none() {
+ from_a = first.enter(passkey, 0);
+ }
+ if pb.is_none() {
+ from_b = second.enter(passkey, 0);
+ }
+ let (saw_a, saw_b) = pump(&mut first, &mut second, from_a, from_b);
+ let done = |saw: &[smp::Output]| {
+ saw.iter().find_map(|o| match o {
+ smp::Output::Done { key, compared: true } => Some(*key),
+ _ => None,
+ })
+ };
+ let (Some(ka), Some(kb)) = (done(&saw_a), done(&saw_b)) else {
+ panic!("{io_a} {io_b}: not paired: {saw_a:?} {saw_b:?}");
+ };
+ assert_eq!(ka, kb);
+ }
+}
+
#[test]
fn numbers_not_confirmed_pair_nothing() {
let mut w = pairing_world([smp::DISPLAY_YES_NO; 2]);
@@ -817,7 +955,8 @@ fn pump(
mut from_b: Vec<smp::Output>,
) -> (Vec<smp::Output>, Vec<smp::Output>) {
let (mut seen_a, mut seen_b) = (Vec::new(), Vec::new());
- for _ in 0..20 {
+ // Passkey entry's twenty rounds take three PDUs each.
+ for _ in 0..200 {
let mut next_b = Vec::new();
for o in from_a.drain(..) {
match o {
diff --git a/programs/src/bin/bt.rs b/programs/src/bin/bt.rs
index 496d5c5..50250d6 100644
--- a/programs/src/bin/bt.rs
+++ b/programs/src/bin/bt.rs
@@ -486,6 +486,8 @@ fn main(_argument: u64) -> u64 {
&connection_bytes(connection, &peer),
);
}
+ // No app shows a passkey yet: refused, as before.
+ Event::Passkey { connection, .. } => host.confirm(connection, false),
Event::Changed { .. } | Event::Subscribed { .. } => {}
}
}