Skip to content

Commit f400736

Browse files
committed
Clean up headers
1 parent 81d6a05 commit f400736

File tree

12 files changed

+296
-313
lines changed

12 files changed

+296
-313
lines changed

include/mls/common.h

Lines changed: 0 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
#include <bytes/bytes.h>
1010
using namespace bytes_ns;
1111

12-
#include <hpke/digest.h>
13-
#include <hpke/hpke.h>
14-
#include <hpke/signature.h>
15-
1612
#include <tls/tls_syntax.h>
1713

1814
namespace mls {
@@ -44,65 +40,6 @@ operator!=(const T& lhs, const T& rhs)
4440
return lhs._tls_fields_w() != rhs._tls_fields_w();
4541
}
4642

47-
///
48-
/// Cipher suites
49-
///
50-
struct CipherSuite
51-
{
52-
enum struct ID : uint16_t
53-
{
54-
unknown = 0x0000,
55-
X25519_AES128GCM_SHA256_Ed25519 = 0x0001,
56-
P256_AES128GCM_SHA256_P256 = 0x0002,
57-
X25519_CHACHA20POLY1305_SHA256_Ed25519 = 0x0003,
58-
X448_AES256GCM_SHA512_Ed448 = 0x0004,
59-
P521_AES256GCM_SHA512_P521 = 0x0005,
60-
X448_CHACHA20POLY1305_SHA512_Ed448 = 0x0006,
61-
};
62-
63-
CipherSuite();
64-
CipherSuite(ID id_in);
65-
CipherSuite(const CipherSuite& other);
66-
CipherSuite(CipherSuite&& other);
67-
CipherSuite& operator=(const CipherSuite& other);
68-
69-
ID id;
70-
std::unique_ptr<hpke::HPKE> hpke;
71-
std::unique_ptr<hpke::Digest> digest;
72-
std::unique_ptr<hpke::Signature> sig;
73-
74-
bytes expand_with_label(const bytes& secret,
75-
const std::string& label,
76-
const bytes& context,
77-
size_t size) const;
78-
79-
private:
80-
void reset(ID id_in);
81-
};
82-
83-
tls::istream&
84-
operator>>(tls::istream& str, CipherSuite& suite);
85-
tls::ostream&
86-
operator<<(tls::ostream& str, const CipherSuite& suite);
87-
bool
88-
operator==(const CipherSuite& lhs, const CipherSuite& rhs);
89-
bool
90-
operator!=(const CipherSuite& lhs, const CipherSuite& rhs);
91-
92-
enum struct SignatureScheme : uint16_t
93-
{
94-
unknown = 0x0000,
95-
P256_SHA256 = 0x0403,
96-
P521_SHA512 = 0x0603,
97-
Ed25519 = 0x0807,
98-
Ed448 = 0x0808,
99-
};
100-
101-
SignatureScheme
102-
scheme_for_suite(CipherSuite::ID id);
103-
104-
extern const std::array<CipherSuite::ID, 6> all_supported_suites;
105-
10643
///
10744
/// Error types
10845
///

include/mls/credential.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ enum struct CredentialType : uint8_t
1818

1919
// struct {
2020
// opaque identity<0..2^16-1>;
21-
// SignatureScheme algorithm;
2221
// SignaturePublicKey public_key;
2322
// } BasicCredential;
2423
struct BasicCredential

include/mls/crypto.h

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,67 @@
11
#pragma once
22

3-
#include "mls/common.h"
3+
#include <mls/common.h>
44
#include <openssl/evp.h>
55
#include <tls/tls_syntax.h>
6+
#include <hpke/digest.h>
7+
#include <hpke/hpke.h>
8+
#include <hpke/signature.h>
9+
#include <hpke/random.h>
610
#include <vector>
711

812
namespace mls {
913

14+
/// Cipher suites
15+
struct CipherSuite
16+
{
17+
enum struct ID : uint16_t
18+
{
19+
unknown = 0x0000,
20+
X25519_AES128GCM_SHA256_Ed25519 = 0x0001,
21+
P256_AES128GCM_SHA256_P256 = 0x0002,
22+
X25519_CHACHA20POLY1305_SHA256_Ed25519 = 0x0003,
23+
X448_AES256GCM_SHA512_Ed448 = 0x0004,
24+
P521_AES256GCM_SHA512_P521 = 0x0005,
25+
X448_CHACHA20POLY1305_SHA512_Ed448 = 0x0006,
26+
};
27+
28+
CipherSuite();
29+
CipherSuite(ID id_in);
30+
CipherSuite(const CipherSuite& other);
31+
CipherSuite(CipherSuite&& other);
32+
CipherSuite& operator=(const CipherSuite& other);
33+
34+
ID id;
35+
std::unique_ptr<hpke::HPKE> hpke;
36+
std::unique_ptr<hpke::Digest> digest;
37+
std::unique_ptr<hpke::Signature> sig;
38+
39+
bytes expand_with_label(const bytes& secret,
40+
const std::string& label,
41+
const bytes& context,
42+
size_t size) const;
43+
44+
private:
45+
void reset(ID id_in);
46+
};
47+
48+
tls::istream&
49+
operator>>(tls::istream& str, CipherSuite& suite);
50+
51+
tls::ostream&
52+
operator<<(tls::ostream& str, const CipherSuite& suite);
53+
54+
bool
55+
operator==(const CipherSuite& lhs, const CipherSuite& rhs);
56+
57+
bool
58+
operator!=(const CipherSuite& lhs, const CipherSuite& rhs);
59+
60+
extern const std::array<CipherSuite::ID, 6> all_supported_suites;
61+
62+
// Utilities
63+
using hpke::random_bytes;
64+
1065
bool
1166
constant_time_eq(const bytes& lhs, const bytes& rhs);
1267

src/common.cpp

Lines changed: 0 additions & 225 deletions
Original file line numberDiff line numberDiff line change
@@ -10,229 +10,4 @@ seconds_since_epoch()
1010
return std::time(nullptr);
1111
}
1212

