Reviewed-on: SGX/detee-sgx#3 Reviewed-by: Valentyn Faychuk <valy@detee.ltd> Co-authored-by: Noor <noormohammedb@protonmail.com> Co-committed-by: Noor <noormohammedb@protonmail.com>
52 lines
1.5 KiB
Rust
52 lines
1.5 KiB
Rust
use detee_sgx::prelude::*;
|
|
use std::{
|
|
fs,
|
|
path::Path,
|
|
time::{SystemTime, UNIX_EPOCH},
|
|
};
|
|
|
|
const DATA_PATH: &str = "/host/sealed_data";
|
|
|
|
fn main() {
|
|
println!("Example of sealing");
|
|
let sgx_sealing = SealingConfig::new().unwrap();
|
|
println!("sealing : {:?}", sgx_sealing);
|
|
|
|
let timestamp = SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)
|
|
.unwrap_or_default()
|
|
.as_secs();
|
|
|
|
println!("timestamp: {:?}", ×tamp);
|
|
|
|
if !Path::new(DATA_PATH).exists() {
|
|
// seal data
|
|
|
|
let payload = format!("this is sealing paylod; timestamp {}", timestamp)
|
|
.as_bytes()
|
|
.to_vec();
|
|
|
|
seal_and_write_data(sgx_sealing, payload);
|
|
} else {
|
|
// unseal data
|
|
|
|
let sealed_data = std::fs::read(DATA_PATH).unwrap();
|
|
unseal_data(sgx_sealing, sealed_data);
|
|
}
|
|
}
|
|
|
|
fn seal_and_write_data(sgx_sealing: SealingConfig, payload: Vec<u8>) {
|
|
let sealed_data = sgx_sealing.seal_data(payload).unwrap();
|
|
println!("sealed_data: {:?}", &sealed_data);
|
|
fs::write(DATA_PATH, &sealed_data).expect("Failed to write file {DATA_PATH}");
|
|
}
|
|
|
|
fn unseal_data(sgx_sealing: SealingConfig, sealed_data: Vec<u8>) {
|
|
println!("sealed_data: {:?}", &sealed_data);
|
|
let unsealed_data = sgx_sealing.un_seal_data(sealed_data).unwrap();
|
|
println!("unsealed_data: {:?}", &unsealed_data);
|
|
|
|
let unsealed_utf8_string = String::from_utf8_lossy(&unsealed_data);
|
|
println!("unsealed data utf8: \"{unsealed_utf8_string}\"");
|
|
}
|