| 1 | #include "group.h" |
| 2 | |
| 3 | #include <hpke/random.h> |
| 4 | |
| 5 | #include "common.h" |
| 6 | #include "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_OPENSSL3) |
| 13 | #include "openssl/core_names.h" |
| 14 | #include "openssl/param_build.h" |
| 15 | #endif |
| 16 | |
| 17 | namespace mlspp::hpke { |
| 18 | |
| 19 | static inline size_t |
| 20 | group_dh_size(Group::ID group_id); |
| 21 | |
| 22 | static inline const EVP_MD* |
| 23 | group_sig_digest(Group::ID group_id) |
| 24 | { |
| 25 | switch (group_id) { |
| 26 | case Group::ID::P256: |
| 27 | return EVP_sha256(); |
| 28 | case Group::ID::P384: |
| 29 | return EVP_sha384(); |
| 30 | case Group::ID::P521: |
| 31 | return EVP_sha512(); |
| 32 | |
| 33 | // EdDSA does its own hashing internally |
| 34 | case Group::ID::Ed25519: |
| 35 | case Group::ID::Ed448: |
| 36 | return nullptr; |
| 37 | |
| 38 | // Groups not used for signature |
| 39 | case Group::ID::X25519: |
| 40 | case Group::ID::X448: |
| 41 | throw std::runtime_error("Signature not supported for group"); |
| 42 | |
| 43 | default: |
| 44 | throw std::runtime_error("Unknown group"); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /// |
| 49 | /// General implementation with OpenSSL EVP_PKEY |
| 50 | /// |
| 51 | |
| 52 | EVPGroup::EVPGroup(Group::ID group_id, const KDF& kdf) |
| 53 | : Group(group_id, kdf) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | EVPGroup::PublicKey::PublicKey(EVP_PKEY* pkey_in) |
| 58 | : pkey(pkey_in, typed_delete<EVP_PKEY>) |
| 59 | { |
| 60 | } |
| 61 | |
| 62 | EVPGroup::PrivateKey::PrivateKey(EVP_PKEY* pkey_in) |
| 63 | : pkey(pkey_in, typed_delete<EVP_PKEY>) |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | std::unique_ptr<Group::PublicKey> |
| 68 | EVPGroup::PrivateKey::public_key() const |
| 69 | { |
| 70 | if (1 != EVP_PKEY_up_ref(pkey.get())) { |
| 71 | throw openssl_error(); |
| 72 | } |
| 73 | return std::make_unique<PublicKey>(pkey.get()); |
| 74 | } |
| 75 | |
| 76 | std::unique_ptr<Group::PrivateKey> |
| 77 | EVPGroup::generate_key_pair() const |
| 78 | { |
| 79 | return derive_key_pair({}, random_bytes(sk_size)); |
| 80 | } |
| 81 | |
| 82 | bytes |
| 83 | EVPGroup::dh(const Group::PrivateKey& sk, const Group::PublicKey& pk) const |
| 84 | { |
| 85 | const auto& rsk = dynamic_cast<const PrivateKey&>(sk); |
| 86 | const auto& rpk = dynamic_cast<const 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.pkey.get()); |
| 93 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
| 94 | auto* pub_pkey = const_cast<EVP_PKEY*>(rpk.pkey.get()); |
| 95 | |
| 96 | auto ctx = make_typed_unique(EVP_PKEY_CTX_new(priv_pkey, nullptr)); |
| 97 | if (ctx == nullptr) { |
| 98 | throw openssl_error(); |
| 99 | } |
| 100 | |
| 101 | if (1 != EVP_PKEY_derive_init(ctx.get())) { |
| 102 | throw openssl_error(); |
| 103 | } |
| 104 | |
| 105 | if (1 != EVP_PKEY_derive_set_peer(ctx.get(), pub_pkey)) { |
| 106 | throw openssl_error(); |
| 107 | } |
| 108 | |
| 109 | size_t out_len = 0; |
| 110 | if (1 != EVP_PKEY_derive(ctx.get(), nullptr, &out_len)) { |
| 111 | throw openssl_error(); |
| 112 | } |
| 113 | |
| 114 | bytes out(out_len); |
| 115 | uint8_t* ptr = out.data(); |
| 116 | if (1 != (EVP_PKEY_derive(ctx.get(), ptr, &out_len))) { |
| 117 | throw openssl_error(); |
| 118 | } |
| 119 | |
| 120 | return out; |
| 121 | } |
| 122 | |
| 123 | bytes |
| 124 | EVPGroup::sign(const bytes& data, const Group::PrivateKey& sk) const |
| 125 | { |
| 126 | const auto& rsk = dynamic_cast<const PrivateKey&>(sk); |
| 127 | |
| 128 | auto ctx = make_typed_unique(EVP_MD_CTX_create()); |
| 129 | if (ctx == nullptr) { |
| 130 | throw openssl_error(); |
| 131 | } |
| 132 | |
| 133 | const auto* digest = group_sig_digest(id); |
| 134 | if (1 != |
| 135 | EVP_DigestSignInit(ctx.get(), nullptr, digest, nullptr, rsk.pkey.get())) { |
| 136 | throw openssl_error(); |
| 137 | } |
| 138 | |
| 139 | size_t siglen = EVP_PKEY_size(rsk.pkey.get()); |
| 140 | bytes sig(siglen); |
| 141 | if (1 != EVP_DigestSign( |
| 142 | ctx.get(), sig.data(), &siglen, data.data(), data.size())) { |
| 143 | throw openssl_error(); |
| 144 | } |
| 145 | |
| 146 | sig.resize(siglen); |
| 147 | return sig; |
| 148 | } |
| 149 | |
| 150 | bool |
| 151 | EVPGroup::verify(const bytes& data, |
| 152 | const bytes& sig, |
| 153 | const Group::PublicKey& pk) const |
| 154 | { |
| 155 | const auto& rpk = dynamic_cast<const PublicKey&>(pk); |
| 156 | |
| 157 | auto ctx = make_typed_unique(EVP_MD_CTX_create()); |
| 158 | if (ctx == nullptr) { |
| 159 | throw openssl_error(); |
| 160 | } |
| 161 | |
| 162 | const auto* digest = group_sig_digest(id); |
| 163 | if (1 != EVP_DigestVerifyInit( |
| 164 | ctx.get(), nullptr, digest, nullptr, rpk.pkey.get())) { |
| 165 | throw openssl_error(); |
| 166 | } |
| 167 | |
| 168 | auto rv = EVP_DigestVerify( |
| 169 | ctx.get(), sig.data(), sig.size(), data.data(), data.size()); |
| 170 | |
| 171 | return rv == 1; |
| 172 | } |
| 173 | |
| 174 | /// |
| 175 | /// DH over "normal" curves |
| 176 | /// |
| 177 | |
| 178 | struct ECKeyGroup : public EVPGroup |
| 179 | { |
| 180 | ECKeyGroup(Group::ID group_id, const KDF& kdf) |
| 181 | : EVPGroup(group_id, kdf) |
| 182 | , curve_nid(group_to_nid(group_id)) |
| 183 | { |
| 184 | } |
| 185 | |
| 186 | #if defined(WITH_OPENSSL3) |
| 187 | typed_unique_ptr<EVP_PKEY> keypair_evp_key( |
| 188 | const typed_unique_ptr<BIGNUM>& priv) const |
| 189 | { |
| 190 | const auto* name = OBJ_nid2sn(curve_nid); |
| 191 | if (name == nullptr) { |
| 192 | throw std::runtime_error("Unsupported algorithm"); |
| 193 | } |
| 194 | |
| 195 | auto group = make_typed_unique( |
| 196 | EC_GROUP_new_by_curve_name_ex(nullptr, nullptr, curve_nid)); |
| 197 | if (group == nullptr) { |
| 198 | throw openssl_error(); |
| 199 | } |
| 200 | |
| 201 | auto pt = make_typed_unique(EC_POINT_new(group.get())); |
| 202 | if (pt == nullptr) { |
| 203 | throw openssl_error(); |
| 204 | } |
| 205 | |
| 206 | if (1 != EC_POINT_mul( |
| 207 | group.get(), pt.get(), priv.get(), nullptr, nullptr, nullptr)) { |
| 208 | throw openssl_error(); |
| 209 | } |
| 210 | |
| 211 | const auto pt_size = EC_POINT_point2oct(group.get(), |
| 212 | pt.get(), |
| 213 | POINT_CONVERSION_UNCOMPRESSED, |
| 214 | nullptr, |
| 215 | 0, |
| 216 | nullptr); |
| 217 | if (0 == pt_size) { |
| 218 | throw openssl_error(); |
| 219 | } |
| 220 | |
| 221 | bytes pub(pt_size); |
| 222 | if (EC_POINT_point2oct(group.get(), |
| 223 | pt.get(), |
| 224 | POINT_CONVERSION_UNCOMPRESSED, |
| 225 | pub.data(), |
| 226 | pt_size, |
| 227 | nullptr) != pt_size) { |
| 228 | throw openssl_error(); |
| 229 | } |
| 230 | |
| 231 | auto builder = make_typed_unique(OSSL_PARAM_BLD_new()); |
| 232 | if (builder == nullptr || |
| 233 | 1 != OSSL_PARAM_BLD_push_utf8_string( |
| 234 | builder.get(), OSSL_PKEY_PARAM_GROUP_NAME, name, 0) || |
| 235 | 1 != OSSL_PARAM_BLD_push_BN( |
| 236 | builder.get(), OSSL_PKEY_PARAM_PRIV_KEY, priv.get()) || |
| 237 | 1 != |
| 238 | OSSL_PARAM_BLD_push_octet_string( |
| 239 | builder.get(), OSSL_PKEY_PARAM_PUB_KEY, pub.data(), pub.size())) { |
| 240 | throw openssl_error(); |
| 241 | } |
| 242 | |
| 243 | auto params = make_typed_unique(OSSL_PARAM_BLD_to_param(builder.get())); |
| 244 | auto ctx = |
| 245 | make_typed_unique(EVP_PKEY_CTX_new_from_name(nullptr, "EC", nullptr)); |
| 246 | auto key = make_typed_unique(EVP_PKEY_new()); |
| 247 | auto* key_ptr = key.get(); |
| 248 | if (params == nullptr || ctx == nullptr || key == nullptr || |
| 249 | EVP_PKEY_fromdata_init(ctx.get()) <= 0 || |
| 250 | EVP_PKEY_fromdata( |
| 251 | ctx.get(), &key_ptr, EVP_PKEY_KEYPAIR, params.get()) <= 0) { |
| 252 | throw openssl_error(); |
| 253 | } |
| 254 | ctx.reset(); |
| 255 | |
| 256 | ctx = make_typed_unique( |
| 257 | EVP_PKEY_CTX_new_from_pkey(nullptr, key.get(), nullptr)); |
| 258 | if (EVP_PKEY_check(ctx.get()) <= 0) { |
| 259 | throw openssl_error(); |
| 260 | } |
| 261 | |
| 262 | return key; |
| 263 | } |
| 264 | |
| 265 | typed_unique_ptr<EVP_PKEY> public_evp_key(const bytes& pub) const |
| 266 | { |
| 267 | const auto* name = OBJ_nid2sn(curve_nid); |
| 268 | if (name == nullptr) { |
| 269 | throw std::runtime_error("Unsupported algorithm"); |
| 270 | } |
| 271 | |
| 272 | auto group = make_typed_unique( |
| 273 | EC_GROUP_new_by_curve_name_ex(nullptr, nullptr, curve_nid)); |
| 274 | if (group == nullptr) { |
| 275 | throw openssl_error(); |
| 276 | } |
| 277 | |
| 278 | auto builder = make_typed_unique(OSSL_PARAM_BLD_new()); |
| 279 | if (builder == nullptr || |
| 280 | 1 != OSSL_PARAM_BLD_push_utf8_string( |
| 281 | builder.get(), OSSL_PKEY_PARAM_GROUP_NAME, name, 0) || |
| 282 | 1 != |
| 283 | OSSL_PARAM_BLD_push_octet_string( |
| 284 | builder.get(), OSSL_PKEY_PARAM_PUB_KEY, pub.data(), pub.size())) { |
| 285 | throw openssl_error(); |
| 286 | } |
| 287 | |
| 288 | auto params = make_typed_unique(OSSL_PARAM_BLD_to_param(builder.get())); |
| 289 | auto ctx = |
| 290 | make_typed_unique(EVP_PKEY_CTX_new_from_name(nullptr, "EC", nullptr)); |
| 291 | auto key = make_typed_unique(EVP_PKEY_new()); |
| 292 | auto* key_ptr = key.get(); |
| 293 | if (params == nullptr || ctx == nullptr || key == nullptr || |
| 294 | EVP_PKEY_fromdata_init(ctx.get()) <= 0 || |
| 295 | EVP_PKEY_fromdata( |
| 296 | ctx.get(), &key_ptr, EVP_PKEY_KEYPAIR, params.get()) <= 0) { |
| 297 | throw openssl_error(); |
| 298 | } |
| 299 | ctx.reset(); |
| 300 | |
| 301 | ctx = make_typed_unique( |
| 302 | EVP_PKEY_CTX_new_from_pkey(nullptr, key.get(), nullptr)); |
| 303 | if (EVP_PKEY_public_check(ctx.get()) <= 0) { |
| 304 | throw openssl_error(); |
| 305 | } |
| 306 | |
| 307 | return key; |
| 308 | } |
| 309 | #endif |
| 310 | |
| 311 | std::unique_ptr<Group::PrivateKey> derive_key_pair( |
| 312 | const bytes& suite_id, |
| 313 | const bytes& ikm) const override |
| 314 | { |
| 315 | static const int retry_limit = 255; |
| 316 | static const auto label_dkp_prk = from_ascii("dkp_prk"); |
| 317 | static const auto label_candidate = from_ascii("candidate"); |
| 318 | |
| 319 | auto dkp_prk = kdf.labeled_extract(suite_id, {}, label_dkp_prk, ikm); |
| 320 | |
| 321 | #if defined(WITH_OPENSSL3) |
| 322 | auto* group = EC_GROUP_new_by_curve_name_ex(nullptr, nullptr, curve_nid); |
| 323 | auto group_ptr = make_typed_unique(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_unique(BN_new()); |
| 330 | if (1 != EC_GROUP_get_order(group, order.get(), nullptr)) { |
| 331 | throw openssl_error(); |
| 332 | } |
| 333 | |
| 334 | auto sk = make_typed_unique(BN_new()); |
| 335 | BN_zero(sk.get()); |
| 336 | |
| 337 | auto counter = int(0); |
| 338 | while (BN_is_zero(sk.get()) != 0 || BN_cmp(sk.get(), order.get()) != -1) { |
| 339 | auto ctr = i2osp(counter, 1); |
| 340 | auto candidate = |
| 341 | kdf.labeled_expand(suite_id, dkp_prk, label_candidate, ctr, sk_size); |
| 342 | candidate.at(0) &= bitmask(); |
| 343 | sk.reset(BN_bin2bn( |
| 344 | candidate.data(), static_cast<int>(candidate.size()), nullptr)); |
| 345 | |
| 346 | counter += 1; |
| 347 | if (counter > retry_limit) { |
| 348 | throw std::runtime_error("DeriveKeyPair iteration limit exceeded"); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | #if defined(WITH_OPENSSL3) |
| 353 | auto key = keypair_evp_key(sk); |
| 354 | return std::make_unique<EVPGroup::PrivateKey>(key.release()); |
| 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 | bytes serialize(const Group::PublicKey& pk) const override |
| 368 | { |
| 369 | const auto& rpk = dynamic_cast<const PublicKey&>(pk); |
| 370 | #if defined(WITH_OPENSSL3) |
| 371 | OSSL_PARAM* param = nullptr; |
| 372 | if (1 != EVP_PKEY_todata(rpk.pkey.get(), EVP_PKEY_PUBLIC_KEY, ¶m)) { |
| 373 | throw openssl_error(); |
| 374 | } |
| 375 | auto param_ptr = make_typed_unique(param); |
| 376 | |
| 377 | const OSSL_PARAM* pk_param = |
| 378 | OSSL_PARAM_locate_const(param_ptr.get(), OSSL_PKEY_PARAM_PUB_KEY); |
| 379 | if (pk_param == nullptr) { |
| 380 | return bytes({}, 0); |
| 381 | } |
| 382 | |
| 383 | size_t len = 0; |
| 384 | if (1 != OSSL_PARAM_get_octet_string(pk_param, nullptr, 0, &len)) { |
| 385 | return bytes({}, 0); |
| 386 | } |
| 387 | |
| 388 | bytes buf(len); |
| 389 | void* data_ptr = buf.data(); |
| 390 | if (1 != OSSL_PARAM_get_octet_string(pk_param, &data_ptr, len, nullptr)) { |
| 391 | return bytes({}, 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_unique( |
| 399 | EC_GROUP_new_by_curve_name_ex(nullptr, nullptr, curve_nid)); |
| 400 | if (group == nullptr) { |
| 401 | return bytes({}, 0); |
| 402 | } |
| 403 | auto point = make_typed_unique(EC_POINT_new(group.get())); |
| 404 | const auto* oct_ptr = static_cast<const unsigned char*>(data_ptr); |
| 405 | if (1 != |
| 406 | EC_POINT_oct2point(group.get(), point.get(), oct_ptr, len, nullptr)) { |
| 407 | return bytes({}, 0); |
| 408 | } |
| 409 | len = EC_POINT_point2oct(group.get(), |
| 410 | point.get(), |
| 411 | POINT_CONVERSION_UNCOMPRESSED, |
| 412 | nullptr, |
| 413 | 0, |
| 414 | nullptr); |
| 415 | if (0 == len) { |
| 416 | return bytes({}, 0); |
| 417 | } |
| 418 | bytes out(len); |
| 419 | auto* data = out.data(); |
| 420 | if (EC_POINT_point2oct(group.get(), |
| 421 | point.get(), |
| 422 | POINT_CONVERSION_UNCOMPRESSED, |
| 423 | data, |
| 424 | len, |
| 425 | nullptr) != len) { |
| 426 | return bytes({}, 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 out; |
| 443 | } |
| 444 | |
| 445 | std::unique_ptr<Group::PublicKey> deserialize(const bytes& enc) const override |
| 446 | { |
| 447 | #if defined(WITH_OPENSSL3) |
| 448 | auto key = public_evp_key(enc); |
| 449 | if (key == nullptr) { |
| 450 | throw std::runtime_error("Unable to deserialize the public key"); |
| 451 | } |
| 452 | return std::make_unique<EVPGroup::PublicKey>(key.release()); |
| 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 | bytes serialize_private(const Group::PrivateKey& sk) const override |
| 471 | { |
| 472 | const auto& rsk = dynamic_cast<const PrivateKey&>(sk); |
| 473 | #if defined(WITH_OPENSSL3) |
| 474 | OSSL_PARAM* param = nullptr; |
| 475 | if (1 != EVP_PKEY_todata(rsk.pkey.get(), EVP_PKEY_KEYPAIR, ¶m)) { |
| 476 | throw openssl_error(); |
| 477 | } |
| 478 | auto param_ptr = make_typed_unique(param); |
| 479 | |
| 480 | const OSSL_PARAM* sk_param = |
| 481 | OSSL_PARAM_locate_const(param_ptr.get(), OSSL_PKEY_PARAM_PRIV_KEY); |
| 482 | if (sk_param == nullptr) { |
| 483 | return bytes({}, 0); |
| 484 | } |
| 485 | |
| 486 | BIGNUM* d = nullptr; |
| 487 | if (1 != OSSL_PARAM_get_BN(sk_param, &d)) { |
| 488 | return bytes({}, 0); |
| 489 | } |
| 490 | auto d_ptr = make_typed_unique(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 = bytes(BN_num_bytes(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.size()); |
| 503 | #endif |
| 504 | if (BN_bn2bin(d, out.data()) != out_size) { |
| 505 | throw openssl_error(); |
| 506 | } |
| 507 | |
| 508 | const auto zeros_needed = group_dh_size(id) - out.size(); |
| 509 | auto leading_zeros = bytes(zeros_needed, 0); |
| 510 | return leading_zeros + out; |
| 511 | } |
| 512 | |
| 513 | std::unique_ptr<Group::PrivateKey> deserialize_private( |
| 514 | const bytes& skm) const override |
| 515 | { |
| 516 | #if defined(WITH_OPENSSL3) |
| 517 | auto priv = make_typed_unique( |
| 518 | BN_bin2bn(skm.data(), static_cast<int>(skm.size()), nullptr)); |
| 519 | if (priv == nullptr) { |
| 520 | throw std::runtime_error("Unable to deserialize the private key"); |
| 521 | } |
| 522 | auto key = keypair_evp_key(priv); |
| 523 | return std::make_unique<EVPGroup::PrivateKey>(key.release()); |
| 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 | std::tuple<bytes, bytes> coordinates( |
| 542 | const Group::PublicKey& pk) const override |
| 543 | { |
| 544 | auto bn_x = make_typed_unique(BN_new()); |
| 545 | auto bn_y = make_typed_unique(BN_new()); |
| 546 | const auto& rpk = dynamic_cast<const PublicKey&>(pk); |
| 547 | |
| 548 | #if defined(WITH_OPENSSL3) |
| 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.pkey.get(), EVP_PKEY_PUBLIC_KEY, ¶m_ptr)) { |
| 552 | throw openssl_error(); |
| 553 | } |
| 554 | |
| 555 | auto param = make_typed_unique(param_ptr); |
| 556 | |
| 557 | // Raw pointer OK here because it is non-owning |
| 558 | const auto* pk_param = |
| 559 | OSSL_PARAM_locate_const(param.get(), OSSL_PKEY_PARAM_PUB_KEY); |
| 560 | if (pk_param == nullptr) { |
| 561 | throw 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 std::runtime_error("Failed to get OSSL_PKEY_PARAM_PUB_KEY len"); |
| 568 | } |
| 569 | |
| 570 | auto buf = bytes(len); |
| 571 | auto* buf_ptr = buf.data(); |
| 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 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_unique( |
| 581 | EC_GROUP_new_by_curve_name_ex(nullptr, nullptr, curve_nid)); |
| 582 | if (group == nullptr) { |
| 583 | throw openssl_error(); |
| 584 | } |
| 585 | |
| 586 | auto point = make_typed_unique(EC_POINT_new(group.get())); |
| 587 | if (point == nullptr) { |
| 588 | throw openssl_error(); |
| 589 | } |
| 590 | |
| 591 | if (1 != |
| 592 | EC_POINT_oct2point(group.get(), point.get(), buf_ptr, len, nullptr)) { |
| 593 | throw openssl_error(); |
| 594 | } |
| 595 | |
| 596 | // Retrieve the affine coordinates of the point |
| 597 | if (1 != EC_POINT_get_affine_coordinates( |
| 598 | group.get(), point.get(), bn_x.get(), bn_y.get(), nullptr)) { |
| 599 | throw openssl_error(); |
| 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_bytes(bn_x.get()); |
| 613 | auto x = bytes(x_size); |
| 614 | if (BN_bn2bin(bn_x.get(), x.data()) != x_size) { |
| 615 | throw openssl_error(); |
| 616 | } |
| 617 | |
| 618 | const auto y_size = BN_num_bytes(bn_y.get()); |
| 619 | auto y = bytes(y_size); |
| 620 | if (BN_bn2bin(bn_y.get(), y.data()) != y_size) { |
| 621 | throw openssl_error(); |
| 622 | } |
| 623 | |
| 624 | const auto zeros_needed_x = dh_size - x.size(); |
| 625 | const auto zeros_needed_y = dh_size - y.size(); |
| 626 | auto leading_zeros_x = bytes(zeros_needed_x, 0); |
| 627 | auto leading_zeros_y = bytes(zeros_needed_y, 0); |
| 628 | |
| 629 | return { leading_zeros_x + x, leading_zeros_y + y }; |
| 630 | } |
| 631 | |
| 632 | // EC Key |
| 633 | std::unique_ptr<Group::PublicKey> public_key_from_coordinates( |
| 634 | const bytes& x, |
| 635 | const bytes& y) const override |
| 636 | { |
| 637 | auto bn_x = make_typed_unique( |
| 638 | BN_bin2bn(x.data(), static_cast<int>(x.size()), nullptr)); |
| 639 | auto bn_y = make_typed_unique( |
| 640 | BN_bin2bn(y.data(), static_cast<int>(y.size()), nullptr)); |
| 641 | |
| 642 | if (bn_x == nullptr || bn_y == nullptr) { |
| 643 | throw std::runtime_error("Failed to convert bn_x or bn_y"); |
| 644 | } |
| 645 | |
| 646 | #if defined(WITH_OPENSSL3) |
| 647 | const auto group = make_typed_unique( |
| 648 | EC_GROUP_new_by_curve_name_ex(nullptr, nullptr, curve_nid)); |
| 649 | if (group == nullptr) { |
| 650 | throw std::runtime_error("Failed to create EC_GROUP"); |
| 651 | } |
| 652 | |
| 653 | // Construct a point with the given coordinates |
| 654 | auto point = make_typed_unique(EC_POINT_new(group.get())); |
| 655 | if (group == nullptr) { |
| 656 | throw std::runtime_error("Failed to create EC_POINT"); |
| 657 | } |
| 658 | |
| 659 | if (1 != EC_POINT_set_affine_coordinates( |
| 660 | group.get(), point.get(), bn_x.get(), bn_y.get(), nullptr)) { |
| 661 | throw openssl_error(); |
| 662 | } |
| 663 | |
| 664 | // Serialize the point |
| 665 | const auto point_size = EC_POINT_point2oct(group.get(), |
| 666 | point.get(), |
| 667 | POINT_CONVERSION_UNCOMPRESSED, |
| 668 | nullptr, |
| 669 | 0, |
| 670 | nullptr); |
| 671 | if (0 == point_size) { |
| 672 | throw openssl_error(); |
| 673 | } |
| 674 | |
| 675 | auto pub = bytes(point_size); |
| 676 | if (EC_POINT_point2oct(group.get(), |
| 677 | point.get(), |
| 678 | POINT_CONVERSION_UNCOMPRESSED, |
| 679 | pub.data(), |
| 680 | point_size, |
| 681 | nullptr) != point_size) { |
| 682 | throw openssl_error(); |
| 683 | } |
| 684 | |
| 685 | // Initialize a public key from the serialized point |
| 686 | auto key = public_evp_key(pub); |
| 687 | return std::make_unique<EVPGroup::PublicKey>(key.release()); |
| 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_nid; |
| 714 | |
| 715 | #if !defined(WITH_OPENSSL3) |
| 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_nid(Group::ID group_id) |
| 731 | { |
| 732 | switch (group_id) { |
| 733 | case Group::ID::P256: |
| 734 | return NID_X9_62_prime256v1; |
| 735 | case Group::ID::P384: |
| 736 | return NID_secp384r1; |
| 737 | case Group::ID::P521: |
| 738 | return NID_secp521r1; |
| 739 | default: |
| 740 | throw std::runtime_error("Unsupported algorithm"); |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | uint8_t bitmask() const |
| 745 | { |
| 746 | switch (id) { |
| 747 | case Group::ID::P256: |
| 748 | case Group::ID::P384: |
| 749 | return 0xff; |
| 750 | |
| 751 | case Group::ID::P521: |
| 752 | return 0x01; |
| 753 | |
| 754 | default: |
| 755 | throw std::runtime_error("Unsupported algorithm"); |
| 756 | } |
| 757 | } |
| 758 | }; |
| 759 | |
| 760 | /// |
| 761 | /// DH over "raw" curves |
| 762 | /// |
| 763 | |
| 764 | struct RawKeyGroup : public EVPGroup |
| 765 | { |
| 766 | RawKeyGroup(Group::ID group_id, const KDF& kdf) |
| 767 | : EVPGroup(group_id, kdf) |
| 768 | , evp_type(group_to_evp(group_id)) |
| 769 | { |
| 770 | } |
| 771 | |
| 772 | template<Group::ID id> |
| 773 | static const RawKeyGroup instance; |
| 774 | |
| 775 | std::unique_ptr<Group::PrivateKey> derive_key_pair( |
| 776 | const bytes& suite_id, |
| 777 | const bytes& ikm) const override |
| 778 | { |
| 779 | static const auto label_dkp_prk = from_ascii("dkp_prk"); |
| 780 | static const auto label_sk = from_ascii("sk"); |
| 781 | |
| 782 | auto dkp_prk = kdf.labeled_extract(suite_id, {}, label_dkp_prk, ikm); |
| 783 | auto skm = kdf.labeled_expand(suite_id, dkp_prk, label_sk, {}, sk_size); |
| 784 | return deserialize_private(skm); |
| 785 | } |
| 786 | |
| 787 | bytes serialize(const Group::PublicKey& pk) const override |
| 788 | { |
| 789 | const auto& rpk = dynamic_cast<const PublicKey&>(pk); |
| 790 | auto raw = bytes(pk_size); |
| 791 | auto* data_ptr = raw.data(); |
| 792 | auto data_len = raw.size(); |
| 793 | if (1 != EVP_PKEY_get_raw_public_key(rpk.pkey.get(), data_ptr, &data_len)) { |
| 794 | throw openssl_error(); |
| 795 | } |
| 796 | |
| 797 | return raw; |
| 798 | } |
| 799 | |
| 800 | std::unique_ptr<Group::PublicKey> deserialize(const bytes& enc) const override |
| 801 | { |
| 802 | auto* pkey = |
| 803 | EVP_PKEY_new_raw_public_key(evp_type, nullptr, enc.data(), enc.size()); |
| 804 | if (pkey == nullptr) { |
| 805 | throw openssl_error(); |
| 806 | } |
| 807 | |
| 808 | return std::make_unique<EVPGroup::PublicKey>(pkey); |
| 809 | } |
| 810 | |
| 811 | bytes serialize_private(const Group::PrivateKey& sk) const override |
| 812 | { |
| 813 | const auto& rsk = dynamic_cast<const PrivateKey&>(sk); |
| 814 | auto raw = bytes(sk_size); |
| 815 | auto* data_ptr = raw.data(); |
| 816 | auto data_len = raw.size(); |
| 817 | if (1 != |
| 818 | EVP_PKEY_get_raw_private_key(rsk.pkey.get(), data_ptr, &data_len)) { |
| 819 | throw openssl_error(); |
| 820 | } |
| 821 | |
| 822 | return raw; |
| 823 | } |
| 824 | |
| 825 | std::unique_ptr<Group::PrivateKey> deserialize_private( |
| 826 | const bytes& skm) const override |
| 827 | { |
| 828 | auto* pkey = |
| 829 | EVP_PKEY_new_raw_private_key(evp_type, nullptr, skm.data(), skm.size()); |
| 830 | if (pkey == nullptr) { |
| 831 | throw openssl_error(); |
| 832 | } |
| 833 | |
| 834 | return std::make_unique<EVPGroup::PrivateKey>(pkey); |
| 835 | } |
| 836 | |
| 837 | // Raw Key |
| 838 | std::tuple<bytes, bytes> coordinates( |
| 839 | const Group::PublicKey& pk) const override |
| 840 | { |
| 841 | const auto& rpk = dynamic_cast<const PublicKey&>(pk); |
| 842 | auto raw = bytes(pk_size); |
| 843 | auto* data_ptr = raw.data(); |
| 844 | auto data_len = raw.size(); |
| 845 | |
| 846 | if (1 != EVP_PKEY_get_raw_public_key(rpk.pkey.get(), data_ptr, &data_len)) { |
| 847 | throw openssl_error(); |
| 848 | } |
| 849 | |
| 850 | return { raw, {} }; |
| 851 | } |
| 852 | |
| 853 | // Raw Key |
| 854 | std::unique_ptr<Group::PublicKey> public_key_from_coordinates( |
| 855 | const bytes& x, |
| 856 | const bytes& /* y */) const override |
| 857 | { |
| 858 | return deserialize(x); |
| 859 | } |
| 860 | |
| 861 | private: |
| 862 | const int evp_type; |
| 863 | |
| 864 | static inline int group_to_evp(Group::ID group_id) |
| 865 | { |
| 866 | switch (group_id) { |
| 867 | case Group::ID::X25519: |
| 868 | return EVP_PKEY_X25519; |
| 869 | case Group::ID::X448: |
| 870 | return EVP_PKEY_X448; |
| 871 | case Group::ID::Ed25519: |
| 872 | return EVP_PKEY_ED25519; |
| 873 | case Group::ID::Ed448: |
| 874 | return EVP_PKEY_ED448; |
| 875 | default: |
| 876 | throw std::runtime_error("Unsupported algorithm"); |
| 877 | } |
| 878 | } |
| 879 | }; |
| 880 | |
| 881 | /// |
| 882 | /// General DH group |
| 883 | /// |
| 884 | |
| 885 | template<> |
| 886 | const Group& |
| 887 | Group::get<Group::ID::P256>() |
| 888 | { |
| 889 | static const ECKeyGroup instance(Group::ID::P256, |
| 890 | KDF::get<KDF::ID::HKDF_SHA256>()); |
| 891 | |
| 892 | return instance; |
| 893 | } |
| 894 | |
| 895 | template<> |
| 896 | const Group& |
| 897 | Group::get<Group::ID::P384>() |
| 898 | { |
| 899 | static const ECKeyGroup instance(Group::ID::P384, |
| 900 | KDF::get<KDF::ID::HKDF_SHA384>()); |
| 901 | |
| 902 | return instance; |
| 903 | } |
| 904 | |
| 905 | template<> |
| 906 | const Group& |
| 907 | Group::get<Group::ID::P521>() |
| 908 | { |
| 909 | static const ECKeyGroup instance(Group::ID::P521, |
| 910 | KDF::get<KDF::ID::HKDF_SHA512>()); |
| 911 | |
| 912 | return instance; |
| 913 | } |
| 914 | |
| 915 | template<> |
| 916 | const Group& |
| 917 | Group::get<Group::ID::X25519>() |
| 918 | { |
| 919 | static const RawKeyGroup instance(Group::ID::X25519, |
| 920 | KDF::get<KDF::ID::HKDF_SHA256>()); |
| 921 | return instance; |
| 922 | } |
| 923 | |
| 924 | template<> |
| 925 | const Group& |
| 926 | Group::get<Group::ID::Ed25519>() |
| 927 | { |
| 928 | static const RawKeyGroup instance(Group::ID::Ed25519, |
| 929 | KDF::get<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 Group& |
| 937 | Group::get<Group::ID::X448>() |
| 938 | { |
| 939 | static const RawKeyGroup instance(Group::ID::X448, |
| 940 | KDF::get<KDF::ID::HKDF_SHA512>()); |
| 941 | return instance; |
| 942 | } |
| 943 | #endif |
| 944 | |
| 945 | template<> |
| 946 | const Group& |
| 947 | Group::get<Group::ID::Ed448>() |
| 948 | { |
| 949 | static const RawKeyGroup instance(Group::ID::Ed448, |
| 950 | KDF::get<KDF::ID::HKDF_SHA512>()); |
| 951 | return instance; |
| 952 | } |
| 953 | |
| 954 | static inline size_t |
| 955 | group_dh_size(Group::ID group_id) |
| 956 | { |
| 957 | switch (group_id) { |
| 958 | case Group::ID::P256: |
| 959 | return 32; |
| 960 | case Group::ID::P384: |
| 961 | return 48; |
| 962 | case Group::ID::P521: |
| 963 | return 66; |
| 964 | case Group::ID::X25519: |
| 965 | return 32; |
| 966 | case Group::ID::X448: |
| 967 | return 56; |
| 968 | |
| 969 | // Non-DH groups |
| 970 | case Group::ID::Ed25519: |
| 971 | case Group::ID::Ed448: |
| 972 | return 0; |
| 973 | |
| 974 | default: |
| 975 | throw std::runtime_error("Unknown group"); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | static inline size_t |
| 980 | group_pk_size(Group::ID group_id) |
| 981 | { |
| 982 | switch (group_id) { |
| 983 | case Group::ID::P256: |
| 984 | return 65; |
| 985 | case Group::ID::P384: |
| 986 | return 97; |
| 987 | case Group::ID::P521: |
| 988 | return 133; |
| 989 | case Group::ID::X25519: |
| 990 | case Group::ID::Ed25519: |
| 991 | return 32; |
| 992 | case Group::ID::X448: |
| 993 | return 56; |
| 994 | case Group::ID::Ed448: |
| 995 | return 57; |
| 996 | |
| 997 | default: |
| 998 | throw std::runtime_error("Unknown group"); |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | static inline size_t |
| 1003 | group_sk_size(Group::ID group_id) |
| 1004 | { |
| 1005 | switch (group_id) { |
| 1006 | case Group::ID::P256: |
| 1007 | return 32; |
| 1008 | case Group::ID::P384: |
| 1009 | return 48; |
| 1010 | case Group::ID::P521: |
| 1011 | return 66; |
| 1012 | case Group::ID::X25519: |
| 1013 | case Group::ID::Ed25519: |
| 1014 | return 32; |
| 1015 | case Group::ID::X448: |
| 1016 | return 56; |
| 1017 | case Group::ID::Ed448: |
| 1018 | return 57; |
| 1019 | |
| 1020 | default: |
| 1021 | throw std::runtime_error("Unknown group"); |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | static inline std::string |
| 1026 | group_jwk_curve_name(Group::ID group_id) |
| 1027 | { |
| 1028 | switch (group_id) { |
| 1029 | case Group::ID::P256: |
| 1030 | return "P-256"; |
| 1031 | case Group::ID::P384: |
| 1032 | return "P-384"; |
| 1033 | case Group::ID::P521: |
| 1034 | return "P-521"; |
| 1035 | case Group::ID::Ed25519: |
| 1036 | return "Ed25519"; |
| 1037 | case Group::ID::Ed448: |
| 1038 | return "Ed448"; |
| 1039 | case Group::ID::X25519: |
| 1040 | return "X25519"; |
| 1041 | case Group::ID::X448: |
| 1042 | return "X448"; |
| 1043 | default: |
| 1044 | throw std::runtime_error("Unknown group"); |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | static inline std::string |
| 1049 | group_jwk_key_type(Group::ID group_id) |
| 1050 | { |
| 1051 | switch (group_id) { |
| 1052 | case Group::ID::P256: |
| 1053 | case Group::ID::P384: |
| 1054 | case Group::ID::P521: |
| 1055 | return "EC"; |
| 1056 | case Group::ID::Ed25519: |
| 1057 | case Group::ID::Ed448: |
| 1058 | case Group::ID::X25519: |
| 1059 | case Group::ID::X448: |
| 1060 | return "OKP"; |
| 1061 | default: |
| 1062 | throw std::runtime_error("Unknown group"); |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | Group::Group(ID group_id_in, const KDF& kdf_in) |
| 1067 | : id(group_id_in) |
| 1068 | , dh_size(group_dh_size(group_id_in)) |
| 1069 | , pk_size(group_pk_size(group_id_in)) |
| 1070 | , sk_size(group_sk_size(group_id_in)) |
| 1071 | , jwk_key_type(group_jwk_key_type(group_id_in)) |
| 1072 | , jwk_curve_name(group_jwk_curve_name(group_id_in)) |
| 1073 | , kdf(kdf_in) |
| 1074 | { |
| 1075 | } |
| 1076 | |
| 1077 | } // namespace mlspp::hpke |