Skip to content

Commit 6917a1d

Browse files
committed
Convert remaining create() methods to get()
1 parent b8ae46c commit 6917a1d

23 files changed

+386
-172
lines changed

cmd/api_example/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
using namespace mls;
1111

12-
const auto suite = CipherSuite::ID::X25519_AES128GCM_SHA256_Ed25519;
12+
const auto suite = CipherSuite{CipherSuite::ID::X25519_AES128GCM_SHA256_Ed25519};
1313

1414
class User
1515
{

lib/hpke/include/hpke/digest.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ struct Digest
1919
template<ID id>
2020
static const Digest& get();
2121

22-
// XXX
23-
static const Digest& create(Digest::ID id);
24-
2522
bytes hash(const bytes& data) const;
2623
bytes hmac(const bytes& key, const bytes& data) const;
2724

@@ -33,6 +30,9 @@ struct Digest
3330

3431
explicit Digest(ID id);
3532
friend Digest make_digest(ID id);
33+
34+
template<Digest::ID id>
35+
static const Digest instance;
3636
};
3737

3838
} // namespace hpke

lib/hpke/include/hpke/hpke.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ struct KEM
1919
DHKEM_X448_SHA512 = 0x0021,
2020
};
2121

22-
static const KEM& create(ID id);
22+
template<KEM::ID>
23+
static const KEM& get();
24+
2325
virtual ~KEM() = default;
2426

2527
struct PublicKey
@@ -70,7 +72,9 @@ struct KDF
7072
HKDF_SHA512 = 0x0003,
7173
};
7274

73-
static const KDF& create(ID id);
75+
template<KDF::ID id>
76+
static const KDF& get();
77+
7478
virtual ~KDF() = default;
7579

7680
virtual bytes extract(const bytes& salt, const bytes& ikm) const = 0;
@@ -100,7 +104,9 @@ struct AEAD
100104
CHACHA20_POLY1305 = 0x0003,
101105
};
102106

103-
static const AEAD& create(ID id);
107+
template<AEAD::ID id>
108+
static const AEAD& get();
109+
104110
virtual ~AEAD() = default;
105111

106112
virtual bytes seal(const bytes& key,

lib/hpke/include/hpke/signature.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ struct Signature
1818
Ed448,
1919
};
2020

21-
static const Signature& create(ID id);
21+
template<Signature::ID id>
22+
static const Signature& get();
23+
2224
virtual ~Signature() = default;
2325

2426
struct PublicKey

lib/hpke/src/aead_cipher.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,35 @@ AEADCipher make_aead(AEAD::ID cipher_in)
1010
return AEADCipher(cipher_in);
1111
}
1212

13-
static AEADCipher aes_128_gcm = make_aead(AEAD::ID::AES_128_GCM);
14-
static AEADCipher aes_256_gcm = make_aead(AEAD::ID::AES_256_GCM);
15-
static AEADCipher chacha20_poly1305 = make_aead(AEAD::ID::CHACHA20_POLY1305);
13+
template<>
14+
const AEADCipher AEADCipher::instance<AEAD::ID::AES_128_GCM> = make_aead(AEAD::ID::AES_128_GCM);
15+
16+
template<>
17+
const AEADCipher AEADCipher::instance<AEAD::ID::AES_256_GCM> = make_aead(AEAD::ID::AES_256_GCM);
18+
19+
template<>
20+
const AEADCipher AEADCipher::instance<AEAD::ID::CHACHA20_POLY1305> = make_aead(AEAD::ID::CHACHA20_POLY1305);
21+
1622

1723
template<>
1824
const AEADCipher&
1925
AEADCipher::get<AEAD::ID::AES_128_GCM>()
2026
{
21-
return aes_128_gcm;
27+
return AEADCipher::instance<AEAD::ID::AES_128_GCM>;
2228
}
2329

2430
template<>
2531
const AEADCipher&
2632
AEADCipher::get<AEAD::ID::AES_256_GCM>()
2733
{
28-
return aes_256_gcm;
34+
return AEADCipher::instance<AEAD::ID::AES_256_GCM>;
2935
}
3036

3137
template<>
3238
const AEADCipher&
3339
AEADCipher::get<AEAD::ID::CHACHA20_POLY1305>()
3440
{
35-
return chacha20_poly1305;
41+
return AEADCipher::instance<AEAD::ID::CHACHA20_POLY1305>;
3642
}
3743

