| 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 core::ops::Deref; |
| 6 |
|
| 7 |
use alloc::vec::Vec; |
| 8 |
|
| 9 |
use mls_rs_core::{ |
| 10 |
crypto::{CipherSuite, HpkePublicKey, HpkeSecretKey}, |
| 11 |
error::IntoAnyError, |
| 12 |
}; |
| 13 |
use mls_rs_crypto_traits::{Curve, DhType, SamplingMethod}; |
| 14 |
|
| 15 |
use crate::ec::{ |
| 16 |
generate_keypair, private_key_bytes_to_public, private_key_ecdh, private_key_from_bytes, |
| 17 |
pub_key_from_uncompressed, EcError, EcPublicKey, |
| 18 |
}; |
| 19 |
|
| 20 |
#[derive(Debug)] |
| 21 |
#[cfg_attr(feature = "std", derive(thiserror::Error))] |
| 22 |
pub enum EcdhKemError { |
| 23 |
#[cfg_attr(feature = "std", error(transparent))] |
| 24 |
EcError(EcError), |
| 25 |
#[cfg_attr(feature = "std", error("unsupported cipher suite"))] |
| 26 |
UnsupportedCipherSuite, |
| 27 |
} |
| 28 |
|
| 29 |
impl From<EcError> for EcdhKemError { |
| 30 |
fn from(e: EcError) -> Self { |
| 31 |
EcdhKemError::EcError(e) |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
impl IntoAnyError for EcdhKemError { |
| 36 |
#[cfg(feature = "std")] |
| 37 |
fn into_dyn_error(self) -> Result<Box<dyn std::error::Error + Send + Sync>, Self> { |
| 38 |
Ok(self.into()) |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
#[derive(Clone, Debug, Eq, PartialEq)] |
| 43 |
pub struct Ecdh(Curve); |
| 44 |
|
| 45 |
impl Deref for Ecdh { |
| 46 |
type Target = Curve; |
| 47 |
|
| 48 |
fn deref(&self) -> &Self::Target { |
| 49 |
&self.0 |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
impl Ecdh { |
| 54 |
pub fn new(cipher_suite: CipherSuite) -> Option<Self> { |
| 55 |
Curve::from_ciphersuite(cipher_suite, false).map(Self) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 60 |
#[cfg_attr(all(target_arch = "wasm32", mls_build_async), maybe_async::must_be_async(?Send))] |
| 61 |
#[cfg_attr( |
| 62 |
all(not(target_arch = "wasm32"), mls_build_async), |
| 63 |
maybe_async::must_be_async |
| 64 |
)] |
| 65 |
impl DhType for Ecdh { |
| 66 |
type Error = EcdhKemError; |
| 67 |
|
| 68 |
async fn dh( |
| 69 |
&self, |
| 70 |
secret_key: &HpkeSecretKey, |
| 71 |
public_key: &HpkePublicKey, |
| 72 |
) -> Result<Vec<u8>, Self::Error> { |
| 73 |
Ok(private_key_ecdh( |
| 74 |
&private_key_from_bytes(secret_key.to_vec(), self.0)?, |
| 75 |
&self.to_ec_public_key(public_key)?, |
| 76 |
)?) |
| 77 |
} |
| 78 |
async fn to_public(&self, secret_key: &HpkeSecretKey) -> Result<HpkePublicKey, Self::Error> { |
| 79 |
Ok(private_key_bytes_to_public(secret_key.to_vec(), self.0)?.into()) |
| 80 |
} |
| 81 |
|
| 82 |
async fn generate(&self) -> Result<(HpkeSecretKey, HpkePublicKey), Self::Error> { |
| 83 |
let key_pair = generate_keypair(self.0)?; |
| 84 |
Ok((key_pair.secret.into(), key_pair.public.into())) |
| 85 |
} |
| 86 |
|
| 87 |
fn bitmask_for_rejection_sampling(&self) -> SamplingMethod { |
| 88 |
self.0.hpke_sampling_method() |
| 89 |
} |
| 90 |
|
| 91 |
fn public_key_validate(&self, key: &HpkePublicKey) -> Result<(), Self::Error> { |
| 92 |
self.to_ec_public_key(key).map(|_| ()) |
| 93 |
} |
| 94 |
|
| 95 |
fn secret_key_size(&self) -> usize { |
| 96 |
self.0.secret_key_size() |
| 97 |
} |
| 98 |
|
| 99 |
fn public_key_size(&self) -> usize { |
| 100 |
self.0.public_key_size() |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
impl Ecdh { |
| 105 |
fn to_ec_public_key(&self, public_key: &HpkePublicKey) -> Result<EcPublicKey, EcdhKemError> { |
| 106 |
Ok(pub_key_from_uncompressed(public_key.to_vec(), self.0)?) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
#[cfg(all(test, not(mls_build_async)))] |
| 111 |
mod test { |
| 112 |
use mls_rs_core::crypto::{CipherSuite, HpkePublicKey, HpkeSecretKey}; |
| 113 |
use mls_rs_crypto_traits::DhType; |
| 114 |
use serde::Deserialize; |
| 115 |
|
| 116 |
use alloc::vec::Vec; |
| 117 |
|
| 118 |
use crate::ecdh::Ecdh; |
| 119 |
|
| 120 |
#[allow(dead_code)] |
| 121 |
fn get_ecdhs() -> Vec<Ecdh> { |
| 122 |
[CipherSuite::P256_AES128, CipherSuite::CURVE25519_AES128] |
| 123 |
.into_iter() |
| 124 |
.map(|c| Ecdh::new(c).unwrap()) |
| 125 |
.collect() |
| 126 |
} |
| 127 |
|
| 128 |
#[derive(Deserialize)] |
| 129 |
struct TestCase { |
| 130 |
pub ciphersuite: u16, |
| 131 |
#[serde(with = "hex::serde")] |
| 132 |
pub alice_pub: Vec<u8>, |
| 133 |
#[serde(with = "hex::serde")] |
| 134 |
pub alice_pri: Vec<u8>, |
| 135 |
#[serde(with = "hex::serde")] |
| 136 |
pub bob_pub: Vec<u8>, |
| 137 |
#[serde(with = "hex::serde")] |
| 138 |
pub bob_pri: Vec<u8>, |
| 139 |
#[serde(with = "hex::serde")] |
| 140 |
pub shared_secret: Vec<u8>, |
| 141 |
} |
| 142 |
|
| 143 |
fn run_test_case(test_case: TestCase) { |
| 144 |
let ecdh = Ecdh::new(test_case.ciphersuite.into()).unwrap(); |
| 145 |
|
| 146 |
// Import the keys into their structures |
| 147 |
let alice_pub: HpkePublicKey = test_case.alice_pub.into(); |
| 148 |
let bob_pub: HpkePublicKey = test_case.bob_pub.into(); |
| 149 |
let alice_pri: HpkeSecretKey = test_case.alice_pri.into(); |
| 150 |
let bob_pri: HpkeSecretKey = test_case.bob_pri.into(); |
| 151 |
|
| 152 |
assert_eq!(ecdh.to_public(&alice_pri).unwrap(), alice_pub); |
| 153 |
assert_eq!(ecdh.to_public(&bob_pri).unwrap(), bob_pub); |
| 154 |
|
| 155 |
assert_eq!( |
| 156 |
ecdh.dh(&alice_pri, &bob_pub).unwrap(), |
| 157 |
test_case.shared_secret |
| 158 |
); |
| 159 |
|
| 160 |
assert_eq!( |
| 161 |
ecdh.dh(&bob_pri, &alice_pub).unwrap(), |
| 162 |
test_case.shared_secret |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
#[test] |
| 167 |
fn test_algo_test_cases() { |
| 168 |
let test_case_file = include_str!("../test_data/test_ecdh.json"); |
| 169 |
let test_cases: Vec<TestCase> = serde_json::from_str(test_case_file).unwrap(); |
| 170 |
|
| 171 |
for case in test_cases { |
| 172 |
run_test_case(case); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
// TODO: discuss if we need this test |
| 177 |
// #[test] |
| 178 |
// fn test_mismatched_curve() { |
| 179 |
// for ecdh in get_ecdhs() { |
| 180 |
// let secret_key = ecdh.generate().unwrap().0; |
| 181 |
|
| 182 |
// for other_ecdh in get_ecdhs().into_iter().filter(|c| c != &ecdh) { |
| 183 |
// let other_public_key = other_ecdh.generate().unwrap().1; |
| 184 |
// assert!(ecdh.dh(&secret_key, &other_public_key).is_err()); |
| 185 |
// } |
| 186 |
// } |
| 187 |
// } |
| 188 |
} |