[][src]Struct nitrokey::Storage

pub struct Storage<'a> { /* fields omitted */ }

A Nitrokey Storage device without user or admin authentication.

Use the connect method to obtain an instance wrapper or the connect_storage method to directly obtain an instance. If you want to execute a command that requires user or admin authentication, use authenticate_admin or authenticate_user.

Examples

Authentication with error handling:

use nitrokey::{Authenticate, User, Storage};

fn perform_user_task<'a>(device: &User<'a, Storage<'a>>) {}
fn perform_other_task(device: &Storage) {}

let mut manager = nitrokey::take()?;
let device = manager.connect_storage()?;
let device = match device.authenticate_user("123456") {
    Ok(user) => {
        perform_user_task(&user);
        user.device()
    },
    Err((device, err)) => {
        eprintln!("Could not authenticate as user: {}", err);
        device
    },
};
perform_other_task(&device);

Methods

impl<'a> Storage<'a>[src]

pub fn change_update_pin(
    &mut self,
    current: &str,
    new: &str
) -> Result<(), Error>
[src]

Changes the update PIN.

The update PIN is used to enable firmware updates. Unlike the user and the admin PIN, the update PIN is not managed by the OpenPGP smart card but by the Nitrokey firmware. There is no retry counter as with the other PIN types.

Errors

Example

let mut manager = nitrokey::take()?;
let mut device = manager.connect_storage()?;
match device.change_update_pin("12345678", "87654321") {
    Ok(()) => println!("Updated update PIN."),
    Err(err) => eprintln!("Failed to update update PIN: {}", err),
};

pub fn enable_firmware_update(&mut self, update_pin: &str) -> Result<(), Error>[src]

Enables the firmware update mode.

During firmware update mode, the Nitrokey can no longer be accessed using HID commands. To resume normal operation, run dfu-programmer at32uc3a3256s launch. In order to enter the firmware update mode, you need the update password that can be changed using the [change_update_pin][] method.

Errors

Example

let mut manager = nitrokey::take()?;
let mut device = manager.connect_storage()?;
match device.enable_firmware_update("12345678") {
    Ok(()) => println!("Nitrokey entered update mode."),
    Err(err) => eprintln!("Could not enter update mode: {}", err),
};

pub fn enable_encrypted_volume(&mut self, user_pin: &str) -> Result<(), Error>[src]

Enables the encrypted storage volume.

Once the encrypted volume is enabled, it is presented to the operating system as a block device. The API does not provide any information on the name or path of this block device.

Errors

Example

let mut manager = nitrokey::take()?;
let mut device = manager.connect_storage()?;
match device.enable_encrypted_volume("123456") {
    Ok(()) => println!("Enabled the encrypted volume."),
    Err(err) => eprintln!("Could not enable the encrypted volume: {}", err),
};

pub fn disable_encrypted_volume(&mut self) -> Result<(), Error>[src]

Disables the encrypted storage volume.

Once the volume is disabled, it can be no longer accessed as a block device. If the encrypted volume has not been enabled, this method still returns a success.

Example

fn use_volume() {}

let mut manager = nitrokey::take()?;
let mut device = manager.connect_storage()?;
match device.enable_encrypted_volume("123456") {
    Ok(()) => {
        println!("Enabled the encrypted volume.");
        use_volume();
        match device.disable_encrypted_volume() {
            Ok(()) => println!("Disabled the encrypted volume."),
            Err(err) => {
                eprintln!("Could not disable the encrypted volume: {}", err);
            },
        };
    },
    Err(err) => eprintln!("Could not enable the encrypted volume: {}", err),
};

pub fn enable_hidden_volume(
    &mut self,
    volume_password: &str
) -> Result<(), Error>
[src]

Enables a hidden storage volume.

This function will only succeed if the encrypted storage (enable_encrypted_volume) or another hidden volume has been enabled previously. Once the hidden volume is enabled, it is presented to the operating system as a block device and any previously opened encrypted or hidden volumes are closed. The API does not provide any information on the name or path of this block device.

