Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto/boringssl: don't overwrite mask #1925

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ COPY quiche/ ./quiche/

RUN apt-get update && apt-get install -y cmake && rm -rf /var/lib/apt/lists/*

RUN cargo build --manifest-path apps/Cargo.toml
RUN cargo build --release --manifest-path apps/Cargo.toml

##
## quiche-base: quiche image for apps
Expand All @@ -20,8 +20,8 @@ RUN apt-get update && apt-get install -y ca-certificates && \
rm -rf /var/lib/apt/lists/*

COPY --from=build \
/build/apps/target/debug/quiche-client \
/build/apps/target/debug/quiche-server \
/build/apps/target/release/quiche-client \
/build/apps/target/release/quiche-server \
/usr/local/bin/

ENV PATH="/usr/local/bin/:${PATH}"
Expand All @@ -39,8 +39,8 @@ WORKDIR /quiche
RUN apt-get update && apt-get install -y wait-for-it && rm -rf /var/lib/apt/lists/*

COPY --from=build \
/build/apps/target/debug/quiche-client \
/build/apps/target/debug/quiche-server \
/build/apps/target/release/quiche-client \
/build/apps/target/release/quiche-server \
/build/apps/run_endpoint.sh \
./

Expand Down
62 changes: 40 additions & 22 deletions quiche/src/crypto/boringssl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::*;

use std::convert::TryFrom;

use std::mem::MaybeUninit;

use libc::c_int;
Expand Down Expand Up @@ -195,38 +197,54 @@ impl HeaderProtectionKey {
}
}

pub fn new_mask(&self, sample: &[u8]) -> Result<[u8; 5]> {
let mut new_mask = [0_u8; 5];

pub fn new_mask(&self, sample: &[u8]) -> Result<HeaderProtectionMask> {
match self {
Self::Aes(aes_key) => unsafe {
AES_ecb_encrypt(
sample.as_ptr(),
new_mask.as_mut_ptr(),
aes_key as _,
1,
);
Self::Aes(aes_key) => {
let mut block = [0_u8; 16];

unsafe {
AES_ecb_encrypt(
sample.as_ptr(),
block.as_mut_ptr(),
aes_key as _,
1,
)
};

// Downsize the encrypted block to the size of the header
// protection mask.
//
// The length of the slice will always match the size of
// `HeaderProtectionMask` so the `unwrap()` is safe.
let new_mask =
HeaderProtectionMask::try_from(&block[..HP_MASK_LEN])
.unwrap();
Ok(new_mask)
},

Self::ChaCha(key) => unsafe {
const PLAINTEXT: &[u8; 5] = &[0_u8; 5];
Self::ChaCha(key) => {
const PLAINTEXT: &[u8; HP_MASK_LEN] = &[0_u8; HP_MASK_LEN];

let mut new_mask = HeaderProtectionMask::default();

let counter = u32::from_le_bytes([
sample[0], sample[1], sample[2], sample[3],
]);

CRYPTO_chacha_20(
new_mask.as_mut_ptr(),
PLAINTEXT.as_ptr(),
PLAINTEXT.len(),
key.as_ptr(),
sample[std::mem::size_of::<u32>()..].as_ptr(),
counter,
);
unsafe {
CRYPTO_chacha_20(
new_mask.as_mut_ptr(),
PLAINTEXT.as_ptr(),
PLAINTEXT.len(),
key.as_ptr(),
sample[std::mem::size_of::<u32>()..].as_ptr(),
counter,
);
};

Ok(new_mask)
},
}

Ok(new_mask)
}
}

Expand Down
5 changes: 5 additions & 0 deletions quiche/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ use crate::packet;
// All the AEAD algorithms we support use 96-bit nonces.
pub const MAX_NONCE_LEN: usize = 12;

// Length of header protection mask.
pub const HP_MASK_LEN: usize = 5;

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Level {
Expand Down Expand Up @@ -120,6 +123,8 @@ struct EVP_MD {
_unused: c_void,
}

type HeaderProtectionMask = [u8; HP_MASK_LEN];

pub struct Open {
alg: Algorithm,

Expand Down
4 changes: 2 additions & 2 deletions quiche/src/crypto/openssl_quictls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ impl HeaderProtectionKey {
})
}

pub fn new_mask(&self, sample: &[u8]) -> Result<[u8; 5]> {
pub fn new_mask(&self, sample: &[u8]) -> Result<HeaderProtectionMask> {
const PLAINTEXT: &[u8; 5] = &[0_u8; 5];

let mut new_mask = [0_u8; 5];
let mut new_mask = HeaderProtectionMask::default();

// Set IV (i.e. the sample).
let rc = unsafe {
Expand Down
Loading