Skip to content
This repository was archived by the owner on May 9, 2022. It is now read-only.

Commit 26318d0

Browse files
authored
Merge pull request #98 from registreerocks/feat-types-enclave-messages
feat(rtc_types): add enclave_messages, with set_access_key
2 parents d37170b + 9c50c82 commit 26318d0

File tree

8 files changed

+231
-10
lines changed

8 files changed

+231
-10
lines changed

codegen/auth_enclave/bindings.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
*/
1313
#define DATA_UPLOAD_RESPONSE_LEN (16 + (24 + 16))
1414

15+
#define ARCHIVED_ENCLAVE_ID_SIZE 8
16+
17+
#define SET_ACCESS_KEY_REQUEST_SIZE 40
18+
19+
#define SET_ACCESS_KEY_RESPONSE_SIZE 1
20+
1521
/**
1622
* FFI safe result type that can be converted to and from a rust result.
1723
*/

codegen/data_enclave/bindings.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
*/
1313
#define DATA_UPLOAD_RESPONSE_LEN (16 + (24 + 16))
1414

15+
#define ARCHIVED_ENCLAVE_ID_SIZE 8
16+
17+
#define SET_ACCESS_KEY_REQUEST_SIZE 40
18+
19+
#define SET_ACCESS_KEY_RESPONSE_SIZE 1
20+
1521
typedef struct DataUploadResponse {
1622
uint8_t ciphertext[DATA_UPLOAD_RESPONSE_LEN];
1723
uint8_t nonce[24];

codegen/exec_enclave/bindings.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
*/
1313
#define DATA_UPLOAD_RESPONSE_LEN (16 + (24 + 16))
1414

15+
#define ARCHIVED_ENCLAVE_ID_SIZE 8
16+
17+
#define SET_ACCESS_KEY_REQUEST_SIZE 40
18+
19+
#define SET_ACCESS_KEY_RESPONSE_SIZE 1
20+
1521
/**
1622
* FFI safe result type that can be converted to and from a rust result.
1723
*/

rtc_tenclave/src/dh/protected_channel.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use secrecy::{ExposeSecret, Secret};
44
use sgx_tcrypto::{rsgx_rijndael128GCM_decrypt, rsgx_rijndael128GCM_encrypt};
55
use sgx_types::*;
66

7+
use rtc_types::enclave_messages::{EncryptedEnclaveMessage, RecommendedAesGcmIv};
8+
79
use super::types::AlignedKey;
810
use crate::util::concat_u8;
911

@@ -12,9 +14,6 @@ use super::enclave;
1214
#[cfg(not(test))]
1315
use sgx_tstd::enclave;
1416

15-
// NIST AES-GCM recommended IV size
16-
type RecommendedAesGcmIv = [u8; 12];
17-
1817
pub struct ProtectedChannel {
1918
iv_constructor: DeterministicAesGcmIvConstructor,
2019
key: Secret<AlignedKey>,
@@ -70,13 +69,6 @@ impl ProtectedChannel {
7069
}
7170
}
7271

73-
pub struct EncryptedEnclaveMessage<const MESSAGE_SIZE: usize, const AAD_SIZE: usize> {
74-
tag: sgx_aes_gcm_128bit_tag_t,
75-
ciphertext: [u8; MESSAGE_SIZE],
76-
aad: [u8; AAD_SIZE],
77-
nonce: RecommendedAesGcmIv,
78-
}
79-
8072
/// Implement the deterministic construction of AES-GCM IVs, as described in section 8.2.1 of [NIST SP 800-38D],
8173
/// "Recommendation for Block Cipher Modes of Operation: Galois/Counter Mode (GCM) and GMAC".
8274
///
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
//! FIXME: Non-generic version of [`set_access_key`], with conversions.
2+
//!
3+
//! This is a workaround for cbindgen not supporting const generics in structs yet,
4+
//! and should be removed once cbindgen implements that.
5+
//!
6+
//! Tracking issue: <https://github.com/eqrion/cbindgen/issues/687>
7+
//!
8+
//! These sizes should match the ones computed in `set_access_key`.
9+
//! (The Rust compiler should report an error if these don't line up:
10+
//! this can be used to update these if `set_access_key` changes.)
11+
12+
use sgx_types::sgx_aes_gcm_128bit_tag_t;
13+
14+
use super::{set_access_key, RecommendedAesGcmIv};
15+
16+
// See enclave_messages::ARCHIVED_ENCLAVE_ID_SIZE
17+
pub const ARCHIVED_ENCLAVE_ID_SIZE: usize = 8;
18+
19+
// Begin FFI types
20+
// (Keep these FFI type comments in sync between set_access_key and ffi_set_access_key, for diffing!)
21+
22+
// FFI type: REQUEST_SIZE
23+
pub const SET_ACCESS_KEY_REQUEST_SIZE: usize = 40;
24+
25+
// FFI type: EncryptedRequest
26+
#[repr(C)]
27+
pub struct SetAccessKeyEncryptedRequest {
28+
pub tag: sgx_aes_gcm_128bit_tag_t,
29+
pub ciphertext: [u8; SET_ACCESS_KEY_REQUEST_SIZE],
30+
pub aad: [u8; ARCHIVED_ENCLAVE_ID_SIZE],
31+
pub nonce: RecommendedAesGcmIv,
32+
}
33+
34+
// FFI type: RESPONSE_SIZE
35+
pub const SET_ACCESS_KEY_RESPONSE_SIZE: usize = 1;
36+
37+
// FFI type: EncryptedResponse
38+
#[derive(Default)]
39+
#[repr(C)]
40+
pub struct SetAccessKeyEncryptedResponse {
41+
pub tag: sgx_aes_gcm_128bit_tag_t,
42+
pub ciphertext: [u8; SET_ACCESS_KEY_RESPONSE_SIZE],
43+
pub aad: [u8; 0],
44+
pub nonce: RecommendedAesGcmIv,
45+
}
46+
47+
// End FFI types
48+
49+
// Boilerplate From implementations:
50+
51+
impl From<set_access_key::EncryptedRequest> for SetAccessKeyEncryptedRequest {
52+
fn from(
53+
set_access_key::EncryptedRequest {
54+
tag,
55+
ciphertext,
56+
aad,
57+
nonce,
58+
}: set_access_key::EncryptedRequest,
59+
) -> Self {
60+
return SetAccessKeyEncryptedRequest {
61+
tag,
62+
ciphertext,
63+
aad,
64+
nonce,
65+
};
66+
}
67+
}
68+
69+
impl From<SetAccessKeyEncryptedRequest> for set_access_key::EncryptedRequest {
70+
fn from(
71+
SetAccessKeyEncryptedRequest {
72+
tag,
73+
ciphertext,
74+
aad,
75+
nonce,
76+
}: SetAccessKeyEncryptedRequest,
77+
) -> Self {
78+
return set_access_key::EncryptedRequest {
79+
tag,
80+
ciphertext,
81+
aad,
82+
nonce,
83+
};
84+
}
85+
}
86+
87+
impl From<set_access_key::EncryptedResponse> for SetAccessKeyEncryptedResponse {
88+
fn from(
89+
set_access_key::EncryptedResponse {
90+
tag,
91+
ciphertext,
92+
aad,
93+
nonce,
94+
}: set_access_key::EncryptedResponse,
95+
) -> Self {
96+
return SetAccessKeyEncryptedResponse {
97+
tag,
98+
ciphertext,
99+
aad,
100+
nonce,
101+
};
102+
}
103+
}
104+
105+
impl From<SetAccessKeyEncryptedResponse> for set_access_key::EncryptedResponse {
106+
fn from(
107+
SetAccessKeyEncryptedResponse {
108+
tag,
109+
ciphertext,
110+
aad,
111+
nonce,
112+
}: SetAccessKeyEncryptedResponse,
113+
) -> Self {
114+
return set_access_key::EncryptedResponse {
115+
tag,
116+
ciphertext,
117+
aad,
118+
nonce,
119+
};
120+
}
121+
}

rtc_types/src/enclave_messages/mod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use core::mem;
2+
3+
use rkyv::Archive;
4+
use sgx_types::{sgx_aes_gcm_128bit_tag_t, sgx_enclave_id_t};
5+
6+
/// Size of [`Archive`] of [`sgx_enclave_id_t`].
7+
pub const ARCHIVED_ENCLAVE_ID_SIZE: usize =
8+
mem::size_of::<<sgx_enclave_id_t as Archive>::Archived>();
9+
10+
// NIST AES-GCM recommended IV size
11+
pub type RecommendedAesGcmIv = [u8; 12];
12+
13+
#[repr(C)]
14+
pub struct EncryptedEnclaveMessage<const MESSAGE_SIZE: usize, const AAD_SIZE: usize> {
15+
pub tag: sgx_aes_gcm_128bit_tag_t,
16+
pub ciphertext: [u8; MESSAGE_SIZE],
17+
pub aad: [u8; AAD_SIZE],
18+
pub nonce: RecommendedAesGcmIv,
19+
}
20+
21+
/// XXX: Ignore this module to work around cbindgen generic type handling
22+
///
23+
/// Issues:
24+
///
25+
/// * <https://github.com/eqrion/cbindgen/issues/7>
26+
/// * <https://github.com/eqrion/cbindgen/issues/286>
27+
/// * <https://github.com/eqrion/cbindgen/issues/573>
28+
///
29+
/// cbindgen:ignore
30+
pub mod set_access_key;
31+
32+
pub mod ffi_set_access_key;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use core::mem;
2+
3+
use rkyv::{Archive, Deserialize, Serialize};
4+
5+
use crate::enclave_messages::{EncryptedEnclaveMessage, ARCHIVED_ENCLAVE_ID_SIZE};
6+
7+
#[derive(Archive, Deserialize, Serialize, Debug, PartialEq, Clone)]
8+
pub struct Request {
9+
// XXX: Technically this only needs to be available inside of enclave contexts.
10+
// It might make sense to conditionally export this as public.
11+
pub uuid: [u8; 16], // TODO: Use UUID crate?
12+
pub access_key: [u8; 24], // [u8; ACCESS_KEY_BYTES]
13+
}
14+
15+
#[derive(Archive, Deserialize, Serialize, Debug, PartialEq)]
16+
pub struct Response {
17+
pub success: bool,
18+
}
19+
20+
// Begin FFI types
21+
// (Keep these FFI type comments in sync between set_access_key and ffi_set_access_key, for diffing!)
22+
23+
// FFI type: REQUEST_SIZE
24+
pub const REQUEST_SIZE: usize = mem::size_of::<ArchivedRequest>();
25+
26+
// FFI type: EncryptedRequest
27+
pub type EncryptedRequest = EncryptedEnclaveMessage<REQUEST_SIZE, ARCHIVED_ENCLAVE_ID_SIZE>;
28+
29+
// FFI type: RESPONSE_SIZE
30+
pub const RESPONSE_SIZE: usize = mem::size_of::<ArchivedResponse>();
31+
32+
// FFI type: EncryptedResponse
33+
pub type EncryptedResponse = EncryptedEnclaveMessage<RESPONSE_SIZE, 0>;
34+
35+
// End FFI types
36+
37+
#[cfg(test)]
38+
mod test {
39+
use crate::byte_formats::rkyv_format;
40+
use crate::enclave_messages::*;
41+
42+
#[test]
43+
fn test_set_access_key_msg() {
44+
let request = set_access_key::Request {
45+
uuid: [5u8; 16],
46+
access_key: [2u8; 24],
47+
};
48+
49+
let buf = rkyv_format::write_array(&request).unwrap();
50+
let deserialized = unsafe { rkyv_format::read_array(&buf) };
51+
52+
assert_eq!(
53+
request, deserialized,
54+
"Deserialized request should match initial request"
55+
);
56+
}
57+
}

rtc_types/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ mod ecall_result;
3030
pub use ecall_result::*;
3131

3232
pub mod byte_formats;
33+
pub mod enclave_messages;
3334

3435
#[repr(C)]
3536
#[derive(Clone, Debug)]

0 commit comments

Comments
 (0)