Debian Codegraph @

mlspp/include/mls/crypto.h

raw symbols SCIP
1 #pragma once
2
3 #include <hpke/digest.h>
4 #include <hpke/hpke.h>
5 #include <hpke/random.h>
6 #include <hpke/signature.h>
7 #include <mls/common.h>
8 #include <tls/tls_syntax.h>
9 #include <vector>
10
11 namespace mlspp {
12
13 /// Signature Code points, borrowed from RFC 8446
14 enum struct SignatureScheme : uint16_t
15 {
16 ecdsa_secp256r1_sha256 = 0x0403,
17 ecdsa_secp384r1_sha384 = 0x0805,
18 ecdsa_secp521r1_sha512 = 0x0603,
19 ed25519 = 0x0807,
20 ed448 = 0x0808,
21 rsa_pkcs1_sha256 = 0x0401,
22 };
23
24 SignatureScheme
25 tls_signature_scheme(hpke::Signature::ID id);
26
27 /// Cipher suites
28
29 struct KeyAndNonce
30 {
31 bytes key;
32 bytes nonce;
33 };
34
35 // opaque HashReference<V>;
36 // HashReference KeyPackageRef;
37 // HashReference ProposalRef;
38 using HashReference = bytes;
39 using KeyPackageRef = HashReference;
40 using ProposalRef = HashReference;
41
42 struct CipherSuite
43 {
44 enum struct ID : uint16_t
45 {
46 unknown = 0x0000,
47 X25519_AES128GCM_SHA256_Ed25519 = 0x0001,
48 P256_AES128GCM_SHA256_P256 = 0x0002,
49 X25519_CHACHA20POLY1305_SHA256_Ed25519 = 0x0003,
50 X448_AES256GCM_SHA512_Ed448 = 0x0004,
51 P521_AES256GCM_SHA512_P521 = 0x0005,
52 X448_CHACHA20POLY1305_SHA512_Ed448 = 0x0006,
53 P384_AES256GCM_SHA384_P384 = 0x0007,
54
55 // GREASE values, included here mainly so that debugger output looks nice
56 GREASE_0 = 0x0A0A,
57 GREASE_1 = 0x1A1A,
58 GREASE_2 = 0x2A2A,
59 GREASE_3 = 0x3A3A,
60 GREASE_4 = 0x4A4A,
61 GREASE_5 = 0x5A5A,
62 GREASE_6 = 0x6A6A,
63 GREASE_7 = 0x7A7A,
64 GREASE_8 = 0x8A8A,
65 GREASE_9 = 0x9A9A,
66 GREASE_A = 0xAAAA,
67 GREASE_B = 0xBABA,
68 GREASE_C = 0xCACA,
69 GREASE_D = 0xDADA,
70 GREASE_E = 0xEAEA,
71 };
72
73 CipherSuite();
74 CipherSuite(ID id_in);
75
76 ID cipher_suite() const { return id; }
77 SignatureScheme signature_scheme() const;
78
79 size_t secret_size() const { return get().digest.hash_size; }
80 size_t key_size() const { return get().hpke.aead.key_size; }
81 size_t nonce_size() const { return get().hpke.aead.nonce_size; }
82
83 bytes zero() const { return bytes(secret_size(), 0); }
84 const hpke::HPKE& hpke() const { return get().hpke; }
85 const hpke::Digest& digest() const { return get().digest; }
86 const hpke::Signature& sig() const { return get().sig; }
87
88 bytes expand_with_label(const bytes& secret,
89 const std::string& label,
90 const bytes& context,
91 size_t length) const;
92 bytes derive_secret(const bytes& secret, const std::string& label) const;
93 bytes derive_tree_secret(const bytes& secret,
94 const std::string& label,
95 uint32_t generation,
96 size_t length) const;
97
98 template<typename T>
99 bytes ref(const T& value) const
100 {
101 return raw_ref(reference_label<T>(), tls::marshal(value));
102 }
103
104 bytes raw_ref(const bytes& label, const bytes& value) const
105 {
106 // RefHash(label, value) = Hash(RefHashInput)
107 //
108 // struct {
109 // opaque label<V>;
110 // opaque value<V>;
111 // } RefHashInput;
112 auto w = tls::ostream();
113 w << label << value;
114 return digest().hash(w.bytes());
115 }
116
117 TLS_SERIALIZABLE(id)
118
119 private:
120 ID id;
121
122 struct Ciphers
123 {
124 hpke::HPKE hpke;
125 const hpke::Digest& digest;
126 const hpke::Signature& sig;
127 };
128
129 const Ciphers& get() const;
130
131 template<typename T>
132 static const bytes& reference_label();
133 };
134
135 #if WITH_BORINGSSL
136 extern const std::array<CipherSuite::ID, 5> all_supported_suites;
137 #else
138 extern const std::array<CipherSuite::ID, 7> all_supported_suites;
139 #endif
140
141 // Utilities
142 using mlspp::hpke::random_bytes;
143
144 // HPKE Keys
145 namespace encrypt_label {
146 extern const std::string update_path_node;
147 extern const std::string welcome;
148 } // namespace encrypt_label
149
150 struct HPKECiphertext
151 {
152 bytes kem_output;
153 bytes ciphertext;
154
155 TLS_SERIALIZABLE(kem_output, ciphertext)
156 };
157
158 struct HPKEPublicKey
159 {
160 bytes data;
161
162 HPKECiphertext encrypt(CipherSuite suite,
163 const std::string& label,
164 const bytes& context,
165 const bytes& pt) const;
166
167 std::tuple<bytes, bytes> do_export(CipherSuite suite,
168 const bytes& info,
169 const std::string& label,
170 size_t size) const;
171
172 TLS_SERIALIZABLE(data)
173 };
174
175 struct HPKEPrivateKey
176 {
177 static HPKEPrivateKey generate(CipherSuite suite);
178 static HPKEPrivateKey parse(CipherSuite suite, const bytes& data);
179 static HPKEPrivateKey derive(CipherSuite suite, const bytes& secret);
180
181 HPKEPrivateKey() = default;
182
183 bytes data;
184 HPKEPublicKey public_key;
185
186 bytes decrypt(CipherSuite suite,
187 const std::string& label,
188 const bytes& context,
189 const HPKECiphertext& ct) const;
190
191 bytes do_export(CipherSuite suite,
192 const bytes& info,
193 const bytes& kem_output,
194 const std::string& label,
195 size_t size) const;
196
197 void set_public_key(CipherSuite suite);
198
199 TLS_SERIALIZABLE(data)
200
201 private:
202 HPKEPrivateKey(bytes priv_data, bytes pub_data);
203 };
204
205 // Signature Keys
206 namespace sign_label {
207 extern const std::string mls_content;
208 extern const std::string leaf_node;
209 extern const std::string key_package;
210 extern const std::string group_info;
211 extern const std::string multi_credential;
212 } // namespace sign_label
213
214 struct SignaturePublicKey
215 {
216 static SignaturePublicKey from_jwk(CipherSuite suite,
217 const std::string& json_str);
218
219 bytes data;
220
221 bool verify(const CipherSuite& suite,
222 const std::string& label,
223 const bytes& message,
224 const bytes& signature) const;
225
226 std::string to_jwk(CipherSuite suite) const;
227
228 TLS_SERIALIZABLE(data)
229 };
230
231 struct PublicJWK
232 {
233 SignatureScheme signature_scheme;
234 std::optional<std::string> key_id;
235 SignaturePublicKey public_key;
236
237 static PublicJWK parse(const std::string& jwk_json);
238 };
239
240 struct SignaturePrivateKey
241 {
242 static SignaturePrivateKey generate(CipherSuite suite);
243 static SignaturePrivateKey parse(CipherSuite suite, const bytes& data);
244 static SignaturePrivateKey derive(CipherSuite suite, const bytes& secret);
245 static SignaturePrivateKey from_jwk(CipherSuite suite,
246 const std::string& json_str);
247
248 SignaturePrivateKey() = default;
249
250 bytes data;
251 SignaturePublicKey public_key;
252
253 bytes sign(const CipherSuite& suite,
254 const std::string& label,
255 const bytes& message) const;
256
257 void set_public_key(CipherSuite suite);
258 std::string to_jwk(CipherSuite suite) const;
259
260 TLS_SERIALIZABLE(data)
261
262 private:
263 SignaturePrivateKey(bytes priv_data, bytes pub_data);
264 };
265
266 } // namespace mlspp