aws-lc-fips-sys 0.13.9

AWS-LC is a general-purpose cryptographic library maintained by the AWS Cryptography team for AWS and their customers. This is the FIPS validated version of AWS-LC.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
/* Copyright (c) 2015, Google Inc.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */

#include <openssl/ssl.h>

#include <assert.h>
#include <string.h>

#include <utility>

#include <openssl/bn.h>
#include <openssl/bytestring.h>
#include <openssl/curve25519.h>
#include <openssl/ec.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/hrss.h>
#include <openssl/mem.h>
#include <openssl/nid.h>
#include <openssl/rand.h>
#include <openssl/span.h>

#include "internal.h"
#include "../crypto/internal.h"
#include "../crypto/fipsmodule/ec/internal.h"
#include "../crypto/fipsmodule/ml_kem/ml_kem.h"
#include "../crypto/kyber/kem_kyber.h"

BSSL_NAMESPACE_BEGIN

namespace {

class ECKeyShare : public SSLKeyShare {
 public:
  ECKeyShare(const EC_GROUP *group, uint16_t group_id)
      : group_(group), group_id_(group_id) {}

  uint16_t GroupID() const override { return group_id_; }

  bool Offer(CBB *out) override {
    assert(!private_key_);
    // Generate a private key.
    private_key_.reset(BN_new());
    if (!private_key_ ||
        !BN_rand_range_ex(private_key_.get(), 1, EC_GROUP_get0_order(group_))) {
      return false;
    }

    // Compute the corresponding public key and serialize it.
    UniquePtr<EC_POINT> public_key(EC_POINT_new(group_));
    if (!public_key ||
        !EC_POINT_mul(group_, public_key.get(), private_key_.get(), nullptr,
                      nullptr, /*ctx=*/nullptr) ||
        !EC_POINT_point2cbb(out, group_, public_key.get(),
                            POINT_CONVERSION_UNCOMPRESSED, /*ctx=*/nullptr)) {
      return false;
    }

    return true;
  }

  bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert,
              Span<const uint8_t> peer_key) override {
    assert(group_);
    assert(private_key_);
    *out_alert = SSL_AD_INTERNAL_ERROR;

    UniquePtr<EC_POINT> peer_point(EC_POINT_new(group_));
    UniquePtr<EC_POINT> result(EC_POINT_new(group_));
    UniquePtr<BIGNUM> x(BN_new());
    if (!peer_point || !result || !x) {
      return false;
    }

    if (peer_key.empty() || peer_key[0] != POINT_CONVERSION_UNCOMPRESSED ||
        !EC_POINT_oct2point(group_, peer_point.get(), peer_key.data(),
                            peer_key.size(), /*ctx=*/nullptr)) {
      OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT);
      *out_alert = SSL_AD_DECODE_ERROR;
      return false;
    }

    // Compute the x-coordinate of |peer_key| * |private_key_|.
    if (!EC_POINT_mul(group_, result.get(), nullptr, peer_point.get(),
                      private_key_.get(), /*ctx=*/nullptr) ||
        !EC_POINT_get_affine_coordinates_GFp(group_, result.get(), x.get(),
                                             nullptr, /*ctx=*/nullptr)) {
      return false;
    }

    // Encode the x-coordinate left-padded with zeros.
    Array<uint8_t> secret;
    if (!secret.Init((EC_GROUP_get_degree(group_) + 7) / 8) ||
        !BN_bn2bin_padded(secret.data(), secret.size(), x.get())) {
      return false;
    }

    *out_secret = std::move(secret);
    return true;
  }

  bool SerializePrivateKey(CBB *out) override {
    assert(group_);
    assert(private_key_);
    // Padding is added to avoid leaking the length.
    size_t len = BN_num_bytes(EC_GROUP_get0_order(group_));
    return BN_bn2cbb_padded(out, len, private_key_.get());
  }

  bool DeserializePrivateKey(CBS *in) override {
    assert(!private_key_);
    private_key_.reset(BN_bin2bn(CBS_data(in), CBS_len(in), nullptr));
    return private_key_ != nullptr;
  }

 private:
  UniquePtr<BIGNUM> private_key_;
  const EC_GROUP *const group_ = nullptr;
  uint16_t group_id_;
};

