fn new(key_id_store: Arc<RwLock<dyn ManageKeyIDs + Send + Sync>>) -> Option<MbedProvider> {
        // Safety: this function should be called before any of the other Mbed Crypto functions
        // are.
        if unsafe { psa_crypto_binding::psa_crypto_init() } != PSA_SUCCESS {
            error!("Error when initialising Mbed Crypto");
            return None;
        }
        let mbed_provider = MbedProvider {
            key_id_store,
            local_ids: RwLock::new(HashSet::new()),
            key_handle_mutex: Mutex::new(()),
            key_slot_semaphore: Semaphore::new(constants::PSA_KEY_SLOT_COUNT),
        };
        {
            // The local scope allows to drop store_handle and local_ids_handle in order to return
            // the mbed_provider.
            let mut store_handle = mbed_provider
                .key_id_store
                .write()
                .expect("Key store lock poisoned");
            let mut local_ids_handle = mbed_provider
                .local_ids
                .write()
                .expect("Local ID lock poisoned");
            let mut to_remove: Vec<KeyTriple> = Vec::new();
            // Go through all MbedProvider key triple to key ID mappings and check if they are still
            // present.
            // Delete those who are not present and add to the local_store the ones present.
            match store_handle.get_all(ProviderID::MbedCrypto) {
                Ok(key_triples) => {
                    for key_triple in key_triples.iter().cloned() {
                        let key_id = match get_key_id(key_triple, &*store_handle) {
                            Ok(key_id) => key_id,
                            Err(response_status) => {
                                error!("Error getting the Key ID for triple:\n{}\n(error: {}), continuing...", key_triple, response_status);
                                to_remove.push(key_triple.clone());
                                continue;
                            }
                        };

                        // Safety: safe because:
                        // * the Mbed Crypto library has been initialized
                        // * this code is executed only by the main thread
                        match unsafe { KeyHandle::open(key_id) } {
                            Ok(_) => {
                                let _ = local_ids_handle.insert(key_id);
                            }
                            Err(ResponseStatus::PsaErrorDoesNotExist) => {
                                to_remove.push(key_triple.clone())
                            }
                            Err(e) => {
                                error!("Error {} when opening a persistent Mbed Crypto key.", e);
                                return None;
                            }
                        };
                    }
                }
                Err(string) => {
                    error!("Key ID Manager error: {}", string);
                    return None;
                }
            };
            for key_triple in to_remove.iter() {
                if let Err(string) = store_handle.remove(key_triple) {
                    error!("Key ID Manager error: {}", string);
                    return None;
                }
            }
        }

        Some(mbed_provider)
    }
