Skip to content

Commit

Permalink
xwing: HPKE integration
Browse files Browse the repository at this point in the history
  • Loading branch information
bwesterb committed Oct 20, 2024
1 parent f40f138 commit 126d471
Showing 1 changed file with 77 additions and 0 deletions.
77 changes: 77 additions & 0 deletions hpke/otherkem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package hpke

// Shim to use generic KEM (kem.Scheme) as HPKE KEM.

import (
"github.com/cloudflare/circl/internal/sha3"
"github.com/cloudflare/circl/kem"
)

type otherKEM struct {

Check failure on line 10 in hpke/otherkem.go

View workflow job for this annotation

GitHub Actions / Go-1.22/amd64

type `otherKEM` is unused (unused)
kem kem.Scheme
name string
}

func (h otherKEM) PrivateKeySize() int { return h.kem.PrivateKeySize() }

Check failure on line 15 in hpke/otherkem.go

View workflow job for this annotation

GitHub Actions / Go-1.22/amd64

func `otherKEM.PrivateKeySize` is unused (unused)
func (h otherKEM) SeedSize() int { return h.kem.SeedSize() }

Check failure on line 16 in hpke/otherkem.go

View workflow job for this annotation

GitHub Actions / Go-1.22/amd64

func `otherKEM.SeedSize` is unused (unused)
func (h otherKEM) CiphertextSize() int { return h.kem.CiphertextSize() }

Check failure on line 17 in hpke/otherkem.go

View workflow job for this annotation

GitHub Actions / Go-1.22/amd64

func `otherKEM.CiphertextSize` is unused (unused)
func (h otherKEM) PublicKeySize() int { return h.kem.PublicKeySize() }

Check failure on line 18 in hpke/otherkem.go

View workflow job for this annotation

GitHub Actions / Go-1.22/amd64

func `otherKEM.PublicKeySize` is unused (unused)
func (h otherKEM) EncapsulationSeedSize() int { return h.kem.EncapsulationSeedSize() }

Check failure on line 19 in hpke/otherkem.go

View workflow job for this annotation

GitHub Actions / Go-1.22/amd64

func `otherKEM.EncapsulationSeedSize` is unused (unused)
func (h otherKEM) SharedKeySize() int { return h.kem.SharedKeySize() }

Check failure on line 20 in hpke/otherkem.go

View workflow job for this annotation

GitHub Actions / Go-1.22/amd64

func `otherKEM.SharedKeySize` is unused (unused)
func (h otherKEM) Name() string { return h.name }

Check failure on line 21 in hpke/otherkem.go

View workflow job for this annotation

GitHub Actions / Go-1.22/amd64

func `otherKEM.Name` is unused (unused)

func (h otherKEM) AuthDecapsulate(skR kem.PrivateKey,

Check failure on line 23 in hpke/otherkem.go

View workflow job for this annotation

GitHub Actions / Go-1.22/amd64

func `otherKEM.AuthDecapsulate` is unused (unused)
ct []byte,
pkS kem.PublicKey,
) ([]byte, error) {
panic("AuthDecapsulate is not supported for this KEM")
}

func (h otherKEM) AuthEncapsulate(pkr kem.PublicKey, sks kem.PrivateKey) (

Check failure on line 30 in hpke/otherkem.go

View workflow job for this annotation

GitHub Actions / Go-1.22/amd64

func `otherKEM.AuthEncapsulate` is unused (unused)
ct []byte, ss []byte, err error,
) {
panic("AuthEncapsulate is not supported for this KEM")
}

func (h otherKEM) AuthEncapsulateDeterministically(pkr kem.PublicKey, sks kem.PrivateKey, seed []byte) (ct, ss []byte, err error) {
panic("AuthEncapsulateDeterministically is not supported for this KEM")
}

func (h otherKEM) Encapsulate(pkr kem.PublicKey) (
ct []byte, ss []byte, err error,
) {
return h.kem.Encapsulate(pkr)
}

func (h otherKEM) Decapsulate(skr kem.PrivateKey, ct []byte) ([]byte, error) {
return h.kem.Decapsulate(skr, ct)
}

func (h otherKEM) EncapsulateDeterministically(
pkr kem.PublicKey, seed []byte,
) (ct, ss []byte, err error) {
return h.kem.EncapsulateDeterministically(pkr, seed)
}

// HPKE requires DeriveKeyPair() to take any seed larger than the private key
// size, whereas typical KEMs expect a specific seed size. We'll just use
// SHAKE256 to hash it to the right size as in X-Wing.
func (h otherKEM) DeriveKeyPair(seed []byte) (kem.PublicKey, kem.PrivateKey) {
seed2 := make([]byte, h.kem.SeedSize())
hh := sha3.NewShake256()
_, _ = hh.Write(seed)
_, _ = hh.Read(seed2)
return h.kem.DeriveKeyPair(seed2)
}

func (h otherKEM) GenerateKeyPair() (kem.PublicKey, kem.PrivateKey, error) {
return h.kem.GenerateKeyPair()
}

func (h otherKEM) UnmarshalBinaryPrivateKey(data []byte) (kem.PrivateKey, error) {
return h.kem.UnmarshalBinaryPrivateKey(data)
}

func (h otherKEM) UnmarshalBinaryPublicKey(data []byte) (kem.PublicKey, error) {
return h.kem.UnmarshalBinaryPublicKey(data)
}

0 comments on commit 126d471

Please sign in to comment.