Repository: google/OpenSK
File: libraries/opensk/src/ctap/storage.rs (Rust)

Code before:
use crate::api::key_store::KeyStore;
use crate::ctap::client_pin::PIN_AUTH_LENGTH;
use crate::ctap::data_formats::{
    extract_array, extract_text_string, PublicKeyCredentialSource, PublicKeyCredentialUserEntity,
};
use crate::ctap::status_code::Ctap2StatusCode;
use crate::ctap::INITIAL_SIGNATURE_COUNTER;
use crate::env::Env;
use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
use arrayref::array_ref;
use core::cmp;
use core::convert::TryInto;
use persistent_store::{fragment, StoreUpdate};
#[cfg(any(feature = "config_command", feature = "std"))]
use sk_cbor::cbor_array_vec;

/// Wrapper for PIN properties.
struct PinProperties {
    /// 16 byte prefix of SHA256 of the currently set PIN.
    hash: [u8; PIN_AUTH_LENGTH],

    /// Length of the current PIN in code points.
    // This field is unused when compiled without config_command.
    #[allow(dead_code)]
    code_point_length: u8,
}

/// Initializes the store by creating missing objects.
pub fn init(env: &mut impl Env) -> Result<(), Ctap2StatusCode> {
    env.key_store().init()?;
    Ok(())
}

/// Returns the credential at the given key.
///
/// # Errors
///
/// Returns `CTAP2_ERR_VENDOR_INTERNAL_ERROR` if the key does not hold a valid credential.
pub fn get_credential(
    env: &mut impl Env,
    key: usize,
) -> Result<PublicKeyCredentialSource, Ctap2StatusCode> {
    let min_key = key::CREDENTIALS.start;
    if key < min_key || key >= min_key + env.customization().max_supported_resident_keys() {
        return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR);
    }
    let credential_entry = env
        .store()
        .find(key)?

Review comment (suggestion):
This could become `[cfg_attr(not(feature = "config_command"), allow(dead_code))]` when `std` is moved out of the equation.

Code after:
use crate::api::key_store::KeyStore;
use crate::ctap::client_pin::PIN_AUTH_LENGTH;
use crate::ctap::data_formats::{
    extract_array, extract_text_string, PublicKeyCredentialSource, PublicKeyCredentialUserEntity,
};
use crate::ctap::status_code::Ctap2StatusCode;
use crate::ctap::INITIAL_SIGNATURE_COUNTER;
use crate::env::Env;
use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
use arrayref::array_ref;
use core::cmp;
use core::convert::TryInto;
use persistent_store::{fragment, StoreUpdate};
#[cfg(feature = "config_command")]
use sk_cbor::cbor_array_vec;

/// Wrapper for PIN properties.
struct PinProperties {
    /// 16 byte prefix of SHA256 of the currently set PIN.
    hash: [u8; PIN_AUTH_LENGTH],

    /// Length of the current PIN in code points.
    #[cfg_attr(not(feature = "config_command"), allow(dead_code))]
    code_point_length: u8,
}

/// Initializes the store by creating missing objects.
pub fn init(env: &mut impl Env) -> Result<(), Ctap2StatusCode> {
    env.key_store().init()?;
    Ok(())
}

/// Returns the credential at the given key.
///
/// # Errors
///
/// Returns `CTAP2_ERR_VENDOR_INTERNAL_ERROR` if the key does not hold a valid credential.
pub fn get_credential(
    env: &mut impl Env,
    key: usize,
) -> Result<PublicKeyCredentialSource, Ctap2StatusCode> {
    let min_key = key::CREDENTIALS.start;
    if key < min_key || key >= min_key + env.customization().max_supported_resident_keys() {
        return Err(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR);
    }
    let credential_entry = env
        .store()
        .find(key)?
        .ok_or(Ctap2StatusCode::CTAP2_ERR_VENDOR_INTERNAL_ERROR)?;