| 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 alloc::vec::Vec; |
| 6 |
use core::{ |
| 7 |
fmt::{self, Debug}, |
| 8 |
ops::Deref, |
| 9 |
}; |
| 10 |
|
| 11 |
use mls_rs_codec::{MlsDecode, MlsEncode, MlsSize}; |
| 12 |
use mls_rs_core::{ |
| 13 |
crypto::CipherSuiteProvider, error::IntoAnyError, group::ConfirmedTranscriptHash, |
| 14 |
}; |
| 15 |
|
| 16 |
use crate::{ |
| 17 |
client::MlsError, |
| 18 |
group::{framing::FramedContent, MessageSignature}, |
| 19 |
WireFormat, |
| 20 |
}; |
| 21 |
|
| 22 |
use super::{AuthenticatedContent, ConfirmationTag}; |
| 23 |
|
| 24 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 25 |
pub(crate) async fn create<P: CipherSuiteProvider>( |
| 26 |
cipher_suite_provider: &P, |
| 27 |
interim_transcript_hash: &InterimTranscriptHash, |
| 28 |
content: &AuthenticatedContent, |
| 29 |
) -> Result<ConfirmedTranscriptHash, MlsError> { |
| 30 |
#[derive(Debug, MlsSize, MlsEncode)] |
| 31 |
struct ConfirmedTranscriptHashInput<'a> { |
| 32 |
wire_format: WireFormat, |
| 33 |
content: &'a FramedContent, |
| 34 |
signature: &'a MessageSignature, |
| 35 |
} |
| 36 |
|
| 37 |
let input = ConfirmedTranscriptHashInput { |
| 38 |
wire_format: content.wire_format, |
| 39 |
content: &content.content, |
| 40 |
signature: &content.auth.signature, |
| 41 |
}; |
| 42 |
|
| 43 |
let hash_input = [ |
| 44 |
interim_transcript_hash.deref(), |
| 45 |
input.mls_encode_to_vec()?.deref(), |
| 46 |
] |
| 47 |
.concat(); |
| 48 |
|
| 49 |
cipher_suite_provider |
| 50 |
.hash(&hash_input) |
| 51 |
.await |
| 52 |
.map(Into::into) |
| 53 |
.map_err(|e| MlsError::CryptoProviderError(e.into_any_error())) |
| 54 |
} |
| 55 |
|
| 56 |
#[derive(Clone, PartialEq, MlsSize, MlsEncode, MlsDecode)] |
| 57 |
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] |
| 58 |
pub(crate) struct InterimTranscriptHash( |
| 59 |
#[mls_codec(with = "mls_rs_codec::byte_vec")] |
| 60 |
#[cfg_attr(feature = "serde", serde(with = "mls_rs_core::vec_serde"))] |
| 61 |
Vec<u8>, |
| 62 |
); |
| 63 |
|
| 64 |
impl Debug for InterimTranscriptHash { |
| 65 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 66 |
mls_rs_core::debug::pretty_bytes(&self.0) |
| 67 |
.named("InterimTranscriptHash") |
| 68 |
.fmt(f) |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
impl Deref for InterimTranscriptHash { |
| 73 |
type Target = Vec<u8>; |
| 74 |
|
| 75 |
fn deref(&self) -> &Self::Target { |
| 76 |
&self.0 |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
impl From<Vec<u8>> for InterimTranscriptHash { |
| 81 |
fn from(value: Vec<u8>) -> Self { |
| 82 |
Self(value) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
impl InterimTranscriptHash { |
| 87 |
#[cfg_attr(not(mls_build_async), maybe_async::must_be_sync)] |
| 88 |
pub async fn create<P: CipherSuiteProvider>( |
| 89 |
cipher_suite_provider: &P, |
| 90 |
confirmed: &ConfirmedTranscriptHash, |
| 91 |
confirmation_tag: &ConfirmationTag, |
| 92 |
) -> Result<Self, MlsError> { |
| 93 |
#[derive(Debug, MlsSize, MlsEncode)] |
| 94 |
struct InterimTranscriptHashInput<'a> { |
| 95 |
confirmation_tag: &'a ConfirmationTag, |
| 96 |
} |
| 97 |
|
| 98 |
let input = InterimTranscriptHashInput { confirmation_tag }.mls_encode_to_vec()?; |
| 99 |
|
| 100 |
cipher_suite_provider |
| 101 |
.hash(&[&confirmed[..], &input].concat()) |
| 102 |
.await |
| 103 |
.map(Into::into) |
| 104 |
.map_err(|e| MlsError::CryptoProviderError(e.into_any_error())) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
// Test vectors come from the MLS interop repository and contain a proposal by reference. |
| 109 |
#[cfg(feature = "by_ref_proposal")] |
| 110 |
#[cfg(test)] |
| 111 |
mod tests { |
| 112 |
use alloc::vec::Vec; |
| 113 |
|
| 114 |
use mls_rs_codec::MlsDecode; |
| 115 |
|
| 116 |
use crate::{ |
| 117 |
crypto::test_utils::try_test_cipher_suite_provider, |
| 118 |
group::{framing::ContentType, message_signature::AuthenticatedContent, transcript_hashes}, |
| 119 |
}; |
| 120 |
|
| 121 |
#[cfg(not(mls_build_async))] |
| 122 |
use alloc::{boxed::Box, vec}; |
| 123 |
|
| 124 |
#[cfg(not(mls_build_async))] |
| 125 |
use crate::{ |
| 126 |
crypto::test_utils::test_cipher_suite_provider, |
| 127 |
group::{ |
| 128 |
confirmation_tag::ConfirmationTag, |
| 129 |
framing::Content, |
| 130 |
proposal::{Proposal, ProposalOrRef, RemoveProposal}, |
| 131 |
test_utils::get_test_group_context, |
| 132 |
Commit, LeafIndex, Sender, |
| 133 |
}, |
| 134 |
mls_rs_codec::MlsEncode, |
| 135 |
CipherSuite, CipherSuiteProvider, WireFormat, |
| 136 |
}; |
| 137 |
|
| 138 |
#[cfg(not(mls_build_async))] |
| 139 |
use super::InterimTranscriptHash; |
| 140 |
|
| 141 |
#[derive(serde::Serialize, serde::Deserialize, Debug, Default, Clone)] |
| 142 |
struct TestCase { |
| 143 |
pub cipher_suite: u16, |
| 144 |
|
| 145 |
#[serde(with = "hex::serde")] |
| 146 |
pub confirmation_key: Vec<u8>, |
| 147 |
#[serde(with = "hex::serde")] |
| 148 |
pub authenticated_content: Vec<u8>, |
| 149 |
#[serde(with = "hex::serde")] |
| 150 |
pub interim_transcript_hash_before: Vec<u8>, |
| 151 |
|
| 152 |
#[serde(with = "hex::serde")] |
| 153 |
pub confirmed_transcript_hash_after: Vec<u8>, |
| 154 |
#[serde(with = "hex::serde")] |
| 155 |
pub interim_transcript_hash_after: Vec<u8>, |
| 156 |
} |
| 157 |
|
| 158 |
#[maybe_async::test(not(mls_build_async), async(mls_build_async, crate::futures_test))] |
| 159 |
async fn transcript_hash() { |
| 160 |
let test_cases: Vec<TestCase> = |
| 161 |
load_test_case_json!(interop_transcript_hashes, generate_test_vector()); |
| 162 |
|
| 163 |
for test_case in test_cases.into_iter() { |
| 164 |
let Some(cs) = try_test_cipher_suite_provider(test_case.cipher_suite) else { |
| 165 |
continue; |
| 166 |
}; |
| 167 |
|
| 168 |
let auth_content = |
| 169 |
AuthenticatedContent::mls_decode(&mut &*test_case.authenticated_content).unwrap(); |
| 170 |
|
| 171 |
assert!(auth_content.content.content_type() == ContentType::Commit); |
| 172 |
|
| 173 |
let conf_key = &test_case.confirmation_key; |
| 174 |
let conf_hash_after = test_case.confirmed_transcript_hash_after.into(); |
| 175 |
let conf_tag = auth_content.auth.confirmation_tag.clone().unwrap(); |
| 176 |
|
| 177 |
let matches = conf_tag |
| 178 |
.matches(conf_key, &conf_hash_after, &cs) |
| 179 |
.await |
| 180 |
.unwrap(); |
| 181 |
|
| 182 |
assert!(matches); |
| 183 |
|
| 184 |
let (expected_interim, expected_conf) = transcript_hashes( |
| 185 |
&cs, |
| 186 |
&test_case.interim_transcript_hash_before.into(), |
| 187 |
&auth_content, |
| 188 |
) |
| 189 |
.await |
| 190 |
.unwrap(); |
| 191 |
|
| 192 |
assert_eq!(*expected_interim, test_case.interim_transcript_hash_after); |
| 193 |
assert_eq!(expected_conf, conf_hash_after); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
#[cfg(not(mls_build_async))] |
| 198 |
#[cfg_attr(coverage_nightly, coverage(off))] |
| 199 |
fn generate_test_vector() -> Vec<TestCase> { |
| 200 |
CipherSuite::all().fold(vec![], |mut test_cases, cs| { |
| 201 |
let cs = test_cipher_suite_provider(cs); |
| 202 |
|
| 203 |
let context = get_test_group_context(0x3456, cs.cipher_suite()); |
| 204 |
|
| 205 |
let proposal = Proposal::Remove(RemoveProposal { |
| 206 |
to_remove: LeafIndex(1), |
| 207 |
}); |
| 208 |
|
| 209 |
let proposal = ProposalOrRef::Proposal(Box::new(proposal)); |
| 210 |
|
| 211 |
let commit = Commit { |
| 212 |
proposals: vec![proposal], |
| 213 |
path: None, |
| 214 |
}; |
| 215 |
|
| 216 |
let signer = cs.signature_key_generate().unwrap().0; |
| 217 |
|
| 218 |
let mut auth_content = AuthenticatedContent::new_signed( |
| 219 |
&cs, |
| 220 |
&context, |
| 221 |
Sender::Member(0), |
| 222 |
Content::Commit(alloc::boxed::Box::new(commit)), |
| 223 |
&signer, |
| 224 |
WireFormat::PublicMessage, |
| 225 |
vec![], |
| 226 |
) |
| 227 |
.unwrap(); |
| 228 |
|
| 229 |
let interim_hash_before = cs.random_bytes_vec(cs.kdf_extract_size()).unwrap().into(); |
| 230 |
let conf_hash_after = super::create(&cs, &interim_hash_before, &auth_content).unwrap(); |
| 231 |
|
| 232 |
let conf_key = cs.random_bytes_vec(cs.kdf_extract_size()).unwrap(); |
| 233 |
let conf_tag = ConfirmationTag::create(&conf_key, &conf_hash_after, &cs).unwrap(); |
| 234 |
|
| 235 |
let interim_hash_after = |
| 236 |
InterimTranscriptHash::create(&cs, &conf_hash_after, &conf_tag).unwrap(); |
| 237 |
|
| 238 |
auth_content.auth.confirmation_tag = Some(conf_tag); |
| 239 |
|
| 240 |
let test_case = TestCase { |
| 241 |
cipher_suite: cs.cipher_suite().into(), |
| 242 |
|
| 243 |
confirmation_key: conf_key, |
| 244 |
authenticated_content: auth_content.mls_encode_to_vec().unwrap(), |
| 245 |
interim_transcript_hash_before: interim_hash_before.0, |
| 246 |
|
| 247 |
confirmed_transcript_hash_after: conf_hash_after.to_vec(), |
| 248 |
interim_transcript_hash_after: interim_hash_after.to_vec(), |
| 249 |
}; |
| 250 |
|
| 251 |
test_cases.push(test_case); |
| 252 |
test_cases |
| 253 |
}) |
| 254 |
} |
| 255 |
|
| 256 |
#[cfg(mls_build_async)] |
| 257 |
fn generate_test_vector() -> Vec<TestCase> { |
| 258 |
panic!("Tests cannot be generated in async mode"); |
| 259 |
} |
| 260 |
} |