u_hpke.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package tls
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/cloudflare/circl/hpke"
  6. "github.com/cloudflare/circl/kem"
  7. )
  8. type HPKERawPublicKey = []byte
  9. type HPKE_KEM_ID = uint16 // RFC 9180
  10. type HPKE_KDF_ID = uint16 // RFC 9180
  11. type HPKE_AEAD_ID = uint16 // RFC 9180
  12. type HPKESymmetricCipherSuite struct {
  13. KdfId HPKE_KDF_ID
  14. AeadId HPKE_AEAD_ID
  15. }
  16. type HPKEKeyConfig struct {
  17. ConfigId uint8
  18. KemId HPKE_KEM_ID
  19. PublicKey kem.PublicKey
  20. rawPublicKey HPKERawPublicKey
  21. CipherSuites []HPKESymmetricCipherSuite
  22. }
  23. var defaultHPKESuite hpke.Suite
  24. func init() {
  25. var err error
  26. defaultHPKESuite, err = hpkeAssembleSuite(
  27. uint16(hpke.KEM_X25519_HKDF_SHA256),
  28. uint16(hpke.KDF_HKDF_SHA256),
  29. uint16(hpke.AEAD_AES128GCM),
  30. )
  31. if err != nil {
  32. panic(fmt.Sprintf("hpke: mandatory-to-implement cipher suite not supported: %s", err))
  33. }
  34. }
  35. func hpkeAssembleSuite(kemId, kdfId, aeadId uint16) (hpke.Suite, error) {
  36. kem := hpke.KEM(kemId)
  37. if !kem.IsValid() {
  38. return hpke.Suite{}, errors.New("KEM is not supported")
  39. }
  40. kdf := hpke.KDF(kdfId)
  41. if !kdf.IsValid() {
  42. return hpke.Suite{}, errors.New("KDF is not supported")
  43. }
  44. aead := hpke.AEAD(aeadId)
  45. if !aead.IsValid() {
  46. return hpke.Suite{}, errors.New("AEAD is not supported")
  47. }
  48. return hpke.NewSuite(kem, kdf, aead), nil
  49. }
  50. var dummyX25519PublicKey = []byte{
  51. 143, 38, 37, 36, 12, 6, 229, 30, 140, 27, 167, 73, 26, 100, 203, 107, 216,
  52. 81, 163, 222, 52, 211, 54, 210, 46, 37, 78, 216, 157, 97, 241, 244,
  53. }