Skip to content
Closed
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
14 changes: 7 additions & 7 deletions ohttp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ stream = ["dep:futures", "dep:pin-project"]
unsafe-print-secrets = []

[dependencies]
aead = {version = "0.5", optional = true, features = ["std"]}
aes-gcm = {version = "0.10", optional = true}
aead = {version = "0.6", optional = true, features = ["alloc"]}
aes-gcm = {version = "0.11", optional = true}
byteorder = "1.4"
chacha20poly1305 = {version = "0.10", optional = true}
chacha20poly1305 = {version = "0.11", optional = true}
futures = {version = "0.3", optional = true}
hex = "0.4"
hkdf = {version = "0.12", optional = true}
hpke = {version = "0.13", optional = true, default-features = false, features = ["std", "x25519", "p256"]}
hkdf = {version = "0.13", optional = true}
hpke = {version = "0.14", optional = true, default-features = false, features = ["getrandom", "x25519", "nistp", "aes", "chacha"]}
log = {version = "0.4", default-features = false}
pin-project = {version = "1.1", optional = true}
rand = {version = "0.9", optional = true}
rand = {version = "0.10", optional = true}
regex = {version = "~1.11", optional = true}
sha2 = {version = "0.10", optional = true}
sha2 = {version = "0.11", optional = true}
thiserror = "2"