class X25519KeyShare : public SSLKeyShare {
 public:
  X25519KeyShare() {}

  uint16_t GroupID() const override { return SSL_GROUP_X25519; }

  bool Offer(CBB *out) override {
    uint8_t public_key[32];
    X25519_keypair(public_key, private_key_);
    return !!CBB_add_bytes(out, public_key, sizeof(public_key));
  }

  bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert,
              Span<const uint8_t> peer_key) override {
    *out_alert = SSL_AD_INTERNAL_ERROR;

    Array<uint8_t> secret;
    if (!secret.Init(32)) {
      return false;
    }

    if (peer_key.size() != 32 ||
        !X25519(secret.data(), private_key_, peer_key.data())) {
      *out_alert = SSL_AD_DECODE_ERROR;
      OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECPOINT);
      return false;
    }

    *out_secret = std::move(secret);
    return true;
  }

  bool SerializePrivateKey(CBB *out) override {
    return CBB_add_bytes(out, private_key_, sizeof(private_key_));
  }

  bool DeserializePrivateKey(CBS *in) override {
    if (CBS_len(in) != sizeof(private_key_) ||
        !CBS_copy_bytes(in, private_key_, sizeof(private_key_))) {
      return false;
    }
    return true;
  }

 private:
  uint8_t private_key_[32];
};

class KEMKeyShare : public SSLKeyShare {
 public:
  KEMKeyShare(int nid, uint16_t group_id) : nid_(nid), group_id_(group_id) {}

  uint16_t GroupID() const override { return group_id_; }

  bool Offer(CBB *out) override {
    // Ensure |out| is valid, has no children, and has been initialized.
    // We check that |out| has no children because otherwise CBB_data()
    // will produce a fatal error by way of assert(cbb->child == NULL).
    if (!out || out->child || !CBB_data(out)) {
      OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
      return false;
    }

    // ctx_ should not have been initialized (by e.g. a previous call
    // to Offer()).
    if (ctx_) {
      OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
      return false;
    }

    // Initialize the KEM ctx
    ctx_.reset(EVP_PKEY_CTX_new_id(EVP_PKEY_KEM, nullptr));
    if (!ctx_ || !EVP_PKEY_CTX_kem_set_params(ctx_.get(), nid_)) {
      OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
      return false;
    }

    // Generate a KEM keypair and retrieve the length of the public key
    size_t public_key_len = 0;
    EVP_PKEY *raw_key = nullptr;
    if (!EVP_PKEY_keygen_init(ctx_.get()) ||
        !EVP_PKEY_keygen(ctx_.get(), &raw_key) ||
        !raw_key ||
        !EVP_PKEY_get_raw_public_key(raw_key, nullptr, &public_key_len)) {
      EVP_PKEY_free(raw_key);
      OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
      return false;
    }

    // Retain the secret key for decapsulation
    UniquePtr<EVP_PKEY> pkey(raw_key);
    ctx_.reset(EVP_PKEY_CTX_new(pkey.get(), nullptr));
    if (!ctx_) {
      OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
      return false;
    }

    uint8_t *public_key = NULL;
    size_t public_key_bytes_written = public_key_len;
    if (!CBB_add_space(out, &public_key, public_key_len) ||
        !EVP_PKEY_get_raw_public_key(pkey.get(), public_key,
                                     &public_key_bytes_written) ||
                                     public_key_bytes_written != public_key_len) {
      OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
      return false;
    }

    return true;
  }

