cascade: add HPKE PSK mode
LGP's Ultra U2 layer passes the McEliece secret as the HPKE PSK (spec §8.5); interop with the hpke crate is tested both ways. Refs: R4-10
This commit is contained in:
parent
09553945af
commit
44a10149ce
|
|
@ -1,4 +1,4 @@
|
|||
//! HPKE base mode, single shot (RFC 9180 §5.1, §6.1), over the provider
|
||||
//! HPKE base and PSK modes, single shot (RFC 9180 §5.1, §6.1), over the provider
|
||||
//! traits (R3-9). Used for the inbox seal to the channel ingest key (CIK) and
|
||||
//! for LGP key hand-outs to receive keys, with the X-Wing KEM.
|
||||
//!
|
||||
|
|
@ -8,6 +8,7 @@
|
|||
//! RFC 9180's; interop is checked against the independent `hpke` crate.
|
||||
//!
|
||||
//! Output of [`seal`]: `enc || ciphertext`, where `enc` is the KEM ciphertext.
|
||||
//! Base mode and PSK mode (`seal_psk` / `open_psk`) are supported.
|
||||
|
||||
use ccc_crypto_core::{AeadAlgorithm, AeadProvider, KemAlgorithm, KemProvider, MacAlgorithm, MacProvider};
|
||||
use zeroize::Zeroizing;
|
||||
|
|
@ -132,14 +133,24 @@ impl Schedule<'_> {
|
|||
Ok(okm)
|
||||
}
|
||||
|
||||
// RFC 9180 §5.1 KeySchedule, mode_base, no PSK. Returns (key, base_nonce).
|
||||
fn key_schedule(&self, shared_secret: &[u8], info: &[u8]) -> Result<(Secret, Secret), CascadeError> {
|
||||
let psk_id_hash = self.labeled_extract(b"", b"psk_id_hash", b"")?;
|
||||
// RFC 9180 §5.1 KeySchedule for mode_base (psk = None) or mode_psk.
|
||||
// Returns (key, base_nonce).
|
||||
fn key_schedule(
|
||||
&self,
|
||||
shared_secret: &[u8],
|
||||
info: &[u8],
|
||||
psk: Option<&Psk<'_>>,
|
||||
) -> Result<(Secret, Secret), CascadeError> {
|
||||
let (mode, psk_bytes, psk_id): (u8, &[u8], &[u8]) = match psk {
|
||||
None => (0, b"", b""),
|
||||
Some(p) => (1, p.psk, p.psk_id),
|
||||
};
|
||||
let psk_id_hash = self.labeled_extract(b"", b"psk_id_hash", psk_id)?;
|
||||
let info_hash = self.labeled_extract(b"", b"info_hash", info)?;
|
||||
let mut context = vec![0u8]; // mode_base
|
||||
let mut context = vec![mode];
|
||||
context.extend_from_slice(&psk_id_hash);
|
||||
context.extend_from_slice(&info_hash);
|
||||
let secret = self.labeled_extract(shared_secret, b"secret", b"")?;
|
||||
let secret = self.labeled_extract(shared_secret, b"secret", psk_bytes)?;
|
||||
let key = self.labeled_expand(&secret, b"key", &context, self.suite.aead.key_len())?;
|
||||
let nonce = self.labeled_expand(&secret, b"base_nonce", &context, self.suite.aead.nonce_len())?;
|
||||
Ok((key, nonce))
|
||||
|
|
@ -157,10 +168,29 @@ pub fn key_schedule_for_tests(
|
|||
shared_secret: &[u8],
|
||||
info: &[u8],
|
||||
) -> Result<(Vec<u8>, Vec<u8>), CascadeError> {
|
||||
let (k, n) = schedule(suite, mac)?.key_schedule(shared_secret, info)?;
|
||||
let (k, n) = schedule(suite, mac)?.key_schedule(shared_secret, info, None)?;
|
||||
Ok((k.to_vec(), n.to_vec()))
|
||||
}
|
||||
|
||||
/// A pre-shared key for HPKE mode_psk (RFC 9180 §5.1.2). LGP's Ultra U2 layer
|
||||
/// passes the Classic McEliece shared secret as the PSK, so the hand-out key
|
||||
/// depends on both KEMs (the spec, §8.5).
|
||||
pub struct Psk<'a> {
|
||||
/// The pre-shared key (at least 32 bytes).
|
||||
pub psk: &'a [u8],
|
||||
/// Its identifier (non-empty).
|
||||
pub psk_id: &'a [u8],
|
||||
}
|
||||
|
||||
fn check_psk(psk: Option<&Psk<'_>>) -> Result<(), CascadeError> {
|
||||
match psk {
|
||||
Some(p) if p.psk.len() < 32 || p.psk_id.is_empty() => {
|
||||
Err(CascadeError::InvalidInput("HPKE psk must be >= 32 bytes with a non-empty psk_id"))
|
||||
}
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Seal `plaintext` to `recipient_pk` (RFC 9180 SealBase, one message, so the
|
||||
/// nonce is `base_nonce`). Returns `enc || ciphertext`.
|
||||
pub fn seal(
|
||||
|
|
@ -171,11 +201,37 @@ pub fn seal(
|
|||
aad: &[u8],
|
||||
plaintext: &[u8],
|
||||
) -> Result<Vec<u8>, CascadeError> {
|
||||
seal_with(suite, p, recipient_pk, info, aad, plaintext, None)
|
||||
}
|
||||
|
||||
/// [`seal`] in mode_psk (RFC 9180 SealPSK).
|
||||
pub fn seal_psk(
|
||||
suite: HpkeSuite,
|
||||
p: &HpkeProviders<'_>,
|
||||
recipient_pk: &[u8],
|
||||
psk: &Psk<'_>,
|
||||
info: &[u8],
|
||||
aad: &[u8],
|
||||
plaintext: &[u8],
|
||||
) -> Result<Vec<u8>, CascadeError> {
|
||||
seal_with(suite, p, recipient_pk, info, aad, plaintext, Some(psk))
|
||||
}
|
||||
|
||||
fn seal_with(
|
||||
suite: HpkeSuite,
|
||||
p: &HpkeProviders<'_>,
|
||||
recipient_pk: &[u8],
|
||||
info: &[u8],
|
||||
aad: &[u8],
|
||||
plaintext: &[u8],
|
||||
psk: Option<&Psk<'_>>,
|
||||
) -> Result<Vec<u8>, CascadeError> {
|
||||
check_psk(psk)?;
|
||||
let encap = p.kem.encapsulate(suite.kem, recipient_pk)?;
|
||||
if encap.ciphertext.len() != suite.enc_len {
|
||||
return Err(CascadeError::Crypto("KEM returned an unexpected enc length".into()));
|
||||
}
|
||||
let (key, nonce) = schedule(suite, p.mac)?.key_schedule(&encap.shared_secret, info)?;
|
||||
let (key, nonce) = schedule(suite, p.mac)?.key_schedule(&encap.shared_secret, info, psk)?;
|
||||
let ct = p.aead.encrypt_aead(suite.aead, &key, &nonce, plaintext, aad)?;
|
||||
let mut out = Vec::with_capacity(encap.ciphertext.len() + ct.len());
|
||||
out.extend_from_slice(&encap.ciphertext);
|
||||
|
|
@ -193,12 +249,38 @@ pub fn open(
|
|||
aad: &[u8],
|
||||
sealed: &[u8],
|
||||
) -> Result<Zeroizing<Vec<u8>>, CascadeError> {
|
||||
open_with(suite, p, recipient_sk, info, aad, sealed, None)
|
||||
}
|
||||
|
||||
/// [`open`] in mode_psk (RFC 9180 OpenPSK).
|
||||
pub fn open_psk(
|
||||
suite: HpkeSuite,
|
||||
p: &HpkeProviders<'_>,
|
||||
recipient_sk: &[u8],
|
||||
psk: &Psk<'_>,
|
||||
info: &[u8],
|
||||
aad: &[u8],
|
||||
sealed: &[u8],
|
||||
) -> Result<Zeroizing<Vec<u8>>, CascadeError> {
|
||||
open_with(suite, p, recipient_sk, info, aad, sealed, Some(psk))
|
||||
}
|
||||
|
||||
fn open_with(
|
||||
suite: HpkeSuite,
|
||||
p: &HpkeProviders<'_>,
|
||||
recipient_sk: &[u8],
|
||||
info: &[u8],
|
||||
aad: &[u8],
|
||||
sealed: &[u8],
|
||||
psk: Option<&Psk<'_>>,
|
||||
) -> Result<Zeroizing<Vec<u8>>, CascadeError> {
|
||||
check_psk(psk)?;
|
||||
if sealed.len() < suite.enc_len + suite.aead.tag_len() {
|
||||
return Err(CascadeError::AuthenticationFailed);
|
||||
}
|
||||
let (enc, ct) = sealed.split_at(suite.enc_len);
|
||||
let ss = p.kem.decapsulate(suite.kem, recipient_sk, enc).map_err(|_| CascadeError::AuthenticationFailed)?;
|
||||
let (key, nonce) = schedule(suite, p.mac)?.key_schedule(&ss, info)?;
|
||||
let (key, nonce) = schedule(suite, p.mac)?.key_schedule(&ss, info, psk)?;
|
||||
let pt = p.aead.decrypt_aead(suite.aead, &key, &nonce, ct, aad).map_err(|_| CascadeError::AuthenticationFailed)?;
|
||||
Ok(Zeroizing::new(pt))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,12 +3,12 @@
|
|||
//! KEM from the RustCrypto provider; HMAC and AEAD from wolfSSL (the vetted
|
||||
//! provider), as in production.
|
||||
|
||||
use ccc_cascade::hpke::{self as ours, HpkeProviders, HpkeSuite};
|
||||
use ::hpke::{Deserializable, Kem as _, OpModeR, OpModeS, PskBundle, Serializable};
|
||||
use ccc_cascade::hpke::{self as ours, HpkeProviders, HpkeSuite, Psk};
|
||||
use ccc_cascade::CascadeError;
|
||||
use ccc_crypto_core::{KemAlgorithm, KemProvider};
|
||||
use ccc_crypto_rustcrypto::RustCryptoProvider;
|
||||
use ccc_crypto_wolfssl::WolfSslProvider;
|
||||
use ::hpke::{Deserializable, Kem as _, OpModeR, OpModeS, Serializable};
|
||||
|
||||
type Kem = ::hpke::kem::XWing;
|
||||
type Kdf = ::hpke::kdf::HkdfSha256;
|
||||
|
|
@ -65,3 +65,34 @@ fn wrong_context_or_tamper_fails_closed() {
|
|||
}
|
||||
assert!(fails(ours::open(SUITE, &p, &kp.private_key, b"info", b"aad", &sealed[..100])), "short");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn psk_mode_interops_both_ways_and_needs_the_psk() {
|
||||
let rc = RustCryptoProvider::new();
|
||||
let wolf = WolfSslProvider::new();
|
||||
let p = HpkeProviders { kem: &rc, mac: &wolf, aead: &wolf };
|
||||
let psk_bytes = [0x42u8; 32];
|
||||
let psk = Psk { psk: &psk_bytes, psk_id: b"lum/lgp/u2" };
|
||||
let bundle = PskBundle::new(&psk_bytes, b"lum/lgp/u2").unwrap();
|
||||
// Ours -> hpke crate.
|
||||
let (sk, pk) = Kem::derive_keypair(b"psk interop seed for x-wing hpke");
|
||||
let sealed = ours::seal_psk(SUITE, &p, &pk.to_bytes(), &psk, b"info", b"aad", b"seed").unwrap();
|
||||
let (enc, ct) = sealed.split_at(SUITE.enc_len);
|
||||
let enc = <Kem as ::hpke::Kem>::EncappedKey::from_bytes(enc).unwrap();
|
||||
let pt = ::hpke::single_shot_open::<Aead, Kdf, Kem>(&OpModeR::Psk(bundle), &sk, &enc, b"info", ct, b"aad")
|
||||
.unwrap();
|
||||
assert_eq!(pt, b"seed");
|
||||
// hpke crate -> ours.
|
||||
let kp = rc.generate_keypair(KemAlgorithm::XWing).unwrap();
|
||||
let pk2 = <Kem as ::hpke::Kem>::PublicKey::from_bytes(&kp.public_key).unwrap();
|
||||
let (enc, ct) =
|
||||
::hpke::single_shot_seal::<Aead, Kdf, Kem>(&OpModeS::Psk(bundle), &pk2, b"info", b"msg", b"aad").unwrap();
|
||||
let mut sealed = enc.to_bytes().to_vec();
|
||||
sealed.extend_from_slice(&ct);
|
||||
assert_eq!(ours::open_psk(SUITE, &p, &kp.private_key, &psk, b"info", b"aad", &sealed).unwrap().as_slice(), b"msg");
|
||||
// Without the PSK, or with another one, it does not open.
|
||||
assert!(ours::open(SUITE, &p, &kp.private_key, b"info", b"aad", &sealed).is_err());
|
||||
let other = Psk { psk: &[0x43; 32], psk_id: b"lum/lgp/u2" };
|
||||
assert!(ours::open_psk(SUITE, &p, &kp.private_key, &other, b"info", b"aad", &sealed).is_err());
|
||||
assert!(ours::seal_psk(SUITE, &p, &kp.public_key, &Psk { psk: &[1; 16], psk_id: b"x" }, b"", b"", b"").is_err());
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue