|
29 | 29 | * |
30 | 30 | */ |
31 | 31 |
|
32 | | -use log::{debug, error, info}; |
| 32 | +use log::{debug, info}; |
33 | 33 | use std::env; |
34 | 34 | use std::fs; |
35 | 35 | mod error; |
36 | 36 | use error::QuoteGeneratorError; |
| 37 | +mod utils; |
| 38 | +use utils::{create_quote, create_report_data, create_tdx_report}; |
37 | 39 |
|
38 | 40 | const REPORT_DATA_SIZE: usize = 64; |
39 | | -const REPORT_SIZE: usize = 1024; |
40 | | -const TDX_UUID_SIZE: usize = 16; |
41 | | - |
42 | | -/// Creates a TDX report data structure from input bytes. |
43 | | -/// |
44 | | -/// # Arguments |
45 | | -/// |
46 | | -/// * `input_bytes` - A byte slice that **must be exactly `REPORT_DATA_SIZE` bytes long**. |
47 | | -/// In this binary, `main` guarantees this by copying/padding the user input into |
48 | | -/// a fixed-size `REPORT_DATA_SIZE` buffer before calling this function. |
49 | | -/// |
50 | | -/// # Returns |
51 | | -/// |
52 | | -/// A `Result` containing the `tdx_report_data_t` structure, or a `QuoteGeneratorError` |
53 | | -/// if the input bytes cannot be converted. |
54 | | -/// |
55 | | -/// # Errors |
56 | | -/// |
57 | | -/// Returns `QuoteGeneratorError::ReportDataConversion` if input bytes length doesn't match `REPORT_DATA_SIZE`. |
58 | | -fn create_report_data( |
59 | | - input_bytes: &[u8], |
60 | | -) -> Result<tdx_attest_rs::tdx_report_data_t, QuoteGeneratorError> { |
61 | | - let report_data = tdx_attest_rs::tdx_report_data_t { |
62 | | - d: input_bytes.try_into()?, |
63 | | - }; |
64 | | - Ok(report_data) |
65 | | -} |
66 | | - |
67 | | -/// Creates a TDX report from the given report data. |
68 | | -/// |
69 | | -/// # Arguments |
70 | | -/// |
71 | | -/// * `report_data` - The report data to use for generating the TDX report |
72 | | -/// |
73 | | -/// # Returns |
74 | | -/// |
75 | | -/// A `Result` containing the `tdx_report_t` structure on success. |
76 | | -/// |
77 | | -/// # Errors |
78 | | -/// |
79 | | -/// Returns `QuoteGeneratorError::TdxReportFailed` if the report generation fails. |
80 | | -fn create_tdx_report( |
81 | | - report_data: &tdx_attest_rs::tdx_report_data_t, |
82 | | -) -> Result<tdx_attest_rs::tdx_report_t, QuoteGeneratorError> { |
83 | | - let mut tdx_report = tdx_attest_rs::tdx_report_t { |
84 | | - d: [0; REPORT_SIZE], |
85 | | - }; |
86 | | - let result = tdx_attest_rs::tdx_att_get_report(Some(report_data), &mut tdx_report); |
87 | | - match result { |
88 | | - tdx_attest_rs::tdx_attest_error_t::TDX_ATTEST_SUCCESS => Ok(tdx_report), |
89 | | - _ => { |
90 | | - error!("Failed to get TDX report: {:?}", result); |
91 | | - Err(QuoteGeneratorError::TdxReportFailed) // _tdx_attest_error_t does not implement std::error::Error |
92 | | - } |
93 | | - } |
94 | | -} |
95 | | - |
96 | | -/// Creates a TDX attestation quote from the given report data. |
97 | | -/// |
98 | | -/// This function generates a cryptographic quote that can be used to verify |
99 | | -/// the integrity and authenticity of the TDX environment. |
100 | | -/// |
101 | | -/// # Arguments |
102 | | -/// |
103 | | -/// * `report_data` - The report data to include in the quote |
104 | | -/// |
105 | | -/// # Returns |
106 | | -/// |
107 | | -/// A `Result` containing the generated quote data as `Vec<u8>` on success. |
108 | | -/// |
109 | | -/// # Errors |
110 | | -/// |
111 | | -/// * `QuoteGeneratorError::TdxQuoteFailed` - if the quote generation API call fails. |
112 | | -/// * `QuoteGeneratorError::TdxQuoteEmpty` - if the API succeeds but returns no quote data. |
113 | | -fn create_quote( |
114 | | - report_data: &tdx_attest_rs::tdx_report_data_t, |
115 | | -) -> Result<Vec<u8>, QuoteGeneratorError> { |
116 | | - let mut selected_att_key_id = tdx_attest_rs::tdx_uuid_t { |
117 | | - d: [0; TDX_UUID_SIZE], |
118 | | - }; |
119 | | - let (result, quote) = tdx_attest_rs::tdx_att_get_quote( |
120 | | - Some(report_data), |
121 | | - None, |
122 | | - Some(&mut selected_att_key_id), |
123 | | - 0, |
124 | | - ); |
125 | | - |
126 | | - match result { |
127 | | - tdx_attest_rs::tdx_attest_error_t::TDX_ATTEST_SUCCESS => match quote { |
128 | | - Some(q) => Ok(q), |
129 | | - None => Err(QuoteGeneratorError::TdxQuoteEmpty), |
130 | | - }, |
131 | | - _ => { |
132 | | - error!("Failed to get TDX quote: {:?}", result); |
133 | | - Err(QuoteGeneratorError::TdxQuoteFailed) // _tdx_attest_error_t does not implement std::error::Error |
134 | | - } |
135 | | - } |
136 | | -} |
137 | 41 |
|
138 | 42 | fn main() -> Result<(), QuoteGeneratorError> { |
139 | 43 | // Initialize the logger (defaults to INFO level, override with RUST_LOG env var) |
|
0 commit comments