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

chore: import 8/29 version #2311

Merged
merged 2 commits into from
Aug 29, 2024
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
126 changes: 96 additions & 30 deletions dc/s2n-quic-dc/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,55 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use crate::credentials::Credentials;
pub use bytes::buf::UninitSlice;
use core::fmt;
pub use s2n_quic_core::packet::KeyPhase;

pub mod awslc;
#[cfg(any(test, feature = "testing"))]
pub mod testing;

pub mod encrypt {
pub mod seal {
use super::*;

pub trait Key {
fn credentials(&self) -> &Credentials;

pub trait Application {
fn key_phase(&self) -> KeyPhase;

fn tag_len(&self) -> usize;

/// Encrypt a payload
fn encrypt<N: IntoNonce>(
fn encrypt(
&self,
nonce: N,
packet_number: u64,
header: &[u8],
extra_payload: Option<&[u8]>,
payload_and_tag: &mut [u8],
);
}

fn retransmission_tag(
&self,
original_packet_number: u64,
retransmission_packet_number: u64,
tag_out: &mut [u8],
);
pub trait Control {
fn tag_len(&self) -> usize;

fn sign(&self, header: &[u8], tag: &mut [u8]);
}

pub mod control {
use super::*;

/// Marker trait for keys to be used with stream control packets
pub trait Stream: Control {
fn retransmission_tag(
&self,
original_packet_number: u64,
retransmission_packet_number: u64,
tag_out: &mut [u8],
);
}

/// Marker trait for keys to be used with secret control packets
pub trait Secret: Control {}
}
}

pub mod decrypt {
pub mod open {
use super::*;

#[derive(PartialEq, Eq, Clone, Copy, Debug)]
Expand All @@ -47,6 +58,10 @@ pub mod decrypt {
ReplayPotentiallyDetected { gap: Option<u64> },
ReplayDefinitelyDetected,
InvalidTag,
SingleUseKey,
UnsupportedOperation,
MacOnly,
RotationNotSupported,
}

impl fmt::Display for Error {
Expand All @@ -61,6 +76,12 @@ pub mod decrypt {
write!(f, "key replay potentially detected: unknown gap")
}
Self::InvalidTag => "invalid tag".fmt(f),
Self::SingleUseKey => "this key can only be used once".fmt(f),
Self::UnsupportedOperation => {
"this key cannot be used with the given operation".fmt(f)
}
Self::MacOnly => "this key is only capable of generating MACs".fmt(f),
Self::RotationNotSupported => "this key does not support key rotation".fmt(f),
}
}
}
Expand All @@ -69,38 +90,83 @@ pub mod decrypt {

pub type Result<T = (), E = Error> = core::result::Result<T, E>;

pub trait Key {
fn credentials(&self) -> &Credentials;

pub trait Application {
fn tag_len(&self) -> usize;

/// Decrypt a payload
fn decrypt<N: IntoNonce>(
fn decrypt(
&self,
key_phase: KeyPhase,
nonce: N,
packet_number: u64,
header: &[u8],
payload_in: &[u8],
tag: &[u8],
payload_out: &mut UninitSlice,
) -> Result;

/// Decrypt a payload
fn decrypt_in_place<N: IntoNonce>(
fn decrypt_in_place(
&self,
key_phase: KeyPhase,
nonce: N,
packet_number: u64,
header: &[u8],
payload_and_tag: &mut [u8],
) -> Result;
}

fn retransmission_tag(
&self,
key_phase: KeyPhase,
original_packet_number: u64,
retransmission_packet_number: u64,
tag_out: &mut [u8],
);
pub trait Control {
fn tag_len(&self) -> usize;

fn verify(&self, header: &[u8], tag: &[u8]) -> Result;
}

pub mod control {
use super::*;

/// Marker trait for keys to be used with stream control packets
pub trait Stream: Control {
fn retransmission_tag(
&self,
original_packet_number: u64,
retransmission_packet_number: u64,
tag_out: &mut [u8],
) -> Result;
}

pub mod stream {
/// A no-op implementation for reliable transports
#[derive(Clone, Default)]
pub struct Reliable(());

impl super::Control for Reliable {
#[inline]
fn tag_len(&self) -> usize {
16
}

#[inline]
fn verify(&self, _header: &[u8], _tag: &[u8]) -> super::Result {
// this method should not be used on reliable transports
Err(super::Error::UnsupportedOperation)
}
}

impl super::Stream for Reliable {
#[inline]
fn retransmission_tag(
&self,
_original_packet_number: u64,
_retransmission_packet_number: u64,
_tag_out: &mut [u8],
) -> super::Result {
// this method should not be used on reliable transports
Err(super::Error::UnsupportedOperation)
}
}
}

/// Marker trait for keys to be used with secret control packets
pub trait Secret: Control {}
}
}

Expand Down
Loading
Loading