  // Because this is a KEM key share, |out_public_key| will be the ciphertext
  // resulting from the encapsulation of the shared secret under the
  // received public key, |peek_key|.
  bool Accept(CBB *out_public_key, Array<uint8_t> *out_secret,
              uint8_t *out_alert, Span<const uint8_t> peer_key) override {
    // Basic nullptr checks
    if (!out_alert) {
      OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
      return false;
    }

    // Set alert to internal error by default
    *out_alert = SSL_AD_INTERNAL_ERROR;

    if (!out_secret) {
      OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
      return false;
    }

    // Ensure |out_public_key| is valid, has no children, and has been
    // initialized. We check that |out_public_key| has no children because
    // otherwise CBB_data() will produce a fatal error by way of
    // assert(cbb->child == NULL).
    if (!out_public_key || out_public_key->child || !CBB_data(out_public_key)) {
      OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
      return false;
    }

    // ctx_ should not have been initialized (by e.g. a call to Offer()).
    if (ctx_) {
      OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
      return false;
    }

    // Ensure that peer_key is valid
    if (!peer_key.data() || peer_key.empty()) {
      *out_alert = SSL_AD_DECODE_ERROR;
      OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
      return false;
    }

    // Initialize pkey from the received public key
    UniquePtr<EVP_PKEY> pkey(
      EVP_PKEY_kem_new_raw_public_key(nid_, peer_key.begin(), peer_key.size()));

    if (!pkey) {
      *out_alert = SSL_AD_DECODE_ERROR;
      OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
      return false;
    }

    // Initialize the context with the pkey
    ctx_.reset(EVP_PKEY_CTX_new(pkey.get(), nullptr));
    if (!ctx_) {
      OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
      return false;
    }

    // Retrieve the lengths of the ciphertext and shared secret
    size_t ciphertext_len = 0;
    size_t secret_len = 0;
    if (!EVP_PKEY_encapsulate(ctx_.get(), nullptr, &ciphertext_len,
                              nullptr, &secret_len)) {
      OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
      return false;
    }

    // EVP_PKEY_encapsulate() will generate a shared secret, then encapsulate it
    // with the peer's public key. We send the resultant ciphertext to the
    // peer by writing it to |out_public_key|, then...
    Array<uint8_t> shared_secret;
    if (!shared_secret.Init(secret_len)) {
      OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
      return false;
    }

    uint8_t *ciphertext = NULL;
    size_t ciphertext_bytes_written = ciphertext_len;
    size_t secret_bytes_written = secret_len;

    if (!CBB_add_space(out_public_key, &ciphertext, ciphertext_len) ||
        !EVP_PKEY_encapsulate(ctx_.get(), ciphertext,
                              &ciphertext_bytes_written, shared_secret.data(),
                              &secret_bytes_written) ||
                              ciphertext_bytes_written != ciphertext_len ||
                              secret_bytes_written != secret_len) {
      OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
      return false;
    }

    // ...we retain the shared secret for our own use.
    *out_secret = std::move(shared_secret);

    // Success; clear the alert
    *out_alert = 0;
    return true;
  }

  // Because this is a KEM key share, |peer_key| is actually the ciphertext
  // resulting from the peer encapsulating the shared secret under our public key.
  // In Finish(), we use our previously generated secret key to decrypt
  // that ciphertext and obtain the shared secret.
  bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert,
              Span<const uint8_t> peer_key) override {
    // Basic nullptr checks
    if (!out_alert) {
      OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
      return false;
    }

    // Set alert to internal error by default
    *out_alert = SSL_AD_INTERNAL_ERROR;

    if (!out_secret) {
      OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
      return false;
    }

    // ctx_ should have been initialized by a previous call to Offer()
    if (!ctx_) {
      OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
      return false;
    }

    // Retrieve the length of the shared secret
    size_t secret_len = 0;
    if (!EVP_PKEY_decapsulate(ctx_.get(), nullptr, &secret_len, nullptr, peer_key.size())) {
      OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
      return false;
    }

    // |peer_key| is the ciphertext, encapsulating the shared secret,
    // that the peer generated using our public key. We use our secret
    // key to decapsulate it, and retain the shared secret by writing
    // it to |out_secret|.
    uint8_t *ciphertext = (uint8_t *)peer_key.begin();
    Array<uint8_t> shared_secret;
    if (!shared_secret.Init(secret_len)) {
      OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
      return false;
    }

    size_t secret_bytes_written = secret_len;
    if (!EVP_PKEY_decapsulate(ctx_.get(), shared_secret.data(),
                              &secret_bytes_written, ciphertext,
                              peer_key.size()) ||
                              secret_bytes_written != secret_len) {
      OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_KEM_CIPHERTEXT);
      return false;
    }

    *out_secret = std::move(shared_secret);

    // Success; clear the alert
    *out_alert = 0;
    return true;
  }

 private:
  int nid_;
  uint16_t group_id_;
  UniquePtr<EVP_PKEY_CTX> ctx_;
};