3844
static size_t

lib/hpke/src/aead_cipher.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ struct AEADCipher : public AEAD
3131

3232
AEADCipher(AEAD::ID cipher_in);
3333
friend AEADCipher make_aead(AEAD::ID cipher_in);
34+
35+
template<AEAD::ID id>
36+
static const AEADCipher instance;
3437
};
3538

3639
} // namespace hpke

lib/hpke/src/dhkem.cpp

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,56 +17,71 @@ DHKEM::PrivateKey::public_key() const
1717
return group_priv->public_key();
1818
}
1919

20-
DHKEM make_dhkem(KEM::ID kem_id_in, Group::ID group_id_in, KDF::ID kdf_id_in)
20+
DHKEM make_dhkem(KEM::ID kem_id_in, const Group& group_in, const KDF& kdf_in)
2121
{
22-
return DHKEM(kem_id_in, group_id_in, kdf_id_in);
22+
return DHKEM(kem_id_in, group_in, kdf_in);
2323
}
2424

25-
static const DHKEM dhkem_p256 = make_dhkem(KEM::ID::DHKEM_P256_SHA256, Group::ID::P256, KDF::ID::HKDF_SHA256);
26-
static const DHKEM dhkem_p384 = make_dhkem(KEM::ID::DHKEM_P384_SHA384, Group::ID::P384, KDF::ID::HKDF_SHA384);
27-
static const DHKEM dhkem_p512 = make_dhkem(KEM::ID::DHKEM_P521_SHA512, Group::ID::P521, KDF::ID::HKDF_SHA512);
28-
static const DHKEM dhkem_x25519 = make_dhkem(KEM::ID::DHKEM_X25519_SHA256, Group::ID::X25519, KDF::ID::HKDF_SHA256);
29-
static const DHKEM dhkem_x448 = make_dhkem(KEM::ID::DHKEM_X448_SHA512, Group::ID::X448, KDF::ID::HKDF_SHA512);
25+
template<>
26+
const DHKEM DHKEM::instance<KEM::ID::DHKEM_P256_SHA256> =
27+
make_dhkem(KEM::ID::DHKEM_P256_SHA256, Group::get<Group::ID::P256>(), KDF::get<KDF::ID::HKDF_SHA256>());
28+
29+
template<>
30+
const DHKEM DHKEM::instance<KEM::ID::DHKEM_P384_SHA384> =
31+
make_dhkem(KEM::ID::DHKEM_P384_SHA384, Group::get<Group::ID::P384>(), KDF::get<KDF::ID::HKDF_SHA384>());
32+
33+
template<>
34+
const DHKEM DHKEM::instance<KEM::ID::DHKEM_P521_SHA512> =
35+
make_dhkem(KEM::ID::DHKEM_P521_SHA512, Group::get<Group::ID::P521>(), KDF::get<KDF::ID::HKDF_SHA512>());
36+
37+
template<>
38+
const DHKEM DHKEM::instance<KEM::ID::DHKEM_X25519_SHA256> =
39+
make_dhkem(KEM::ID::DHKEM_X25519_SHA256, Group::get<Group::ID::X25519>(), KDF::get<KDF::ID::HKDF_SHA256>());
40+
41+
template<>
42+
const DHKEM DHKEM::instance<KEM::ID::DHKEM_X448_SHA512> =
43+
make_dhkem(KEM::ID::DHKEM_X448_SHA512, Group::get<Group::ID::X448>(), KDF::get<KDF::ID::HKDF_SHA512>());
44+
3045

3146
template<>
3247
const DHKEM&
3348
DHKEM::get<KEM::ID::DHKEM_P256_SHA256>()
3449
{
35-
return dhkem_p256;
50+
return DHKEM::instance<KEM::ID::DHKEM_P256_SHA256>;
3651
}
3752

3853
template<>
3954
const DHKEM&
4055
DHKEM::get<KEM::ID::DHKEM_P384_SHA384>()
4156
{
42-
return dhkem_p384;
57+
return DHKEM::instance<KEM::ID::DHKEM_P384_SHA384>;
4358
}
4459