/// Creates and initialise a new instance of MbedProvider.
/// Checks if there are not more keys stored in the Key ID Manager than in the MbedProvider and
/// if there, delete them. Adds Key IDs currently in use in the local IDs store.
/// Returns `None` if the initialisation failed.
Creates and initialise a new instance of MbedProvider.
Checks if there are not more keys stored in the Key ID Manager than in the MbedProvider and
if there, delete them. Adds Key IDs currently in use in the local IDs store.
Returns `None` if the initialisation failed.
Creates and initialise a new instance of MbedProvider . Checks if there are not more keys stored in the Key ID Manager than in the MbedProvider and if there delete them . Adds Key IDs currently in use in the local IDs store . Returns ` None ` if the initialisation failed .
fn new(key_id_store: Arc<RwLock<dyn ManageKeyIDs + Send + Sync>>) -> Option<MbedProvider> {
        // Safety: this function should be called before any of the other Mbed Crypto functions
        // are.
        if unsafe { psa_crypto_binding::psa_crypto_init() } != PSA_SUCCESS {
            error!("Error when initialising Mbed Crypto");
            return None;
        }
        let mbed_provider = MbedProvider {
            key_id_store,
            local_ids: RwLock::new(HashSet::new()),
            key_handle_mutex: Mutex::new(()),
            key_slot_semaphore: Semaphore::new(constants::PSA_KEY_SLOT_COUNT),
        };
        {
            // The local scope allows to drop store_handle and local_ids_handle in order to return
            // the mbed_provider.
            let mut store_handle = mbed_provider
                .key_id_store
                .write()
                .expect("Key store lock poisoned");
            let mut local_ids_handle = mbed_provider
                .local_ids
                .write()
                .expect("Local ID lock poisoned");
            let mut to_remove: Vec<KeyTriple> = Vec::new();
            // Go through all MbedProvider key triple to key ID mappings and check if they are still
            // present.
            // Delete those who are not present and add to the local_store the ones present.
            match store_handle.get_all(ProviderID::MbedCrypto) {
                Ok(key_triples) => {
                    for key_triple in key_triples.iter().cloned() {
                        let key_id = match get_key_id(key_triple, &*store_handle) {
                            Ok(key_id) => key_id,
                            Err(response_status) => {
                                error!("Error getting the Key ID for triple:\n{}\n(error: {}), continuing...", key_triple, response_status);
                                to_remove.push(key_triple.clone());
                                continue;
                            }
                        };
                        // Safety: safe because:
                        // * the Mbed Crypto library has been initialized
                        // * this code is executed only by the main thread
                        match unsafe { KeyHandle::open(key_id) } {
                            Ok(_) => {
                                let _ = local_ids_handle.insert(key_id);
                            }
                            Err(ResponseStatus::PsaErrorDoesNotExist) => {
                                to_remove.push(key_triple.clone())
                            }
                            Err(e) => {
                                error!("Error {} when opening a persistent Mbed Crypto key.", e);
                                return None;
                            }
                        };
                    }
                }
                Err(string) => {
                    error!("Key ID Manager error: {}", string);
                    return None;
                }
            };
            for key_triple in to_remove.iter() {
                if let Err(string) = store_handle.remove(key_triple) {
                    error!("Key ID Manager error: {}", string);
                    return None;
                }
            }
        }
        Some(mbed_provider)
    }
fn new ( key_id_store : Arc < RwLock < dyn ManageKeyIDs + Send + Sync > > ) -> Option < MbedProvider > { // Safety: this function should be called before any of the other Mbed Crypto functions // are. if unsafe { psa_crypto_binding :: psa_crypto_init ( ) } != PSA_SUCCESS { error ! ( " " ) ; return None ; } let mbed_provider = MbedProvider { key_id_store , local_ids : RwLock :: new ( HashSet :: new ( ) ) , key_handle_mutex : Mutex :: new ( ( ) ) , key_slot_semaphore : Semaphore :: new ( constants :: PSA_KEY_SLOT_COUNT ) , } ; { // The local scope allows to drop store_handle and local_ids_handle in order to return // the mbed_provider. let mut store_handle = mbed_provider . key_id_store . write ( ) . expect ( " " ) ; let mut local_ids_handle = mbed_provider . local_ids . write ( ) . expect ( " " ) ; let mut to_remove : Vec < KeyTriple > = Vec :: new ( ) ; // Go through all MbedProvider key triple to key ID mappings and check if they are still // present. // Delete those who are not present and add to the local_store the ones present. match store_handle . get_all ( ProviderID :: MbedCrypto ) { Ok ( key_triples ) => { for key_triple in key_triples . iter ( ) . cloned ( ) { let key_id = match get_key_id ( key_triple , & * store_handle ) { Ok ( key_id ) => key_id , Err ( response_status ) => { error ! ( " \n \n " key_triple response_status ) ; to_remove . push ( key_triple . clone ( ) ) ; continue ; } } ; // Safety: safe because: // * the Mbed Crypto library has been initialized // * this code is executed only by the main thread match unsafe { KeyHandle :: open ( key_id ) } { Ok ( _ ) => { let _ = local_ids_handle . insert ( key_id ) ; } Err ( ResponseStatus :: PsaErrorDoesNotExist ) => { to_remove . push ( key_triple . clone ( ) ) } Err ( e ) => { error ! ( " " e ) ; return None ; } } ; } } Err ( string ) => { error ! ( " " string ) ; return None ; } } ; for key_triple in to_remove . iter ( ) { if let Err ( string ) = store_handle . remove ( key_triple ) { error ! ( " " string ) ; return None ; } } } Some ( mbed_provider ) }
Creates and initialise a new instance of MbedProvider.
Creates and initialise a new instance of MbedProvider .
key_id_store
Arc<RwLock<dyn ManageKeyIDs + Send + Sync>>
key_id_store
Arc<RwLock<dyn ManageKeyIDs + Send + Sync>>