yubikit 1.0.0-beta.0

YubiKey management library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// Copyright 2026 Yubico AB
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Low-level OTP HID frame protocol, codec, and related helpers.

use std::thread;
use std::time::Duration;

use thiserror::Error;

use crate::core::Version;

// --- OTP Codec ---

const MODHEX_ALPHABET: &[u8; 16] = b"cbdefghijklnrtuv";
const CRC_OK_RESIDUAL: u16 = 0xF0B8;

/// Errors from modhex/OTP codec operations.
#[derive(Debug, Error)]
pub enum OtpCodecError {
    /// Input length is not a multiple of 2.
    #[error("Length must be a multiple of 2")]
    OddLength,
    /// A character outside the modhex alphabet was encountered.
    #[error("'{0}' is not a valid modhex character")]
    InvalidModhexChar(char),
}

/// Calculate CRC-16 (CCITT) over data.
pub fn calculate_crc(data: &[u8]) -> u16 {
    crc16(data)
}

/// Check if CRC-16 over data matches expected residual.
pub fn check_crc(data: &[u8]) -> bool {
    crc16(data) == CRC_OK_RESIDUAL
}

fn crc16(data: &[u8]) -> u16 {
    let mut crc: u16 = 0xFFFF;
    for &byte in data {
        crc ^= byte as u16;
        for _ in 0..8 {
            let carry = crc & 1;
            crc >>= 1;
            if carry == 1 {
                crc ^= 0x8408;
            }
        }
    }
    crc
}

/// Encode bytes to modhex string.
pub fn modhex_encode(data: &[u8]) -> String {
    let mut result = String::with_capacity(data.len() * 2);
    for &byte in data {
        result.push(MODHEX_ALPHABET[(byte >> 4) as usize] as char);
        result.push(MODHEX_ALPHABET[(byte & 0x0F) as usize] as char);
    }
    result
}

/// Decode modhex string to bytes.
pub fn modhex_decode(string: &str) -> Result<Vec<u8>, OtpCodecError> {
    if !string.len().is_multiple_of(2) {
        return Err(OtpCodecError::OddLength);
    }
    let bytes = string.as_bytes();
    let mut result = Vec::with_capacity(string.len() / 2);
    for i in (0..bytes.len()).step_by(2) {
        let hi = modhex_char_value(bytes[i])?;
        let lo = modhex_char_value(bytes[i + 1])?;
        result.push((hi << 4) | lo);
    }
    Ok(result)
}

fn modhex_char_value(ch: u8) -> Result<u8, OtpCodecError> {
    MODHEX_ALPHABET
        .iter()
        .position(|&c| c == ch)
        .map(|p| p as u8)
        .ok_or(OtpCodecError::InvalidModhexChar(ch as char))
}

// --- OTP Protocol ---

// Constants
// ---------------------------------------------------------------------------

const SLOT_DATA_SIZE: usize = 64;
const FRAME_SIZE: usize = SLOT_DATA_SIZE + 6; // 70
const FEATURE_RPT_SIZE: usize = 8;
const FEATURE_RPT_DATA_SIZE: usize = FEATURE_RPT_SIZE - 1; // 7

const RESP_PENDING_FLAG: u8 = 0x40;
const SLOT_WRITE_FLAG: u8 = 0x80;
const RESP_TIMEOUT_WAIT_FLAG: u8 = 0x20;
const SEQUENCE_MASK: u8 = 0x1F;

/// Byte offset of the programming sequence counter in an OTP status report.
pub(crate) const STATUS_OFFSET_PROG_SEQ: usize = 4;
const STATUS_OFFSET_TOUCH_LOW: usize = 5;
const CONFIG_SLOTS_PROGRAMMED_MASK: u8 = 0b0000_0011;

/// ConfigSlot::ScanMap raw value, used to probe NEO devices.
const SCAN_MAP_SLOT: u8 = 0x12;

// ---------------------------------------------------------------------------
// Errors
// ---------------------------------------------------------------------------

/// Connection-level error for OTP HID transport.
#[derive(Debug, Error)]
pub enum OtpError {
    /// The device rejected the command.
    #[error("Command rejected: {0}")]
    CommandRejected(String),
    /// The device returned a malformed or unexpected response.
    #[error("Bad response: {0}")]
    BadResponse(String),
    /// The operation timed out.
    #[error("Timeout: {0}")]
    Timeout(String),
    /// An underlying transport error (e.g. HID I/O failure).
    #[error("Transport error: {0}")]
    Transport(Box<dyn std::error::Error + Send + Sync>),
}

