Skip to content

Commit

Permalink
[WIP] Convert signature to ecdsa::Signature
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Gautier <[email protected]>
  • Loading branch information
baloo committed Aug 4, 2024
1 parent 412d14a commit ef81f09
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tss-esapi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ regex = "1.3.9"
zeroize = { version = "1.5.7", features = ["zeroize_derive"] }
tss-esapi-sys = { path = "../tss-esapi-sys", version = "0.5.0" }
x509-cert = { version = "0.2.0", optional = true }
ecdsa = { version = "0.16.9", optional = true }
elliptic-curve = { version = "0.13.8", optional = true, features = ["alloc", "pkcs8"] }
p192 = { version = "0.13.0", optional = true }
p224 = { version = "0.13.2", optional = true }
Expand Down Expand Up @@ -58,5 +59,5 @@ semver = "1.0.7"
[features]
default = ["abstraction"]
generate-bindings = ["tss-esapi-sys/generate-bindings"]
abstraction = ["elliptic-curve", "rsa", "x509-cert", "p192", "p224", "p256", "p384", "p521", "sha1", "sha2", "sha3", "sm2", "sm3"]
abstraction = ["ecdsa", "elliptic-curve", "rsa", "x509-cert", "p192", "p224", "p256", "p384", "p521", "sha1", "sha2", "sha3", "sm2", "sm3"]
integration-tests = ["strum", "strum_macros"]
1 change: 1 addition & 0 deletions tss-esapi/src/abstraction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod public;
pub mod transient;

mod hashing;
mod signatures;
pub use hashing::AssociatedHashingAlgorithm;

use std::convert::TryFrom;
Expand Down
42 changes: 42 additions & 0 deletions tss-esapi/src/abstraction/signatures.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2024 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0

use crate::{structures::EccSignature, Error, Result, WrapperErrorKind};

use std::convert::TryFrom;

use ecdsa::SignatureSize;
use elliptic_curve::{
generic_array::{typenum::Unsigned, ArrayLength},
FieldBytes, FieldBytesSize, PrimeCurve,
};

impl<C> TryFrom<EccSignature> for ecdsa::Signature<C>
where
C: PrimeCurve,
SignatureSize<C>: ArrayLength<u8>,
{
type Error = Error;

fn try_from(signature: EccSignature) -> Result<Self> {
let r = signature.signature_r().as_slice();
let s = signature.signature_s().as_slice();

if r.len() != FieldBytesSize::<C>::USIZE {
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
}
if s.len() != FieldBytesSize::<C>::USIZE {
return Err(Error::local_error(WrapperErrorKind::InvalidParam));
}

let signature = ecdsa::Signature::from_scalars(
FieldBytes::<C>::from_slice(r).clone(),
FieldBytes::<C>::from_slice(s).clone(),
)
.map_err(|_| Error::local_error(WrapperErrorKind::InvalidParam))?;
Ok(signature)
}
}

// TODO(baloo): impl TryFrom<RsaSignature> for rsa::pkcs1v15::Signature
// TODO(baloo): impl TryFrom<RsaSignature> for rsa::pss::Signature

0 comments on commit ef81f09

Please sign in to comment.