// A HybridKeyShare consists of key shares from two or more component groups,
// all of which are used to generate a hybrid shared secret.
// See https://datatracker.ietf.org/doc/html/draft-ietf-tls-hybrid-design.
class HybridKeyShare : public SSLKeyShare {
  public:
    HybridKeyShare(uint16_t group_id) : group_id_(group_id),
    exchange_performed(false), hybrid_group_(nullptr) {
      for (const HybridGroup &hybrid_group : HybridGroups()) {
         if (group_id_ == hybrid_group.group_id) {
           hybrid_group_ = &hybrid_group;
           for (size_t i = 0; i < NUM_HYBRID_COMPONENTS; i++) {
             key_shares_[i] = SSLKeyShare::Create(hybrid_group.component_group_ids[i]);
           }
           return;
         }
      }
      hybrid_group_ = nullptr;
    }

    uint16_t GroupID() const override { return group_id_; }

    bool Offer(CBB *out) override {
      // Ensure |out| is valid, has no children, and has been initialized.
      // We check that |out| has no children because otherwise CBB_data()
      // will produce a fatal error by way of assert(cbb->child == NULL);
      if (!out || out->child || !CBB_data(out)) {
        OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
        return false;
      }

      if (!hybrid_group_ || this->exchange_performed) {
        OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
        return false;
      }

      // Iterate through the component groups and Offer() each of their key
      // shares. If one of the calls to a component Offer() fails,
      // OPENSSL_PUT_ERROR will be set appropriately in that component.
      for (const UniquePtr<SSLKeyShare> &key_share : key_shares_) {
        if (!key_share || !key_share->Offer(out)) {
          return false;
        }
      }
      this->exchange_performed = true;
      return true;
    }

    bool Accept(CBB *out_public_key, Array<uint8_t> *out_secret,
                uint8_t *out_alert, Span<const uint8_t> peer_key) override {
      if (!out_alert) {
        OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
        return false;
      }

      // Set alert to internal error by default
      *out_alert = SSL_AD_INTERNAL_ERROR;

      if (!out_secret|| !peer_key.data()) {
        OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
        return false;
      }

      // Ensure |out_public_key| is valid, has no children, and has been
      // initialized. We check that |out_public_key| has no children because
      // otherwise CBB_data() will produce a fatal error by way of
      // assert(cbb->child == NULL);
      if (!out_public_key || out_public_key->child || !CBB_data(out_public_key)) {
        OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
        return false;
      }

      if (!hybrid_group_ || this->exchange_performed) {
        OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
        return false;
      }

      // A hybrid shared secret with two components should be 64 bytes.
      // If it happens to be larger, the CBB will grow accordingly.
      CBB hybrid_shared_secret;
      if (!CBB_init(&hybrid_shared_secret, 64)) {
        OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
        return false;
      }

      // Accept() each component key share. Each component's Accept() function
      // will generate a shared secret and a public key to be sent back to
      // the peer. The hybrid public key is the concatenation of all component
      // public keys; the hybrid shared secret is the concatenation of all
      // component shared secrets.
      size_t peer_key_read_index = 0;
      for (size_t i = 0; i < NUM_HYBRID_COMPONENTS; i++) {
        size_t component_key_size = 0;
        if (!get_component_offer_key_share_size(&component_key_size, hybrid_group_->component_group_ids[i])) {
          OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
          return false;
        }

        // Verify that |peer_key| contains enough data
        if (peer_key_read_index + component_key_size > peer_key.size()) {
          CBB_cleanup(&hybrid_shared_secret);
          *out_alert = SSL_AD_DECODE_ERROR;
          OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HYBRID_KEYSHARE);
          return false;
        }

        Span<const uint8_t> component_key =
          peer_key.subspan(peer_key_read_index, component_key_size);

        Array<uint8_t> component_secret;
        if (!key_shares_[i] ||
            !key_shares_[i]->Accept(out_public_key, &component_secret, out_alert, component_key) ||
            !CBB_add_bytes(&hybrid_shared_secret, component_secret.data(), component_secret.size())) {
          CBB_cleanup(&hybrid_shared_secret);
          OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
          return false;
        }

        peer_key_read_index += component_key_size;
      }