[build-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion ohttp/src/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use ::rand::{rng, RngCore};
use ::rand::{rng, Rng};

#[must_use]
pub fn random(size: usize) -> Vec<u8> {
Expand Down
32 changes: 14 additions & 18 deletions ohttp/src/rh/aead.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::convert::TryFrom;

use aead::{AeadMut, Key, KeyInit, Nonce, Payload};
use aead::{Aead as _, KeyInit, Payload};
use aes_gcm::{Aes128Gcm, Aes256Gcm};
use chacha20poly1305::ChaCha20Poly1305;

Expand Down Expand Up @@ -34,21 +34,17 @@ enum AeadEngine {
impl AeadEngine {
fn encrypt(&mut self, nonce: &[u8], pt: Payload) -> Res<Vec<u8>> {
let tag = match self {
Self::Aes128Gcm(e) => e.encrypt(Nonce::<Aes128Gcm>::from_slice(nonce), pt)?,
Self::Aes256Gcm(e) => e.encrypt(Nonce::<Aes256Gcm>::from_slice(nonce), pt)?,
Self::ChaCha20Poly1305(e) => {
e.encrypt(Nonce::<ChaCha20Poly1305>::from_slice(nonce), pt)?
}
Self::Aes128Gcm(e) => e.encrypt(&nonce.try_into().unwrap(), pt)?,
Self::Aes256Gcm(e) => e.encrypt(&nonce.try_into().unwrap(), pt)?,
Self::ChaCha20Poly1305(e) => e.encrypt(&nonce.try_into().unwrap(), pt)?,
};
Ok(tag)
}
fn decrypt(&mut self, nonce: &[u8], pt: Payload) -> Res<Vec<u8>> {
let tag = match self {
Self::Aes128Gcm(e) => e.decrypt(Nonce::<Aes128Gcm>::from_slice(nonce), pt)?,
Self::Aes256Gcm(e) => e.decrypt(Nonce::<Aes256Gcm>::from_slice(nonce), pt)?,
Self::ChaCha20Poly1305(e) => {
e.decrypt(Nonce::<ChaCha20Poly1305>::from_slice(nonce), pt)?
}
Self::Aes128Gcm(e) => e.decrypt(&nonce.try_into().unwrap(), pt)?,
Self::Aes256Gcm(e) => e.decrypt(&nonce.try_into().unwrap(), pt)?,
Self::ChaCha20Poly1305(e) => e.decrypt(&nonce.try_into().unwrap(), pt)?,
};
Ok(tag)
}
Expand All @@ -73,14 +69,14 @@ impl Aead {
nonce_base: [u8; NONCE_LEN],
) -> Res<Self> {
let aead = match algorithm {
AeadId::Aes128Gcm => AeadEngine::Aes128Gcm(Box::new(Aes128Gcm::new(
Key::<Aes128Gcm>::from_slice(key.as_ref()),
))),
AeadId::Aes256Gcm => AeadEngine::Aes256Gcm(Box::new(Aes256Gcm::new(
Key::<Aes256Gcm>::from_slice(key.as_ref()),
))),
AeadId::Aes128Gcm => {
AeadEngine::Aes128Gcm(Box::new(Aes128Gcm::new(key.as_ref().try_into().unwrap())))
}
AeadId::Aes256Gcm => {
AeadEngine::Aes256Gcm(Box::new(Aes256Gcm::new(key.as_ref().try_into().unwrap())))
}
AeadId::ChaCha20Poly1305 => AeadEngine::ChaCha20Poly1305(Box::new(
ChaCha20Poly1305::new(Key::<ChaCha20Poly1305>::from_slice(key.as_ref())),
ChaCha20Poly1305::new(key.as_ref().try_into().unwrap()),
)),
};
Ok(Self {
Expand Down
31 changes: 13 additions & 18 deletions ohttp/src/rh/hpke.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::ops::Deref;

use ::hpke as rust_hpke;
use ::rand::rng;
use log::trace;
use rust_hpke::{
aead::{AeadCtxR, AeadCtxS, AeadTag, AesGcm128, ChaCha20Poly1305},
Expand Down Expand Up @@ -145,25 +144,25 @@ impl SenderContext {
Self::X25519HkdfSha256(SenderContextX25519HkdfSha256::HkdfSha256(
SenderContextX25519HkdfSha256HkdfSha256::AesGcm128(context),
)) => {
let tag = context.seal_in_place_detached(plaintext, aad)?;
let tag = context.seal_inout_detached(plaintext.into(), aad)?;
Vec::from(tag.to_bytes().as_slice())
}
Self::X25519HkdfSha256(SenderContextX25519HkdfSha256::HkdfSha256(
SenderContextX25519HkdfSha256HkdfSha256::ChaCha20Poly1305(context),
)) => {
let tag = context.seal_in_place_detached(plaintext, aad)?;
let tag = context.seal_inout_detached(plaintext.into(), aad)?;
Vec::from(tag.to_bytes().as_slice())
}
Self::P256HkdfSha256(SenderContextP256HkdfSha256::HkdfSha256(
SenderContextP256HkdfSha256HkdfSha256::AesGcm128(context),
)) => {
let tag = context.seal_in_place_detached(plaintext, aad)?;
let tag = context.seal_inout_detached(plaintext.into(), aad)?;
Vec::from(tag.to_bytes().as_slice())
}
Self::P256HkdfSha256(SenderContextP256HkdfSha256::HkdfSha256(
SenderContextP256HkdfSha256HkdfSha256::ChaCha20Poly1305(context),
)) => {
let tag = context.seal_in_place_detached(plaintext, aad)?;
let tag = context.seal_inout_detached(plaintext.into(), aad)?;
Vec::from(tag.to_bytes().as_slice())
}
})
Expand Down Expand Up @@ -210,11 +209,9 @@ pub struct HpkeS {
impl HpkeS {
/// Create a new context that uses the KEM mode for sending.
pub fn new(config: Config, pk_r: &PublicKey, info: &[u8]) -> Res<Self> {
let mut csprng = rng();

macro_rules! dispatch_hpkes_new {
{
($c:expr, $pk:expr, $csprng:expr): [$( $(#[$meta:meta])* {
($c:expr, $pk:expr): [$( $(#[$meta:meta])* {
$kemid:path => $kem:path,
$kdfid:path => $kdf:path,
$aeadid:path => $aead:path,
Expand All @@ -232,11 +229,10 @@ impl HpkeS {
},
$pke(pk_r),
) => {
let (enc, context) = setup_sender::<$aead, $kdf, $kem, _>(
let (enc, context) = setup_sender::<$aead, $kdf, $kem>(
&OpModeS::Base,
pk_r,
info,
$csprng,
)?;
(
$ctxt1($ctxt2($ctxt3(Box::new(context)))),
Expand All @@ -249,7 +245,7 @@ impl HpkeS {
};
}

let (context, enc) = dispatch_hpkes_new! { (config, pk_r, &mut csprng): [
let (context, enc) = dispatch_hpkes_new! { (config, pk_r): [
{
Kem::X25519Sha256 => X25519HkdfSha256,
Kdf::HkdfSha256 => HkdfSha256,
Expand Down Expand Up @@ -369,7 +365,7 @@ impl ReceiverContext {
let (ct, tag_slice) =
ciphertext.split_at_mut(ciphertext.len() - AeadTag::<AesGcm128>::size());
let tag = AeadTag::<AesGcm128>::from_bytes(tag_slice)?;
context.open_in_place_detached(ct, aad, &tag)?;
context.open_inout_detached(ct.into(), aad, &tag)?;
ct
}
Self::X25519HkdfSha256(ReceiverContextX25519HkdfSha256::HkdfSha256(
Expand All @@ -381,7 +377,7 @@ impl ReceiverContext {
let (ct, tag_slice) =
ciphertext.split_at_mut(ciphertext.len() - AeadTag::<ChaCha20Poly1305>::size());
let tag = AeadTag::<ChaCha20Poly1305>::from_bytes(tag_slice)?;
context.open_in_place_detached(ct, aad, &tag)?;
context.open_inout_detached(ct.into(), aad, &tag)?;
ct
}
Self::P256HkdfSha256(ReceiverContextP256HkdfSha256::HkdfSha256(
Expand All @@ -393,7 +389,7 @@ impl ReceiverContext {
let (ct, tag_slice) =
ciphertext.split_at_mut(ciphertext.len() - AeadTag::<AesGcm128>::size());
let tag = AeadTag::<AesGcm128>::from_bytes(tag_slice)?;
context.open_in_place_detached(ct, aad, &tag)?;
context.open_inout_detached(ct.into(), aad, &tag)?;
ct
}
Self::P256HkdfSha256(ReceiverContextP256HkdfSha256::HkdfSha256(
Expand All @@ -405,7 +401,7 @@ impl ReceiverContext {
let (ct, tag_slice) =
ciphertext.split_at_mut(ciphertext.len() - AeadTag::<ChaCha20Poly1305>::size());
let tag = AeadTag::<ChaCha20Poly1305>::from_bytes(tag_slice)?;
context.open_in_place_detached(ct, aad, &tag)?;
context.open_inout_detached(ct.into(), aad, &tag)?;
ct
}
})
Expand Down Expand Up @@ -577,14 +573,13 @@ impl Deref for HpkeR {
/// Generate a key pair for the identified KEM.
#[allow(clippy::unnecessary_wraps)]
pub fn generate_key_pair(kem: Kem) -> Res<(PrivateKey, PublicKey)> {
let mut csprng = rng();
let (sk, pk) = match kem {
Kem::X25519Sha256 => {
let (sk, pk) = X25519HkdfSha256::gen_keypair(&mut csprng);
let (sk, pk) = X25519HkdfSha256::gen_keypair();
(PrivateKey::X25519(sk), PublicKey::X25519(pk))
}
Kem::P256Sha256 => {
let (sk, pk) = DhP256HkdfSha256::gen_keypair(&mut csprng);
let (sk, pk) = DhP256HkdfSha256::gen_keypair();
(PrivateKey::P256(sk), PublicKey::P256(pk))
}
};
Expand Down
Loading