-
Notifications
You must be signed in to change notification settings - Fork 14
/
lib.rs
205 lines (180 loc) · 5.67 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
Copyright 2021 Integritee AG and Supercomputing Systems AG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//!Primitives for teerex
#![cfg_attr(not(feature = "std"), no_std)]
use codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_core::H256;
use sp_std::prelude::*;
#[derive(Encode, Decode, Copy, Clone, PartialEq, Eq, sp_core::RuntimeDebug, TypeInfo)]
pub enum SgxBuildMode {
Debug,
Production,
}
impl Default for SgxBuildMode {
fn default() -> Self {
SgxBuildMode::Production
}
}
#[derive(Encode, Decode, Default, Copy, Clone, PartialEq, Eq, sp_core::RuntimeDebug, TypeInfo)]
pub struct Enclave<PubKey, Url> {
pub pubkey: PubKey, // FIXME: this is redundant information
pub mr_enclave: MrEnclave,
// Todo: make timestamp: Moment
pub timestamp: u64, // unix epoch in milliseconds
pub url: Url, // utf8 encoded url
pub sgx_mode: SgxBuildMode,
}
impl<PubKey, Url> Enclave<PubKey, Url> {
pub fn new(
pubkey: PubKey,
mr_enclave: MrEnclave,
timestamp: u64,
url: Url,
sgx_build_mode: SgxBuildMode,
) -> Self {
Enclave { pubkey, mr_enclave, timestamp, url, sgx_mode: sgx_build_mode }
}
}
/// The list of valid TCBs for an enclave.
#[derive(Encode, Decode, Clone, PartialEq, Eq, sp_core::RuntimeDebug, TypeInfo)]
pub struct QeTcb {
pub isvsvn: u16,
}
impl QeTcb {
pub fn new(isvsvn: u16) -> Self {
Self { isvsvn }
}
}
/// This represents all the collateral data that we need to store on chain in order to verify
/// the quoting enclave validity of another enclave that wants to register itself on chain
#[derive(Encode, Decode, Default, Clone, PartialEq, Eq, sp_core::RuntimeDebug, TypeInfo)]
pub struct QuotingEnclave {
// Todo: make timestamp: Moment
pub issue_date: u64, // unix epoch in milliseconds
// Todo: make timestamp: Moment
pub next_update: u64, // unix epoch in milliseconds
pub miscselect: [u8; 4],
pub miscselect_mask: [u8; 4],
pub attributes: [u8; 16],
pub attributes_mask: [u8; 16],
pub mrsigner: MrSigner,
pub isvprodid: u16,
/// Contains only the TCB versions that are considered UpToDate
pub tcb: Vec<QeTcb>,
}
impl QuotingEnclave {
#[allow(clippy::too_many_arguments)]
pub fn new(
issue_date: u64,
next_update: u64,
miscselect: [u8; 4],
miscselect_mask: [u8; 4],
attributes: [u8; 16],
attributes_mask: [u8; 16],
mrsigner: MrSigner,
isvprodid: u16,
tcb: Vec<QeTcb>,
) -> Self {
Self {
issue_date,
next_update,
miscselect,
miscselect_mask,
attributes,
attributes_mask,
mrsigner,
isvprodid,
tcb,
}
}
pub fn attributes_flags_mask_as_u64(&self) -> u64 {
let slice_as_array: [u8; 8] = self.attributes_mask[0..8].try_into().unwrap();
u64::from_le_bytes(slice_as_array)
}
pub fn attributes_flags_as_u64(&self) -> u64 {
let slice_as_array: [u8; 8] = self.attributes[0..8].try_into().unwrap();
u64::from_le_bytes(slice_as_array)
}
}
#[derive(Encode, Decode, Default, Clone, PartialEq, Eq, sp_core::RuntimeDebug, TypeInfo)]
pub struct TcbVersionStatus {
pub cpusvn: Cpusvn,
pub pcesvn: Pcesvn,
}
impl TcbVersionStatus {
pub fn new(cpusvn: Cpusvn, pcesvn: Pcesvn) -> Self {
Self { cpusvn, pcesvn }
}
pub fn verify_examinee(&self, examinee: &TcbVersionStatus) -> bool {
for (v, r) in self.cpusvn.iter().zip(examinee.cpusvn.iter()) {
if *v > *r {
return false
}
}
self.pcesvn <= examinee.pcesvn
}
}
/// This represents all the collateral data that we need to store on chain in order to verify
/// the quoting enclave validity of another enclave that wants to register itself on chain
#[derive(Encode, Decode, Default, Clone, PartialEq, Eq, sp_core::RuntimeDebug, TypeInfo)]
pub struct TcbInfoOnChain {
// Todo: make timestamp: Moment
pub issue_date: u64, // unix epoch in milliseconds
// Todo: make timestamp: Moment
pub next_update: u64, // unix epoch in milliseconds
tcb_levels: Vec<TcbVersionStatus>,
}
impl TcbInfoOnChain {
pub fn new(issue_date: u64, next_update: u64, tcb_levels: Vec<TcbVersionStatus>) -> Self {
Self { issue_date, next_update, tcb_levels }
}
pub fn verify_examinee(&self, examinee: &TcbVersionStatus) -> bool {
for tb in &self.tcb_levels {
if tb.verify_examinee(examinee) {
return true
}
}
false
}
}
pub type MrSigner = [u8; 32];
pub type MrEnclave = [u8; 32];
pub type Fmspc = [u8; 6];
pub type Cpusvn = [u8; 16];
pub type Pcesvn = u16;
pub type ShardIdentifier = H256;
#[derive(Encode, Decode, Default, Clone, PartialEq, Eq, sp_core::RuntimeDebug, TypeInfo)]
pub struct Request {
pub shard: ShardIdentifier,
pub cyphertext: Vec<u8>,
}
#[cfg(test)]
mod tests {
use super::*;
use hex_literal::hex;
#[test]
fn tcb_full_is_valid() {
// The strings are the hex encodings of the 16-byte CPUSVN numbers
let reference = TcbVersionStatus::new(hex!("11110204018007000000000000000000"), 7);
assert!(reference.verify_examinee(&reference));
assert!(reference
.verify_examinee(&TcbVersionStatus::new(hex!("11110204018007000000000000000000"), 7)));
assert!(reference
.verify_examinee(&TcbVersionStatus::new(hex!("21110204018007000000000000000001"), 7)));
assert!(!reference
.verify_examinee(&TcbVersionStatus::new(hex!("10110204018007000000000000000000"), 6)));
assert!(!reference
.verify_examinee(&TcbVersionStatus::new(hex!("11110204018007000000000000000000"), 6)));
}
}