Note that the encrypted and the hidden volumes operate on the same storage area, so using both at the same time might lead to data loss.

The hidden volume to unlock is selected based on the provided password.

Errors

  • AesDecryptionFailed if the encrypted storage has not been opened before calling this method or the AES key has not been built
  • InvalidString if the provided password contains a null byte

Example

let mut manager = nitrokey::take()?;
let mut device = manager.connect_storage()?;
device.enable_encrypted_volume("123445")?;
match device.enable_hidden_volume("hidden-pw") {
    Ok(()) => println!("Enabled a hidden volume."),
    Err(err) => eprintln!("Could not enable the hidden volume: {}", err),
};

pub fn disable_hidden_volume(&mut self) -> Result<(), Error>[src]

Disables a hidden storage volume.

Once the volume is disabled, it can be no longer accessed as a block device. If no hidden volume has been enabled, this method still returns a success.

Example

fn use_volume() {}

let mut manager = nitrokey::take()?;
let mut device = manager.connect_storage()?;
device.enable_encrypted_volume("123445")?;
match device.enable_hidden_volume("hidden-pw") {
    Ok(()) => {
        println!("Enabled the hidden volume.");
        use_volume();
        match device.disable_hidden_volume() {
            Ok(()) => println!("Disabled the hidden volume."),
            Err(err) => {
                eprintln!("Could not disable the hidden volume: {}", err);
            },
        };
    },
    Err(err) => eprintln!("Could not enable the hidden volume: {}", err),
};

pub fn create_hidden_volume(
    &mut self,
    slot: u8,
    start: u8,
    end: u8,
    password: &str
) -> Result<(), Error>
[src]

Creates a hidden volume.

The volume is crated in the given slot and in the given range of the available memory, where start is the start position as a percentage of the available memory, and end is the end position as a percentage of the available memory. The volume will be protected by the given password.

Note that the encrypted and the hidden volumes operate on the same storage area, so using both at the same time might lead to data loss.

According to the libnitrokey documentation, this function only works if the encrypted storage has been opened.

Errors

  • AesDecryptionFailed if the encrypted storage has not been opened before calling this method or the AES key has not been built
  • InvalidString if the provided password contains a null byte

Example

let mut manager = nitrokey::take()?;
let mut device = manager.connect_storage()?;
device.enable_encrypted_volume("123445")?;
device.create_hidden_volume(0, 0, 100, "hidden-pw")?;

pub fn set_unencrypted_volume_mode(
    &mut self,
    admin_pin: &str,
    mode: VolumeMode
) -> Result<(), Error>
[src]

Sets the access mode of the unencrypted volume.

This command will reconnect the unencrypted volume so buffers should be flushed before calling it. Since firmware version v0.51, this command requires the admin PIN. Older firmware versions are not supported.

Errors

Example

use nitrokey::VolumeMode;

let mut manager = nitrokey::take()?;
let mut device = manager.connect_storage()?;
match device.set_unencrypted_volume_mode("12345678", VolumeMode::ReadWrite) {
    Ok(()) => println!("Set the unencrypted volume to read-write mode."),
    Err(err) => eprintln!("Could not set the unencrypted volume to read-write mode: {}", err),
};

pub fn set_encrypted_volume_mode(
    &mut self,
    admin_pin: &str,
    mode: VolumeMode
) -> Result<(), Error>
[src]

Sets the access mode of the encrypted volume.

This command will reconnect the encrypted volume so buffers should be flushed before calling it. It is only available in firmware version 0.49.

Errors

Example

use nitrokey::VolumeMode;

let mut manager = nitrokey::take()?;
let mut device = manager.connect_storage()?;
match device.set_encrypted_volume_mode("12345678", VolumeMode::ReadWrite) {
    Ok(()) => println!("Set the encrypted volume to read-write mode."),
    Err(err) => eprintln!("Could not set the encrypted volume to read-write mode: {}", err),
};

