Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve CF_INFO #167

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
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()
);
}
}
Loading