// ---------------------------------------------------------------------------
// OTP transport trait
// ---------------------------------------------------------------------------

/// Trait for low-level OTP HID transport (feature report read/write).
pub trait OtpConnection: crate::core::Connection<Error = OtpError> {
    /// Read an OTP HID feature report from the device.
    fn otp_receive(&mut self) -> Result<Vec<u8>, OtpError>;
    /// Write an OTP HID feature report to the device.
    fn otp_send(&mut self, data: &[u8]) -> Result<(), OtpError>;
}

impl crate::core::Connection for Box<dyn OtpConnection + Send> {
    type Error = OtpError;
    fn close(&mut self) {
        (**self).close();
    }
}

impl OtpConnection for Box<dyn OtpConnection + Send> {
    fn otp_receive(&mut self) -> Result<Vec<u8>, OtpError> {
        (**self).otp_receive()
    }
    fn otp_send(&mut self, data: &[u8]) -> Result<(), OtpError> {
        (**self).otp_send(data)
    }
}

impl crate::core::Connection for Box<dyn OtpConnection + Send + Sync> {
    type Error = OtpError;
    fn close(&mut self) {
        (**self).close();
    }
}

impl OtpConnection for Box<dyn OtpConnection + Send + Sync> {
    fn otp_receive(&mut self) -> Result<Vec<u8>, OtpError> {
        (**self).otp_receive()
    }
    fn otp_send(&mut self, data: &[u8]) -> Result<(), OtpError> {
        (**self).otp_send(data)
    }
}

// ---------------------------------------------------------------------------
// OTP HID protocol
// ---------------------------------------------------------------------------

/// Low-level OTP frame protocol over HID feature reports.
pub struct OtpProtocol<T: OtpConnection> {
    connection: T,
    /// The firmware version reported by the YubiKey.
    pub version: Version,
}

impl<T: OtpConnection> OtpProtocol<T> {
    /// Creates a new OTP protocol by reading the initial status from the connection.
    ///
    /// Returns the connection back alongside the error if initialization fails.
    pub fn new(connection: T) -> Result<Self, (OtpError, T)> {
        let mut proto = Self {
            connection,
            version: Version(0, 0, 0),
        };
        match proto.receive() {
            Ok(report) => proto.version = Version::from_bytes(&report[1..4]),
            Err(e) => return Err((e, proto.into_connection())),
        }

        // NEO (version 3.x): force communication to refresh pgmSeq
        if proto.version.0 == 3 {
            // Write an invalid scan map — expected to be rejected
            let _ = proto.send_and_receive(SCAN_MAP_SLOT, Some(&[b'c'; 51]), None);
        }

        Ok(proto)
    }

    /// Read status bytes from the YubiKey (first 3 bytes are firmware version).
    pub fn read_status(&mut self) -> Result<Vec<u8>, OtpError> {
        let report = self.receive()?;
        // Return bytes 1..7 (skip first byte, drop last byte = status flags)
        Ok(report[1..FEATURE_RPT_DATA_SIZE].to_vec())
    }

    /// Send a command and read the response.
    /// `expected_len >= 0`: verifies CRC and returns exactly that many bytes.
    /// `expected_len == -1`: returns the raw data response (with CRC/padding).
    /// `expected_len == None`: expects no data (status-only), returns `None`.
    pub fn send_and_receive(
        &mut self,
        slot: u8,
        data: Option<&[u8]>,
        expected_len: Option<i32>,
    ) -> Result<Option<Vec<u8>>, OtpError> {
        self.send_and_receive_with_cancel(slot, data, expected_len, None, None)
    }

