|
1 | 1 | use reqwest::Client; |
| 2 | +use sha3::{Digest, Keccak256}; |
2 | 3 | use tracing::info; |
3 | 4 |
|
4 | 5 | const DEBUG_QUOTE_SERVICE_URL: &str = "http://ns31695324.ip-141-94-163.eu:10080/attest"; |
5 | 6 |
|
| 7 | +// Automata serialized output structure constants |
| 8 | +// The output from Automata's verifyAndAttestOnChain has a 13-byte header: |
| 9 | +// quoteVersion (2 bytes) + tee (4 bytes) + tcbStatus (1 byte) + fmspcBytes (6 bytes) |
| 10 | +const SERIALIZED_OUTPUT_OFFSET: usize = 13; |
| 11 | +const TD_REPORT10_LENGTH: usize = 584; |
| 12 | + |
| 13 | +// TDX workload constants |
| 14 | +const TD_XFAM_FPU: u64 = 0x0000000000000001; |
| 15 | +const TD_XFAM_SSE: u64 = 0x0000000000000002; |
| 16 | +const TD_TDATTRS_VE_DISABLED: u64 = 0x0000000010000000; |
| 17 | +const TD_TDATTRS_PKS: u64 = 0x0000000040000000; |
| 18 | +const TD_TDATTRS_KL: u64 = 0x0000000080000000; |
| 19 | + |
6 | 20 | /// Configuration for attestation |
7 | 21 | #[derive(Default)] |
8 | 22 | pub struct AttestationConfig { |
@@ -63,3 +77,73 @@ pub fn get_attestation_provider(config: AttestationConfig) -> RemoteAttestationP |
63 | 77 | ) |
64 | 78 | } |
65 | 79 | } |
| 80 | + |
| 81 | +/// ComputeWorkloadID computes the workload ID from Automata's serialized verifier output |
| 82 | +/// This corresponds to QuoteParser.parseV4VerifierOutput in Solidity implementation |
| 83 | +/// https://github.com/flashbots/flashtestations/tree/7cc7f68492fe672a823dd2dead649793aac1f216 |
| 84 | +/// The workload ID uniquely identifies a TEE workload based on its measurement registers |
| 85 | +pub fn compute_workload_id(serialized_output: &[u8]) -> eyre::Result<[u8; 32]> { |
| 86 | + // Validate output length |
| 87 | + if serialized_output.len() < SERIALIZED_OUTPUT_OFFSET + TD_REPORT10_LENGTH { |
| 88 | + eyre::bail!( |
| 89 | + "invalid output length: {}, expected at least {}", |
| 90 | + serialized_output.len(), |
| 91 | + SERIALIZED_OUTPUT_OFFSET + TD_REPORT10_LENGTH |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + // Skip the 13-byte header to get to the TD10ReportBody |
| 96 | + let report_body = &serialized_output[SERIALIZED_OUTPUT_OFFSET..]; |
| 97 | + |
| 98 | + // Extract fields exactly as parseRawReportBody does in Solidity |
| 99 | + // Using hardcoded offsets to match Solidity implementation exactly |
| 100 | + let mr_td = &report_body[136..136 + 48]; |
| 101 | + let rt_mr0 = &report_body[328..328 + 48]; |
| 102 | + let rt_mr1 = &report_body[376..376 + 48]; |
| 103 | + let rt_mr2 = &report_body[424..424 + 48]; |
| 104 | + let rt_mr3 = &report_body[472..472 + 48]; |
| 105 | + let mr_config_id = &report_body[184..184 + 48]; |
| 106 | + |
| 107 | + // Extract xFAM and tdAttributes (8 bytes each) |
| 108 | + // In Solidity, bytes8 is treated as big-endian for bitwise operations |
| 109 | + let xfam = u64::from_be_bytes(report_body[128..128 + 8].try_into().unwrap()); |
| 110 | + let td_attributes = u64::from_be_bytes(report_body[120..120 + 8].try_into().unwrap()); |
| 111 | + |
| 112 | + // Apply transformations as per the Solidity implementation |
| 113 | + // expectedXfamBits = TD_XFAM_FPU | TD_XFAM_SSE |
| 114 | + let expected_xfam_bits = TD_XFAM_FPU | TD_XFAM_SSE; |
| 115 | + |
| 116 | + // ignoredTdAttributesBitmask = TD_TDATTRS_VE_DISABLED | TD_TDATTRS_PKS | TD_TDATTRS_KL |
| 117 | + let ignored_td_attributes_bitmask = TD_TDATTRS_VE_DISABLED | TD_TDATTRS_PKS | TD_TDATTRS_KL; |
| 118 | + |
| 119 | + // Transform xFAM: xFAM ^ expectedXfamBits |
| 120 | + let transformed_xfam = xfam ^ expected_xfam_bits; |
| 121 | + |
| 122 | + // Transform tdAttributes: tdAttributes & ~ignoredTdAttributesBitmask |
| 123 | + let transformed_td_attributes = td_attributes & !ignored_td_attributes_bitmask; |
| 124 | + |
| 125 | + // Convert transformed values to bytes (big-endian, to match Solidity bytes8) |
| 126 | + let xfam_bytes = transformed_xfam.to_be_bytes(); |
| 127 | + let td_attributes_bytes = transformed_td_attributes.to_be_bytes(); |
| 128 | + |
| 129 | + // Concatenate all fields |
| 130 | + let mut concatenated = Vec::new(); |
| 131 | + concatenated.extend_from_slice(mr_td); |
| 132 | + concatenated.extend_from_slice(rt_mr0); |
| 133 | + concatenated.extend_from_slice(rt_mr1); |
| 134 | + concatenated.extend_from_slice(rt_mr2); |
| 135 | + concatenated.extend_from_slice(rt_mr3); |
| 136 | + concatenated.extend_from_slice(mr_config_id); |
| 137 | + concatenated.extend_from_slice(&xfam_bytes); |
| 138 | + concatenated.extend_from_slice(&td_attributes_bytes); |
| 139 | + |
| 140 | + // Compute keccak256 hash |
| 141 | + let mut hasher = Keccak256::new(); |
| 142 | + hasher.update(&concatenated); |
| 143 | + let result = hasher.finalize(); |
| 144 | + |
| 145 | + let mut workload_id = [0u8; 32]; |
| 146 | + workload_id.copy_from_slice(&result); |
| 147 | + |
| 148 | + Ok(workload_id) |
| 149 | +} |
0 commit comments