Skip to content

Commit

Permalink
Improve CF_INFO
Browse files Browse the repository at this point in the history
Fix #158.
  • Loading branch information
sosthene-nitrokey committed Jan 4, 2024
1 parent 931e120 commit 65b8716
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions pkcs11/src/defs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,54 @@ pub const CRYPTOKI_VERSION: cryptoki_sys::CK_VERSION = cryptoki_sys::CK_VERSION
major: CRYPTOKI_VERSION_MAJOR,
minor: cryptoki_sys::CRYPTOKI_VERSION_MINOR,
};
pub const LIB_VERSION: CK_VERSION = CK_VERSION { major: 0, minor: 1 };
pub const LIB_DESCRIPTION: &str = "Nitrokey PKCS#11 library";

#[allow(clippy::result_unit_err)]
const fn parse_u8(s: &str) -> Result<u8, ()> {
let mut idx = 0;
let mut acc = 0u8;
while idx < s.len() {
let Some(temp1) = acc.checked_mul(10u8) else {
return Err(());

Check warning on line 16 in pkcs11/src/defs.rs

View check run for this annotation

Codecov / codecov/patch

pkcs11/src/defs.rs#L11-L16

Added lines #L11 - L16 were not covered by tests
};
let Some(digit) = s.as_bytes()[idx].checked_sub(b'0') else {
return Err(());

Check warning on line 19 in pkcs11/src/defs.rs

View check run for this annotation

Codecov / codecov/patch

pkcs11/src/defs.rs#L18-L19

Added lines #L18 - L19 were not covered by tests
};
if digit >= 10 {
return Err(());
}
let Some(temp2) = temp1.checked_add(digit) else {
return Err(());

Check warning on line 25 in pkcs11/src/defs.rs

View check run for this annotation

Codecov / codecov/patch

pkcs11/src/defs.rs#L21-L25

Added lines #L21 - L25 were not covered by tests
};
acc = temp2;
idx += 1;

Check warning on line 28 in pkcs11/src/defs.rs

View check run for this annotation

Codecov / codecov/patch

pkcs11/src/defs.rs#L27-L28

Added lines #L27 - L28 were not covered by tests
}
Ok(acc)
}

Check warning on line 31 in pkcs11/src/defs.rs

View check run for this annotation

Codecov / codecov/patch

pkcs11/src/defs.rs#L30-L31

Added lines #L30 - L31 were not covered by tests

pub const LIB_VERSION_MINOR: u8 = {
match parse_u8(env!("CARGO_PKG_VERSION_MINOR")) {
Ok(v) => v,
Err(()) => panic!("Failed to parse minor version"),
}
};
pub const LIB_VERSION_MAJOR: u8 = {
match parse_u8(env!("CARGO_PKG_VERSION_MAJOR")) {
Ok(v) => v,
Err(()) => panic!("Failed to parse major version"),
}
};

pub const LIB_VERSION: CK_VERSION = CK_VERSION {
major: LIB_VERSION_MAJOR,
minor: LIB_VERSION_MINOR,
};
pub const LIB_DESCRIPTION: &str = {
let v = "Nitrokey NetHsm PKCS#11 library";
// max length of libraryDescription in CK_INFO
assert!(v.len() < 32);
v
};

pub const LIB_MANUFACTURER: &str = "Nitrokey";
pub const DEFAULT_FIRMWARE_VERSION: CK_VERSION = CK_VERSION { major: 0, minor: 1 };
pub const DEFAULT_HARDWARE_VERSION: CK_VERSION = CK_VERSION { major: 0, minor: 1 };
Expand Down Expand Up @@ -41,3 +87,20 @@ pub const MECHANISM_LIST: [Mechanism; 27] = [
Mechanism::GenerateEd,
Mechanism::GenerateGeneric,
];

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

#[test]
fn version_parsing() {
assert_eq!(
LIB_VERSION_MAJOR,
env!("CARGO_PKG_VERSION_MAJOR").parse::<u8>().unwrap()
);
assert_eq!(
LIB_VERSION_MINOR,
env!("CARGO_PKG_VERSION_MINOR").parse::<u8>().unwrap()
);
}
}

0 comments on commit 65b8716

Please sign in to comment.