Skip to content

Commit

Permalink
Fixes problem with sign requiring a HashcheckTicket.
Browse files Browse the repository at this point in the history
- This fixes parallaxsecond#475 by making the HashcheckTicket that was
  previously required in the ```sign``` context method
  optional instead. If it is ```None``` it is then internally
  converted into the HashcheckTicket version of the `Null ticket`
  before being converted to the corresponding TSS type.
  This has the benefit of removing the need to use the TSS type in
  order to create a `Null ticket`.

Signed-off-by: Jesper Brynolf <[email protected]>
  • Loading branch information
Superhepper committed Jan 18, 2024
1 parent 9c7eb46 commit 13f06e0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,66 @@ impl Context {
}

/// Sign a digest with a key present in the TPM and return the signature.
///
/// # Details
/// For signatures using a restricted key, a hashcheck must be provided. For unrestricted keys, this may be None.
///
/// # Parameters
/// `key_handle` - Handle to the key be used for signing.
/// `digest` - The digest that is going to be signed.
/// `scheme` - The scheme to use if the scheme for the key referenced by the key handle is null.
/// `validation` - An optional [HashcheckTicket] that proof that the digest was created by the TPM.
/// N.B. None will be treated as a "Null ticket".
/// # Example
///
/// ```rust
/// # use tss_esapi::{Context, TctiNameConf,
/// # interface_types::{
/// # algorithm::{HashingAlgorithm, RsaSchemeAlgorithm},
/// # key_bits::RsaKeyBits,
/// # resource_handles::Hierarchy,
/// # },
/// # structures::{RsaScheme, RsaExponent},
/// # utils::create_unrestricted_signing_rsa_public
/// # };
/// use tss_esapi::structures::SignatureScheme;
/// # let mut context =
/// # Context::new(
/// # TctiNameConf::from_environment_variable().expect("Failed to get TCTI"),
/// # ).expect("Failed to create Context");
/// # let signing_key_pub = create_unrestricted_signing_rsa_public(
/// # RsaScheme::create(RsaSchemeAlgorithm::RsaSsa, Some(HashingAlgorithm::Sha256))
/// # .expect("Failed to create RSA scheme"),
/// # RsaKeyBits::Rsa2048,
/// # RsaExponent::default(),
/// # )
/// # .expect("Failed to create an unrestricted signing rsa public structure");
/// # let unrestricted_signing_key_handle = context
/// # .execute_with_nullauth_session(|ctx| {
/// # ctx.create_primary(Hierarchy::Owner, signing_key_pub, None, None, None, None)
/// # })
/// # .unwrap()
/// # .key_handle;
/// # let digest = context.get_random(32).unwrap();
/// let signature = context.execute_with_nullauth_session(|ctx| {
/// ctx.sign(
/// unrestricted_signing_key_handle,
/// digest,
/// SignatureScheme::Null,
/// None,
/// )
/// })
/// .expect("Failed to sign digest");
/// ```
pub fn sign(
&mut self,
key_handle: KeyHandle,
digest: Digest,
scheme: SignatureScheme,
validation: Option<HashcheckTicket>,
validation: impl Into<Option<HashcheckTicket>>,
) -> Result<Signature> {
let mut signature_ptr = null_mut();
let validation_ticket = validation.unwrap_or_default().try_into()?;
let validation_ticket = validation.into().unwrap_or_default().try_into()?;
ReturnCode::ensure_success(
unsafe {
Esys_Sign(
Expand Down
4 changes: 2 additions & 2 deletions tss-esapi/src/structures/tickets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ pub struct HashcheckTicket {
impl Default for HashcheckTicket {
/// The default for the Hashcheck ticket is the Null ticket.
fn default() -> Self {
return Self {
Self {
tag: StructureTag::Hashcheck,
hierarchy: Hierarchy::Null,
digest: Vec::<u8>::new(),
};
}
}
}

Expand Down
10 changes: 3 additions & 7 deletions tss-esapi/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,9 @@ impl TryFrom<TPMS_CONTEXT> for TpmsContext {
hierarchy: tss2_context.hierarchy,
context_blob: tss2_context.contextBlob.buffer.to_vec(),
};
context.context_blob.truncate(
tss2_context
.contextBlob
.size
.try_into()
.map_err(|_| Error::local_error(WrapperErrorKind::WrongParamSize))?,
);
context
.context_blob
.truncate(tss2_context.contextBlob.size.into());
Ok(context)
}
}
Expand Down

0 comments on commit 13f06e0

Please sign in to comment.