Debian Codegraph @

mlspp/lib/hpke/src/hpke.cpp

raw symbols SCIP
1 #cxx . . $ `<file>/mlspp/lib/hpke/src/hpke.cpp`/include <hpke/digest.h>cxx . . $ `<file>/mlspp/lib/hpke/include/hpke/digest.h`/
2 #include <hpke/hpke.h>cxx . . $ `<file>/mlspp/lib/hpke/include/hpke/hpke.h`/
3
4 #include "aead_cipher.h"cxx . . $ `<file>/mlspp/lib/hpke/src/aead_cipher.h`/
5 #include "common.h"cxx . . $ `<file>/mlspp/lib/hpke/src/common.h`/
6 #include "dhkem.h"cxx . . $ `<file>/mlspp/lib/hpke/src/dhkem.h`/
7 #include "hkdf.h"cxx . . $ `<file>/mlspp/lib/hpke/src/hkdf.h`/
8
9 #include <limits>
10 #include <stdexcept>
11 #include <string>
12
13 namespace mlsppcxx . . $ mlspp/::hpkecxx . . $ mlspp/hpke/ {
14
15 ///
16 /// Helper functions and constants
17 ///
18
19 static const bytescxx . . $ mlspp/bytes_ns/bytes#&
20 label_expcxx . . $ mlspp/hpke/label_exp(f9dc8c7ea672a8f6).()
21 {
22 static const bytescxx . . $ mlspp/bytes_ns/bytes# val = from_asciicxx . . $ mlspp/bytes_ns/from_ascii(fc01d114001b7256).("exp"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
23 return val;
24 }
25
26 static const bytescxx . . $ mlspp/bytes_ns/bytes#&
27 label_hpkecxx . . $ mlspp/hpke/label_hpke(f9dc8c7ea672a8f6).()
28 {
29 static const bytescxx . . $ mlspp/bytes_ns/bytes# val = from_asciicxx . . $ mlspp/bytes_ns/from_ascii(fc01d114001b7256).("HPKE"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
30 return val;
31 }
32
33 static const bytescxx . . $ mlspp/bytes_ns/bytes#&
34 label_hpke_versioncxx . . $ mlspp/hpke/label_hpke_version(f9dc8c7ea672a8f6).()
35 {
36 static const bytescxx . . $ mlspp/bytes_ns/bytes# val = from_asciicxx . . $ mlspp/bytes_ns/from_ascii(fc01d114001b7256).("HPKE-v1"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
37 return val;
38 }
39
40 static const bytescxx . . $ mlspp/bytes_ns/bytes#&
41 label_info_hashcxx . . $ mlspp/hpke/label_info_hash(f9dc8c7ea672a8f6).()
42 {
43 static const bytescxx . . $ mlspp/bytes_ns/bytes# val = from_asciicxx . . $ mlspp/bytes_ns/from_ascii(fc01d114001b7256).("info_hash"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
44 return val;
45 }
46
47 static const bytescxx . . $ mlspp/bytes_ns/bytes#&
48 label_keycxx . . $ mlspp/hpke/label_key(f9dc8c7ea672a8f6).()
49 {
50 static const bytescxx . . $ mlspp/bytes_ns/bytes# val = from_asciicxx . . $ mlspp/bytes_ns/from_ascii(fc01d114001b7256).("key"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
51 return val;
52 }
53
54 static const bytescxx . . $ mlspp/bytes_ns/bytes#&
55 label_base_noncecxx . . $ mlspp/hpke/label_base_nonce(f9dc8c7ea672a8f6).()
56 {
57 static const bytescxx . . $ mlspp/bytes_ns/bytes# val = from_asciicxx . . $ mlspp/bytes_ns/from_ascii(fc01d114001b7256).("base_nonce"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
58 return val;
59 }
60
61 static const bytescxx . . $ mlspp/bytes_ns/bytes#&
62 label_psk_id_hashcxx . . $ mlspp/hpke/label_psk_id_hash(f9dc8c7ea672a8f6).()
63 {
64 static const bytescxx . . $ mlspp/bytes_ns/bytes# val = from_asciicxx . . $ mlspp/bytes_ns/from_ascii(fc01d114001b7256).("psk_id_hash"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
65 return val;
66 }
67
68 static const bytescxx . . $ mlspp/bytes_ns/bytes#&
69 label_seccxx . . $ mlspp/hpke/label_sec(f9dc8c7ea672a8f6).()
70 {
71 static const bytescxx . . $ mlspp/bytes_ns/bytes# val = from_asciicxx . . $ mlspp/bytes_ns/from_ascii(fc01d114001b7256).("sec"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
72 return val;
73 }
74
75 static const bytescxx . . $ mlspp/bytes_ns/bytes#&
76 label_secretcxx . . $ mlspp/hpke/label_secret(f9dc8c7ea672a8f6).()
77 {
78 static const bytescxx . . $ mlspp/bytes_ns/bytes# val = from_asciicxx . . $ mlspp/bytes_ns/from_ascii(fc01d114001b7256).("secret"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).);
79 return val;
80 }
81
82 ///
83 /// Factory methods for primitives
84 ///
85
86 KEMcxx . . $ mlspp/hpke/KEM#::KEMcxx . . $ mlspp/hpke/KEM#KEM(afa293977f5fd521).(IDcxx . . $ mlspp/hpke/KEM#ID# id_in,
87 size_t secret_size_in,
88 size_t enc_size_in,
89 size_t pk_size_in,
90 size_t sk_size_in)
91 : idcxx . . $ mlspp/hpke/KEM#id.(id_in)
92 , secret_sizecxx . . $ mlspp/hpke/KEM#secret_size.(secret_size_in)
93 , enc_sizecxx . . $ mlspp/hpke/KEM#enc_size.(enc_size_in)
94 , pk_sizecxx . . $ mlspp/hpke/KEM#pk_size.(pk_size_in)
95 , sk_sizecxx . . $ mlspp/hpke/KEM#sk_size.(sk_size_in)
96 {
97 }
98
99 template<>
100 const KEMcxx . . $ mlspp/hpke/KEM#&
101 KEMcxx . . $ mlspp/hpke/KEM#::getcxx . . $ mlspp/hpke/KEM#get(503e23cf0acd5d29).<KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_P256_SHA256cxx . . $ mlspp/hpke/KEM#ID#DHKEM_P256_SHA256.>()
102 {
103 return DHKEMcxx . . $ mlspp/hpke/DHKEM#::getcxx . . $ mlspp/hpke/DHKEM#get(c298abef80e23fa6).<KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_P256_SHA256cxx . . $ mlspp/hpke/KEM#ID#DHKEM_P256_SHA256.>();
104 }
105
106 template<>
107 const KEMcxx . . $ mlspp/hpke/KEM#&
108 KEMcxx . . $ mlspp/hpke/KEM#::getcxx . . $ mlspp/hpke/KEM#get(503e23cf0acd5d29).<KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_P384_SHA384cxx . . $ mlspp/hpke/KEM#ID#DHKEM_P384_SHA384.>()
109 {
110 return DHKEMcxx . . $ mlspp/hpke/DHKEM#::getcxx . . $ mlspp/hpke/DHKEM#get(c298abef80e23fa6).<KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_P384_SHA384cxx . . $ mlspp/hpke/KEM#ID#DHKEM_P384_SHA384.>();
111 }
112
113 template<>
114 const KEMcxx . . $ mlspp/hpke/KEM#&
115 KEMcxx . . $ mlspp/hpke/KEM#::getcxx . . $ mlspp/hpke/KEM#get(503e23cf0acd5d29).<KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_P521_SHA512cxx . . $ mlspp/hpke/KEM#ID#DHKEM_P521_SHA512.>()
116 {
117 return DHKEMcxx . . $ mlspp/hpke/DHKEM#::getcxx . . $ mlspp/hpke/DHKEM#get(c298abef80e23fa6).<KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_P521_SHA512cxx . . $ mlspp/hpke/KEM#ID#DHKEM_P521_SHA512.>();
118 }
119
120 template<>
121 const KEMcxx . . $ mlspp/hpke/KEM#&
122 KEMcxx . . $ mlspp/hpke/KEM#::getcxx . . $ mlspp/hpke/KEM#get(503e23cf0acd5d29).<KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_X25519_SHA256cxx . . $ mlspp/hpke/KEM#ID#DHKEM_X25519_SHA256.>()
123 {
124 return DHKEMcxx . . $ mlspp/hpke/DHKEM#::getcxx . . $ mlspp/hpke/DHKEM#get(c298abef80e23fa6).<KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_X25519_SHA256cxx . . $ mlspp/hpke/KEM#ID#DHKEM_X25519_SHA256.>();
125 }
126
127 #if !defined(WITH_BORINGSSL)
128 template<>
129 const KEMcxx . . $ mlspp/hpke/KEM#&
130 KEMcxx . . $ mlspp/hpke/KEM#::getcxx . . $ mlspp/hpke/KEM#get(503e23cf0acd5d29).<KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_X448_SHA512cxx . . $ mlspp/hpke/KEM#ID#DHKEM_X448_SHA512.>()
131 {
132 return DHKEMcxx . . $ mlspp/hpke/DHKEM#::getcxx . . $ mlspp/hpke/DHKEM#get(c298abef80e23fa6).<KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_X448_SHA512cxx . . $ mlspp/hpke/KEM#ID#DHKEM_X448_SHA512.>();
133 }
134 #endif
135
136 bytescxx . . $ mlspp/bytes_ns/bytes#
137 KEMcxx . . $ mlspp/hpke/KEM#::serialize_privatecxx . . $ mlspp/hpke/KEM#serialize_private(659c63e46d9645fb).(const KEMcxx . . $ mlspp/hpke/KEM#::PrivateKeycxx . . $ mlspp/hpke/KEM#PrivateKey#& /* unused */) const
138 {
139 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Not implemented");
140 }
141
142 stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<KEMcxx . . $ mlspp/hpke/KEM#::PrivateKeycxx . . $ mlspp/hpke/KEM#PrivateKey#>
143 KEMcxx . . $ mlspp/hpke/KEM#::deserialize_privatecxx . . $ mlspp/hpke/KEM#deserialize_private(b1a67661546df615).(const bytescxx . . $ mlspp/bytes_ns/bytes#& /* unused */) const
144 {
145 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Not implemented");
146 }
147
148 stdcxx . . $ std/::paircxx . . $ std/pair#<bytescxx . . $ mlspp/bytes_ns/bytes#, bytescxx . . $ mlspp/bytes_ns/bytes#>
149 KEMcxx . . $ mlspp/hpke/KEM#::auth_encapcxx . . $ mlspp/hpke/KEM#auth_encap(70a5a9729afdc401).(const PublicKeycxx . . $ mlspp/hpke/KEM#PublicKey#& /* unused */,
150 const PrivateKeycxx . . $ mlspp/hpke/KEM#PrivateKey#& /* unused */) const
151 {
152 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Not implemented");
153 }
154
155 bytescxx . . $ mlspp/bytes_ns/bytes#
156 KEMcxx . . $ mlspp/hpke/KEM#::auth_decapcxx . . $ mlspp/hpke/KEM#auth_decap(ba74d0d94964aa1d).(const bytescxx . . $ mlspp/bytes_ns/bytes#& /* unused */,
157 const PublicKeycxx . . $ mlspp/hpke/KEM#PublicKey#& /* unused */,
158 const PrivateKeycxx . . $ mlspp/hpke/KEM#PrivateKey#& /* unused */) const
159 {
160 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Not implemented");
161 }
162
163 template<>
164 const KDFcxx . . $ mlspp/hpke/KDF#&
165 KDFcxx . . $ mlspp/hpke/KDF#::getcxx . . $ mlspp/hpke/KDF#get(9904bd6e979faa13).<KDFcxx . . $ mlspp/hpke/KDF#::IDcxx . . $ mlspp/hpke/KDF#ID#::HKDF_SHA256cxx . . $ mlspp/hpke/KDF#ID#HKDF_SHA256.>()
166 {
167 return HKDFcxx . . $ mlspp/hpke/HKDF#::getcxx . . $ mlspp/hpke/HKDF#get(1591171d168e2cff).<Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA256cxx . . $ mlspp/hpke/Digest#ID#SHA256.>();
168 }
169
170 template<>
171 const KDFcxx . . $ mlspp/hpke/KDF#&
172 KDFcxx . . $ mlspp/hpke/KDF#::getcxx . . $ mlspp/hpke/KDF#get(9904bd6e979faa13).<KDFcxx . . $ mlspp/hpke/KDF#::IDcxx . . $ mlspp/hpke/KDF#ID#::HKDF_SHA384cxx . . $ mlspp/hpke/KDF#ID#HKDF_SHA384.>()
173 {
174 return HKDFcxx . . $ mlspp/hpke/HKDF#::getcxx . . $ mlspp/hpke/HKDF#get(1591171d168e2cff).<Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA384cxx . . $ mlspp/hpke/Digest#ID#SHA384.>();
175 }
176
177 template<>
178 const KDFcxx . . $ mlspp/hpke/KDF#&
179 KDFcxx . . $ mlspp/hpke/KDF#::getcxx . . $ mlspp/hpke/KDF#get(9904bd6e979faa13).<KDFcxx . . $ mlspp/hpke/KDF#::IDcxx . . $ mlspp/hpke/KDF#ID#::HKDF_SHA512cxx . . $ mlspp/hpke/KDF#ID#HKDF_SHA512.>()
180 {
181 return HKDFcxx . . $ mlspp/hpke/HKDF#::getcxx . . $ mlspp/hpke/HKDF#get(1591171d168e2cff).<Digestcxx . . $ mlspp/hpke/Digest#::IDcxx . . $ mlspp/hpke/Digest#ID#::SHA512cxx . . $ mlspp/hpke/Digest#ID#SHA512.>();
182 }
183
184 KDFcxx . . $ mlspp/hpke/KDF#::KDFcxx . . $ mlspp/hpke/KDF#KDF(5215224282772cd5).(IDcxx . . $ mlspp/hpke/KDF#ID# id_in, size_t hash_size_in)
185 : idcxx . . $ mlspp/hpke/KDF#id.(id_in)
186 , hash_sizecxx . . $ mlspp/hpke/KDF#hash_size.(hash_size_in)
187 {
188 }
189
190 bytescxx . . $ mlspp/bytes_ns/bytes#
191 KDFcxx . . $ mlspp/hpke/KDF#::labeled_extractcxx . . $ mlspp/hpke/KDF#labeled_extract(a2160eb270d8417e).(const bytescxx . . $ mlspp/bytes_ns/bytes#& suite_id,
192 const bytescxx . . $ mlspp/bytes_ns/bytes#& salt,
193 const bytescxx . . $ mlspp/bytes_ns/bytes#& label,
194 const bytescxx . . $ mlspp/bytes_ns/bytes#& ikm) const
195 {
196 auto labeled_ikm = label_hpke_versioncxx . . $ mlspp/hpke/label_hpke_version(f9dc8c7ea672a8f6).() +cxx . . $ mlspp/bytes_ns/bytes#operator+(c5e1b85e16e94921). suite_id +cxx . . $ mlspp/bytes_ns/bytes#operator+(c5e1b85e16e94921). label +cxx . . $ mlspp/bytes_ns/bytes#operator+(c5e1b85e16e94921). ikm;
197 return extractcxx . . $ mlspp/hpke/KDF#extract(802fe03ef4f7e693).(salt, labeled_ikm);
198 }
199
200 bytescxx . . $ mlspp/bytes_ns/bytes#
201 KDFcxx . . $ mlspp/hpke/KDF#::labeled_expandcxx . . $ mlspp/hpke/KDF#labeled_expand(a3b1f2557e911f84).(const bytescxx . . $ mlspp/bytes_ns/bytes#& suite_id,
202 const bytescxx . . $ mlspp/bytes_ns/bytes#& prk,
203 const bytescxx . . $ mlspp/bytes_ns/bytes#& label,
204 const bytescxx . . $ mlspp/bytes_ns/bytes#& info,
205 size_t size) const
206 {
207 auto labeled_info =
208 i2ospcxx . . $ mlspp/hpke/i2osp(b57ad3058a1ef3ae).(size, 2) +cxx . . $ mlspp/bytes_ns/bytes#operator+(c5e1b85e16e94921). label_hpke_versioncxx . . $ mlspp/hpke/label_hpke_version(f9dc8c7ea672a8f6).() +cxx . . $ mlspp/bytes_ns/bytes#operator+(c5e1b85e16e94921). suite_id +cxx . . $ mlspp/bytes_ns/bytes#operator+(c5e1b85e16e94921). label +cxx . . $ mlspp/bytes_ns/bytes#operator+(c5e1b85e16e94921). info;
209 return expandcxx . . $ mlspp/hpke/KDF#expand(fc2505b904ce7e90).(prk, labeled_info, size);
210 }
211
212 template<>
213 const AEADcxx . . $ mlspp/hpke/AEAD#&
214 AEADcxx . . $ mlspp/hpke/AEAD#::getcxx . . $ mlspp/hpke/AEAD#get(889f45db4bad3d2d).<AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_128_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_128_GCM.>()
215 {
216 return AEADCiphercxx . . $ mlspp/hpke/AEADCipher#::getcxx . . $ mlspp/hpke/AEADCipher#get(857c9916fe512820).<AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_128_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_128_GCM.>();
217 }
218
219 template<>
220 const AEADcxx . . $ mlspp/hpke/AEAD#&
221 AEADcxx . . $ mlspp/hpke/AEAD#::getcxx . . $ mlspp/hpke/AEAD#get(889f45db4bad3d2d).<AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_256_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_256_GCM.>()
222 {
223 return AEADCiphercxx . . $ mlspp/hpke/AEADCipher#::getcxx . . $ mlspp/hpke/AEADCipher#get(857c9916fe512820).<AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_256_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_256_GCM.>();
224 }
225
226 template<>
227 const AEADcxx . . $ mlspp/hpke/AEAD#&
228 AEADcxx . . $ mlspp/hpke/AEAD#::getcxx . . $ mlspp/hpke/AEAD#get(889f45db4bad3d2d).<AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::CHACHA20_POLY1305cxx . . $ mlspp/hpke/AEAD#ID#CHACHA20_POLY1305.>()
229 {
230 return AEADCiphercxx . . $ mlspp/hpke/AEADCipher#::getcxx . . $ mlspp/hpke/AEADCipher#get(857c9916fe512820).<AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::CHACHA20_POLY1305cxx . . $ mlspp/hpke/AEAD#ID#CHACHA20_POLY1305.>();
231 }
232
233 template<>
234 const AEADcxx . . $ mlspp/hpke/AEAD#&
235 AEADcxx . . $ mlspp/hpke/AEAD#::getcxx . . $ mlspp/hpke/AEAD#get(889f45db4bad3d2d).<AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::export_onlycxx . . $ mlspp/hpke/AEAD#ID#export_only.>()
236 {
237 static const auto export_only = ExportOnlyCiphercxx . . $ mlspp/hpke/ExportOnlyCipher#ExportOnlyCipher(49f6e7a06ebc5aa8).{};
238 return export_only;
239 }
240
241 AEADcxx . . $ mlspp/hpke/AEAD#::AEADcxx . . $ mlspp/hpke/AEAD#AEAD(c9122d5efed02209).(IDcxx . . $ mlspp/hpke/AEAD#ID# id_in, size_t key_size_in, size_t nonce_size_in)
242 : idcxx . . $ mlspp/hpke/AEAD#id.(id_in)
243 , key_sizecxx . . $ mlspp/hpke/AEAD#key_size.(key_size_in)
244 , nonce_sizecxx . . $ mlspp/hpke/AEAD#nonce_size.(nonce_size_in)
245 {
246 }
247
248 ///
249 /// Encryption Contexts
250 ///
251
252 bytescxx . . $ mlspp/bytes_ns/bytes#
253 Contextcxx . . $ mlspp/hpke/Context#::do_exportcxx . . $ mlspp/hpke/Context#do_export(25e8a9b4283f7384).(const bytescxx . . $ mlspp/bytes_ns/bytes#& exporter_context, size_t size) const
254 {
255 return kdfcxx . . $ mlspp/hpke/Context#kdf..labeled_expandcxx . . $ mlspp/hpke/KDF#labeled_expand(a3b1f2557e911f84).(
256 suitecxx . . $ mlspp/hpke/Context#suite., exporter_secretcxx . . $ mlspp/hpke/Context#exporter_secret., label_seccxx . . $ mlspp/hpke/label_sec(f9dc8c7ea672a8f6).(), exporter_context, size);
257 }
258
259 bytescxx . . $ mlspp/bytes_ns/bytes#
260 Contextcxx . . $ mlspp/hpke/Context#::current_noncecxx . . $ mlspp/hpke/Context#current_nonce(d7392ba79aa35767).() const
261 {
262 auto curr = i2ospcxx . . $ mlspp/hpke/i2osp(b57ad3058a1ef3ae).(seqcxx . . $ mlspp/hpke/Context#seq., aeadcxx . . $ mlspp/hpke/Context#aead..nonce_sizecxx . . $ mlspp/hpke/AEAD#nonce_size.);
263 return curr ^cxx . . $ mlspp/bytes_ns/bytes#`operator^`(c5e1b85e16e94921). noncecxx . . $ mlspp/hpke/Context#nonce.;
264 }
265
266 void
267 Contextcxx . . $ mlspp/hpke/Context#::increment_seqcxx . . $ mlspp/hpke/Context#increment_seq(49f6e7a06ebc5aa8).()
268 {
269 if (seqcxx . . $ mlspp/hpke/Context#seq. == stdcxx . . $ std/::numeric_limitscxx . . $ std/numeric_limits#<uint64_t>::maxcxx . . $ std/numeric_limits#max(cee159cc271e827).()) {
270 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Sequence number overflow");
271 }
272
273 seqcxx . . $ mlspp/hpke/Context#seq. += 1;
274 }
275
276 Contextcxx . . $ mlspp/hpke/Context#::Contextcxx . . $ mlspp/hpke/Context#Context(5ac4442da2c9d040).(bytescxx . . $ mlspp/bytes_ns/bytes# suite_in,
277 bytescxx . . $ mlspp/bytes_ns/bytes# key_in,
278 bytescxx . . $ mlspp/bytes_ns/bytes# nonce_in,
279 bytescxx . . $ mlspp/bytes_ns/bytes# exporter_secret_in,
280 const KDFcxx . . $ mlspp/hpke/KDF#& kdf_in,
281 const AEADcxx . . $ mlspp/hpke/AEAD#& aead_in)
282 : suitecxx . . $ mlspp/hpke/Context#suite.(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(suite_in))
283 , keycxx . . $ mlspp/hpke/Context#key.(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(key_in))
284 , noncecxx . . $ mlspp/hpke/Context#nonce.(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(nonce_in))
285 , exporter_secretcxx . . $ mlspp/hpke/Context#exporter_secret.(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(exporter_secret_in))
286 , kdfcxx . . $ mlspp/hpke/Context#kdf.(kdf_in)
287 , aeadcxx . . $ mlspp/hpke/Context#aead.(aead_in)
288 , seqcxx . . $ mlspp/hpke/Context#seq.(0)
289 {
290 }
291
292 bool
293 operatorcxx . . $ mlspp/hpke/`operator==`(2317825b428f343d).==(const Contextcxx . . $ mlspp/hpke/Context#& lhs, const Contextcxx . . $ mlspp/hpke/Context#& rhs)
294 {
295 // TODO(RLB) Compare KDF and AEAD algorithms
296 auto suite = (lhs.suitecxx . . $ mlspp/hpke/Context#suite. ==cxx . . $ mlspp/bytes_ns/bytes#`operator==`(efd4f3c976ab92ce). rhs.suitecxx . . $ mlspp/hpke/Context#suite.);
297 auto key = (lhs.keycxx . . $ mlspp/hpke/Context#key. ==cxx . . $ mlspp/bytes_ns/bytes#`operator==`(efd4f3c976ab92ce). rhs.keycxx . . $ mlspp/hpke/Context#key.);
298 auto nonce = (lhs.noncecxx . . $ mlspp/hpke/Context#nonce. ==cxx . . $ mlspp/bytes_ns/bytes#`operator==`(efd4f3c976ab92ce). rhs.noncecxx . . $ mlspp/hpke/Context#nonce.);
299 auto exporter_secret = (lhs.exporter_secretcxx . . $ mlspp/hpke/Context#exporter_secret. ==cxx . . $ mlspp/bytes_ns/bytes#`operator==`(efd4f3c976ab92ce). rhs.exporter_secretcxx . . $ mlspp/hpke/Context#exporter_secret.);
300 auto seq = (lhs.seqcxx . . $ mlspp/hpke/Context#seq. == rhs.seqcxx . . $ mlspp/hpke/Context#seq.);
301 return suite && key && nonce && exporter_secret && seq;
302 }
303
304 SenderContextcxx . . $ mlspp/hpke/SenderContext#::SenderContextcxx . . $ mlspp/hpke/SenderContext#SenderContext(e7dba9c45e997a81).(Contextcxx . . $ mlspp/hpke/Context#&& c)
305 : Contextcxx . . $ mlspp/hpke/Context#(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(c))
306 {
307 }
308
309 bytescxx . . $ mlspp/bytes_ns/bytes#
310 SenderContextcxx . . $ mlspp/hpke/SenderContext#::sealcxx . . $ mlspp/hpke/SenderContext#seal(5591ef0fb4aab668).(const bytescxx . . $ mlspp/bytes_ns/bytes#& aad, const bytescxx . . $ mlspp/bytes_ns/bytes#& pt)
311 {
312 auto ct = aeadcxx . . $ mlspp/hpke/Context#aead..sealcxx . . $ mlspp/hpke/AEAD#seal(a2160eb270d8417e).(keycxx . . $ mlspp/hpke/Context#key., current_noncecxx . . $ mlspp/hpke/Context#current_nonce(d7392ba79aa35767).(), aad, pt);
313 increment_seqcxx . . $ mlspp/hpke/Context#increment_seq(49f6e7a06ebc5aa8).();
314 return ctcxx . . $ mlspp/bytes_ns/bytes#bytes(628b76f52f2a67f).;
315 }
316
317 ReceiverContextcxx . . $ mlspp/hpke/ReceiverContext#::ReceiverContextcxx . . $ mlspp/hpke/ReceiverContext#ReceiverContext(e7dba9c45e997a81).(Contextcxx . . $ mlspp/hpke/Context#&& c)
318 : Contextcxx . . $ mlspp/hpke/Context#(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(c))
319 {
320 }
321
322 stdcxx . . $ std/::optionalcxx . . $ std/optional#<bytescxx . . $ mlspp/bytes_ns/bytes#>
323 ReceiverContextcxx . . $ mlspp/hpke/ReceiverContext#::opencxx . . $ mlspp/hpke/ReceiverContext#open(4e9304a318c66e3a).(const bytescxx . . $ mlspp/bytes_ns/bytes#& aad, const bytescxx . . $ mlspp/bytes_ns/bytes#& ct)
324 {
325 auto maybe_pt = aeadcxx . . $ mlspp/hpke/Context#aead..opencxx . . $ mlspp/hpke/AEAD#open(f7542dab5ac51900).(keycxx . . $ mlspp/hpke/Context#key., current_noncecxx . . $ mlspp/hpke/Context#current_nonce(d7392ba79aa35767).(), aad, ct);
326 increment_seqcxx . . $ mlspp/hpke/Context#increment_seq(49f6e7a06ebc5aa8).();
327 return maybe_pt;
328 }
329
330 ///
331 /// HPKE
332 ///
333
334 static const bytescxx . . $ mlspp/bytes_ns/bytes# default_pskcxx . . $ mlspp/hpke/default_psk. = {cxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).};
335 static const bytescxx . . $ mlspp/bytes_ns/bytes# default_psk_idcxx . . $ mlspp/hpke/default_psk_id. = {cxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).};
336
337 static bytescxx . . $ mlspp/bytes_ns/bytes#
338 suite_idcxx . . $ mlspp/hpke/suite_id(76c73f7f7621655e).(KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID# kem_id, KDFcxx . . $ mlspp/hpke/KDF#::IDcxx . . $ mlspp/hpke/KDF#ID# kdf_id, AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID# aead_id)
339 {
340 return label_hpkecxx . . $ mlspp/hpke/label_hpke(f9dc8c7ea672a8f6).() +cxx . . $ mlspp/bytes_ns/bytes#operator+(c5e1b85e16e94921). i2ospcxx . . $ mlspp/hpke/i2osp(b57ad3058a1ef3ae).(static_cast<uint64_tcxx . . $ uint64_t#>(kem_id), 2) +cxx . . $ mlspp/bytes_ns/bytes#operator+(c5e1b85e16e94921).
341 i2ospcxx . . $ mlspp/hpke/i2osp(b57ad3058a1ef3ae).(static_cast<uint64_tcxx . . $ uint64_t#>(kdf_id), 2) +cxx . . $ mlspp/bytes_ns/bytes#operator+(c5e1b85e16e94921).
342 i2ospcxx . . $ mlspp/hpke/i2osp(b57ad3058a1ef3ae).(static_cast<uint64_tcxx . . $ uint64_t#>(aead_id), 2);
343 }
344
345 static const KEMcxx . . $ mlspp/hpke/KEM#&
346 select_kemcxx . . $ mlspp/hpke/select_kem(e82f2af8f037055a).(KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID# id)
347 {
348 switch (id) {
349 case KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_P256_SHA256cxx . . $ mlspp/hpke/KEM#ID#DHKEM_P256_SHA256.:
350 return KEMcxx . . $ mlspp/hpke/KEM#::getcxx . . $ mlspp/hpke/KEM#get(503e23cf0acd5d29).<KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_P256_SHA256cxx . . $ mlspp/hpke/KEM#ID#DHKEM_P256_SHA256.>();
351 case KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_P384_SHA384cxx . . $ mlspp/hpke/KEM#ID#DHKEM_P384_SHA384.:
352 return KEMcxx . . $ mlspp/hpke/KEM#::getcxx . . $ mlspp/hpke/KEM#get(503e23cf0acd5d29).<KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_P384_SHA384cxx . . $ mlspp/hpke/KEM#ID#DHKEM_P384_SHA384.>();
353 case KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_P521_SHA512cxx . . $ mlspp/hpke/KEM#ID#DHKEM_P521_SHA512.:
354 return KEMcxx . . $ mlspp/hpke/KEM#::getcxx . . $ mlspp/hpke/KEM#get(503e23cf0acd5d29).<KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_P521_SHA512cxx . . $ mlspp/hpke/KEM#ID#DHKEM_P521_SHA512.>();
355 case KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_X25519_SHA256cxx . . $ mlspp/hpke/KEM#ID#DHKEM_X25519_SHA256.:
356 return KEMcxx . . $ mlspp/hpke/KEM#::getcxx . . $ mlspp/hpke/KEM#get(503e23cf0acd5d29).<KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_X25519_SHA256cxx . . $ mlspp/hpke/KEM#ID#DHKEM_X25519_SHA256.>();
357 #if !defined(WITH_BORINGSSL)
358 case KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_X448_SHA512cxx . . $ mlspp/hpke/KEM#ID#DHKEM_X448_SHA512.:
359 return KEMcxx . . $ mlspp/hpke/KEM#::getcxx . . $ mlspp/hpke/KEM#get(503e23cf0acd5d29).<KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID#::DHKEM_X448_SHA512cxx . . $ mlspp/hpke/KEM#ID#DHKEM_X448_SHA512.>();
360 #endif
361 default:
362 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unsupported algorithm");
363 }
364 }
365
366 static const KDFcxx . . $ mlspp/hpke/KDF#&
367 select_kdfcxx . . $ mlspp/hpke/select_kdf(d45e1e2c09a8206d).(KDFcxx . . $ mlspp/hpke/KDF#::IDcxx . . $ mlspp/hpke/KDF#ID# id)
368 {
369 switch (id) {
370 case KDFcxx . . $ mlspp/hpke/KDF#::IDcxx . . $ mlspp/hpke/KDF#ID#::HKDF_SHA256cxx . . $ mlspp/hpke/KDF#ID#HKDF_SHA256.:
371 return KDFcxx . . $ mlspp/hpke/KDF#::getcxx . . $ mlspp/hpke/KDF#get(9904bd6e979faa13).<KDFcxx . . $ mlspp/hpke/KDF#::IDcxx . . $ mlspp/hpke/KDF#ID#::HKDF_SHA256cxx . . $ mlspp/hpke/KDF#ID#HKDF_SHA256.>();
372 case KDFcxx . . $ mlspp/hpke/KDF#::IDcxx . . $ mlspp/hpke/KDF#ID#::HKDF_SHA384cxx . . $ mlspp/hpke/KDF#ID#HKDF_SHA384.:
373 return KDFcxx . . $ mlspp/hpke/KDF#::getcxx . . $ mlspp/hpke/KDF#get(9904bd6e979faa13).<KDFcxx . . $ mlspp/hpke/KDF#::IDcxx . . $ mlspp/hpke/KDF#ID#::HKDF_SHA384cxx . . $ mlspp/hpke/KDF#ID#HKDF_SHA384.>();
374 case KDFcxx . . $ mlspp/hpke/KDF#::IDcxx . . $ mlspp/hpke/KDF#ID#::HKDF_SHA512cxx . . $ mlspp/hpke/KDF#ID#HKDF_SHA512.:
375 return KDFcxx . . $ mlspp/hpke/KDF#::getcxx . . $ mlspp/hpke/KDF#get(9904bd6e979faa13).<KDFcxx . . $ mlspp/hpke/KDF#::IDcxx . . $ mlspp/hpke/KDF#ID#::HKDF_SHA512cxx . . $ mlspp/hpke/KDF#ID#HKDF_SHA512.>();
376 default:
377 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unsupported algorithm");
378 }
379 }
380
381 static const AEADcxx . . $ mlspp/hpke/AEAD#&
382 select_aeadcxx . . $ mlspp/hpke/select_aead(2f5040a57862bd17).(AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID# id)
383 {
384 switch (id) {
385 case AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_128_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_128_GCM.:
386 return AEADcxx . . $ mlspp/hpke/AEAD#::getcxx . . $ mlspp/hpke/AEAD#get(889f45db4bad3d2d).<AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_128_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_128_GCM.>();
387 case AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_256_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_256_GCM.:
388 return AEADcxx . . $ mlspp/hpke/AEAD#::getcxx . . $ mlspp/hpke/AEAD#get(889f45db4bad3d2d).<AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::AES_256_GCMcxx . . $ mlspp/hpke/AEAD#ID#AES_256_GCM.>();
389 case AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::CHACHA20_POLY1305cxx . . $ mlspp/hpke/AEAD#ID#CHACHA20_POLY1305.:
390 return AEADcxx . . $ mlspp/hpke/AEAD#::getcxx . . $ mlspp/hpke/AEAD#get(889f45db4bad3d2d).<AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::CHACHA20_POLY1305cxx . . $ mlspp/hpke/AEAD#ID#CHACHA20_POLY1305.>();
391 case AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::export_onlycxx . . $ mlspp/hpke/AEAD#ID#export_only.:
392 return AEADcxx . . $ mlspp/hpke/AEAD#::getcxx . . $ mlspp/hpke/AEAD#get(889f45db4bad3d2d).<AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID#::export_onlycxx . . $ mlspp/hpke/AEAD#ID#export_only.>();
393 default:
394 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unsupported algorithm");
395 }
396 }
397
398 HPKEcxx . . $ mlspp/hpke/HPKE#::HPKEcxx . . $ mlspp/hpke/HPKE#HPKE(8898dfa600f2c6b2).(KEMcxx . . $ mlspp/hpke/KEM#::IDcxx . . $ mlspp/hpke/KEM#ID# kem_id, KDFcxx . . $ mlspp/hpke/KDF#::IDcxx . . $ mlspp/hpke/KDF#ID# kdf_id, AEADcxx . . $ mlspp/hpke/AEAD#::IDcxx . . $ mlspp/hpke/AEAD#ID# aead_id)
399 : suitecxx . . $ mlspp/hpke/HPKE#suite.(suite_idcxx . . $ mlspp/hpke/suite_id(76c73f7f7621655e).(kem_id, kdf_id, aead_id))
400 , kemcxx . . $ mlspp/hpke/HPKE#kem.(select_kemcxx . . $ mlspp/hpke/select_kem(e82f2af8f037055a).(kem_id))
401 , kdfcxx . . $ mlspp/hpke/HPKE#kdf.(select_kdfcxx . . $ mlspp/hpke/select_kdf(d45e1e2c09a8206d).(kdf_id))
402 , aeadcxx . . $ mlspp/hpke/HPKE#aead.(select_aeadcxx . . $ mlspp/hpke/select_aead(2f5040a57862bd17).(aead_id))
403 {
404 }
405
406 HPKEcxx . . $ mlspp/hpke/HPKE#::SenderInfocxx . . $ mlspp/hpke/HPKE#SenderInfo#
407 HPKEcxx . . $ mlspp/hpke/HPKE#::setup_base_scxx . . $ mlspp/hpke/HPKE#setup_base_s(8d59ca38ce5a36e0).(const KEMcxx . . $ mlspp/hpke/KEM#::PublicKeycxx . . $ mlspp/hpke/KEM#PublicKey#& pkR, const bytescxx . . $ mlspp/bytes_ns/bytes#& info) const
408 {
409 auto [shared_secret, enc] = kemcxx . . $ mlspp/hpke/HPKE#kem..encapcxx . . $ mlspp/hpke/KEM#encap(326a9ee7a29d5f25).(pkR);
410 auto ctx =
411 key_schedulecxx . . $ mlspp/hpke/HPKE#key_schedule(1d718a37a17310e5).(Modecxx . . $ mlspp/hpke/HPKE#Mode#::basecxx . . $ mlspp/hpke/HPKE#Mode#base., shared_secret, info, default_pskcxx . . $ mlspp/hpke/default_psk., default_psk_idcxx . . $ mlspp/hpke/default_psk_id.);
412 return stdcxx . . $ std/::make_paircxx . . $ std/make_pair(1d30b6e115203116).(enc, SenderContextcxx . . $ mlspp/hpke/SenderContext#(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(ctx)));
413 }
414
415 ReceiverContextcxx . . $ mlspp/hpke/ReceiverContext#
416 HPKEcxx . . $ mlspp/hpke/HPKE#::setup_base_rcxx . . $ mlspp/hpke/HPKE#setup_base_r(684b9f3e9f783afe).(const bytescxx . . $ mlspp/bytes_ns/bytes#& enc,
417 const KEMcxx . . $ mlspp/hpke/KEM#::PrivateKeycxx . . $ mlspp/hpke/KEM#PrivateKey#& skR,
418 const bytescxx . . $ mlspp/bytes_ns/bytes#& info) const
419 {
420 auto pkRm = kemcxx . . $ mlspp/hpke/HPKE#kem..serializecxx . . $ mlspp/hpke/KEM#serialize(f76243b94cf1479f).(*cxx . . $ std/unique_ptr#`operator*`(26c95836c1e8ca05).skR.public_keycxx . . $ mlspp/hpke/KEM#PrivateKey#public_key(db950997dbdd512b).());
421 auto shared_secret = kemcxx . . $ mlspp/hpke/HPKE#kem..decapcxx . . $ mlspp/hpke/KEM#decap(e80f053e4b107a3d).(enc, skR);
422 auto ctx =
423 key_schedulecxx . . $ mlspp/hpke/HPKE#key_schedule(1d718a37a17310e5).(Modecxx . . $ mlspp/hpke/HPKE#Mode#::basecxx . . $ mlspp/hpke/HPKE#Mode#base., shared_secret, info, default_pskcxx . . $ mlspp/hpke/default_psk., default_psk_idcxx . . $ mlspp/hpke/default_psk_id.);
424 return {cxx . . $ mlspp/hpke/ReceiverContext#ReceiverContext(e7dba9c45e997a81). stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(ctx) };
425 }
426
427 HPKEcxx . . $ mlspp/hpke/HPKE#::SenderInfocxx . . $ mlspp/hpke/HPKE#SenderInfo#
428 HPKEcxx . . $ mlspp/hpke/HPKE#::setup_psk_scxx . . $ mlspp/hpke/HPKE#setup_psk_s(7e4da0237b4cf0e9).(const KEMcxx . . $ mlspp/hpke/KEM#::PublicKeycxx . . $ mlspp/hpke/KEM#PublicKey#& pkR,
429 const bytescxx . . $ mlspp/bytes_ns/bytes#& info,
430 const bytescxx . . $ mlspp/bytes_ns/bytes#& psk,
431 const bytescxx . . $ mlspp/bytes_ns/bytes#& psk_id) const
432 {
433 auto [shared_secret, enc] = kemcxx . . $ mlspp/hpke/HPKE#kem..encapcxx . . $ mlspp/hpke/KEM#encap(326a9ee7a29d5f25).(pkR);
434 auto ctx = key_schedulecxx . . $ mlspp/hpke/HPKE#key_schedule(1d718a37a17310e5).(Modecxx . . $ mlspp/hpke/HPKE#Mode#::pskcxx . . $ mlspp/hpke/HPKE#Mode#psk., shared_secret, info, psk, psk_id);
435 return stdcxx . . $ std/::make_paircxx . . $ std/make_pair(1d30b6e115203116).(enc, SenderContextcxx . . $ mlspp/hpke/SenderContext#(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(ctx)));
436 }
437
438 ReceiverContextcxx . . $ mlspp/hpke/ReceiverContext#
439 HPKEcxx . . $ mlspp/hpke/HPKE#::setup_psk_rcxx . . $ mlspp/hpke/HPKE#setup_psk_r(f1a1ce203256edcc).(const bytescxx . . $ mlspp/bytes_ns/bytes#& enc,
440 const KEMcxx . . $ mlspp/hpke/KEM#::PrivateKeycxx . . $ mlspp/hpke/KEM#PrivateKey#& skR,
441 const bytescxx . . $ mlspp/bytes_ns/bytes#& info,
442 const bytescxx . . $ mlspp/bytes_ns/bytes#& psk,
443 const bytescxx . . $ mlspp/bytes_ns/bytes#& psk_id) const
444 {
445 auto shared_secret = kemcxx . . $ mlspp/hpke/HPKE#kem..decapcxx . . $ mlspp/hpke/KEM#decap(e80f053e4b107a3d).(enc, skR);
446 auto ctx = key_schedulecxx . . $ mlspp/hpke/HPKE#key_schedule(1d718a37a17310e5).(Modecxx . . $ mlspp/hpke/HPKE#Mode#::pskcxx . . $ mlspp/hpke/HPKE#Mode#psk., shared_secret, info, psk, psk_id);
447 return {cxx . . $ mlspp/hpke/ReceiverContext#ReceiverContext(e7dba9c45e997a81). stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(ctx) };
448 }
449
450 HPKEcxx . . $ mlspp/hpke/HPKE#::SenderInfocxx . . $ mlspp/hpke/HPKE#SenderInfo#
451 HPKEcxx . . $ mlspp/hpke/HPKE#::setup_auth_scxx . . $ mlspp/hpke/HPKE#setup_auth_s(5a497800a2e18ac8).(const KEMcxx . . $ mlspp/hpke/KEM#::PublicKeycxx . . $ mlspp/hpke/KEM#PublicKey#& pkR,
452 const bytescxx . . $ mlspp/bytes_ns/bytes#& info,
453 const KEMcxx . . $ mlspp/hpke/KEM#::PrivateKeycxx . . $ mlspp/hpke/KEM#PrivateKey#& skS) const
454 {
455 auto [shared_secret, enc] = kemcxx . . $ mlspp/hpke/HPKE#kem..auth_encapcxx . . $ mlspp/hpke/KEM#auth_encap(70a5a9729afdc401).(pkR, skS);
456 auto ctx =
457 key_schedulecxx . . $ mlspp/hpke/HPKE#key_schedule(1d718a37a17310e5).(Modecxx . . $ mlspp/hpke/HPKE#Mode#::authcxx . . $ mlspp/hpke/HPKE#Mode#auth., shared_secret, info, default_pskcxx . . $ mlspp/hpke/default_psk., default_psk_idcxx . . $ mlspp/hpke/default_psk_id.);
458 return stdcxx . . $ std/::make_paircxx . . $ std/make_pair(1d30b6e115203116).(enc, SenderContextcxx . . $ mlspp/hpke/SenderContext#(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(ctx)));
459 }
460
461 ReceiverContextcxx . . $ mlspp/hpke/ReceiverContext#
462 HPKEcxx . . $ mlspp/hpke/HPKE#::setup_auth_rcxx . . $ mlspp/hpke/HPKE#setup_auth_r(8f15f95bddbc3827).(const bytescxx . . $ mlspp/bytes_ns/bytes#& enc,
463 const KEMcxx . . $ mlspp/hpke/KEM#::PrivateKeycxx . . $ mlspp/hpke/KEM#PrivateKey#& skR,
464 const bytescxx . . $ mlspp/bytes_ns/bytes#& info,
465 const KEMcxx . . $ mlspp/hpke/KEM#::PublicKeycxx . . $ mlspp/hpke/KEM#PublicKey#& pkS) const
466 {
467 auto shared_secret = kemcxx . . $ mlspp/hpke/HPKE#kem..auth_decapcxx . . $ mlspp/hpke/KEM#auth_decap(ba74d0d94964aa1d).(enc, pkS, skR);
468 auto ctx =
469 key_schedulecxx . . $ mlspp/hpke/HPKE#key_schedule(1d718a37a17310e5).(Modecxx . . $ mlspp/hpke/HPKE#Mode#::authcxx . . $ mlspp/hpke/HPKE#Mode#auth., shared_secret, info, default_pskcxx . . $ mlspp/hpke/default_psk., default_psk_idcxx . . $ mlspp/hpke/default_psk_id.);
470 return {cxx . . $ mlspp/hpke/ReceiverContext#ReceiverContext(e7dba9c45e997a81). stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(ctx) };
471 }
472
473 HPKEcxx . . $ mlspp/hpke/HPKE#::SenderInfocxx . . $ mlspp/hpke/HPKE#SenderInfo#
474 HPKEcxx . . $ mlspp/hpke/HPKE#::setup_auth_psk_scxx . . $ mlspp/hpke/HPKE#setup_auth_psk_s(ac04f16ec08fdd3f).(const KEMcxx . . $ mlspp/hpke/KEM#::PublicKeycxx . . $ mlspp/hpke/KEM#PublicKey#& pkR,
475 const bytescxx . . $ mlspp/bytes_ns/bytes#& info,
476 const bytescxx . . $ mlspp/bytes_ns/bytes#& psk,
477 const bytescxx . . $ mlspp/bytes_ns/bytes#& psk_id,
478 const KEMcxx . . $ mlspp/hpke/KEM#::PrivateKeycxx . . $ mlspp/hpke/KEM#PrivateKey#& skS) const
479 {
480 auto [shared_secret, enc] = kemcxx . . $ mlspp/hpke/HPKE#kem..auth_encapcxx . . $ mlspp/hpke/KEM#auth_encap(70a5a9729afdc401).(pkR, skS);
481 auto ctx = key_schedulecxx . . $ mlspp/hpke/HPKE#key_schedule(1d718a37a17310e5).(Modecxx . . $ mlspp/hpke/HPKE#Mode#::auth_pskcxx . . $ mlspp/hpke/HPKE#Mode#auth_psk., shared_secret, info, psk, psk_id);
482 return stdcxx . . $ std/::make_paircxx . . $ std/make_pair(1d30b6e115203116).(enc, SenderContextcxx . . $ mlspp/hpke/SenderContext#(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(ctx)));
483 }
484
485 ReceiverContextcxx . . $ mlspp/hpke/ReceiverContext#
486 HPKEcxx . . $ mlspp/hpke/HPKE#::setup_auth_psk_rcxx . . $ mlspp/hpke/HPKE#setup_auth_psk_r(79a81ac41984436b).(const bytescxx . . $ mlspp/bytes_ns/bytes#& enc,
487 const KEMcxx . . $ mlspp/hpke/KEM#::PrivateKeycxx . . $ mlspp/hpke/KEM#PrivateKey#& skR,
488 const bytescxx . . $ mlspp/bytes_ns/bytes#& info,
489 const bytescxx . . $ mlspp/bytes_ns/bytes#& psk,
490 const bytescxx . . $ mlspp/bytes_ns/bytes#& psk_id,
491 const KEMcxx . . $ mlspp/hpke/KEM#::PublicKeycxx . . $ mlspp/hpke/KEM#PublicKey#& pkS) const
492 {
493 auto shared_secret = kemcxx . . $ mlspp/hpke/HPKE#kem..auth_decapcxx . . $ mlspp/hpke/KEM#auth_decap(ba74d0d94964aa1d).(enc, pkS, skR);
494 auto ctx = key_schedulecxx . . $ mlspp/hpke/HPKE#key_schedule(1d718a37a17310e5).(Modecxx . . $ mlspp/hpke/HPKE#Mode#::auth_pskcxx . . $ mlspp/hpke/HPKE#Mode#auth_psk., shared_secret, info, psk, psk_id);
495 return {cxx . . $ mlspp/hpke/ReceiverContext#ReceiverContext(e7dba9c45e997a81). stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(ctx) };
496 }
497
498 bool
499 HPKEcxx . . $ mlspp/hpke/HPKE#::verify_psk_inputscxx . . $ mlspp/hpke/HPKE#verify_psk_inputs(5eca935afb934f63).(Modecxx . . $ mlspp/hpke/HPKE#Mode# mode, const bytescxx . . $ mlspp/bytes_ns/bytes#& psk, const bytescxx . . $ mlspp/bytes_ns/bytes#& psk_id)
500 {
501 auto got_psk = (psk !=cxx . . $ mlspp/bytes_ns/bytes#`operator!=`(efd4f3c976ab92ce). default_pskcxx . . $ mlspp/hpke/default_psk.);
502 auto got_psk_id = (psk_id !=cxx . . $ mlspp/bytes_ns/bytes#`operator!=`(efd4f3c976ab92ce). default_psk_idcxx . . $ mlspp/hpke/default_psk_id.);
503 if (got_psk != got_psk_id) {
504 return false;
505 }
506
507 return (!got_psk && (mode == Modecxx . . $ mlspp/hpke/HPKE#Mode#::basecxx . . $ mlspp/hpke/HPKE#Mode#base. || mode == Modecxx . . $ mlspp/hpke/HPKE#Mode#::authcxx . . $ mlspp/hpke/HPKE#Mode#auth.)) ||
508 (got_psk && (mode == Modecxx . . $ mlspp/hpke/HPKE#Mode#::pskcxx . . $ mlspp/hpke/HPKE#Mode#psk. || mode == Modecxx . . $ mlspp/hpke/HPKE#Mode#::auth_pskcxx . . $ mlspp/hpke/HPKE#Mode#auth_psk.));
509 }
510
511 Contextcxx . . $ mlspp/hpke/Context#
512 HPKEcxx . . $ mlspp/hpke/HPKE#::key_schedulecxx . . $ mlspp/hpke/HPKE#key_schedule(1d718a37a17310e5).(Modecxx . . $ mlspp/hpke/HPKE#Mode# mode,
513 const bytescxx . . $ mlspp/bytes_ns/bytes#& shared_secret,
514 const bytescxx . . $ mlspp/bytes_ns/bytes#& info,
515 const bytescxx . . $ mlspp/bytes_ns/bytes#& psk,
516 const bytescxx . . $ mlspp/bytes_ns/bytes#& psk_id) const
517 {
518 if (!verify_psk_inputscxx . . $ mlspp/hpke/HPKE#verify_psk_inputs(5eca935afb934f63).(mode, psk, psk_id)) {
519 throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Invalid PSK inputs");
520 }
521
522 auto psk_id_hash =
523 kdfcxx . . $ mlspp/hpke/HPKE#kdf..labeled_extractcxx . . $ mlspp/hpke/KDF#labeled_extract(a2160eb270d8417e).(suitecxx . . $ mlspp/hpke/HPKE#suite., {cxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).}, label_psk_id_hashcxx . . $ mlspp/hpke/label_psk_id_hash(f9dc8c7ea672a8f6).(), psk_id);
524 auto info_hash = kdfcxx . . $ mlspp/hpke/HPKE#kdf..labeled_extractcxx . . $ mlspp/hpke/KDF#labeled_extract(a2160eb270d8417e).(suitecxx . . $ mlspp/hpke/HPKE#suite., {cxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).}, label_info_hashcxx . . $ mlspp/hpke/label_info_hash(f9dc8c7ea672a8f6).(), info);
525 auto mode_bytes = bytescxx . . $ mlspp/bytes_ns/bytes#bytes(162c049f8cb8078a).{ uint8_tcxx . . $ uint8_t#(mode) };
526 auto key_schedule_context = mode_bytes +cxx . . $ mlspp/bytes_ns/bytes#operator+(c5e1b85e16e94921). psk_id_hash +cxx . . $ mlspp/bytes_ns/bytes#operator+(c5e1b85e16e94921). info_hash;
527
528 auto secret = kdfcxx . . $ mlspp/hpke/HPKE#kdf..labeled_extractcxx . . $ mlspp/hpke/KDF#labeled_extract(a2160eb270d8417e).(suitecxx . . $ mlspp/hpke/HPKE#suite., shared_secret, label_secretcxx . . $ mlspp/hpke/label_secret(f9dc8c7ea672a8f6).(), psk);
529
530 auto key = kdfcxx . . $ mlspp/hpke/HPKE#kdf..labeled_expandcxx . . $ mlspp/hpke/KDF#labeled_expand(a3b1f2557e911f84).(
531 suitecxx . . $ mlspp/hpke/HPKE#suite., secret, label_keycxx . . $ mlspp/hpke/label_key(f9dc8c7ea672a8f6).(), key_schedule_context, aeadcxx . . $ mlspp/hpke/HPKE#aead..key_sizecxx . . $ mlspp/hpke/AEAD#key_size.);
532 auto nonce = kdfcxx . . $ mlspp/hpke/HPKE#kdf..labeled_expandcxx . . $ mlspp/hpke/KDF#labeled_expand(a3b1f2557e911f84).(
533 suitecxx . . $ mlspp/hpke/HPKE#suite., secret, label_base_noncecxx . . $ mlspp/hpke/label_base_nonce(f9dc8c7ea672a8f6).(), key_schedule_context, aeadcxx . . $ mlspp/hpke/HPKE#aead..nonce_sizecxx . . $ mlspp/hpke/AEAD#nonce_size.);
534 auto exporter_secret = kdfcxx . . $ mlspp/hpke/HPKE#kdf..labeled_expandcxx . . $ mlspp/hpke/KDF#labeled_expand(a3b1f2557e911f84).(
535 suitecxx . . $ mlspp/hpke/HPKE#suite., secret, label_expcxx . . $ mlspp/hpke/label_exp(f9dc8c7ea672a8f6).(), key_schedule_context, kdfcxx . . $ mlspp/hpke/HPKE#kdf..hash_sizecxx . . $ mlspp/hpke/KDF#hash_size.);
536
537 return {cxx . . $ mlspp/hpke/Context#Context(5ac4442da2c9d040). suitecxx . . $ mlspp/bytes_ns/bytes#bytes(68ca607eebb5be3e)., keycxx . . $ mlspp/bytes_ns/bytes#bytes(68ca607eebb5be3e)., noncecxx . . $ mlspp/bytes_ns/bytes#bytes(68ca607eebb5be3e)., exporter_secretcxx . . $ mlspp/bytes_ns/bytes#bytes(68ca607eebb5be3e)., kdfcxx . . $ mlspp/hpke/HPKE#kdf., aeadcxx . . $ mlspp/hpke/HPKE#aead. };
538 }
539
540 } // namespace mlspp::hpke