Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove use of PassthroughDigest from EthereumPersonalSignature2021 #337

Merged
merged 2 commits into from
Oct 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions did-pkh/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,18 @@ mod tests {
)
.await;

// eth/epsig
credential_prove_verify_did_pkh(
key_secp256k1_eip712sig.clone(),
other_key_secp256k1.clone(),
"eip155",
"#blockchainAccountId",
&ssi::ldp::EthereumPersonalSignature2021,
None,
None,
)
.await;

// eth/Eip712
let eip712_domain: ssi::eip712::ProofInfo = serde_json::from_value(json!({
"messageSchema": {
Expand Down
8 changes: 6 additions & 2 deletions src/keccak_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ pub fn hash_public_key(jwk: &JWK) -> Result<String, Error> {
Ok(hash_last20_hex)
}

pub fn hash_personal_message(msg: &str) -> Vec<u8> {
pub fn prefix_personal_message(msg: &str) -> Vec<u8> {
let msg_bytes = msg.as_bytes();
let prefix = format!("\x19Ethereum Signed Message:\n{}", msg_bytes.len());
let data = [prefix.as_bytes().to_vec(), msg_bytes.to_vec()].concat();
[prefix.as_bytes().to_vec(), msg_bytes.to_vec()].concat()
}

pub fn hash_personal_message(msg: &str) -> Vec<u8> {
let data = prefix_personal_message(msg);
keccak(data).to_fixed_bytes().to_vec()
}

Expand Down
13 changes: 5 additions & 8 deletions src/ldp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1283,24 +1283,22 @@ impl ProofSuite for EthereumPersonalSignature2021 {
key: &JWK,
extra_proof_properties: Option<Map<String, Value>>,
) -> Result<Proof, Error> {
use crate::passthrough_digest::PassthroughDigest;
use k256::ecdsa::signature::{digest::Digest, DigestSigner};
use k256::ecdsa::signature::Signer;
let mut proof = Proof {
context: serde_json::json!([EPSIG_CONTEXT.clone()]),
..Proof::new("EthereumPersonalSignature2021")
.with_options(options)
.with_properties(extra_proof_properties)
};
let signing_string = string_from_document_and_options(document, &proof).await?;
let hash = crate::keccak_hash::hash_personal_message(&signing_string);
let hash = crate::keccak_hash::prefix_personal_message(&signing_string);
let ec_params = match &key.params {
JWKParams::EC(ec) => ec,
_ => return Err(Error::KeyTypeNotImplemented),
};
let secret_key = k256::SecretKey::try_from(ec_params)?;
let signing_key = k256::ecdsa::SigningKey::from(secret_key);
let digest = Digest::chain(<PassthroughDigest as Digest>::new(), &hash);
let sig: k256::ecdsa::recoverable::Signature = signing_key.try_sign_digest(digest)?;
let sig: k256::ecdsa::recoverable::Signature = signing_key.try_sign(&hash)?;
let sig_bytes = &mut sig.as_ref().to_vec();
// Recovery ID starts at 27 instead of 0.
sig_bytes[64] += 27;
Expand Down Expand Up @@ -1371,9 +1369,8 @@ impl ProofSuite for EthereumPersonalSignature2021 {
let sig = k256::ecdsa::Signature::try_from(&dec_sig[..64])?;
let sig = k256::ecdsa::recoverable::Signature::new(&sig, rec_id)?;
let signing_string = string_from_document_and_options(document, proof).await?;
let hash = crate::keccak_hash::hash_personal_message(&signing_string);
let digest = k256::elliptic_curve::FieldBytes::<k256::Secp256k1>::from_slice(&hash);
let recovered_key = sig.recover_verify_key_from_digest_bytes(digest)?;
let hash = crate::keccak_hash::prefix_personal_message(&signing_string);
let recovered_key = sig.recover_verify_key(&hash)?;
use crate::jwk::ECParams;
let jwk = JWK {
params: JWKParams::EC(ECParams::try_from(&k256::PublicKey::from_sec1_bytes(
Expand Down