Skip to content

Commit

Permalink
Renamed ffi enum types an let original type be primitive to minimize …
Browse files Browse the repository at this point in the history
…changes
  • Loading branch information
qpernil committed Jan 24, 2022
1 parent 641c3e5 commit f9ddd22
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 199 deletions.
47 changes: 25 additions & 22 deletions libyubihsm-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,13 @@ fn bindgen_test_layout_yh_capabilities() {
);
}

/// Raw Command byte definitions
pub type yh_cmd = u32;

#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, num_enum::TryFromPrimitive)]
/// Command byte definitions
pub enum yh_cmd {
pub enum yh_cmd_enum {
/// Echo, request
YHC_ECHO = 0x01,
/// Echo, response
Expand Down Expand Up @@ -371,10 +374,13 @@ pub enum yh_cmd {
YHC_ERROR = 0x7f,
}

/// Raw Object types
pub type yh_object_type = u32;

#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, num_enum::FromPrimitive)]
/// Object types
pub enum yh_object_type {
pub enum yh_object_type_enum {
#[num_enum(default)]
/// Any object type (convenience value, not in libyubihsm)
YH_ANY = 0x00,
Expand All @@ -396,16 +402,13 @@ pub enum yh_object_type {
YH_PUBLIC_KEY = 0x83,
}

impl Default for yh_object_type {
fn default() -> yh_object_type {
yh_object_type::YH_ANY
}
}
/// Raw Algorithms
pub type yh_algorithm = u32;

#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, num_enum::FromPrimitive)]
/// Algorithms
pub enum yh_algorithm {
pub enum yh_algorithm_enum {
/// Any algorithm (convenience value, not in libyubihsm)
#[num_enum(default)]
YH_ALGO_ANY = 0,
Expand Down Expand Up @@ -509,26 +512,26 @@ pub enum yh_algorithm {
YH_ALGO_EC_P256_YUBICO_AUTHENTICATION = 49,
}

impl Default for yh_algorithm {
fn default() -> yh_algorithm {
yh_algorithm::YH_ALGO_ANY
}
}
/// Raw Device-global options
pub type yh_option = u32;

#[repr(u32)]
/// Device-global options
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, num_enum::TryFromPrimitive)]
pub enum yh_option {
pub enum yh_option_enum {
/// Forced audit mode
YH_OPTION_FORCE_AUDIT = 1,
/// Audit logging per command
YH_OPTION_COMMAND_AUDIT = 3,
}

/// Raw Connector options
pub type yh_connector_option = u32;

#[repr(u32)]
/// Connector options
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, num_enum::TryFromPrimitive)]
pub enum yh_connector_option {
pub enum yh_connector_option_enum {
/// File with CA certificate to validate the connector with (const char *) not
/// implemented on Windows
YH_CONNECTOR_HTTPS_CA = 1,
Expand Down Expand Up @@ -726,9 +729,9 @@ pub struct yh_object_descriptor {
/// Object domains
pub domains: u16,
/// Object type
pub type_: u32, // yh_object_type
pub type_: yh_object_type,
/// Object algorithm
pub algorithm: u32, // yh_algorithm
pub algorithm: yh_algorithm,
/// Object sequence
pub sequence: u8,
/// Object origin
Expand Down Expand Up @@ -1173,10 +1176,10 @@ extern "C" {
**/
pub fn yh_send_plain_msg(
connector: *const yh_connector,
cmd: u32,
cmd: yh_cmd,
data: *const u8,
data_len: usize,
response_cmd: *mut u32,
response_cmd: *mut yh_cmd,
response: *mut u8,
response_len: *mut usize,
) -> yh_rc;
Expand All @@ -1198,10 +1201,10 @@ extern "C" {
**/
pub fn yh_send_secure_msg(
session: *const yh_session,
cmd: u32,
cmd: yh_cmd,
data: *const u8,
data_len: usize,
response_cmd: *mut u32,
response_cmd: *mut yh_cmd,
response: *mut u8,
response_len: *mut usize,
) -> yh_rc;
Expand Down Expand Up @@ -1356,7 +1359,7 @@ extern "C" {
serial: *mut u32,
log_total: *mut u8,
log_used: *mut u8,
algorithms: *mut u32,
algorithms: *mut yh_algorithm,
n_algorithms: *mut usize,
) -> yh_rc;
}
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
extern crate libyubihsm_sys as lyh;
extern crate num_enum;

use lyh::{yh_algorithm, yh_capabilities, yh_object_descriptor, yh_object_type, yh_rc_enum};
use lyh::{yh_algorithm_enum, yh_capabilities, yh_object_descriptor, yh_object_type, yh_rc_enum};

#[macro_use]
extern crate lazy_static;
Expand Down Expand Up @@ -83,7 +83,7 @@ pub struct DeviceInfo {
/// Used log entries
log_used: u8,
/// Supported algorihms
algorithms: Vec<yh_algorithm>,
algorithms: Vec<yh_algorithm_enum>,
}

/// Initialize libyubihsm
Expand Down Expand Up @@ -175,7 +175,7 @@ impl YubiHsm {
serial,
log_total,
log_used,
algorithms: algorithms[0..n_algorithms].iter().map(|v| yh_algorithm::from(*v)).collect(),
algorithms: algorithms[0..n_algorithms].iter().map(|v| yh_algorithm_enum::from(*v)).collect(),
})
}

Expand Down Expand Up @@ -249,10 +249,10 @@ impl Session {
lyh::yh_util_list_objects(
self.ptr,
0,
lyh::yh_object_type::YH_ANY,
0,
0,
&capa,
lyh::yh_algorithm::YH_ALGO_ANY,
0,
std::ptr::null(),
objects.as_mut_ptr(),
&mut n_objects,
Expand Down Expand Up @@ -417,7 +417,7 @@ impl Session {
wrapping_key_id: u16,
bytes: &[u8],
) -> Result<ObjectHandle, Error> {
let mut object_type: yh_object_type = yh_object_type::default();
let mut object_type = 0;
let mut id: u16 = 0;

let res = unsafe {
Expand All @@ -434,7 +434,7 @@ impl Session {

Ok(ObjectHandle {
object_id: id,
object_type: (&object_type).into(),
object_type: object_type.into(),
})
}

Expand Down
Loading

0 comments on commit f9ddd22

Please sign in to comment.