pub fn get_storage_status(&self) -> Result<StorageStatus, Error>[src]

Returns the status of the connected storage device.

Example

fn use_volume() {}

let mut manager = nitrokey::take()?;
let device = manager.connect_storage()?;
match device.get_storage_status() {
    Ok(status) => {
        println!("SD card ID: {:#x}", status.serial_number_sd_card);
    },
    Err(err) => eprintln!("Could not get Storage status: {}", err),
};

pub fn get_production_info(&self) -> Result<StorageProductionInfo, Error>[src]

Returns the production information for the connected storage device.

Example

fn use_volume() {}

let mut manager = nitrokey::take()?;
let device = manager.connect_storage()?;
match device.get_production_info() {
    Ok(data) => {
        println!("SD card ID:   {:#x}", data.sd_card.serial_number);
        println!("SD card size: {} GB", data.sd_card.size);
    },
    Err(err) => eprintln!("Could not get Storage production info: {}", err),
};

pub fn clear_new_sd_card_warning(
    &mut self,
    admin_pin: &str
) -> Result<(), Error>
[src]

Clears the warning for a new SD card.

The Storage status contains a field for a new SD card warning. After a factory reset, the field is set to true. After filling the SD card with random data, it is set to false. This method can be used to set it to false without filling the SD card with random data.

Errors

Example

let mut manager = nitrokey::take()?;
let mut device = manager.connect_storage()?;
match device.clear_new_sd_card_warning("12345678") {
    Ok(()) => println!("Cleared the new SD card warning."),
    Err(err) => eprintln!("Could not set the clear the new SD card warning: {}", err),
};

pub fn get_sd_card_usage(&self) -> Result<Range<u8>, Error>[src]

Returns a range of the SD card that has not been used to during this power cycle.

The Nitrokey Storage tracks read and write access to the SD card during a power cycle. This method returns a range of the SD card that has not been accessed during this power cycle. The range is relative to the total size of the SD card, so both values are less than or equal to 100. This can be used as a guideline when creating a hidden volume.

Example

let mut manager = nitrokey::take()?;
let storage = manager.connect_storage()?;
let usage = storage.get_sd_card_usage()?;
println!("SD card usage: {}..{}", usage.start, usage.end);

pub fn wink(&mut self) -> Result<(), Error>[src]

Blinks the red and green LED alternatively and infinitely until the device is reconnected.

pub fn get_operation_status(&self) -> Result<OperationStatus, Error>[src]

Returns the status of an ongoing background operation on the Nitrokey Storage.

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.

pub fn fill_sd_card(&mut self, admin_pin: &str) -> Result<(), Error>[src]

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

Example

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;
        }
    }
}

pub fn export_firmware(&mut self, admin_pin: &str) -> Result<(), Error>[src]

Exports the firmware to the unencrypted volume.

This command requires the admin PIN. The unencrypted volume must be in read-write mode when this command is executed. Otherwise, it will still return Ok but not write the firmware.

This command unmounts the unencrypted volume if it has been mounted, so all buffers should be flushed. The firmware is written to the firmware.bin file on the unencrypted volume.

Errors

Trait Implementations

impl<'a> Authenticate<'a> for Storage<'a>[src]

impl<'a> Debug for Storage<'a>[src]

impl<'a> Device<'a> for Storage<'a>[src]

impl<'a> Drop for Storage<'a>[src]

impl<'a> From<Storage<'a>> for DeviceWrapper<'a>[src]

impl<'a> GenerateOtp for Storage<'a>[src]

impl<'a> GetPasswordSafe<'a> for Storage<'a>[src]

Auto Trait Implementations

impl<'a> RefUnwindSafe for Storage<'a>

impl<'a> Send for Storage<'a>

impl<'a> Sync for Storage<'a>

impl<'a> Unpin for Storage<'a>

impl<'a> !UnwindSafe for Storage<'a>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.