hpke.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2020 Cloudflare, Inc. All rights reserved. Use of this source code
  2. // is governed by a BSD-style license that can be found in the LICENSE file.
  3. package tls
  4. import (
  5. "errors"
  6. "fmt"
  7. "github.com/cloudflare/circl/hpke"
  8. )
  9. // The mandatory-to-implement HPKE cipher suite for use with the ECH extension.
  10. var defaultHPKESuite hpke.Suite
  11. func init() {
  12. var err error
  13. defaultHPKESuite, err = hpkeAssembleSuite(
  14. uint16(hpke.KEM_X25519_HKDF_SHA256),
  15. uint16(hpke.KDF_HKDF_SHA256),
  16. uint16(hpke.AEAD_AES128GCM),
  17. )
  18. if err != nil {
  19. panic(fmt.Sprintf("hpke: mandatory-to-implement cipher suite not supported: %s", err))
  20. }
  21. }
  22. func hpkeAssembleSuite(kemId, kdfId, aeadId uint16) (hpke.Suite, error) {
  23. kem := hpke.KEM(kemId)
  24. if !kem.IsValid() {
  25. return hpke.Suite{}, errors.New("KEM is not supported")
  26. }
  27. kdf := hpke.KDF(kdfId)
  28. if !kdf.IsValid() {
  29. return hpke.Suite{}, errors.New("KDF is not supported")
  30. }
  31. aead := hpke.AEAD(aeadId)
  32. if !aead.IsValid() {
  33. return hpke.Suite{}, errors.New("AEAD is not supported")
  34. }
  35. return hpke.NewSuite(kem, kdf, aead), nil
  36. }