Skip to content

Commit

Permalink
Replace status_to_result with a method on Status
Browse files Browse the repository at this point in the history
Signed-off-by: Hugues de Valon <hugues.devalon@arm.com>
  • Loading branch information
hug-dev committed May 19, 2020
1 parent 4d436fd commit 873ffb9
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
4 changes: 2 additions & 2 deletions psa-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub mod operations;
pub mod types;

use core::sync::atomic::{AtomicBool, Ordering};
use types::status::{status_to_result, Error, Result};
use types::status::{Error, Result, Status};

static INITIALISED: AtomicBool = AtomicBool::new(false);

Expand All @@ -54,7 +54,7 @@ static INITIALISED: AtomicBool = AtomicBool::new(false);
/// subsequent calls are guaranteed to succeed.
pub fn init() -> Result<()> {
// It it not a problem to call psa_crypto_init more than once.
status_to_result(unsafe { psa_crypto_sys::psa_crypto_init() })?;
Status::from(unsafe { psa_crypto_sys::psa_crypto_init() }).to_result()?;
let _ = INITIALISED.compare_and_swap(false, true, Ordering::Relaxed);

Ok(())
Expand Down
12 changes: 7 additions & 5 deletions psa-crypto/src/operations/asym_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use crate::initialized;
use crate::types::algorithm::{Algorithm, AsymmetricSignature};
use crate::types::key::Id;
use crate::types::status::{status_to_result, Result};
use crate::types::status::{Result, Status};

/// Sign a hash
pub fn sign_hash(
Expand All @@ -20,7 +20,7 @@ pub fn sign_hash(
let mut signature_length = 0;
let handle = key.handle()?;

status_to_result(unsafe {
Status::from(unsafe {
psa_crypto_sys::psa_asymmetric_sign(
handle,
Algorithm::from(alg).into(),
Expand All @@ -30,7 +30,8 @@ pub fn sign_hash(
signature.len(),
&mut signature_length,
)
})?;
})
.to_result()?;

key.close_handle(handle)?;

Expand All @@ -43,7 +44,7 @@ pub fn verify_hash(key: Id, alg: AsymmetricSignature, hash: &[u8], signature: &[

let handle = key.handle()?;

status_to_result(unsafe {
Status::from(unsafe {
psa_crypto_sys::psa_asymmetric_verify(
handle,
Algorithm::from(alg).into(),
Expand All @@ -52,7 +53,8 @@ pub fn verify_hash(key: Id, alg: AsymmetricSignature, hash: &[u8], signature: &[
signature.as_ptr(),
signature.len(),
)
})?;
})
.to_result()?;

key.close_handle(handle)
}
17 changes: 10 additions & 7 deletions psa-crypto/src/operations/key_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::initialized;
use crate::types::key::{Attributes, Id};
use crate::types::status::{status_to_result, Result};
use crate::types::status::{Result, Status};

/// Generate a key
pub fn generate_key(attributes: Attributes, id: Option<u32>) -> Result<Id> {
Expand All @@ -20,7 +20,8 @@ pub fn generate_key(attributes: Attributes, id: Option<u32>) -> Result<Id> {
};
let mut handle = 0;

status_to_result(unsafe { psa_crypto_sys::psa_generate_key(&attributes, &mut handle) })?;
Status::from(unsafe { psa_crypto_sys::psa_generate_key(&attributes, &mut handle) })
.to_result()?;

unsafe { psa_crypto_sys::psa_reset_key_attributes(&mut attributes) };

Expand All @@ -38,7 +39,7 @@ pub fn generate_key(attributes: Attributes, id: Option<u32>) -> Result<Id> {
pub unsafe fn destroy_key(key: Id) -> Result<()> {
initialized()?;
let handle = key.handle()?;
status_to_result(psa_crypto_sys::psa_destroy_key(handle))?;
Status::from(psa_crypto_sys::psa_destroy_key(handle)).to_result()?;
key.close_handle(handle)
}

Expand All @@ -55,9 +56,10 @@ pub fn import_key(attributes: Attributes, id: Option<u32>, data: &[u8]) -> Resul
};
let mut handle = 0;

status_to_result(unsafe {
Status::from(unsafe {
psa_crypto_sys::psa_import_key(&attributes, data.as_ptr(), data.len(), &mut handle)
})?;
})
.to_result()?;

unsafe { psa_crypto_sys::psa_reset_key_attributes(&mut attributes) };

Expand All @@ -73,14 +75,15 @@ pub fn export_public_key(key: Id, data: &mut [u8]) -> Result<usize> {
let handle = key.handle()?;
let mut data_length = 0;

status_to_result(unsafe {
Status::from(unsafe {
psa_crypto_sys::psa_export_public_key(
handle,
data.as_mut_ptr(),
data.len(),
&mut data_length,
)
})?;
})
.to_result()?;

key.close_handle(handle)?;

Expand Down
7 changes: 4 additions & 3 deletions psa-crypto/src/types/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#![allow(deprecated)]

use crate::types::algorithm::{Algorithm, Cipher};
use crate::types::status::{status_to_result, Error, Result};
use crate::types::status::{Error, Result, Status};
use log::error;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -394,7 +394,8 @@ impl Id {
Some(handle) => handle,
None => {
let mut handle = 0;
status_to_result(unsafe { psa_crypto_sys::psa_open_key(self.id, &mut handle) })?;
Status::from(unsafe { psa_crypto_sys::psa_open_key(self.id, &mut handle) })
.to_result()?;

handle
}
Expand All @@ -403,7 +404,7 @@ impl Id {

pub(crate) fn close_handle(self, handle: psa_crypto_sys::psa_key_handle_t) -> Result<()> {
if self.handle.is_none() {
status_to_result(unsafe { psa_crypto_sys::psa_close_key(handle) })
Status::from(unsafe { psa_crypto_sys::psa_close_key(handle) }).to_result()
} else {
Ok(())
}
Expand Down
17 changes: 10 additions & 7 deletions psa-crypto/src/types/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ pub enum Status {
Error(Error),
}

impl Status {
/// Convert the Status into a Result returning the empty tuple
pub fn to_result(self) -> Result<()> {
match self {
Status::Success => Ok(()),
Status::Error(error) => Err(error),
}
}
}

/// Definition of a PSA status code
#[derive(Clone, Copy, Debug)]
pub enum Error {
Expand Down Expand Up @@ -136,10 +146,3 @@ impl From<Status> for psa_crypto_sys::psa_status_t {
}
}
}

pub(crate) fn status_to_result(status: psa_crypto_sys::psa_status_t) -> Result<()> {
match status.into() {
Status::Success => Ok(()),
Status::Error(error) => Err(error),
}
}

0 comments on commit 873ffb9

Please sign in to comment.