From fc322c7a99dda07887c1ea2fca0d9d53cc5c847f Mon Sep 17 00:00:00 2001 From: hrezaei Date: Wed, 17 Nov 2021 08:51:03 +0330 Subject: [PATCH 1/7] Nightly snapshot, trying to upgrade to the latest version of crypto and rocket --- Cargo.toml | 8 ++++--- examples/common.rs | 52 +++++++++++++++++++++++++++++++----------- examples/sm_manager.rs | 4 ++-- 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b9d2c04b..3273c649 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,10 +61,12 @@ optional = true [dev-dependencies] criterion = "0.3" -rust-crypto = "0.2" +#rust-crypto = "0.2" +aes-gcm = "0.9.4" hex = "0.4" -rocket = { version = "0.4.2", default-features = false } -rocket_contrib = "0.4.2" +rocket = { version = "0.5.0-rc.1" } +#rocket = { version = "0.5.0-rc.1", default-features = false } +rocket_contrib = "0.4.10" reqwest = { version = "0.9", default-features = false } uuid = { version = "0.8", features = ["v4"] } serde_json = "1.0" diff --git a/examples/common.rs b/examples/common.rs index 45ecad38..720740c4 100644 --- a/examples/common.rs +++ b/examples/common.rs @@ -1,16 +1,23 @@ use std::{env, iter::repeat, thread, time, time::Duration}; - +/* use crypto::{ aead::{AeadDecryptor, AeadEncryptor}, aes::KeySize::KeySize256, aes_gcm::AesGcm, }; +*/ +use aes_gcm::{Aes256Gcm, Nonce}; +use aes_gcm::aead::{NewAead, Aead}; + + use curv::{ arithmetic::traits::Converter, elliptic::curves::secp256_k1::{FE, GE}, elliptic::curves::traits::{ECPoint, ECScalar}, BigInt, }; +use rand::distributions::Alphanumeric; +use rand::{Rng, thread_rng}; use reqwest::Client; use serde::{Deserialize, Serialize}; @@ -50,25 +57,44 @@ pub struct Params { #[allow(dead_code)] pub fn aes_encrypt(key: &[u8], plaintext: &[u8]) -> AEAD { - let nonce: Vec = repeat(3).take(12).collect(); - let aad: [u8; 0] = []; - let mut gcm = AesGcm::new(KeySize256, key, &nonce[..], &aad); - let mut out: Vec = repeat(0).take(plaintext.len()).collect(); - let mut out_tag: Vec = repeat(0).take(16).collect(); - gcm.encrypt(&plaintext[..], &mut out[..], &mut out_tag[..]); + + let aes_key = aes_gcm::Key::from_slice(key); + let cipher = Aes256Gcm::new(aes_key); + + let rand_string: String = thread_rng() + .sample_iter(&Alphanumeric) + .take(12) + .map(char::from) + .collect(); + + let nonce = Nonce::from_slice(rand_string.as_bytes()); // 12-Bytes; unique per message + + let ciphertext = cipher.encrypt(nonce, plaintext.as_ref()) + .expect("encryption failure!"); // NOTE: handle this error to avoid panics! + + let out_tag: Vec = repeat(0).take(16).collect(); + AEAD { - ciphertext: out.to_vec(), + ciphertext: ciphertext, tag: out_tag.to_vec(), } } #[allow(dead_code)] pub fn aes_decrypt(key: &[u8], aead_pack: AEAD) -> Vec { - let mut out: Vec = repeat(0).take(aead_pack.ciphertext.len()).collect(); - let nonce: Vec = repeat(3).take(12).collect(); - let aad: [u8; 0] = []; - let mut gcm = AesGcm::new(KeySize256, key, &nonce[..], &aad); - gcm.decrypt(&aead_pack.ciphertext[..], &mut out, &aead_pack.tag[..]); + + let aes_key = aes_gcm::Key::from_slice(key); + + let rand_string: String = thread_rng() + .sample_iter(&Alphanumeric) + .take(12) + .map(char::from) + .collect(); + + let nonce = Nonce::from_slice(rand_string.as_bytes()); // 12-Bytes; unique per message + + let gcm = Aes256Gcm::new(aes_key); + let out = gcm.decrypt(nonce, &aead_pack.ciphertext[..]).unwrap(); out } diff --git a/examples/sm_manager.rs b/examples/sm_manager.rs index f023d5b0..303819b3 100644 --- a/examples/sm_manager.rs +++ b/examples/sm_manager.rs @@ -13,7 +13,7 @@ use common::{Entry, Index, Key, Params, PartySignup}; #[post("/get", format = "json", data = "")] fn get( - db_mtx: State>>, + db_mtx: &State>>, request: Json, ) -> Json> { let index: Index = request.0; @@ -137,7 +137,7 @@ fn main() { hm.insert(sign_key, serde_json::to_string(&party_signup_sign).unwrap()); } ///////////////////////////////////////////////////////////////// - rocket::ignite() + rocket::build() .mount("/", routes![get, set, signup_keygen, signup_sign]) .manage(db_mtx) .launch(); From 7d9987f8227d711ec50130f59dea2adc628720e2 Mon Sep 17 00:00:00 2001 From: hrezaei Date: Wed, 17 Nov 2021 15:25:13 +0330 Subject: [PATCH 2/7] Upgrade Rocket from 0.4 to 0.5.0-rc.1 --- Cargo.toml | 5 +---- examples/common.rs | 9 +-------- examples/sm_manager.rs | 9 +++++---- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3273c649..9597a426 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -61,12 +61,9 @@ optional = true [dev-dependencies] criterion = "0.3" -#rust-crypto = "0.2" aes-gcm = "0.9.4" hex = "0.4" -rocket = { version = "0.5.0-rc.1" } -#rocket = { version = "0.5.0-rc.1", default-features = false } -rocket_contrib = "0.4.10" +rocket = { version = "0.5.0-rc.1", features = ["json"] } reqwest = { version = "0.9", default-features = false } uuid = { version = "0.8", features = ["v4"] } serde_json = "1.0" diff --git a/examples/common.rs b/examples/common.rs index 720740c4..a2120bc8 100644 --- a/examples/common.rs +++ b/examples/common.rs @@ -1,15 +1,8 @@ use std::{env, iter::repeat, thread, time, time::Duration}; -/* -use crypto::{ - aead::{AeadDecryptor, AeadEncryptor}, - aes::KeySize::KeySize256, - aes_gcm::AesGcm, -}; -*/ + use aes_gcm::{Aes256Gcm, Nonce}; use aes_gcm::aead::{NewAead, Aead}; - use curv::{ arithmetic::traits::Converter, elliptic::curves::secp256_k1::{FE, GE}, diff --git a/examples/sm_manager.rs b/examples/sm_manager.rs index 303819b3..9f1572b5 100644 --- a/examples/sm_manager.rs +++ b/examples/sm_manager.rs @@ -5,7 +5,8 @@ use std::fs; use std::sync::RwLock; use rocket::{post, routes, State}; -use rocket_contrib::json::Json; +use rocket::serde::json::Json; + use uuid::Uuid; mod common; @@ -31,7 +32,7 @@ fn get( } #[post("/set", format = "json", data = "")] -fn set(db_mtx: State>>, request: Json) -> Json> { +fn set(db_mtx: &State>>, request: Json) -> Json> { let entry: Entry = request.0; let mut hm = db_mtx.write().unwrap(); hm.insert(entry.key.clone(), entry.value.clone()); @@ -39,7 +40,7 @@ fn set(db_mtx: State>>, request: Json) -> Jso } #[post("/signupkeygen", format = "json")] -fn signup_keygen(db_mtx: State>>) -> Json> { +fn signup_keygen(db_mtx: &State>>) -> Json> { let data = fs::read_to_string("params.json") .expect("Unable to read params, make sure config file is present in the same folder "); let params: Params = serde_json::from_str(&data).unwrap(); @@ -70,7 +71,7 @@ fn signup_keygen(db_mtx: State>>) -> Json>>) -> Json> { +fn signup_sign(db_mtx: &State>>) -> Json> { //read parameters: let data = fs::read_to_string("params.json") .expect("Unable to read params, make sure config file is present in the same folder "); From ab27e475e9ee522e5ae21b01f4b91ac94daf0ee4 Mon Sep 17 00:00:00 2001 From: hrezaei Date: Wed, 17 Nov 2021 15:52:48 +0330 Subject: [PATCH 3/7] Update sm_manager according to latest changes in Rocket 0.5 --- examples/sm_manager.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/examples/sm_manager.rs b/examples/sm_manager.rs index 9f1572b5..4f694c34 100644 --- a/examples/sm_manager.rs +++ b/examples/sm_manager.rs @@ -1,10 +1,11 @@ #![feature(proc_macro_hygiene, decl_macro)] +#[macro_use] extern crate rocket; use std::collections::HashMap; use std::fs; use std::sync::RwLock; -use rocket::{post, routes, State}; +use rocket::{post, routes, State, launch, Rocket}; use rocket::serde::json::Json; use uuid::Uuid; @@ -103,7 +104,8 @@ fn signup_sign(db_mtx: &State>>) -> Json _ { // let mut my_config = Config::development(); // my_config.set_port(18001); let db: HashMap = HashMap::new(); @@ -141,5 +143,4 @@ fn main() { rocket::build() .mount("/", routes![get, set, signup_keygen, signup_sign]) .manage(db_mtx) - .launch(); } From 787a4475bcf9a4899ba4fccb27d08a60cc518abc Mon Sep 17 00:00:00 2001 From: hrezaei Date: Wed, 17 Nov 2021 19:02:10 +0330 Subject: [PATCH 4/7] Fix a bug in encrypt decrypt flow --- examples/common.rs | 23 +++++++++++++++++------ examples/sm_manager.rs | 3 +-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/examples/common.rs b/examples/common.rs index a2120bc8..25270378 100644 --- a/examples/common.rs +++ b/examples/common.rs @@ -1,7 +1,7 @@ use std::{env, iter::repeat, thread, time, time::Duration}; use aes_gcm::{Aes256Gcm, Nonce}; -use aes_gcm::aead::{NewAead, Aead}; +use aes_gcm::aead::{NewAead, Aead, Payload}; use curv::{ arithmetic::traits::Converter, @@ -62,11 +62,16 @@ pub fn aes_encrypt(key: &[u8], plaintext: &[u8]) -> AEAD { let nonce = Nonce::from_slice(rand_string.as_bytes()); // 12-Bytes; unique per message - let ciphertext = cipher.encrypt(nonce, plaintext.as_ref()) - .expect("encryption failure!"); // NOTE: handle this error to avoid panics! - let out_tag: Vec = repeat(0).take(16).collect(); + let text_payload = Payload { + msg: plaintext, + aad: &out_tag[..] + }; + + let ciphertext = cipher.encrypt(nonce, text_payload) + .expect("encryption failure!"); // NOTE: handle this error to avoid panics! + AEAD { ciphertext: ciphertext, tag: out_tag.to_vec(), @@ -87,8 +92,14 @@ pub fn aes_decrypt(key: &[u8], aead_pack: AEAD) -> Vec { let nonce = Nonce::from_slice(rand_string.as_bytes()); // 12-Bytes; unique per message let gcm = Aes256Gcm::new(aes_key); - let out = gcm.decrypt(nonce, &aead_pack.ciphertext[..]).unwrap(); - out + + let text_payload = Payload { + msg: &aead_pack.ciphertext[..], + aad: &aead_pack.tag[..] + }; + + let out = gcm.decrypt(nonce, text_payload); + out.unwrap_or_default() } pub fn postb(client: &Client, path: &str, body: T) -> Option diff --git a/examples/sm_manager.rs b/examples/sm_manager.rs index 4f694c34..dbc6bbc3 100644 --- a/examples/sm_manager.rs +++ b/examples/sm_manager.rs @@ -1,11 +1,10 @@ #![feature(proc_macro_hygiene, decl_macro)] -#[macro_use] extern crate rocket; use std::collections::HashMap; use std::fs; use std::sync::RwLock; -use rocket::{post, routes, State, launch, Rocket}; +use rocket::{post, routes, State, launch}; use rocket::serde::json::Json; use uuid::Uuid; From 670ef7874a2973e4599b0309fd38d5200a340c14 Mon Sep 17 00:00:00 2001 From: hrezaei Date: Thu, 18 Nov 2021 11:59:35 +0330 Subject: [PATCH 5/7] Use the same nonce vector as it was in the previous versions --- examples/common.rs | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/examples/common.rs b/examples/common.rs index 25270378..dda08827 100644 --- a/examples/common.rs +++ b/examples/common.rs @@ -54,19 +54,14 @@ pub fn aes_encrypt(key: &[u8], plaintext: &[u8]) -> AEAD { let aes_key = aes_gcm::Key::from_slice(key); let cipher = Aes256Gcm::new(aes_key); - let rand_string: String = thread_rng() - .sample_iter(&Alphanumeric) - .take(12) - .map(char::from) - .collect(); - - let nonce = Nonce::from_slice(rand_string.as_bytes()); // 12-Bytes; unique per message + let nonce_vector: Vec = repeat(3).take(12).collect(); + let nonce = Nonce::from_slice(nonce_vector.as_slice()); let out_tag: Vec = repeat(0).take(16).collect(); let text_payload = Payload { msg: plaintext, - aad: &out_tag[..] + aad: &out_tag.as_slice() }; let ciphertext = cipher.encrypt(nonce, text_payload) @@ -83,19 +78,14 @@ pub fn aes_decrypt(key: &[u8], aead_pack: AEAD) -> Vec { let aes_key = aes_gcm::Key::from_slice(key); - let rand_string: String = thread_rng() - .sample_iter(&Alphanumeric) - .take(12) - .map(char::from) - .collect(); - - let nonce = Nonce::from_slice(rand_string.as_bytes()); // 12-Bytes; unique per message + let nonce_vector: Vec = repeat(3).take(12).collect(); + let nonce = Nonce::from_slice(nonce_vector.as_slice()); let gcm = Aes256Gcm::new(aes_key); let text_payload = Payload { - msg: &aead_pack.ciphertext[..], - aad: &aead_pack.tag[..] + msg: &aead_pack.ciphertext.as_slice(), + aad: &aead_pack.tag.as_slice() }; let out = gcm.decrypt(nonce, text_payload); From c752f774114d14bff031df305e059dfdad4d3cf8 Mon Sep 17 00:00:00 2001 From: hrezaei Date: Thu, 18 Nov 2021 15:50:57 +0330 Subject: [PATCH 6/7] Try to fix a bug in decryption --- examples/common.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/common.rs b/examples/common.rs index dda08827..efddc9b4 100644 --- a/examples/common.rs +++ b/examples/common.rs @@ -84,8 +84,8 @@ pub fn aes_decrypt(key: &[u8], aead_pack: AEAD) -> Vec { let gcm = Aes256Gcm::new(aes_key); let text_payload = Payload { - msg: &aead_pack.ciphertext.as_slice(), - aad: &aead_pack.tag.as_slice() + msg: aead_pack.ciphertext.as_slice(), + aad: aead_pack.tag.as_slice() }; let out = gcm.decrypt(nonce, text_payload); From f7fbc01820f5e22b68dcf9651adcdd611008d357 Mon Sep 17 00:00:00 2001 From: hrezaei Date: Fri, 19 Nov 2021 14:11:51 +0330 Subject: [PATCH 7/7] Remove default value of aes_encrypt and unused crates --- examples/common.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/common.rs b/examples/common.rs index efddc9b4..84126a6f 100644 --- a/examples/common.rs +++ b/examples/common.rs @@ -9,8 +9,7 @@ use curv::{ elliptic::curves::traits::{ECPoint, ECScalar}, BigInt, }; -use rand::distributions::Alphanumeric; -use rand::{Rng, thread_rng}; + use reqwest::Client; use serde::{Deserialize, Serialize}; @@ -65,7 +64,7 @@ pub fn aes_encrypt(key: &[u8], plaintext: &[u8]) -> AEAD { }; let ciphertext = cipher.encrypt(nonce, text_payload) - .expect("encryption failure!"); // NOTE: handle this error to avoid panics! + .expect("encryption failure!"); AEAD { ciphertext: ciphertext, @@ -89,7 +88,7 @@ pub fn aes_decrypt(key: &[u8], aead_pack: AEAD) -> Vec { }; let out = gcm.decrypt(nonce, text_payload); - out.unwrap_or_default() + out.unwrap() } pub fn postb(client: &Client, path: &str, body: T) -> Option