| 1 | #cxx . . $ `<file>/mlspp/lib/hpke/src/group.cpp`/include "group.h"cxx . . $ `<file>/mlspp/lib/hpke/src/group.h`/ |
| 2 | |
| 3 | #include <hpke/random.h>cxx . . $ `<file>/mlspp/lib/hpke/include/hpke/random.h`/ |
| 4 | |
| 5 | #include "common.h"cxx . . $ `<file>/mlspp/lib/hpke/src/common.h`/ |
| 6 | #include "openssl_common.h"cxx . . $ `<file>/mlspp/lib/hpke/src/openssl_common.h`/ |
| 7 | |
| 8 | #include "openssl/bn.h" |
| 9 | #include "openssl/ec.h" |
| 10 | #include "openssl/evp.h" |
| 11 | #include "openssl/obj_mac.h" |
| 12 | #if defined(WITH_OPENSSL3cxx . . $ `<built-in>:494:9`!) |
| 13 | #include "openssl/core_names.h" |
| 14 | #include "openssl/param_build.h" |
| 15 | #endif |
| 16 | |
| 17 | namespace mlsppcxx . . $ mlspp/::hpkecxx . . $ mlspp/hpke/ { |
| 18 | |
| 19 | static inline size_t |
| 20 | group_dh_sizecxx . . $ mlspp/hpke/group_dh_size(e1c07ae153638830).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID# group_id); |
| 21 | |
| 22 | static inline const EVP_MD* |
| 23 | group_sig_digestcxx . . $ mlspp/hpke/group_sig_digest(cc31db6b8ea23885).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID# group_id) |
| 24 | { |
| 25 | switch (group_id) { |
| 26 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P256cxx . . $ mlspp/hpke/Group#ID#P256.: |
| 27 | return EVP_sha256(); |
| 28 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P384cxx . . $ mlspp/hpke/Group#ID#P384.: |
| 29 | return EVP_sha384(); |
| 30 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P521cxx . . $ mlspp/hpke/Group#ID#P521.: |
| 31 | return EVP_sha512(); |
| 32 | |
| 33 | // EdDSA does its own hashing internally |
| 34 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::Ed25519cxx . . $ mlspp/hpke/Group#ID#Ed25519.: |
| 35 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::Ed448cxx . . $ mlspp/hpke/Group#ID#Ed448.: |
| 36 | return nullptr; |
| 37 | |
| 38 | // Groups not used for signature |
| 39 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::X25519cxx . . $ mlspp/hpke/Group#ID#X25519.: |
| 40 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::X448cxx . . $ mlspp/hpke/Group#ID#X448.: |
| 41 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Signature not supported for group"); |
| 42 | |
| 43 | default: |
| 44 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unknown group"); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /// |
| 49 | /// General implementation with OpenSSL EVP_PKEY |
| 50 | /// |
| 51 | |
| 52 | EVPGroupcxx . . $ mlspp/hpke/EVPGroup#::EVPGroupcxx . . $ mlspp/hpke/EVPGroup#EVPGroup(3802f28964769a07).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID# group_id, const KDFcxx . . $ mlspp/hpke/KDF#& kdf) |
| 53 | : Groupcxx . . $ mlspp/hpke/Group#(group_id, kdf) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | EVPGroupcxx . . $ mlspp/hpke/EVPGroup#::PublicKeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#::PublicKeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#PublicKey(a36cc1e96a19ea9b).(EVP_PKEY* pkey_in) |
| 58 | : pkeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#pkey.(pkey_in, typed_deletecxx . . $ mlspp/hpke/typed_delete(ebd0a1552f8ce24f).<EVP_PKEY>) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | EVPGroupcxx . . $ mlspp/hpke/EVPGroup#::PrivateKeycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#::PrivateKeycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#PrivateKey(a36cc1e96a19ea9b).(EVP_PKEY* pkey_in) |
| 63 | : pkeycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#pkey.(pkey_in, typed_deletecxx . . $ mlspp/hpke/typed_delete(ebd0a1552f8ce24f).<EVP_PKEY>) |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<Groupcxx . . $ mlspp/hpke/Group#::PublicKeycxx . . $ mlspp/hpke/Group#PublicKey#> |
| 68 | EVPGroupcxx . . $ mlspp/hpke/EVPGroup#::PrivateKeycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#::public_keycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#public_key(be74f74a136692ce).() const |
| 69 | { |
| 70 | if (1 != EVP_PKEY_up_ref(pkeycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).())) { |
| 71 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 72 | } |
| 73 | return stdcxx . . $ std/unique_ptr#unique_ptr(8bcafd3b0b660df8).::make_uniquecxx . . $ std/make_unique(5cd499e8540a10bb).<PublicKeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#>(pkeycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()); |
| 74 | } |
| 75 | |
| 76 | stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<Groupcxx . . $ mlspp/hpke/Group#::PrivateKeycxx . . $ mlspp/hpke/Group#PrivateKey#> |
| 77 | EVPGroupcxx . . $ mlspp/hpke/EVPGroup#::generate_key_paircxx . . $ mlspp/hpke/EVPGroup#generate_key_pair(1c1051ed47dfeb34).() const |
| 78 | { |
| 79 | return derive_key_paircxx . . $ mlspp/hpke/Group#derive_key_pair(7c8b5279bfce12cf).({cxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).}, random_bytescxx . . $ mlspp/hpke/random_bytes(b7161179e0ed6f95).(sk_sizecxx . . $ mlspp/hpke/Group#sk_size.)); |
| 80 | } |
| 81 | |
| 82 | bytescxx . . $ mlspp/bytes_ns/bytes# |
| 83 | EVPGroupcxx . . $ mlspp/hpke/EVPGroup#::dhcxx . . $ mlspp/hpke/EVPGroup#dh(5515d6704683bf5b).(const Groupcxx . . $ mlspp/hpke/Group#::PrivateKeycxx . . $ mlspp/hpke/Group#PrivateKey#& sk, const Groupcxx . . $ mlspp/hpke/Group#::PublicKeycxx . . $ mlspp/hpke/Group#PublicKey#& pk) const |
| 84 | { |
| 85 | const auto& rsk = dynamic_cast<const PrivateKeycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#&>(sk); |
| 86 | const auto& rpk = dynamic_cast<const PublicKeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#&>(pk); |
| 87 | |
| 88 | // This and the next line are acceptable because the OpenSSL |
| 89 | // functions fail to mark the required EVP_PKEYs as const, even |
| 90 | // though they are not modified. |
| 91 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
| 92 | auto* priv_pkey = const_cast<EVP_PKEY*>(rsk.pkeycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()); |
| 93 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
| 94 | auto* pub_pkey = const_cast<EVP_PKEY*>(rpk.pkeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()); |
| 95 | |
| 96 | auto ctx = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(EVP_PKEY_CTX_new(priv_pkey, nullptr)); |
| 97 | if (ctx ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) { |
| 98 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 99 | } |
| 100 | |
| 101 | if (1 != EVP_PKEY_derive_init(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).())) { |
| 102 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 103 | } |
| 104 | |
| 105 | if (1 != EVP_PKEY_derive_set_peer(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), pub_pkey)) { |
| 106 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 107 | } |
| 108 | |
| 109 | size_t out_len = 0; |
| 110 | if (1 != EVP_PKEY_derive(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), nullptr, &out_len)) { |
| 111 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 112 | } |
| 113 | |
| 114 | bytescxx . . $ mlspp/bytes_ns/bytes# outcxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).(out_len); |
| 115 | uint8_tcxx . . $ uint8_t#* ptr = out.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(); |
| 116 | if (1 != (EVP_PKEY_derive(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), ptr, &out_len))) { |
| 117 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 118 | } |
| 119 | |
| 120 | return outcxx . . $ mlspp/bytes_ns/bytes#bytes(628b76f52f2a67f).; |
| 121 | } |
| 122 | |
| 123 | bytescxx . . $ mlspp/bytes_ns/bytes# |
| 124 | EVPGroupcxx . . $ mlspp/hpke/EVPGroup#::signcxx . . $ mlspp/hpke/EVPGroup#sign(5f3ce07bf36d85db).(const bytescxx . . $ mlspp/bytes_ns/bytes#& data, const Groupcxx . . $ mlspp/hpke/Group#::PrivateKeycxx . . $ mlspp/hpke/Group#PrivateKey#& sk) const |
| 125 | { |
| 126 | const auto& rsk = dynamic_cast<const PrivateKeycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#&>(sk); |
| 127 | |
| 128 | auto ctx = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(EVP_MD_CTX_createcxx . . $ `<external>/2497497914425334111/evp.h:725:9`!()); |
| 129 | if (ctx ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) { |
| 130 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 131 | } |
| 132 | |
| 133 | const auto* digest = group_sig_digestcxx . . $ mlspp/hpke/group_sig_digest(cc31db6b8ea23885).(idcxx . . $ mlspp/hpke/Group#id.); |
| 134 | if (1 != |
| 135 | EVP_DigestSignInit(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), nullptr, digest, nullptr, rsk.pkeycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).())) { |
| 136 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 137 | } |
| 138 | |
| 139 | size_t siglen = EVP_PKEY_sizecxx . . $ `<external>/2497497914425334111/evp.h:1374:9`!(rsk.pkeycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()); |
| 140 | bytescxx . . $ mlspp/bytes_ns/bytes# sigcxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).(siglen); |
| 141 | if (1 != EVP_DigestSign( |
| 142 | ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), sig.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(), &siglen, data.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), data.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).())) { |
| 143 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 144 | } |
| 145 | |
| 146 | sig.resizecxx . . $ mlspp/bytes_ns/bytes#resize(2c44cd906ae4e829).(siglen); |
| 147 | return sigcxx . . $ mlspp/bytes_ns/bytes#bytes(628b76f52f2a67f).; |
| 148 | } |
| 149 | |
| 150 | bool |
| 151 | EVPGroupcxx . . $ mlspp/hpke/EVPGroup#::verifycxx . . $ mlspp/hpke/EVPGroup#verify(aac7f32cb6307656).(const bytescxx . . $ mlspp/bytes_ns/bytes#& data, |
| 152 | const bytescxx . . $ mlspp/bytes_ns/bytes#& sig, |
| 153 | const Groupcxx . . $ mlspp/hpke/Group#::PublicKeycxx . . $ mlspp/hpke/Group#PublicKey#& pk) const |
| 154 | { |
| 155 | const auto& rpk = dynamic_cast<const PublicKeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#&>(pk); |
| 156 | |
| 157 | auto ctx = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(EVP_MD_CTX_createcxx . . $ `<external>/2497497914425334111/evp.h:725:9`!()); |
| 158 | if (ctx ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) { |
| 159 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 160 | } |
| 161 | |
| 162 | const auto* digest = group_sig_digestcxx . . $ mlspp/hpke/group_sig_digest(cc31db6b8ea23885).(idcxx . . $ mlspp/hpke/Group#id.); |
| 163 | if (1 != EVP_DigestVerifyInit( |
| 164 | ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), nullptr, digest, nullptr, rpk.pkeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).())) { |
| 165 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 166 | } |
| 167 | |
| 168 | auto rv = EVP_DigestVerify( |
| 169 | ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), sig.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), sig.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).(), data.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), data.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).()); |
| 170 | |
| 171 | return rv == 1; |
| 172 | } |
| 173 | |
| 174 | /// |
| 175 | /// DH over "normal" curves |
| 176 | /// |
| 177 | |
| 178 | struct ECKeyGroupcxx . . $ mlspp/hpke/ECKeyGroup# : public EVPGroupcxx . . $ mlspp/hpke/EVPGroup# |
| 179 | { |
| 180 | ECKeyGroupcxx . . $ mlspp/hpke/ECKeyGroup#ECKeyGroup(3802f28964769a07).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID# group_id, const KDFcxx . . $ mlspp/hpke/KDF#& kdf) |
| 181 | : EVPGroupcxx . . $ mlspp/hpke/EVPGroup#(group_id, kdf) |
| 182 | , curve_nidcxx . . $ mlspp/hpke/ECKeyGroup#curve_nid.(group_to_nidcxx . . $ mlspp/hpke/ECKeyGroup#group_to_nid(ebba8658cbbd2b35).(group_id)) |
| 183 | { |
| 184 | } |
| 185 | |
| 186 | #if defined(WITH_OPENSSL3cxx . . $ `<built-in>:494:9`!) |
| 187 | typed_unique_ptrcxx . . $ mlspp/hpke/typed_unique_ptr#<EVP_PKEY> keypair_evp_keycxx . . $ mlspp/hpke/ECKeyGroup#keypair_evp_key(ebdc44d674a56cd6).( |
| 188 | const typed_unique_ptrcxx . . $ mlspp/hpke/typed_unique_ptr#<BIGNUM>& priv) const |
| 189 | { |
| 190 | const auto* name = OBJ_nid2sn(curve_nidcxx . . $ mlspp/hpke/ECKeyGroup#curve_nid.); |
| 191 | if (name == nullptr) { |
| 192 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unsupported algorithm"); |
| 193 | } |
| 194 | |
| 195 | auto group = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).( |
| 196 | EC_GROUP_new_by_curve_name_ex(nullptr, nullptr, curve_nidcxx . . $ mlspp/hpke/ECKeyGroup#curve_nid.)); |
| 197 | if (group ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) { |
| 198 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 199 | } |
| 200 | |
| 201 | auto pt = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(EC_POINT_new(group.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).())); |
| 202 | if (pt ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) { |
| 203 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 204 | } |
| 205 | |
| 206 | if (1 != EC_POINT_mul( |
| 207 | group.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), pt.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), priv.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), nullptr, nullptr, nullptr)) { |
| 208 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 209 | } |
| 210 | |
| 211 | const auto pt_size = EC_POINT_point2oct(group.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), |
| 212 | pt.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), |
| 213 | POINT_CONVERSION_UNCOMPRESSED, |
| 214 | nullptr, |
| 215 | 0, |
| 216 | nullptr); |
| 217 | if (0 == pt_size) { |
| 218 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 219 | } |
| 220 | |
| 221 | bytescxx . . $ mlspp/bytes_ns/bytes# pubcxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).(pt_size); |
| 222 | if (EC_POINT_point2oct(group.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), |
| 223 | pt.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), |
| 224 | POINT_CONVERSION_UNCOMPRESSED, |
| 225 | pub.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(), |
| 226 | pt_size, |
| 227 | nullptr) != pt_size) { |
| 228 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 229 | } |
| 230 | |
| 231 | auto builder = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(OSSL_PARAM_BLD_new()); |
| 232 | if (builder ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr || |
| 233 | 1 != OSSL_PARAM_BLD_push_utf8_string( |
| 234 | builder.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), OSSL_PKEY_PARAM_GROUP_NAMEcxx . . $ `<external>/16618910896700259571/core_names.h:420:10`!, name, 0) || |
| 235 | 1 != OSSL_PARAM_BLD_push_BN( |
| 236 | builder.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), OSSL_PKEY_PARAM_PRIV_KEYcxx . . $ `<external>/16618910896700259571/core_names.h:439:10`!, priv.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()) || |
| 237 | 1 != |
| 238 | OSSL_PARAM_BLD_push_octet_string( |
| 239 | builder.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), OSSL_PKEY_PARAM_PUB_KEYcxx . . $ `<external>/16618910896700259571/core_names.h:441:10`!, pub.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(), pub.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).())) { |
| 240 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 241 | } |
| 242 | |
| 243 | auto params = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(OSSL_PARAM_BLD_to_param(builder.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).())); |
| 244 | auto ctx = |
| 245 | make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(EVP_PKEY_CTX_new_from_name(nullptr, "EC", nullptr)); |
| 246 | auto key = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(EVP_PKEY_new()); |
| 247 | auto* key_ptr = key.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(); |
| 248 | if (params ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr || ctx ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr || key ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr || |
| 249 | EVP_PKEY_fromdata_init(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()) <= 0 || |
| 250 | EVP_PKEY_fromdata( |
| 251 | ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), &key_ptr, EVP_PKEY_KEYPAIRcxx . . $ `<external>/2497497914425334111/evp.h:112:9`!, params.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()) <= 0) { |
| 252 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 253 | } |
| 254 | ctx.resetcxx . . $ std/unique_ptr#reset(c8c3025d73122d7d).(); |
| 255 | |
| 256 | ctx =cxx . . $ std/unique_ptr#`operator=`(1b63e42eaa758a73). make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).( |
| 257 | EVP_PKEY_CTX_new_from_pkey(nullptr, key.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), nullptr)); |
| 258 | if (EVP_PKEY_check(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()) <= 0) { |
| 259 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 260 | } |
| 261 | |
| 262 | return keycxx . . $ std/unique_ptr#unique_ptr(d6762fce684538e4).; |
| 263 | } |
| 264 | |
| 265 | typed_unique_ptrcxx . . $ mlspp/hpke/typed_unique_ptr#<EVP_PKEY> public_evp_keycxx . . $ mlspp/hpke/ECKeyGroup#public_evp_key(f6b3ef0ce83ebc29).(const bytescxx . . $ mlspp/bytes_ns/bytes#& pub) const |
| 266 | { |
| 267 | const auto* name = OBJ_nid2sn(curve_nidcxx . . $ mlspp/hpke/ECKeyGroup#curve_nid.); |
| 268 | if (name == nullptr) { |
| 269 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unsupported algorithm"); |
| 270 | } |
| 271 | |
| 272 | auto group = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).( |
| 273 | EC_GROUP_new_by_curve_name_ex(nullptr, nullptr, curve_nidcxx . . $ mlspp/hpke/ECKeyGroup#curve_nid.)); |
| 274 | if (group ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) { |
| 275 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 276 | } |
| 277 | |
| 278 | auto builder = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(OSSL_PARAM_BLD_new()); |
| 279 | if (builder ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr || |
| 280 | 1 != OSSL_PARAM_BLD_push_utf8_string( |
| 281 | builder.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), OSSL_PKEY_PARAM_GROUP_NAMEcxx . . $ `<external>/16618910896700259571/core_names.h:420:10`!, name, 0) || |
| 282 | 1 != |
| 283 | OSSL_PARAM_BLD_push_octet_string( |
| 284 | builder.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), OSSL_PKEY_PARAM_PUB_KEYcxx . . $ `<external>/16618910896700259571/core_names.h:441:10`!, pub.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), pub.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).())) { |
| 285 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 286 | } |
| 287 | |
| 288 | auto params = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(OSSL_PARAM_BLD_to_param(builder.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).())); |
| 289 | auto ctx = |
| 290 | make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(EVP_PKEY_CTX_new_from_name(nullptr, "EC", nullptr)); |
| 291 | auto key = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(EVP_PKEY_new()); |
| 292 | auto* key_ptr = key.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(); |
| 293 | if (params ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr || ctx ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr || key ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr || |
| 294 | EVP_PKEY_fromdata_init(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()) <= 0 || |
| 295 | EVP_PKEY_fromdata( |
| 296 | ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), &key_ptr, EVP_PKEY_KEYPAIRcxx . . $ `<external>/2497497914425334111/evp.h:112:9`!, params.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()) <= 0) { |
| 297 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 298 | } |
| 299 | ctx.resetcxx . . $ std/unique_ptr#reset(c8c3025d73122d7d).(); |
| 300 | |
| 301 | ctx =cxx . . $ std/unique_ptr#`operator=`(1b63e42eaa758a73). make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).( |
| 302 | EVP_PKEY_CTX_new_from_pkey(nullptr, key.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), nullptr)); |
| 303 | if (EVP_PKEY_public_check(ctx.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()) <= 0) { |
| 304 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 305 | } |
| 306 | |
| 307 | return keycxx . . $ std/unique_ptr#unique_ptr(d6762fce684538e4).; |
| 308 | } |
| 309 | #endif |
| 310 | |
| 311 | stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<Groupcxx . . $ mlspp/hpke/Group#::PrivateKeycxx . . $ mlspp/hpke/Group#PrivateKey#> derive_key_paircxx . . $ mlspp/hpke/ECKeyGroup#derive_key_pair(7c8b5279bfce12cf).( |
| 312 | const bytescxx . . $ mlspp/bytes_ns/bytes#& suite_id, |
| 313 | const bytescxx . . $ mlspp/bytes_ns/bytes#& ikm) const override |
| 314 | { |
| 315 | static const int retry_limit = 255; |
| 316 | static const auto label_dkp_prk = from_asciicxx . . $ mlspp/bytes_ns/from_ascii(fc01d114001b7256).("dkp_prk"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 317 | static const auto label_candidate = from_asciicxx . . $ mlspp/bytes_ns/from_ascii(fc01d114001b7256).("candidate"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 318 | |
| 319 | auto dkp_prk = kdfcxx . . $ mlspp/hpke/Group#kdf..labeled_extractcxx . . $ mlspp/hpke/KDF#labeled_extract(a2160eb270d8417e).(suite_id, {cxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).}, label_dkp_prk, ikm); |
| 320 | |
| 321 | #if defined(WITH_OPENSSL3cxx . . $ `<built-in>:494:9`!) |
| 322 | auto* group = EC_GROUP_new_by_curve_name_ex(nullptr, nullptr, curve_nidcxx . . $ mlspp/hpke/ECKeyGroup#curve_nid.); |
| 323 | auto group_ptr = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(group); |
| 324 | #else |
| 325 | auto eckey = new_ec_key(); |
| 326 | const auto* group = EC_KEY_get0_group(eckey.get()); |
| 327 | #endif |
| 328 | |
| 329 | auto order = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(BN_new()); |
| 330 | if (1 != EC_GROUP_get_order(group, order.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), nullptr)) { |
| 331 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 332 | } |
| 333 | |
| 334 | auto sk = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(BN_new()); |
| 335 | BN_zerocxx . . $ `<external>/8357373700258316281/bn.h:201:9`!(sk.get()); |
| 336 | |
| 337 | auto counter = int(0); |
| 338 | while (BN_is_zero(sk.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()) != 0 || BN_cmp(sk.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), order.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).()) != -1) { |
| 339 | auto ctr = i2ospcxx . . $ mlspp/hpke/i2osp(b57ad3058a1ef3ae).(counter, 1); |
| 340 | auto candidate = |
| 341 | kdfcxx . . $ mlspp/hpke/Group#kdf..labeled_expandcxx . . $ mlspp/hpke/KDF#labeled_expand(a3b1f2557e911f84).(suite_id, dkp_prk, label_candidate, ctr, sk_sizecxx . . $ mlspp/hpke/Group#sk_size.); |
| 342 | candidate.atcxx . . $ mlspp/bytes_ns/bytes#at(b19923e35e3d04d7).(0) &= bitmaskcxx . . $ mlspp/hpke/ECKeyGroup#bitmask(bd807b21b9713a30).(); |
| 343 | sk.resetcxx . . $ std/unique_ptr#reset(c8c3025d73122d7d).(BN_bin2bn( |
| 344 | candidate.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(), static_cast<int>(candidate.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).()), nullptr)); |
| 345 | |
| 346 | counter += 1; |
| 347 | if (counter > retry_limit) { |
| 348 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("DeriveKeyPair iteration limit exceeded"); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | #if defined(WITH_OPENSSL3cxx . . $ `<built-in>:494:9`!) |
| 353 | auto key = keypair_evp_keycxx . . $ mlspp/hpke/ECKeyGroup#keypair_evp_key(ebdc44d674a56cd6).(sk); |
| 354 | return stdcxx . . $ std/unique_ptr#unique_ptr(8bcafd3b0b660df8).::make_uniquecxx . . $ std/make_unique(5cd499e8540a10bb).<EVPGroupcxx . . $ mlspp/hpke/EVPGroup#::PrivateKeycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#>(key.releasecxx . . $ std/unique_ptr#release(aad4c3669c5e940c).()); |
| 355 | #else |
| 356 | auto pt = make_typed_unique(EC_POINT_new(group)); |
| 357 | EC_POINT_mul(group, pt.get(), sk.get(), nullptr, nullptr, nullptr); |
| 358 | |
| 359 | EC_KEY_set_private_key(eckey.get(), sk.get()); |
| 360 | EC_KEY_set_public_key(eckey.get(), pt.get()); |
| 361 | |
| 362 | auto pkey = to_pkey(eckey.release()); |
| 363 | return std::make_unique<PrivateKey>(pkey.release()); |
| 364 | #endif |
| 365 | } |
| 366 | |
| 367 | bytescxx . . $ mlspp/bytes_ns/bytes# serializecxx . . $ mlspp/hpke/ECKeyGroup#serialize(709e30fcbf6c1f24).(const Groupcxx . . $ mlspp/hpke/Group#::PublicKeycxx . . $ mlspp/hpke/Group#PublicKey#& pk) const override |
| 368 | { |
| 369 | const auto& rpk = dynamic_cast<const PublicKeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#&>(pk); |
| 370 | #if defined(WITH_OPENSSL3cxx . . $ `<built-in>:494:9`!) |
| 371 | OSSL_PARAM* param = nullptr; |
| 372 | if (1 != EVP_PKEY_todata(rpk.pkeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), EVP_PKEY_PUBLIC_KEYcxx . . $ `<external>/2497497914425334111/evp.h:110:9`!, ¶m)) { |
| 373 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 374 | } |
| 375 | auto param_ptr = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(param); |
| 376 | |
| 377 | const OSSL_PARAM* pk_param = |
| 378 | OSSL_PARAM_locate_const(param_ptr.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), OSSL_PKEY_PARAM_PUB_KEYcxx . . $ `<external>/16618910896700259571/core_names.h:441:10`!); |
| 379 | if (pk_param == nullptr) { |
| 380 | return bytescxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).({}, 0); |
| 381 | } |
| 382 | |
| 383 | size_t len = 0; |
| 384 | if (1 != OSSL_PARAM_get_octet_string(pk_param, nullptr, 0, &len)) { |
| 385 | return bytescxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).({}, 0); |
| 386 | } |
| 387 | |
| 388 | bytescxx . . $ mlspp/bytes_ns/bytes# bufcxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).(len); |
| 389 | void* data_ptr = buf.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(); |
| 390 | if (1 != OSSL_PARAM_get_octet_string(pk_param, &data_ptr, len, nullptr)) { |
| 391 | return bytescxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).({}, 0); |
| 392 | } |
| 393 | |
| 394 | // Prior to OpenSSL 3.0.8, we will always get compressed point from |
| 395 | // OSSL_PKEY_PARAM_PUB_KEY, so we will have to do the following conversion. |
| 396 | // From OpenSSL 3.0.8, we can obtain the uncompressed point value by setting |
| 397 | // OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT appropriately. |
| 398 | auto group = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).( |
| 399 | EC_GROUP_new_by_curve_name_ex(nullptr, nullptr, curve_nidcxx . . $ mlspp/hpke/ECKeyGroup#curve_nid.)); |
| 400 | if (group ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) { |
| 401 | return bytescxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).({}, 0); |
| 402 | } |
| 403 | auto point = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(EC_POINT_new(group.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).())); |
| 404 | const auto* oct_ptr = static_cast<const unsigned char*>(data_ptr); |
| 405 | if (1 != |
| 406 | EC_POINT_oct2point(group.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), point.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), oct_ptr, len, nullptr)) { |
| 407 | return bytescxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).({}, 0); |
| 408 | } |
| 409 | len = EC_POINT_point2oct(group.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), |
| 410 | point.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), |
| 411 | POINT_CONVERSION_UNCOMPRESSED, |
| 412 | nullptr, |
| 413 | 0, |
| 414 | nullptr); |
| 415 | if (0 == len) { |
| 416 | return bytescxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).({}, 0); |
| 417 | } |
| 418 | bytescxx . . $ mlspp/bytes_ns/bytes# outcxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).(len); |
| 419 | auto* data = out.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(); |
| 420 | if (EC_POINT_point2oct(group.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), |
| 421 | point.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), |
| 422 | POINT_CONVERSION_UNCOMPRESSED, |
| 423 | data, |
| 424 | len, |
| 425 | nullptr) != len) { |
| 426 | return bytescxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).({}, 0); |
| 427 | } |
| 428 | #else |
| 429 | auto* pub = EVP_PKEY_get0_EC_KEY(rpk.pkey.get()); |
| 430 | |
| 431 | auto len = i2o_ECPublicKey(pub, nullptr); |
| 432 | if (len != static_cast<int>(pk_size)) { |
| 433 | throw openssl_error(); |
| 434 | } |
| 435 | |
| 436 | bytes out(len); |
| 437 | auto* data = out.data(); |
| 438 | if (i2o_ECPublicKey(pub, &data) == 0) { |
| 439 | throw openssl_error(); |
| 440 | } |
| 441 | #endif |
| 442 | return outcxx . . $ mlspp/bytes_ns/bytes#bytes(628b76f52f2a67f).; |
| 443 | } |
| 444 | |
| 445 | stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<Groupcxx . . $ mlspp/hpke/Group#::PublicKeycxx . . $ mlspp/hpke/Group#PublicKey#> deserializecxx . . $ mlspp/hpke/ECKeyGroup#deserialize(6b5fed5c6c0b0d85).(const bytescxx . . $ mlspp/bytes_ns/bytes#& enc) const override |
| 446 | { |
| 447 | #if defined(WITH_OPENSSL3cxx . . $ `<built-in>:494:9`!) |
| 448 | auto key = public_evp_keycxx . . $ mlspp/hpke/ECKeyGroup#public_evp_key(f6b3ef0ce83ebc29).(enc); |
| 449 | if (key ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) { |
| 450 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unable to deserialize the public key"); |
| 451 | } |
| 452 | return stdcxx . . $ std/unique_ptr#unique_ptr(8bcafd3b0b660df8).::make_uniquecxx . . $ std/make_unique(5cd499e8540a10bb).<EVPGroupcxx . . $ mlspp/hpke/EVPGroup#::PublicKeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#>(key.releasecxx . . $ std/unique_ptr#release(aad4c3669c5e940c).()); |
| 453 | #else |
| 454 | auto eckey = new_ec_key(); |
| 455 | auto* eckey_ptr = eckey.get(); |
| 456 | const auto* data_ptr = enc.data(); |
| 457 | if (nullptr == |
| 458 | o2i_ECPublicKey(&eckey_ptr, |
| 459 | &data_ptr, |
| 460 | static_cast<long>( // NOLINT(google-runtime-int) |
| 461 | enc.size()))) { |
| 462 | throw openssl_error(); |
| 463 | } |
| 464 | |
| 465 | auto pkey = to_pkey(eckey.release()); |
| 466 | return std::make_unique<EVPGroup::PublicKey>(pkey.release()); |
| 467 | #endif |
| 468 | } |
| 469 | |
| 470 | bytescxx . . $ mlspp/bytes_ns/bytes# serialize_privatecxx . . $ mlspp/hpke/ECKeyGroup#serialize_private(3111aac385788878).(const Groupcxx . . $ mlspp/hpke/Group#::PrivateKeycxx . . $ mlspp/hpke/Group#PrivateKey#& sk) const override |
| 471 | { |
| 472 | const auto& rsk = dynamic_cast<const PrivateKeycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#&>(sk); |
| 473 | #if defined(WITH_OPENSSL3cxx . . $ `<built-in>:494:9`!) |
| 474 | OSSL_PARAM* param = nullptr; |
| 475 | if (1 != EVP_PKEY_todata(rsk.pkeycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), EVP_PKEY_KEYPAIRcxx . . $ `<external>/2497497914425334111/evp.h:112:9`!, ¶m)) { |
| 476 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 477 | } |
| 478 | auto param_ptr = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(param); |
| 479 | |
| 480 | const OSSL_PARAM* sk_param = |
| 481 | OSSL_PARAM_locate_const(param_ptr.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), OSSL_PKEY_PARAM_PRIV_KEYcxx . . $ `<external>/16618910896700259571/core_names.h:439:10`!); |
| 482 | if (sk_param == nullptr) { |
| 483 | return bytescxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).({}, 0); |
| 484 | } |
| 485 | |
| 486 | BIGNUM* d = nullptr; |
| 487 | if (1 != OSSL_PARAM_get_BN(sk_param, &d)) { |
| 488 | return bytescxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).({}, 0); |
| 489 | } |
| 490 | auto d_ptr = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(d); |
| 491 | #else |
| 492 | auto* eckey = EVP_PKEY_get0_EC_KEY(rsk.pkey.get()); |
| 493 | const auto* d = EC_KEY_get0_private_key(eckey); |
| 494 | #endif |
| 495 | |
| 496 | auto out = bytescxx . . $ mlspp/bytes_ns/bytes#(BN_num_bytescxx . . $ `<external>/8357373700258316281/bn.h:188:9`!(d)); |
| 497 | #if WITH_BORINGSSL |
| 498 | // In BoringSSL, BN_bn2bin returns size_t |
| 499 | const auto out_size = out.size(); |
| 500 | #else |
| 501 | // In OpenSSL, BN_bn2bin returns int |
| 502 | const auto out_size = static_cast<int>(out.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).()); |
| 503 | #endif |
| 504 | if (BN_bn2bin(d, out.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).()) != out_size) { |
| 505 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 506 | } |
| 507 | |
| 508 | const auto zeros_needed = group_dh_sizecxx . . $ mlspp/hpke/group_dh_size(e1c07ae153638830).(idcxx . . $ mlspp/hpke/Group#id.) - out.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).(); |
| 509 | auto leading_zeros = bytescxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).(zeros_needed, 0); |
| 510 | return leading_zeros +cxx . . $ mlspp/bytes_ns/bytes#operator+(c5e1b85e16e94921). out; |
| 511 | } |
| 512 | |
| 513 | stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<Groupcxx . . $ mlspp/hpke/Group#::PrivateKeycxx . . $ mlspp/hpke/Group#PrivateKey#> deserialize_privatecxx . . $ mlspp/hpke/ECKeyGroup#deserialize_private(9261b7df97b3e19f).( |
| 514 | const bytescxx . . $ mlspp/bytes_ns/bytes#& skm) const override |
| 515 | { |
| 516 | #if defined(WITH_OPENSSL3cxx . . $ `<built-in>:494:9`!) |
| 517 | auto priv = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).( |
| 518 | BN_bin2bn(skm.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), static_cast<int>(skm.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).()), nullptr)); |
| 519 | if (priv ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) { |
| 520 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unable to deserialize the private key"); |
| 521 | } |
| 522 | auto key = keypair_evp_keycxx . . $ mlspp/hpke/ECKeyGroup#keypair_evp_key(ebdc44d674a56cd6).(priv); |
| 523 | return stdcxx . . $ std/unique_ptr#unique_ptr(8bcafd3b0b660df8).::make_uniquecxx . . $ std/make_unique(5cd499e8540a10bb).<EVPGroupcxx . . $ mlspp/hpke/EVPGroup#::PrivateKeycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#>(key.releasecxx . . $ std/unique_ptr#release(aad4c3669c5e940c).()); |
| 524 | #else |
| 525 | auto eckey = new_ec_key(); |
| 526 | const auto* group = EC_KEY_get0_group(eckey.get()); |
| 527 | const auto d = make_typed_unique( |
| 528 | BN_bin2bn(skm.data(), static_cast<int>(skm.size()), nullptr)); |
| 529 | auto pt = make_typed_unique(EC_POINT_new(group)); |
| 530 | |
| 531 | EC_POINT_mul(group, pt.get(), d.get(), nullptr, nullptr, nullptr); |
| 532 | EC_KEY_set_private_key(eckey.get(), d.get()); |
| 533 | EC_KEY_set_public_key(eckey.get(), pt.get()); |
| 534 | |
| 535 | auto pkey = to_pkey(eckey.release()); |
| 536 | return std::make_unique<EVPGroup::PrivateKey>(pkey.release()); |
| 537 | #endif |
| 538 | } |
| 539 | |
| 540 | // EC Key |
| 541 | stdcxx . . $ std/::tuplecxx . . $ std/tuple#<bytescxx . . $ mlspp/bytes_ns/bytes#, bytescxx . . $ mlspp/bytes_ns/bytes#> coordinatescxx . . $ mlspp/hpke/ECKeyGroup#coordinates(4ed3c5dd9bcc18fa).( |
| 542 | const Groupcxx . . $ mlspp/hpke/Group#::PublicKeycxx . . $ mlspp/hpke/Group#PublicKey#& pk) const override |
| 543 | { |
| 544 | auto bn_x = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(BN_new()); |
| 545 | auto bn_y = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(BN_new()); |
| 546 | const auto& rpk = dynamic_cast<const PublicKeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#&>(pk); |
| 547 | |
| 548 | #if defined(WITH_OPENSSL3cxx . . $ `<built-in>:494:9`!) |
| 549 | // Raw pointer OK here because it becomes managed as soon as possible |
| 550 | OSSL_PARAM* param_ptr = nullptr; |
| 551 | if (1 != EVP_PKEY_todata(rpk.pkeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), EVP_PKEY_PUBLIC_KEYcxx . . $ `<external>/2497497914425334111/evp.h:110:9`!, ¶m_ptr)) { |
| 552 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 553 | } |
| 554 | |
| 555 | auto param = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(param_ptr); |
| 556 | |
| 557 | // Raw pointer OK here because it is non-owning |
| 558 | const auto* pk_param = |
| 559 | OSSL_PARAM_locate_const(param.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), OSSL_PKEY_PARAM_PUB_KEYcxx . . $ `<external>/16618910896700259571/core_names.h:441:10`!); |
| 560 | if (pk_param == nullptr) { |
| 561 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Failed to locate OSSL_PKEY_PARAM_PUB_KEY"); |
| 562 | } |
| 563 | |
| 564 | // Copy the octet string representation of the key into a buffer |
| 565 | auto len = size_t(0); |
| 566 | if (1 != OSSL_PARAM_get_octet_string(pk_param, nullptr, 0, &len)) { |
| 567 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Failed to get OSSL_PKEY_PARAM_PUB_KEY len"); |
| 568 | } |
| 569 | |
| 570 | auto buf = bytescxx . . $ mlspp/bytes_ns/bytes#(len); |
| 571 | auto* buf_ptr = buf.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(); |
| 572 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 573 | auto* buf_ptr_void = reinterpret_cast<void*>(buf_ptr); |
| 574 | if (1 != |
| 575 | OSSL_PARAM_get_octet_string(pk_param, &buf_ptr_void, len, nullptr)) { |
| 576 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Failed to get OSSL_PKEY_PARAM_PUB_KEY data"); |
| 577 | } |
| 578 | |
| 579 | // Parse the octet string representation into an EC_POINT |
| 580 | auto group = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).( |
| 581 | EC_GROUP_new_by_curve_name_ex(nullptr, nullptr, curve_nidcxx . . $ mlspp/hpke/ECKeyGroup#curve_nid.)); |
| 582 | if (group ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) { |
| 583 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 584 | } |
| 585 | |
| 586 | auto point = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(EC_POINT_new(group.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).())); |
| 587 | if (point ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) { |
| 588 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 589 | } |
| 590 | |
| 591 | if (1 != |
| 592 | EC_POINT_oct2point(group.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), point.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), buf_ptr, len, nullptr)) { |
| 593 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 594 | } |
| 595 | |
| 596 | // Retrieve the affine coordinates of the point |
| 597 | if (1 != EC_POINT_get_affine_coordinates( |
| 598 | group.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), point.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), bn_x.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), bn_y.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), nullptr)) { |
| 599 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 600 | } |
| 601 | #else |
| 602 | // Raw pointers are non-owning |
| 603 | auto* pub = EVP_PKEY_get0_EC_KEY(rpk.pkey.get()); |
| 604 | const auto* point = EC_KEY_get0_public_key(pub); |
| 605 | const auto* group = EC_KEY_get0_group(pub); |
| 606 | |
| 607 | if (1 != EC_POINT_get_affine_coordinates_GFp( |
| 608 | group, point, bn_x.get(), bn_y.get(), nullptr)) { |
| 609 | throw openssl_error(); |
| 610 | } |
| 611 | #endif |
| 612 | const auto x_size = BN_num_bytescxx . . $ `<external>/8357373700258316281/bn.h:188:9`!(bn_x.get()); |
| 613 | auto x = bytescxx . . $ mlspp/bytes_ns/bytes#(x_size); |
| 614 | if (BN_bn2bin(bn_x.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), x.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).()) != x_size) { |
| 615 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 616 | } |
| 617 | |
| 618 | const auto y_size = BN_num_bytescxx . . $ `<external>/8357373700258316281/bn.h:188:9`!(bn_y.get()); |
| 619 | auto y = bytescxx . . $ mlspp/bytes_ns/bytes#(y_size); |
| 620 | if (BN_bn2bin(bn_y.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), y.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).()) != y_size) { |
| 621 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 622 | } |
| 623 | |
| 624 | const auto zeros_needed_x = dh_sizecxx . . $ mlspp/hpke/Group#dh_size. - x.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).(); |
| 625 | const auto zeros_needed_y = dh_sizecxx . . $ mlspp/hpke/Group#dh_size. - y.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).(); |
| 626 | auto leading_zeros_x = bytescxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).(zeros_needed_x, 0); |
| 627 | auto leading_zeros_y = bytescxx . . $ mlspp/bytes_ns/bytes#bytes(913f486ae89fd8a8).(zeros_needed_y, 0); |
| 628 | |
| 629 | return {cxx . . $ std/tuple#tuple(533e79eb6234819d). leading_zeros_x +cxx . . $ mlspp/bytes_ns/bytes#operator+(c5e1b85e16e94921). x, leading_zeros_y +cxx . . $ mlspp/bytes_ns/bytes#operator+(c5e1b85e16e94921). y }; |
| 630 | } |
| 631 | |
| 632 | // EC Key |
| 633 | stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<Groupcxx . . $ mlspp/hpke/Group#::PublicKeycxx . . $ mlspp/hpke/Group#PublicKey#> public_key_from_coordinatescxx . . $ mlspp/hpke/ECKeyGroup#public_key_from_coordinates(5d18259fe6fe47b6).( |
| 634 | const bytescxx . . $ mlspp/bytes_ns/bytes#& x, |
| 635 | const bytescxx . . $ mlspp/bytes_ns/bytes#& y) const override |
| 636 | { |
| 637 | auto bn_x = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).( |
| 638 | BN_bin2bn(x.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), static_cast<int>(x.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).()), nullptr)); |
| 639 | auto bn_y = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).( |
| 640 | BN_bin2bn(y.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), static_cast<int>(y.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).()), nullptr)); |
| 641 | |
| 642 | if (bn_x ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr || bn_y ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) { |
| 643 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Failed to convert bn_x or bn_y"); |
| 644 | } |
| 645 | |
| 646 | #if defined(WITH_OPENSSL3cxx . . $ `<built-in>:494:9`!) |
| 647 | const auto group = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).( |
| 648 | EC_GROUP_new_by_curve_name_ex(nullptr, nullptr, curve_nidcxx . . $ mlspp/hpke/ECKeyGroup#curve_nid.)); |
| 649 | if (group ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) { |
| 650 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Failed to create EC_GROUP"); |
| 651 | } |
| 652 | |
| 653 | // Construct a point with the given coordinates |
| 654 | auto point = make_typed_uniquecxx . . $ mlspp/hpke/make_typed_unique(f611cc58871fbeab).(EC_POINT_new(group.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).())); |
| 655 | if (group ==cxx . . $ std/`operator==`(e445595a98297a59). nullptr) { |
| 656 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Failed to create EC_POINT"); |
| 657 | } |
| 658 | |
| 659 | if (1 != EC_POINT_set_affine_coordinates( |
| 660 | group.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), point.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), bn_x.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), bn_y.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), nullptr)) { |
| 661 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 662 | } |
| 663 | |
| 664 | // Serialize the point |
| 665 | const auto point_size = EC_POINT_point2oct(group.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), |
| 666 | point.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), |
| 667 | POINT_CONVERSION_UNCOMPRESSED, |
| 668 | nullptr, |
| 669 | 0, |
| 670 | nullptr); |
| 671 | if (0 == point_size) { |
| 672 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 673 | } |
| 674 | |
| 675 | auto pub = bytescxx . . $ mlspp/bytes_ns/bytes#(point_size); |
| 676 | if (EC_POINT_point2oct(group.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), |
| 677 | point.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), |
| 678 | POINT_CONVERSION_UNCOMPRESSED, |
| 679 | pub.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(), |
| 680 | point_size, |
| 681 | nullptr) != point_size) { |
| 682 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 683 | } |
| 684 | |
| 685 | // Initialize a public key from the serialized point |
| 686 | auto key = public_evp_keycxx . . $ mlspp/hpke/ECKeyGroup#public_evp_key(f6b3ef0ce83ebc29).(pub); |
| 687 | return stdcxx . . $ std/unique_ptr#unique_ptr(8bcafd3b0b660df8).::make_uniquecxx . . $ std/make_unique(5cd499e8540a10bb).<EVPGroupcxx . . $ mlspp/hpke/EVPGroup#::PublicKeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#>(key.releasecxx . . $ std/unique_ptr#release(aad4c3669c5e940c).()); |
| 688 | #else |
| 689 | auto eckey = new_ec_key(); |
| 690 | if (eckey == nullptr) { |
| 691 | throw std::runtime_error("Failed to create EC_KEY"); |
| 692 | } |
| 693 | |
| 694 | // Group pointer is non-owning |
| 695 | const auto* group = EC_KEY_get0_group(eckey.get()); |
| 696 | auto point = make_typed_unique(EC_POINT_new(group)); |
| 697 | |
| 698 | if (1 != EC_POINT_set_affine_coordinates_GFp( |
| 699 | group, point.get(), bn_x.get(), bn_y.get(), nullptr)) { |
| 700 | throw openssl_error(); |
| 701 | } |
| 702 | |
| 703 | if (1 != EC_KEY_set_public_key(eckey.get(), point.get())) { |
| 704 | throw openssl_error(); |
| 705 | } |
| 706 | |
| 707 | auto pkey = to_pkey(eckey.release()); |
| 708 | return std::make_unique<EVPGroup::PublicKey>(pkey.release()); |
| 709 | #endif |
| 710 | } |
| 711 | |
| 712 | private: |
| 713 | int curve_nidcxx . . $ mlspp/hpke/ECKeyGroup#curve_nid.; |
| 714 | |
| 715 | #if !defined(WITH_OPENSSL3cxx . . $ `<built-in>:494:9`!) |
| 716 | typed_unique_ptr<EC_KEY> new_ec_key() const |
| 717 | { |
| 718 | return make_typed_unique(EC_KEY_new_by_curve_name(curve_nid)); |
| 719 | } |
| 720 | |
| 721 | static typed_unique_ptr<EVP_PKEY> to_pkey(EC_KEY* eckey) |
| 722 | { |
| 723 | auto* pkey = EVP_PKEY_new(); |
| 724 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast) |
| 725 | EVP_PKEY_assign_EC_KEY(pkey, eckey); |
| 726 | return make_typed_unique(pkey); |
| 727 | } |
| 728 | #endif |
| 729 | |
| 730 | static inline int group_to_nidcxx . . $ mlspp/hpke/ECKeyGroup#group_to_nid(ebba8658cbbd2b35).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID# group_id) |
| 731 | { |
| 732 | switch (group_id) { |
| 733 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P256cxx . . $ mlspp/hpke/Group#ID#P256.: |
| 734 | return NID_X9_62_prime256v1cxx . . $ `<external>/18030087176251566933/obj_mac.h:292:9`!; |
| 735 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P384cxx . . $ mlspp/hpke/Group#ID#P384.: |
| 736 | return NID_secp384r1cxx . . $ `<external>/18030087176251566933/obj_mac.h:372:9`!; |
| 737 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P521cxx . . $ mlspp/hpke/Group#ID#P521.: |
| 738 | return NID_secp521r1cxx . . $ `<external>/18030087176251566933/obj_mac.h:376:9`!; |
| 739 | default: |
| 740 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unsupported algorithm"); |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | uint8_tcxx . . $ uint8_t# bitmaskcxx . . $ mlspp/hpke/ECKeyGroup#bitmask(bd807b21b9713a30).() const |
| 745 | { |
| 746 | switch (idcxx . . $ mlspp/hpke/Group#id.) { |
| 747 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P256cxx . . $ mlspp/hpke/Group#ID#P256.: |
| 748 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P384cxx . . $ mlspp/hpke/Group#ID#P384.: |
| 749 | return 0xff; |
| 750 | |
| 751 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P521cxx . . $ mlspp/hpke/Group#ID#P521.: |
| 752 | return 0x01; |
| 753 | |
| 754 | default: |
| 755 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unsupported algorithm"); |
| 756 | } |
| 757 | } |
| 758 | }; |
| 759 | |
| 760 | /// |
| 761 | /// DH over "raw" curves |
| 762 | /// |
| 763 | |
| 764 | struct RawKeyGroupcxx . . $ mlspp/hpke/RawKeyGroup# : public EVPGroupcxx . . $ mlspp/hpke/EVPGroup# |
| 765 | { |
| 766 | RawKeyGroupcxx . . $ mlspp/hpke/RawKeyGroup#RawKeyGroup(3802f28964769a07).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID# group_id, const KDFcxx . . $ mlspp/hpke/KDF#& kdf) |
| 767 | : EVPGroupcxx . . $ mlspp/hpke/EVPGroup#(group_id, kdf) |
| 768 | , evp_typecxx . . $ mlspp/hpke/RawKeyGroup#evp_type.(group_to_evpcxx . . $ mlspp/hpke/RawKeyGroup#group_to_evp(ebba8658cbbd2b35).(group_id)) |
| 769 | { |
| 770 | } |
| 771 | |
| 772 | template<Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID# id> |
| 773 | static const RawKeyGroupcxx . . $ mlspp/hpke/RawKeyGroup# instancecxx . . $ mlspp/hpke/RawKeyGroup#instance.; |
| 774 | |
| 775 | stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<Groupcxx . . $ mlspp/hpke/Group#::PrivateKeycxx . . $ mlspp/hpke/Group#PrivateKey#> derive_key_paircxx . . $ mlspp/hpke/RawKeyGroup#derive_key_pair(7c8b5279bfce12cf).( |
| 776 | const bytescxx . . $ mlspp/bytes_ns/bytes#& suite_id, |
| 777 | const bytescxx . . $ mlspp/bytes_ns/bytes#& ikm) const override |
| 778 | { |
| 779 | static const auto label_dkp_prk = from_asciicxx . . $ mlspp/bytes_ns/from_ascii(fc01d114001b7256).("dkp_prk"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 780 | static const auto label_sk = from_asciicxx . . $ mlspp/bytes_ns/from_ascii(fc01d114001b7256).("sk"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).); |
| 781 | |
| 782 | auto dkp_prk = kdfcxx . . $ mlspp/hpke/Group#kdf..labeled_extractcxx . . $ mlspp/hpke/KDF#labeled_extract(a2160eb270d8417e).(suite_id, {cxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).}, label_dkp_prk, ikm); |
| 783 | auto skm = kdfcxx . . $ mlspp/hpke/Group#kdf..labeled_expandcxx . . $ mlspp/hpke/KDF#labeled_expand(a3b1f2557e911f84).(suite_id, dkp_prk, label_sk, {cxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).}, sk_sizecxx . . $ mlspp/hpke/Group#sk_size.); |
| 784 | return deserialize_privatecxx . . $ mlspp/hpke/RawKeyGroup#deserialize_private(9261b7df97b3e19f).(skm); |
| 785 | } |
| 786 | |
| 787 | bytescxx . . $ mlspp/bytes_ns/bytes# serializecxx . . $ mlspp/hpke/RawKeyGroup#serialize(709e30fcbf6c1f24).(const Groupcxx . . $ mlspp/hpke/Group#::PublicKeycxx . . $ mlspp/hpke/Group#PublicKey#& pk) const override |
| 788 | { |
| 789 | const auto& rpk = dynamic_cast<const PublicKeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#&>(pk); |
| 790 | auto raw = bytescxx . . $ mlspp/bytes_ns/bytes#(pk_sizecxx . . $ mlspp/hpke/Group#pk_size.); |
| 791 | auto* data_ptr = raw.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(); |
| 792 | auto data_len = raw.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).(); |
| 793 | if (1 != EVP_PKEY_get_raw_public_key(rpk.pkeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), data_ptr, &data_len)) { |
| 794 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 795 | } |
| 796 | |
| 797 | return rawcxx . . $ mlspp/bytes_ns/bytes#bytes(628b76f52f2a67f).; |
| 798 | } |
| 799 | |
| 800 | stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<Groupcxx . . $ mlspp/hpke/Group#::PublicKeycxx . . $ mlspp/hpke/Group#PublicKey#> deserializecxx . . $ mlspp/hpke/RawKeyGroup#deserialize(6b5fed5c6c0b0d85).(const bytescxx . . $ mlspp/bytes_ns/bytes#& enc) const override |
| 801 | { |
| 802 | auto* pkey = |
| 803 | EVP_PKEY_new_raw_public_key(evp_typecxx . . $ mlspp/hpke/RawKeyGroup#evp_type., nullptr, enc.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), enc.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).()); |
| 804 | if (pkey == nullptr) { |
| 805 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 806 | } |
| 807 | |
| 808 | return stdcxx . . $ std/unique_ptr#unique_ptr(8bcafd3b0b660df8).::make_uniquecxx . . $ std/make_unique(5cd499e8540a10bb).<EVPGroupcxx . . $ mlspp/hpke/EVPGroup#::PublicKeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#>(pkey); |
| 809 | } |
| 810 | |
| 811 | bytescxx . . $ mlspp/bytes_ns/bytes# serialize_privatecxx . . $ mlspp/hpke/RawKeyGroup#serialize_private(3111aac385788878).(const Groupcxx . . $ mlspp/hpke/Group#::PrivateKeycxx . . $ mlspp/hpke/Group#PrivateKey#& sk) const override |
| 812 | { |
| 813 | const auto& rsk = dynamic_cast<const PrivateKeycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#&>(sk); |
| 814 | auto raw = bytescxx . . $ mlspp/bytes_ns/bytes#(sk_sizecxx . . $ mlspp/hpke/Group#sk_size.); |
| 815 | auto* data_ptr = raw.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(); |
| 816 | auto data_len = raw.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).(); |
| 817 | if (1 != |
| 818 | EVP_PKEY_get_raw_private_key(rsk.pkeycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), data_ptr, &data_len)) { |
| 819 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 820 | } |
| 821 | |
| 822 | return rawcxx . . $ mlspp/bytes_ns/bytes#bytes(628b76f52f2a67f).; |
| 823 | } |
| 824 | |
| 825 | stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<Groupcxx . . $ mlspp/hpke/Group#::PrivateKeycxx . . $ mlspp/hpke/Group#PrivateKey#> deserialize_privatecxx . . $ mlspp/hpke/RawKeyGroup#deserialize_private(9261b7df97b3e19f).( |
| 826 | const bytescxx . . $ mlspp/bytes_ns/bytes#& skm) const override |
| 827 | { |
| 828 | auto* pkey = |
| 829 | EVP_PKEY_new_raw_private_key(evp_typecxx . . $ mlspp/hpke/RawKeyGroup#evp_type., nullptr, skm.datacxx . . $ mlspp/bytes_ns/bytes#data(7eb257f306f79f97).(), skm.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).()); |
| 830 | if (pkey == nullptr) { |
| 831 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 832 | } |
| 833 | |
| 834 | return stdcxx . . $ std/unique_ptr#unique_ptr(8bcafd3b0b660df8).::make_uniquecxx . . $ std/make_unique(5cd499e8540a10bb).<EVPGroupcxx . . $ mlspp/hpke/EVPGroup#::PrivateKeycxx . . $ mlspp/hpke/EVPGroup#PrivateKey#>(pkey); |
| 835 | } |
| 836 | |
| 837 | // Raw Key |
| 838 | stdcxx . . $ std/::tuplecxx . . $ std/tuple#<bytescxx . . $ mlspp/bytes_ns/bytes#, bytescxx . . $ mlspp/bytes_ns/bytes#> coordinatescxx . . $ mlspp/hpke/RawKeyGroup#coordinates(4ed3c5dd9bcc18fa).( |
| 839 | const Groupcxx . . $ mlspp/hpke/Group#::PublicKeycxx . . $ mlspp/hpke/Group#PublicKey#& pk) const override |
| 840 | { |
| 841 | const auto& rpk = dynamic_cast<const PublicKeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#&>(pk); |
| 842 | auto raw = bytescxx . . $ mlspp/bytes_ns/bytes#(pk_sizecxx . . $ mlspp/hpke/Group#pk_size.); |
| 843 | auto* data_ptr = raw.datacxx . . $ mlspp/bytes_ns/bytes#data(37d8967d10cb1e7b).(); |
| 844 | auto data_len = raw.sizecxx . . $ mlspp/bytes_ns/bytes#size(fe4d2d262d4164a8).(); |
| 845 | |
| 846 | if (1 != EVP_PKEY_get_raw_public_key(rpk.pkeycxx . . $ mlspp/hpke/EVPGroup#PublicKey#pkey..getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).(), data_ptr, &data_len)) { |
| 847 | throw openssl_errorcxx . . $ mlspp/hpke/openssl_error(d6ab8afd00dfc651).(); |
| 848 | } |
| 849 | |
| 850 | return {cxx . . $ std/tuple#tuple(68f3cec925df479f). raw, {cxx . . $ mlspp/bytes_ns/bytes#bytes(ced63f7c635d850d).} }; |
| 851 | } |
| 852 | |
| 853 | // Raw Key |
| 854 | stdcxx . . $ std/::unique_ptrcxx . . $ std/unique_ptr#<Groupcxx . . $ mlspp/hpke/Group#::PublicKeycxx . . $ mlspp/hpke/Group#PublicKey#> public_key_from_coordinatescxx . . $ mlspp/hpke/RawKeyGroup#public_key_from_coordinates(5d18259fe6fe47b6).( |
| 855 | const bytescxx . . $ mlspp/bytes_ns/bytes#& x, |
| 856 | const bytescxx . . $ mlspp/bytes_ns/bytes#& /* y */) const override |
| 857 | { |
| 858 | return deserializecxx . . $ mlspp/hpke/RawKeyGroup#deserialize(6b5fed5c6c0b0d85).(x); |
| 859 | } |
| 860 | |
| 861 | private: |
| 862 | const int evp_typecxx . . $ mlspp/hpke/RawKeyGroup#evp_type.; |
| 863 | |
| 864 | static inline int group_to_evpcxx . . $ mlspp/hpke/RawKeyGroup#group_to_evp(ebba8658cbbd2b35).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID# group_id) |
| 865 | { |
| 866 | switch (group_id) { |
| 867 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::X25519cxx . . $ mlspp/hpke/Group#ID#X25519.: |
| 868 | return EVP_PKEY_X25519cxx . . $ `<external>/2497497914425334111/evp.h:82:9`!; |
| 869 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::X448cxx . . $ mlspp/hpke/Group#ID#X448.: |
| 870 | return EVP_PKEY_X448cxx . . $ `<external>/2497497914425334111/evp.h:84:9`!; |
| 871 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::Ed25519cxx . . $ mlspp/hpke/Group#ID#Ed25519.: |
| 872 | return EVP_PKEY_ED25519cxx . . $ `<external>/2497497914425334111/evp.h:83:9`!; |
| 873 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::Ed448cxx . . $ mlspp/hpke/Group#ID#Ed448.: |
| 874 | return EVP_PKEY_ED448cxx . . $ `<external>/2497497914425334111/evp.h:85:9`!; |
| 875 | default: |
| 876 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unsupported algorithm"); |
| 877 | } |
| 878 | } |
| 879 | }; |
| 880 | |
| 881 | /// |
| 882 | /// General DH group |
| 883 | /// |
| 884 | |
| 885 | template<> |
| 886 | const Groupcxx . . $ mlspp/hpke/Group#& |
| 887 | Groupcxx . . $ mlspp/hpke/Group#::getcxx . . $ mlspp/hpke/Group#get(f05f80c0d9382943).<Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P256cxx . . $ mlspp/hpke/Group#ID#P256.>() |
| 888 | { |
| 889 | static const ECKeyGroupcxx . . $ mlspp/hpke/ECKeyGroup# instancecxx . . $ mlspp/hpke/ECKeyGroup#ECKeyGroup(3802f28964769a07).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P256cxx . . $ mlspp/hpke/Group#ID#P256., |
| 890 | 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.>()); |
| 891 | |
| 892 | return instance; |
| 893 | } |
| 894 | |
| 895 | template<> |
| 896 | const Groupcxx . . $ mlspp/hpke/Group#& |
| 897 | Groupcxx . . $ mlspp/hpke/Group#::getcxx . . $ mlspp/hpke/Group#get(f05f80c0d9382943).<Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P384cxx . . $ mlspp/hpke/Group#ID#P384.>() |
| 898 | { |
| 899 | static const ECKeyGroupcxx . . $ mlspp/hpke/ECKeyGroup# instancecxx . . $ mlspp/hpke/ECKeyGroup#ECKeyGroup(3802f28964769a07).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P384cxx . . $ mlspp/hpke/Group#ID#P384., |
| 900 | 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.>()); |
| 901 | |
| 902 | return instance; |
| 903 | } |
| 904 | |
| 905 | template<> |
| 906 | const Groupcxx . . $ mlspp/hpke/Group#& |
| 907 | Groupcxx . . $ mlspp/hpke/Group#::getcxx . . $ mlspp/hpke/Group#get(f05f80c0d9382943).<Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P521cxx . . $ mlspp/hpke/Group#ID#P521.>() |
| 908 | { |
| 909 | static const ECKeyGroupcxx . . $ mlspp/hpke/ECKeyGroup# instancecxx . . $ mlspp/hpke/ECKeyGroup#ECKeyGroup(3802f28964769a07).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P521cxx . . $ mlspp/hpke/Group#ID#P521., |
| 910 | 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.>()); |
| 911 | |
| 912 | return instance; |
| 913 | } |
| 914 | |
| 915 | template<> |
| 916 | const Groupcxx . . $ mlspp/hpke/Group#& |
| 917 | Groupcxx . . $ mlspp/hpke/Group#::getcxx . . $ mlspp/hpke/Group#get(f05f80c0d9382943).<Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::X25519cxx . . $ mlspp/hpke/Group#ID#X25519.>() |
| 918 | { |
| 919 | static const RawKeyGroupcxx . . $ mlspp/hpke/RawKeyGroup# instancecxx . . $ mlspp/hpke/RawKeyGroup#RawKeyGroup(3802f28964769a07).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::X25519cxx . . $ mlspp/hpke/Group#ID#X25519., |
| 920 | 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.>()); |
| 921 | return instance; |
| 922 | } |
| 923 | |
| 924 | template<> |
| 925 | const Groupcxx . . $ mlspp/hpke/Group#& |
| 926 | Groupcxx . . $ mlspp/hpke/Group#::getcxx . . $ mlspp/hpke/Group#get(f05f80c0d9382943).<Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::Ed25519cxx . . $ mlspp/hpke/Group#ID#Ed25519.>() |
| 927 | { |
| 928 | static const RawKeyGroupcxx . . $ mlspp/hpke/RawKeyGroup# instancecxx . . $ mlspp/hpke/RawKeyGroup#RawKeyGroup(3802f28964769a07).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::Ed25519cxx . . $ mlspp/hpke/Group#ID#Ed25519., |
| 929 | 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.>()); |
| 930 | return instance; |
| 931 | } |
| 932 | |
| 933 | // BoringSSL doesn't support X448 / Ed448 |
| 934 | #if !defined(WITH_BORINGSSL) |
| 935 | template<> |
| 936 | const Groupcxx . . $ mlspp/hpke/Group#& |
| 937 | Groupcxx . . $ mlspp/hpke/Group#::getcxx . . $ mlspp/hpke/Group#get(f05f80c0d9382943).<Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::X448cxx . . $ mlspp/hpke/Group#ID#X448.>() |
| 938 | { |
| 939 | static const RawKeyGroupcxx . . $ mlspp/hpke/RawKeyGroup# instancecxx . . $ mlspp/hpke/RawKeyGroup#RawKeyGroup(3802f28964769a07).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::X448cxx . . $ mlspp/hpke/Group#ID#X448., |
| 940 | 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.>()); |
| 941 | return instance; |
| 942 | } |
| 943 | #endif |
| 944 | |
| 945 | template<> |
| 946 | const Groupcxx . . $ mlspp/hpke/Group#& |
| 947 | Groupcxx . . $ mlspp/hpke/Group#::getcxx . . $ mlspp/hpke/Group#get(f05f80c0d9382943).<Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::Ed448cxx . . $ mlspp/hpke/Group#ID#Ed448.>() |
| 948 | { |
| 949 | static const RawKeyGroupcxx . . $ mlspp/hpke/RawKeyGroup# instancecxx . . $ mlspp/hpke/RawKeyGroup#RawKeyGroup(3802f28964769a07).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::Ed448cxx . . $ mlspp/hpke/Group#ID#Ed448., |
| 950 | 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.>()); |
| 951 | return instance; |
| 952 | } |
| 953 | |
| 954 | static inline size_t |
| 955 | group_dh_sizecxx . . $ mlspp/hpke/group_dh_size(e1c07ae153638830).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID# group_id) |
| 956 | { |
| 957 | switch (group_id) { |
| 958 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P256cxx . . $ mlspp/hpke/Group#ID#P256.: |
| 959 | return 32; |
| 960 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P384cxx . . $ mlspp/hpke/Group#ID#P384.: |
| 961 | return 48; |
| 962 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P521cxx . . $ mlspp/hpke/Group#ID#P521.: |
| 963 | return 66; |
| 964 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::X25519cxx . . $ mlspp/hpke/Group#ID#X25519.: |
| 965 | return 32; |
| 966 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::X448cxx . . $ mlspp/hpke/Group#ID#X448.: |
| 967 | return 56; |
| 968 | |
| 969 | // Non-DH groups |
| 970 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::Ed25519cxx . . $ mlspp/hpke/Group#ID#Ed25519.: |
| 971 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::Ed448cxx . . $ mlspp/hpke/Group#ID#Ed448.: |
| 972 | return 0; |
| 973 | |
| 974 | default: |
| 975 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unknown group"); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | static inline size_t |
| 980 | group_pk_sizecxx . . $ mlspp/hpke/group_pk_size(e1c07ae153638830).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID# group_id) |
| 981 | { |
| 982 | switch (group_id) { |
| 983 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P256cxx . . $ mlspp/hpke/Group#ID#P256.: |
| 984 | return 65; |
| 985 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P384cxx . . $ mlspp/hpke/Group#ID#P384.: |
| 986 | return 97; |
| 987 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P521cxx . . $ mlspp/hpke/Group#ID#P521.: |
| 988 | return 133; |
| 989 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::X25519cxx . . $ mlspp/hpke/Group#ID#X25519.: |
| 990 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::Ed25519cxx . . $ mlspp/hpke/Group#ID#Ed25519.: |
| 991 | return 32; |
| 992 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::X448cxx . . $ mlspp/hpke/Group#ID#X448.: |
| 993 | return 56; |
| 994 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::Ed448cxx . . $ mlspp/hpke/Group#ID#Ed448.: |
| 995 | return 57; |
| 996 | |
| 997 | default: |
| 998 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unknown group"); |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | static inline size_t |
| 1003 | group_sk_sizecxx . . $ mlspp/hpke/group_sk_size(e1c07ae153638830).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID# group_id) |
| 1004 | { |
| 1005 | switch (group_id) { |
| 1006 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P256cxx . . $ mlspp/hpke/Group#ID#P256.: |
| 1007 | return 32; |
| 1008 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P384cxx . . $ mlspp/hpke/Group#ID#P384.: |
| 1009 | return 48; |
| 1010 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P521cxx . . $ mlspp/hpke/Group#ID#P521.: |
| 1011 | return 66; |
| 1012 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::X25519cxx . . $ mlspp/hpke/Group#ID#X25519.: |
| 1013 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::Ed25519cxx . . $ mlspp/hpke/Group#ID#Ed25519.: |
| 1014 | return 32; |
| 1015 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::X448cxx . . $ mlspp/hpke/Group#ID#X448.: |
| 1016 | return 56; |
| 1017 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::Ed448cxx . . $ mlspp/hpke/Group#ID#Ed448.: |
| 1018 | return 57; |
| 1019 | |
| 1020 | default: |
| 1021 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unknown group"); |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | static inline stdcxx . . $ std/::stringcxx . . $ std/string# |
| 1026 | group_jwk_curve_namecxx . . $ mlspp/hpke/group_jwk_curve_name(519e1df709152d7d).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID# group_id) |
| 1027 | { |
| 1028 | switch (group_id) { |
| 1029 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P256cxx . . $ mlspp/hpke/Group#ID#P256.: |
| 1030 | return "P-256"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).; |
| 1031 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P384cxx . . $ mlspp/hpke/Group#ID#P384.: |
| 1032 | return "P-384"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).; |
| 1033 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P521cxx . . $ mlspp/hpke/Group#ID#P521.: |
| 1034 | return "P-521"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).; |
| 1035 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::Ed25519cxx . . $ mlspp/hpke/Group#ID#Ed25519.: |
| 1036 | return "Ed25519"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).; |
| 1037 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::Ed448cxx . . $ mlspp/hpke/Group#ID#Ed448.: |
| 1038 | return "Ed448"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).; |
| 1039 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::X25519cxx . . $ mlspp/hpke/Group#ID#X25519.: |
| 1040 | return "X25519"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).; |
| 1041 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::X448cxx . . $ mlspp/hpke/Group#ID#X448.: |
| 1042 | return "X448"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).; |
| 1043 | default: |
| 1044 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unknown group"); |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | static inline stdcxx . . $ std/::stringcxx . . $ std/string# |
| 1049 | group_jwk_key_typecxx . . $ mlspp/hpke/group_jwk_key_type(519e1df709152d7d).(Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID# group_id) |
| 1050 | { |
| 1051 | switch (group_id) { |
| 1052 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P256cxx . . $ mlspp/hpke/Group#ID#P256.: |
| 1053 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P384cxx . . $ mlspp/hpke/Group#ID#P384.: |
| 1054 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::P521cxx . . $ mlspp/hpke/Group#ID#P521.: |
| 1055 | return "EC"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).; |
| 1056 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::Ed25519cxx . . $ mlspp/hpke/Group#ID#Ed25519.: |
| 1057 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::Ed448cxx . . $ mlspp/hpke/Group#ID#Ed448.: |
| 1058 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::X25519cxx . . $ mlspp/hpke/Group#ID#X25519.: |
| 1059 | case Groupcxx . . $ mlspp/hpke/Group#::IDcxx . . $ mlspp/hpke/Group#ID#::X448cxx . . $ mlspp/hpke/Group#ID#X448.: |
| 1060 | return "OKP"cxx . . $ std/__cxx11/basic_string#basic_string(e36662616dba894d).; |
| 1061 | default: |
| 1062 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#("Unknown group"); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | Groupcxx . . $ mlspp/hpke/Group#::Groupcxx . . $ mlspp/hpke/Group#Group(3802f28964769a07).(IDcxx . . $ mlspp/hpke/Group#ID# group_id_in, const KDFcxx . . $ mlspp/hpke/KDF#& kdf_in) |
| 1067 | : idcxx . . $ mlspp/hpke/Group#id.(group_id_in) |
| 1068 | , dh_sizecxx . . $ mlspp/hpke/Group#dh_size.(group_dh_sizecxx . . $ mlspp/hpke/group_dh_size(e1c07ae153638830).(group_id_in)) |
| 1069 | , pk_sizecxx . . $ mlspp/hpke/Group#pk_size.(group_pk_sizecxx . . $ mlspp/hpke/group_pk_size(e1c07ae153638830).(group_id_in)) |
| 1070 | , sk_sizecxx . . $ mlspp/hpke/Group#sk_size.(group_sk_sizecxx . . $ mlspp/hpke/group_sk_size(e1c07ae153638830).(group_id_in)) |
| 1071 | , jwk_key_typecxx . . $ mlspp/hpke/Group#jwk_key_type.(group_jwk_key_typecxx . . $ mlspp/hpke/group_jwk_key_type(519e1df709152d7d).(group_id_in)) |
| 1072 | , jwk_curve_namecxx . . $ mlspp/hpke/Group#jwk_curve_name.(group_jwk_curve_namecxx . . $ mlspp/hpke/group_jwk_curve_name(519e1df709152d7d).(group_id_in)) |
| 1073 | , kdfcxx . . $ mlspp/hpke/Group#kdf.(kdf_in) |
| 1074 | { |
| 1075 | } |
| 1076 | |
| 1077 | } // namespace mlspp::hpke |