Skip to content

Commit c381d9b

Browse files
committed
refactor: create a separate module for functions
1 parent 37740c2 commit c381d9b

File tree

2 files changed

+104
-99
lines changed

2 files changed

+104
-99
lines changed

cvmassistants/quote-generator/src/main.rs

Lines changed: 3 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -29,111 +29,15 @@
2929
*
3030
*/
3131

32-
use log::{debug, error, info};
32+
use log::{debug, info};
3333
use std::env;
3434
use std::fs;
3535
mod error;
3636
use error::QuoteGeneratorError;
37+
mod utils;
38+
use utils::{create_quote, create_report_data, create_tdx_report};
3739

3840
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-
}
13741

13842
fn main() -> Result<(), QuoteGeneratorError> {
13943
// Initialize the logger (defaults to INFO level, override with RUST_LOG env var)
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
use crate::error::QuoteGeneratorError;
2+
use log::error;
3+
4+
const REPORT_SIZE: usize = 1024;
5+
const TDX_UUID_SIZE: usize = 16;
6+
7+
/// Creates a TDX report data structure from input bytes.
8+
///
9+
/// # Arguments
10+
///
11+
/// * `input_bytes` - A byte slice that **must be exactly `REPORT_DATA_SIZE` bytes long**.
12+
/// In this binary, `main` guarantees this by copying/padding the user input into
13+
/// a fixed-size `REPORT_DATA_SIZE` buffer before calling this function.
14+
///
15+
/// # Returns
16+
///
17+
/// A `Result` containing the `tdx_report_data_t` structure, or a `QuoteGeneratorError`
18+
/// if the input bytes cannot be converted.
19+
///
20+
/// # Errors
21+
///
22+
/// Returns `QuoteGeneratorError::ReportDataConversion` if input bytes length doesn't match `REPORT_DATA_SIZE`.
23+
pub fn create_report_data(
24+
input_bytes: &[u8],
25+
) -> Result<tdx_attest_rs::tdx_report_data_t, QuoteGeneratorError> {
26+
let report_data = tdx_attest_rs::tdx_report_data_t {
27+
d: input_bytes.try_into()?,
28+
};
29+
Ok(report_data)
30+
}
31+
32+
/// Creates a TDX report from the given report data.
33+
///
34+
/// # Arguments
35+
///
36+
/// * `report_data` - The report data to use for generating the TDX report
37+
///
38+
/// # Returns
39+
///
40+
/// A `Result` containing the `tdx_report_t` structure on success.
41+
///
42+
/// # Errors
43+
///
44+
/// Returns `QuoteGeneratorError::TdxReportFailed` if the report generation fails.
45+
pub fn create_tdx_report(
46+
report_data: &tdx_attest_rs::tdx_report_data_t,
47+
) -> Result<tdx_attest_rs::tdx_report_t, QuoteGeneratorError> {
48+
let mut tdx_report = tdx_attest_rs::tdx_report_t {
49+
d: [0; REPORT_SIZE],
50+
};
51+
let result = tdx_attest_rs::tdx_att_get_report(Some(report_data), &mut tdx_report);
52+
match result {
53+
tdx_attest_rs::tdx_attest_error_t::TDX_ATTEST_SUCCESS => Ok(tdx_report),
54+
_ => {
55+
error!("Failed to get TDX report: {:?}", result);
56+
Err(QuoteGeneratorError::TdxReportFailed) // _tdx_attest_error_t does not implement std::error::Error
57+
}
58+
}
59+
}
60+
61+
/// Creates a TDX attestation quote from the given report data.
62+
///
63+
/// This function generates a cryptographic quote that can be used to verify
64+
/// the integrity and authenticity of the TDX environment.
65+
///
66+
/// # Arguments
67+
///
68+
/// * `report_data` - The report data to include in the quote
69+
///
70+
/// # Returns
71+
///
72+
/// A `Result` containing the generated quote data as `Vec<u8>` on success.
73+
///
74+
/// # Errors
75+
///
76+
/// * `QuoteGeneratorError::TdxQuoteFailed` - if the quote generation API call fails.
77+
/// * `QuoteGeneratorError::TdxQuoteEmpty` - if the API succeeds but returns no quote data.
78+
pub fn create_quote(
79+
report_data: &tdx_attest_rs::tdx_report_data_t,
80+
) -> Result<Vec<u8>, QuoteGeneratorError> {
81+
let mut selected_att_key_id = tdx_attest_rs::tdx_uuid_t {
82+
d: [0; TDX_UUID_SIZE],
83+
};
84+
let (result, quote) = tdx_attest_rs::tdx_att_get_quote(
85+
Some(report_data),
86+
None,
87+
Some(&mut selected_att_key_id),
88+
0,
89+
);
90+
91+
match result {
92+
tdx_attest_rs::tdx_attest_error_t::TDX_ATTEST_SUCCESS => match quote {
93+
Some(q) => Ok(q),
94+
None => Err(QuoteGeneratorError::TdxQuoteEmpty),
95+
},
96+
_ => {
97+
error!("Failed to get TDX quote: {:?}", result);
98+
Err(QuoteGeneratorError::TdxQuoteFailed) // _tdx_attest_error_t does not implement std::error::Error
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)