-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathmessages.cpp
More file actions
169 lines (144 loc) · 5.02 KB
/
Copy pathmessages.cpp
File metadata and controls
169 lines (144 loc) · 5.02 KB
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
#include <catch2/catch_all.hpp>
#include <mls/messages.h>
#include <mls/state.h>
#include <mls_vectors/mls_vectors.h>
#include <tls/tls_syntax.h>
using namespace MLS_NAMESPACE;
using namespace mls_vectors;
TEST_CASE("Extensions")
{
auto kid0 = ApplicationIDExtension{ { 0, 1, 2, 3 } };
auto tree0 = RatchetTreeExtension{};
ExtensionList exts;
exts.add(kid0);
exts.add(tree0);
auto kid1 = exts.find<ApplicationIDExtension>();
auto tree1 = exts.find<RatchetTreeExtension>();
REQUIRE(kid0 == kid1);
REQUIRE(tree0 == tree1);
}
// TODO(RLB) Verify sign/verify on:
// * KeyPackage
// * GroupInfo
// * PublicGroupState
class MLSMessageTest
{
public:
MLSMessageTest()
{
auto hpke_priv = HPKEPrivateKey::generate(suite);
auto leaf_node = LeafNode{
suite,
hpke_priv.public_key,
sig_priv.public_key,
Credential::basic({}),
Capabilities::create_default(),
Lifetime::create_default(),
{},
sig_priv,
};
tree.add_leaf(leaf_node);
keys = GroupKeySource{ suite,
LeafCount{ 1 },
random_bytes(suite.secret_size()) };
application_content = GroupContent{
group_id, epoch, { MemberSender{ index } }, authenticated_data,
application_data,
};
proposal_content = GroupContent{
group_id,
epoch,
{ MemberSender{ index } },
authenticated_data,
Proposal{ GroupContextExtensions{} },
};
}
protected:
const bytes group_id = from_ascii("group_id");
const epoch_t epoch = 0xA0A0A0A0A0A0A0A0;
const bytes authenticated_data = from_ascii("authenticated_data");
const ApplicationData application_data{ from_ascii("application_data") };
const Proposal proposal{ GroupContextExtensions{} };
const CipherSuite suite{ CipherSuite::ID::P256_AES128GCM_SHA256_P256 };
const SignaturePrivateKey sig_priv = SignaturePrivateKey::generate(suite);
const GroupContext context{
suite,
group_id,
epoch,
random_bytes(suite.secret_size()),
random_bytes(suite.secret_size()),
{},
};
const bytes membership_key = from_ascii("membership_key");
const bytes sender_data_secret = from_ascii("sender_data_secret");
const LeafIndex index{ 0 };
const size_t padding_size = 1024;
TreeKEMPublicKey tree{ suite };
GroupKeySource keys;
GroupContent application_content;
GroupContent proposal_content;
};
TEST_CASE_METHOD(MLSMessageTest, "AuthenticatedContent Sign/Verify")
{
// Verify that a sign / verify round-trip works
auto content_auth =
AuthenticatedContent::sign(WireFormat::mls_private_message,
application_content,
suite,
sig_priv,
context);
REQUIRE(content_auth.verify(suite, sig_priv.public_key, context));
REQUIRE(content_auth.content == application_content);
// Verify that `mls_plaintext` is forbidden for ApplicationData
// NOLINTNEXTLINE(llvm-else-after-return, readability-else-after-return)
REQUIRE_THROWS(AuthenticatedContent::sign(WireFormat::mls_public_message,
application_content,
suite,
sig_priv,
context));
}
TEST_CASE_METHOD(MLSMessageTest, "PublicMessage Protect/Unprotect")
{
auto content = proposal_content;
auto content_auth_original =
AuthenticatedContent::sign(WireFormat::mls_public_message,
std::move(content),
suite,
sig_priv,
context);
auto pt = PublicMessage::protect(
content_auth_original, suite, membership_key, context);
auto val_auth_unprotected = pt.unprotect(suite, membership_key, context);
const auto& content_auth_unprotected =
opt::get(val_auth_unprotected).authenticated_content();
REQUIRE(content_auth_unprotected == content_auth_original);
}
TEST_CASE_METHOD(MLSMessageTest, "PrivateMessage Protect/Unprotect")
{
auto content = proposal_content;
auto content_auth_original =
AuthenticatedContent::sign(WireFormat::mls_private_message,
std::move(content),
suite,
sig_priv,
context);
auto ct = PrivateMessage::protect(
content_auth_original, suite, keys, sender_data_secret, padding_size);
auto val_auth_unprotected = ct.unprotect(suite, keys, sender_data_secret);
const auto& content_auth_unprotected =
opt::get(val_auth_unprotected).authenticated_content();
REQUIRE(content_auth_unprotected == content_auth_original);
}
TEST_CASE("Messages Interop")
{
auto tv = MessagesTestVector();
auto result = tv.verify();
REQUIRE(result == std::nullopt);
}
TEST_CASE("Message Protection Interop")
{
for (auto suite : all_supported_cipher_suites) {
auto tv = MessageProtectionTestVector{ suite };
REQUIRE(tv.verify() == std::nullopt);
}
}