libcrux/hpke/kem.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
#![doc = include_str!("KEM_Readme.md")]
#![doc = include_str!("KEM_Security.md")]
#![allow(non_camel_case_types, non_snake_case)]
use crate::kem::*;
use super::errors::*;
use super::kdf::*;
/// ## Key Encapsulation Mechanisms (KEMs)
///
/// | Value | KEM | Nsecret | Nenc | Npk | Nsk | Auth | Reference |
/// |:-------|:---------------------------|:---------|:-----|:----|:----|:-----|:------------------------|
/// | 0x0000 | (reserved) | N/A | N/A | N/A | N/A | yes | N/A |
/// | 0x0010 | DHKEM(P-256, HKDF-SHA256) | 32 | 65 | 65 | 32 | yes | [NISTCurves], [RFC5869] |
/// | 0x0011 | DHKEM(P-384, HKDF-SHA384) | 48 | 97 | 97 | 48 | yes | [NISTCurves], [RFC5869] |
/// | 0x0012 | DHKEM(P-521, HKDF-SHA512) | 64 | 133 | 133 | 66 | yes | [NISTCurves], [RFC5869] |
/// | 0x0020 | DHKEM(X25519, HKDF-SHA256) | 32 | 32 | 32 | 32 | yes | [RFC7748], [RFC5869] |
/// | 0x0021 | DHKEM(X448, HKDF-SHA512) | 64 | 56 | 56 | 56 | yes | [RFC7748], [RFC5869] |
///
/// The `Auth` column indicates if the KEM algorithm provides the [`AuthEncap()`]/[`AuthDecap()`]
/// interface and is therefore suitable for the Auth and AuthPSK modes. The meaning of all
/// other columns is explained below. All algorithms are suitable for the
/// PSK mode.
///
/// ### KEM Identifiers
///
/// The "HPKE KEM Identifiers" registry lists identifiers for key encapsulation
/// algorithms defined for use with HPKE. These identifiers are two-byte values,
/// so the maximum possible value is 0xFFFF = 65535.
///
/// Template:
///
/// * Value: The two-byte identifier for the algorithm
/// * KEM: The name of the algorithm
/// * Nsecret: The length in bytes of a KEM shared secret produced by the algorithm
/// * Nenc: The length in bytes of an encoded encapsulated key produced by the algorithm
/// * Npk: The length in bytes of an encoded public key for the algorithm
/// * Nsk: The length in bytes of an encoded private key for the algorithm
/// * Auth: A boolean indicating if this algorithm provides the [`AuthEncap()`]/[`AuthDecap()`] interface
/// * Reference: Where this algorithm is defined
///
/// [NISTCurves]: https://doi.org/10.6028/nist.fips.186-4
/// [RFC7748]: https://www.rfc-editor.org/info/rfc7748
/// [RFC5869]: https://www.rfc-editor.org/info/rfc5869
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum KEM {
/// 0x0010
DHKEM_P256_HKDF_SHA256,
/// 0x0011
DHKEM_P384_HKDF_SHA384,
/// 0x0012
DHKEM_P521_HKDF_SHA512,
/// 0x0020
DHKEM_X25519_HKDF_SHA256,
/// 0x0021
DHKEM_X448_HKDF_SHA512,
}
/// [`u16`] value of the `kem_id`.
///
/// See [`KEM`] for details.
pub fn kem_value(kem_id: KEM) -> u16 {
match kem_id {
KEM::DHKEM_P256_HKDF_SHA256 => 0x0010u16,
KEM::DHKEM_P384_HKDF_SHA384 => 0x0011u16,
KEM::DHKEM_P521_HKDF_SHA512 => 0x0012u16,
KEM::DHKEM_X25519_HKDF_SHA256 => 0x00020u16,
KEM::DHKEM_X448_HKDF_SHA512 => 0x0021u16,
}
}
/// Get the [`KDF`] algorithm for the given `kem_id`.
///
/// See [`KEM`] for details.
fn kdf_for_kem(kem_id: KEM) -> KDF {
match kem_id {
KEM::DHKEM_P256_HKDF_SHA256 => KDF::HKDF_SHA256,
KEM::DHKEM_P384_HKDF_SHA384 => KDF::HKDF_SHA384,
KEM::DHKEM_P521_HKDF_SHA512 => KDF::HKDF_SHA512,
KEM::DHKEM_X25519_HKDF_SHA256 => KDF::HKDF_SHA256,
KEM::DHKEM_X448_HKDF_SHA512 => KDF::HKDF_SHA512,
}
}
/// Convert the KEM type to the KEM algorithm of libcrux.
fn kem_to_named_group(alg: KEM) -> Algorithm {
match alg {
KEM::DHKEM_P256_HKDF_SHA256 => Algorithm::Secp256r1,
KEM::DHKEM_P384_HKDF_SHA384 => Algorithm::Secp384r1,
KEM::DHKEM_P521_HKDF_SHA512 => Algorithm::Secp521r1,
KEM::DHKEM_X25519_HKDF_SHA256 => Algorithm::X25519,
KEM::DHKEM_X448_HKDF_SHA512 => Algorithm::X448,
}
}
/// Get the length of the shared secret.
///
/// See [`KEM`] for details.
pub fn Nsecret(kem_id: KEM) -> usize {
match kem_id {
KEM::DHKEM_P256_HKDF_SHA256 => 32,
KEM::DHKEM_P384_HKDF_SHA384 => 48,
KEM::DHKEM_P521_HKDF_SHA512 => 64,
KEM::DHKEM_X25519_HKDF_SHA256 => 32,
KEM::DHKEM_X448_HKDF_SHA512 => 64,
}
}
/// Get the length of the encoded encapsulated key.
///
/// See [`KEM`] for details.
pub fn Nenc(kem_id: KEM) -> usize {
match kem_id {
KEM::DHKEM_P256_HKDF_SHA256 => 65,
KEM::DHKEM_P384_HKDF_SHA384 => 97,
KEM::DHKEM_P521_HKDF_SHA512 => 133,
KEM::DHKEM_X25519_HKDF_SHA256 => 32,
KEM::DHKEM_X448_HKDF_SHA512 => 56,
}
}
/// Get the length of the private key.
///
/// See [`KEM`] for details.
pub fn Nsk(kem_id: KEM) -> usize {
match kem_id {
KEM::DHKEM_P256_HKDF_SHA256 => 32,
KEM::DHKEM_P384_HKDF_SHA384 => 48,
KEM::DHKEM_P521_HKDF_SHA512 => 66,
KEM::DHKEM_X25519_HKDF_SHA256 => 32,
KEM::DHKEM_X448_HKDF_SHA512 => 56,
}
}
/// Get the length of the encoded public key.
///
/// See [`KEM`] for details.
pub fn Npk(kem_id: KEM) -> usize {
match kem_id {
KEM::DHKEM_P256_HKDF_SHA256 => 65,
KEM::DHKEM_P384_HKDF_SHA384 => 97,
KEM::DHKEM_P521_HKDF_SHA512 => 133,
KEM::DHKEM_X25519_HKDF_SHA256 => 32,
KEM::DHKEM_X448_HKDF_SHA512 => 56,
}
}
/// The length in bytes of a Diffie-Hellman shared secret produced by [`DH()`].
///
/// | | [`Ndh`] |
/// | ------ | ------- |
/// | P-256 | 32 |
/// | P-384 | 48 |
/// | P-521 | 66 |
/// | X25519 | 32 |
/// | X448 | 56 |
pub fn Ndh(kem_id: KEM) -> usize {
match kem_id {
KEM::DHKEM_P256_HKDF_SHA256 => 32,
KEM::DHKEM_P384_HKDF_SHA384 => 48,
KEM::DHKEM_P521_HKDF_SHA512 => 66,
KEM::DHKEM_X25519_HKDF_SHA256 => 32,
KEM::DHKEM_X448_HKDF_SHA512 => 56,
}
}
pub type PrivateKey = Vec<u8>;
pub type PublicKey = Vec<u8>;
pub type KeyPair = (PrivateKey, PublicKey);
pub type PublicKeyIn = [u8];
pub type PrivateKeyIn = [u8];
pub type SharedSecret = Vec<u8>;
pub type SerializedPublicKey = Vec<u8>;
pub type Randomness = Vec<u8>;
pub type EncapResult = Result<(SharedSecret, SerializedPublicKey), HpkeError>;
// === Label ===
/// "dkp_prk"
fn dkp_prk_label() -> Vec<u8> {
vec![0x64u8, 0x6bu8, 0x70u8, 0x5fu8, 0x70u8, 0x72u8, 0x6bu8]
}
/// "eae_prk"
fn eae_prk_label() -> Vec<u8> {
vec![0x65u8, 0x61u8, 0x65u8, 0x5fu8, 0x70u8, 0x72u8, 0x6bu8]
}
/// "sk"
fn sk_label() -> Vec<u8> {
vec![0x73u8, 0x6bu8]
}
/// "candidate"
fn candidate_label() -> Vec<u8> {
vec![
0x63u8, 0x61u8, 0x6eu8, 0x64u8, 0x69u8, 0x64u8, 0x61u8, 0x74u8, 0x65u8,
]
}
/// "shared_secret"
fn shared_secret_label() -> Vec<u8> {
vec![
0x73u8, 0x68u8, 0x61u8, 0x72u8, 0x65u8, 0x64u8, 0x5fu8, 0x73u8, 0x65u8, 0x63u8, 0x72u8,
0x65u8, 0x74u8,
]
}
/// Get an empty byte sequence.
fn empty() -> Vec<u8> {
vec![]
}
/// Get the label for the KEM with the cipher suite ID.
/// "KEM"
fn suite_id(alg: KEM) -> Vec<u8> {
let mut suite_id = vec![0x4bu8, 0x45u8, 0x4du8]; // "KEM"
suite_id.extend_from_slice(&kem_value(alg).to_be_bytes());
suite_id
}
/// For the variants of DHKEM defined in this document, the size [`Nsecret`] of the
/// KEM shared secret is equal to the output length of the hash function
/// underlying the KDF. For P-256, P-384 and P-521, the size `Ndh` of the
/// Diffie-Hellman shared secret is equal to 32, 48, and 66, respectively,
/// corresponding to the x-coordinate of the resulting elliptic curve point.
/// For X25519 and X448, the size [`Ndh`] of is equal to 32 and 56, respectively.
fn shared_secret_from_dh(alg: KEM, mut secret: Vec<u8>) -> Vec<u8> {
match alg {
KEM::DHKEM_P256_HKDF_SHA256 => secret.drain(0..Ndh(alg)).collect(),
KEM::DHKEM_P384_HKDF_SHA384 => secret.drain(0..Ndh(alg)).collect(),
KEM::DHKEM_P521_HKDF_SHA512 => secret.drain(0..Ndh(alg)).collect(),
KEM::DHKEM_X25519_HKDF_SHA256 => secret,
KEM::DHKEM_X448_HKDF_SHA512 => secret,
}
}
/// Perform a non-interactive Diffie-Hellman exchange using the private key
/// `skX` and public key `pkY` to produce a Diffie-Hellman shared
/// secret of length `Ndh`. This function can raise a
/// [`ValidationError`](`HpkeError::ValidationError`) as described in
/// [validation](#validation-of-inputs-and-outputs).
pub fn DH(alg: KEM, sk: &PrivateKeyIn, pk: &PublicKeyIn) -> Result<SharedSecret, HpkeError> {
match crate::ecdh::derive(kem_to_named_group(alg).try_into().unwrap(), pk, sk) {
Ok(secret) => HpkeBytesResult::Ok(shared_secret_from_dh(alg, secret)),
Err(_) => HpkeBytesResult::Err(HpkeError::ValidationError),
}
}
fn pk(alg: KEM, sk: &PrivateKeyIn) -> Result<PublicKey, HpkeError> {
match crate::kem::secret_to_public(kem_to_named_group(alg), sk) {
Ok(pk) => HpkeBytesResult::Ok(pk),
Err(_) => HpkeBytesResult::Err(HpkeError::ValidationError),
}
}
/// Prepend 0x04 to the byte sequence.
fn nist_curve_to_uncompressed(pk: PublicKey) -> PublicKey {
let mut out = vec![0x04u8];
out.extend_from_slice(&pk);
out
}
/// Produce a byte string of length `Npk` encoding the public key `pkX`.
///
/// For P-256, P-384 and P-521, the [`SerializePublicKey()`] function of the
/// KEM performs the uncompressed Elliptic-Curve-Point-to-Octet-String
/// conversion according to [SECG]. [`DeserializePublicKey()`] performs the
/// uncompressed Octet-String-to-Elliptic-Curve-Point conversion.
///
/// For X25519 and X448, the `SerializePublicKey()` and `DeserializePublicKey()`
/// functions are the identity function, since these curves already use
/// fixed-length byte strings for public keys.
///
/// Some deserialized public keys MUST be validated before they can be used.
///
/// [secg]: https://secg.org/sec1-v2.pdf
pub fn SerializePublicKey(alg: KEM, pk: PublicKey) -> PublicKey {
match alg {
KEM::DHKEM_P256_HKDF_SHA256 => nist_curve_to_uncompressed(pk),
KEM::DHKEM_P384_HKDF_SHA384 => nist_curve_to_uncompressed(pk),
KEM::DHKEM_P521_HKDF_SHA512 => nist_curve_to_uncompressed(pk),
KEM::DHKEM_X25519_HKDF_SHA256 => pk,
KEM::DHKEM_X448_HKDF_SHA512 => pk,
}
}
/// Remove the leading 0x04 from the public key.
fn nist_curve_from_uncompressed(pk: &PublicKeyIn) -> Vec<u8> {
if pk[0] == 0x04 {
pk[1..].to_vec()
} else {
pk.to_vec()
}
}
/// Parse a byte string of length `Npk` to recover a
/// public key. This function can raise a `DeserializeError` error upon `pkXm`
/// deserialization failure.
pub fn DeserializePublicKey(alg: KEM, enc: &[u8]) -> HpkeBytesResult {
HpkeBytesResult::Ok(match alg {
KEM::DHKEM_P256_HKDF_SHA256 => nist_curve_from_uncompressed(enc),
KEM::DHKEM_P384_HKDF_SHA384 => nist_curve_from_uncompressed(enc),
KEM::DHKEM_P521_HKDF_SHA512 => nist_curve_from_uncompressed(enc),
KEM::DHKEM_X25519_HKDF_SHA256 => enc.to_vec(),
KEM::DHKEM_X448_HKDF_SHA512 => enc.to_vec(),
})
}
/// ```text
/// def ExtractAndExpand(dh, kem_context):
/// eae_prk = LabeledExtract("", "eae_prk", dh)
/// shared_secret = LabeledExpand(eae_prk, "shared_secret",
/// kem_context, Nsecret)
/// return shared_secret
/// ```
fn ExtractAndExpand(
alg: KEM,
suite_id: Vec<u8>,
dh: SharedSecret,
kem_context: &[u8],
) -> HpkeBytesResult {
let kdf = kdf_for_kem(alg);
let eae_prk = LabeledExtract(kdf, suite_id.clone(), &empty(), eae_prk_label(), &dh)?;
LabeledExpand(
kdf,
suite_id,
&eae_prk,
shared_secret_label(),
kem_context,
Nsecret(alg),
)
}
fn I2OSP(counter: usize) -> Vec<u8> {
vec![counter as u8]
}
/// For X25519 and X448, the `DeriveKeyPair()` function applies a KDF to the input:
///
/// ```text
/// def DeriveKeyPair(ikm):
/// dkp_prk = LabeledExtract("", "dkp_prk", ikm)
/// sk = LabeledExpand(dkp_prk, "sk", "", Nsk)
/// return (sk, pk(sk))
/// ```
pub fn DeriveKeyPairX(alg: KEM, ikm: &InputKeyMaterial) -> Result<KeyPair, HpkeError> {
let kdf = kdf_for_kem(alg);
let dkp_prk = LabeledExtract(kdf, suite_id(alg), &empty(), dkp_prk_label(), ikm)?;
let sk = LabeledExpand(kdf, suite_id(alg), &dkp_prk, sk_label(), &empty(), Nsk(alg))?;
match crate::kem::secret_to_public(kem_to_named_group(alg), &sk) {
Ok(pk) => Result::<KeyPair, HpkeError>::Ok((sk, pk)),
Err(_) => Result::<KeyPair, HpkeError>::Err(HpkeError::CryptoError),
}
}
/// ### DeriveKeyPair
///
/// The keys that [`DeriveKeyPair()`] produces have only as much entropy as the provided
/// input keying material. For a given KEM, the `ikm` parameter given to [`DeriveKeyPair()`] SHOULD
/// have length at least [`Nsk`], and SHOULD have at least [`Nsk`] bytes of entropy.
///
/// All invocations of KDF functions (such as [`LabeledExtract()`] or [`LabeledExpand()`]) in any
/// DHKEM's [`DeriveKeyPair()`] function use the DHKEM's associated KDF (as opposed to
/// the ciphersuite's KDF).
///
/// For P-256, P-384 and P-521, the [`DeriveKeyPair()`] function of the KEM performs
/// rejection sampling over field elements.
///
/// ```text
/// def DeriveKeyPair(ikm):
/// dkp_prk = LabeledExtract("", "dkp_prk", ikm)
/// sk = 0
/// counter = 0
/// while sk == 0 or sk >= order:
/// if counter > 255:
/// raise DeriveKeyPairError
/// bytes = LabeledExpand(dkp_prk, "candidate",
/// I2OSP(counter, 1), Nsk)
/// bytes[0] = bytes[0] & bitmask
/// sk = OS2IP(bytes)
/// counter = counter + 1
/// return (sk, pk(sk))
/// ```
///
/// `order` is the order of the curve being used (see section D.1.2 of [NISTCurves]), and
/// is listed below for completeness.
///
/// ```text
/// P-256:
/// 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551
///
/// P-384:
/// 0xffffffffffffffffffffffffffffffffffffffffffffffffc7634d81f4372ddf
/// 581a0db248b0a77aecec196accc52973
///
/// P-521:
/// 0x01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
/// fa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409
/// ```
///
/// `bitmask` is defined to be 0xFF for P-256 and P-384, and 0x01 for P-521.
/// The precise likelihood of `DeriveKeyPair()` failing with DeriveKeyPairError
/// depends on the group being used, but it is negligibly small in all cases.
/// See [hpke errors](`mod@crate::hpke::errors`) for information about dealing with such failures.
///
/// For X25519 and X448, the [`DeriveKeyPair()`] function applies a KDF to the input:
///
/// ```text
/// def DeriveKeyPair(ikm):
/// dkp_prk = LabeledExtract("", "dkp_prk", ikm)
/// sk = LabeledExpand(dkp_prk, "sk", "", Nsk)
/// return (sk, pk(sk))
/// ```
///
/// [NISTCurves]: https://doi.org/10.6028/nist.fips.186-4
pub fn DeriveKeyPair(alg: KEM, ikm: &InputKeyMaterial) -> Result<KeyPair, HpkeError> {
let kdf = kdf_for_kem(alg);
let dkp_prk = LabeledExtract(kdf, suite_id(alg), &empty(), dkp_prk_label(), ikm)?;
let named_group = kem_to_named_group(alg);
let sk = if alg == KEM::DHKEM_X25519_HKDF_SHA256 || alg == KEM::DHKEM_X448_HKDF_SHA512 {
LabeledExpand(kdf, suite_id(alg), &dkp_prk, sk_label(), &empty(), 32)?
} else {
let mut bitmask = 0xFFu8;
if alg == KEM::DHKEM_P521_HKDF_SHA512 {
bitmask = 0x01u8;
}
let mut sk = Vec::new();
for counter in 0..256 {
if sk.len() == 0 {
// Only keep looking if we didn't find one.
let mut bytes = LabeledExpand(
kdf,
suite_id(alg),
&dkp_prk,
candidate_label(),
&I2OSP(counter),
32,
)?;
bytes[0] = bytes[0] & bitmask;
// This check ensure sk != 0 or sk < order
if crate::ecdh::validate_scalar(named_group.try_into().unwrap(), &bytes).is_ok() {
sk = bytes;
}
}
}
sk
};
if sk.len() == 0 {
Result::<KeyPair, HpkeError>::Err(HpkeError::DeriveKeyPairError)
} else {
let pk = pk(alg, &sk)?;
Ok((sk, pk))
}
}
/// Randomized algorithm to generate a key pair `(skX, pkX)`.
pub fn GenerateKeyPair(alg: KEM, randomness: Randomness) -> Result<KeyPair, HpkeError> {
if randomness.len() != Nsk(alg) {
Err(HpkeError::InvalidParameters)
} else {
DeriveKeyPair(alg, &randomness)
}
}
/// ```text
/// def Encap(pkR):
/// skE, pkE = GenerateKeyPair()
/// dh = DH(skE, pkR)
/// enc = SerializePublicKey(pkE)
///
/// pkRm = SerializePublicKey(pkR)
/// kem_context = concat(enc, pkRm)
///
/// shared_secret = ExtractAndExpand(dh, kem_context)
/// ```
pub fn Encap(alg: KEM, pkR: &PublicKeyIn, randomness: Randomness) -> EncapResult {
let (skE, pkE) = GenerateKeyPair(alg, randomness)?;
let dh = DH(alg, &skE, pkR)?;
let enc = SerializePublicKey(alg, pkE);
let pkRm = SerializePublicKey(alg, pkR.to_vec());
let mut kem_context = enc.clone();
kem_context.extend_from_slice(&pkRm);
let shared_secret = ExtractAndExpand(alg, suite_id(alg), dh, &kem_context)?;
EncapResult::Ok((shared_secret, enc))
}
/// ```text
/// def Decap(enc, skR):
/// pkE = DeserializePublicKey(enc)
/// dh = DH(skR, pkE)
///
/// pkRm = SerializePublicKey(pk(skR))
/// kem_context = concat(enc, pkRm)
///
/// shared_secret = ExtractAndExpand(dh, kem_context)
/// return shared_secret
/// ```
pub fn Decap(alg: KEM, enc: &[u8], skR: &PrivateKeyIn) -> Result<SharedSecret, HpkeError> {
let pkE = DeserializePublicKey(alg, enc)?;
let dh = DH(alg, skR, &pkE)?;
let pkR = pk(alg, skR)?;
let pkRm = SerializePublicKey(alg, pkR);
let mut kem_context = enc.to_vec();
kem_context.extend_from_slice(&pkRm);
ExtractAndExpand(alg, suite_id(alg), dh, &kem_context)
}
/// ```text
/// def AuthEncap(pkR, skS):
/// skE, pkE = GenerateKeyPair()
/// dh = concat(DH(skE, pkR), DH(skS, pkR))
/// enc = SerializePublicKey(pkE)
///
/// pkRm = SerializePublicKey(pkR)
/// pkSm = SerializePublicKey(pk(skS))
/// kem_context = concat(enc, pkRm, pkSm)
///
/// shared_secret = ExtractAndExpand(dh, kem_context)
/// return shared_secret, enc
/// ```
pub fn AuthEncap(
alg: KEM,
pkR: &PublicKeyIn,
skS: &PrivateKeyIn,
randomness: Randomness,
) -> EncapResult {
let (skE, pkE) = GenerateKeyPair(alg, randomness)?;
let dhE = DH(alg, &skE, pkR)?;
let dhS = DH(alg, skS, pkR)?;
let mut dh = dhE;
dh.extend_from_slice(&dhS);
let enc = SerializePublicKey(alg, pkE);
let pkRm = SerializePublicKey(alg, pkR.to_vec());
let pkS = pk(alg, skS)?;
let pkSm = SerializePublicKey(alg, pkS);
let mut kem_context = enc.clone();
kem_context.extend_from_slice(&pkRm);
kem_context.extend_from_slice(&pkSm);
let shared_secret = ExtractAndExpand(alg, suite_id(alg), dh, &kem_context)?;
EncapResult::Ok((shared_secret, enc))
}
/// ```text
/// def AuthDecap(enc, skR, pkS):
/// pkE = DeserializePublicKey(enc)
/// dh = concat(DH(skR, pkE), DH(skR, pkS))
///
/// pkRm = SerializePublicKey(pk(skR))
/// pkSm = SerializePublicKey(pkS)
/// kem_context = concat(enc, pkRm, pkSm)
///
/// shared_secret = ExtractAndExpand(dh, kem_context)
/// return shared_secret
/// ```
pub fn AuthDecap(
alg: KEM,
enc: &[u8],
skR: &PrivateKeyIn,
pkS: &PublicKeyIn,
) -> Result<SharedSecret, HpkeError> {
let pkE = DeserializePublicKey(alg, enc)?;
let dhE = DH(alg, skR, &pkE)?;
let dhS = DH(alg, skR, pkS)?;
let mut dh = dhE;
dh.extend_from_slice(&dhS);
let pkR = pk(alg, skR)?;
let pkRm = SerializePublicKey(alg, pkR);
let pkSm = SerializePublicKey(alg, pkS.to_vec());
let mut kem_context = enc.to_vec();
kem_context.extend_from_slice(&pkRm);
kem_context.extend_from_slice(&pkSm);
ExtractAndExpand(alg, suite_id(alg), dh, &kem_context)
}
#[test]
fn derive_x25519() {
use std::num::ParseIntError;
fn from_hex(s: &str) -> Vec<u8> {
debug_assert!(s.len() % 2 == 0);
let b: Result<Vec<u8>, ParseIntError> = (0..s.len())
.step_by(2)
.map(|i| u8::from_str_radix(&s[i..i + 2], 16))
.collect();
b.expect("Error parsing hex string")
}
// A.1.1. test vector
let ikm_e = from_hex("7268600d403fce431561aef583ee1613527cff655c1343f29812e66706df3234");
let ikm_r = from_hex("6db9df30aa07dd42ee5e8181afdb977e538f5e1fec8a06223f33f7013e525037");
let expected_sk_e =
from_hex("52c4a758a802cd8b936eceea314432798d5baf2d7e9235dc084ab1b9cfa2f736");
let expected_pk_e =
from_hex("37fda3567bdbd628e88668c3c8d7e97d1d1253b6d4ea6d44c150f741f1bf4431");
let expected_sk_r =
from_hex("4612c550263fc8ad58375df3f557aac531d26850903e55a9f23f21d8534e8ac8");
let expected_pk_r =
from_hex("3948cfe0ad1ddb695d780e59077195da6c56506b027329794ab02bca80815c4d");
let (sk_e, pk_e) =
DeriveKeyPair(KEM::DHKEM_X25519_HKDF_SHA256, &ikm_e).expect("Error deriving key pair");
let (sk_r, pk_r) =
DeriveKeyPair(KEM::DHKEM_X25519_HKDF_SHA256, &ikm_r).expect("Error deriving key pair");
assert_eq!(expected_sk_e, sk_e);
assert_eq!(expected_sk_r, sk_r);
assert_eq!(expected_pk_e, pk_e);
assert_eq!(expected_pk_r, pk_r);
}