From e84e378ef21dd418f068ce1d2caeec5300eb1dc8 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 29 May 2022 20:35:11 +0800 Subject: [PATCH] partial progress on aes integration need to retrench and update AES crate to support some APIs that allow for CBC modes --- Cargo.lock | 66 ++++++++++++++++++++++---- apps/vault/libraries/crypto/Cargo.toml | 2 + apps/vault/libraries/crypto/src/cbc.rs | 46 ++++++++++-------- apps/vault/libraries/crypto/src/lib.rs | 2 + 4 files changed, 89 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6a3e51584..d0b2ce0a4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,7 +30,7 @@ dependencies = [ name = "aes" version = "0.7.5" dependencies = [ - "cipher", + "cipher 0.3.0", "ctr 0.7.0", "hex-literal", "log", @@ -48,7 +48,7 @@ checksum = "589c637f0e68c877bbd59a4599bbe849cac8e5f3e4b5a3ebae8f528cd218dcdc" dependencies = [ "aead", "aes", - "cipher", + "cipher 0.3.0", "ctr 0.8.0", "polyval", "subtle", @@ -242,6 +242,15 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block-padding" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a90ec2df9600c28a01c56c4784c9207a96d2451833aeceb8cc97e4c9548bb78" +dependencies = [ + "generic-array", +] + [[package]] name = "blowfish" version = "0.8.0" @@ -249,7 +258,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fe3ff3fc1de48c1ac2e3341c4df38b0d1bfb8fdf04632a187c8b75aaa319a7ab" dependencies = [ "byteorder", - "cipher", + "cipher 0.3.0", "opaque-debug", ] @@ -313,6 +322,15 @@ dependencies = [ "rustc_version 0.4.0", ] +[[package]] +name = "cbc" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26b52a9543ae338f279b96b0b9fed9c8093744685043739079ce85cd58f289a6" +dependencies = [ + "cipher 0.4.3", +] + [[package]] name = "cbor" version = "0.1.0" @@ -369,6 +387,16 @@ dependencies = [ "generic-array", ] +[[package]] +name = "cipher" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1873270f8f7942c191139cb8a40fd228da6c3fd2fc376d7e92d47aa14aeb59e" +dependencies = [ + "crypto-common", + "inout", +] + [[package]] name = "clap" version = "2.34.0" @@ -636,6 +664,16 @@ dependencies = [ "zeroize", ] +[[package]] +name = "crypto-common" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57952ca27b5e3606ff4dd79b0020231aaf9d6aa76dc05fd30137538c50bd3ce8" +dependencies = [ + "generic-array", + "typenum", +] + [[package]] name = "crypto-mac" version = "0.11.1" @@ -672,7 +710,9 @@ dependencies = [ name = "ctap-crypto" version = "0.1.0" dependencies = [ + "aes", "arrayref", + "cbc", "cbor", "p256", "rand_core 0.5.1", @@ -687,7 +727,7 @@ version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a232f92a03f37dd7d7dd2adc67166c77e9cd88de5b019b9a9eecfaeaf7bfd481" dependencies = [ - "cipher", + "cipher 0.3.0", ] [[package]] @@ -696,7 +736,7 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "049bb91fb4aaf0e3c7efa6cd5ef877dbbbd15b39dad06d9948de4ec8a75761ea" dependencies = [ - "cipher", + "cipher 0.3.0", ] [[package]] @@ -1581,6 +1621,16 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" +[[package]] +name = "inout" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0c10553d664a4d0bcff9f4215d0aac67a639cc68ef660840afe309b807bc9f5" +dependencies = [ + "block-padding", + "generic-array", +] + [[package]] name = "instant" version = "0.1.12" @@ -2217,7 +2267,7 @@ dependencies = [ "bitfield", "bitflags", "blowfish", - "cipher", + "cipher 0.3.0", "digest", "gam", "hkdf", @@ -2680,7 +2730,7 @@ dependencies = [ "aes", "blowfish", "byteorder", - "cipher", + "cipher 0.3.0", "com", "curve25519-dalek", "digest", @@ -2972,7 +3022,7 @@ version = "0.1.0" dependencies = [ "aes", "chrono", - "cipher", + "cipher 0.3.0", "codec", "com", "com_rs-ref", diff --git a/apps/vault/libraries/crypto/Cargo.toml b/apps/vault/libraries/crypto/Cargo.toml index 34dd554be..1ff201406 100644 --- a/apps/vault/libraries/crypto/Cargo.toml +++ b/apps/vault/libraries/crypto/Cargo.toml @@ -17,3 +17,5 @@ trng = {path = "../../../../services/trng"} xous-names = {path = "../../../../services/xous-names"} rand_core = "0.5.1" p256 = {version = "0.10.1", default-features = false, features = ["ecdsa", "ecdh", "std"]} +cbc = "0.1.2" +aes = {path="../../../../services/aes"} \ No newline at end of file diff --git a/apps/vault/libraries/crypto/src/cbc.rs b/apps/vault/libraries/crypto/src/cbc.rs index fd7dcc2e8..25d863bfd 100644 --- a/apps/vault/libraries/crypto/src/cbc.rs +++ b/apps/vault/libraries/crypto/src/cbc.rs @@ -1,3 +1,18 @@ +// Xous maintainer's note: +// +// This library is vendored in from the Google OpenSK reference implementation. +// The OpenSK library contains its own implementations of crypto functions. +// The port to Xous attempts to undo that, but where possible leaves a thin +// adapter between the OpenSK custom APIs and the more "standard" Rustcrypto APIs. +// There is always a hazard in adapting crypto APIs and reviewers should take +// note of this. However, by calling out the API differences, it hopefully highlights +// any potential problems in the OpenSK library, rather than papering them over. +// +// Leaving the OpenSK APIs in place also makes it easier to apply upstream +// patches from OpenSK to fix bugs in the code base. + +// Original copyright notice preserved below: + // Copyright 2019 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -12,30 +27,23 @@ // See the License for the specific language governing permissions and // limitations under the License. -use super::util::{xor_block_16, Block16}; -use super::{Decrypt16BytesBlock, Encrypt16BytesBlock}; +use aes::cipher::{BlockDecryptMut, BlockEncryptMut, KeyIvInit}; -pub fn cbc_encrypt(key: &K, mut iv: Block16, blocks: &mut [Block16]) -where - K: Encrypt16BytesBlock, +type Aes256CbcEnc = cbc::Encryptor; +type Aes256CbcDec = cbc::Decryptor; + +use super::util::Block16; + +pub fn cbc_encrypt(key: &[u8; 32], mut iv: Block16, blocks: &mut [Block16]) { - for block in blocks { - xor_block_16(block, &iv); - key.encrypt_block(block); - iv = *block; - } + Aes256CbcEnc::new(&key.into(), &iv.into()) + .encrypt_blocks_mut(blocks); } -pub fn cbc_decrypt(key: &K, mut iv: Block16, blocks: &mut [Block16]) -where - K: Decrypt16BytesBlock, +pub fn cbc_decrypt(key: &[u8; 32], mut iv: Block16, blocks: &mut [Block16]) { - for block in blocks { - let tmp = *block; - key.decrypt_block(block); - xor_block_16(block, &iv); - iv = tmp; - } + Aes256CbcDec::new(&key.into(), &iv.into()) + .decrypt_blocks_mut(blocks); } #[cfg(test)] diff --git a/apps/vault/libraries/crypto/src/lib.rs b/apps/vault/libraries/crypto/src/lib.rs index 39d56eb3d..34b451e54 100644 --- a/apps/vault/libraries/crypto/src/lib.rs +++ b/apps/vault/libraries/crypto/src/lib.rs @@ -13,5 +13,7 @@ // limitations under the License. pub mod rng256; +pub mod cbc; +mod util; //pub mod ecdh; //pub mod ecdsa; \ No newline at end of file