Skip to content

Commit

Permalink
runtime-sdk: harmonize signature impls
Browse files Browse the repository at this point in the history
  • Loading branch information
nhynes committed Oct 6, 2023
1 parent ff72bb4 commit dac06fb
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 36 deletions.
29 changes: 7 additions & 22 deletions runtime-sdk/src/crypto/signature/secp256k1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,8 @@ impl PublicKey {

/// Construct a public key from a slice of bytes.
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
if bytes.len() != 33 {
return Err(Error::MalformedPublicKey);
}
let ep = k256::EncodedPoint::from_bytes(bytes).map_err(|_| Error::MalformedPublicKey)?;
if !ep.is_compressed() {
// This should never happen due to the size check above.
return Err(Error::MalformedPublicKey);
}
Ok(PublicKey(ep))
Expand All @@ -54,21 +50,10 @@ impl PublicKey {
message: &[u8],
signature: &Signature,
) -> Result<(), Error> {
// Note that we must use Sha512_256 instead of our Hash here,
// even though it's the same thing, because it implements the Digest
// trait, so we can use verify_digest() below, which doesn't pre-hash
// the data (verify() does).
let mut digest = Sha512_256::new();
for byte in &[context, message] {
<Sha512_256 as Digest>::update(&mut digest, byte);
}
let sig = ecdsa::Signature::from_der(signature.0.as_ref())
.map_err(|_| Error::MalformedSignature)?;
let verify_key = ecdsa::VerifyingKey::from_encoded_point(&self.0)
.map_err(|_| Error::MalformedPublicKey)?;

verify_key
.verify_digest(digest, &sig)
let digest = Sha512_256::new()
.chain_update(context)
.chain_update(message);
self.verify_digest(digest, signature)
.map_err(|_| Error::VerificationFailed)
}

Expand Down Expand Up @@ -157,9 +142,9 @@ impl super::Signer for MemorySigner {
}

fn sign(&self, context: &[u8], message: &[u8]) -> Result<Signature, Error> {
let mut digest = Sha512_256::new();
<Sha512_256 as Digest>::update(&mut digest, context);
<Sha512_256 as Digest>::update(&mut digest, message);
let digest = Sha512_256::new()
.chain_update(context)
.chain_update(message);
let signature: ecdsa::Signature = self.sk.sign_digest(digest);
Ok(signature.to_der().as_bytes().to_vec().into())
}
Expand Down
10 changes: 3 additions & 7 deletions runtime-sdk/src/crypto/signature/secp256r1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@ impl PublicKey {

/// Construct a public key from a slice of bytes.
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
if bytes.len() != 33 {
return Err(Error::MalformedPublicKey);
}
let ep = p256::EncodedPoint::from_bytes(bytes).map_err(|_| Error::MalformedPublicKey)?;
if !ep.is_compressed() {
// This should never happen due to the size check above.
return Err(Error::MalformedPublicKey);
}
Ok(PublicKey(ep))
Expand Down Expand Up @@ -132,9 +128,9 @@ impl super::Signer for MemorySigner {
}

fn sign(&self, context: &[u8], message: &[u8]) -> Result<Signature, Error> {
let mut digest = Sha512_256::new();
<Sha512_256 as Digest>::update(&mut digest, context);
<Sha512_256 as Digest>::update(&mut digest, message);
let digest = sha2::Sha256::new()
.chain_update(context)
.chain_update(message);
let signature: ecdsa::Signature = self.sk.sign_digest(digest);
Ok(signature.to_der().as_bytes().to_vec().into())
}
Expand Down
11 changes: 4 additions & 7 deletions runtime-sdk/src/crypto/signature/secp384r1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ impl PublicKey {
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error> {
let ep = p384::EncodedPoint::from_bytes(bytes).map_err(|_| Error::MalformedPublicKey)?;
if !ep.is_compressed() {
// This should never happen due to the size check above.
return Err(Error::MalformedPublicKey);

Check warning on line 27 in runtime-sdk/src/crypto/signature/secp384r1.rs

View check run for this annotation

Codecov / codecov/patch

runtime-sdk/src/crypto/signature/secp384r1.rs#L27

Added line #L27 was not covered by tests
}
Ok(PublicKey(ep))
Expand All @@ -37,12 +36,10 @@ impl PublicKey {
message: &[u8],
signature: &Signature,
) -> Result<(), Error> {
self.verify_digest(
sha2::Sha384::new()
.chain_update(context)
.chain_update(message),
signature,
)
let digest = sha2::Sha384::new()

Check warning on line 39 in runtime-sdk/src/crypto/signature/secp384r1.rs

View check run for this annotation

Codecov / codecov/patch

runtime-sdk/src/crypto/signature/secp384r1.rs#L39

Added line #L39 was not covered by tests
.chain_update(context)
.chain_update(message);
self.verify_digest(digest, signature)

Check warning on line 42 in runtime-sdk/src/crypto/signature/secp384r1.rs

View check run for this annotation

Codecov / codecov/patch

runtime-sdk/src/crypto/signature/secp384r1.rs#L42

Added line #L42 was not covered by tests
}

/// Verify signature without using any domain separation scheme.
Expand Down

0 comments on commit dac06fb

Please sign in to comment.