teaclave-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From din...@apache.org
Subject [incubator-mesatee-sgx] branch v1.1.0-beta updated: Update db-proxy
Date Wed, 20 Nov 2019 06:56:50 GMT
This is an automated email from the ASF dual-hosted git repository.

dingyu pushed a commit to branch v1.1.0-beta
in repository https://gitbox.apache.org/repos/asf/incubator-mesatee-sgx.git


The following commit(s) were added to refs/heads/v1.1.0-beta by this push:
     new b9eb95d  Update db-proxy
b9eb95d is described below

commit b9eb95ddeca2ca7edd36220d48af6702ba6c1686
Author: Yu Ding <dingelish@gmail.com>
AuthorDate: Tue Nov 19 22:56:36 2019 -0800

    Update db-proxy
---
 samplecode/db-proxy/db-proxy/app/Cargo.toml        |   1 -
 samplecode/db-proxy/db-proxy/app/src/main.rs       | 105 +++----------
 samplecode/db-proxy/db-proxy/bin/.keep             |   0
 samplecode/db-proxy/db-server/Cargo.toml           |   2 +-
 samplecode/db-proxy/db-server/src/main.rs          |  14 +-
 samplecode/db-proxy/db-server/src/server/mod.rs    | 175 ++++++++++++++++++---
 .../db-proxy/db-server/src/verifytree/mbtree.rs    |  43 +++--
 7 files changed, 208 insertions(+), 132 deletions(-)

diff --git a/samplecode/db-proxy/db-proxy/app/Cargo.toml b/samplecode/db-proxy/db-proxy/app/Cargo.toml
index b1bdf34..59a78e3 100644
--- a/samplecode/db-proxy/db-proxy/app/Cargo.toml
+++ b/samplecode/db-proxy/db-proxy/app/Cargo.toml
@@ -7,7 +7,6 @@ build = "build.rs"
 [dependencies]
 sgx_types = { git = "https://github.com/baidu/rust-sgx-sdk.git" }
 sgx_urts = { git = "https://github.com/baidu/rust-sgx-sdk.git" }
-dirs = "1.0.2"
 
 [patch.'https://github.com/baidu/rust-sgx-sdk.git']
 sgx_types = { path = "../../../../sgx_types" }
diff --git a/samplecode/db-proxy/db-proxy/app/src/main.rs b/samplecode/db-proxy/db-proxy/app/src/main.rs
index 0a08d02..dc901bf 100644
--- a/samplecode/db-proxy/db-proxy/app/src/main.rs
+++ b/samplecode/db-proxy/db-proxy/app/src/main.rs
@@ -26,109 +26,44 @@
 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-extern crate dirs;
 extern crate sgx_types;
 extern crate sgx_urts;
 use sgx_types::*;
 use sgx_urts::SgxEnclave;
-use std::{fs, path};
-use std::io::{Read, Write};
 
 static ENCLAVE_FILE: &'static str = "enclave.signed.so";
-static ENCLAVE_TOKEN: &'static str = "enclave.token";
 
