Skip to content

Commit

Permalink
Crypto: fix aes256ctr decryption and test
Browse files Browse the repository at this point in the history
The buf of decrypted text should be set the same
length as the ciphertext. This commit also changed
the feature `rust-crypto` and `openssl` to be
compatitable .

When both of them are enabled, uses `openssl`.

Signed-off-by: Xynnn007 <[email protected]>
  • Loading branch information
Xynnn007 authored and jialez0 committed Feb 13, 2023
1 parent 3b30b96 commit b84d037
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 28 deletions.
12 changes: 4 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ anyhow = "1.0"
async-trait = "0.1.56"
base64 = "0.13.0"
bincode = { version = "1.3.3", optional = true }
ctr = "0.9.2"
ctr = { version = "0.9.2", optional = true }
foreign-types = { version = "0.5.0", optional = true }
kbs-types = { git = "https://github.com/virtee/kbs-types" }
log = "0.4.14"
Expand All @@ -38,7 +38,7 @@ tokio = { version = "1.20.1", features = ["macros", "rt-multi-thread"] }
tonic-build = { version = "0.8.0", optional = true }

[features]
default = ["sample_kbc"]
default = ["sample_kbc", "rust-crypto"]
sample_kbc = []
cc_kbc = ["rand", "rsa", "sha2", "tdx-attest-rs", "reqwest"]
eaa_kbc = ["foreign-types"]
Expand All @@ -48,9 +48,5 @@ online_sev_kbc = ["tonic", "prost", "uuid", "bincode", "tokio"]
gen-proto = ["tonic-build"]

# Either `rust-crypto` or `openssl` should be enabled to work as underlying crypto module
rust-crypto = ["dep:aes-gcm", "crypto"]
openssl = ["dep:openssl", "crypto"]

crypto = []

crypto-compatibility-test = [ "dep:aes-gcm", "openssl" ]
rust-crypto = ["dep:aes-gcm", "ctr"]
openssl = ["dep:openssl"]
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@ RUSTFLAGS_ARGS ?=
OPENSSL ?=

ifdef KBC
feature := --no-default-features --features
FEATURES := $(KBC)
else
feature := --features
FEATURES := default
FEATURES := sample_kbc
endif

ifeq ($(LIBC), musl)
Expand Down Expand Up @@ -104,7 +102,7 @@ else
endif

build:
cd app && $(RUST_FLAGS) cargo build $(release) $(feature) $(FEATURES) $(LIBC_FLAG)
cd app && $(RUST_FLAGS) cargo build $(release) --no-default-features --features $(FEATURES) $(LIBC_FLAG)

TARGET := app/$(TARGET_DIR)/$(BIN_NAME)

Expand Down
44 changes: 35 additions & 9 deletions src/common/crypto/aes256ctr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
use anyhow::*;

#[cfg(feature = "rust-crypto")]
use aes_gcm::aes::Aes256;
#[cfg(feature = "rust-crypto")]
use ctr::{
cipher::{KeyIvInit, StreamCipher},
Ctr128BE,
};

#[cfg(feature = "rust-crypto")]
#[cfg(all(feature = "rust-crypto", not(feature = "openssl")))]
pub fn decrypt(encrypted_data: &[u8], key: &[u8], iv: &[u8]) -> Result<Vec<u8>> {
use aes_gcm::aes::Aes256;
use ctr::{
cipher::{KeyIvInit, StreamCipher},
Ctr128BE,
};

let mut decryptor = Ctr128BE::<Aes256>::new(key.into(), iv.into());
let mut buf = Vec::new();
buf.resize(encrypted_data.len(), b' ');
decryptor
.apply_keystream_b2b(encrypted_data, &mut buf)
.map_err(|e| anyhow!("aes-256-ctr decrypt failed: {:?}", e))?;
Expand All @@ -35,3 +34,30 @@ pub fn decrypt(encrypted_data: &[u8], key: &[u8], iv: &[u8]) -> Result<Vec<u8>>
openssl::symm::decrypt(cipher, key, Some(iv), encrypted_data)
.map_err(|e| anyhow!(e.to_string()))
}

#[cfg(all(feature = "rust-crypto", feature = "openssl"))]
#[cfg(test)]
mod tests {
use ctr::{
cipher::{KeyIvInit, StreamCipher},
Ctr128BE,
};

#[test]
fn compatible_with_openssl() {
use aes_gcm::aes::Aes256;

let plaintext = b"plaintext message";
let key = [0x42; 32];
let iv = [0x24; 16];
let mut cipher = Ctr128BE::<Aes256>::new(&key.into(), &iv.into());
let mut cipher_text = Vec::new();
cipher_text.resize(plaintext.len(), b' ');
cipher
.apply_keystream_b2b(plaintext, &mut cipher_text)
.expect("encryption failed");

let decrypted = super::decrypt(&cipher_text, &key, &iv).expect("decrypt failed");
assert_eq!(decrypted, plaintext);
}
}
7 changes: 3 additions & 4 deletions src/common/crypto/aes256gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use anyhow::*;

#[cfg(feature = "rust-crypto")]
#[cfg(all(feature = "rust-crypto", not(feature = "openssl")))]
use aes_gcm::{aead::Aead, Aes256Gcm, Key, KeyInit, Nonce};

#[cfg(feature = "rust-crypto")]
#[cfg(all(feature = "rust-crypto", not(feature = "openssl")))]
pub fn decrypt(encrypted_data: &[u8], key: &[u8], iv: &[u8]) -> Result<Vec<u8>> {
let decrypting_key = Key::<Aes256Gcm>::from_slice(key);
let cipher = Aes256Gcm::new(decrypting_key);
Expand Down Expand Up @@ -40,15 +40,14 @@ pub fn decrypt(encrypted_data: &[u8], key: &[u8], iv: &[u8]) -> Result<Vec<u8>>
.map_err(|e| anyhow!(e.to_string()))
}

#[cfg(all(feature = "rust-crypto", feature = "openssl"))]
#[cfg(test)]
mod tests {
#[cfg(feature = "crypto-compatibility-test")]
use aes_gcm::{
aead::{Aead, OsRng},
Aes256Gcm, KeyInit, Nonce,
};

#[cfg(feature = "crypto-compatibility-test")]
#[test]
fn compatible_with_openssl() {
let plaintext = b"plaintext message";
Expand Down
3 changes: 0 additions & 3 deletions src/common/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ use zeroize::Zeroizing;
mod aes256ctr;
mod aes256gcm;

#[cfg(all(not(feature = "openssl"), not(feature = "rust-crypto")))]
compile_error!("One feature of `openssl` and ``rust-crypto` must be enabled.");

/// Supported WrapType, s.t. encryption algorithm using to encrypt the
/// [PLBCO](https://github.com/confidential-containers/attestation-agent/blob/main/docs/IMPLEMENTATION.md#encryption-and-decryption-of-container-image).
/// TODO: Support more kinds of en/decryption schemes.
Expand Down

0 comments on commit b84d037

Please sign in to comment.