diff options
| author | Robin Krahl <robin.krahl@ireas.org> | 2020-01-14 16:15:40 +0100 | 
|---|---|---|
| committer | Robin Krahl <robin.krahl@ireas.org> | 2020-01-14 16:53:52 +0100 | 
| commit | 2e543445c3059fa9decdbef718caf84696bb8786 (patch) | |
| tree | 1e4af2a6a12e587d3c84756eeb592504ecc4c4f1 /src/device | |
| parent | f266ea63039c87886f871b068ef3dcdf851a1eca (diff) | |
| download | nitrokey-rs-2e543445c3059fa9decdbef718caf84696bb8786.tar.gz nitrokey-rs-2e543445c3059fa9decdbef718caf84696bb8786.tar.bz2 | |
Add the fill_sd_card function to Storage
This patch adds support for libnitrokey’s
NK_fill_SD_card_with_random_data function.  It is executed by the
fill_sd_card function of the Storage struct.  We also add a new test
case that is set to ignore because it takes between 30 and 60 minutes to
run.
Diffstat (limited to 'src/device')
| -rw-r--r-- | src/device/storage.rs | 53 | 
1 files changed, 51 insertions, 2 deletions
| diff --git a/src/device/storage.rs b/src/device/storage.rs index 8d45763..deb2844 100644 --- a/src/device/storage.rs +++ b/src/device/storage.rs @@ -8,7 +8,7 @@ use std::ops;  use nitrokey_sys;  use crate::device::{Device, FirmwareVersion, Model, Status}; -use crate::error::Error; +use crate::error::{CommandError, Error};  use crate::otp::GenerateOtp;  use crate::util::{get_command_result, get_cstring, get_last_error}; @@ -700,7 +700,9 @@ impl<'a> Storage<'a> {      /// Some commands may start a background operation during which no other commands can be      /// executed.  This method can be used to check whether such an operation is ongoing.      /// -    /// Currently, this is only used by the `fill_sd_card` method. +    /// Currently, this is only used by the [`fill_sd_card`][] method. +    /// +    /// [`fill_sd_card`]: #method.fill_sd_card      pub fn get_operation_status(&self) -> Result<OperationStatus, Error> {          let status = unsafe { nitrokey_sys::NK_get_progress_bar_value() };          match status { @@ -713,6 +715,53 @@ impl<'a> Storage<'a> {          }      } +    /// Overwrites the SD card with random data. +    /// +    /// Ths method starts a background operation that overwrites the SD card with random data. +    /// While this operation is ongoing, no other commands can be executed.  Use the +    /// [`get_operation_status`][] function to check the progress of the operation. +    /// +    /// # Errors +    /// +    /// - [`InvalidString`][] if one of the provided passwords contains a null byte +    /// - [`WrongPassword`][] if the admin password is wrong +    /// +    /// # Example +    /// +    /// ```no_run +    /// use nitrokey::OperationStatus; +    /// +    /// let mut manager = nitrokey::take()?; +    /// let mut storage = manager.connect_storage()?; +    /// storage.fill_sd_card("12345678")?; +    /// loop { +    ///     match storage.get_operation_status()? { +    ///         OperationStatus::Ongoing(progress) => println!("{}/100", progress), +    ///         OperationStatus::Idle => { +    ///             println!("Done!"); +    ///             break; +    ///         } +    ///     } +    /// } +    /// # Ok::<(), nitrokey::Error>(()) +    /// ``` +    /// +    /// [`get_operation_status`]: #method.get_operation_status +    /// [`InvalidString`]: enum.LibraryError.html#variant.InvalidString +    /// [`WrongPassword`]: enum.CommandError.html#variant.WrongPassword +    pub fn fill_sd_card(&mut self, admin_pin: &str) -> Result<(), Error> { +        let admin_pin_string = get_cstring(admin_pin)?; +        get_command_result(unsafe { +            nitrokey_sys::NK_fill_SD_card_with_random_data(admin_pin_string.as_ptr()) +        }) +        .or_else(|err| match err { +            // libnitrokey’s C API returns a LongOperationInProgressException with the same error +            // code as the WrongCrc command error, so we cannot distinguish them. +            Error::CommandError(CommandError::WrongCrc) => Ok(()), +            err => Err(err), +        }) +    } +      /// Exports the firmware to the unencrypted volume.      ///      /// This command requires the admin PIN.  The unencrypted volume must be in read-write mode | 
