Skip to content

Commit 5a1b117

Browse files
author
vlad
committed
on-chain machine-id WIP
1 parent 269ee17 commit 5a1b117

File tree

10 files changed

+128
-5
lines changed

10 files changed

+128
-5
lines changed

cosmwasm/enclaves/execute/Enclave.edl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ enclave {
5353
uint32_t msg_len
5454
);
5555

56+
public sgx_status_t ecall_onchain_approve_machine_id(
57+
[in, count=n_id] const uint8_t* p_id,
58+
uint32_t n_id,
59+
[out, count=32] uint8_t* p_proof
60+
);
61+
5662
public sgx_status_t ecall_get_attestation_report(
5763
uint32_t flags
5864
);

cosmwasm/enclaves/execute/src/registration/offchain.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,20 @@ pub unsafe extern "C" fn ecall_onchain_approve_upgrade(
705705
sgx_types::sgx_status_t::SGX_SUCCESS
706706
}
707707

708+
#[no_mangle]
709+
pub unsafe extern "C" fn ecall_onchain_approve_machine_id(
710+
p_id: *const u8,
711+
n_id: u32,
712+
p_proof: *mut u8,
713+
) -> sgx_types::sgx_status_t {
714+
validate_const_ptr!(p_id, n_id as usize, sgx_status_t::SGX_ERROR_UNEXPECTED);
715+
validate_mut_ptr!(p_proof, 32, sgx_status_t::SGX_ERROR_UNEXPECTED);
716+
717+
// TODO
718+
719+
sgx_types::sgx_status_t::SGX_SUCCESS
720+
}
721+
708722
pub fn calculate_truncated_hash(input: &[u8]) -> [u8; 20] {
709723
let mut res = [0u8; 20];
710724

cosmwasm/packages/sgx-vm/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ pub use crate::attestation::{
5454
create_attestation_report_u, untrusted_get_encrypted_genesis_seed, untrusted_get_encrypted_seed,
5555
};
5656
pub use crate::seed::{
57-
untrusted_approve_upgrade, untrusted_get_network_pubkey, untrusted_health_check,
58-
untrusted_init_bootstrap, untrusted_init_node, untrusted_key_gen, untrusted_migration_op,
59-
untrusted_rotate_store, untrusted_submit_validator_set_evidence,
57+
untrusted_approve_machine_id, untrusted_approve_upgrade, untrusted_get_network_pubkey,
58+
untrusted_health_check, untrusted_init_bootstrap, untrusted_init_node, untrusted_key_gen,
59+
untrusted_migration_op, untrusted_rotate_store, untrusted_submit_validator_set_evidence,
6060
};
6161

6262
pub use crate::random::untrusted_submit_block_signatures;

cosmwasm/packages/sgx-vm/src/seed.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ extern "C" {
6262
msg_len: u32,
6363
) -> sgx_types::sgx_status_t;
6464

65+
pub fn ecall_onchain_approve_machine_id(
66+
eid: sgx_enclave_id_t,
67+
retval: *mut sgx_status_t,
68+
p_id: *const u8,
69+
n_id: u32,
70+
p_proof: *mut u8,
71+
) -> sgx_types::sgx_status_t;
72+
6573
/// Trigger a query method in a wasm contract
6674
pub fn ecall_health_check(
6775
eid: sgx_enclave_id_t,
@@ -276,6 +284,41 @@ pub fn untrusted_approve_upgrade(msg_slice: &[u8]) -> SgxResult<()> {
276284
Ok(())
277285
}
278286

287+
pub fn untrusted_approve_machine_id(machine_id: &[u8]) -> SgxResult<Vec<u8>> {
288+
// Bind the token to a local variable to ensure its
289+
// destructor runs in the end of the function
290+
let enclave_access_token = ENCLAVE_DOORBELL
291+
.get_access(1) // This can never be recursive
292+
.ok_or(sgx_status_t::SGX_ERROR_BUSY)?;
293+
let enclave = (*enclave_access_token)?;
294+
295+
//info!("Initialized enclave successfully!");
296+
297+
let mut proof = [0_u8; 32];
298+
299+
let eid = enclave.geteid();
300+
let mut ret = sgx_status_t::SGX_SUCCESS;
301+
let status = unsafe {
302+
ecall_onchain_approve_machine_id(
303+
eid,
304+
&mut ret,
305+
machine_id.as_ptr(),
306+
machine_id.len() as u32,
307+
proof.as_mut_ptr(),
308+
)
309+
};
310+
311+
if status != sgx_status_t::SGX_SUCCESS {
312+
return Err(status);
313+
}
314+
315+
if ret != sgx_status_t::SGX_SUCCESS {
316+
return Err(ret);
317+
}
318+
319+
Ok(proof.to_vec())
320+
}
321+
279322
pub fn untrusted_key_gen() -> SgxResult<[u8; 32]> {
280323
info!("Initializing enclave..");
281324

go-cosmwasm/api/bindings.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ Buffer migrate(cache_t *cache,
222222

223223
bool migration_op(uint32_t opcode);
224224

225+
bool onchain_approve_machine_id(Buffer machine_id, Buffer *proof);
226+
225227
bool onchain_approve_upgrade(Buffer msg);
226228

227229
Buffer query(cache_t *cache,

go-cosmwasm/api/lib.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,23 @@ func OnUpgradeProposalPassed(mrEnclaveHash []byte) error {
205205
return nil
206206
}
207207

208+
func OnUpdateMachineID(machineID []byte) (error, []byte) {
209+
msgBuf := sendSlice(machineID)
210+
defer freeAfterSend(msgBuf)
211+
212+
proof := C.Buffer{}
213+
214+
ret, err := C.onchain_approve_machine_id(msgBuf, &proof)
215+
if err != nil {
216+
return err, nil
217+
}
218+
if !ret {
219+
return errors.New("onchain_approve_machine_id failed"), nil
220+
}
221+
222+
return nil, receiveVector(proof)
223+
}
224+
208225
func Create(cache Cache, wasm []byte) ([]byte, error) {
209226
code := sendSlice(wasm)
210227
defer freeAfterSend(code)

go-cosmwasm/api/lib_mock.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,7 @@ func GetEncryptedGenesisSeed(cert []byte) ([]byte, error) {
315315
func OnUpgradeProposalPassed(mrEnclaveHash []byte) error {
316316
return nil
317317
}
318+
319+
func OnUpdateMachineID(machineID []byte) (error, []byte) {
320+
return nil, nil
321+
}

go-cosmwasm/src/lib.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ pub use api::GoApi;
1212
use base64;
1313
use cosmwasm_sgx_vm::{
1414
call_handle_raw, call_init_raw, call_migrate_raw, call_query_raw, call_update_admin_raw,
15-
create_attestation_report_u, features_from_csv, untrusted_approve_upgrade,
16-
untrusted_get_encrypted_genesis_seed, untrusted_get_encrypted_seed,
15+
create_attestation_report_u, features_from_csv, untrusted_approve_machine_id,
16+
untrusted_approve_upgrade, untrusted_get_encrypted_genesis_seed, untrusted_get_encrypted_seed,
1717
untrusted_get_network_pubkey, untrusted_health_check, untrusted_init_bootstrap,
1818
untrusted_init_node, untrusted_key_gen, untrusted_migration_op, untrusted_rotate_store,
1919
untrusted_submit_validator_set_evidence, Checksum, CosmCache, Extern,
@@ -992,3 +992,27 @@ pub extern "C" fn onchain_approve_upgrade(msg: Buffer) -> bool {
992992
}
993993
}
994994
}
995+
996+
#[no_mangle]
997+
#[allow(deprecated)]
998+
pub extern "C" fn onchain_approve_machine_id(machine_id: Buffer, proof: &mut Buffer) -> bool {
999+
let machine_id_slice = match unsafe { machine_id.read() } {
1000+
None => {
1001+
return false;
1002+
}
1003+
Some(r) => r,
1004+
};
1005+
1006+
match untrusted_approve_machine_id(&machine_id_slice) {
1007+
Err(e) => {
1008+
set_error(Error::enclave_err(e.to_string()), None);
1009+
false
1010+
}
1011+
Ok(x) => {
1012+
clear_error();
1013+
1014+
*proof = Buffer::from_vec(x);
1015+
true
1016+
}
1017+
}
1018+
}

x/compute/internal/keeper/msg_server.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,5 +349,17 @@ func (m msgServer) UpdateMachineWhitelist(goCtx context.Context, msg *types.MsgU
349349
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender),
350350
))
351351

352+
store := m.keeper.storeService.OpenKVStore(ctx)
353+
354+
for _, id := range msg.MachineIds {
355+
if err, proof := api.OnUpdateMachineID(id); err != nil {
356+
return nil, err
357+
358+
key := append(types.MachineIDEvidencePrefix, id...)
359+
_ = store.Set(key, proof)
360+
361+
}
362+
}
363+
352364
return &types.MsgUpdateMachineWhitelistResponse{}, nil
353365
}

x/compute/internal/types/keys.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var (
3838
UpdateAdminPrefix = []byte{0x0D}
3939
RandomPrefix = []byte{0xFF}
4040
ValidatorSetEvidencePrefix = []byte{0xFE}
41+
MachineIDEvidencePrefix = []byte{0xFD}
4142

4243
KeyLastCodeID = append(SequenceKeyPrefix, []byte("lastCodeId")...)
4344
KeyLastInstanceID = append(SequenceKeyPrefix, []byte("lastContractId")...)

0 commit comments

Comments
 (0)