-extern "C" {
-    fn say_something(
-        eid: sgx_enclave_id_t,
-        retval: *mut sgx_status_t,
-        some_string: *const u8,
-        len: usize,
-    ) -> sgx_status_t;
+extern {
+    fn say_something(eid: sgx_enclave_id_t, retval: *mut sgx_status_t,
+                     some_string: *const u8, len: usize) -> sgx_status_t;
 }
 
 fn init_enclave() -> SgxResult<SgxEnclave> {
+
     let mut launch_token: sgx_launch_token_t = [0; 1024];
     let mut launch_token_updated: i32 = 0;
-    // Step 1: try to retrieve the launch token saved by last transaction
-    //         if there is no token, then create a new one.
-    //
-    // try to get the token saved in $HOME */
-    let mut home_dir = path::PathBuf::new();
-    let use_token = match dirs::home_dir() {
-        Some(path) => {
-            println!("[+] Home dir is {}", path.display());
-            home_dir = path;
-            true
-        }
-        None => {
-            println!("[-] Cannot get home dir");
-            false
-        }
-    };
-
-    let token_file: path::PathBuf = home_dir.join(ENCLAVE_TOKEN);;
-    if use_token == true {
-        match fs::File::open(&token_file) {
-            Err(_) => {
-                println!(
-                    "[-] Open token file {} error! Will create one.",
-                    token_file.as_path().to_str().unwrap()
-                );
-            }
-            Ok(mut f) => {
-                println!("[+] Open token file success! ");
-                match f.read(&mut launch_token) {
-                    Ok(1024) => {
-                        println!("[+] Token file valid!");
-                    }
-                    _ => println!("[+] Token file invalid, will create new token file"),
-                }
-            }
-        }
-    }
-
-    // Step 2: call sgx_create_enclave to initialize an enclave instance
+    // call sgx_create_enclave to initialize an enclave instance
     // Debug Support: set 2nd parameter to 1
     let debug = 1;
-    let mut misc_attr = sgx_misc_attribute_t {
-        secs_attr: sgx_attributes_t { flags: 0, xfrm: 0 },
-        misc_select: 0,
-    };
-    let enclave = try!(SgxEnclave::create(
-        ENCLAVE_FILE,
-        debug,
-        &mut launch_token,
-        &mut launch_token_updated,
-        &mut misc_attr
-    ));
-
-    // Step 3: save the launch token if it is updated
-    if use_token == true && launch_token_updated != 0 {
-        // reopen the file with write capablity
-        match fs::File::create(&token_file) {
-            Ok(mut f) => match f.write_all(&launch_token) {
-                Ok(()) => println!("[+] Saved updated launch token!"),
-                Err(_) => println!("[-] Failed to save updated launch token!"),
-            },
-            Err(_) => {
-                println!("[-] Failed to save updated enclave token, but doesn't matter");
-            }
-        }
-    }
-
-    Ok(enclave)
+    let mut misc_attr = sgx_misc_attribute_t {secs_attr: sgx_attributes_t { flags:0, xfrm:0},
misc_select:0};
+    SgxEnclave::create(ENCLAVE_FILE,
+                       debug,
+                       &mut launch_token,
+                       &mut launch_token_updated,
+                       &mut misc_attr)
 }
 
 fn main() {
+
     let enclave = match init_enclave() {
         Ok(r) => {
             println!("[+] Init Enclave Successful {}!", r.geteid());
             r
-        }
+        },
         Err(x) => {
             println!("[-] Init Enclave Failed {}!", x.as_str());
             return;
-        }
+        },
     };
 
     let input_string = String::from("This is a normal world string passed into Enclave!\n");
