aboutsummaryrefslogtreecommitdiff
path: root/nitrocli/src/nitrokey.rs
diff options
context:
space:
mode:
authorRobin Krahl <me@robin-krahl.de>2018-12-11 23:51:01 +0100
committerDaniel Mueller <deso@posteo.net>2018-12-17 07:52:13 -0800
commit4d314264a897c474c12e626a2be36b75dc57f5c9 (patch)
tree11385b55f271a2b95d2d632ad1a2107180be5c3a /nitrocli/src/nitrokey.rs
parent986ad2f782cf944990e4eda8bf88ea1821233302 (diff)
downloadnitrocli-4d314264a897c474c12e626a2be36b75dc57f5c9.tar.gz
nitrocli-4d314264a897c474c12e626a2be36b75dc57f5c9.tar.bz2
Port the open and close commands to libnitrokey
This patch removes the raw hidapi implementations of the Enable Encrypted Volume and Disable Encrypted Volume commands and replaces them with the methods enable_encrypted_volume and disable_encrypted_volume of the Storage struct provided by the nitrokey trait. To provide some context to the error messages, the errors are wrapped using the map_err method of the Result enum and the get_error function that combines a nitrokey error code and a string into a nitrocli error. It would be more idiomatic to define a conversion from a nitrokey error to a nitrocli error, but then we would lose information about the context of the error.
Diffstat (limited to 'nitrocli/src/nitrokey.rs')
-rw-r--r--nitrocli/src/nitrokey.rs83
1 files changed, 0 insertions, 83 deletions
diff --git a/nitrocli/src/nitrokey.rs b/nitrocli/src/nitrokey.rs
index 64685e2..9f767d6 100644
--- a/nitrocli/src/nitrokey.rs
+++ b/nitrocli/src/nitrokey.rs
@@ -17,7 +17,6 @@
// * along with this program. If not, see <http://www.gnu.org/licenses/>. *
// *************************************************************************
-use std::cmp;
use std::mem;
use crate::crc32::crc;
@@ -41,10 +40,6 @@ pub const VOLUME_ACTIVE_HIDDEN: u8 = 0b100;
#[derive(PartialEq)]
#[repr(u8)]
pub enum Command {
- // The command to enable the encrypted volume.
- EnableEncryptedVolume = 0x20,
- // The command to disable the encrypted volume.
- DisableEncryptedVolume = 0x21,
// Retrieve the device status.
GetDeviceStatus = 0x2E,
}
@@ -183,39 +178,6 @@ macro_rules! defaultCommand {
}
-#[allow(dead_code)]
-#[repr(packed)]
-pub struct EnableEncryptedVolumeCommand {
- command: Command,
- // The kind of password. Unconditionally 'P' because the User PIN is
- // used to enable the encrypted volume.
- kind: u8,
- // The password has a maximum length of twenty characters.
- password: [u8; 20],
- padding: [u8; 38],
-}
-
-
-impl EnableEncryptedVolumeCommand {
- pub fn new(password: &[u8]) -> EnableEncryptedVolumeCommand {
- let mut report = EnableEncryptedVolumeCommand {
- command: Command::EnableEncryptedVolume,
- kind: b'P',
- password: [0; 20],
- padding: [0; 38],
- };
-
- debug_assert!(password.len() <= report.password.len());
-
- let len = cmp::min(report.password.len(), password.len());
- report.password[..len].copy_from_slice(&password[..len]);
- report
- }
-}
-
-defaultPayloadAsRef!(EnableEncryptedVolumeCommand);
-
-defaultCommand!(DisableEncryptedVolumeCommand, DisableEncryptedVolume);
defaultCommand!(DeviceStatusCommand, GetDeviceStatus);
@@ -271,17 +233,6 @@ impl<P> AsRef<[u8]> for Response<P> {
#[repr(packed)]
-pub struct StorageResponse {
- pub padding1: [u8; 13],
- pub command_counter: u8,
- pub last_storage_command: Command,
- pub storage_status: StorageStatus,
- pub progress: u8,
- pub padding2: [u8; 2],
-}
-
-
-#[repr(packed)]
pub struct DeviceStatusResponse {
pub padding0: [u8; 22],
pub magic: u16,
@@ -303,37 +254,3 @@ pub struct DeviceStatusResponse {
pub active_smartcard_id: u32,
pub storage_keys_missing: u8,
}
-
-
-#[cfg(test)]
-mod tests {
- use super::*;
-
- #[test]
- fn encrypted_volume_report() {
- let password = "test42".to_string().into_bytes();
- let report = EnableEncryptedVolumeCommand::new(&password);
- let expected = ['t' as u8, 'e' as u8, 's' as u8, 't' as u8, '4' as u8, '2' as u8, 0u8, 0u8,
- 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8, 0u8];
- assert_eq!(report.password, expected);
- }
-
- #[test]
- #[cfg(debug)]
- #[should_panic(expected = "assertion failed")]
- fn overly_long_password() {
- let password = "012345678912345678901".to_string().into_bytes();
- EnableEncryptedVolumeCommand::new(&password);
- }
-
- #[test]
- fn report_crc() {
- let password = "passphrase".to_string().into_bytes();
- let payload = EnableEncryptedVolumeCommand::new(&password);
- let report = Report::from(payload);
-
- // The expected checksum was computed using the original
- // functionality.
- assert_eq!(report.crc, 0xeeb583c);
- }
-}