Skip to content

Commit 7afcdc5

Browse files
tgonzalezorlandoarmgowthamsk-arm
authored andcommitted
signature: Small signature optimization
Only calculate the signature when necessary. When sigsize is smaller than the expected signature length, preemptively exit the function instead of doing that after having calculated the signature. This saves some computation power. Signed-off-by: Tomás González <tomasagustin.gonzalezorlando@arm.com>
1 parent 2677975 commit 7afcdc5

File tree

1 file changed

+11
-7
lines changed
  • parsec-openssl-provider/src/signature

1 file changed

+11
-7
lines changed

parsec-openssl-provider/src/signature/mod.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ unsafe extern "C" fn parsec_provider_signature_sign(
165165
return Ok(OPENSSL_SUCCESS);
166166
}
167167

168+
if (sigsize as usize) < siglength {
169+
return Err(format!(
170+
"Signature length is bigger than sigsize. Signature length: {}",
171+
siglength
172+
)
173+
.into());
174+
}
175+
168176
if tbs.is_null() {
169177
return Err("Received unexpected NULL pointer as an argument.".into());
170178
}
@@ -186,16 +194,12 @@ unsafe extern "C" fn parsec_provider_signature_sign(
186194
.psa_sign_hash(key_name, tbs_slice, sign_algorithm)
187195
.map_err(|e| format!("Parsec Client failed to sign: {:?}", e))?;
188196

189-
if sigsize >= sign_res.len() as u32 {
197+
if siglength != sign_res.len() {
198+
Err(format!("Unexpected signature length: {}", sign_res.len()).into())
199+
} else {
190200
std::ptr::copy(sign_res.as_ptr(), sig, sign_res.len());
191201
*siglen = sign_res.len() as u32;
192202
Ok(OPENSSL_SUCCESS)
193-
} else {
194-
Err(format!(
195-
"Signature length is bigger than sigsize. Signature length: {}",
196-
sign_res.len()
197-
)
198-
.into())
199203
}
200204
});
201205

0 commit comments

Comments
 (0)