      // Final validation that |peer_key| was the correct size
      if (peer_key_read_index != peer_key.size()) {
        CBB_cleanup(&hybrid_shared_secret);
        *out_alert = SSL_AD_DECODE_ERROR;
        OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
        return false;
      }

      // Retain the hybrid shared secret for our use.
      if (!CBBFinishArray(&hybrid_shared_secret, out_secret)) {
        CBB_cleanup(&hybrid_shared_secret);
        OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
        return false;
      }

      // Success; clear the alert
      *out_alert = 0;

      this->exchange_performed = true;
      return true;
    }

    bool Finish(Array<uint8_t> *out_secret, uint8_t *out_alert,
                Span<const uint8_t> peer_key) override {
      if (!out_alert) {
        OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
        return false;
      }

      // Set alert to internal error by default
      *out_alert = SSL_AD_INTERNAL_ERROR;

      if (!out_secret || !peer_key.data()) {
        OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
        return false;
      }

      if (!hybrid_group_ || !this->exchange_performed) {
        OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
        return false;
      }

      // A hybrid shared secret with two components should be 64 bytes.
      // If it happens to be larger, the CBB will grow accordingly.
      CBB hybrid_shared_secret;
      if (!CBB_init(&hybrid_shared_secret, 64)) {
        OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
        return false;
      }

      // Finish() each component key share. Each component's Finish() function
      // will generate a shared secret. The hybrid shared secret is the
      // concatenation of all component shared secrets.
      size_t peer_key_index = 0;
      for (size_t i = 0; i < NUM_HYBRID_COMPONENTS; i++) {

        size_t component_key_size = 0;
        if (!get_component_accept_key_share_size(&component_key_size, hybrid_group_->component_group_ids[i])) {
          OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
          return false;
        }

        // Verify that |peer_key| contains enough data
        if (peer_key_index + component_key_size > peer_key.size()) {
          CBB_cleanup(&hybrid_shared_secret);
          *out_alert = SSL_AD_DECODE_ERROR;
          OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
          return false;
        }

        Span<const uint8_t> component_key =
          peer_key.subspan(peer_key_index, component_key_size);

        Array<uint8_t> component_secret;
        if (!key_shares_[i] ||
            !key_shares_[i]->Finish(&component_secret, out_alert, component_key) ||
            !CBB_add_bytes(&hybrid_shared_secret, component_secret.data(), component_secret.size())) {
          CBB_cleanup(&hybrid_shared_secret);
          OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
          return false;
        }

        peer_key_index += component_key_size;
      }

      // Final validation that |peer_key| was the correct size
      if (peer_key_index != peer_key.size()) {
        CBB_cleanup(&hybrid_shared_secret);
        *out_alert = SSL_AD_DECODE_ERROR;
        OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
        return false;
      }

      // Retain the hybrid shared secret for our use.
      if (!CBBFinishArray(&hybrid_shared_secret, out_secret)) {
        CBB_cleanup(&hybrid_shared_secret);
        OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
        return false;
      }

      // Success; clear the alert
      *out_alert = 0;
      return true;
    }

  private:
   // Only need to support SSL Groups that are a component of a supported HybridKeyShare
   bool get_component_offer_key_share_size(size_t *out, uint16_t component_group_id) {
     switch (component_group_id) {
     case SSL_GROUP_SECP256R1:
       *out = 1 + (2 * EC_P256R1_FIELD_ELEM_BYTES);
       return true;
     case SSL_GROUP_KYBER768_R3:
       *out = KYBER768_R3_PUBLIC_KEY_BYTES;
       return true;
     case SSL_GROUP_MLKEM768:
       *out = MLKEM768_PUBLIC_KEY_BYTES;
       return true;
     case SSL_GROUP_X25519:
       *out = 32;
       return true;
     default:
       return false;
     }
   }

   // Only need to support SSL Groups that are a component of a supported HybridKeyShare
   bool get_component_accept_key_share_size(size_t *out, uint16_t component_group_id) {
     switch (component_group_id) {
     case SSL_GROUP_SECP256R1:
       *out = 1 + (2 * EC_P256R1_FIELD_ELEM_BYTES);
       return true;
     case SSL_GROUP_KYBER768_R3:
       *out = KYBER768_R3_CIPHERTEXT_BYTES;
       return true;
     case SSL_GROUP_MLKEM768:
       *out = MLKEM768_CIPHERTEXT_BYTES;
       return true;
     case SSL_GROUP_X25519:
       *out = 32;
       return true;
     default:
       return false;
     }
   }

   uint16_t group_id_;
   bool exchange_performed;
   const HybridGroup *hybrid_group_;
   UniquePtr<SSLKeyShare> key_shares_[NUM_HYBRID_COMPONENTS];
};

