partial progress on aes integration

need to retrench and update AES crate to support some APIs
that allow for CBC modes
This commit is contained in:
bunnie 2022-05-29 20:35:11 +08:00
parent cbb5462895
commit e84e378ef2
4 changed files with 89 additions and 27 deletions

66
Cargo.lock generated
View file

@ -30,7 +30,7 @@ dependencies = [
name = "aes" name = "aes"
version = "0.7.5" version = "0.7.5"
dependencies = [ dependencies = [
"cipher", "cipher 0.3.0",
"ctr 0.7.0", "ctr 0.7.0",
"hex-literal", "hex-literal",
"log", "log",
@ -48,7 +48,7 @@ checksum = "589c637f0e68c877bbd59a4599bbe849cac8e5f3e4b5a3ebae8f528cd218dcdc"
dependencies = [ dependencies = [
"aead", "aead",
"aes", "aes",
"cipher", "cipher 0.3.0",
"ctr 0.8.0", "ctr 0.8.0",
"polyval", "polyval",
"subtle", "subtle",
@ -242,6 +242,15 @@ dependencies = [
"generic-array", "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]] [[package]]
name = "blowfish" name = "blowfish"
version = "0.8.0" version = "0.8.0"
@ -249,7 +258,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe3ff3fc1de48c1ac2e3341c4df38b0d1bfb8fdf04632a187c8b75aaa319a7ab" checksum = "fe3ff3fc1de48c1ac2e3341c4df38b0d1bfb8fdf04632a187c8b75aaa319a7ab"
dependencies = [ dependencies = [
"byteorder", "byteorder",
"cipher", "cipher 0.3.0",
"opaque-debug", "opaque-debug",
] ]
@ -313,6 +322,15 @@ dependencies = [
"rustc_version 0.4.0", "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]] [[package]]
name = "cbor" name = "cbor"
version = "0.1.0" version = "0.1.0"
@ -369,6 +387,16 @@ dependencies = [
"generic-array", "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]] [[package]]
name = "clap" name = "clap"
version = "2.34.0" version = "2.34.0"
@ -636,6 +664,16 @@ dependencies = [
"zeroize", "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]] [[package]]
name = "crypto-mac" name = "crypto-mac"
version = "0.11.1" version = "0.11.1"
@ -672,7 +710,9 @@ dependencies = [
name = "ctap-crypto" name = "ctap-crypto"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"aes",
"arrayref", "arrayref",
"cbc",
"cbor", "cbor",
"p256", "p256",
"rand_core 0.5.1", "rand_core 0.5.1",
@ -687,7 +727,7 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a232f92a03f37dd7d7dd2adc67166c77e9cd88de5b019b9a9eecfaeaf7bfd481" checksum = "a232f92a03f37dd7d7dd2adc67166c77e9cd88de5b019b9a9eecfaeaf7bfd481"
dependencies = [ dependencies = [
"cipher", "cipher 0.3.0",
] ]
[[package]] [[package]]
@ -696,7 +736,7 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "049bb91fb4aaf0e3c7efa6cd5ef877dbbbd15b39dad06d9948de4ec8a75761ea" checksum = "049bb91fb4aaf0e3c7efa6cd5ef877dbbbd15b39dad06d9948de4ec8a75761ea"
dependencies = [ dependencies = [
"cipher", "cipher 0.3.0",
] ]
[[package]] [[package]]
@ -1581,6 +1621,16 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" 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]] [[package]]
name = "instant" name = "instant"
version = "0.1.12" version = "0.1.12"
@ -2217,7 +2267,7 @@ dependencies = [
"bitfield", "bitfield",
"bitflags", "bitflags",
"blowfish", "blowfish",
"cipher", "cipher 0.3.0",
"digest", "digest",
"gam", "gam",
"hkdf", "hkdf",
@ -2680,7 +2730,7 @@ dependencies = [
"aes", "aes",
"blowfish", "blowfish",
"byteorder", "byteorder",
"cipher", "cipher 0.3.0",
"com", "com",
"curve25519-dalek", "curve25519-dalek",
"digest", "digest",
@ -2972,7 +3022,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"aes", "aes",
"chrono", "chrono",
"cipher", "cipher 0.3.0",
"codec", "codec",
"com", "com",
"com_rs-ref", "com_rs-ref",

View file

@ -17,3 +17,5 @@ trng = {path = "../../../../services/trng"}
xous-names = {path = "../../../../services/xous-names"} xous-names = {path = "../../../../services/xous-names"}
rand_core = "0.5.1" rand_core = "0.5.1"
p256 = {version = "0.10.1", default-features = false, features = ["ecdsa", "ecdh", "std"]} p256 = {version = "0.10.1", default-features = false, features = ["ecdsa", "ecdh", "std"]}
cbc = "0.1.2"
aes = {path="../../../../services/aes"}

View file

@ -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 // Copyright 2019 Google LLC
// //
// Licensed under the Apache License, Version 2.0 (the "License"); // Licensed under the Apache License, Version 2.0 (the "License");
@ -12,30 +27,23 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
use super::util::{xor_block_16, Block16}; use aes::cipher::{BlockDecryptMut, BlockEncryptMut, KeyIvInit};
use super::{Decrypt16BytesBlock, Encrypt16BytesBlock};
pub fn cbc_encrypt<K>(key: &K, mut iv: Block16, blocks: &mut [Block16]) type Aes256CbcEnc = cbc::Encryptor<aes::Aes256>;
where type Aes256CbcDec = cbc::Decryptor<aes::Aes256>;
K: Encrypt16BytesBlock,
use super::util::Block16;
pub fn cbc_encrypt(key: &[u8; 32], mut iv: Block16, blocks: &mut [Block16])
{ {
for block in blocks { Aes256CbcEnc::new(&key.into(), &iv.into())
xor_block_16(block, &iv); .encrypt_blocks_mut(blocks);
key.encrypt_block(block);
iv = *block;
}
} }
pub fn cbc_decrypt<K>(key: &K, mut iv: Block16, blocks: &mut [Block16]) pub fn cbc_decrypt(key: &[u8; 32], mut iv: Block16, blocks: &mut [Block16])
where
K: Decrypt16BytesBlock,
{ {
for block in blocks { Aes256CbcDec::new(&key.into(), &iv.into())
let tmp = *block; .decrypt_blocks_mut(blocks);
key.decrypt_block(block);
xor_block_16(block, &iv);
iv = tmp;
}
} }
#[cfg(test)] #[cfg(test)]

View file

@ -13,5 +13,7 @@
// limitations under the License. // limitations under the License.
pub mod rng256; pub mod rng256;
pub mod cbc;
mod util;
//pub mod ecdh; //pub mod ecdh;
//pub mod ecdsa; //pub mod ecdsa;