aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatharina Fey <kookie@spacekookie.de>2018-04-02 12:05:59 +0200
committerKatharina Fey <kookie@spacekookie.de>2018-04-02 12:05:59 +0200
commit5fe943efec2f7c0210d9b12767876ef3a251dfac (patch)
treebee7828484fbad19e7680915a3505398ebd98a6b
parent9526f691b5a2cb65aff132b2d96d95ec77c6c088 (diff)
Making a small crypto example work
-rw-r--r--Cargo.lock131
-rw-r--r--Cargo.toml4
-rw-r--r--src/main.rs108
-rw-r--r--src/security/crypto.rs74
-rw-r--r--src/security/random.rs1
5 files changed, 113 insertions, 205 deletions
diff --git a/Cargo.lock b/Cargo.lock
index f7ad73b..d6eb8f2 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -8,14 +8,6 @@ dependencies = [
]
[[package]]
-name = "arrayvec"
-version = "0.4.7"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
name = "base64"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -63,11 +55,6 @@ version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
-name = "cfg-if"
-version = "0.1.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
name = "chrono"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -103,37 +90,6 @@ version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
-name = "crossbeam-deque"
-version = "0.2.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "crossbeam-epoch 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "crossbeam-epoch"
-version = "0.3.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
- "cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)",
- "scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "crossbeam-utils"
-version = "0.2.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
name = "crypto-mac"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -205,16 +161,6 @@ version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
-name = "lazy_static"
-version = "0.2.11"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
-name = "lazy_static"
-version = "1.0.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
name = "libc"
version = "0.2.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -223,7 +169,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
name = "lockchain"
version = "0.1.0"
dependencies = [
- "aesni 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"base64 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
"blake2 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -232,18 +177,12 @@ dependencies = [
"ordermap 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"pwhash 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)",
- "ring 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.36 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.36 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
-name = "memoffset"
-version = "0.2.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
name = "miscreant"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -261,11 +200,6 @@ dependencies = [
]
[[package]]
-name = "nodrop"
-version = "0.1.12"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
name = "num-integer"
version = "0.1.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -279,14 +213,6 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
-name = "num_cpus"
-version = "1.8.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
name = "opaque-debug"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -352,43 +278,11 @@ dependencies = [
]
[[package]]
-name = "rayon"
-version = "0.8.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "rayon-core 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
-name = "rayon-core"
-version = "1.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "crossbeam-deque 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
- "num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
name = "redox_syscall"
version = "0.1.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
-name = "ring"
-version = "0.12.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)",
- "lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
- "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
- "rayon 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "untrusted 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
name = "rust-crypto"
version = "0.2.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -411,11 +305,6 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
-name = "scopeguard"
-version = "0.3.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
name = "serde"
version = "1.0.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -487,11 +376,6 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
-name = "untrusted"
-version = "0.5.1"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
name = "winapi"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -512,7 +396,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum aesni 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "5e88c3698cd61460af7bdbcc747d0e37b61255492fcfc81845dd4666f3bf6714"
-"checksum arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "a1e964f9e24d588183fcb43503abda40d288c8657dfc27311516ce2f05675aef"
"checksum base64 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7c4a342b450b268e1be8036311e2c613d7f8a7ed31214dff1cc3b60852a3168d"
"checksum bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3c30d3802dfb7281680d6285f2ccdaa8c2d8fee41f93805dba5c4cf50dc23cf"
"checksum blake2 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b0da79fe9391e02df251e0d86197332d398b4721e80722fc91b9c4cbde3ce355"
@@ -520,14 +403,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum byte-tools 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "560c32574a12a89ecd91f5e742165893f86e3ab98d21f8ea548658eb9eef5f40"
"checksum byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "73b5bdfe7ee3ad0b99c9801d58807a9dbc9e09196365b0203853b99889ab3c87"
"checksum cc 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "2b4911e4bdcb4100c7680e7e854ff38e23f1b34d4d9e079efae3da2801341ffc"
-"checksum cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c819a1287eb618df47cc647173c5c4c66ba19d888a6e50d605672aed3140de"
"checksum chrono 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "ba5f60682a4c264e7f8d77b82e7788938a76befdf949d4a98026d19099c9d873"
"checksum clear_on_drop 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "97276801e127ffb46b66ce23f35cc96bd454fa311294bced4bbace7baa8b1d17"
"checksum cmac 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "44f175b5f76aa82ebe4c7e85ef95b23e9293c5618db28461cb10ee929e0f6e2f"
"checksum constant_time_eq 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8ff012e225ce166d4422e0e78419d901719760f62ae2b7969ca6b564d1b54a9e"
-"checksum crossbeam-deque 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f739f8c5363aca78cfb059edf753d8f0d36908c348f3d8d1503f03d8b75d9cf3"
-"checksum crossbeam-epoch 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "927121f5407de9956180ff5e936fe3cf4324279280001cd56b669d28ee7e9150"
-"checksum crossbeam-utils 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2760899e32a1d58d5abb31129f8fae5de75220bc2176e77ff7c627ae45c918d9"
"checksum crypto-mac 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "0999b4ff4d3446d4ddb19a63e9e00c1876e75cd7000d20e57a693b4b3f08d958"
"checksum crypto-mac 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "99376574a55849855052aa6e3b15f3bdebf8bcdd3b24f3cbc3371469bcd5b480"
"checksum dbl 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "920e117b69060a961c4164ccf83af573292cb167ccdd918950bcf0f5afc32c1c"
@@ -538,15 +417,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb"
"checksum generic-array 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ef25c5683767570c2bbd7deba372926a55eaae9982d7726ee2a1050239d45b9d"
"checksum itoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c069bbec61e1ca5a596166e55dfe4773ff745c3d16b700013bcaff9a6df2c682"
-"checksum lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "76f033c7ad61445c5b347c7382dd1237847eb1bce590fe50365dcb33d546be73"
-"checksum lazy_static 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c8f31047daa365f19be14b47c29df4f7c3b581832407daabe6ae77397619237d"
"checksum libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)" = "6fd41f331ac7c5b8ac259b8bf82c75c0fb2e469bbf37d2becbba9a6a2221965b"
-"checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3"
"checksum miscreant 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "345b52b06ce7a0e2fab0a0ea99ef52e81d63102ba0425b2914f1867b9d820628"
-"checksum nodrop 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "9a2228dca57108069a5262f2ed8bd2e82496d2e074a06d1ccc7ce1687b6ae0a2"
"checksum num-integer 0.1.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f8d26da319fb45674985c78f1d1caf99aa4941f785d384a2ae36d0740bc3e2fe"
"checksum num-traits 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dee092fcdf725aee04dd7da1d21debff559237d49ef1cb3e69bcb8ece44c7364"
-"checksum num_cpus 1.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "c51a3322e4bca9d212ad9a158a02abc6934d005490c054a2778df73a70aa0a30"
"checksum opaque-debug 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d620c9c26834b34f039489ac0dfdb12c7ac15ccaf818350a64c9b5334a452ad7"
"checksum ordermap 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "a86ed3f5f244b372d6b1a00b72ef7f8876d0bc6a78a4c9985c53614041512063"
"checksum pmac 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0a82cc12454dc99354a9342c237149aec041ef16f618066d0a682df256b97714"
@@ -555,14 +429,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum quote 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "1eca14c727ad12702eb4b6bfb5a232287dcf8385cb8ca83a3eeaf6519c44c408"
"checksum rand 0.3.22 (registry+https://github.com/rust-lang/crates.io-index)" = "15a732abf9d20f0ad8eeb6f909bf6868722d9a06e1e50802b6a70351f40b4eb1"
"checksum rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eba5f8cb59cc50ed56be8880a5c7b496bfd9bd26394e176bc67884094145c2c5"
-"checksum rayon 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b614fe08b6665cb9a231d07ac1364b0ef3cb3698f1239ee0c4c3a88a524f54c8"
-"checksum rayon-core 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "9d24ad214285a7729b174ed6d3bcfcb80177807f959d95fafd5bfc5c4f201ac8"
"checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd"
-"checksum ring 0.12.1 (registry+https://github.com/rust-lang/crates.io-index)" = "6f7d28b30a72c01b458428e0ae988d4149c20d902346902be881e3edc4bb325c"
"checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a"
"checksum rustc-serialize 0.3.24 (registry+https://github.com/rust-lang/crates.io-index)" = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda"
"checksum safemem 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e27a8b19b835f7aea908818e871f5cc3a5a186550c30773be987e155e8163d8f"
-"checksum scopeguard 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "94258f53601af11e6a49f722422f6e3425c52b06245a5cf9bc09908b174f5e27"
"checksum serde 1.0.36 (registry+https://github.com/rust-lang/crates.io-index)" = "c70142ae874a42c70e03c63c6a49abe2ea0079b090bf6e136e99252fc1974bd6"
"checksum serde_derive 1.0.36 (registry+https://github.com/rust-lang/crates.io-index)" = "6fffe22d41dbddcead5b2c380c4714d44f2eb39292f7e7a0d966d2d45bf56408"
"checksum serde_derive_internals 0.22.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d2f04ed291686ce195a5c8f554aaf36e50a721fbf829ee3b6151e6f85eccf945"
@@ -572,7 +442,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "a15375f1df02096fb3317256ce2cee6a1f42fc84ea5ad5fc8c421cfe40c73098"
"checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
-"checksum untrusted 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f392d7819dbe58833e26872f5f6f0d68b7bbbe90fc3667e98731c4a15ad9a7ae"
"checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3"
"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
diff --git a/Cargo.toml b/Cargo.toml
index abfd39d..af3c30c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -17,7 +17,3 @@ base64 = "0.8.0"
blake2 = "0.7"
pwhash = "0.1"
rand = "0.3.0"
-
-# Deprecated crates -- remove with time --
-aesni = "0.2.1"
-ring = "0.12.1"
diff --git a/src/main.rs b/src/main.rs
index 2e7cf75..30f0f72 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,73 +8,69 @@ extern crate serde_json;
#[macro_use]
extern crate serde_derive;
extern crate base64;
-extern crate rand;
extern crate blake2;
extern crate miscreant;
-
+extern crate rand;
pub mod record;
-pub mod vault;
mod security;
mod test;
+pub mod vault;
-use vault::*;
-use record::Payload::Text;
-
+use security::{crypto::CryptoEngine, keys::Key};
+use record::Record;
fn main() {
+ let rec = Record::new("name", "category");
+ println!("{:?}", rec);
- let rec = record::Record::new("name", "category");
-
- let encrypted = security::crypto::encrypt(&rec);
- println!("Encrypted: {}", encrypted);
-
- // security::bla();
+ println!("====================================");
+ let mut engine = CryptoEngine::new(Key::new());
+ let cipher = engine.encrypt(&rec).unwrap();
+ println!("Encrypted: {}", cipher);
- // create_and_populate();
- // load();
-}
-
-
-fn load() {
-
- let vault = Vault::load(
- "Personal",
- "/home/spacekookie/Desktop",
- "my password is cheese",
- );
- println!("{:?}", vault.records);
+ println!("====================================");
+ let decrypted: Record = engine.decrypt(cipher).unwrap();
+ println!("{:?}", decrypted);
}
-fn create_and_populate() {
-
- /* Create a new vault at a path, name and primary password */
- let mut vault = match Vault::new(
- "Personal",
- "/home/spacekookie/Desktop",
- "my password is cheese",
- ) {
- Ok(s) => s,
- Err(e) => panic!("Somehow failed to create the vault because {:?}", e),
- };
-
- /* Add a record with some tags */
- vault.add_record("mastodon", "web", vec!["social", "network"]);
-
- /* Add a few data fields to the body */
- vault.add_data(
- "mastodon",
- "url",
- Text(String::from("https://mastodon.social")),
- );
- vault.add_data("mastodon", "user", Text(String::from("spacekookie")));
- vault.add_data(
- "mastodon",
- "password",
- Text(String::from("My password is molten cheese")),
- );
-
- /* Sync the changes to disk */
- vault.sync();
-} \ No newline at end of file
+// fn load() {
+// let vault = Vault::load(
+// "Personal",
+// "/home/spacekookie/Desktop",
+// "my password is cheese",
+// );
+// println!("{:?}", vault.records);
+// }
+
+// fn create_and_populate() {
+// /* Create a new vault at a path, name and primary password */
+// let mut vault = match Vault::new(
+// "Personal",
+// "/home/spacekookie/Desktop",
+// "my password is cheese",
+// ) {
+// Ok(s) => s,
+// Err(e) => panic!("Somehow failed to create the vault because {:?}", e),
+// };
+
+// /* Add a record with some tags */
+// vault.add_record("mastodon", "web", vec!["social", "network"]);
+
+// /* Add a few data fields to the body */
+// vault.add_data(
+// "mastodon",
+// "url",
+// Text(String::from("https://mastodon.social")),
+// );
+// vault.add_data("mastodon", "user", Text(String::from("spacekookie")));
+// vault.add_data(
+// "mastodon",
+// "password",
+// Text(String::from("My password is molten cheese")),
+// );
+
+// /* Sync the changes to disk */
+// vault.sync();
+// }
diff --git a/src/security/crypto.rs b/src/security/crypto.rs
index 6252395..da7ccdb 100644
--- a/src/security/crypto.rs
+++ b/src/security/crypto.rs
@@ -1,25 +1,71 @@
//!
-
-use miscreant::aead::{Aes128PmacSiv, Aes128Siv, Aes256PmacSiv, Aes256Siv, Algorithm};
-use security::{keys::Key, random, encoding};
-use serde::Serialize;
+use miscreant::aead::{Aes256Siv, Algorithm};
+use security::{encoding, random, keys::{Key, KEY_LENGTH}};
+use serde::{Serialize, de::DeserializeOwned};
use serde_json;
+use std::error::Error;
-struct Encryptor {
+/// The main encryption context
+pub struct CryptoEngine {
+ ctx: Aes256Siv,
+ key: Key,
+ iv: Vec<u8>,
+}
+/// Represents some packed data – includes nonce and blob
+#[derive(Serialize, Deserialize)]
+struct PackedData {
+ nonce: Vec<u8>,
+ data: Vec<u8>,
}
+impl CryptoEngine {
+ /// Create a new encryption context with a key
+ pub fn new(key: Key) -> CryptoEngine {
+ return CryptoEngine {
+ ctx: Aes256Siv::new(&key.to_slice()),
+ key: key,
+ iv: random::bytes(KEY_LENGTH),
+ };
+ }
+
+ /// Load an existing encryption context into scope
+ pub fn load(key: Key, iv: Vec<u8>) -> CryptoEngine {
+ return CryptoEngine {
+ ctx: Aes256Siv::new(&key.to_slice()),
+ key: key,
+ iv: iv,
+ };
+ }
+
+ /// Encrypt a piece of data, returns a packed and encoded string
+ pub fn encrypt<T: Serialize>(&mut self, data: &T) -> Result<String, Box<Error>> {
+ let serial = serde_json::to_string(&data)?;
+ let nonce = random::bytes(64);
+ let iv = &self.iv.as_slice();
+ let data = &serial.as_bytes();
+
+ let encrypted = self.ctx.seal(nonce.as_slice(), iv, data);
+ let packed = PackedData {
+ nonce: nonce,
+ data: encrypted,
+ };
+
+ let enc_packed = serde_json::to_string(&packed)?;
+ return Ok(encoding::base64_encode(&enc_packed.into_bytes()));
+ }
-pub fn encrypt<T: Serialize>(data: &T) -> String {
- let encoded: String = serde_json::to_string(&data).unwrap();
+ /// Decrypt a ciphertext string into a type object
+ pub fn decrypt<T: DeserializeOwned>(&mut self, cipher: String) -> Result<T, Box<Error>> {
+ let dec_packed = String::from_utf8(encoding::base64_decode(&cipher))?;
+ let p: PackedData = serde_json::from_str(&dec_packed)?;
- let key: Key = Key::new();
- let mut aes: Aes256Siv = Aes256Siv::new(&key.to_slice());
-
- let nonce = random::bytes(64);
- let ad = random::bytes(64);
+ let iv = &self.iv.as_slice();
+ let decrypted = self.ctx.open(p.nonce.as_slice(), iv, p.data.as_slice())?;
+ let decr_str = String::from_utf8(decrypted)?;
- let encrypted = aes.seal(nonce.as_slice(), ad.as_slice(), encoded.as_bytes());
- return encoding::base64_encode(&encrypted);
+ let t: T = serde_json::from_str(&decr_str)?;
+ return Ok(t);
+ }
}
diff --git a/src/security/random.rs b/src/security/random.rs
index b98c233..eb88d99 100644
--- a/src/security/random.rs
+++ b/src/security/random.rs
@@ -15,6 +15,7 @@ pub fn number(bound: u64) -> u64 {
/// as a vector.
///
/// Can at most allocate 2048 bytes at a time
+/// FIXME: That shouldn't have a limit!
pub fn bytes(length: usize) -> Vec<u8> {
let mut vec: Vec<u8> = Vec::new();