| 1 |
// Copyright (c) 2015, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <openssl/ssl.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <string.h> |
| 8 |
|
| 9 |
#include <utility> |
| 10 |
|
| 11 |
#include <openssl/bn.h> |
| 12 |
#include <openssl/bytestring.h> |
| 13 |
#include <openssl/curve25519.h> |
| 14 |
#include <openssl/ec.h> |
| 15 |
#include <openssl/err.h> |
| 16 |
#include <openssl/evp.h> |
| 17 |
#include <openssl/hrss.h> |
| 18 |
#include <openssl/mem.h> |
| 19 |
#include <openssl/nid.h> |
| 20 |
#include <openssl/rand.h> |
| 21 |
#include <openssl/span.h> |
| 22 |
|
| 23 |
#include "internal.h" |
| 24 |
#include "../crypto/internal.h" |
| 25 |
#include "../crypto/fipsmodule/ec/internal.h" |
| 26 |
#include "../crypto/fipsmodule/ml_kem/ml_kem.h" |
| 27 |
|
| 28 |
BSSL_NAMESPACE_BEGIN |
| 29 |
|
| 30 |
namespace { |
| 31 |
|
| 32 |
class ECKeyShare : public SSLKeyShare { |
| 33 |
public: |
| 34 |
ECKeyShare(const EC_GROUP *group, uint16_t group_id) |
| 35 |
: group_(group), group_id_(group_id) {} |
| 36 |
|
| 37 |
uint16_t GroupID() const override { return group_id_; } |
| 38 |
|
| 39 |
bool Offer(CBB *out) override { |
| 40 |
assert(!private_key_); |
| 41 |
// Generate a private key. |
| 42 |
private_key_.reset(BN_new()); |
| 43 |
if (!private_key_ || |
| 44 |
!BN_rand_range_ex(private_key_.get(), 1, EC_GROUP_get0_order(group_))) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
// Compute the corresponding public key and serialize it. |
| 49 |
UniquePtr<EC_POINT> public_key(EC_POINT_new(group_)); |
| 50 |
if (!public_key || |
| 51 |
!EC_POINT_mul(group_, public_key.get(), private_key_.get(), nullptr, |
| 52 |
nullptr, /*ctx=*/nullptr) || |
| 53 |
!EC_POINT_point2cbb(out, group_, public_key.get(), |
| 54 |
POINT_CONVERSION_UNCOMPRESSED, /*ctx=*/nullptr)) { |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
return true; |
| 59 |
} |
| 60 |
|
| 61 |
bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert, |
| 62 |
Span<const uint8_t> peer_key) override { |
| 63 |
assert(group_); |
| 64 |
assert(private_key_); |
| 65 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 66 |
|
| 67 |
UniquePtr<EC_POINT> peer_point(EC_POINT_new(group_)); |
| 68 |
UniquePtr<EC_POINT> result(EC_POINT_new(group_)); |
| 69 |
UniquePtr<BIGNUM> x(BN_new()); |
| 70 |
if (!peer_point || !result || !x) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
if (peer_key.empty() || peer_key[0] != POINT_CONVERSION_UNCOMPRESSED || |
| 75 |
!EC_POINT_oct2point(group_, peer_point.get(), peer_key.data(), |
| 76 |
peer_key.size(), /*ctx=*/nullptr)) { |
| 77 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT); |
| 78 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
// Compute the x-coordinate of |peer_key| * |private_key_|. |
| 83 |
if (!EC_POINT_mul(group_, result.get(), nullptr, peer_point.get(), |
| 84 |
private_key_.get(), /*ctx=*/nullptr) || |
| 85 |
!EC_POINT_get_affine_coordinates_GFp(group_, result.get(), x.get(), |
| 86 |
nullptr, /*ctx=*/nullptr)) { |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
// Encode the x-coordinate left-padded with zeros. |
| 91 |
Array<uint8_t> secret; |
| 92 |
if (!secret.Init((EC_GROUP_get_degree(group_) + 7) / 8) || |
| 93 |
!BN_bn2bin_padded(secret.data(), secret.size(), x.get())) { |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
*out_secret = std::move(secret); |
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
bool SerializePrivateKey(CBB *out) override { |
| 102 |
assert(group_); |
| 103 |
assert(private_key_); |
| 104 |
// Padding is added to avoid leaking the length. |
| 105 |
size_t len = BN_num_bytes(EC_GROUP_get0_order(group_)); |
| 106 |
return BN_bn2cbb_padded(out, len, private_key_.get()); |
| 107 |
} |
| 108 |
|
| 109 |
bool DeserializePrivateKey(CBS *in) override { |
| 110 |
assert(!private_key_); |
| 111 |
private_key_.reset(BN_bin2bn(CBS_data(in), CBS_len(in), nullptr)); |
| 112 |
return private_key_ != nullptr; |
| 113 |
} |
| 114 |
|
| 115 |
private: |
| 116 |
UniquePtr<BIGNUM> private_key_; |
| 117 |
const EC_GROUP *const group_ = nullptr; |
| 118 |
uint16_t group_id_; |
| 119 |
}; |
| 120 |
|
| 121 |
class X25519KeyShare : public SSLKeyShare { |
| 122 |
public: |
| 123 |
X25519KeyShare() {} |
| 124 |
|
| 125 |
uint16_t GroupID() const override { return SSL_GROUP_X25519; } |
| 126 |
|
| 127 |
bool Offer(CBB *out) override { |
| 128 |
uint8_t public_key[32]; |
| 129 |
X25519_keypair(public_key, private_key_); |
| 130 |
return !!CBB_add_bytes(out, public_key, sizeof(public_key)); |
| 131 |
} |
| 132 |
|
| 133 |
bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert, |
| 134 |
Span<const uint8_t> peer_key) override { |
| 135 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 136 |
|
| 137 |
Array<uint8_t> secret; |
| 138 |
if (!secret.Init(32)) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
if (peer_key.size() != 32 || |
| 143 |
!X25519(secret.data(), private_key_, peer_key.data())) { |
| 144 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 145 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT); |
| 146 |
return false; |
| 147 |
} |
| 148 |
|
| 149 |
*out_secret = std::move(secret); |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
bool SerializePrivateKey(CBB *out) override { |
| 154 |
return CBB_add_bytes(out, private_key_, sizeof(private_key_)); |
| 155 |
} |
| 156 |
|
| 157 |
bool DeserializePrivateKey(CBS *in) override { |
| 158 |
if (CBS_len(in) != sizeof(private_key_) || |
| 159 |
!CBS_copy_bytes(in, private_key_, sizeof(private_key_))) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
private: |
| 166 |
uint8_t private_key_[32]; |
| 167 |
}; |
| 168 |
|
| 169 |
class KEMKeyShare : public SSLKeyShare { |
| 170 |
public: |
| 171 |
KEMKeyShare(int nid, uint16_t group_id) : nid_(nid), group_id_(group_id) {} |
| 172 |
|
| 173 |
uint16_t GroupID() const override { return group_id_; } |
| 174 |
|
| 175 |
bool Offer(CBB *out) override { |
| 176 |
// Ensure |out| is valid, has no children, and has been initialized. |
| 177 |
// We check that |out| has no children because otherwise CBB_data() |
| 178 |
// will produce a fatal error by way of assert(cbb->child == NULL). |
| 179 |
if (!out || out->child || !CBB_data(out)) { |
| 180 |
OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
// ctx_ should not have been initialized (by e.g. a previous call |
| 185 |
// to Offer()). |
| 186 |
if (ctx_) { |
| 187 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
// Initialize the KEM ctx |
| 192 |
ctx_.reset(EVP_PKEY_CTX_new_id(EVP_PKEY_KEM, nullptr)); |
| 193 |
if (!ctx_ || !EVP_PKEY_CTX_kem_set_params(ctx_.get(), nid_)) { |
| 194 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
// Generate a KEM keypair and retrieve the length of the public key |
| 199 |
size_t public_key_len = 0; |
| 200 |
EVP_PKEY *raw_key = nullptr; |
| 201 |
if (!EVP_PKEY_keygen_init(ctx_.get()) || |
| 202 |
!EVP_PKEY_keygen(ctx_.get(), &raw_key) || |
| 203 |
!raw_key || |
| 204 |
!EVP_PKEY_get_raw_public_key(raw_key, nullptr, &public_key_len)) { |
| 205 |
EVP_PKEY_free(raw_key); |
| 206 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
// Retain the secret key for decapsulation |
| 211 |
UniquePtr<EVP_PKEY> pkey(raw_key); |
| 212 |
ctx_.reset(EVP_PKEY_CTX_new(pkey.get(), nullptr)); |
| 213 |
if (!ctx_) { |
| 214 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 215 |
return false; |
| 216 |
} |
| 217 |
|
| 218 |
uint8_t *public_key = NULL; |
| 219 |
size_t public_key_bytes_written = public_key_len; |
| 220 |
if (!CBB_add_space(out, &public_key, public_key_len) || |
| 221 |
!EVP_PKEY_get_raw_public_key(pkey.get(), public_key, |
| 222 |
&public_key_bytes_written) || |
| 223 |
public_key_bytes_written != public_key_len) { |
| 224 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 225 |
return false; |
| 226 |
} |
| 227 |
|
| 228 |
return true; |
| 229 |
} |
| 230 |
|
| 231 |
// Because this is a KEM key share, |out_public_key| will be the ciphertext |
| 232 |
// resulting from the encapsulation of the shared secret under the |
| 233 |
// received public key, |peek_key|. |
| 234 |
bool Accept(CBB *out_public_key, Array<uint8_t> *out_secret, |
| 235 |
uint8_t *out_alert, Span<const uint8_t> peer_key) override { |
| 236 |
// Basic nullptr checks |
| 237 |
if (!out_alert) { |
| 238 |
OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
// Set alert to internal error by default |
| 243 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 244 |
|
| 245 |
if (!out_secret) { |
| 246 |
OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 247 |
return false; |
| 248 |
} |
| 249 |
|
| 250 |
// Ensure |out_public_key| is valid, has no children, and has been |
| 251 |
// initialized. We check that |out_public_key| has no children because |
| 252 |
// otherwise CBB_data() will produce a fatal error by way of |
| 253 |
// assert(cbb->child == NULL). |
| 254 |
if (!out_public_key || out_public_key->child || !CBB_data(out_public_key)) { |
| 255 |
OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 256 |
return false; |
| 257 |
} |
| 258 |
|
| 259 |
// ctx_ should not have been initialized (by e.g. a call to Offer()). |
| 260 |
if (ctx_) { |
| 261 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 262 |
return false; |
| 263 |
} |
| 264 |
|
| 265 |
// Ensure that peer_key is valid |
| 266 |
if (!peer_key.data() || peer_key.empty()) { |
| 267 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 268 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 269 |
return false; |
| 270 |
} |
| 271 |
|
| 272 |
// Initialize pkey from the received public key |
| 273 |
UniquePtr<EVP_PKEY> pkey( |
| 274 |
EVP_PKEY_kem_new_raw_public_key(nid_, peer_key.begin(), peer_key.size())); |
| 275 |
|
| 276 |
if (!pkey) { |
| 277 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 278 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 279 |
return false; |
| 280 |
} |
| 281 |
|
| 282 |
// Initialize the context with the pkey |
| 283 |
ctx_.reset(EVP_PKEY_CTX_new(pkey.get(), nullptr)); |
| 284 |
if (!ctx_) { |
| 285 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 286 |
return false; |
| 287 |
} |
| 288 |
|
| 289 |
// Retrieve the lengths of the ciphertext and shared secret |
| 290 |
size_t ciphertext_len = 0; |
| 291 |
size_t secret_len = 0; |
| 292 |
if (!EVP_PKEY_encapsulate(ctx_.get(), nullptr, &ciphertext_len, |
| 293 |
nullptr, &secret_len)) { |
| 294 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 295 |
return false; |
| 296 |
} |
| 297 |
|
| 298 |
// EVP_PKEY_encapsulate() will generate a shared secret, then encapsulate it |
| 299 |
// with the peer's public key. We send the resultant ciphertext to the |
| 300 |
// peer by writing it to |out_public_key|, then... |
| 301 |
Array<uint8_t> shared_secret; |
| 302 |
if (!shared_secret.Init(secret_len)) { |
| 303 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 304 |
return false; |
| 305 |
} |
| 306 |
|
| 307 |
uint8_t *ciphertext = NULL; |
| 308 |
size_t ciphertext_bytes_written = ciphertext_len; |
| 309 |
size_t secret_bytes_written = secret_len; |
| 310 |
|
| 311 |
if (!CBB_add_space(out_public_key, &ciphertext, ciphertext_len) || |
| 312 |
!EVP_PKEY_encapsulate(ctx_.get(), ciphertext, |
| 313 |
&ciphertext_bytes_written, shared_secret.data(), |
| 314 |
&secret_bytes_written) || |
| 315 |
ciphertext_bytes_written != ciphertext_len || |
| 316 |
secret_bytes_written != secret_len) { |
| 317 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 318 |
return false; |
| 319 |
} |
| 320 |
|
| 321 |
// ...we retain the shared secret for our own use. |
| 322 |
*out_secret = std::move(shared_secret); |
| 323 |
|
| 324 |
// Success; clear the alert |
| 325 |
*out_alert = 0; |
| 326 |
return true; |
| 327 |
} |
| 328 |
|
| 329 |
// Because this is a KEM key share, |peer_key| is actually the ciphertext |
| 330 |
// resulting from the peer encapsulating the shared secret under our public key. |
| 331 |
// In Finish(), we use our previously generated secret key to decrypt |
| 332 |
// that ciphertext and obtain the shared secret. |
| 333 |
bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert, |
| 334 |
Span<const uint8_t> peer_key) override { |
| 335 |
// Basic nullptr checks |
| 336 |
if (!out_alert) { |
| 337 |
OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 338 |
return false; |
| 339 |
} |
| 340 |
|
| 341 |
// Set alert to internal error by default |
| 342 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 343 |
|
| 344 |
if (!out_secret) { |
| 345 |
OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 346 |
return false; |
| 347 |
} |
| 348 |
|
| 349 |
// ctx_ should have been initialized by a previous call to Offer() |
| 350 |
if (!ctx_) { |
| 351 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 352 |
return false; |
| 353 |
} |
| 354 |
|
| 355 |
// Retrieve the length of the shared secret |
| 356 |
size_t secret_len = 0; |
| 357 |
if (!EVP_PKEY_decapsulate(ctx_.get(), nullptr, &secret_len, nullptr, peer_key.size())) { |
| 358 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 359 |
return false; |
| 360 |
} |
| 361 |
|
| 362 |
// |peer_key| is the ciphertext, encapsulating the shared secret, |
| 363 |
// that the peer generated using our public key. We use our secret |
| 364 |
// key to decapsulate it, and retain the shared secret by writing |
| 365 |
// it to |out_secret|. |
| 366 |
uint8_t *ciphertext = (uint8_t *)peer_key.begin(); |
| 367 |
Array<uint8_t> shared_secret; |
| 368 |
if (!shared_secret.Init(secret_len)) { |
| 369 |
OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 370 |
return false; |
| 371 |
} |
| 372 |
|
| 373 |
size_t secret_bytes_written = secret_len; |
| 374 |
if (!EVP_PKEY_decapsulate(ctx_.get(), shared_secret.data(), |
| 375 |
&secret_bytes_written, ciphertext, |
| 376 |
peer_key.size()) || |
| 377 |
secret_bytes_written != secret_len) { |
| 378 |
OPENSSL_PUT_ERROR(SSL, SSL_AD_ILLEGAL_PARAMETER); |
| 379 |
return false; |
| 380 |
} |
| 381 |
|
| 382 |
*out_secret = std::move(shared_secret); |
| 383 |
|
| 384 |
// Success; clear the alert |
| 385 |
*out_alert = 0; |
| 386 |
return true; |
| 387 |
} |
| 388 |
|
| 389 |
private: |
| 390 |
int nid_; |
| 391 |
uint16_t group_id_; |
| 392 |
UniquePtr<EVP_PKEY_CTX> ctx_; |
| 393 |
}; |
| 394 |
|
| 395 |
// A HybridKeyShare consists of key shares from two or more component groups, |
| 396 |
// all of which are used to generate a hybrid shared secret. |
| 397 |
// See https://datatracker.ietf.org/doc/html/draft-ietf-tls-hybrid-design. |
| 398 |
class HybridKeyShare : public SSLKeyShare { |
| 399 |
public: |
| 400 |
HybridKeyShare(uint16_t group_id) : group_id_(group_id), |
| 401 |
exchange_performed(false), hybrid_group_(nullptr) { |
| 402 |
for (const HybridGroup &hybrid_group : HybridGroups()) { |
| 403 |
if (group_id_ == hybrid_group.group_id) { |
| 404 |
hybrid_group_ = &hybrid_group; |
| 405 |
for (size_t i = 0; i < NUM_HYBRID_COMPONENTS; i++) { |
| 406 |
key_shares_[i] = SSLKeyShare::Create(hybrid_group.component_group_ids[i]); |
| 407 |
} |
| 408 |
return; |
| 409 |
} |
| 410 |
} |
| 411 |
hybrid_group_ = nullptr; |
| 412 |
} |
| 413 |
|
| 414 |
uint16_t GroupID() const override { return group_id_; } |
| 415 |
|
| 416 |
bool Offer(CBB *out) override { |
| 417 |
// Ensure |out| is valid, has no children, and has been initialized. |
| 418 |
// We check that |out| has no children because otherwise CBB_data() |
| 419 |
// will produce a fatal error by way of assert(cbb->child == NULL); |
| 420 |
if (!out || out->child || !CBB_data(out)) { |
| 421 |
OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 422 |
return false; |
| 423 |
} |
| 424 |
|
| 425 |
if (!hybrid_group_ || this->exchange_performed) { |
| 426 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 427 |
return false; |
| 428 |
} |
| 429 |
|
| 430 |
// Iterate through the component groups and Offer() each of their key |
| 431 |
// shares. If one of the calls to a component Offer() fails, |
| 432 |
// OPENSSL_PUT_ERROR will be set appropriately in that component. |
| 433 |
for (const UniquePtr<SSLKeyShare> &key_share : key_shares_) { |
| 434 |
if (!key_share || !key_share->Offer(out)) { |
| 435 |
return false; |
| 436 |
} |
| 437 |
} |
| 438 |
this->exchange_performed = true; |
| 439 |
return true; |
| 440 |
} |
| 441 |
|
| 442 |
bool Accept(CBB *out_public_key, Array<uint8_t> *out_secret, |
| 443 |
uint8_t *out_alert, Span<const uint8_t> peer_key) override { |
| 444 |
if (!out_alert) { |
| 445 |
OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 446 |
return false; |
| 447 |
} |
| 448 |
|
| 449 |
// Set alert to internal error by default |
| 450 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 451 |
|
| 452 |
if (!out_secret|| !peer_key.data()) { |
| 453 |
OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 454 |
return false; |
| 455 |
} |
| 456 |
|
| 457 |
// Ensure |out_public_key| is valid, has no children, and has been |
| 458 |
// initialized. We check that |out_public_key| has no children because |
| 459 |
// otherwise CBB_data() will produce a fatal error by way of |
| 460 |
// assert(cbb->child == NULL); |
| 461 |
if (!out_public_key || out_public_key->child || !CBB_data(out_public_key)) { |
| 462 |
OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 463 |
return false; |
| 464 |
} |
| 465 |
|
| 466 |
if (!hybrid_group_ || this->exchange_performed) { |
| 467 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 468 |
return false; |
| 469 |
} |
| 470 |
|
| 471 |
// A hybrid shared secret with two components should be 64 bytes. |
| 472 |
// If it happens to be larger, the CBB will grow accordingly. |
| 473 |
CBB hybrid_shared_secret; |
| 474 |
if (!CBB_init(&hybrid_shared_secret, 64)) { |
| 475 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 476 |
return false; |
| 477 |
} |
| 478 |
|
| 479 |
// Accept() each component key share. Each component's Accept() function |
| 480 |
// will generate a shared secret and a public key to be sent back to |
| 481 |
// the peer. The hybrid public key is the concatenation of all component |
| 482 |
// public keys; the hybrid shared secret is the concatenation of all |
| 483 |
// component shared secrets. |
| 484 |
size_t peer_key_read_index = 0; |
| 485 |
for (size_t i = 0; i < NUM_HYBRID_COMPONENTS; i++) { |
| 486 |
size_t component_key_size = 0; |
| 487 |
if (!get_component_offer_key_share_size(&component_key_size, hybrid_group_->component_group_ids[i])) { |
| 488 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 489 |
return false; |
| 490 |
} |
| 491 |
|
| 492 |
// Verify that |peer_key| contains enough data |
| 493 |
if (peer_key_read_index + component_key_size > peer_key.size()) { |
| 494 |
CBB_cleanup(&hybrid_shared_secret); |
| 495 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 496 |
OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HYBRID_KEYSHARE); |
| 497 |
return false; |
| 498 |
} |
| 499 |
|
| 500 |
Span<const uint8_t> component_key = |
| 501 |
peer_key.subspan(peer_key_read_index, component_key_size); |
| 502 |
|
| 503 |
Array<uint8_t> component_secret; |
| 504 |
if (!key_shares_[i] || |
| 505 |
!key_shares_[i]->Accept(out_public_key, &component_secret, out_alert, component_key) || |
| 506 |
!CBB_add_bytes(&hybrid_shared_secret, component_secret.data(), component_secret.size())) { |
| 507 |
CBB_cleanup(&hybrid_shared_secret); |
| 508 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 509 |
return false; |
| 510 |
} |
| 511 |
|
| 512 |
peer_key_read_index += component_key_size; |
| 513 |
} |
| 514 |
|
| 515 |
// Final validation that |peer_key| was the correct size |
| 516 |
if (peer_key_read_index != peer_key.size()) { |
| 517 |
CBB_cleanup(&hybrid_shared_secret); |
| 518 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 519 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 520 |
return false; |
| 521 |
} |
| 522 |
|
| 523 |
// Retain the hybrid shared secret for our use. |
| 524 |
if (!CBBFinishArray(&hybrid_shared_secret, out_secret)) { |
| 525 |
CBB_cleanup(&hybrid_shared_secret); |
| 526 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 527 |
return false; |
| 528 |
} |
| 529 |
|
| 530 |
// Success; clear the alert |
| 531 |
*out_alert = 0; |
| 532 |
|
| 533 |
this->exchange_performed = true; |
| 534 |
return true; |
| 535 |
} |
| 536 |
|
| 537 |
bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert, |
| 538 |
Span<const uint8_t> peer_key) override { |
| 539 |
if (!out_alert) { |
| 540 |
OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 541 |
return false; |
| 542 |
} |
| 543 |
|
| 544 |
// Set alert to internal error by default |
| 545 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 546 |
|
| 547 |
if (!out_secret || !peer_key.data()) { |
| 548 |
OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER); |
| 549 |
return false; |
| 550 |
} |
| 551 |
|
| 552 |
if (!hybrid_group_ || !this->exchange_performed) { |
| 553 |
OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 554 |
return false; |
| 555 |
} |
| 556 |
|
| 557 |
// A hybrid shared secret with two components should be 64 bytes. |
| 558 |
// If it happens to be larger, the CBB will grow accordingly. |
| 559 |
CBB hybrid_shared_secret; |
| 560 |
if (!CBB_init(&hybrid_shared_secret, 64)) { |
| 561 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 562 |
return false; |
| 563 |
} |
| 564 |
|
| 565 |
// Finish() each component key share. Each component's Finish() function |
| 566 |
// will generate a shared secret. The hybrid shared secret is the |
| 567 |
// concatenation of all component shared secrets. |
| 568 |
size_t peer_key_index = 0; |
| 569 |
for (size_t i = 0; i < NUM_HYBRID_COMPONENTS; i++) { |
| 570 |
|
| 571 |
size_t component_key_size = 0; |
| 572 |
if (!get_component_accept_key_share_size(&component_key_size, hybrid_group_->component_group_ids[i])) { |
| 573 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 574 |
return false; |
| 575 |
} |
| 576 |
|
| 577 |
// Verify that |peer_key| contains enough data |
| 578 |
if (peer_key_index + component_key_size > peer_key.size()) { |
| 579 |
CBB_cleanup(&hybrid_shared_secret); |
| 580 |
*out_alert = SSL_AD_DECODE_ERROR; |
| 581 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 582 |
return false; |
| 583 |
} |
| 584 |
|
| 585 |
Span<const uint8_t> component_key = |
| 586 |
peer_key.subspan(peer_key_index, component_key_size); |
| 587 |
|
| 588 |
Array<uint8_t> component_secret; |
| 589 |
if (!key_shares_[i] || |
| 590 |
!key_shares_[i]->Finish(&component_secret, out_alert, component_key) || |
| 591 |
!CBB_add_bytes(&hybrid_shared_secret, component_secret.data(), component_secret.size())) { |
| 592 |
CBB_cleanup(&hybrid_shared_secret); |
| 593 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 594 |
return false; |
| 595 |
} |
| 596 |
|
| 597 |
peer_key_index += component_key_size; |
| 598 |
} |
| 599 |
|
| 600 |
// Final validation that |peer_key| was the correct size |
| 601 |
if (peer_key_index != peer_key.size()) { |
| 602 |
CBB_cleanup(&hybrid_shared_secret); |
| 603 |
*out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 604 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 605 |
return false; |
| 606 |
} |
| 607 |
|
| 608 |
// Retain the hybrid shared secret for our use. |
| 609 |
if (!CBBFinishArray(&hybrid_shared_secret, out_secret)) { |
| 610 |
CBB_cleanup(&hybrid_shared_secret); |
| 611 |
OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 612 |
return false; |
| 613 |
} |
| 614 |
|
| 615 |
// Success; clear the alert |
| 616 |
*out_alert = 0; |
| 617 |
return true; |
| 618 |
} |
| 619 |
|
| 620 |
private: |
| 621 |
// Only need to support SSL Groups that are a component of a supported HybridKeyShare |
| 622 |
bool get_component_offer_key_share_size(size_t *out, uint16_t component_group_id) { |
| 623 |
switch (component_group_id) { |
| 624 |
case SSL_GROUP_SECP256R1: |
| 625 |
*out = 1 + (2 * EC_P256R1_FIELD_ELEM_BYTES); |
| 626 |
return true; |
| 627 |
case SSL_GROUP_SECP384R1: |
| 628 |
*out = 1 + (2 * EC_P384R1_FIELD_ELEM_BYTES); |
| 629 |
return true; |
| 630 |
case SSL_GROUP_MLKEM768: |
| 631 |
*out = MLKEM768_PUBLIC_KEY_BYTES; |
| 632 |
return true; |
| 633 |
case SSL_GROUP_MLKEM1024: |
| 634 |
*out = MLKEM1024_PUBLIC_KEY_BYTES; |
| 635 |
return true; |
| 636 |
case SSL_GROUP_X25519: |
| 637 |
*out = 32; |
| 638 |
return true; |
| 639 |
default: |
| 640 |
return false; |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
// Only need to support SSL Groups that are a component of a supported HybridKeyShare |
| 645 |
bool get_component_accept_key_share_size(size_t *out, uint16_t component_group_id) { |
| 646 |
switch (component_group_id) { |
| 647 |
case SSL_GROUP_SECP256R1: |
| 648 |
*out = 1 + (2 * EC_P256R1_FIELD_ELEM_BYTES); |
| 649 |
return true; |
| 650 |
case SSL_GROUP_SECP384R1: |
| 651 |
*out = 1 + (2 * EC_P384R1_FIELD_ELEM_BYTES); |
| 652 |
return true; |
| 653 |
case SSL_GROUP_MLKEM768: |
| 654 |
*out = MLKEM768_CIPHERTEXT_BYTES; |
| 655 |
return true; |
| 656 |
case SSL_GROUP_MLKEM1024: |
| 657 |
*out = MLKEM1024_CIPHERTEXT_BYTES; |
| 658 |
return true; |
| 659 |
case SSL_GROUP_X25519: |
| 660 |
*out = 32; |
| 661 |
return true; |
| 662 |
default: |
| 663 |
return false; |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
uint16_t group_id_; |
| 668 |
bool exchange_performed; |
| 669 |
const HybridGroup *hybrid_group_; |
| 670 |
UniquePtr<SSLKeyShare> key_shares_[NUM_HYBRID_COMPONENTS]; |
| 671 |
}; |
| 672 |
|
| 673 |
CONSTEXPR_ARRAY NamedGroup kNamedGroups[] = { |
| 674 |
{NID_secp224r1, SSL_GROUP_SECP224R1, "P-224", "secp224r1"}, |
| 675 |
{NID_X9_62_prime256v1, SSL_GROUP_SECP256R1, "P-256", "prime256v1"}, |
| 676 |
{NID_secp384r1, SSL_GROUP_SECP384R1, "P-384", "secp384r1"}, |
| 677 |
{NID_secp521r1, SSL_GROUP_SECP521R1, "P-521", "secp521r1"}, |
| 678 |
{NID_X25519, SSL_GROUP_X25519, "X25519", "x25519"}, |
| 679 |
{NID_SecP256r1MLKEM768, SSL_GROUP_SECP256R1_MLKEM768, "SecP256r1MLKEM768", ""}, |
| 680 |
{NID_X25519MLKEM768, SSL_GROUP_X25519_MLKEM768, "X25519MLKEM768", ""}, |
| 681 |
{NID_SecP384r1MLKEM1024, SSL_GROUP_SECP384R1_MLKEM1024, "SecP384r1MLKEM1024", ""}, |
| 682 |
{NID_MLKEM512, SSL_GROUP_MLKEM512, "MLKEM512", "ML-KEM-512"}, |
| 683 |
{NID_MLKEM768, SSL_GROUP_MLKEM768, "MLKEM768", "ML-KEM-768"}, |
| 684 |
{NID_MLKEM1024, SSL_GROUP_MLKEM1024, "MLKEM1024", "ML-KEM-1024"}, |
| 685 |
}; |
| 686 |
|
| 687 |
CONSTEXPR_ARRAY uint16_t kPQGroups[] = { |
| 688 |
SSL_GROUP_MLKEM512, |
| 689 |
SSL_GROUP_MLKEM768, |
| 690 |
SSL_GROUP_MLKEM1024, |
| 691 |
SSL_GROUP_SECP256R1_MLKEM768, |
| 692 |
SSL_GROUP_X25519_MLKEM768, |
| 693 |
SSL_GROUP_SECP384R1_MLKEM1024, |
| 694 |
}; |
| 695 |
|
| 696 |
CONSTEXPR_ARRAY HybridGroup kHybridGroups[] = { |
| 697 |
{ |
| 698 |
SSL_GROUP_SECP256R1_MLKEM768, // group_id |
| 699 |
{ |
| 700 |
SSL_GROUP_SECP256R1, // component_group_ids[0] |
| 701 |
SSL_GROUP_MLKEM768, // component_group_ids[1] |
| 702 |
}, |
| 703 |
}, |
| 704 |
{ |
| 705 |
SSL_GROUP_X25519_MLKEM768, // group_id |
| 706 |
{ |
| 707 |
// Note: MLKEM768 is sent first due to FIPS requirements. |
| 708 |
// For more details, see https://datatracker.ietf.org/doc/html/draft-kwiatkowski-tls-ecdhe-mlkem.html#section-3 |
| 709 |
SSL_GROUP_MLKEM768, // component_group_ids[0] |
| 710 |
SSL_GROUP_X25519, // component_group_ids[1] |
| 711 |
}, |
| 712 |
}, |
| 713 |
{ |
| 714 |
SSL_GROUP_SECP384R1_MLKEM1024, // group_id |
| 715 |
{ |
| 716 |
SSL_GROUP_SECP384R1, // component_group_ids[0] |
| 717 |
SSL_GROUP_MLKEM1024, // component_group_ids[1] |
| 718 |
}, |
| 719 |
}, |
| 720 |
}; |
| 721 |
|
| 722 |
} // namespace |
| 723 |
|
| 724 |
Span<const NamedGroup> NamedGroups() { |
| 725 |
return MakeConstSpan(kNamedGroups, OPENSSL_ARRAY_SIZE(kNamedGroups)); |
| 726 |
} |
| 727 |
|
| 728 |
Span<const HybridGroup> HybridGroups() { |
| 729 |
return MakeConstSpan(kHybridGroups, OPENSSL_ARRAY_SIZE(kHybridGroups)); |
| 730 |
} |
| 731 |
|
| 732 |
Span<const uint16_t> PQGroups() { |
| 733 |
return MakeConstSpan(kPQGroups, OPENSSL_ARRAY_SIZE(kPQGroups)); |
| 734 |
} |
| 735 |
|
| 736 |
UniquePtr<SSLKeyShare> SSLKeyShare::Create(uint16_t group_id) { |
| 737 |
switch (group_id) { |
| 738 |
case SSL_GROUP_SECP224R1: |
| 739 |
return MakeUnique<ECKeyShare>(EC_group_p224(), SSL_GROUP_SECP224R1); |
| 740 |
case SSL_GROUP_SECP256R1: |
| 741 |
return MakeUnique<ECKeyShare>(EC_group_p256(), SSL_GROUP_SECP256R1); |
| 742 |
case SSL_GROUP_SECP384R1: |
| 743 |
return MakeUnique<ECKeyShare>(EC_group_p384(), SSL_GROUP_SECP384R1); |
| 744 |
case SSL_GROUP_SECP521R1: |
| 745 |
return MakeUnique<ECKeyShare>(EC_group_p521(), SSL_GROUP_SECP521R1); |
| 746 |
case SSL_GROUP_X25519: |
| 747 |
return MakeUnique<X25519KeyShare>(); |
| 748 |
case SSL_GROUP_MLKEM512: |
| 749 |
return MakeUnique<KEMKeyShare>(NID_MLKEM512, SSL_GROUP_MLKEM512); |
| 750 |
case SSL_GROUP_MLKEM768: |
| 751 |
return MakeUnique<KEMKeyShare>(NID_MLKEM768, SSL_GROUP_MLKEM768); |
| 752 |
case SSL_GROUP_MLKEM1024: |
| 753 |
return MakeUnique<KEMKeyShare>(NID_MLKEM1024, SSL_GROUP_MLKEM1024); |
| 754 |
case SSL_GROUP_SECP256R1_MLKEM768: |
| 755 |
return MakeUnique<HybridKeyShare>(SSL_GROUP_SECP256R1_MLKEM768); |
| 756 |
case SSL_GROUP_X25519_MLKEM768: |
| 757 |
return MakeUnique<HybridKeyShare>(SSL_GROUP_X25519_MLKEM768); |
| 758 |
case SSL_GROUP_SECP384R1_MLKEM1024: |
| 759 |
return MakeUnique<HybridKeyShare>(SSL_GROUP_SECP384R1_MLKEM1024); |
| 760 |
default: |
| 761 |
return nullptr; |
| 762 |
} |
| 763 |
} |
| 764 |
|
| 765 |
bool SSLKeyShare::Accept(CBB *out_public_key, Array<uint8_t> *out_secret, |
| 766 |
uint8_t *out_alert, Span<const uint8_t> peer_key) { |
| 767 |
*out_alert = SSL_AD_INTERNAL_ERROR; |
| 768 |
return Offer(out_public_key) && |
| 769 |
Finish(out_secret, out_alert, peer_key); |
| 770 |
} |
| 771 |
|
| 772 |
bool ssl_nid_to_group_id(uint16_t *out_group_id, int nid) { |
| 773 |
for (const auto &group : kNamedGroups) { |
| 774 |
if (group.nid == nid) { |
| 775 |
*out_group_id = group.group_id; |
| 776 |
return true; |
| 777 |
} |
| 778 |
} |
| 779 |
return false; |
| 780 |
} |
| 781 |
|
| 782 |
bool ssl_group_id_to_nid(uint16_t *out_nid, int group_id) { |
| 783 |
GUARD_PTR(out_nid); |
| 784 |
for (const auto &group : kNamedGroups) { |
| 785 |
if (group.group_id == group_id) { |
| 786 |
*out_nid = group.nid; |
| 787 |
return true; |
| 788 |
} |
| 789 |
} |
| 790 |
return false; |
| 791 |
} |
| 792 |
|
| 793 |
bool ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len) { |
| 794 |
for (const auto &group : kNamedGroups) { |
| 795 |
if (len == strlen(group.name) && |
| 796 |
!strncmp(group.name, name, len)) { |
| 797 |
*out_group_id = group.group_id; |
| 798 |
return true; |
| 799 |
} |
| 800 |
if ((strlen(group.alias) > 0) && len == strlen(group.alias) && |
| 801 |
!strncmp(group.alias, name, len)) { |
| 802 |
*out_group_id = group.group_id; |
| 803 |
return true; |
| 804 |
} |
| 805 |
} |
| 806 |
return false; |
| 807 |
} |
| 808 |
|
| 809 |
int ssl_group_id_to_nid(uint16_t group_id) { |
| 810 |
for (const auto &group : kNamedGroups) { |
| 811 |
if (group.group_id == group_id) { |
| 812 |
return group.nid; |
| 813 |
} |
| 814 |
} |
| 815 |
return NID_undef; |
| 816 |
} |
| 817 |
|
| 818 |
BSSL_NAMESPACE_END |
| 819 |
|
| 820 |
using namespace bssl; |
| 821 |
|
| 822 |
const char* SSL_get_group_name(uint16_t group_id) { |
| 823 |
for (const auto &group : kNamedGroups) { |
| 824 |
if (group.group_id == group_id) { |
| 825 |
return group.name; |
| 826 |
} |
| 827 |
} |
| 828 |
return nullptr; |
| 829 |
} |
| 830 |
|
| 831 |
size_t SSL_get_all_group_names(const char **out, size_t max_out) { |
| 832 |
return GetAllNames(out, max_out, Span<const char *>(), &NamedGroup::name, |
| 833 |
MakeConstSpan(kNamedGroups)); |
| 834 |
} |