Skip to content

Commit 13f06e0

Browse files
committed
Fixes problem with sign requiring a HashcheckTicket.
- This fixes #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 <jesper.brynolf@gmail.com>
1 parent 9c7eb46 commit 13f06e0

File tree

3 files changed

+58
-11
lines changed

3 files changed

+58
-11
lines changed

tss-esapi/src/context/tpm_commands/signing_and_signature_verification.rs

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,66 @@ impl Context {
4040
}
4141

4242
/// Sign a digest with a key present in the TPM and return the signature.
43+
///
44+
/// # Details
45+
/// For signatures using a restricted key, a hashcheck must be provided. For unrestricted keys, this may be None.
46+
///
47+
/// # Parameters
48+
/// `key_handle` - Handle to the key be used for signing.
49+
/// `digest` - The digest that is going to be signed.
50+
/// `scheme` - The scheme to use if the scheme for the key referenced by the key handle is null.
51+
/// `validation` - An optional [HashcheckTicket] that proof that the digest was created by the TPM.
52+
/// N.B. None will be treated as a "Null ticket".
53+
/// # Example
54+
///
55+
/// ```rust
56+
/// # use tss_esapi::{Context, TctiNameConf,
57+
/// # interface_types::{
58+
/// # algorithm::{HashingAlgorithm, RsaSchemeAlgorithm},
59+
/// # key_bits::RsaKeyBits,
60+
/// # resource_handles::Hierarchy,
61+
/// # },
62+
/// # structures::{RsaScheme, RsaExponent},
63+
/// # utils::create_unrestricted_signing_rsa_public
64+
/// # };
65+
/// use tss_esapi::structures::SignatureScheme;
66+
/// # let mut context =
67+
/// # Context::new(
68+
/// # TctiNameConf::from_environment_variable().expect("Failed to get TCTI"),
69+
/// # ).expect("Failed to create Context");
70+
/// # let signing_key_pub = create_unrestricted_signing_rsa_public(
71+
/// # RsaScheme::create(RsaSchemeAlgorithm::RsaSsa, Some(HashingAlgorithm::Sha256))
72+
/// # .expect("Failed to create RSA scheme"),
73+
/// # RsaKeyBits::Rsa2048,
74+
/// # RsaExponent::default(),
75+
/// # )
76+
/// # .expect("Failed to create an unrestricted signing rsa public structure");
77+
/// # let unrestricted_signing_key_handle = context
78+
/// # .execute_with_nullauth_session(|ctx| {
79+
/// # ctx.create_primary(Hierarchy::Owner, signing_key_pub, None, None, None, None)
80+
/// # })
81+
/// # .unwrap()
82+
/// # .key_handle;
83+
/// # let digest = context.get_random(32).unwrap();
84+
/// let signature = context.execute_with_nullauth_session(|ctx| {
85+
/// ctx.sign(
86+
/// unrestricted_signing_key_handle,
87+
/// digest,
88+
/// SignatureScheme::Null,
89+
/// None,
90+
/// )
91+
/// })
92+
/// .expect("Failed to sign digest");
93+
/// ```
4394
pub fn sign(
4495
&mut self,
4596
key_handle: KeyHandle,
4697
digest: Digest,
4798
scheme: SignatureScheme,
48-
validation: Option<HashcheckTicket>,
99+
validation: impl Into<Option<HashcheckTicket>>,
49100
) -> Result<Signature> {
50101
let mut signature_ptr = null_mut();
51-
let validation_ticket = validation.unwrap_or_default().try_into()?;
102+
let validation_ticket = validation.into().unwrap_or_default().try_into()?;
52103
ReturnCode::ensure_success(
53104
unsafe {
54105
Esys_Sign(

tss-esapi/src/structures/tickets.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ pub struct HashcheckTicket {
130130
impl Default for HashcheckTicket {
131131
/// The default for the Hashcheck ticket is the Null ticket.
132132
fn default() -> Self {
133-
return Self {
133+
Self {
134134
tag: StructureTag::Hashcheck,
135135
hierarchy: Hierarchy::Null,
136136
digest: Vec::<u8>::new(),
137-
};
137+
}
138138
}
139139
}
140140

tss-esapi/src/utils/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,9 @@ impl TryFrom<TPMS_CONTEXT> for TpmsContext {
5757
hierarchy: tss2_context.hierarchy,
5858
context_blob: tss2_context.contextBlob.buffer.to_vec(),
5959
};
60-
context.context_blob.truncate(
61-
tss2_context
62-
.contextBlob
63-
.size
64-
.try_into()
65-
.map_err(|_| Error::local_error(WrapperErrorKind::WrongParamSize))?,
66-
);
60+
context
61+
.context_blob
62+
.truncate(tss2_context.contextBlob.size.into());
6763
Ok(context)
6864
}
6965
}

0 commit comments

Comments
 (0)