Skip to content

Commit a61af21

Browse files
signature: ecdsa OSSL_FUNC_SIGNATURE_DIGEST_SIGN
Signed-off-by: Tomás González <tomasagustin.gonzalezorlando@arm.com>
1 parent 7893506 commit a61af21

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

parsec-openssl-provider/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ num-derive = "0.4.2"
2121
picky-asn1-x509 = "0.12.0"
2222
picky-asn1 = "0.8.0"
2323
picky-asn1-der = "0.4.0"
24+
serde = "1.0.123"

parsec-openssl-provider/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ openssl_errors::openssl_errors! {
120120
PROVIDER_KEYMGMT_VALIDATE("parsec_provider_kmgmt_validate");
121121
PROVIDER_QUERY("parsec_provider_query");
122122
PROVIDER_SIGNATURE_SIGN("parsec_provider_signature_sign");
123+
PROVIDER_SIGNATURE_DIGEST_SIGN("parsec_provider_signature_digest_sign");
123124
PROVIDER_SIGNATURE_DIGEST_SIGN_INIT("parsec_provider_signature_digest_sign_init");
124125
PROVIDER_TEARDOWN("parsec_provider_teardown");
125126
}

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@ use parsec_client::core::interface::operations::psa_algorithm::Hash;
1212
use parsec_client::core::interface::operations::psa_key_attributes::{Attributes, EccFamily, Type};
1313
use parsec_openssl2::types::VOID_PTR;
1414
use parsec_openssl2::*;
15+
use picky_asn1::wrapper::IntegerAsn1;
16+
use serde::{Deserialize, Serialize};
1517
use std::ffi::CStr;
1618
use std::sync::{Arc, RwLock};
1719

20+
#[derive(Serialize, Deserialize)]
21+
struct EccSignature {
22+
r: IntegerAsn1,
23+
s: IntegerAsn1,
24+
}
25+
1826
struct ParsecProviderSignatureContext {
1927
/* The key object is set in the signature context by calling OSSL_FUNC_signature_sign_init().
2028
Before calling OSSL_FUNC_signature_sign_init(), the key object itself should have been set up
@@ -88,7 +96,7 @@ unsafe extern "C" fn parsec_provider_signature_digest_sign(
8896
tbs: *const std::os::raw::c_uchar,
8997
tbslen: std::os::raw::c_uint,
9098
) -> std::os::raw::c_int {
91-
let result = super::r#catch(Some(|| super::Error::PROVIDER_SIGNATURE_SIGN), || {
99+
let result = super::r#catch(Some(|| super::Error::PROVIDER_SIGNATURE_DIGEST_SIGN), || {
92100
if ctx.is_null() || siglen.is_null() {
93101
return Err("Received unexpected NULL pointer as an argument.".into());
94102
}
@@ -155,19 +163,23 @@ unsafe extern "C" fn parsec_provider_signature_digest_sign(
155163
.psa_hash_compute(Hash::Sha256, tbs_slice)
156164
.map_err(|e| format!("Parsec Client failed to hash: {:?}", e))?;
157165

158-
let sign_res: Vec<u8> = key_data
166+
let mut sign_res: Vec<u8> = key_data
159167
.get_provctx()
160168
.get_client()
161169
.psa_sign_hash(key_name, &hash_res, sign_algorithm)
162170
.map_err(|e| format!("Parsec Client failed to sign: {:?}", e))?;
163171

164-
if siglength != sign_res.len() {
165-
Err(format!("Unexpected signature length: {}", sign_res.len()).into())
166-
} else {
167-
std::ptr::copy(sign_res.as_ptr(), sig, sign_res.len());
168-
*siglen = sign_res.len() as u32;
169-
Ok(OPENSSL_SUCCESS)
172+
if sign_algorithm.is_ecc_alg() {
173+
let s = IntegerAsn1::from_bytes_be_unsigned(sign_res.split_off(sign_res.len() / 2));
174+
sign_res = picky_asn1_der::to_vec(&EccSignature {
175+
r: IntegerAsn1::from_bytes_be_unsigned(sign_res),
176+
s,
177+
})
178+
.map_err(|e| format!("Failed to convert ECC Signature: {:?}", e))?;
170179
}
180+
std::ptr::copy(sign_res.as_ptr(), sig, sign_res.len());
181+
*siglen = sign_res.len() as u32;
182+
Ok(OPENSSL_SUCCESS)
171183
});
172184

173185
match result {

0 commit comments

Comments
 (0)