Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate rust crypto to support m1 macs #149

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,9 @@ optional = true

[dev-dependencies]
criterion = "0.3"
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", features = ["json"] }
reqwest = { version = "0.9", default-features = false }
uuid = { version = "0.8", features = ["v4"] }
serde_json = "1.0"
Expand Down
55 changes: 37 additions & 18 deletions examples/common.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
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, Payload};

use curv::{
arithmetic::traits::Converter,
elliptic::curves::secp256_k1::{FE, GE},
elliptic::curves::traits::{ECPoint, ECScalar},
BigInt,
};

use reqwest::Client;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -50,26 +49,46 @@ pub struct Params {

#[allow(dead_code)]
pub fn aes_encrypt(key: &[u8], plaintext: &[u8]) -> AEAD {
let nonce: Vec<u8> = repeat(3).take(12).collect();
let aad: [u8; 0] = [];
let mut gcm = AesGcm::new(KeySize256, key, &nonce[..], &aad);
let mut out: Vec<u8> = repeat(0).take(plaintext.len()).collect();
let mut out_tag: Vec<u8> = 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 nonce_vector: Vec<u8> = repeat(3).take(12).collect();
let nonce = Nonce::from_slice(nonce_vector.as_slice());

let out_tag: Vec<u8> = repeat(0).take(16).collect();

let text_payload = Payload {
msg: plaintext,
aad: &out_tag.as_slice()
};

let ciphertext = cipher.encrypt(nonce, text_payload)
.expect("encryption failure!");

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<u8> {
let mut out: Vec<u8> = repeat(0).take(aead_pack.ciphertext.len()).collect();
let nonce: Vec<u8> = 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[..]);
out

let aes_key = aes_gcm::Key::from_slice(key);

let nonce_vector: Vec<u8> = 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.as_slice(),
aad: aead_pack.tag.as_slice()
};

let out = gcm.decrypt(nonce, text_payload);
out.unwrap()
}

pub fn postb<T>(client: &Client, path: &str, body: T) -> Option<String>
Expand Down
19 changes: 10 additions & 9 deletions examples/sm_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ use std::collections::HashMap;
use std::fs;
use std::sync::RwLock;

use rocket::{post, routes, State};
use rocket_contrib::json::Json;
use rocket::{post, routes, State, launch};
use rocket::serde::json::Json;

use uuid::Uuid;

mod common;
use common::{Entry, Index, Key, Params, PartySignup};

#[post("/get", format = "json", data = "<request>")]
fn get(
db_mtx: State<RwLock<HashMap<Key, String>>>,
db_mtx: &State<RwLock<HashMap<Key, String>>>,
request: Json<Index>,
) -> Json<Result<Entry, ()>> {
let index: Index = request.0;
Expand All @@ -31,15 +32,15 @@ fn get(
}

#[post("/set", format = "json", data = "<request>")]
fn set(db_mtx: State<RwLock<HashMap<Key, String>>>, request: Json<Entry>) -> Json<Result<(), ()>> {
fn set(db_mtx: &State<RwLock<HashMap<Key, String>>>, request: Json<Entry>) -> Json<Result<(), ()>> {
let entry: Entry = request.0;
let mut hm = db_mtx.write().unwrap();
hm.insert(entry.key.clone(), entry.value.clone());
Json(Ok(()))
}

#[post("/signupkeygen", format = "json")]
fn signup_keygen(db_mtx: State<RwLock<HashMap<Key, String>>>) -> Json<Result<PartySignup, ()>> {
fn signup_keygen(db_mtx: &State<RwLock<HashMap<Key, String>>>) -> Json<Result<PartySignup, ()>> {
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();
Expand Down Expand Up @@ -70,7 +71,7 @@ fn signup_keygen(db_mtx: State<RwLock<HashMap<Key, String>>>) -> Json<Result<Par
}

#[post("/signupsign", format = "json")]
fn signup_sign(db_mtx: State<RwLock<HashMap<Key, String>>>) -> Json<Result<PartySignup, ()>> {
fn signup_sign(db_mtx: &State<RwLock<HashMap<Key, String>>>) -> Json<Result<PartySignup, ()>> {
//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 ");
Expand Down Expand Up @@ -102,7 +103,8 @@ fn signup_sign(db_mtx: State<RwLock<HashMap<Key, String>>>) -> Json<Result<Party

//refcell, arc

fn main() {
#[launch]
fn rocket() -> _ {
// let mut my_config = Config::development();
// my_config.set_port(18001);
let db: HashMap<Key, String> = HashMap::new();
Expand Down Expand Up @@ -137,8 +139,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();
}