From b778064aeedb2b63271b43a6cc8214ab014d46dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20B=C4=99za?= Date: Fri, 20 Dec 2024 12:19:19 +0100 Subject: [PATCH] feat(tee-key-preexec): add support for Solidity-compatible pubkey in report_data --- Cargo.lock | 1 + Cargo.toml | 1 + bin/tee-key-preexec/src/main.rs | 23 +++++++++++++++------ bin/verify-era-proof-attestation/Cargo.toml | 1 + 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 355096c..0477e6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5795,6 +5795,7 @@ dependencies = [ "secp256k1 0.29.1", "serde", "serde_with 3.9.0", + "sha3", "teepot", "tokio", "tracing", diff --git a/Cargo.toml b/Cargo.toml index d36677f..732d266 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -47,6 +47,7 @@ serde = { version = "1", features = ["derive", "rc"] } serde_json = "1" serde_with = { version = "3.8", features = ["base64", "hex"] } sha2 = "0.10.8" +sha3 = "0.10.8" signature = "2.2.0" tdx-attest-rs = { version = "0.1.2", git = "https://github.com/intel/SGXDataCenterAttestationPrimitives.git", rev = "aa239d25a437a28f3f4de92c38f5b6809faac842" } teepot = { path = "crates/teepot" } diff --git a/bin/tee-key-preexec/src/main.rs b/bin/tee-key-preexec/src/main.rs index 70be9c4..120ea07 100644 --- a/bin/tee-key-preexec/src/main.rs +++ b/bin/tee-key-preexec/src/main.rs @@ -9,6 +9,7 @@ use anyhow::{Context, Result}; use clap::Parser; use secp256k1::{rand, Keypair, PublicKey, Secp256k1, SecretKey}; +use sha3::Keccak256; use std::{ffi::OsString, os::unix::process::CommandExt, process::Command}; use teepot::quote::get_quote; use tracing::error; @@ -28,6 +29,19 @@ struct Args { cmd_args: Vec, } +/// Converts a public key into an Ethereum address by hashing the encoded public key with Keccak256. +pub fn public_key_to_address(public: &PublicKey) -> [u8; 20] { + let public_key_bytes = public.serialize_uncompressed(); + + // Skip the first byte (0x04) which indicates uncompressed key + let hash = Keccak256::digest(&public_key_bytes[1..]).into(); + + // Take the last 20 bytes of the hash to get the Ethereum address + let mut address = [0u8; 20]; + address.copy_from_slice(&hash[12..]); + address +} + fn main_with_error() -> Result<()> { LogTracer::init().context("Failed to set logger")?; @@ -37,14 +51,11 @@ fn main_with_error() -> Result<()> { tracing::subscriber::set_global_default(subscriber).context("Failed to set logger")?; let args = Args::parse(); - let mut rng = rand::thread_rng(); let secp = Secp256k1::new(); - let keypair = Keypair::new(&secp, &mut rng); - let signing_key = SecretKey::from_keypair(&keypair); - let verifying_key = PublicKey::from_keypair(&keypair); - let verifying_key_bytes = verifying_key.serialize(); - let tee_type = match get_quote(verifying_key_bytes.as_ref()) { + let (signing_key, verifying_key) = secp.generate_keypair(&mut rng); + let ethereum_address = public_key_to_address(&verifying_key); + let tee_type = match get_quote(ethereum_address.as_ref()) { Ok((tee_type, quote)) => { // save quote to file std::fs::write(TEE_QUOTE_FILE, quote)?; diff --git a/bin/verify-era-proof-attestation/Cargo.toml b/bin/verify-era-proof-attestation/Cargo.toml index acd88a9..2a8cc62 100644 --- a/bin/verify-era-proof-attestation/Cargo.toml +++ b/bin/verify-era-proof-attestation/Cargo.toml @@ -17,6 +17,7 @@ reqwest.workspace = true secp256k1.workspace = true serde.workspace = true serde_with = { workspace = true, features = ["hex"] } +sha3.workspace = true teepot.workspace = true tokio.workspace = true tracing.workspace = true