CONSTEXPR_ARRAY NamedGroup kNamedGroups[] = {
    {NID_secp224r1, SSL_GROUP_SECP224R1, "P-224", "secp224r1"},
    {NID_X9_62_prime256v1, SSL_GROUP_SECP256R1, "P-256", "prime256v1"},
    {NID_secp384r1, SSL_GROUP_SECP384R1, "P-384", "secp384r1"},
    {NID_secp521r1, SSL_GROUP_SECP521R1, "P-521", "secp521r1"},
    {NID_X25519, SSL_GROUP_X25519, "X25519", "x25519"},
    {NID_SecP256r1Kyber768Draft00, SSL_GROUP_SECP256R1_KYBER768_DRAFT00, "SecP256r1Kyber768Draft00", ""},
    {NID_X25519Kyber768Draft00, SSL_GROUP_X25519_KYBER768_DRAFT00, "X25519Kyber768Draft00", ""},
    {NID_SecP256r1MLKEM768, SSL_GROUP_SECP256R1_MLKEM768, "SecP256r1MLKEM768", ""},
    {NID_X25519MLKEM768, SSL_GROUP_X25519_MLKEM768, "X25519MLKEM768", ""},
};

CONSTEXPR_ARRAY uint16_t kPQGroups[] = {
    SSL_GROUP_KYBER512_R3,
    SSL_GROUP_KYBER768_R3,
    SSL_GROUP_KYBER1024_R3,
    SSL_GROUP_MLKEM768,
    SSL_GROUP_MLKEM1024,
    SSL_GROUP_SECP256R1_KYBER768_DRAFT00,
    SSL_GROUP_X25519_KYBER768_DRAFT00,
    SSL_GROUP_SECP256R1_MLKEM768,
    SSL_GROUP_X25519_MLKEM768
};

CONSTEXPR_ARRAY HybridGroup kHybridGroups[] = {
  {
    SSL_GROUP_SECP256R1_KYBER768_DRAFT00,  // group_id
    {
      SSL_GROUP_SECP256R1,            // component_group_ids[0]
      SSL_GROUP_KYBER768_R3,          // component_group_ids[1]
    },
  },
  {
    SSL_GROUP_X25519_KYBER768_DRAFT00,     // group_id
    {
      SSL_GROUP_X25519,               // component_group_ids[0]
      SSL_GROUP_KYBER768_R3,          // component_group_ids[1]
    },
  },

  {
    SSL_GROUP_SECP256R1_MLKEM768,     // group_id
    {
      SSL_GROUP_SECP256R1,         // component_group_ids[0]
      SSL_GROUP_MLKEM768,          // component_group_ids[1]
    },
  },

  {
    SSL_GROUP_X25519_MLKEM768,     // group_id
    {
      // Note: MLKEM768 is sent first due to FIPS requirements.
      // For more details, see https://datatracker.ietf.org/doc/html/draft-kwiatkowski-tls-ecdhe-mlkem.html#section-3
      SSL_GROUP_MLKEM768,          // component_group_ids[0]
      SSL_GROUP_X25519,            // component_group_ids[1]
    },
  }
};

} // namespace

Span<const NamedGroup> NamedGroups() {
  return MakeConstSpan(kNamedGroups, OPENSSL_ARRAY_SIZE(kNamedGroups));
}