13-
///
14-
/// CipherSuites and details
15-
///
16-
17-
using hpke::AEAD;
18-
using hpke::Digest;
19-
using hpke::HPKE;
20-
using hpke::KDF;
21-
using hpke::KEM;
22-
using hpke::Signature;
23-
24-
struct CipherAlgorithms
25-
{
26-
KEM::ID kem_id;
27-
KDF::ID kdf_id;
28-
AEAD::ID aead_id;
29-
Digest::ID digest_id;
30-
Signature::ID sig_id;
31-
SignatureScheme scheme;
32-
};
33-
34-
template<CipherSuite::ID CS>
35-
extern const CipherAlgorithms cipher_algs;
36-
37-
template<>
38-
const CipherAlgorithms
39-
cipher_algs<CipherSuite::ID::X25519_AES128GCM_SHA256_Ed25519>{
40-
KEM::ID::DHKEM_X25519_SHA256, KDF::ID::HKDF_SHA256,
41-
AEAD::ID::AES_128_GCM, Digest::ID::SHA256,
42-
Signature::ID::Ed25519, SignatureScheme::Ed25519,
43-
};
44-
45-
template<>
46-
const CipherAlgorithms cipher_algs<CipherSuite::ID::P256_AES128GCM_SHA256_P256>{
47-
KEM::ID::DHKEM_P256_SHA256, KDF::ID::HKDF_SHA256,
48-
AEAD::ID::AES_128_GCM, Digest::ID::SHA256,
49-
Signature::ID::P256_SHA256, SignatureScheme::P256_SHA256,
50-
};
51-
52-
template<>
53-
const CipherAlgorithms
54-
cipher_algs<CipherSuite::ID::X25519_CHACHA20POLY1305_SHA256_Ed25519>{
55-
KEM::ID::DHKEM_X25519_SHA256, KDF::ID::HKDF_SHA256,
56-
AEAD::ID::CHACHA20_POLY1305, Digest::ID::SHA256,
57-
Signature::ID::Ed25519, SignatureScheme::Ed25519,
58-
};
59-
60-
template<>
61-
const CipherAlgorithms
62-
cipher_algs<CipherSuite::ID::X448_AES256GCM_SHA512_Ed448>{
63-
KEM::ID::DHKEM_X448_SHA512, KDF::ID::HKDF_SHA512, AEAD::ID::AES_256_GCM,
64-
Digest::ID::SHA512, Signature::ID::Ed448, SignatureScheme::Ed448,
65-
};
66-
67-
template<>
68-
const CipherAlgorithms cipher_algs<CipherSuite::ID::P521_AES256GCM_SHA512_P521>{
69-
KEM::ID::DHKEM_P521_SHA512, KDF::ID::HKDF_SHA512,
70-
AEAD::ID::AES_256_GCM, Digest::ID::SHA512,
71-
Signature::ID::P521_SHA512, SignatureScheme::P521_SHA512,
72-
};
73-
74-
template<>
75-
const CipherAlgorithms
76-
cipher_algs<CipherSuite::ID::X448_CHACHA20POLY1305_SHA512_Ed448>{
77-
KEM::ID::DHKEM_X448_SHA512, KDF::ID::HKDF_SHA512,
78-
AEAD::ID::CHACHA20_POLY1305, Digest::ID::SHA512,
79-
Signature::ID::Ed448, SignatureScheme::Ed448,
80-
};
81-
82-
static const CipherAlgorithms&
83-
algs_for_suite(CipherSuite::ID id)
84-
{
85-
switch (id) {
86-
case CipherSuite::ID::X25519_AES128GCM_SHA256_Ed25519:
87-
return cipher_algs<CipherSuite::ID::X25519_AES128GCM_SHA256_Ed25519>;
88-
89-
case CipherSuite::ID::P256_AES128GCM_SHA256_P256:
90-
return cipher_algs<CipherSuite::ID::P256_AES128GCM_SHA256_P256>;
91-
92-
case CipherSuite::ID::X25519_CHACHA20POLY1305_SHA256_Ed25519:
93-
return cipher_algs<
94-
CipherSuite::ID::X25519_CHACHA20POLY1305_SHA256_Ed25519>;
95-
96-
case CipherSuite::ID::X448_AES256GCM_SHA512_Ed448:
97-
return cipher_algs<CipherSuite::ID::X448_AES256GCM_SHA512_Ed448>;
98-
99-
case CipherSuite::ID::P521_AES256GCM_SHA512_P521:
100-
return cipher_algs<CipherSuite::ID::P521_AES256GCM_SHA512_P521>;
101-
102-
case CipherSuite::ID::X448_CHACHA20POLY1305_SHA512_Ed448:
103-
return cipher_algs<CipherSuite::ID::X448_CHACHA20POLY1305_SHA512_Ed448>;
104-
105-
default:
106-
throw InvalidParameterError("Unsupported ciphersuite");
107-
}
108-
}
109-
110-
static std::unique_ptr<HPKE>
111-
hpke_for_suite(CipherSuite::ID id)
112-
{
113-
const auto& algs = algs_for_suite(id);
114-
return std::make_unique<HPKE>(algs.kem_id, algs.kdf_id, algs.aead_id);
115-
}
116-
117-
static std::unique_ptr<Digest>
118-
digest_for_suite(CipherSuite::ID id)
119-
{
120-
return Digest::create(algs_for_suite(id).digest_id);
121-
}
122-
123-
static std::unique_ptr<Signature>
124-
sig_for_suite(CipherSuite::ID id)
125-
{
126-
return Signature::create(algs_for_suite(id).sig_id);
127-
}
128-
129-
SignatureScheme
130-
scheme_for_suite(CipherSuite::ID id)
131-
{
132-
return algs_for_suite(id).scheme;
133-
}
134-
135-
CipherSuite::CipherSuite()
136-
: id(CipherSuite::ID::unknown)
137-
{}
138-
139-
CipherSuite::CipherSuite(ID id_in)
140-
: id(id_in)
141-
{
142-
reset(id);
143-
}
144-
145-
CipherSuite::CipherSuite(const CipherSuite& other)
146-
: id(other.id)
147-
{
148-
reset(id);
149-
}
150-
151-
CipherSuite::CipherSuite(CipherSuite&& other)
152-
: id(other.id)
153-
, hpke(std::move(other.hpke))
154-
, digest(std::move(other.digest))
155-
, sig(std::move(other.sig))
156-
{}
157-
158-
CipherSuite&
159-
CipherSuite::operator=(const CipherSuite& other)
160-
{
161-
if (this != &other) {
162-
reset(other.id);
163-
}
164-
return *this;
165-
}
166-
167-
struct HKDFLabel
168-
{
169-
uint16_t length;
170-
bytes label;
171-
bytes context;
172-
173-
TLS_SERIALIZABLE(length, label, context)
174-
TLS_TRAITS(tls::pass, tls::vector<1>, tls::vector<4>)
175-
};
176-
177-
bytes
178-
CipherSuite::expand_with_label(const bytes& secret,
179-
const std::string& label,
180-
const bytes& context,
181-
size_t length) const
182-
{
183-
auto mls_label = to_bytes(std::string("mls10 ") + label);
184-
auto length16 = static_cast<uint16_t>(length);
185-
auto label_bytes = tls::marshal(HKDFLabel{ length16, mls_label, context });
186-
return hpke->kdf->expand(secret, label_bytes, length);
187-
}
188-
189-
void
190-
CipherSuite::reset(ID id_in)
191-
{
192-
if (id_in == ID::unknown) {
193-
return;
194-
}
195-
196-
id = id_in;
197-
hpke = hpke_for_suite(id);
198-
digest = digest_for_suite(id);
199-
sig = sig_for_suite(id);
200-
}
201-
202-
tls::istream&
203-
operator>>(tls::istream& str, CipherSuite& suite)
204-
{
205-
CipherSuite::ID id;
206-
str >> id;
207-
suite = CipherSuite(id);
208-
return str;
209-
}
210-
211-
tls::ostream&
212-
operator<<(tls::ostream& str, const CipherSuite& suite)
213-
{
214-
return str << suite.id;
215-
}
216-
217-
bool
218-
operator==(const CipherSuite& lhs, const CipherSuite& rhs)
219-
{
220-
return lhs.id == rhs.id;
221-
}
222-
223-
bool
224-
operator!=(const CipherSuite& lhs, const CipherSuite& rhs)
225-
{
226-
return lhs.id != rhs.id;
227-
}
228-
229-
const std::array<CipherSuite::ID, 6> all_supported_suites = {
230-
CipherSuite::ID::X25519_AES128GCM_SHA256_Ed25519,
231-
CipherSuite::ID::P256_AES128GCM_SHA256_P256,
232-
CipherSuite::ID::X25519_CHACHA20POLY1305_SHA256_Ed25519,
233-
CipherSuite::ID::X448_AES256GCM_SHA512_Ed448,
234-
CipherSuite::ID::P521_AES256GCM_SHA512_P521,
235-
CipherSuite::ID::X448_CHACHA20POLY1305_SHA512_Ed448,
236-
};
237-
23813
} // namespace mls

0 commit comments

Comments
 (0)