4560
template<>
4661
const DHKEM&
4762
DHKEM::get<KEM::ID::DHKEM_P521_SHA512>()
4863
{
49-
return dhkem_p512;
64+
return DHKEM::instance<KEM::ID::DHKEM_P521_SHA512>;
5065
}
5166

5267
template<>
5368
const DHKEM&
5469
DHKEM::get<KEM::ID::DHKEM_X25519_SHA256>()
5570
{
56-
return dhkem_x25519;
71+
return DHKEM::instance<KEM::ID::DHKEM_X25519_SHA256>;
5772
}
5873

5974
template<>
6075
const DHKEM&
6176
DHKEM::get<KEM::ID::DHKEM_X448_SHA512>()
6277
{
63-
return dhkem_x448;
78+
return DHKEM::instance<KEM::ID::DHKEM_X448_SHA512>;
6479
}
6580

6681

67-
DHKEM::DHKEM(KEM::ID kem_id_in, Group::ID group_id_in, KDF::ID kdf_id_in)
68-
: group(Group::create(group_id_in))
69-
, kdf(KDF::create(kdf_id_in))
82+
DHKEM::DHKEM(KEM::ID kem_id_in, const Group& group_in, const KDF& kdf_in)
83+
: group(group_in)
84+
, kdf(kdf_in)
7085
{
7186
static const auto label_kem = to_bytes("KEM");
7287
suite_id = label_kem + i2osp(uint16_t(kem_id_in), 2);

lib/hpke/src/dhkem.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ struct DHKEM : public KEM
5353

5454
bytes extract_and_expand(const bytes& dh, const bytes& kem_context) const;
5555

56-
DHKEM(KEM::ID kem_id_in, Group::ID group_id_in, KDF::ID kdf_id_in);
57-
friend DHKEM make_dhkem(KEM::ID kem_id_in, Group::ID group_id_in, KDF::ID kdf_id_in);
56+
DHKEM(KEM::ID kem_id_in, const Group& group_in, const KDF& kdf_in);
57+
friend DHKEM make_dhkem(KEM::ID kem_id_in, const Group& group_in, const KDF& kdf_in);
58+
59+
template<KEM::ID id>
60+
static const DHKEM instance;
5861
};
5962

6063
} // namespace hpke

lib/hpke/src/digest.cpp

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,44 +29,31 @@ Digest make_digest(Digest::ID id) {
2929
return Digest(id);
3030
}
3131

32-
static const Digest sha256 = make_digest(Digest::ID::SHA256);
33-
static const Digest sha384 = make_digest(Digest::ID::SHA384);
34-
static const Digest sha512 = make_digest(Digest::ID::SHA512);
32+
template<>
33+
const Digest Digest::instance<Digest::ID::SHA256> = make_digest(Digest::ID::SHA256);
34+
35+
template<>
36+
const Digest Digest::instance<Digest::ID::SHA384> = make_digest(Digest::ID::SHA384);
37+
38+
template<>
39+
const Digest Digest::instance<Digest::ID::SHA512> = make_digest(Digest::ID::SHA512);
3540

3641
template<>
3742
const Digest& Digest::get<Digest::ID::SHA256>()
3843
{
39-
return sha256;
44+
return Digest::instance<Digest::ID::SHA256>;
4045
}
4146

4247
template<>
4348
const Digest& Digest::get<Digest::ID::SHA384>()
4449
{
45-
return sha384;
50+
return Digest::instance<Digest::ID::SHA384>;
4651
}
4752

4853
template<>
4954
const Digest& Digest::get<Digest::ID::SHA512>()
5055
{
51-
return sha512;
52-
}
53-
54-
const Digest&
55-
Digest::create(Digest::ID id)
56-
{
57-
switch (id) {
58-
case Digest::ID::SHA256:
59-
return Digest::get<Digest::ID::SHA256>();
60-
61-
case Digest::ID::SHA384:
62-
return Digest::get<Digest::ID::SHA384>();
63-
64-
case Digest::ID::SHA512:
65-
return Digest::get<Digest::ID::SHA512>();
66-
67-
default:
68-
throw std::runtime_error("Unsupported ciphersuite");
69-
}
56+
return Digest::instance<Digest::ID::SHA512>;
7057
}
7158

7259
Digest::Digest(Digest::ID id_in)

0 commit comments

Comments
 (0)