Skip to content

Commit 527f4bb

Browse files
committed
Add QuoteParser library for parsing TDXQuotes
We need a way to extract the data flashtestations will use for the AllowList and workloadIds, and this library does that for us. It relies heavily on existing parsing logic given by Automata's TD10ReportBody struct
1 parent df5210d commit 527f4bb

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

script/TEERegistry.s.sol

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {Script, console} from "forge-std/Script.sol";
55
import {TEERegistry} from "../src/TEERegistry.sol";
66
import {AutomataDcapAttestationFee} from "automata-dcap-attestation/contracts/AutomataDcapAttestationFee.sol";
77

8+
89
contract TEERegistryScript is Script {
910
TEERegistry public registry;
1011
AutomataDcapAttestationFee public attestationFee;

src/TEERegistry.sol

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pragma solidity 0.8.28;
44
import "solmate/src/auth/Owned.sol";
55
import {TDXLibrary} from "./utils/TDXLibrary.sol";
66
import {AutomataDcapAttestationFee} from "../lib/automata-dcap-attestation/evm/contracts/AutomataDcapAttestationFee.sol";
7+
import {TD10ReportBody} from "automata-dcap-attestation/contracts/types/V4Structs.sol";
78

89
/**
910
* @title TEERegistry
@@ -92,8 +93,18 @@ contract TEERegistry is Owned {
9293
returns (bool, bytes memory)
9394
{
9495
(bool success, bytes memory output) = AutomataDcapAttestationFee(attestationFeeContract).verifyAndAttestOnChain(quote);
96+
9597
if (success) {
96-
isVerified = true;
98+
isVerified = true; // TODO: delete this once done testing
99+
100+
// since the verifyAndAttestOnChain call has succeeded, we can safely
101+
// decode the output into the report body struct. We implicitly assume
102+
// only V4 TDX quotes will be used here, and not SGX quotes. If you use
103+
// anything else, you're on your own
104+
TD10ReportBody memory td10ReportBody = abi.decode(output, (TD10ReportBody));
105+
106+
// TODO do flashtestations protocol, so far we've only verified that the quote is valid
107+
97108
}
98109

99110
return (success, output);

src/utils/QuoteParser.sol

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity 0.8.28;
3+
4+
import {TD10ReportBody} from "automata-dcap-attestation/contracts/types/V4Structs.sol";
5+
6+
library QuoteParser {
7+
// Intel TDX V4 byte lengths
8+
uint256 internal constant REPORT_DATA_FIELD_SIZE = 64;
9+
uint256 internal constant MIN_TD_REPORT_BODY_LENGTH = 584; // 584 bytes
10+
11+
// Extracts the Ethereum address from the REPORTDATA field of a TDReport
12+
// Assumes the first 20 bytes of REPORTDATA contain the address.
13+
function extractAddressFromReportDataBytes(bytes calldata reportDataBytes) internal pure returns (address) {
14+
require(reportDataBytes.length == REPORT_DATA_FIELD_SIZE, "QuoteParser: ReportData must be 64 bytes");
15+
address addr;
16+
// Extract the first 20 bytes for the address
17+
assembly {
18+
addr := mload(add(reportDataBytes.offset, 0x14)) // Load 32 bytes, address is in lower 20
19+
addr := shr(96, addr) // Right shift remaining 12 bytesto get the address (most significant 12 bytes are zeroed out)
20+
}
21+
return addr;
22+
}
23+
24+
function calculateWorkloadIdRaw(
25+
bytes memory mrtd,
26+
bytes memory rtmr0,
27+
bytes memory rtmr1,
28+
bytes memory rtmr2,
29+
bytes memory rtmr3,
30+
bytes memory mrowner,
31+
bytes memory mrownerconfig,
32+
bytes memory mrconfigid
33+
) internal pure returns (bytes32) {
34+
return keccak256(abi.encodePacked(mrtd, rtmr0, rtmr1, rtmr2, rtmr3, mrowner, mrownerconfig, mrconfigid));
35+
}
36+
37+
function calculateWorkloadId(TD10ReportBody calldata tdReportBody) internal pure returns (bytes32) {
38+
return calculateWorkloadIdRaw(
39+
tdReportBody.mrTd,
40+
tdReportBody.rtMr0,
41+
tdReportBody.rtMr1,
42+
tdReportBody.rtMr2,
43+
tdReportBody.rtMr3,
44+
tdReportBody.mrOwner,
45+
tdReportBody.mrOwnerConfig,
46+
tdReportBody.mrConfigId
47+
);
48+
}
49+
}

0 commit comments

Comments
 (0)