| 1 |
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
// Copyright by contributors to this project. |
| 3 |
// SPDX-License-Identifier: (Apache-2.0 OR MIT) |
| 4 |
|
| 5 |
use mls_rs_core::{ |
| 6 |
crypto::{CipherSuite, HpkePublicKey, HpkeSecretKey}, |
| 7 |
error::IntoAnyError, |
| 8 |
mls_rs_codec::{self, MlsDecode, MlsEncode, MlsSize}, |
| 9 |
}; |
| 10 |
|
| 11 |
use alloc::vec::Vec; |
| 12 |
|
| 13 |
#[cfg(feature = "mock")] |
| 14 |
use mockall::automock; |
| 15 |
|
| 16 |
/// A trait that provides the required KEM functions |
| 17 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 18 |
#[cfg_attr(all(target_arch = "wasm32", mls_build_async), maybe_async::must_be_async(?Send))] |
| 19 |
#[cfg_attr( |
| 20 |
all(not(target_arch = "wasm32"), mls_build_async), |
| 21 |
maybe_async::must_be_async |
| 22 |
)] |
| 23 |
#[cfg_attr(feature = "mock", automock(type Error = crate::mock::TestError;))] |
| 24 |
pub trait KemType: Send + Sync + Sized { |
| 25 |
type Error: IntoAnyError + Send + Sync; |
| 26 |
|
| 27 |
/// KEM Id, as specified in RFC 9180, Section 5.1 and Table 2. |
| 28 |
fn kem_id(&self) -> u16; |
| 29 |
|
| 30 |
async fn generate_deterministic( |
| 31 |
&self, |
| 32 |
seed: &[u8], |
| 33 |
) -> Result<(HpkeSecretKey, HpkePublicKey), Self::Error>; |
| 34 |
|
| 35 |
async fn generate(&self) -> Result<(HpkeSecretKey, HpkePublicKey), Self::Error>; |
| 36 |
fn public_key_validate(&self, key: &HpkePublicKey) -> Result<(), Self::Error>; |
| 37 |
|
| 38 |
async fn encap(&self, remote_key: &HpkePublicKey) -> Result<KemResult, Self::Error>; |
| 39 |
|
| 40 |
async fn decap( |
| 41 |
&self, |
| 42 |
enc: &[u8], |
| 43 |
secret_key: &HpkeSecretKey, |
| 44 |
local_public: &HpkePublicKey, |
| 45 |
) -> Result<Vec<u8>, Self::Error>; |
| 46 |
|
| 47 |
fn seed_length_for_derive(&self) -> usize; |
| 48 |
fn public_key_size(&self) -> usize; |
| 49 |
fn secret_key_size(&self) -> usize; |
| 50 |
|
| 51 |
fn enc_size(&self) -> usize { |
| 52 |
self.public_key_size() |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/// Struct to represent the output of the kem [encap](KemType::encap) function |
| 57 |
#[derive(Clone, Debug, MlsDecode, MlsEncode, MlsSize)] |
| 58 |
pub struct KemResult { |
| 59 |
pub shared_secret: Vec<u8>, |
| 60 |
pub enc: Vec<u8>, |
| 61 |
} |
| 62 |
|
| 63 |
impl KemResult { |
| 64 |
pub fn new(shared_secret: Vec<u8>, enc: Vec<u8>) -> Self { |
| 65 |
Self { shared_secret, enc } |
| 66 |
} |
| 67 |
|
| 68 |
pub fn shared_secret(&self) -> &[u8] { |
| 69 |
&self.shared_secret |
| 70 |
} |
| 71 |
|
| 72 |
/// Returns the ciphertext encapsulating the shared secret. |
| 73 |
pub fn enc(&self) -> &[u8] { |
| 74 |
&self.enc |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
/// Kem identifiers for HPKE |
| 79 |
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] |
| 80 |
#[repr(u16)] |
| 81 |
#[non_exhaustive] |
| 82 |
pub enum KemId { |
| 83 |
DhKemP256Sha256 = 0x0010, |
| 84 |
DhKemP384Sha384 = 0x0011, |
| 85 |
DhKemP521Sha512 = 0x0012, |
| 86 |
DhKemX25519Sha256 = 0x0020, |
| 87 |
DhKemX448Sha512 = 0x0021, |
| 88 |
} |
| 89 |
|
| 90 |
impl KemId { |
| 91 |
pub fn new(cipher_suite: CipherSuite) -> Option<Self> { |
| 92 |
match cipher_suite { |
| 93 |
CipherSuite::CURVE25519_AES128 | CipherSuite::CURVE25519_CHACHA => { |
| 94 |
Some(KemId::DhKemX25519Sha256) |
| 95 |
} |
| 96 |
CipherSuite::P256_AES128 => Some(KemId::DhKemP256Sha256), |
| 97 |
CipherSuite::CURVE448_AES256 | CipherSuite::CURVE448_CHACHA => { |
| 98 |
Some(KemId::DhKemX448Sha512) |
| 99 |
} |
| 100 |
CipherSuite::P384_AES256 => Some(KemId::DhKemP384Sha384), |
| 101 |
CipherSuite::P521_AES256 => Some(KemId::DhKemP521Sha512), |
| 102 |
_ => None, |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
pub fn n_secret(&self) -> usize { |
| 107 |
match self { |
| 108 |
KemId::DhKemP256Sha256 => 32, |
| 109 |
KemId::DhKemP384Sha384 => 48, |
| 110 |
KemId::DhKemP521Sha512 => 64, |
| 111 |
KemId::DhKemX25519Sha256 => 32, |
| 112 |
KemId::DhKemX448Sha512 => 64, |
| 113 |
} |
| 114 |
} |
| 115 |
} |