Skip to content

Commit

Permalink
feat: add nusb support
Browse files Browse the repository at this point in the history
  • Loading branch information
louib committed Aug 30, 2024
1 parent 1a4223f commit 1b037bc
Show file tree
Hide file tree
Showing 7 changed files with 389 additions and 86 deletions.
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ include = [
name = "challenge_response"
path = "src/lib.rs"

[features]
rusb = ["dep:rusb"]
nusb = ["dep:nusb"]
default = ["rusb"]

[dependencies]
rand = "0.8"
bitflags = "2.4"
rusb = "0.9"
rusb = { version = "0.9", optional = true }
nusb = { version = "0.1", optional = true }
structure = "0.1"
aes = "0.8"
block-modes = "0.9"
Expand Down
4 changes: 2 additions & 2 deletions src/configure.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use config::Command;
use hmacmode::HmacKey;
use manager::Frame;
use otpmode::Aes128Key;
use sec::crc16;
use std;
use usb::Frame;

const FIXED_SIZE: usize = 16;
const UID_SIZE: usize = 6;
Expand Down Expand Up @@ -48,7 +48,7 @@ const SIZEOF_CONFIG: usize = 52;
impl DeviceModeConfig {
#[doc(hidden)]
pub fn to_frame(&mut self, command: Command) -> Frame {
let mut payload = [0; crate::manager::PAYLOAD_SIZE];
let mut payload = [0; crate::usb::PAYLOAD_SIZE];
// First set CRC.
self.crc = {
let first_fields = unsafe {
Expand Down
7 changes: 7 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(feature = "rusb")]
use rusb::Error as usbError;
use std::error;
use std::fmt;
Expand All @@ -6,11 +7,13 @@ use std::io::Error as ioError;
#[derive(Debug)]
pub enum ChallengeResponseError {
IOError(ioError),
#[cfg(feature = "rusb")]
UsbError(usbError),
CommandNotSupported,
DeviceNotFound,
OpenDeviceError,
CanNotWriteToDevice,
CanNotReadFromDevice,
WrongCRC,
ConfigNotWritten,
}
Expand All @@ -19,12 +22,14 @@ impl fmt::Display for ChallengeResponseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ChallengeResponseError::IOError(ref err) => write!(f, "IO error: {}", err),
#[cfg(feature = "rusb")]
ChallengeResponseError::UsbError(ref err) => write!(f, "USB error: {}", err),
ChallengeResponseError::DeviceNotFound => write!(f, "Device not found"),
ChallengeResponseError::OpenDeviceError => write!(f, "Can not open device"),
ChallengeResponseError::CommandNotSupported => write!(f, "Command Not Supported"),
ChallengeResponseError::WrongCRC => write!(f, "Wrong CRC"),
ChallengeResponseError::CanNotWriteToDevice => write!(f, "Can not write to Device"),
ChallengeResponseError::CanNotReadFromDevice => write!(f, "Can not read from Device"),
ChallengeResponseError::ConfigNotWritten => write!(f, "Configuration has failed"),
}
}
Expand All @@ -33,6 +38,7 @@ impl fmt::Display for ChallengeResponseError {
impl error::Error for ChallengeResponseError {
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
#[cfg(feature = "rusb")]
ChallengeResponseError::UsbError(ref err) => Some(err),
_ => None,
}
Expand All @@ -45,6 +51,7 @@ impl From<ioError> for ChallengeResponseError {
}
}

#[cfg(feature = "rusb")]
impl From<usbError> for ChallengeResponseError {
fn from(err: usbError) -> ChallengeResponseError {
ChallengeResponseError::UsbError(err)
Expand Down
Loading

0 comments on commit 1b037bc

Please sign in to comment.