From 36b21e2629a7fbe7e4adb0d4fff2a8898efb73cd Mon Sep 17 00:00:00 2001 From: Georg Bramm Date: Mon, 21 Mar 2022 17:33:36 +0100 Subject: [PATCH] v0.3.0 --- Cargo.toml | 15 +- README.md | 18 +- rabe-console/Cargo.toml | 19 +- rabe-console/README.md | 28 + rabe-console/src/mod.rs | 1642 ++++++++++---------------------- src/.DS_Store | Bin 0 -> 6148 bytes src/error.rs | 92 ++ src/lib.rs | 106 +-- src/schemes/ac17/mod.rs | 48 +- src/schemes/aw11/mod.rs | 34 +- src/schemes/bdabe/mod.rs | 54 +- src/schemes/bsw/mod.rs | 30 +- src/schemes/lsw/mod.rs | 26 +- src/schemes/mke08/mod.rs | 50 +- src/schemes/mod.rs | 11 +- src/schemes/yct14/mod.rs | 36 +- src/utils/aes/mod.rs | 2 +- src/utils/file/mod.rs | 2 +- src/utils/hash/mod.rs | 25 +- src/utils/mod.rs | 17 +- src/utils/policy/dnf.rs | 16 +- src/utils/policy/msp.rs | 3 +- src/utils/policy/pest/mod.rs | 24 +- src/utils/secretsharing/mod.rs | 2 +- 24 files changed, 928 insertions(+), 1372 deletions(-) create mode 100644 rabe-console/README.md create mode 100644 src/.DS_Store create mode 100644 src/error.rs diff --git a/Cargo.toml b/Cargo.toml index 9160da7..75f153a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rabe" -version = "0.2.7" +version = "0.3.0" description = "ABE Schemes implemented in rust." authors = [ "Schanzenbach, Martin ", @@ -14,6 +14,11 @@ homepage = "https://github.com/Fraunhofer-AISEC/rabe" repository = "https://github.com/Fraunhofer-AISEC/rabe" documentation = "https://docs.rs/rabe" +[features] +default = ["serde"] +borsh = ["borsh/std", "rabe-bn/borsh"] +serde = ["serde/std", "rabe-bn/serde"] + [lib] name="rabe" crate-type=["rlib", "cdylib"] @@ -21,14 +26,14 @@ path = "src/lib.rs" [dependencies] aes = "0.7.0" +borsh = { version = "0.9.3", optional = true, default-features = false } eax = "0.4.1" pest = "2.0" pest_derive = "2.0" permutation = "0.4.0" -rabe-bn = "0.4.18" +rabe-bn = { version = "0.4.20", optional = true, default-features = false } rand = "0.8.5" -serde = "1.0.136" -serde_derive = "1.0.136" +serde = { version = "1.0", features = ["derive"], optional = true } sha3 = "0.9.1" [workspace] @@ -38,7 +43,7 @@ members = [ ] [dev-dependencies] -criterion = { version = "0.3", feaures = ["html_reports"]} +criterion = { version = "0.3", features = ["html_reports"]} rand = "0.8.5" [[bench]] diff --git a/README.md b/README.md index d42777b..dcbee5d 100644 --- a/README.md +++ b/README.md @@ -5,9 +5,10 @@ # Rabe -rabe is a rust library implementing several Attribute Based Encryption (ABE) schemes using a modified version of the `bn` library of zcash (type-3 pairing / Baretto Naering curve). The modification of `bn` brings in `serde` instead of the deprecated `rustc_serialize`. +rabe is a rust library implementing several Attribute Based Encryption (ABE) schemes using a modified version of the `bn` library of zcash (type-3 pairing / Baretto Naering curve). The modification of `bn` brings in `serde` or `borsh` instead of the deprecated `rustc_serialize`. +The standard serialization library is `serde`. If you want to use `borsh`, you need to specify it as feature. -This is a rust crate and comes with C bindings. For integration in distributed applications contact [us](mailto:info@aisec.fraunhofer.de). +For integration in distributed applications contact [us](mailto:info@aisec.fraunhofer.de). # Implemented Ciphertext Policy Schemes (CP-ABE) @@ -53,17 +54,10 @@ In order to compile and test: - install rust nightly - git clone library - install build-essential -- and then run `cargo build --release && RUST_BACKTRACE=1 cargo test -- --nocapture` +- and then run `cargo build --release && RUST_BACKTRACE=1 cargo test -- --nocapture` +- rabe is also available with borsh serialization. just add `--features borsh` to build command # Building rabe console app -In order to compile and test: -- install rust nightly -- git clone library -- install build-essential -- and then run `cargo run -p rabe-console` +See [README.md](./rabe-console/README.md) -For example, in order to create msk and pk of an AC17 KP-ABE scheme run: -```bash -$ cargo run -p rabe-console -- --scheme AC17KP setup -``` diff --git a/rabe-console/Cargo.toml b/rabe-console/Cargo.toml index 5ab96cc..56b52e2 100644 --- a/rabe-console/Cargo.toml +++ b/rabe-console/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rabe-console" -version = "0.2.7" +version = "0.3.0" description = "Console App for the ABE Schemes implemented in rabe." authors = [ "Schanzenbach, Martin ", @@ -14,20 +14,25 @@ homepage = "https://github.com/Fraunhofer-AISEC/rabe" repository = "https://github.com/Fraunhofer-AISEC/rabe" documentation = "https://docs.rs/rabe" +[features] +default = ["serde"] +std = [] +borsh = ["borsh/std", "rabe/borsh"] +serde = ["serde/std", "serde_cbor/std", "rabe/serde"] + [[bin]] name = "rabe" path = "src/mod.rs" [dependencies] -base64 = "0.10.1" +borsh = { version = "0.9.3", optional = true, default-features = false } +rustc-hex = "2.1.0" deflate = "0.9.0" inflate = "0.4.5" clap = "2.33.3" rand = "0.8.5" -serde = "1.0.136" -serde_derive = "1.0.136" -serde_json = "1.0.79" -serde_cbor = "0.11.2" +serde = { version = "1.0", features = ["derive"], optional = true } +serde_cbor = { version = "0.11.2", optional = true, default-features = false } pest = "2.0" pest_derive = "2.0" -rabe = { path = "../" } \ No newline at end of file +rabe = { path = "..", optional = true, default-features = false } \ No newline at end of file diff --git a/rabe-console/README.md b/rabe-console/README.md new file mode 100644 index 0000000..9c8f4c0 --- /dev/null +++ b/rabe-console/README.md @@ -0,0 +1,28 @@ +# Rabe console app + +This is an example console app, to test the implemented schemes + +For integration in distributed applications contact [us](mailto:info@aisec.fraunhofer.de). + + +# Building + +In order to compile and test: +- install rust nightly +- git clone library +- install build-essential +- Do one of the following + - run `cargo run -p rabe-console` from parent directory + - run `cargo run` from this directory + - compile using `cargo build --release` and afterwards run executable `./target/release/rabe` + +## Example calls using executable +- Setup a AC17 KP-ABE scheme + * ```bash + $ rabe --s AC17CP setup + ``` + * This generates msk.key and pk.key +- Generate a new key with attributes "A" and "B" + * ```bash + $ rabe --s AC17CP keygen --a 'A B' + ``` \ No newline at end of file diff --git a/rabe-console/src/mod.rs b/rabe-console/src/mod.rs index 333ba8b..da5c1e0 100644 --- a/rabe-console/src/mod.rs +++ b/rabe-console/src/mod.rs @@ -1,24 +1,25 @@ //! a rabe console application. //! -//! * Developped by Georg Bramm, Fraunhofer AISEC -//! * Date: 02/2021 -//! -extern crate base64; extern crate rand; -extern crate serde; extern crate rabe; extern crate deflate; extern crate inflate; -extern crate serde_json; -extern crate serde_derive; +#[cfg(feature = "borsh")] +extern crate borsh; +#[cfg(not(feature = "borsh"))] +extern crate serde; +#[cfg(not(feature = "borsh"))] +extern crate serde_cbor; + +extern crate rustc_hex as hex; + #[macro_use] extern crate clap; -extern crate serde_cbor; -use base64::{decode, encode}; +use hex::{FromHex, ToHex}; use clap::{App, Arg, ArgMatches, SubCommand}; use crate::rabe::{ - RabeError, + error::RabeError, schemes::{ ac17, aw11, @@ -33,15 +34,23 @@ use crate::rabe::{ file::{write_file, read_file, read_raw, write_from_vec, read_to_vec} } }; -use serde::Serialize; -use serde_cbor::{ - from_slice, - ser::to_vec_packed +#[cfg(not(feature = "borsh"))] +use serde::{ + de::DeserializeOwned, + Serialize }; +#[cfg(feature = "borsh")] +use borsh::{BorshSerialize, BorshDeserialize}; + use std::{ process, path::Path }; +#[cfg(not(feature = "borsh"))] +use serde_cbor::{ + ser::to_vec_packed, + from_slice +}; // File extensions const CT_EXTENSION: &'static str = "ct"; @@ -50,13 +59,12 @@ const KEY_DELEGATE_EXTENSION: &'static str = "del"; const DOT: &'static str = "."; // Object names -const ATTRIBUTES: &'static str = "attribute"; -const POLICY: &'static str = "policy"; -const NAME: &'static str = "name"; -const SCHEME: &'static str = "scheme"; -const JSON: &'static str = "json"; -const LANG: &'static str = "lang"; -const FILE: &'static str = "file"; +const ATTRIBUTES: &'static str = "a"; +const POLICY: &'static str = "p"; +const NAME: &'static str = "n"; +const SCHEME: &'static str = "s"; +const LANG: &'static str = "l"; +const FILE: &'static str = "f"; // Default file names const GP_FILE: &'static str = "gp"; @@ -69,24 +77,24 @@ const AU_PK_FILE: &'static str = "pkau"; const AU_SK_FILE: &'static str = "skau"; // Key file header and footer -const GP_BEGIN: &'static str = "-----BEGIN GLOBAL PARAMETERS-----\n"; -const GP_END: &'static str = "\n-----END GLOBAL PARAMETERS-----"; -const SK_BEGIN: &'static str = "-----BEGIN SECRET KEY-----\n"; -const SK_END: &'static str = "\n-----END SECRET KEY-----"; -const MSK_BEGIN: &'static str = "-----BEGIN MASTER SECRET KEY-----\n"; -const MSK_END: &'static str = "\n-----END MASTER SECRET KEY-----"; -const PK_BEGIN: &'static str = "-----BEGIN PUBLIC KEY-----\n"; -const PK_END: &'static str = "\n-----END PUBLIC KEY-----"; -const CT_BEGIN: &'static str = "-----BEGIN CIPHERTEXT-----\n"; -const CT_END: &'static str = "\n-----END CIPHERTEXT-----"; -const SKA_BEGIN: &'static str = "-----BEGIN SECRET ATTRIBUTE KEY-----\n"; -const SKA_END: &'static str = "\n-----END SECRET ATTRIBUTE KEY-----"; -const PKA_BEGIN: &'static str = "-----BEGIN PUBLIC ATTRIBUTE-----\n"; -const PKA_END: &'static str = "\n-----END PUBLIC ATTRIBUTE-----"; -const AU_PK_BEGIN: &'static str = "-----BEGIN PUBLIC AUTHORITY-----\n"; -const AU_PK_END: &'static str = "\n-----END PUBLIC AUTHORITY-----"; -const AU_SK_BEGIN: &'static str = "-----BEGIN SECRET AUTHORITY-----\n"; -const AU_SK_END: &'static str = "\n-----END SECRET AUTHORITY-----"; +const GP_BEGIN: &'static str = "-----BEGIN GP-----\n"; +const GP_END: &'static str = "\n-----END GP-----"; +const SK_BEGIN: &'static str = "-----BEGIN SK-----\n"; +const SK_END: &'static str = "\n-----END SK-----"; +const MSK_BEGIN: &'static str = "-----BEGIN MSK-----\n"; +const MSK_END: &'static str = "\n-----END MSK-----"; +const PK_BEGIN: &'static str = "-----BEGIN PK-----\n"; +const PK_END: &'static str = "\n-----END PK-----"; +const CT_BEGIN: &'static str = "-----BEGIN CT-----\n"; +const CT_END: &'static str = "\n-----END CT-----"; +const SKA_BEGIN: &'static str = "-----BEGIN SAK-----\n"; +const SKA_END: &'static str = "\n-----END SAK-----"; +const PKA_BEGIN: &'static str = "-----BEGIN PAK-----\n"; +const PKA_END: &'static str = "\n-----END PAK-----"; +const AU_PK_BEGIN: &'static str = "-----BEGIN PAUK-----\n"; +const AU_PK_END: &'static str = "\n-----END PAUK-----"; +const AU_SK_BEGIN: &'static str = "-----BEGIN SAUK-----\n"; +const AU_SK_END: &'static str = "\n-----END SAUK-----"; // Application commands const CMD_SETUP: &'static str = "setup"; @@ -141,12 +149,6 @@ fn main() { .possible_values(&Scheme::variants()) .help("scheme(s) to use."), ) - .arg( - Arg::with_name(JSON) - .long(JSON) - .required(false) - .takes_value(false), - ) .arg( Arg::with_name(LANG) .long(LANG) @@ -185,6 +187,7 @@ fn main() { .required(false) .takes_value(true) .multiple(true) + .last(true) .help("attributes to use."), ), ) @@ -274,13 +277,6 @@ fn main() { .default_value(&_au_sk_default) .help("authrotiy secret key file."), ) - .arg( - Arg::with_name(ATTRIBUTES) - .required(false) - .takes_value(true) - .multiple(true) - .help("attributes to use."), - ) .arg( Arg::with_name(POLICY) .required(false) @@ -292,6 +288,13 @@ fn main() { .required(false) .takes_value(true) .help("id of the user (AW11)"), + ) + .arg( + Arg::with_name(ATTRIBUTES) + .long(ATTRIBUTES) + .required(false) + .takes_value(true) + .help("attributes to use."), ), ) .subcommand( @@ -490,16 +493,15 @@ fn main() { } else { _lang = PolicyLanguage::HumanPolicy; } - let _json: bool = argument_matches.is_present(JSON); match argument_matches.subcommand() { - (CMD_SETUP, Some(arguments)) => run_setup(arguments, _scheme, _json), - (CMD_AUTHGEN, Some(arguments)) => run_authgen(arguments, _scheme, _lang, _json), - (CMD_KEYGEN, Some(arguments)) => run_keygen(arguments, _scheme, _lang, _json), - (CMD_DELEGATE, Some(arguments)) => run_delegate(arguments, _scheme, _lang, _json), - (CMD_ENCRYPT, Some(arguments)) => run_encrypt(arguments, _scheme, _lang, _json), - (CMD_DECRYPT, Some(arguments)) => run_decrypt(arguments, _scheme, _lang, _json), - (CMD_REQ_ATTR_PK, Some(arguments)) => run_req_attr_pk(arguments, _scheme, _lang, _json), - (CMD_REQ_ATTR_SK, Some(arguments)) => run_req_attr_sk(arguments, _scheme, _lang, _json), + (CMD_SETUP, Some(arguments)) => run_setup(arguments, _scheme), + (CMD_AUTHGEN, Some(arguments)) => run_authgen(arguments, _scheme, _lang), + (CMD_KEYGEN, Some(arguments)) => run_keygen(arguments, _scheme, _lang), + (CMD_DELEGATE, Some(arguments)) => run_delegate(arguments, _scheme, _lang), + (CMD_ENCRYPT, Some(arguments)) => run_encrypt(arguments, _scheme, _lang), + (CMD_DECRYPT, Some(arguments)) => run_decrypt(arguments, _scheme, _lang), + (CMD_REQ_ATTR_PK, Some(arguments)) => run_req_attr_pk(arguments, _scheme, _lang), + (CMD_REQ_ATTR_SK, Some(arguments)) => run_req_attr_sk(arguments, _scheme, _lang), _ => Ok(()), } } else { @@ -508,7 +510,7 @@ fn main() { } } - fn run_setup(arguments: &ArgMatches, _scheme: Scheme, _json: bool) -> Result<(), RabeError> { + fn run_setup(arguments: &ArgMatches, _scheme: Scheme) -> Result<(), RabeError> { let mut _msk_file = String::from(""); let mut _pk_file = String::from(""); let mut _gp_file = String::from(""); @@ -539,127 +541,65 @@ fn main() { match _scheme { Scheme::AC17CP | Scheme::AC17KP => { let (_pk, _msk) = ac17::setup(); - if _json { - write_file( - Path::new(&_msk_file), - serde_json::to_string_pretty(&_msk).unwrap(), - ); - write_file( - Path::new(&_pk_file), - serde_json::to_string_pretty(&_pk).unwrap(), - ); - } else { - write_file( - Path::new(&_msk_file), - ser_enc(_msk, MSK_BEGIN, MSK_END) - ); - write_file( - Path::new(&_pk_file), - ser_enc(_pk, PK_BEGIN, PK_END) - ); - } + write_file( + Path::new(&_msk_file), + ser_enc(_msk, MSK_BEGIN, MSK_END) + ); + write_file( + Path::new(&_pk_file), + ser_enc(_pk, PK_BEGIN, PK_END) + ); } Scheme::AW11 => { let _gp = aw11::setup(); - if _json { - write_file( - Path::new(&_gp_file), - serde_json::to_string_pretty(&_gp).unwrap(), - ); - } else { - write_file( - Path::new(&_msk_file), - ser_enc(_gp, GP_BEGIN, GP_END) - ); - } + write_file( + Path::new(&_msk_file), + ser_enc(_gp, GP_BEGIN, GP_END) + ); } Scheme::BDABE => { let (_pk, _msk) = bdabe::setup(); - if _json { - write_file( - Path::new(&_msk_file), - serde_json::to_string_pretty(&_msk).unwrap(), - ); - write_file( - Path::new(&_pk_file), - serde_json::to_string_pretty(&_pk).unwrap(), - ); - } else { - write_file( - Path::new(&_msk_file), - ser_enc(_msk, MSK_BEGIN, MSK_END) - ); - write_file( - Path::new(&_pk_file), - ser_enc(_pk, PK_BEGIN, PK_END) - ); - } + write_file( + Path::new(&_msk_file), + ser_enc(_msk, MSK_BEGIN, MSK_END) + ); + write_file( + Path::new(&_pk_file), + ser_enc(_pk, PK_BEGIN, PK_END) + ); } Scheme::BSW => { let (_pk, _msk) = bsw::setup(); - if _json { - write_file( - Path::new(&_msk_file), - serde_json::to_string_pretty(&_msk).unwrap(), - ); - write_file( - Path::new(&_pk_file), - serde_json::to_string_pretty(&_pk).unwrap(), - ); - } else { - write_file( - Path::new(&_msk_file), - ser_enc(_msk, MSK_BEGIN, MSK_END) - ); - write_file( - Path::new(&_pk_file), - ser_enc(_pk, PK_BEGIN, PK_END) - ); - } + write_file( + Path::new(&_msk_file), + ser_enc(_msk, MSK_BEGIN, MSK_END) + ); + write_file( + Path::new(&_pk_file), + ser_enc(_pk, PK_BEGIN, PK_END) + ); } Scheme::LSW => { let (_pk, _msk) = lsw::setup(); - if _json { - write_file( - Path::new(&_msk_file), - serde_json::to_string_pretty(&_msk).unwrap(), - ); - write_file( - Path::new(&_pk_file), - serde_json::to_string_pretty(&_pk).unwrap(), - ); - } else { - write_file( - Path::new(&_msk_file), - ser_enc(_msk, MSK_BEGIN, MSK_END) - ); - write_file( - Path::new(&_pk_file), - ser_enc(_pk, PK_BEGIN, PK_END) - ); - } + write_file( + Path::new(&_msk_file), + ser_enc(_msk, MSK_BEGIN, MSK_END) + ); + write_file( + Path::new(&_pk_file), + ser_enc(_pk, PK_BEGIN, PK_END) + ); } Scheme::MKE08 => { let (_pk, _msk) = mke08::setup(); - if _json { - write_file( - Path::new(&_msk_file), - serde_json::to_string_pretty(&_msk).unwrap(), - ); - write_file( - Path::new(&_pk_file), - serde_json::to_string_pretty(&_pk).unwrap(), - ); - } else { - write_file( - Path::new(&_msk_file), - ser_enc(_msk, MSK_BEGIN, MSK_END) - ); - write_file( - Path::new(&_pk_file), - ser_enc(_pk, PK_BEGIN, PK_END) - ); - } + write_file( + Path::new(&_msk_file), + ser_enc(_msk, MSK_BEGIN, MSK_END) + ); + write_file( + Path::new(&_pk_file), + ser_enc(_pk, PK_BEGIN, PK_END) + ); }, Scheme::YCT14 => { let mut _attributes: Vec = Vec::new(); @@ -676,25 +616,14 @@ fn main() { } if _attributes.len() > 0 { let (_pk, _msk) = yct14::setup(_attributes); - if _json { - write_file( - Path::new(&_msk_file), - serde_json::to_string_pretty(&_msk).unwrap(), - ); - write_file( - Path::new(&_pk_file), - serde_json::to_string_pretty(&_pk).unwrap(), - ); - } else { - write_file( - Path::new(&_msk_file), - ser_enc(_msk, MSK_BEGIN, MSK_END) - ); - write_file( - Path::new(&_pk_file), - ser_enc(_pk, PK_BEGIN, PK_END) - ); - } + write_file( + Path::new(&_msk_file), + ser_enc(_msk, MSK_BEGIN, MSK_END) + ); + write_file( + Path::new(&_pk_file), + ser_enc(_pk, PK_BEGIN, PK_END) + ); } else { return Err(RabeError::new("sorry, yct14 needs attributes at setup()")); @@ -708,7 +637,6 @@ fn main() { arguments: &ArgMatches, _scheme: Scheme, _lang: PolicyLanguage, - _json: bool, ) -> Result<(), RabeError> { let mut _policy: String = String::new(); let mut _attributes: Vec = Vec::new(); @@ -767,18 +695,10 @@ fn main() { } match _scheme { Scheme::AW11 => { - let mut _gp: aw11::Aw11GlobalKey; - if _json { - _gp = serde_json::from_str(&read_file(Path::new(&_gp_file))).unwrap(); - } else { - _gp = match ser_dec(&_gp_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _gp: aw11::Aw11GlobalKey = match ser_dec(&_gp_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; match aw11::authgen(&_gp, &_attributes) { None => { return Err(RabeError::new( @@ -786,98 +706,47 @@ fn main() { )); } Some((_pk, _msk)) => { - if _json { - write_file( - Path::new(&_msk_file), - serde_json::to_string_pretty(&_msk).unwrap(), - ); - write_file( - Path::new(&_pk_file), - serde_json::to_string_pretty(&_pk).unwrap(), - ); - } else { - write_file( - Path::new(&_msk_file), - ser_enc(_msk, AU_SK_BEGIN, AU_SK_END) - ); - write_file( - Path::new(&_pk_file), - ser_enc(_pk, AU_PK_BEGIN, AU_PK_END) - ); - } + write_file( + Path::new(&_msk_file), + ser_enc(_msk, AU_SK_BEGIN, AU_SK_END) + ); + write_file( + Path::new(&_pk_file), + ser_enc(_pk, AU_PK_BEGIN, AU_PK_END) + ); } } } Scheme::BDABE => { - let mut _pk: bdabe::BdabePublicKey; - let mut _msk: bdabe::BdabeMasterKey; - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_pk_file))).unwrap(); - _msk = serde_json::from_str(&read_file(Path::new(&_msk_file))).unwrap(); - } else { - _pk = match ser_dec(&_pk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _msk = match ser_dec(&_msk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: bdabe::BdabePublicKey = match ser_dec(&_pk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _msk: bdabe::BdabeMasterKey = match ser_dec(&_msk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; let _sk: bdabe::BdabeSecretAuthorityKey = bdabe::authgen(&_pk, &_msk, &_name); - if _json { - write_file( - Path::new(&_au_file), - serde_json::to_string_pretty(&_sk).unwrap(), - ); - } else { - write_file( - Path::new(&_au_file), - ser_enc(_sk, AU_SK_BEGIN, AU_SK_END) - ); - } + write_file( + Path::new(&_au_file), + ser_enc(_sk, AU_SK_BEGIN, AU_SK_END) + ); } Scheme::MKE08 => { - let mut _pk: mke08::Mke08PublicKey; - let mut _msk: mke08::Mke08MasterKey; - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_pk_file))).unwrap(); - _msk = serde_json::from_str(&read_file(Path::new(&_msk_file))).unwrap(); - } else { - _pk = match ser_dec(&_pk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _msk = match ser_dec(&_msk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: mke08::Mke08PublicKey = match ser_dec(&_pk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _msk: mke08::Mke08MasterKey = match ser_dec(&_msk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; let _sk: mke08::Mke08SecretAuthorityKey = mke08::authgen(&_name); - if _json { - write_file( - Path::new(&_au_file), - serde_json::to_string_pretty(&_sk).unwrap(), - ); - } else { - write_file( - Path::new(&_au_file), - ser_enc(_sk, AU_SK_BEGIN, AU_SK_END) - ); - } + write_file( + Path::new(&_au_file), + ser_enc(_sk, AU_SK_BEGIN, AU_SK_END) + ); }, _ => { return Err(RabeError::new("sorry, this is not a multi-authoriy scheme")); @@ -890,7 +759,6 @@ fn main() { arguments: &ArgMatches, _scheme: Scheme, _lang: PolicyLanguage, - _json: bool, ) -> Result<(), RabeError> { let mut _sk_file = String::from(""); let mut _ska_file = String::from(""); @@ -969,235 +837,113 @@ fn main() { } match _scheme { Scheme::AC17CP => { - let mut _msk: ac17::Ac17MasterKey; - if _json { - _msk = serde_json::from_str(&read_file(Path::new(&_msk_file))).unwrap(); - } else { - _msk = match ser_dec(&_msk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } - let _sk: ac17::Ac17CpSecretKey = ac17::cp_keygen(&_msk, &_attributes) - .unwrap(); - if _json { - write_file( - Path::new(&_sk_file), - serde_json::to_string_pretty(&_sk).unwrap(), - ); - } else { - write_file( - Path::new(&_sk_file), - ser_enc(_sk, SK_BEGIN, SK_END) - ); - } + print!("loading {} ...", _msk_file); + let _msk: ac17::Ac17MasterKey = match ser_dec(&_msk_file) { + Ok(parsed) => parsed, + Err(e) => panic!("{}", e.to_string()) + }; + println!("... done"); + print!("creating AC17CP sk for {:?} ...", _attributes); + match ac17::cp_keygen(&_msk, &_attributes) { + Ok(sk) => { + println!("... done"); + println!("saving as {}", _sk_file); + write_file( + Path::new(&_sk_file), + ser_enc(sk, SK_BEGIN, SK_END) + ) + }, + Err(e) => panic!("{}", e.to_string()) + }; } Scheme::AC17KP => { - let mut _msk: ac17::Ac17MasterKey; - if _json { - _msk = serde_json::from_str(&read_file(Path::new(&_msk_file))).unwrap(); - } else { - _msk = match ser_dec(&_msk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _msk: ac17::Ac17MasterKey = match ser_dec(&_msk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; let _sk: ac17::Ac17KpSecretKey = ac17::kp_keygen(&_msk, &_policy, _lang).unwrap(); - if _json { - write_file( - Path::new(&_sk_file), - serde_json::to_string_pretty(&_sk).unwrap(), - ); - } else { - write_file( - Path::new(&_sk_file), - ser_enc(_sk, SK_BEGIN, SK_END) - ); - } + write_file( + Path::new(&_sk_file), + ser_enc(_sk, SK_BEGIN, SK_END) + ); } Scheme::BSW => { - let mut _pk: bsw::CpAbePublicKey; - let mut _msk: bsw::CpAbeMasterKey; - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_pk_file))).unwrap(); - _msk = serde_json::from_str(&read_file(Path::new(&_msk_file))).unwrap(); - } else { - _pk = match ser_dec(&_pk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _msk = match ser_dec(&_msk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: bsw::CpAbePublicKey = match ser_dec(&_pk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _msk: bsw::CpAbeMasterKey = match ser_dec(&_msk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; let _sk: bsw::CpAbeSecretKey = bsw::keygen(&_pk, &_msk, &_attributes) .unwrap(); - if _json { - write_file( - Path::new(&_sk_file), - serde_json::to_string_pretty(&_sk).unwrap(), - ); - } else { - write_file( - Path::new(&_sk_file), - ser_enc(_sk, SK_BEGIN, SK_END) - ); - } + write_file( + Path::new(&_sk_file), + ser_enc(_sk, SK_BEGIN, SK_END) + ); } Scheme::LSW => { - let mut _pk: lsw::KpAbePublicKey; - let mut _msk: lsw::KpAbeMasterKey; - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_pk_file))).unwrap(); - _msk = serde_json::from_str(&read_file(Path::new(&_msk_file))).unwrap(); - } else { - _pk = match ser_dec(&_pk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _msk = match ser_dec(&_msk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: lsw::KpAbePublicKey = match ser_dec(&_pk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _msk: lsw::KpAbeMasterKey = match ser_dec(&_msk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; let _sk: lsw::KpAbeSecretKey = lsw::keygen(&_pk, &_msk, &_policy, _lang).unwrap(); - if _json { - write_file( - Path::new(&_sk_file), - serde_json::to_string_pretty(&_sk).unwrap(), - ); - } else { - write_file( - Path::new(&_sk_file), - ser_enc(_sk, SK_BEGIN, SK_END) - ); - } + write_file( + Path::new(&_sk_file), + ser_enc(_sk, SK_BEGIN, SK_END) + ); } Scheme::AW11 => { - let mut _pk: aw11::Aw11GlobalKey; - let mut _msk: aw11::Aw11MasterKey; - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_gp_file))).unwrap(); - _msk = serde_json::from_str(&read_file(Path::new(&_msk_file))).unwrap(); - } else { - _pk = match ser_dec(&_gp_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _msk = match ser_dec(&_msk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: aw11::Aw11GlobalKey = match ser_dec(&_gp_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _msk: aw11::Aw11MasterKey = match ser_dec(&_msk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; let _sk: aw11::Aw11SecretKey = aw11::keygen(&_pk, &_msk, &_name, &_attributes).unwrap(); - if _json { - write_file( - Path::new(&_name_file), - serde_json::to_string_pretty(&_sk).unwrap(), - ); - } else { - write_file( - Path::new(&_name_file), - ser_enc(_sk, SK_BEGIN, SK_END) - ); - } + write_file( + Path::new(&_name_file), + ser_enc(_sk, SK_BEGIN, SK_END) + ); } Scheme::BDABE => { - let mut _pk: bdabe::BdabePublicKey; - let mut _msk: bdabe::BdabeSecretAuthorityKey; - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_pk_file))).unwrap(); - _msk = serde_json::from_str(&read_file(Path::new(&_ska_file))).unwrap(); - } else { - _pk = match ser_dec(&_pk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _msk = match ser_dec(&_ska_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: bdabe::BdabePublicKey = match ser_dec(&_pk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _msk: bdabe::BdabeSecretAuthorityKey = match ser_dec(&_ska_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; let _sk: bdabe::BdabeUserKey = bdabe::keygen(&_pk, &_msk, &_name); - if _json { - write_file( - Path::new(&_name_file), - serde_json::to_string_pretty(&_sk).unwrap(), - ); - } else { - write_file( - Path::new(&_name_file), - ser_enc(_sk, SK_BEGIN, SK_END) - ); - } + write_file( + Path::new(&_name_file), + ser_enc(_sk, SK_BEGIN, SK_END) + ); } Scheme::MKE08 => { - let mut _pk: mke08::Mke08PublicKey; - let mut _msk: mke08::Mke08MasterKey; - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_pk_file))).unwrap(); - _msk = serde_json::from_str(&read_file(Path::new(&_msk_file))).unwrap(); - } else { - _pk = match ser_dec(&_pk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _msk = match ser_dec(&_msk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: mke08::Mke08PublicKey = match ser_dec(&_pk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _msk: mke08::Mke08MasterKey = match ser_dec(&_msk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; if _name != String::from("") { let _sk: mke08::Mke08UserKey = mke08::keygen(&_pk, &_msk, &_name); - if _json { - write_file( - Path::new(&_name_file), - serde_json::to_string_pretty(&_sk).unwrap(), - ); - } else { - write_file( - Path::new(&_name_file), - ser_enc(_sk, SK_BEGIN, SK_END) - ); - } + write_file( + Path::new(&_name_file), + ser_enc(_sk, SK_BEGIN, SK_END) + ); } else { return Err(RabeError::new( "sorry, the name/id for the user key is not set.", @@ -1205,39 +951,19 @@ fn main() { } } Scheme::YCT14 => { - let mut _pk: yct14::Yct14AbePublicKey; - let mut _msk: yct14::Yct14AbeMasterKey; - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_pk_file))).unwrap(); - _msk = serde_json::from_str(&read_file(Path::new(&_msk_file))).unwrap(); - } else { - _pk = match ser_dec(&_pk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _msk = match ser_dec(&_msk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: yct14::Yct14AbePublicKey = match ser_dec(&_pk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _msk: yct14::Yct14AbeMasterKey = match ser_dec(&_msk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; let _sk: yct14::Yct14AbeSecretKey = yct14::keygen(&_pk, &_msk, &_policy, _lang).unwrap(); - if _json { - write_file( - Path::new(&_sk_file), - serde_json::to_string_pretty(&_sk).unwrap(), - ); - } else { - write_file( - Path::new(&_sk_file), - ser_enc(_sk, SK_BEGIN, SK_END) - ); - } + write_file( + Path::new(&_sk_file), + ser_enc(_sk, SK_BEGIN, SK_END) + ); } } Ok(()) @@ -1247,7 +973,6 @@ fn main() { arguments: &ArgMatches, _scheme: Scheme, _lang: PolicyLanguage, - _json: bool, ) -> Result<(), RabeError> { let mut _sk_file = String::from(""); let mut _dg_file = String::from(""); @@ -1292,27 +1017,14 @@ fn main() { } match _scheme { Scheme::BSW => { - let mut _pk: bsw::CpAbePublicKey; - let mut _msk: bsw::CpAbeSecretKey; - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_pk_file))).unwrap(); - _msk = serde_json::from_str(&read_file(Path::new(&_sk_file))).unwrap(); - } else { - _pk = match ser_dec(&_pk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _msk = match ser_dec(&_sk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: bsw::CpAbePublicKey = match ser_dec(&_pk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _msk: bsw::CpAbeSecretKey = match ser_dec(&_sk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; let _sk: Option = bsw::delegate(&_pk, &_msk, &_attributes); match _sk { @@ -1322,17 +1034,10 @@ fn main() { )); } Some(_del) => { - if _json { - write_file( - Path::new(&_dg_file), - serde_json::to_string_pretty(&_del).unwrap(), - ); - } else { - write_file( - Path::new(&_dg_file), - ser_enc(_del, SK_BEGIN, SK_END) - ); - } + write_file( + Path::new(&_dg_file), + ser_enc(_del, SK_BEGIN, SK_END) + ); } } } @@ -1349,7 +1054,6 @@ fn main() { arguments: &ArgMatches, _scheme: Scheme, _lang: PolicyLanguage, - _json: bool, ) -> Result<(), RabeError> { let mut _pk_files: Vec = Vec::new(); let mut _pk_file = String::from(""); @@ -1407,33 +1111,18 @@ fn main() { let buffer: Vec = read_to_vec(Path::new(&_pt_file)); match _scheme { Scheme::AC17CP => { - let mut _pk: ac17::Ac17PublicKey; // only one pk is allowed if _pk_files.len() == 1 { _pk_file = _pk_files[0].clone(); - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_pk_file))).unwrap(); - } else { - _pk = match ser_dec(&_pk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: ac17::Ac17PublicKey = match ser_dec(&_pk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; let _ct = ac17::cp_encrypt(&_pk, &_policy, &buffer, _lang); - if _json { - write_file( - Path::new(&_ct_file), - serde_json::to_string_pretty(&_ct).unwrap(), - ); - } else { - write_file( - Path::new(&_ct_file), - ser_enc(_ct, CT_BEGIN, CT_END) - ); - } + write_file( + Path::new(&_ct_file), + ser_enc(_ct, CT_BEGIN, CT_END) + ); } else { return Err(RabeError::new( "sorry, encryption using the AC17CP Scheme with zero or multiple PKs is not possible. ", @@ -1441,33 +1130,17 @@ fn main() { } } Scheme::AC17KP => { - let mut _pk: ac17::Ac17PublicKey; // only one pk is allowed if _pk_files.len() == 1 { - _pk_file = _pk_files[0].clone(); - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_pk_file))).unwrap(); - } else { - _pk = match ser_dec(&_pk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: ac17::Ac17PublicKey = match ser_dec(&_pk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; let _ct = ac17::kp_encrypt(&_pk, &_attributes, &buffer); - if _json { - write_file( - Path::new(&_ct_file), - serde_json::to_string_pretty(&_ct).unwrap(), - ); - } else { - write_file( - Path::new(&_ct_file), - ser_enc(_ct, CT_BEGIN, CT_END) - ); - } + write_file( + Path::new(&_ct_file), + ser_enc(_ct, CT_BEGIN, CT_END) + ); } else { return Err(RabeError::new( "sorry, encryption using the AC17KP Scheme with zero or multiple PKs is not possible. ", @@ -1475,33 +1148,17 @@ fn main() { } } Scheme::BSW => { - let mut _pk: bsw::CpAbePublicKey; // only one pk is allowed if _pk_files.len() == 1 { - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_pk_files[0].clone()))) - .unwrap(); - } else { - _pk = match ser_dec(&_pk_files[0].clone()) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: bsw::CpAbePublicKey = match ser_dec(&_pk_files[0].clone()) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; let _ct = bsw::encrypt(&_pk, &_policy, &buffer, _lang); - if _json { - write_file( - Path::new(&_ct_file), - serde_json::to_string_pretty(&_ct).unwrap(), - ); - } else { - write_file( - Path::new(&_ct_file), - ser_enc(_ct, CT_BEGIN, CT_END) - ); - } + write_file( + Path::new(&_ct_file), + ser_enc(_ct, CT_BEGIN, CT_END) + ); } else { return Err(RabeError::new( "sorry, encryption using the BSW Scheme with zero or multiple PKs is not possible. ", @@ -1509,33 +1166,17 @@ fn main() { } } Scheme::LSW => { - let mut _pk: lsw::KpAbePublicKey; // only one pk is allowed if _pk_files.len() == 1 { - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_pk_files[0].clone()))) - .unwrap(); - } else { - _pk = match ser_dec(&_pk_files[0].clone()) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: lsw::KpAbePublicKey = match ser_dec(&_pk_files[0].clone()) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; let _ct = lsw::encrypt(&_pk, &_attributes, &buffer); - if _json { - write_file( - Path::new(&_ct_file), - serde_json::to_string_pretty(&_ct).unwrap(), - ); - } else { - write_file( - Path::new(&_ct_file), - ser_enc(_ct, CT_BEGIN, CT_END) - ); - } + write_file( + Path::new(&_ct_file), + ser_enc(_ct, CT_BEGIN, CT_END) + ); } else { return Err(RabeError::new( "sorry, encryption using the LSW Scheme with zero or multiple PKs is not possible. ", @@ -1543,159 +1184,73 @@ fn main() { } } Scheme::AW11 => { - let mut _gp: aw11::Aw11GlobalKey; - if _json { - _gp = serde_json::from_str(&read_file(Path::new(&_gp_file))).unwrap(); - } else { - _gp = match ser_dec(&_gp_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _gp: aw11::Aw11GlobalKey = match ser_dec(&_gp_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; let mut _pks: Vec = Vec::new(); for filename in _pk_files { - let mut _pka: aw11::Aw11PublicKey; - if _json { - _pka = serde_json::from_str(&read_file(Path::new(&filename))).unwrap(); - } else { - _pka = match ser_dec(&filename) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pka: aw11::Aw11PublicKey = match ser_dec(&filename) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; _pks.push(_pka); } let _ct = aw11::encrypt(&_gp, &_pks, &_policy, _lang, &buffer); - if _json { - write_file( - Path::new(&_ct_file), - serde_json::to_string_pretty(&_ct).unwrap(), - ); - } else { - write_file( - Path::new(&_ct_file), - ser_enc(_ct, CT_BEGIN, CT_END) - ); - } + write_file( + Path::new(&_ct_file), + ser_enc(_ct, CT_BEGIN, CT_END) + ); } Scheme::BDABE => { - let mut _pk: bdabe::BdabePublicKey; - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_pk_file))).unwrap(); - } else { - _pk = match ser_dec(&_pk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: bdabe::BdabePublicKey = match ser_dec(&_pk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; let mut _attr_vec: Vec = Vec::new(); for filename in _pk_files { - let mut _pka: bdabe::BdabePublicAttributeKey; - if _json { - _pka = serde_json::from_str(&read_file(Path::new(&filename))).unwrap(); - } else { - _pka = match ser_dec(&filename) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pka: bdabe::BdabePublicAttributeKey = match ser_dec(&filename) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; _attr_vec.push(_pka); } let _ct = bdabe::encrypt(&_pk, &_attr_vec, &_policy, &buffer, _lang); - if _json { - write_file( - Path::new(&_ct_file), - serde_json::to_string_pretty(&_ct).unwrap(), - ); - } else { - write_file( - Path::new(&_ct_file), - ser_enc(_ct, CT_BEGIN, CT_END) - ); - } + write_file( + Path::new(&_ct_file), + ser_enc(_ct, CT_BEGIN, CT_END) + ); } Scheme::MKE08 => { - let mut _pk: mke08::Mke08PublicKey; - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_pk_file))).unwrap(); - } else { - _pk = match ser_dec(&_pk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: mke08::Mke08PublicKey = match ser_dec(&_pk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; let mut _attr_vec: Vec = Vec::new(); for filename in _pk_files { - let mut _pka: mke08::Mke08PublicAttributeKey; - if _json { - _pka = serde_json::from_str(&read_file(Path::new(&filename))).unwrap(); - } else { - _pka = match ser_dec(&filename) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pka: mke08::Mke08PublicAttributeKey = match ser_dec(&filename) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; _attr_vec.push(_pka); } let _ct = mke08::encrypt(&_pk, &_attr_vec, &_policy, _lang, &buffer); - if _json { - write_file( - Path::new(&_ct_file), - serde_json::to_string_pretty(&_ct).unwrap(), - ); - } else { - write_file( - Path::new(&_ct_file), - ser_enc(_ct, CT_BEGIN, CT_END) - ); - } + write_file( + Path::new(&_ct_file), + ser_enc(_ct, CT_BEGIN, CT_END) + ); } Scheme::YCT14 => { - let mut _pk: yct14::Yct14AbePublicKey; - // only one pk is allowed if _pk_files.len() == 1 { - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_pk_files[0].clone()))) - .unwrap(); - } else { - _pk = match ser_dec(&_pk_files[0].clone()) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: yct14::Yct14AbePublicKey = match ser_dec(&_pk_files[0].clone()) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; let _ct = yct14::encrypt(&_pk, &_attributes, &buffer); - if _json { - write_file( - Path::new(&_ct_file), - serde_json::to_string_pretty(&_ct).unwrap(), - ); - } else { - write_file( - Path::new(&_ct_file), - ser_enc(_ct, CT_BEGIN, CT_END) - ); - } + write_file( + Path::new(&_ct_file), + ser_enc(_ct, CT_BEGIN, CT_END) + ); } else { return Err(RabeError::new( "sorry, encryption using the LSW Scheme with zero or multiple PKs is not possible. ", @@ -1710,7 +1265,6 @@ fn main() { arguments: &ArgMatches, _scheme: Scheme, _lang: PolicyLanguage, - _json: bool, ) -> Result<(), RabeError> { let mut _sk_file = String::from(""); let mut _gp_file = String::from(""); @@ -1752,222 +1306,103 @@ fn main() { } match _scheme { Scheme::AC17CP => { - let mut _sk: ac17::Ac17CpSecretKey; - let mut _ct: ac17::Ac17CpCiphertext; - if _json { - _sk = serde_json::from_str(&read_file(Path::new(&_sk_file))).unwrap(); - _ct = serde_json::from_str(&read_file(Path::new(&_file))).unwrap(); - } else { - _sk = match ser_dec(&_sk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _ct = match ser_dec(&_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _sk: ac17::Ac17CpSecretKey = match ser_dec(&_sk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _ct: ac17::Ac17CpCiphertext = match ser_dec(&_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; _pt_option = ac17::cp_decrypt(&_sk, &_ct); } Scheme::AC17KP => { - let mut _sk: ac17::Ac17KpSecretKey; - let mut _ct: ac17::Ac17KpCiphertext; - if _json { - _sk = serde_json::from_str(&read_file(Path::new(&_sk_file))).unwrap(); - _ct = serde_json::from_str(&read_file(Path::new(&_file))).unwrap(); - } else { - _sk = match ser_dec(&_sk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _ct = match ser_dec(&_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _sk: ac17::Ac17KpSecretKey = match ser_dec(&_sk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _ct: ac17::Ac17KpCiphertext = match ser_dec(&_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; _pt_option = ac17::kp_decrypt(&_sk, &_ct); } Scheme::BSW => { - let mut _sk: bsw::CpAbeSecretKey; - let mut _ct: bsw::CpAbeCiphertext; - if _json { - _sk = serde_json::from_str(&read_file(Path::new(&_sk_file))).unwrap(); - _ct = serde_json::from_str(&read_file(Path::new(&_file))).unwrap(); - } else { - _sk = match ser_dec(&_sk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _ct = match ser_dec(&_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _sk: bsw::CpAbeSecretKey = match ser_dec(&_sk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _ct: bsw::CpAbeCiphertext = match ser_dec(&_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; _pt_option = bsw::decrypt(&_sk, &_ct); } Scheme::LSW => { - let mut _sk: lsw::KpAbeSecretKey; - let mut _ct: lsw::KpAbeCiphertext; - if _json { - _sk = serde_json::from_str(&read_file(Path::new(&_sk_file))).unwrap(); - _ct = serde_json::from_str(&read_file(Path::new(&_file))).unwrap(); - } else { - _sk = match ser_dec(&_sk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _ct = match ser_dec(&_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _sk: lsw::KpAbeSecretKey = match ser_dec(&_sk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _ct: lsw::KpAbeCiphertext = match ser_dec(&_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; _pt_option = lsw::decrypt(&_sk, &_ct); } Scheme::AW11 => { - let mut _gp: aw11::Aw11GlobalKey; - let mut _sk: aw11::Aw11SecretKey; - let mut _ct: aw11::Aw11Ciphertext; - if _json { - _gp = serde_json::from_str(&read_file(Path::new(&_gp_file))).unwrap(); - _sk = serde_json::from_str(&read_file(Path::new(&_sk_file))).unwrap(); - _ct = serde_json::from_str(&read_file(Path::new(&_file))).unwrap(); - } else { - _gp = match ser_dec(&_gp_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _sk = match ser_dec(&_sk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _ct = match ser_dec(&_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _gp: aw11::Aw11GlobalKey = match ser_dec(&_gp_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _sk: aw11::Aw11SecretKey = match ser_dec(&_sk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _ct: aw11::Aw11Ciphertext = match ser_dec(&_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; _pt_option = aw11::decrypt(&_gp, &_sk, &_ct); } Scheme::BDABE => { - let mut _pk: bdabe::BdabePublicKey; - let mut _sk: bdabe::BdabeUserKey; - let mut _ct: bdabe::BdabeCiphertext; - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_pk_file))).unwrap(); - _sk = serde_json::from_str(&read_file(Path::new(&_sk_file))).unwrap(); - _ct = serde_json::from_str(&read_file(Path::new(&_file))).unwrap(); - } else { - _pk = match ser_dec(&_pk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _sk = match ser_dec(&_sk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _ct = match ser_dec(&_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: bdabe::BdabePublicKey = match ser_dec(&_pk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _sk: bdabe::BdabeUserKey = match ser_dec(&_sk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _ct: bdabe::BdabeCiphertext = match ser_dec(&_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; _pt_option = bdabe::decrypt(&_pk, &_sk, &_ct); } Scheme::MKE08 => { - let mut _pk: mke08::Mke08PublicKey; - let mut _sk: mke08::Mke08UserKey; - let mut _ct: mke08::Mke08Ciphertext; - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_gp_file))).unwrap(); - _sk = serde_json::from_str(&read_file(Path::new(&_sk_file))).unwrap(); - _ct = serde_json::from_str(&read_file(Path::new(&_file))).unwrap(); - } else { - _pk = match ser_dec(&_gp_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _sk = match ser_dec(&_sk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _ct = match ser_dec(&_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: mke08::Mke08PublicKey = match ser_dec(&_gp_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _sk: mke08::Mke08UserKey = match ser_dec(&_sk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _ct: mke08::Mke08Ciphertext = match ser_dec(&_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; _pt_option = mke08::decrypt(&_pk, &_sk, &_ct); } Scheme::YCT14 => { - let mut _sk: yct14::Yct14AbeSecretKey; - let mut _ct: yct14::Yct14AbeCiphertext; - if _json { - _sk = serde_json::from_str(&read_file(Path::new(&_sk_file))).unwrap(); - _ct = serde_json::from_str(&read_file(Path::new(&_file))).unwrap(); - } else { - _sk = match ser_dec(&_sk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _ct = match ser_dec(&_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _sk: yct14::Yct14AbeSecretKey = match ser_dec(&_sk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _ct: yct14::Yct14AbeCiphertext = match ser_dec(&_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; _pt_option = yct14::decrypt(&_sk, &_ct); } } @@ -1986,7 +1421,6 @@ fn main() { arguments: &ArgMatches, _scheme: Scheme, _lang: PolicyLanguage, - _json: bool, ) -> Result<(), RabeError> { let mut _pk_file = String::from(""); let mut _au_sk_file = String::from(""); @@ -2030,79 +1464,39 @@ fn main() { if _attributes.len() == 1 { match _scheme { Scheme::MKE08 => { - let mut _pk: mke08::Mke08PublicKey; - let mut _ska: mke08::Mke08SecretAuthorityKey; - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_pk_file))).unwrap(); - _ska = serde_json::from_str(&read_file(Path::new(&_au_sk_file))).unwrap(); - } else { - _pk = match ser_dec(&_pk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _ska = match ser_dec(&_au_sk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: mke08::Mke08PublicKey = match ser_dec(&_pk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _ska: mke08::Mke08SecretAuthorityKey = match ser_dec(&_au_sk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; match mke08::request_authority_pk(&_pk, &_attributes[0], &_ska) { Ok(_a_pk) => { - if _json { - write_file( - Path::new(&_pka_file), - serde_json::to_string_pretty(&_a_pk).unwrap(), - ); - } else { - write_file( - Path::new(&_pka_file), - ser_enc(_a_pk, PKA_BEGIN, PKA_END) - ); - } + write_file( + Path::new(&_pka_file), + ser_enc(_a_pk, PKA_BEGIN, PKA_END) + ); }, Err(e) => return Err(e) } } Scheme::BDABE => { - let mut _pk: bdabe::BdabePublicKey; - let mut _ska: bdabe::BdabeSecretAuthorityKey; - if _json { - _pk = serde_json::from_str(&read_file(Path::new(&_pk_file))).unwrap(); - _ska = serde_json::from_str(&read_file(Path::new(&_au_sk_file))).unwrap(); - } else { - _pk = match ser_dec(&_pk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _ska = match ser_dec(&_au_sk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _pk: bdabe::BdabePublicKey = match ser_dec(&_pk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _ska: bdabe::BdabeSecretAuthorityKey = match ser_dec(&_au_sk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; match bdabe::request_attribute_pk(&_pk, &_ska, &_attributes[0]) { Ok(_a_pk) => { - if _json { - write_file( - Path::new(&_pka_file), - serde_json::to_string_pretty(&_a_pk).unwrap(), - ); - } else { - write_file( - Path::new(&_pka_file), - ser_enc(_a_pk, PKA_BEGIN, PKA_END) - ); - } + write_file( + Path::new(&_pka_file), + ser_enc(_a_pk, PKA_BEGIN, PKA_END) + ); }, Err(e) => return Err(e) } @@ -2125,7 +1519,6 @@ fn main() { arguments: &ArgMatches, _scheme: Scheme, _lang: PolicyLanguage, - _json: bool, ) -> Result<(), RabeError> { let mut _sk_file = String::from(""); let mut _ask_file = String::from(""); @@ -2169,89 +1562,47 @@ fn main() { if _attributes.len() == 1 { match _scheme { Scheme::MKE08 => { - let mut _usk: mke08::Mke08UserKey; - let mut _ska: mke08::Mke08SecretAttributeKey; - let mut _skau: mke08::Mke08SecretAuthorityKey; - if _json { - _usk = serde_json::from_str(&read_file(Path::new(&_sk_file))).unwrap(); - _skau = serde_json::from_str(&read_file(Path::new(&_au_sk_file))).unwrap(); - } else { - _usk = match ser_dec(&_sk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _skau = match ser_dec(&_au_sk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _usk: mke08::Mke08UserKey = match ser_dec(&_sk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _skau: mke08::Mke08SecretAuthorityKey = match ser_dec(&_au_sk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; match mke08::request_authority_sk( &_attributes[0], &_skau, &_usk._pk_u, ) { Ok(_a_sk) => { - if _json { - write_file( - Path::new(&_ask_file), - serde_json::to_string_pretty(&_a_sk).unwrap(), - ); - } else { - write_file( - Path::new(&_ask_file), - ser_enc(_a_sk, SKA_BEGIN, SKA_END), - ); - } + write_file( + Path::new(&_ask_file), + ser_enc(_a_sk, SKA_BEGIN, SKA_END), + ); }, Err(e) => return Err(e) } } Scheme::BDABE => { - let mut _usk: bdabe::BdabeUserKey; - let mut _ska: bdabe::BdabeSecretAttributeKey; - let mut _skau: bdabe::BdabeSecretAuthorityKey; - if _json { - _usk = serde_json::from_str(&read_file(Path::new(&_sk_file))).unwrap(); - _skau = serde_json::from_str(&read_file(Path::new(&_au_sk_file))).unwrap(); - } else { - _usk = match ser_dec(&_sk_file) { - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - _skau = match ser_dec(&_au_sk_file){ - Ok(parsed) => match from_slice(&parsed) { - Ok(parsed_res) => parsed_res, - Err(e) => return Err(RabeError::new(e.to_string().as_str())) - }, - Err(e) => return Err(e) - }; - } + let _usk: bdabe::BdabeUserKey = match ser_dec(&_sk_file) { + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; + let _skau: bdabe::BdabeSecretAuthorityKey = match ser_dec(&_au_sk_file){ + Ok(parsed) => parsed, + Err(e) => return Err(e) + }; match bdabe::request_attribute_sk( &_usk._pk, &_skau, &_attributes[0], ) { Ok(_a_sk) => { - if _json { - write_file( - Path::new(&_ask_file), - serde_json::to_string_pretty(&_a_sk).unwrap(), - ); - } else { - write_file( - Path::new(&_ask_file), - ser_enc(_a_sk, SKA_BEGIN, SKA_END) - ); - } + write_file( + Path::new(&_ask_file), + ser_enc(_a_sk, SKA_BEGIN, SKA_END) + ); }, Err(e) => println!("Error: {}", e.to_string()) } @@ -2271,34 +1622,63 @@ fn main() { } } +#[cfg(feature = "borsh")] +fn ser_enc(input: T, head: &str, tail: &str) -> String { + use deflate::deflate_bytes; + [ + head.to_string(), + deflate_bytes( + &input.try_to_vec().unwrap() + ).to_hex(), + tail.to_string() + ].concat() +} + +#[cfg(not(feature = "borsh"))] fn ser_enc(input: T, head: &str, tail: &str) -> String { use deflate::deflate_bytes; [ - head, - &encode( - &deflate_bytes( - &to_vec_packed(&input).unwrap() - ) - ), - tail + head.to_string(), + deflate_bytes( + &to_vec_packed(&input).unwrap() + ) + .to_hex(), + tail.to_string() ].concat() } -fn ser_dec(file_name: &String) -> Result, RabeError> { + +#[cfg(feature = "borsh")] +fn ser_dec(file_name: &String) -> Result { + match ser_dec_bin(file_name) { + Ok(parsed_bin) => match T::try_from_slice(&parsed_bin) { + Ok(parsed) => Ok(parsed), + Err(e) => return Err(RabeError::new(&format!("try_from_slice: {}", e.to_string().as_str()))) + }, + Err(e) => return Err(RabeError::new(&format!("ser_dec_bin: {}", e.to_string().as_str()))) + } +} + +#[cfg(not(feature = "borsh"))] +fn ser_dec(file_name: &String) -> Result { + match ser_dec_bin(file_name) { + Ok(parsed_bin) => match from_slice(&parsed_bin) { + Ok(parsed_res) => Ok(parsed_res), + Err(e) => return Err(RabeError::new(&format!("from_slice: {}", e.to_string().as_str()))) + }, + Err(e) => return Err(RabeError::new(&format!("ser_dec_bin: {}", e.to_string().as_str()))) + } +} + +fn ser_dec_bin(file_name: &String) -> Result, RabeError> { use inflate::inflate_bytes; - match decode( - &read_raw( - &read_file( - Path::new(file_name) - ) - ) - ) { - Ok(base64) => { - match inflate_bytes(&base64) { + let string = read_raw(&read_file(Path::new(file_name))); + match string.from_hex::>() { + Ok(byte_slice) => { + match inflate_bytes(&byte_slice) { Ok(bytes) => Ok(bytes), - Err(e) => Err(RabeError::new(e.to_string().as_str())) + Err(e) => return Err(RabeError::new(&format!("inflate_bytes: {}", e.to_string().as_str()))) } }, - Err(e) => Err(RabeError::new(e.to_string().as_str())) + Err(e) => return Err(RabeError::new(&format!("read_raw: {}", e.to_string().as_str()))) } - } \ No newline at end of file diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..fa24ac627079e0abcb4d509d86488d395dfdde6f GIT binary patch literal 6148 zcmeHKOH0E*5Z-O8O({YS3OxqA7L1Sjz)Ohr4;aydN=8j~qL@vh|vyi1ho=$?e zVxYfh!nHS9${yGV*8XYzA^^`@Fo~lyv+Z|Yt2frxn?}=U8+YDF*e@i-ALib@WwOWDv^!Ye3Ws(SXONi32_@RV6)G$AoS z3=jjvz-lp|&jF*oT4htU!~iky0|sz^u%ID28ViMT>wphlpV8kyL;)S&5{Q;YM`NK7 zJRn@90;*K5pBP-FgWb|`j>bZvN@rZo4CCmT%f}0svxD7|;fy;9=}Qa{1IrB5RoBGx z{~Ugq%18ck3iXHqV&I=Kz#Dz9??O@fZ2eXqp0yUVduS*aSD*p{`qCu;I=GK?q*M7V b)FIB%SSZ9%&@R&f=^`Krp${?e3k-Y!-?~hd literal 0 HcmV?d00001 diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..a1e6e9a --- /dev/null +++ b/src/error.rs @@ -0,0 +1,92 @@ +use std::{fmt::{ + Display, + Result, + Formatter +}, cmp}; +use pest::error::{Error as PestError, LineColLocation}; +use utils::policy::pest::json::Rule as jsonRule; +use utils::policy::pest::human::Rule as humanRule; +use eax::aead; +use std::array::TryFromSliceError; +use rabe_bn::FieldError; +#[cfg(not(feature = "borsh"))] +use serde::{Serialize, Deserialize}; + +#[cfg(feature = "borsh")] +use borsh::{BorshSerialize, BorshDeserialize}; + +/// Simple, generic Error that is compose of a String +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] +pub struct RabeError { + details: String, +} + +impl RabeError { + /// Creates a new Error + pub fn new(msg: &str) -> RabeError { + RabeError { details: msg.to_string() } + } +} + +impl Display for RabeError { + fn fmt(&self, f: &mut Formatter) -> Result { + write!(f, "Error: {}", self.details) + } +} + + +impl From> for RabeError { + fn from(error: PestError) -> Self { + let line = match error.line_col.to_owned() { + LineColLocation::Pos((line, _)) => line, + LineColLocation::Span((start_line, _), (end_line, _)) => cmp::max(start_line, end_line), + }; + RabeError::new( + format!("Json Policy Error in line {}\n", line).as_ref() + ) + } +} + +impl From> for RabeError { + fn from(error: PestError) -> Self { + let line = match error.line_col.to_owned() { + LineColLocation::Pos((line, _)) => line, + LineColLocation::Span((start_line, _), (end_line, _)) => cmp::max(start_line, end_line), + }; + RabeError::new( + format!("Json Policy Error in line {}\n", line).as_ref() + ) + } +} +impl From for RabeError { + fn from(error: FieldError) -> Self { + // Aead's error is intentionally opaque, there is no more information in here + match error { + FieldError::InvalidSliceLength => RabeError::new("FieldError::InvalidSliceLength"), + FieldError::InvalidU512Encoding => RabeError::new("FieldError::InvalidU512Encoding"), + FieldError::NotMember => RabeError::new("FieldError::NotMember"), + } + } +} + + +impl From for RabeError { + fn from(_error: aead::Error) -> Self { + // Aead's error is intentionally opaque, there is no more information in here + RabeError::new("Error during symmetric encryption or decryption!") + } +} + +impl From for RabeError { + fn from(_error: TryFromSliceError) -> Self { + RabeError::new(&_error.to_string()) + } +} + +impl From for RabeError { + fn from(_error: String) -> Self { + RabeError::new(_error.as_str()) + } +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 89cffa1..ac0f1b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,16 +1,18 @@ -//! This is the documentation for the RABE library. -//! -//! * Developped by Georg Bramm, Martin Schanzenbach, Julian Schuette -//! * Type: encryption (attribute-based) -//! * Setting: bilinear groups (asymmetric), based on a modified bn library by zcash -//! * Date: 07/2020 -//! -#![allow(dead_code)] -#[macro_use] -extern crate serde_derive; +//! rabe is a rust library implementing several Attribute Based Encryption (ABE) schemes using a modified version of the `bn` library of zcash (type-3 pairing / Baretto Naering curve). The modification of `bn` brings in `serde` or `borsh` instead of the deprecated `rustc_serialize`. +/// The standard serialization library is `serde`. If you want to use `borsh`, you need to specify it as feature. +/// +/// For integration in distributed applications contact [us](mailto:info@aisec.fraunhofer.de). +///! +///! * Developped by Bramm, Schanzenbach, Schuette +#[allow(dead_code)] + +#[cfg(feature = "borsh")] +extern crate borsh; +#[cfg(not(feature = "borsh"))] +extern crate serde; + extern crate rabe_bn; extern crate rand; -extern crate serde; extern crate pest; extern crate eax; extern crate aes; @@ -18,84 +20,10 @@ extern crate sha3; #[macro_use] extern crate pest_derive; -/// implemented schemes +/// rabe schemes pub mod schemes; -/// various utilities +/// rabe library utilities pub mod utils; +/// rabe error, that is used in the library +pub mod error; -use std::{fmt::{ - Display, - Result, - Formatter -}, error::Error, cmp}; -use pest::error::{Error as PestError, LineColLocation}; -use utils::policy::pest::json::Rule as jsonRule; -use utils::policy::pest::human::Rule as humanRule; -use eax::aead; -use std::array::TryFromSliceError; - -#[derive(Debug, Serialize)] -pub struct RabeError { - details: String, -} - -impl RabeError { - pub fn new(msg: &str) -> RabeError { - RabeError { details: msg.to_string() } - } -} - -impl Display for RabeError { - fn fmt(&self, f: &mut Formatter) -> Result { - write!(f, "Error: {}", self.details) - } -} - -impl Error for RabeError { - fn description(&self) -> &str { - &self.details - } -} - -impl From> for RabeError { - fn from(error: PestError) -> Self { - let line = match error.line_col.to_owned() { - LineColLocation::Pos((line, _)) => line, - LineColLocation::Span((start_line, _), (end_line, _)) => cmp::max(start_line, end_line), - }; - RabeError::new( - format!("Json Policy Error in line {}\n", line).as_ref() - ) - } -} - -impl From> for RabeError { - fn from(error: PestError) -> Self { - let line = match error.line_col.to_owned() { - LineColLocation::Pos((line, _)) => line, - LineColLocation::Span((start_line, _), (end_line, _)) => cmp::max(start_line, end_line), - }; - RabeError::new( - format!("Json Policy Error in line {}\n", line).as_ref() - ) - } -} - -impl From for RabeError { - fn from(_error: aead::Error) -> Self { - // Aead's error is intentionally opaque, there is no more information in here - RabeError::new("Error during symmetric encryption or decryption!") - } -} - -impl From for RabeError { - fn from(_error: TryFromSliceError) -> Self { - RabeError::new(&_error.to_string()) - } -} - -impl From for RabeError { - fn from(_error: String) -> Self { - RabeError::new(_error.as_str()) - } -} \ No newline at end of file diff --git a/src/schemes/ac17/mod.rs b/src/schemes/ac17/mod.rs index d5d6d0b..330d119 100644 --- a/src/schemes/ac17/mod.rs +++ b/src/schemes/ac17/mod.rs @@ -1,8 +1,8 @@ -//! This is the documentation for the `AC17` scheme, both versions AC17CP and AC17KP are included. +//! `AC17` scheme by Shashank Agrawal, Melissa Chase. //! //! * Developped by Shashank Agrawal, Melissa Chase, "FAME: Fast Attribute-based Message Encryption", see Section 3 //! * Published in Proceedings of the 2017 ACM SIGSAC Conference on Computer and Communications Security 2017 -//! * Available from https://eprint.iacr.org/2017/807.pdf +//! * Available from //! * Type: encryption (attribute-based) //! * Setting: bilinear groups (asymmetric) //! * Authors: Georg Bramm @@ -55,10 +55,16 @@ use utils::{ hash::sha3_hash }; use utils::policy::pest::{PolicyLanguage, parse, PolicyType}; -use RabeError; +use crate::error::RabeError; +#[cfg(not(feature = "borsh"))] +use serde::{Serialize, Deserialize}; +#[cfg(feature = "borsh")] +use borsh::{BorshSerialize, BorshDeserialize}; /// An AC17 Public Key (PK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Ac17PublicKey { pub _g: G1, pub _h_a: Vec, @@ -66,7 +72,9 @@ pub struct Ac17PublicKey { } /// An AC17 Public Key (MK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Ac17MasterKey { pub _g: G1, pub _h: G2, @@ -76,7 +84,9 @@ pub struct Ac17MasterKey { } /// An AC17 Ciphertext (CT) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Ac17Ciphertext { pub _c_0: Vec, pub _c: Vec<(String, Vec)>, @@ -85,21 +95,27 @@ pub struct Ac17Ciphertext { } /// An AC17 CP-ABE Ciphertext (CT), composed of a policy and an Ac17Ciphertext. -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Ac17CpCiphertext { pub _policy: (String, PolicyLanguage), pub _ct: Ac17Ciphertext, } /// An AC17 KP-ABE Ciphertext (CT), composed of a set of attributes and an Ac17Ciphertext. -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Ac17KpCiphertext { pub _attr: Vec, pub _ct: Ac17Ciphertext, } /// An AC17 Secret Key (SK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Ac17SecretKey { pub _k_0: Vec, pub _k: Vec<(String, Vec)>, @@ -107,14 +123,18 @@ pub struct Ac17SecretKey { } /// An AC17 KP-ABE Secret Key (SK), composed of a policy and an Ac17Ciphertext. -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Ac17KpSecretKey { pub _policy: (String, PolicyLanguage), pub _sk: Ac17SecretKey, } /// An AC17 CP-ABE Secret Key (SK), composed of a set of attributes and an Ac17Ciphertext. -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Ac17CpSecretKey { pub _attr: Vec, pub _sk: Ac17SecretKey, @@ -174,11 +194,11 @@ pub fn setup() -> (Ac17PublicKey, Ac17MasterKey) { /// * `msk` - A Master Key (MSK), generated by the function setup() /// * `attributes` - A Vector of String attributes assigned to this user key /// -pub fn cp_keygen(msk: &Ac17MasterKey, attributes: &Vec) -> Option { +pub fn cp_keygen(msk: &Ac17MasterKey, attributes: &Vec) -> Result { // if no attibutes or an empty policy // maybe add empty msk also here if attributes.is_empty() { - return None; + return Err(RabeError::new("empty attributes!")); } // random number generator let mut _rng = rand::thread_rng(); @@ -244,7 +264,7 @@ pub fn cp_keygen(msk: &Ac17MasterKey, attributes: &Vec) -> Option //! * Type: encryption (identity-based) //! * Setting: bilinear groups (asymmetric) //! * Authors: Georg Bramm @@ -37,29 +37,41 @@ use utils::{ }; use utils::policy::pest::{PolicyLanguage, parse, PolicyType}; use utils::secretsharing::gen_shares_policy; -use RabeError; +use crate::error::RabeError; +#[cfg(not(feature = "borsh"))] +use serde::{Serialize, Deserialize}; +#[cfg(feature = "borsh")] +use borsh::{BorshSerialize, BorshDeserialize}; /// An AW11 Global Parameters Key (GK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Aw11GlobalKey { pub _g1: G1, pub _g2: G2, } /// An AW11 Public Key (PK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Aw11PublicKey { pub _attr: Vec<(String, Gt, G2)>, } /// An AW11 Master Key (MK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Aw11MasterKey { pub _attr: Vec<(String, Fr, Fr)>, } /// An AW11 Ciphertext (CT) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Aw11Ciphertext { pub _policy: (String, PolicyLanguage), pub _c_0: Gt, @@ -68,14 +80,18 @@ pub struct Aw11Ciphertext { } /// An AW11 Secret Key (SK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Aw11SecretKey { pub _gid: String, pub _attr: Vec<(String, G1)>, } /// A global Context for an AW11 Global Parameters Key (GP) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Aw11GlobalContext { pub _gk: Aw11GlobalKey, } diff --git a/src/schemes/bdabe/mod.rs b/src/schemes/bdabe/mod.rs index 3585a25..0161833 100644 --- a/src/schemes/bdabe/mod.rs +++ b/src/schemes/bdabe/mod.rs @@ -1,8 +1,8 @@ -//! This is the documentation for the `BDABE` scheme. +//! `BDABE` scheme by Bramm, Gall, Schuette. //! -//! * Developped by Bramm, Gall, Schuette , "Blockchain based Distributed Attribute-based Encryption" -//! * Published in not yet -//! * Available from not yet +//! * Developped by Bramm, Gall, Schuette, "Blockchain based Distributed Attribute-based Encryption" +//! * Published in ICETE 2018 +//! * Available from //! * Type: encryption (attribute-based) //! * Setting: bilinear groups (asymmetric) //! * Authors: Georg Bramm @@ -36,11 +36,17 @@ use utils::{ hash::sha3_hash_fr }; use utils::policy::pest::{PolicyLanguage, parse, PolicyType}; -use RabeError; +use crate::error::RabeError; use utils::policy::dnf::policy_in_dnf; +#[cfg(not(feature = "borsh"))] +use serde::{Serialize, Deserialize}; +#[cfg(feature = "borsh")] +use borsh::{BorshSerialize, BorshDeserialize}; /// A BDABE Public Key (PK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct BdabePublicKey { pub _g1: G1, pub _g2: G2, @@ -50,13 +56,17 @@ pub struct BdabePublicKey { } /// A BDABE Master Key (MK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct BdabeMasterKey { pub _y: Fr, } /// A BDABE User Key (PKu, SKu and SKa's) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct BdabeUserKey { pub _sk: BdabeSecretUserKey, pub _pk: BdabePublicUserKey, @@ -64,7 +74,9 @@ pub struct BdabeUserKey { } /// A BDABE Public User Key (PKu) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct BdabePublicUserKey { pub _u: String, pub _u1: G1, @@ -72,14 +84,18 @@ pub struct BdabePublicUserKey { } /// A BDABE Secret User Key (SKu) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct BdabeSecretUserKey { pub _u1: G1, pub _u2: G2, } /// A BDABE Secret Attribute Key (SKa) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct BdabeSecretAttributeKey { pub _str: String, pub _au1: G1, @@ -87,7 +103,9 @@ pub struct BdabeSecretAttributeKey { } /// A BDABE Public Attribute Key (PKa) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct BdabePublicAttributeKey { pub _str: String, pub _a1: G1, @@ -96,7 +114,9 @@ pub struct BdabePublicAttributeKey { } /// A BDABE Secret Authority Key (SKauth) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct BdabeSecretAuthorityKey { pub _a1: G1, pub _a2: G2, @@ -105,7 +125,9 @@ pub struct BdabeSecretAuthorityKey { } /// A Ciphertext Tuple representing a conjunction in a CT -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct BdabeCiphertextTuple { pub _str: Vec, pub _e1: Gt, @@ -116,7 +138,9 @@ pub struct BdabeCiphertextTuple { } /// A BDABE Ciphertext (CT) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct BdabeCiphertext { pub _policy: (String, PolicyLanguage), pub _j: Vec, diff --git a/src/schemes/bsw/mod.rs b/src/schemes/bsw/mod.rs index cb5136f..6296193 100644 --- a/src/schemes/bsw/mod.rs +++ b/src/schemes/bsw/mod.rs @@ -1,8 +1,8 @@ -//! This is the documentation for the `BSW` scheme: +//! `BSW` scheme by John Bethencourt, Amit Sahai, Brent Waters. //! //! * Developped by John Bethencourt, Amit Sahai, Brent Waters, "Ciphertext-Policy Attribute-Based Encryption" //! * Published in Security and Privacy, 2007. SP'07. IEEE Symposium on. IEEE -//! * Available from https://doi.org/10.1109/SP.2007.11 +//! * Available from //! * Type: encryption (attribute-based) //! * Setting: bilinear groups (asymmetric) //! * Authors: Georg Bramm @@ -29,10 +29,16 @@ use utils::{ hash::* }; use utils::policy::pest::{PolicyLanguage, parse, PolicyType}; -use RabeError; +use crate::error::RabeError; +#[cfg(not(feature = "borsh"))] +use serde::{Serialize, Deserialize}; +#[cfg(feature = "borsh")] +use borsh::{BorshSerialize, BorshDeserialize}; /// A BSW Public Key (PK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct CpAbePublicKey { pub _g1: G1, pub _g2: G2, @@ -42,14 +48,18 @@ pub struct CpAbePublicKey { } /// A BSW Master Key (MSK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct CpAbeMasterKey { pub _beta: Fr, pub _g2_alpha: G2, } /// A BSW Ciphertext (CT) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct CpAbeCiphertext { pub _policy: (String, PolicyLanguage), pub _c: G1, @@ -59,14 +69,18 @@ pub struct CpAbeCiphertext { } /// A BSW Secret User Key (SK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct CpAbeSecretKey { pub _d: G2, pub _d_j: Vec, } /// A BSW Attribute -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct CpAbeAttribute { pub _str: String, pub _g1: G1, diff --git a/src/schemes/lsw/mod.rs b/src/schemes/lsw/mod.rs index 7dbca3c..bd364eb 100644 --- a/src/schemes/lsw/mod.rs +++ b/src/schemes/lsw/mod.rs @@ -1,8 +1,8 @@ -//! This is the documentation for the `LSW` scheme: +//! `LSW` scheme by Allison Lewko, Amit Sahai and Brent Waters. //! //! * Developped by Allison Lewko, Amit Sahai and Brent Waters, "Revocation Systems with Very Small Private Keys" //! * Published in Security and Privacy, 2010. SP'10. IEEE Symposium on. IEEE -//! * Available from http://eprint.iacr.org/2008/309.pdf +//! * Available from //! * Type: encryption (key-policy attribute-based) //! * Setting: bilinear groups (asymmetric) //! * Authors: Georg Bramm @@ -30,10 +30,16 @@ use utils::{ }; use rand::Rng; use utils::policy::pest::{PolicyLanguage, parse}; -use RabeError; +use crate::error::RabeError; +#[cfg(not(feature = "borsh"))] +use serde::{Serialize, Deserialize}; +#[cfg(feature = "borsh")] +use borsh::{BorshSerialize, BorshDeserialize}; /// A LSW Public Key (PK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct KpAbePublicKey { _g_g1: G1, _g_g2: G2, @@ -44,7 +50,9 @@ pub struct KpAbePublicKey { } /// A LSW Master Key (MSK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct KpAbeMasterKey { _alpha1: Fr, _alpha2: Fr, @@ -54,14 +62,18 @@ pub struct KpAbeMasterKey { } /// A LSW Secret User Key (SK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct KpAbeSecretKey { _policy: (String, PolicyLanguage), _dj: Vec<(String, G1, G2, G1, G1, G1)>, } /// A LSW Ciphertext (CT) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct KpAbeCiphertext { _e1: Gt, _e2: G2, diff --git a/src/schemes/mke08/mod.rs b/src/schemes/mke08/mod.rs index 13956e3..8367f66 100644 --- a/src/schemes/mke08/mod.rs +++ b/src/schemes/mke08/mod.rs @@ -1,8 +1,8 @@ -//! This is the documentation for the `MKE08` scheme: +//! `MKE08` scheme Müller, Katzenbeisser, Eckert. //! //! * Developped by S Müller, S Katzenbeisser, C Eckert , "Distributed Attribute-based Encryption" //! * Published in International Conference on Information Security and Cryptology, Heidelberg, 2008 -//! * Available from http://www2.seceng.informatik.tu-darmstadt.de/assets/mueller/icisc08.pdf +//! * Available from //! * Type encryption (attribute-based) //! * Setting bilinear groups (asymmetric) //! * Authors Georg Bramm @@ -39,10 +39,16 @@ use utils::{ }; use utils::policy::pest::{PolicyLanguage, parse, PolicyType}; use utils::policy::dnf::policy_in_dnf; -use RabeError; +use crate::error::RabeError; +#[cfg(not(feature = "borsh"))] +use serde::{Serialize, Deserialize}; +#[cfg(feature = "borsh")] +use borsh::{BorshSerialize, BorshDeserialize}; /// A MKE08 Public Key (PK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Mke08PublicKey { pub _g1: G1, pub _g2: G2, @@ -53,14 +59,18 @@ pub struct Mke08PublicKey { } /// A MKE08 Master Key (MK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Mke08MasterKey { pub _g1_y: G1, pub _g2_y: G2, } /// A MKE08 User Key (SK), consisting of a Secret User Key (SKu), a Public User Key (PKu) and a Vector of Secret Attribute Keys (SKau) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Mke08UserKey { pub _sk_u: Mke08SecretUserKey, pub _pk_u: Mke08PublicUserKey, @@ -68,7 +78,9 @@ pub struct Mke08UserKey { } /// A MKE08 Public User Key (PKu) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Mke08PublicUserKey { pub _u: String, pub _pk_g1: G1, @@ -76,21 +88,27 @@ pub struct Mke08PublicUserKey { } /// A MKE08 Secret User Key (SKu) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Mke08SecretUserKey { pub _sk_g1: G1, pub _sk_g2: G2, } /// A MKE08 Secret Authrotiy Key (SKauth) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Mke08SecretAuthorityKey { pub _a: String, pub _r: Fr, } /// A MKE08 Public Attribute Key (PKa) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Mke08PublicAttributeKey { pub _str: String, pub _g1: G1, @@ -100,7 +118,9 @@ pub struct Mke08PublicAttributeKey { } /// A MKE08 Secret Attribute Key (SKa) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Mke08SecretAttributeKey { pub _str: String, pub _g1: G1, @@ -108,7 +128,9 @@ pub struct Mke08SecretAttributeKey { } /// A MKE08 Ciphertext (CT) consisting of the AES encrypted data as well as a Vector of all Conjunctions of the access policy -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Mke08Ciphertext { pub _policy: (String, PolicyLanguage), pub _e: Vec, @@ -116,7 +138,9 @@ pub struct Mke08Ciphertext { } /// A MKE08 Ciphertext Conjunction (CTcon) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Mke08CTConjunction { pub _str: Vec, pub _j1: Gt, diff --git a/src/schemes/mod.rs b/src/schemes/mod.rs index fc0be21..5d3caf9 100644 --- a/src/schemes/mod.rs +++ b/src/schemes/mod.rs @@ -1,14 +1,5 @@ -//! This is the documentation for all R-ABE schemes -//! -//! Currently those are: -//! * AC17 CP-ABE -//! * AC17 KP-ABE -//! * AW11 CP-ABE -//! * BDABE CP-ABE -//! * BSW CP-ABE -//! * LSW KP-ABE -//! * MKE08 CP-ABE //! +//! The following schemes are implemented in the rabe library pub mod ac17; pub mod aw11; pub mod bdabe; diff --git a/src/schemes/yct14/mod.rs b/src/schemes/yct14/mod.rs index 5adc418..6916c1f 100644 --- a/src/schemes/yct14/mod.rs +++ b/src/schemes/yct14/mod.rs @@ -1,7 +1,8 @@ +//! `YCT14` scheme by Xuanxia Yao, Zhi Chen, Ye Tian. //! //! * Developped by Xuanxia Yao, Zhi Chen, Ye Tian, "A lightweight attribute-based encryption scheme for the Internet of things" //! * Published in: Future Generation Computer Systems -//! * Available From: http://www.sciencedirect.com/science/article/pii/S0167739X14002039 +//! * Available From: //! * Type: encryption (key-policy attribute-based) //! * Setting: No pairing //! * Authors: Georg Bramm @@ -26,17 +27,26 @@ use utils::{ }; use rand::Rng; use utils::policy::pest::{PolicyLanguage, parse}; -use RabeError; +use crate::error::RabeError; use std::ops::Mul; +#[cfg(not(feature = "borsh"))] +use serde::{Serialize, Deserialize}; +#[cfg(feature = "borsh")] +use borsh::{BorshSerialize, BorshDeserialize}; -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Yct14Attribute { name: String, - #[serde(skip_serializing_if = "Option::is_none")] + #[cfg_attr(feature = "borsh", borsh_skip)] + #[cfg_attr(not(feature = "borsh"), serde(skip_serializing_if = "Option::is_none"))] node: Option, } -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub enum Yct14Type { Public(Gt), Private(Fr), @@ -115,14 +125,18 @@ impl Yct14Attribute { } /// A Public Key (PK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Yct14AbePublicKey { g: Gt, attributes: Vec } /// A Master Key (MSK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Yct14AbeMasterKey { s: Fr, attributes: Vec @@ -144,7 +158,9 @@ impl Yct14AbeMasterKey { } /// A Secret User Key (SK) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Yct14AbeSecretKey { policy: (String, PolicyLanguage), du: Vec, @@ -166,7 +182,9 @@ impl Yct14AbeSecretKey { } /// A Ciphertext (CT) -#[derive(Serialize, Deserialize, PartialEq, Clone)] +#[derive(Clone, PartialEq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub struct Yct14AbeCiphertext { attributes: Vec, ct: Vec, diff --git a/src/utils/aes/mod.rs b/src/utils/aes/mod.rs index c1411ec..d008ea8 100644 --- a/src/utils/aes/mod.rs +++ b/src/utils/aes/mod.rs @@ -1,6 +1,6 @@ use eax::Eax; use eax::aead::{Aead, NewAead, generic_array::GenericArray}; -use crate::RabeError; +use crate::error::RabeError; use std::convert::TryInto; use rand::thread_rng; use rand::Rng; diff --git a/src/utils/file/mod.rs b/src/utils/file/mod.rs index 1d67b0c..b736e9a 100644 --- a/src/utils/file/mod.rs +++ b/src/utils/file/mod.rs @@ -17,7 +17,7 @@ pub fn read_file(_path: &Path) -> String { let mut s = String::new(); match file.read_to_string(&mut s) { Err(why) => panic!("sorry, couldn't read {}: {}", display, why.to_string()), - Ok(_) => print!("successfully read {}", display), + Ok(_) => {}, } return s; } diff --git a/src/utils/hash/mod.rs b/src/utils/hash/mod.rs index 2a73015..d641ed8 100644 --- a/src/utils/hash/mod.rs +++ b/src/utils/hash/mod.rs @@ -1,32 +1,27 @@ use rabe_bn::Fr; use sha3::{ Digest, - Sha3_512 + Sha3_256 }; -use RabeError; +use crate::error::RabeError; use std::ops::Mul; -use std::convert::TryInto; -/// hash a String to an element of G using Sha3_256 and generator g +/// Hash to a &String to [`rabe-bn::G1`] or [`rabe-bn::G2`] using Base g pub fn sha3_hash>(g: T, data: &String) -> Result { - let mut hasher = Sha3_512::new(); + let mut hasher = Sha3_256::new(); hasher.update(data.as_bytes()); - let vec = hasher.finalize().to_vec(); - assert_eq!(vec.len(), 64); - match vec.as_slice().try_into() { - Ok(res) => Ok(g * Fr::interpret(res)), + match Fr::from_slice(&hasher.finalize()) { + Ok(fr) => Ok(g * fr), Err(e) => Err(e.into()) } } -/// hash a String to Fr using blake2b +/// Hash to a &String to [`rabe-bn::Fr`] pub fn sha3_hash_fr(data: &String) -> Result { - let mut hasher = Sha3_512::new(); + let mut hasher = Sha3_256::new(); hasher.update(data.as_bytes()); - let vec = hasher.finalize().to_vec(); - assert_eq!(vec.len(), 64); - match vec.as_slice().try_into() { - Ok(res) => Ok(Fr::interpret(res)), + match Fr::from_slice(&hasher.finalize()) { + Ok(fr) => Ok(fr), Err(e) => Err(e.into()) } } \ No newline at end of file diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 312d59b..0c37b72 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,16 +1,15 @@ -//! This is the documentation for all R-ABE utils -//! -//! Currently those are: -//! * aes -//! * hash -//! * policy -//! * secretSharing -//! * tools -//! * file //! +//! The following tools are used in the rabe library + +/// Symmetric encryption pub mod aes; +/// Hash to [`rabe-bn::Fr`] and Hash to [`rabe-bn::G1`] or [`rabe-bn::G2`] pub mod hash; +/// Language (human and json) and Policy parsers (in msp and dnf) pub mod policy; +/// Secret sharing utilities pub mod secretsharing; +/// various functions pub mod tools; +/// File operations pub mod file; diff --git a/src/utils/policy/dnf.rs b/src/utils/policy/dnf.rs index 7880ec6..42a75af 100644 --- a/src/utils/policy/dnf.rs +++ b/src/utils/policy/dnf.rs @@ -1,7 +1,7 @@ use std::string::String; use rabe_bn::{Group, Gt, G1, G2}; use crate::{ - RabeError, + error::RabeError, schemes::{ mke08::*, bdabe::* @@ -105,7 +105,7 @@ pub fn dnf( _pks: &Vec, _p: &PolicyValue, _i: usize, - _parent: Option + _parent: Option<&PolicyType> ) -> bool { let mut ret = true; // inner node @@ -139,13 +139,13 @@ pub fn dnf( return match _parent { Some(PolicyType::And) => { for child in children { - ret = ret && dnf(_dnfp, _pks, &child, _i, Some(PolicyType::And)) + ret = ret && dnf(_dnfp, _pks, &child, _i, Some(&PolicyType::And)) } ret }, Some(PolicyType::Or) => { for (i, child) in children.iter().enumerate() { - ret = ret && dnf(_dnfp, _pks, &child, i + _i, Some(PolicyType::Or)) + ret = ret && dnf(_dnfp, _pks, &child, i + _i, Some(&PolicyType::Or)) } ret }, @@ -154,17 +154,17 @@ pub fn dnf( }, PolicyValue::Object(obj) => { return match _parent { - None => dnf(_dnfp, _pks, &obj.1, _i, Some(obj.0.clone())), - Some(PolicyType::Leaf) => dnf(_dnfp, _pks, &obj.1, _i, Some(PolicyType::Leaf)), + None => dnf(_dnfp, _pks, &obj.1, _i, Some(&obj.0)), + Some(PolicyType::Leaf) => dnf(_dnfp, _pks, &obj.1, _i, Some(&PolicyType::Leaf)), Some(PolicyType::Or) => { match &obj.0 { - PolicyType::And => dnf(_dnfp, _pks, &obj.1, _i, Some(PolicyType::And)), + PolicyType::And => dnf(_dnfp, _pks, &obj.1, _i, Some(&PolicyType::And)), _ => false, } } Some(PolicyType::And) => { match &obj.0 { - PolicyType::Leaf => dnf(_dnfp, _pks, &obj.1, _i, Some(PolicyType::Leaf)), + PolicyType::Leaf => dnf(_dnfp, _pks, &obj.1, _i, Some(&PolicyType::Leaf)), _ => false, } } diff --git a/src/utils/policy/msp.rs b/src/utils/policy/msp.rs index 5d22a63..637cdd1 100644 --- a/src/utils/policy/msp.rs +++ b/src/utils/policy/msp.rs @@ -1,12 +1,13 @@ use std::string::String; use utils::policy::pest::{PolicyLanguage, PolicyValue, parse, PolicyType}; -use RabeError; +use crate::error::RabeError; use std::fmt::{Display, Formatter, Result as FormatResult}; const ZERO: i8 = 0; const PLUS: i8 = 1; const MINUS: i8 = -1; + pub struct AbePolicy { pub _m: Vec>, pub _pi: Vec, diff --git a/src/utils/policy/pest/mod.rs b/src/utils/policy/pest/mod.rs index 6ca40c3..82b14ec 100644 --- a/src/utils/policy/pest/mod.rs +++ b/src/utils/policy/pest/mod.rs @@ -1,32 +1,42 @@ use pest::Parser; use std::string::String; -use RabeError; +use crate::error::RabeError; +use self::human::HumanPolicyParser; +use self::json::JSONPolicyParser; +#[cfg(not(feature = "borsh"))] +use serde::{Serialize, Deserialize}; +#[cfg(feature = "borsh")] +use borsh::{BorshSerialize, BorshDeserialize}; pub(crate) mod json; pub(crate) mod human; -use self::human::HumanPolicyParser; -use self::json::JSONPolicyParser; -#[derive(Serialize, Deserialize, PartialEq, Clone, Copy)] +/// Policy Language Type. Currently two types are available: +#[derive(Copy, Clone, PartialEq, Eq, Debug)] +#[cfg_attr(feature = "borsh", derive(BorshSerialize, BorshDeserialize))] +#[cfg_attr(not(feature = "borsh"), derive(Serialize, Deserialize))] pub enum PolicyLanguage { + /// A JSON policy language JsonPolicy, + /// A natural human language HumanPolicy, } -#[derive(Serialize, Deserialize, PartialEq, Clone)] +/// Internally there are only three types of nodes: AND, OR and LEAF nodes pub enum PolicyType { And, Or, Leaf } -#[derive(Serialize, Deserialize, PartialEq, Clone)] +/// The value of a node may either be a String, and Array of values oder a child with value pub enum PolicyValue<'a> { Object((PolicyType, Box>)), Array(Vec>), String(&'a str), } +/// Parses a &str in a give [PolicyLanguage] pub fn parse(policy: &str, language: PolicyLanguage) -> Result { match language { PolicyLanguage::JsonPolicy => { @@ -45,7 +55,7 @@ pub fn parse(policy: &str, language: PolicyLanguage) -> Result) -> String { use self::PolicyValue::*; match language { diff --git a/src/utils/secretsharing/mod.rs b/src/utils/secretsharing/mod.rs index fa8792f..b6d67e6 100644 --- a/src/utils/secretsharing/mod.rs +++ b/src/utils/secretsharing/mod.rs @@ -4,7 +4,7 @@ use utils::{ tools::{contains, usize_to_fr, get_value}, policy::pest::{PolicyValue, PolicyLanguage, parse, PolicyType} }; -use RabeError; +use crate::error::RabeError; pub fn calc_coefficients(_json: &PolicyValue, _fr: Option, _type: Option) -> Option> { let _coeff = _fr.unwrap_or(Fr::one());