| 1 |
use super::SymKey; |
| 2 |
use crate::{ |
| 3 |
hpke::{Aead, Kdf, Kem}, |
| 4 |
Error, Res, |
| 5 |
}; |
| 6 |
|
| 7 |
#[cfg(not(feature = "pq"))] |
| 8 |
use ::hpke as rust_hpke; |
| 9 |
|
| 10 |
#[cfg(feature = "pq")] |
| 11 |
use ::hpke_pq as rust_hpke; |
| 12 |
|
| 13 |
use rust_hpke::{ |
| 14 |
aead::{AeadCtxR, AeadCtxS, AeadTag, AesGcm128, ChaCha20Poly1305}, |
| 15 |
kdf::HkdfSha256, |
| 16 |
kem::{Kem as KemTrait, X25519HkdfSha256}, |
| 17 |
setup_receiver, setup_sender, Deserializable, OpModeR, OpModeS, Serializable, |
| 18 |
}; |
| 19 |
|
| 20 |
#[cfg(feature = "pq")] |
| 21 |
use rust_hpke::kem::X25519Kyber768Draft00; |
| 22 |
|
| 23 |
use ::rand::thread_rng; |
| 24 |
use log::trace; |
| 25 |
use std::ops::Deref; |
| 26 |
|
| 27 |
/// Configuration for `Hpke`. |
| 28 |
#[derive(Clone, Copy)] |
| 29 |
pub struct Config { |
| 30 |
kem: Kem, |
| 31 |
kdf: Kdf, |
| 32 |
aead: Aead, |
| 33 |
} |
| 34 |
|
| 35 |
impl Config { |
| 36 |
pub fn new(kem: Kem, kdf: Kdf, aead: Aead) -> Self { |
| 37 |
Self { kem, kdf, aead } |
| 38 |
} |
| 39 |
|
| 40 |
pub fn kem(self) -> Kem { |
| 41 |
self.kem |
| 42 |
} |
| 43 |
|
| 44 |
pub fn kdf(self) -> Kdf { |
| 45 |
self.kdf |
| 46 |
} |
| 47 |
|
| 48 |
pub fn aead(self) -> Aead { |
| 49 |
self.aead |
| 50 |
} |
| 51 |
|
| 52 |
pub fn supported(self) -> bool { |
| 53 |
// TODO support more options |
| 54 |
self.kdf == Kdf::HkdfSha256 && matches!(self.aead, Aead::Aes128Gcm | Aead::ChaCha20Poly1305) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
impl Default for Config { |
| 59 |
fn default() -> Self { |
| 60 |
Self { |
| 61 |
kem: Kem::X25519Sha256, |
| 62 |
kdf: Kdf::HkdfSha256, |
| 63 |
aead: Aead::Aes128Gcm, |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
#[allow(clippy::large_enum_variant)] |
| 69 |
#[derive(Clone)] |
| 70 |
pub enum PublicKey { |
| 71 |
X25519(<X25519HkdfSha256 as KemTrait>::PublicKey), |
| 72 |
|
| 73 |
#[cfg(feature = "pq")] |
| 74 |
X25519Kyber768Draft00(<X25519Kyber768Draft00 as KemTrait>::PublicKey), |
| 75 |
} |
| 76 |
|
| 77 |
impl PublicKey { |
| 78 |
#[allow(clippy::unnecessary_wraps)] |
| 79 |
pub fn key_data(&self) -> Res<Vec<u8>> { |
| 80 |
Ok(match self { |
| 81 |
Self::X25519(k) => Vec::from(k.to_bytes().as_slice()), |
| 82 |
|
| 83 |
#[cfg(feature = "pq")] |
| 84 |
Self::X25519Kyber768Draft00(k) => Vec::from(k.to_bytes().as_slice()), |
| 85 |
}) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
impl std::fmt::Debug for PublicKey { |
| 90 |
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 91 |
if let Ok(b) = self.key_data() { |
| 92 |
write!(f, "PublicKey {}", hex::encode(b)) |
| 93 |
} else { |
| 94 |
write!(f, "Opaque PublicKey") |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
#[allow(clippy::large_enum_variant)] |
| 100 |
#[derive(Clone)] |
| 101 |
pub enum PrivateKey { |
| 102 |
X25519(<X25519HkdfSha256 as KemTrait>::PrivateKey), |
| 103 |
|
| 104 |
#[cfg(feature = "pq")] |
| 105 |
X25519Kyber768Draft00(<X25519Kyber768Draft00 as KemTrait>::PrivateKey), |
| 106 |
} |
| 107 |
|
| 108 |
impl PrivateKey { |
| 109 |
#[allow(clippy::unnecessary_wraps)] |
| 110 |
pub fn key_data(&self) -> Res<Vec<u8>> { |
| 111 |
Ok(match self { |
| 112 |
Self::X25519(k) => Vec::from(k.to_bytes().as_slice()), |
| 113 |
|
| 114 |
#[cfg(feature = "pq")] |
| 115 |
Self::X25519Kyber768Draft00(k) => Vec::from(k.to_bytes().as_slice()), |
| 116 |
}) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
impl std::fmt::Debug for PrivateKey { |
| 121 |
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 122 |
if let Ok(b) = self.key_data() { |
| 123 |
write!(f, "PrivateKey {}", hex::encode(b)) |
| 124 |
} else { |
| 125 |
write!(f, "Opaque PrivateKey") |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
// TODO: Use macros here. To do that, we needs concat_ident!(), but it's not ready. |
| 131 |
// This is what a macro that uses concat_ident!() might produce, written out in full. |
| 132 |
enum SenderContextX25519HkdfSha256HkdfSha256 { |
| 133 |
AesGcm128(Box<AeadCtxS<AesGcm128, HkdfSha256, X25519HkdfSha256>>), |
| 134 |
ChaCha20Poly1305(Box<AeadCtxS<ChaCha20Poly1305, HkdfSha256, X25519HkdfSha256>>), |
| 135 |
} |
| 136 |
|
| 137 |
#[cfg(feature = "pq")] |
| 138 |
enum SenderContextX25519Kyber768Draft00HkdfSha256 { |
| 139 |
AesGcm128(Box<AeadCtxS<AesGcm128, HkdfSha256, X25519Kyber768Draft00>>), |
| 140 |
} |
| 141 |
|
| 142 |
enum SenderContextX25519HkdfSha256 { |
| 143 |
HkdfSha256(SenderContextX25519HkdfSha256HkdfSha256), |
| 144 |
} |
| 145 |
|
| 146 |
#[cfg(feature = "pq")] |
| 147 |
enum SenderContextX25519Kyber768Draft00 { |
| 148 |
HkdfSha256(SenderContextX25519Kyber768Draft00HkdfSha256), |
| 149 |
} |
| 150 |
|
| 151 |
enum SenderContext { |
| 152 |
X25519HkdfSha256(SenderContextX25519HkdfSha256), |
| 153 |
|
| 154 |
#[cfg(feature = "pq")] |
| 155 |
X25519Kyber768Draft00(SenderContextX25519Kyber768Draft00), |
| 156 |
} |
| 157 |
|
| 158 |
impl SenderContext { |
| 159 |
fn seal(&mut self, plaintext: &mut [u8], aad: &[u8]) -> Res<Vec<u8>> { |
| 160 |
Ok(match self { |
| 161 |
Self::X25519HkdfSha256(SenderContextX25519HkdfSha256::HkdfSha256( |
| 162 |
SenderContextX25519HkdfSha256HkdfSha256::AesGcm128(context), |
| 163 |
)) => { |
| 164 |
let tag = context.seal_in_place_detached(plaintext, aad)?; |
| 165 |
Vec::from(tag.to_bytes().as_slice()) |
| 166 |
} |
| 167 |
Self::X25519HkdfSha256(SenderContextX25519HkdfSha256::HkdfSha256( |
| 168 |
SenderContextX25519HkdfSha256HkdfSha256::ChaCha20Poly1305(context), |
| 169 |
)) => { |
| 170 |
let tag = context.seal_in_place_detached(plaintext, aad)?; |
| 171 |
Vec::from(tag.to_bytes().as_slice()) |
| 172 |
} |
| 173 |
|
| 174 |
#[cfg(feature = "pq")] |
| 175 |
Self::X25519Kyber768Draft00(SenderContextX25519Kyber768Draft00::HkdfSha256( |
| 176 |
SenderContextX25519Kyber768Draft00HkdfSha256::AesGcm128(context), |
| 177 |
)) => { |
| 178 |
let tag = context.seal_in_place_detached(plaintext, aad)?; |
| 179 |
Vec::from(tag.to_bytes().as_slice()) |
| 180 |
} |
| 181 |
}) |
| 182 |
} |
| 183 |
|
| 184 |
fn export(&self, info: &[u8], out_buf: &mut [u8]) -> Res<()> { |
| 185 |
match self { |
| 186 |
Self::X25519HkdfSha256(SenderContextX25519HkdfSha256::HkdfSha256( |
| 187 |
SenderContextX25519HkdfSha256HkdfSha256::AesGcm128(context), |
| 188 |
)) => { |
| 189 |
context.export(info, out_buf)?; |
| 190 |
} |
| 191 |
Self::X25519HkdfSha256(SenderContextX25519HkdfSha256::HkdfSha256( |
| 192 |
SenderContextX25519HkdfSha256HkdfSha256::ChaCha20Poly1305(context), |
| 193 |
)) => { |
| 194 |
context.export(info, out_buf)?; |
| 195 |
} |
| 196 |
|
| 197 |
#[cfg(feature = "pq")] |
| 198 |
Self::X25519Kyber768Draft00(SenderContextX25519Kyber768Draft00::HkdfSha256( |
| 199 |
SenderContextX25519Kyber768Draft00HkdfSha256::AesGcm128(context), |
| 200 |
)) => { |
| 201 |
context.export(info, out_buf)?; |
| 202 |
} |
| 203 |
} |
| 204 |
Ok(()) |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
pub trait Exporter { |
| 209 |
fn export(&self, info: &[u8], len: usize) -> Res<SymKey>; |
| 210 |
} |
| 211 |
|
| 212 |
#[allow(clippy::module_name_repetitions)] |
| 213 |
pub struct HpkeS { |
| 214 |
context: SenderContext, |
| 215 |
enc: Vec<u8>, |
| 216 |
config: Config, |
| 217 |
} |
| 218 |
|
| 219 |
impl HpkeS { |
| 220 |
/// Create a new context that uses the KEM mode for sending. |
| 221 |
pub fn new(config: Config, pk_r: &mut PublicKey, info: &[u8]) -> Res<Self> { |
| 222 |
let mut csprng = thread_rng(); |
| 223 |
|
| 224 |
macro_rules! dispatch_hpkes_new { |
| 225 |
{ |
| 226 |
($c:expr, $pk:expr, $csprng:expr): [$( $(#[$meta:meta])* { |
| 227 |
$kemid:path => $kem:path, |
| 228 |
$kdfid:path => $kdf:path, |
| 229 |
$aeadid:path => $aead:path, |
| 230 |
$pke:path, $ctxt1:path, $ctxt2:path, $ctxt3:path $(,)? |
| 231 |
}),* $(,)?] |
| 232 |
} => { |
| 233 |
match ($c, $pk) { |
| 234 |
$( |
| 235 |
$(#[$meta])* |
| 236 |
( |
| 237 |
Config { |
| 238 |
kem: $kemid, |
| 239 |
kdf: $kdfid, |
| 240 |
aead: $aeadid, |
| 241 |
}, |
| 242 |
$pke(pk_r), |
| 243 |
) => { |
| 244 |
let (enc, context) = setup_sender::<$aead, $kdf, $kem, _>( |
| 245 |
&OpModeS::Base, |
| 246 |
pk_r, |
| 247 |
info, |
| 248 |
$csprng, |
| 249 |
)?; |
| 250 |
( |
| 251 |
$ctxt1($ctxt2($ctxt3(Box::new(context)))), |
| 252 |
Vec::from(enc.to_bytes().as_slice()), |
| 253 |
) |
| 254 |
} |
| 255 |
)* |
| 256 |
_ => return Err(Error::InvalidKeyType), |
| 257 |
} |
| 258 |
}; |
| 259 |
} |
| 260 |
|
| 261 |
let (context, enc) = dispatch_hpkes_new! { (config, pk_r, &mut csprng): [ |
| 262 |
{ |
| 263 |
Kem::X25519Sha256 => X25519HkdfSha256, |
| 264 |
Kdf::HkdfSha256 => HkdfSha256, |
| 265 |
Aead::Aes128Gcm => AesGcm128, |
| 266 |
PublicKey::X25519, |
| 267 |
SenderContext::X25519HkdfSha256, |
| 268 |
SenderContextX25519HkdfSha256::HkdfSha256, |
| 269 |
SenderContextX25519HkdfSha256HkdfSha256::AesGcm128, |
| 270 |
}, |
| 271 |
{ |
| 272 |
Kem::X25519Sha256 => X25519HkdfSha256, |
| 273 |
Kdf::HkdfSha256 => HkdfSha256, |
| 274 |
Aead::ChaCha20Poly1305 => ChaCha20Poly1305, |
| 275 |
PublicKey::X25519, |
| 276 |
SenderContext::X25519HkdfSha256, |
| 277 |
SenderContextX25519HkdfSha256::HkdfSha256, |
| 278 |
SenderContextX25519HkdfSha256HkdfSha256::ChaCha20Poly1305, |
| 279 |
}, |
| 280 |
|
| 281 |
#[cfg(feature = "pq")] |
| 282 |
{ |
| 283 |
Kem::X25519Kyber768Draft00 => X25519Kyber768Draft00, |
| 284 |
Kdf::HkdfSha256 => HkdfSha256, |
| 285 |
Aead::Aes128Gcm => AesGcm128, |
| 286 |
PublicKey::X25519Kyber768Draft00, |
| 287 |
SenderContext::X25519Kyber768Draft00, |
| 288 |
SenderContextX25519Kyber768Draft00::HkdfSha256, |
| 289 |
SenderContextX25519Kyber768Draft00HkdfSha256::AesGcm128, |
| 290 |
}, |
| 291 |
]}; |
| 292 |
|
| 293 |
Ok(Self { |
| 294 |
context, |
| 295 |
enc, |
| 296 |
config, |
| 297 |
}) |
| 298 |
} |
| 299 |
|
| 300 |
pub fn config(&self) -> Config { |
| 301 |
self.config |
| 302 |
} |
| 303 |
|
| 304 |
/// Get the encapsulated KEM secret. |
| 305 |
#[allow(clippy::unnecessary_wraps)] |
| 306 |
pub fn enc(&self) -> Res<Vec<u8>> { |
| 307 |
Ok(self.enc.clone()) |
| 308 |
} |
| 309 |
|
| 310 |
pub fn seal(&mut self, aad: &[u8], pt: &[u8]) -> Res<Vec<u8>> { |
| 311 |
let mut buf = pt.to_owned(); |
| 312 |
let mut tag = self.context.seal(&mut buf, aad)?; |
| 313 |
buf.append(&mut tag); |
| 314 |
Ok(buf) |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
impl Exporter for HpkeS { |
| 319 |
fn export(&self, info: &[u8], len: usize) -> Res<SymKey> { |
| 320 |
let mut buf = vec![0; len]; |
| 321 |
self.context.export(info, &mut buf)?; |
| 322 |
Ok(SymKey::from(buf)) |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
impl Deref for HpkeS { |
| 327 |
type Target = Config; |
| 328 |
fn deref(&self) -> &Self::Target { |
| 329 |
&self.config |
| 330 |
} |
| 331 |
} |
| 332 |
|
| 333 |
enum ReceiverContextX25519HkdfSha256HkdfSha256 { |
| 334 |
AesGcm128(Box<AeadCtxR<AesGcm128, HkdfSha256, X25519HkdfSha256>>), |
| 335 |
ChaCha20Poly1305(Box<AeadCtxR<ChaCha20Poly1305, HkdfSha256, X25519HkdfSha256>>), |
| 336 |
} |
| 337 |
|
| 338 |
#[cfg(feature = "pq")] |
| 339 |
enum ReceiverContextX25519Kyber768Draft00HkdfSha256 { |
| 340 |
AesGcm128(Box<AeadCtxR<AesGcm128, HkdfSha256, X25519Kyber768Draft00>>), |
| 341 |
} |
| 342 |
|
| 343 |
enum ReceiverContextX25519HkdfSha256 { |
| 344 |
HkdfSha256(ReceiverContextX25519HkdfSha256HkdfSha256), |
| 345 |
} |
| 346 |
|
| 347 |
#[cfg(feature = "pq")] |
| 348 |
enum ReceiverContextX25519Kyber768Draft00 { |
| 349 |
HkdfSha256(ReceiverContextX25519Kyber768Draft00HkdfSha256), |
| 350 |
} |
| 351 |
|
| 352 |
enum ReceiverContext { |
| 353 |
X25519HkdfSha256(ReceiverContextX25519HkdfSha256), |
| 354 |
|
| 355 |
#[cfg(feature = "pq")] |
| 356 |
X25519Kyber768Draft00(ReceiverContextX25519Kyber768Draft00), |
| 357 |
} |
| 358 |
|
| 359 |
impl ReceiverContext { |
| 360 |
fn open<'a>(&mut self, ciphertext: &'a mut [u8], aad: &[u8]) -> Res<&'a [u8]> { |
| 361 |
Ok(match self { |
| 362 |
Self::X25519HkdfSha256(ReceiverContextX25519HkdfSha256::HkdfSha256( |
| 363 |
ReceiverContextX25519HkdfSha256HkdfSha256::AesGcm128(context), |
| 364 |
)) => { |
| 365 |
if ciphertext.len() < AeadTag::<AesGcm128>::size() { |
| 366 |
return Err(Error::Truncated); |
| 367 |
} |
| 368 |
let (ct, tag_slice) = |
| 369 |
ciphertext.split_at_mut(ciphertext.len() - AeadTag::<AesGcm128>::size()); |
| 370 |
let tag = AeadTag::<AesGcm128>::from_bytes(tag_slice)?; |
| 371 |
context.open_in_place_detached(ct, aad, &tag)?; |
| 372 |
ct |
| 373 |
} |
| 374 |
Self::X25519HkdfSha256(ReceiverContextX25519HkdfSha256::HkdfSha256( |
| 375 |
ReceiverContextX25519HkdfSha256HkdfSha256::ChaCha20Poly1305(context), |
| 376 |
)) => { |
| 377 |
if ciphertext.len() < AeadTag::<ChaCha20Poly1305>::size() { |
| 378 |
return Err(Error::Truncated); |
| 379 |
} |
| 380 |
let (ct, tag_slice) = |
| 381 |
ciphertext.split_at_mut(ciphertext.len() - AeadTag::<ChaCha20Poly1305>::size()); |
| 382 |
let tag = AeadTag::<ChaCha20Poly1305>::from_bytes(tag_slice)?; |
| 383 |
context.open_in_place_detached(ct, aad, &tag)?; |
| 384 |
ct |
| 385 |
} |
| 386 |
|
| 387 |
#[cfg(feature = "pq")] |
| 388 |
Self::X25519Kyber768Draft00(ReceiverContextX25519Kyber768Draft00::HkdfSha256( |
| 389 |
ReceiverContextX25519Kyber768Draft00HkdfSha256::AesGcm128(context), |
| 390 |
)) => { |
| 391 |
if ciphertext.len() < AeadTag::<AesGcm128>::size() { |
| 392 |
return Err(Error::Truncated); |
| 393 |
} |
| 394 |
let (ct, tag_slice) = |
| 395 |
ciphertext.split_at_mut(ciphertext.len() - AeadTag::<AesGcm128>::size()); |
| 396 |
let tag = AeadTag::<AesGcm128>::from_bytes(tag_slice)?; |
| 397 |
context.open_in_place_detached(ct, aad, &tag)?; |
| 398 |
ct |
| 399 |
} |
| 400 |
}) |
| 401 |
} |
| 402 |
|
| 403 |
fn export(&self, info: &[u8], out_buf: &mut [u8]) -> Res<()> { |
| 404 |
match self { |
| 405 |
Self::X25519HkdfSha256(ReceiverContextX25519HkdfSha256::HkdfSha256( |
| 406 |
ReceiverContextX25519HkdfSha256HkdfSha256::AesGcm128(context), |
| 407 |
)) => { |
| 408 |
context.export(info, out_buf)?; |
| 409 |
} |
| 410 |
Self::X25519HkdfSha256(ReceiverContextX25519HkdfSha256::HkdfSha256( |
| 411 |
ReceiverContextX25519HkdfSha256HkdfSha256::ChaCha20Poly1305(context), |
| 412 |
)) => { |
| 413 |
context.export(info, out_buf)?; |
| 414 |
} |
| 415 |
|
| 416 |
#[cfg(feature = "pq")] |
| 417 |
Self::X25519Kyber768Draft00(ReceiverContextX25519Kyber768Draft00::HkdfSha256( |
| 418 |
ReceiverContextX25519Kyber768Draft00HkdfSha256::AesGcm128(context), |
| 419 |
)) => { |
| 420 |
context.export(info, out_buf)?; |
| 421 |
} |
| 422 |
} |
| 423 |
Ok(()) |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
#[allow(clippy::module_name_repetitions)] |
| 428 |
pub struct HpkeR { |
| 429 |
context: ReceiverContext, |
| 430 |
config: Config, |
| 431 |
} |
| 432 |
|
| 433 |
impl HpkeR { |
| 434 |
/// Create a new context that uses the KEM mode for sending. |
| 435 |
#[allow(clippy::similar_names)] |
| 436 |
pub fn new( |
| 437 |
config: Config, |
| 438 |
_pk_r: &PublicKey, |
| 439 |
sk_r: &PrivateKey, |
| 440 |
enc: &[u8], |
| 441 |
info: &[u8], |
| 442 |
) -> Res<Self> { |
| 443 |
macro_rules! dispatch_hpker_new { |
| 444 |
{ |
| 445 |
($c:ident, $sk:ident): [$( $(#[$meta:meta])* { |
| 446 |
$kemid:path => $kem:path, |
| 447 |
$kdfid:path => $kdf:path, |
| 448 |
$aeadid:path => $aead:path, |
| 449 |
$ske:path, $ctxt1:path, $ctxt2:path, $ctxt3:path $(,)? |
| 450 |
}),* $(,)?] |
| 451 |
} => { |
| 452 |
match ($c, $sk) { |
| 453 |
$( |
| 454 |
$(#[$meta])* |
| 455 |
( |
| 456 |
Config { |
| 457 |
kem: $kemid, |
| 458 |
kdf: $kdfid, |
| 459 |
aead: $aeadid, |
| 460 |
}, |
| 461 |
$ske(sk_r), |
| 462 |
) => { |
| 463 |
let enc = <$kem as KemTrait>::EncappedKey::from_bytes(enc)?; |
| 464 |
let context = setup_receiver::<$aead, $kdf, $kem>( |
| 465 |
&OpModeR::Base, |
| 466 |
sk_r, |
| 467 |
&enc, |
| 468 |
info, |
| 469 |
)?; |
| 470 |
$ctxt1($ctxt2($ctxt3(Box::new(context)))) |
| 471 |
} |
| 472 |
)* |
| 473 |
_ => return Err(Error::InvalidKeyType), |
| 474 |
} |
| 475 |
}; |
| 476 |
} |
| 477 |
let context = dispatch_hpker_new! {(config, sk_r): [ |
| 478 |
{ |
| 479 |
Kem::X25519Sha256 => X25519HkdfSha256, |
| 480 |
Kdf::HkdfSha256 => HkdfSha256, |
| 481 |
Aead::Aes128Gcm => AesGcm128, |
| 482 |
PrivateKey::X25519, |
| 483 |
ReceiverContext::X25519HkdfSha256, |
| 484 |
ReceiverContextX25519HkdfSha256::HkdfSha256, |
| 485 |
ReceiverContextX25519HkdfSha256HkdfSha256::AesGcm128, |
| 486 |
}, |
| 487 |
{ |
| 488 |
Kem::X25519Sha256 => X25519HkdfSha256, |
| 489 |
Kdf::HkdfSha256 => HkdfSha256, |
| 490 |
Aead::ChaCha20Poly1305 => ChaCha20Poly1305, |
| 491 |
PrivateKey::X25519, |
| 492 |
ReceiverContext::X25519HkdfSha256, |
| 493 |
ReceiverContextX25519HkdfSha256::HkdfSha256, |
| 494 |
ReceiverContextX25519HkdfSha256HkdfSha256::ChaCha20Poly1305, |
| 495 |
}, |
| 496 |
|
| 497 |
#[cfg(feature = "pq")] |
| 498 |
{ |
| 499 |
Kem::X25519Kyber768Draft00 => X25519Kyber768Draft00, |
| 500 |
Kdf::HkdfSha256 => HkdfSha256, |
| 501 |
Aead::Aes128Gcm => AesGcm128, |
| 502 |
PrivateKey::X25519Kyber768Draft00, |
| 503 |
ReceiverContext::X25519Kyber768Draft00, |
| 504 |
ReceiverContextX25519Kyber768Draft00::HkdfSha256, |
| 505 |
ReceiverContextX25519Kyber768Draft00HkdfSha256::AesGcm128, |
| 506 |
}, |
| 507 |
]}; |
| 508 |
|
| 509 |
Ok(Self { context, config }) |
| 510 |
} |
| 511 |
|
| 512 |
pub fn config(&self) -> Config { |
| 513 |
self.config |
| 514 |
} |
| 515 |
|
| 516 |
pub fn decode_public_key(kem: Kem, k: &[u8]) -> Res<PublicKey> { |
| 517 |
Ok(match kem { |
| 518 |
Kem::X25519Sha256 => { |
| 519 |
PublicKey::X25519(<X25519HkdfSha256 as KemTrait>::PublicKey::from_bytes(k)?) |
| 520 |
} |
| 521 |
|
| 522 |
#[cfg(feature = "pq")] |
| 523 |
Kem::X25519Kyber768Draft00 => PublicKey::X25519Kyber768Draft00( |
| 524 |
<X25519Kyber768Draft00 as KemTrait>::PublicKey::from_bytes(k)?, |
| 525 |
), |
| 526 |
}) |
| 527 |
} |
| 528 |
|
| 529 |
pub fn open(&mut self, aad: &[u8], ct: &[u8]) -> Res<Vec<u8>> { |
| 530 |
let mut buf = ct.to_owned(); |
| 531 |
let pt_len = self.context.open(&mut buf, aad)?.len(); |
| 532 |
buf.truncate(pt_len); |
| 533 |
Ok(buf) |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
impl Exporter for HpkeR { |
| 538 |
fn export(&self, info: &[u8], len: usize) -> Res<SymKey> { |
| 539 |
let mut buf = vec![0; len]; |
| 540 |
self.context.export(info, &mut buf)?; |
| 541 |
Ok(SymKey::from(buf)) |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
impl Deref for HpkeR { |
| 546 |
type Target = Config; |
| 547 |
fn deref(&self) -> &Self::Target { |
| 548 |
&self.config |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
/// Generate a key pair for the identified KEM. |
| 553 |
#[allow(clippy::unnecessary_wraps)] |
| 554 |
pub fn generate_key_pair(kem: Kem) -> Res<(PrivateKey, PublicKey)> { |
| 555 |
let mut csprng = thread_rng(); |
| 556 |
let (sk, pk) = match kem { |
| 557 |
Kem::X25519Sha256 => { |
| 558 |
let (sk, pk) = X25519HkdfSha256::gen_keypair(&mut csprng); |
| 559 |
(PrivateKey::X25519(sk), PublicKey::X25519(pk)) |
| 560 |
} |
| 561 |
|
| 562 |
#[cfg(feature = "pq")] |
| 563 |
Kem::X25519Kyber768Draft00 => { |
| 564 |
let (sk, pk) = X25519Kyber768Draft00::gen_keypair(&mut csprng); |
| 565 |
( |
| 566 |
PrivateKey::X25519Kyber768Draft00(sk), |
| 567 |
PublicKey::X25519Kyber768Draft00(pk), |
| 568 |
) |
| 569 |
} |
| 570 |
}; |
| 571 |
trace!("Generated key pair: sk={:?} pk={:?}", sk, pk); |
| 572 |
Ok((sk, pk)) |
| 573 |
} |
| 574 |
|
| 575 |
#[allow(clippy::unnecessary_wraps)] |
| 576 |
pub fn derive_key_pair(kem: Kem, ikm: &[u8]) -> Res<(PrivateKey, PublicKey)> { |
| 577 |
let (sk, pk) = match kem { |
| 578 |
Kem::X25519Sha256 => { |
| 579 |
let (sk, pk) = X25519HkdfSha256::derive_keypair(ikm); |
| 580 |
(PrivateKey::X25519(sk), PublicKey::X25519(pk)) |
| 581 |
} |
| 582 |
|
| 583 |
#[cfg(feature = "pq")] |
| 584 |
Kem::X25519Kyber768Draft00 => { |
| 585 |
let (sk, pk) = X25519Kyber768Draft00::derive_keypair(ikm); |
| 586 |
( |
| 587 |
PrivateKey::X25519Kyber768Draft00(sk), |
| 588 |
PublicKey::X25519Kyber768Draft00(pk), |
| 589 |
) |
| 590 |
} |
| 591 |
}; |
| 592 |
trace!("Derived key pair: sk={:?} pk={:?}", sk, pk); |
| 593 |
Ok((sk, pk)) |
| 594 |
} |
| 595 |
|
| 596 |
#[cfg(test)] |
| 597 |
mod test { |
| 598 |
use super::{generate_key_pair, Config, HpkeR, HpkeS}; |
| 599 |
use crate::{ |
| 600 |
hpke::{Aead, Kem}, |
| 601 |
init, |
| 602 |
}; |
| 603 |
|
| 604 |
const INFO: &[u8] = b"info"; |
| 605 |
const AAD: &[u8] = b"aad"; |
| 606 |
const PT: &[u8] = b"message"; |
| 607 |
|
| 608 |
#[allow(clippy::similar_names)] // for sk_x and pk_x |
| 609 |
#[test] |
| 610 |
fn make() { |
| 611 |
init(); |
| 612 |
let cfg = Config::default(); |
| 613 |
let (sk_r, mut pk_r) = generate_key_pair(cfg.kem()).unwrap(); |
| 614 |
let hpke_s = HpkeS::new(cfg, &mut pk_r, INFO).unwrap(); |
| 615 |
let _hpke_r = HpkeR::new(cfg, &pk_r, &sk_r, &hpke_s.enc().unwrap(), INFO).unwrap(); |
| 616 |
} |
| 617 |
|
| 618 |
#[allow(clippy::similar_names)] // for sk_x and pk_x |
| 619 |
fn seal_open(aead: Aead, kem: Kem) { |
| 620 |
// Setup |
| 621 |
init(); |
| 622 |
let cfg = Config { |
| 623 |
kem, |
| 624 |
aead, |
| 625 |
..Config::default() |
| 626 |
}; |
| 627 |
assert!(cfg.supported()); |
| 628 |
let (sk_r, mut pk_r) = generate_key_pair(cfg.kem()).unwrap(); |
| 629 |
|
| 630 |
// Send |
| 631 |
let mut hpke_s = HpkeS::new(cfg, &mut pk_r, INFO).unwrap(); |
| 632 |
let enc = hpke_s.enc().unwrap(); |
| 633 |
let ct = hpke_s.seal(AAD, PT).unwrap(); |
| 634 |
|
| 635 |
// Receive |
| 636 |
let mut hpke_r = HpkeR::new(cfg, &pk_r, &sk_r, &enc, INFO).unwrap(); |
| 637 |
let pt = hpke_r.open(AAD, &ct).unwrap(); |
| 638 |
assert_eq!(&pt[..], PT); |
| 639 |
} |
| 640 |
|
| 641 |
#[test] |
| 642 |
fn seal_open_gcm() { |
| 643 |
seal_open(Aead::Aes128Gcm, Kem::X25519Sha256); |
| 644 |
} |
| 645 |
|
| 646 |
#[test] |
| 647 |
fn seal_open_chacha() { |
| 648 |
seal_open(Aead::ChaCha20Poly1305, Kem::X25519Sha256); |
| 649 |
} |
| 650 |
|
| 651 |
#[cfg(feature = "pq")] |
| 652 |
#[test] |
| 653 |
fn seal_open_xyber768d00() { |
| 654 |
seal_open(Aead::Aes128Gcm, Kem::X25519Kyber768Draft00); |
| 655 |
} |
| 656 |
} |