    /// Send a command with optional cancellation and keepalive callback.
    /// `expected_len >= 0`: verifies CRC and returns exactly that many bytes.
    /// `expected_len == -1`: returns the raw data response (with CRC/padding).
    /// `expected_len == None`: expects no data (status-only), returns `None`.
    pub fn send_and_receive_with_cancel(
        &mut self,
        slot: u8,
        data: Option<&[u8]>,
        expected_len: Option<i32>,
        cancel: Option<&dyn Fn() -> bool>,
        on_keepalive: Option<&dyn Fn(u8)>,
    ) -> Result<Option<Vec<u8>>, OtpError> {
        let payload_data = data.unwrap_or(&[]);
        if payload_data.len() > SLOT_DATA_SIZE {
            return Err(OtpError::BadResponse(
                "Payload too large for HID frame".into(),
            ));
        }
        let mut payload = [0u8; SLOT_DATA_SIZE];
        payload[..payload_data.len()].copy_from_slice(payload_data);

        let frame = format_frame(slot, &payload);
        let prog_seq = self.send_frame(&frame)?;
        let response = self.read_frame(prog_seq, cancel, on_keepalive)?;

        match (response, expected_len) {
            (Some(raw), Some(len)) if len >= 0 => {
                verify_and_strip_crc(&raw, len as usize).map(Some)
            }
            (Some(raw), Some(_)) => Ok(Some(raw)), // -1: raw
            (Some(_), None) | (None, _) => Ok(None),
        }
    }

    fn receive(&mut self) -> Result<Vec<u8>, OtpError> {
        let report = self.connection.otp_receive()?;
        if report.len() != FEATURE_RPT_SIZE {
            return Err(OtpError::BadResponse(format!(
                "Incorrect feature report size (was {}, expected {FEATURE_RPT_SIZE})",
                report.len()
            )));
        }
        Ok(report)
    }

    fn await_ready_to_write(&mut self) -> Result<(), OtpError> {
        for _ in 0..20 {
            let report = self.receive()?;
            if report[FEATURE_RPT_DATA_SIZE] & SLOT_WRITE_FLAG == 0 {
                return Ok(());
            }
            thread::sleep(Duration::from_millis(50));
        }
        Err(OtpError::Timeout(
            "Timeout waiting for YubiKey to become ready to receive".into(),
        ))
    }

    fn send_frame(&mut self, buf: &[u8]) -> Result<u8, OtpError> {
        debug_assert_eq!(buf.len(), FRAME_SIZE);
        let prog_seq = self.receive()?[STATUS_OFFSET_PROG_SEQ];
        let mut seq: u8 = 0;
        let mut offset = 0;
        while offset < buf.len() {
            let end = (offset + FEATURE_RPT_DATA_SIZE).min(buf.len());
            let packet = &buf[offset..end];
            if should_send(packet, seq) {
                let mut report = [0u8; FEATURE_RPT_SIZE];
                report[..packet.len()].copy_from_slice(packet);
                report[FEATURE_RPT_DATA_SIZE] = 0x80 | seq;
                self.await_ready_to_write()?;
                self.connection.otp_send(&report)?;
            }
            seq += 1;
            offset += FEATURE_RPT_DATA_SIZE;
        }
        Ok(prog_seq)
    }

    fn read_frame(
        &mut self,
        prog_seq: u8,
        cancel: Option<&dyn Fn() -> bool>,
        on_keepalive: Option<&dyn Fn(u8)>,
    ) -> Result<Option<Vec<u8>>, OtpError> {
        let mut response = Vec::new();
        let mut seq: u8 = 0;
        let mut needs_touch = false;
        let mut last_ka: Option<u8> = None;

        loop {
            let report = self.receive()?;
            let status_byte = report[FEATURE_RPT_DATA_SIZE];

            if status_byte & RESP_PENDING_FLAG != 0 {
                // Response packet
                if seq == (status_byte & SEQUENCE_MASK) {
                    response.extend_from_slice(&report[..FEATURE_RPT_DATA_SIZE]);
                    seq += 1;
                } else if (status_byte & SEQUENCE_MASK) == 0 {
                    // Transmission complete
                    self.reset_state()?;
                    return Ok(Some(response));
                }
            } else if status_byte == 0 {
                // Status response
                if !response.is_empty() {
                    return Err(OtpError::BadResponse("Incomplete transfer".into()));
                } else if is_sequence_updated(&report, prog_seq) {
                    return Ok(None);
                } else if needs_touch {
                    return Err(OtpError::Timeout("Timed out waiting for touch".into()));
                } else {
                    return Err(OtpError::CommandRejected("No data".into()));
                }
            } else {
                // Need to wait
                let status = if status_byte & RESP_TIMEOUT_WAIT_FLAG != 0 {
                    needs_touch = true;
                    thread::sleep(Duration::from_millis(100));
                    2u8 // STATUS_UPNEEDED
                } else {
                    thread::sleep(Duration::from_millis(20));
                    1u8 // STATUS_PROCESSING
                };
                if let Some(cb) = on_keepalive
                    && last_ka != Some(status)
                {
                    last_ka = Some(status);
                    cb(status);
                }
                if cancel.is_some_and(|f| f()) {
                    self.reset_state()?;
                    return Err(OtpError::Timeout("Command cancelled".into()));
                }
            }
        }
    }

    fn reset_state(&mut self) -> Result<(), OtpError> {
        let mut report = [0u8; FEATURE_RPT_SIZE];
        report[FEATURE_RPT_DATA_SIZE] = 0xFF;
        self.connection.otp_send(&report)?;
        Ok(())
    }

    /// Consume the protocol, returning the underlying connection.
    pub fn into_connection(self) -> T {
        self.connection
    }
}

/// Verify and strip CRC from a raw OTP data response, returning `expected_len` bytes.
pub(crate) fn verify_and_strip_crc(
    response: &[u8],
    expected_len: usize,
) -> Result<Vec<u8>, OtpError> {
    if response.len() < expected_len + 2 {
        return Err(OtpError::BadResponse(format!(
            "Response too short: expected at least {}, got {}",
            expected_len + 2,
            response.len()
        )));
    }
    if check_crc(&response[..expected_len + 2]) {
        Ok(response[..expected_len].to_vec())
    } else {
        Err(OtpError::BadResponse("Invalid CRC".into()))
    }
}

fn should_send(packet: &[u8], seq: u8) -> bool {
    seq == 0 || seq == 9 || packet.iter().any(|&b| b != 0)
}

fn format_frame(slot: u8, payload: &[u8; SLOT_DATA_SIZE]) -> Vec<u8> {
    let crc = calculate_crc(payload);
    let mut frame = Vec::with_capacity(FRAME_SIZE);
    frame.extend_from_slice(payload);
    frame.push(slot);
    frame.extend_from_slice(&crc.to_le_bytes());
    frame.extend_from_slice(&[0u8; 3]);
    debug_assert_eq!(frame.len(), FRAME_SIZE);
    frame
}

fn is_sequence_updated(report: &[u8], prev_seq: u8) -> bool {
    let next_seq = report[STATUS_OFFSET_PROG_SEQ];
    next_seq == prev_seq.wrapping_add(1)
        || (next_seq == 0
            && prev_seq > 0
            && report[STATUS_OFFSET_TOUCH_LOW] & CONFIG_SLOTS_PROGRAMMED_MASK == 0)
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_crc() {
        assert_eq!(calculate_crc(&[]), 0xFFFF);
        let crc = calculate_crc(b"hello");
        assert_ne!(crc, 0);
    }

    #[test]
    fn test_modhex_roundtrip() {
        let data = b"\x01\x02\x03\x04";
        let encoded = modhex_encode(data);
        let decoded = modhex_decode(&encoded).unwrap();
        assert_eq!(decoded, data);
    }

    #[test]
    fn test_format_frame_size() {
        let payload = [0u8; SLOT_DATA_SIZE];
        let frame = format_frame(0x01, &payload);
        assert_eq!(frame.len(), FRAME_SIZE);
    }

    #[test]
    fn test_should_send() {
        // First and last packets always sent
        assert!(should_send(&[0; 7], 0));
        assert!(should_send(&[0; 7], 9));
        // Zero packet in the middle is skipped
        assert!(!should_send(&[0; 7], 5));
        // Non-zero packet always sent
        assert!(should_send(&[0, 0, 1, 0, 0, 0, 0], 5));
    }

    #[test]
    fn test_is_sequence_updated() {
        let mut report = [0u8; FEATURE_RPT_SIZE];
        report[STATUS_OFFSET_PROG_SEQ] = 2;
        assert!(is_sequence_updated(&report, 1));
        assert!(!is_sequence_updated(&report, 2));

        // Wrap-around: prog_seq goes to 0, all slots empty
        report[STATUS_OFFSET_PROG_SEQ] = 0;
        report[STATUS_OFFSET_TOUCH_LOW] = 0; // no slots programmed
        assert!(is_sequence_updated(&report, 5));

        // Wrap-around but slots still programmed → not updated
        report[STATUS_OFFSET_TOUCH_LOW] = 0x01;
        assert!(!is_sequence_updated(&report, 5));
    }
}