Skip to content

Commit 3101070

Browse files
committed
Added has compute and compare
Signed-off-by: Samuel Bailey <samuel.bailey@arm.com>
1 parent 2219d8b commit 3101070

File tree

2 files changed

+99
-1
lines changed

2 files changed

+99
-1
lines changed

src/core/basic_client.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use parsec_interface::operations::ping::Operation as Ping;
1313
use parsec_interface::operations::psa_aead_decrypt::Operation as PsaAeadDecrypt;
1414
use parsec_interface::operations::psa_aead_encrypt::Operation as PsaAeadEncrypt;
1515
use parsec_interface::operations::psa_algorithm::{
16-
Aead, AsymmetricEncryption, AsymmetricSignature,
16+
Aead, AsymmetricEncryption, AsymmetricSignature, Hash,
1717
};
1818
use parsec_interface::operations::psa_asymmetric_decrypt::Operation as PsaAsymDecrypt;
1919
use parsec_interface::operations::psa_asymmetric_encrypt::Operation as PsaAsymEncrypt;
@@ -22,6 +22,8 @@ use parsec_interface::operations::psa_export_key::Operation as PsaExportKey;
2222
use parsec_interface::operations::psa_export_public_key::Operation as PsaExportPublicKey;
2323
use parsec_interface::operations::psa_generate_key::Operation as PsaGenerateKey;
2424
use parsec_interface::operations::psa_generate_random::Operation as PsaGenerateRandom;
25+
use parsec_interface::operations::psa_hash_compare::Operation as PsaHashCompare;
26+
use parsec_interface::operations::psa_hash_compute::Operation as PsaHashCompute;
2527
use parsec_interface::operations::psa_import_key::Operation as PsaImportKey;
2628
use parsec_interface::operations::psa_key_attributes::Attributes;
2729
use parsec_interface::operations::psa_sign_hash::Operation as PsaSignHash;
@@ -696,6 +698,70 @@ impl BasicClient {
696698
Err(Error::Client(ClientErrorKind::InvalidServiceResponseType))
697699
}
698700
}
701+
/// **[Cryptographic Operation]** Compute hash of a message.
702+
///
703+
/// The hash computation will be performed with the algorithm defined in `alg`.
704+
///
705+
/// # Errors
706+
///
707+
/// If the implicit client provider is `ProviderID::Core`, a client error
708+
/// of `InvalidProvider` type is returned.
709+
///
710+
/// If the implicit client provider has not been set, a client error of
711+
/// `NoProvider` type is returned.
712+
///
713+
/// See the operation-specific response codes returned by the service
714+
/// [here](https://parallaxsecond.github.io/parsec-book/parsec_client/operations/psa_hash_compute.html#specific-response-status-codes).
715+
pub fn psa_hash_compute(&self, alg: Hash, input: &[u8]) -> Result<Vec<u8>> {
716+
let crypto_provider = self.can_provide_crypto()?;
717+
let op = PsaHashCompute {
718+
alg,
719+
input: input.to_vec().into(),
720+
};
721+
let hash_compute_res = self.op_client.process_operation(
722+
NativeOperation::PsaHashCompute(op),
723+
crypto_provider,
724+
&self.auth_data,
725+
)?;
726+
if let NativeResult::PsaHashCompute(res) = hash_compute_res {
727+
Ok(res.hash.to_vec())
728+
} else {
729+
// Should really not be reached given the checks we do, but it's not impossible if some
730+
// changes happen in the interface
731+
Err(Error::Client(ClientErrorKind::InvalidServiceResponseType))
732+
}
733+
}
734+
735+
/// **[Cryptographic Operation]** Compute hash of a message and compare it with a reference value.
736+
///
737+
/// The hash computation will be performed with the algorithm defined in `alg`.
738+
///
739+
/// If this operation returns no error, the hash was computed successfully and it matches the reference value.
740+
///
741+
/// # Errors
742+
///
743+
/// If the implicit client provider is `ProviderID::Core`, a client error
744+
/// of `InvalidProvider` type is returned.
745+
///
746+
/// If the implicit client provider has not been set, a client error of
747+
/// `NoProvider` type is returned.
748+
///
749+
/// See the operation-specific response codes returned by the service
750+
/// [here](https://parallaxsecond.github.io/parsec-book/parsec_client/operations/psa_hash_compare.html#specific-response-status-codes).
751+
pub fn psa_hash_compare(&self, alg: Hash, input: &[u8], hash: &[u8]) -> Result<()> {
752+
let crypto_provider = self.can_provide_crypto()?;
753+
let op = PsaHashCompare {
754+
alg,
755+
input: input.to_vec().into(),
756+
hash: hash.to_vec().into(),
757+
};
758+
let _ = self.op_client.process_operation(
759+
NativeOperation::PsaHashCompare(op),
760+
crypto_provider,
761+
&self.auth_data,
762+
)?;
763+
Ok(())
764+
}
699765

700766
/// **[Cryptographic Operation]** Authenticate and encrypt a short message.
701767
///

src/core/testing/core_tests.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,38 @@ fn aead_decrypt_test() {
558558
}
559559
}
560560

561+
#[test]
562+
fn raw_key_agreement_test() {
563+
let mut client: TestBasicClient = Default::default();
564+
let key_name = String::from("key_name");
565+
let agreement_alg = RawKeyAgreement::Ecdh;
566+
let peer_key = vec![0x33_u8; 128];
567+
let shared_secret = vec![0xff_u8, 64];
568+
client.set_mock_read(&get_response_bytes_from_result(
569+
NativeResult::PsaRawKeyAgreement(operations::psa_raw_key_agreement::Result {
570+
shared_secret: Secret::new(shared_secret.clone()),
571+
}),
572+
));
573+
574+
// Check response
575+
assert_eq!(
576+
client
577+
.psa_raw_key_agreement(agreement_alg, key_name.clone(), &peer_key)
578+
.expect("Failed key agreement"),
579+
shared_secret
580+
);
581+
582+
// Check request:
583+
let op = get_operation_from_req_bytes(client.get_mock_write());
584+
if let NativeOperation::PsaRawKeyAgreement(op) = op {
585+
assert_eq!(op.private_key_name, key_name);
586+
assert_eq!(op.alg, agreement_alg);
587+
assert_eq!(*op.peer_key, peer_key);
588+
} else {
589+
panic!("Got wrong operation type: {:?}", op);
590+
}
591+
}
592+
561593
#[test]
562594
fn different_response_type_test() {
563595
let mut client: TestBasicClient = Default::default();

0 commit comments

Comments
 (0)