Span<const HybridGroup> HybridGroups() {
  return MakeConstSpan(kHybridGroups, OPENSSL_ARRAY_SIZE(kHybridGroups));
}

Span<const uint16_t> PQGroups() {
  return MakeConstSpan(kPQGroups, OPENSSL_ARRAY_SIZE(kPQGroups));
}

UniquePtr<SSLKeyShare> SSLKeyShare::Create(uint16_t group_id) {
  switch (group_id) {
    case SSL_GROUP_SECP224R1:
      return MakeUnique<ECKeyShare>(EC_group_p224(), SSL_GROUP_SECP224R1);
    case SSL_GROUP_SECP256R1:
      return MakeUnique<ECKeyShare>(EC_group_p256(), SSL_GROUP_SECP256R1);
    case SSL_GROUP_SECP384R1:
      return MakeUnique<ECKeyShare>(EC_group_p384(), SSL_GROUP_SECP384R1);
    case SSL_GROUP_SECP521R1:
      return MakeUnique<ECKeyShare>(EC_group_p521(), SSL_GROUP_SECP521R1);
    case SSL_GROUP_X25519:
      return MakeUnique<X25519KeyShare>();
    case SSL_GROUP_KYBER768_R3:
      // Kyber, as a standalone group, is not a NamedGroup; however, we
      // need to create Kyber key shares as part of hybrid groups.
      return MakeUnique<KEMKeyShare>(NID_KYBER768_R3, SSL_GROUP_KYBER768_R3);
    case SSL_GROUP_SECP256R1_KYBER768_DRAFT00:
      return MakeUnique<HybridKeyShare>(SSL_GROUP_SECP256R1_KYBER768_DRAFT00);
    case SSL_GROUP_X25519_KYBER768_DRAFT00:
      return MakeUnique<HybridKeyShare>(SSL_GROUP_X25519_KYBER768_DRAFT00);
    case SSL_GROUP_MLKEM768:
      // MLKEM768, as a standalone group, is not a NamedGroup; however, we
      // need to create MLKEM768 key shares as part of hybrid groups.
      return MakeUnique<KEMKeyShare>(NID_MLKEM768, SSL_GROUP_MLKEM768);
    case SSL_GROUP_SECP256R1_MLKEM768:
      return MakeUnique<HybridKeyShare>(SSL_GROUP_SECP256R1_MLKEM768);
    case SSL_GROUP_X25519_MLKEM768:
      return MakeUnique<HybridKeyShare>(SSL_GROUP_X25519_MLKEM768);
    default:
      return nullptr;
  }
}

bool SSLKeyShare::Accept(CBB *out_public_key, Array<uint8_t> *out_secret,
                         uint8_t *out_alert, Span<const uint8_t> peer_key) {
  *out_alert = SSL_AD_INTERNAL_ERROR;
  return Offer(out_public_key) &&
         Finish(out_secret, out_alert, peer_key);
}

bool ssl_nid_to_group_id(uint16_t *out_group_id, int nid) {
  for (const auto &group : kNamedGroups) {
    if (group.nid == nid) {
      *out_group_id = group.group_id;
      return true;
    }
  }
  return false;
}

bool ssl_name_to_group_id(uint16_t *out_group_id, const char *name, size_t len) {
  for (const auto &group : kNamedGroups) {
    if (len == strlen(group.name) &&
        !strncmp(group.name, name, len)) {
      *out_group_id = group.group_id;
      return true;
    }
    if ((strlen(group.alias) > 0) && len == strlen(group.alias) &&
        !strncmp(group.alias, name, len)) {
      *out_group_id = group.group_id;
      return true;
    }
  }
  return false;
}

BSSL_NAMESPACE_END

using namespace bssl;

const char* SSL_get_group_name(uint16_t group_id) {
  for (const auto &group : kNamedGroups) {
    if (group.group_id == group_id) {
      return group.name;
    }
  }
  return nullptr;
}

size_t SSL_get_all_group_names(const char **out, size_t max_out) {
  return GetAllNames(out, max_out, Span<const char *>(), &NamedGroup::name,
                     MakeConstSpan(kNamedGroups));
}