Skip to content

Commit

Permalink
use Signature::parse_standard_slice() to parse signature bytes
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <jun.kimura@datachain.jp>
  • Loading branch information
bluele committed Nov 19, 2024
1 parent 478277f commit 5d104a9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 8 additions & 0 deletions modules/crypto/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ define_error! {
format_args!("invalid address length: expected=20 actual={}", e.length)
},

InvalidSignatureLength
{
length: usize,
}
|e| {
format_args!("invalid signature length: expected=65 actual={}", e.length)
},

InsufficientSecretKeySize
{
path: String,
Expand Down
6 changes: 4 additions & 2 deletions modules/crypto/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,15 @@ impl Verifier for EnclavePublicKey {
}

pub fn verify_signature(sign_bytes: &[u8], signature: &[u8]) -> Result<EnclavePublicKey, Error> {
assert!(signature.len() == 65);
if signature.len() != 65 {
return Err(Error::invalid_signature_length(signature.len()));
}

let sign_hash = keccak256(sign_bytes);
let mut s = Scalar::default();
let _ = s.set_b32(&sign_hash);

let sig = Signature::parse_overflowing_slice(&signature[..64]).map_err(Error::secp256k1)?;
let sig = Signature::parse_standard_slice(&signature[..64]).map_err(Error::secp256k1)?;
let rid = RecoveryId::parse(signature[64]).map_err(Error::secp256k1)?;
let signer = libsecp256k1::recover(&Message(s), &sig, &rid).map_err(Error::secp256k1)?;
Ok(EnclavePublicKey(signer))
Expand Down

0 comments on commit 5d104a9

Please sign in to comment.