@@ -136,16 +71,14 @@ fn main() {
     let mut retval = sgx_status_t::SGX_SUCCESS;
 
     let result = unsafe {
-        say_something(
-            enclave.geteid(),
-            &mut retval,
-            input_string.as_ptr() as *const u8,
-            input_string.len(),
-        )
+        say_something(enclave.geteid(),
+                      &mut retval,
+                      input_string.as_ptr() as * const u8,
+                      input_string.len())
     };
 
     match result {
-        sgx_status_t::SGX_SUCCESS => {}
+        sgx_status_t::SGX_SUCCESS => {},
         _ => {
             println!("[-] ECALL Enclave Failed {}!", result.as_str());
             return;
diff --git a/samplecode/db-proxy/db-proxy/bin/.keep b/samplecode/db-proxy/db-proxy/bin/.keep
new file mode 100644
index 0000000..e69de29
diff --git a/samplecode/db-proxy/db-server/Cargo.toml b/samplecode/db-proxy/db-server/Cargo.toml
index 016505e..2009302 100644
--- a/samplecode/db-proxy/db-server/Cargo.toml
+++ b/samplecode/db-proxy/db-server/Cargo.toml
@@ -7,7 +7,7 @@ edition = "2018"
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
-merklebtree = "0.1.1"
+merklebtree = "0.1.5"
 ring = "0.16.9"
 hex = "0.4.0"
 serde = { version = "1.0.60", features = ["derive"] }
diff --git a/samplecode/db-proxy/db-server/src/main.rs b/samplecode/db-proxy/db-server/src/main.rs
index 892485f..06a9e56 100644
--- a/samplecode/db-proxy/db-server/src/main.rs
+++ b/samplecode/db-proxy/db-server/src/main.rs
@@ -1,29 +1,26 @@
 extern crate hex;
 extern crate merklebtree;
+extern crate parking_lot;
 extern crate ring;
+extern crate rocksdb;
 extern crate serde;
 extern crate serde_json;
-extern crate rocksdb;
-extern crate parking_lot;
-
 
 mod client;
 mod server;
 mod verifytree;
 
-
-use std::fs;
-use std::io::{Read, Write};
-use std::path;
 use parking_lot::RwLock;
 use rocksdb::{DBVector, WriteBatch, DB};
 use server::new_server;
+use std::fs;
+use std::io::{Read, Write};
+use std::path;
 
 fn main() {
     vertias_db();
 }
 
-
 pub fn vertias_db() {
     // generate the key_value of hmac key
     let mut key_value = Vec::new();
@@ -34,4 +31,3 @@ pub fn vertias_db() {
     let mut server = new_server(key_value);
     client::client_test(&mut server);
 }
-
diff --git a/samplecode/db-proxy/db-server/src/server/mod.rs b/samplecode/db-proxy/db-server/src/server/mod.rs
index 52f2503..1b7a499 100644
--- a/samplecode/db-proxy/db-server/src/server/mod.rs
+++ b/samplecode/db-proxy/db-server/src/server/mod.rs
@@ -1,10 +1,13 @@
 use crate::client::*;
 use crate::verifytree::mbtree::*;
 use hex;
-use ring::hmac::Key;
+use merklebtree::iterator::prev;
+use merklebtree::merklebtree::{MerkleBTree, Nodes};
 use merklebtree::node::Node;
+use merklebtree::sgxdb;
 use merklebtree::traits::CalculateHash;
 use parking_lot::RwLock;
+use ring::hmac::Key;
 use ring::{digest, hmac, rand};
 use rocksdb::{DBVector, DB};
 use serde::{Deserialize, Serialize};
@@ -17,6 +20,8 @@ pub struct server {
     hmac_key: Key,
     present_mbtree: Merklebtree,
     deleted_mbtree: Merklebtree,
+    sgx_present_root_node: Node<key_version>,
+    sgx_delete_root_node: Node<key_version>,
 }
 
 //hmacPayload is used to compute hmac
@@ -52,12 +57,12 @@ pub struct mbtree_payload {
 }
 
 impl server {
-    fn put(&mut self, key: String, value: String) {
+    fn db_put(&mut self, key: String, value: String) {
         let db = self.db_handler.clone();
         db.write().put(key.as_bytes(), value.as_bytes()).unwrap();
     }
 
-    fn get(&mut self, key: String) -> String {
+    fn db_get(&mut self, key: String) -> String {
         let db = self.db_handler.clone();
         let db_read = db.read();
         let r = db.read().get(key.as_str());
@@ -70,10 +75,140 @@ impl server {
         }
     }
 
-    fn delete(&mut self, key: String) {
+    fn db_delete(&mut self, key: String) {
         let db = self.db_handler.clone();
         db.write().delete(key.as_bytes()).unwrap();
     }
+
+    fn present_search(&mut self, key: String) -> search_result {
+        let (get_result, mut present_subnodes) = self.present_mbtree.search(key);
+        if get_result.existed {
+            present_subnodes.nodes_map.remove(&0).unwrap();
+            present_subnodes
+                .nodes_map
+                .insert(0, self.sgx_present_root_node.clone());
+            sgxdb::verify_subnodes_hash(&present_subnodes);
+        }
+        get_result
+    }
+
+    fn delete_search(&mut self, key: String) -> search_result {
+        let (get_result, mut delete_subnodes) = self.deleted_mbtree.search(key);
+        if get_result.existed {
+            delete_subnodes.nodes_map.remove(&0).unwrap();
+            delete_subnodes
+                .nodes_map
+                .insert(0, self.sgx_delete_root_node.clone());
+            sgxdb::verify_subnodes_hash(&delete_subnodes);
+        }
+        get_result
+    }
+
+    fn present_remove(&mut self, key: String) {
+        let mut present_subnodes = self.present_mbtree.delete(key.clone());
+        if present_subnodes.nodes_map.len() == 0 {
+            return;
+        }
+
+        present_subnodes.nodes_map.remove(&0).unwrap();
+        present_subnodes
+            .nodes_map
+            .insert(0, self.sgx_present_root_node.clone());
+
+        sgxdb::verify_subnodes_hash(&present_subnodes);
+
+        let kv = key_version {
+            key: key.clone(),
+            version: 0,
+        };
+
+        self.present_mbtree
+            .mbtree
+            .remove(kv.clone(), &mut present_subnodes);
+
+        let node_hash = self.present_mbtree.nodes.merkleroot();
+        let subnode_hash = present_subnodes.merkleroot();
+        assert_eq!(node_hash, subnode_hash);
+
+        self.sgx_present_root_node = present_subnodes.nodes_map.remove(&0).unwrap();
+    }
+
+    fn delete_remove(&mut self, key: String) {
+        let mut delete_subnodes = self.deleted_mbtree.delete(key.clone());
+        if delete_subnodes.nodes_map.len() == 0 {
+            return;
+        }
+
+        delete_subnodes.nodes_map.remove(&0).unwrap();
+        delete_subnodes
+            .nodes_map
+            .insert(0, self.sgx_delete_root_node.clone());
+
+        sgxdb::verify_subnodes_hash(&delete_subnodes);
+        let kv = key_version {
+            key: key.clone(),
+            version: 0,
+        };
+        self.deleted_mbtree
+            .mbtree
+            .remove(kv.clone(), &mut delete_subnodes);
+
+        let node_hash = self.deleted_mbtree.nodes.merkleroot();
+        let subnode_hash = delete_subnodes.merkleroot();
+        assert_eq!(node_hash, subnode_hash);
+
+        self.sgx_delete_root_node = delete_subnodes.nodes_map.remove(&0).unwrap();
+    }
+
+    fn present_build_with_kv(&mut self, kv: key_version) {
+        if self.sgx_present_root_node.content.len() == 0 {
+            self.sgx_present_root_node = Node::new_node(kv.clone(), 0);
+            self.sgx_present_root_node.root_flag = true;
+            self.present_mbtree.build_with_key_value(kv.clone());
+            return;
+        }
+        let mut present_subnodes = self.present_mbtree.build_with_key_value(kv.clone());
+        present_subnodes.nodes_map.remove(&0).unwrap();
+        present_subnodes
+            .nodes_map
+            .insert(0, self.sgx_present_root_node.clone());
+
+        sgxdb::verify_subnodes_hash(&present_subnodes);
+        self.present_mbtree
+            .mbtree
+            .put(kv.clone(), &mut present_subnodes);
+
+        let node_hash = self.present_mbtree.nodes.merkleroot();
+        let subnode_hash = present_subnodes.merkleroot();
+        assert_eq!(node_hash, subnode_hash);
+
+        self.sgx_present_root_node = present_subnodes.nodes_map.remove(&0).unwrap();
+    }
+
+    fn delete_build_with_kv(&mut self, kv: key_version) {
+        if self.sgx_delete_root_node.content.len() == 0 {
+            self.sgx_delete_root_node = Node::new_node(kv.clone(), 0);
+            self.sgx_delete_root_node.root_flag = true;
+            self.deleted_mbtree.build_with_key_value(kv.clone());
+            return;
+        }
+        let mut delete_subnodes = self.deleted_mbtree.build_with_key_value(kv.clone());
+        delete_subnodes.nodes_map.remove(&0).unwrap();
+        delete_subnodes
+            .nodes_map
+            .insert(0, self.sgx_present_root_node.clone());
+
+        sgxdb::verify_subnodes_hash(&delete_subnodes);
+        self.deleted_mbtree
+            .mbtree
+            .put(kv.clone(), &mut delete_subnodes);
+
+        let node_hash = self.deleted_mbtree.nodes.merkleroot();
+        let subnode_hash = delete_subnodes.merkleroot();
+        assert_eq!(node_hash, subnode_hash);
+
+        self.sgx_delete_root_node = delete_subnodes.nodes_map.remove(&0).unwrap();
+    }
 }
 
 pub fn new_server(key_value: Vec<u8>) -> server {
@@ -90,6 +225,8 @@ pub fn new_server(key_value: Vec<u8>) -> server {
         hmac_key: s_key,
         present_mbtree: new_mbtree(),
         deleted_mbtree: new_mbtree(),
+        sgx_present_root_node: Node::new_empty(0),
+        sgx_delete_root_node: Node::new_empty(0),
     }
 }
 
@@ -117,7 +254,7 @@ impl server {
     }
 
     pub fn veritasdb_get(&mut self, req: request) -> String {
-        let data = self.get(req.key.clone());
+        let data = self.db_get(req.key.clone());
         if data == "".to_string() {
             return String::new();
         }
@@ -131,7 +268,7 @@ impl server {
         let hmac_string = serde_json::to_string(&hmac_data).unwrap();
         let verify_result = self.verify_hmac(hmac_string, sp.tag.clone());
 
-        let sr = self.present_mbtree.search(req.key.clone());
+        let sr = self.present_search(req.key.clone());
         if verify_result && sp.ctr == sr.version {
             return sp.value;
         } else {
@@ -141,7 +278,7 @@ impl server {
     }
 
     pub fn veritasdb_put(&mut self, req: request) {
-        let get_result = self.present_mbtree.search(req.key.clone());
+        let get_result = self.present_search(req.key.clone());
         if get_result.existed {
             let hmac_data = hmac_payload {
                 key: req.key.clone(),
@@ -158,10 +295,10 @@ impl server {
                 ctr: get_result.version + 1,
             };
             let store_string = serde_json::to_string(&store_data).unwrap();
-            self.put(req.key.clone(), store_string.clone());
+            self.db_put(req.key.clone(), store_string.clone());
 
             //update present if there is no error
-            self.present_mbtree.build_with_key_value(key_version {
+            self.present_build_with_kv(key_version {
                 key: req.key,
                 version: get_result.version + 1,
             });
@@ -172,13 +309,13 @@ impl server {
     }
 
     pub fn veritasdb_insert(&mut self, req: request) {
-        let sr = self.present_mbtree.search(req.key.clone());
+        let sr = self.present_search(req.key.clone());
         if sr.existed {
             println!("key existed in present when called insert");
             return;
         } else {
             let mut ctr = 0;
-            let delete_sr = self.deleted_mbtree.search(req.key.clone());
+            let delete_sr = self.delete_search(req.key.clone());
             if delete_sr.existed {
                 ctr = delete_sr.version + 1;
             } else {
@@ -198,26 +335,26 @@ impl server {
                 tag: tag_string,
             };
             let store_string = serde_json::to_string(&store_data).unwrap();
-            self.put(req.key.clone(), store_string);
-            self.present_mbtree.build_with_key_value(key_version {
+            self.db_put(req.key.clone(), store_string);
+            self.present_build_with_kv(key_version {
                 key: req.key.clone(),
                 version: ctr,
             });
-            self.deleted_mbtree.delete(req.key.clone());
+            self.delete_remove(req.key.clone());
         }
     }
 
     pub fn veritasdb_delete(&mut self, req: request) {
-        let get_result = self.get(req.key.clone());
+        let get_result = self.db_get(req.key.clone());
         if get_result == "".to_string() {
         } else {
-            self.delete(req.key.clone());
-            let sr = self.present_mbtree.search(req.key.clone());
-            self.deleted_mbtree.build_with_key_value(key_version {
+            self.db_delete(req.key.clone());
+            let sr = self.present_search(req.key.clone());
+            self.delete_build_with_kv(key_version {
                 key: req.key.clone(),
                 version: sr.version,
             });
-            self.present_mbtree.delete(req.key);
+            self.present_remove(req.key.clone());
         }
     }
 
diff --git a/samplecode/db-proxy/db-server/src/verifytree/mbtree.rs b/samplecode/db-proxy/db-server/src/verifytree/mbtree.rs
index b9f7965..7dbf2d1 100644
--- a/samplecode/db-proxy/db-server/src/verifytree/mbtree.rs
+++ b/samplecode/db-proxy/db-server/src/verifytree/mbtree.rs
@@ -1,4 +1,5 @@
 use merklebtree::merklebtree::{MerkleBTree, Nodes};
+use merklebtree::node::Node;
 use merklebtree::traits::CalculateHash;
 use ring::digest;
 use std::cmp::Ordering;
@@ -52,6 +53,7 @@ impl CalculateHash for key_version {
 pub struct Merklebtree {
     pub mbtree: MerkleBTree,
     pub nodes: Nodes<key_version>,
+    pub root_node: Node<key_version>,
 }
 
 pub fn new_mbtree() -> Merklebtree {
@@ -71,12 +73,13 @@ pub fn new_mbtree() -> Merklebtree {
     Merklebtree {
         mbtree: tree,
         nodes,
+        root_node: Node::new_empty(0),
     }
 }
 
 impl Merklebtree {
-    pub fn search(&mut self, key: String) -> search_result {
-        let (value, found) = self.mbtree.get(
+    pub fn search(&mut self, key: String) -> (search_result, Nodes<key_version>)
{
+        let (value, found, subnodes) = self.mbtree.get_clone(
             key_version {
                 key: key.clone(),
                 version: 0,
@@ -84,38 +87,46 @@ impl Merklebtree {
             &mut self.nodes,
         );
         if found {
-            return search_result {
-                existed: true,
-                key: key,
-                version: value.version,
-            };
+            return (
+                search_result {
+                    existed: true,
+                    key: key,
+                    version: value.version,
+                },
+                subnodes,
+            );
         } else {
-            return search_result {
-                existed: false,
-                key: key,
-                version: -1,
-            };
+            return (
+                search_result {
+                    existed: false,
+                    key: key,
+                    version: -1,
+                },
+                subnodes,
+            );
         }
     }
 
-    pub fn delete(&mut self, key: String) {
-        self.mbtree.remove(
+    pub fn delete(&mut self, key: String) -> Nodes<key_version> {
+        let subnodes = self.mbtree.remove_clone(
             key_version {
                 key: key,
                 version: 0,
             },
             &mut self.nodes,
         );
+        subnodes
     }
 
-    pub fn build_with_key_value(&mut self, kv: key_version) {
+    pub fn build_with_key_value(&mut self, kv: key_version) -> Nodes<key_version>
{
         println!("kv:{:?}", kv);
-        self.mbtree.put(
+        let subnodes = self.mbtree.put_clone(
             key_version {
                 key: kv.key,
                 version: kv.version,
             },
             &mut self.nodes,
         );
+        subnodes
     }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@mesatee.apache.org
For additional commands, e-mail: commits-help@mesatee.apache.org


Mime
View raw message