-
Notifications
You must be signed in to change notification settings - Fork 146
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
X-Wing PQ/T hybrid #471
Open
bwesterb
wants to merge
6
commits into
main
Choose a base branch
from
bas/xwing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
X-Wing PQ/T hybrid #471
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ff932be
[WIP] X-Wing PQ/T hybrid
bwesterb e7a3c44
x-wing: remove stretching
bwesterb 2b4220b
xwing: include X25519 public key in X-Wing private key
bwesterb c1c1704
xwing: align with draft 04
bwesterb f40f138
xwing: final version (-05)
bwesterb 5a648d6
xwing: HPKE integration
bwesterb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
kem kem.Scheme | ||
name string | ||
} | ||
|
||
func (h otherKEM) PrivateKeySize() int { return h.kem.PrivateKeySize() } | ||
func (h otherKEM) SeedSize() int { return h.kem.SeedSize() } | ||
func (h otherKEM) CiphertextSize() int { return h.kem.CiphertextSize() } | ||
func (h otherKEM) PublicKeySize() int { return h.kem.PublicKeySize() } | ||
func (h otherKEM) EncapsulationSeedSize() int { return h.kem.EncapsulationSeedSize() } | ||
func (h otherKEM) SharedKeySize() int { return h.kem.SharedKeySize() } | ||
func (h otherKEM) Name() string { return h.name } | ||
|
||
func (h otherKEM) AuthDecapsulate(skR kem.PrivateKey, | ||
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) ( | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,4 +160,5 @@ func Example_schemes() { | |
// Kyber1024-X448 | ||
// P256Kyber768Draft00 | ||
// X25519MLKEM768 | ||
// X-Wing | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
package xwing | ||
|
||
import ( | ||
"bytes" | ||
cryptoRand "crypto/rand" | ||
"crypto/subtle" | ||
|
||
"github.com/cloudflare/circl/kem" | ||
"github.com/cloudflare/circl/kem/mlkem/mlkem768" | ||
) | ||
|
||
// This file contains the boilerplate code to connect X-Wing to the | ||
// generic KEM API. | ||
|
||
// Returns the generic KEM interface for X-Wing PQ/T hybrid KEM. | ||
func Scheme() kem.Scheme { return &xwing } | ||
|
||
type scheme struct{} | ||
|
||
var xwing scheme | ||
|
||
func (*scheme) Name() string { return "X-Wing" } | ||
func (*scheme) PublicKeySize() int { return PublicKeySize } | ||
func (*scheme) PrivateKeySize() int { return PrivateKeySize } | ||
func (*scheme) SeedSize() int { return SeedSize } | ||
func (*scheme) EncapsulationSeedSize() int { return EncapsulationSeedSize } | ||
func (*scheme) SharedKeySize() int { return SharedKeySize } | ||
func (*scheme) CiphertextSize() int { return CiphertextSize } | ||
func (*PrivateKey) Scheme() kem.Scheme { return &xwing } | ||
func (*PublicKey) Scheme() kem.Scheme { return &xwing } | ||
|
||
func (sch *scheme) Encapsulate(pk kem.PublicKey) (ct, ss []byte, err error) { | ||
var seed [EncapsulationSeedSize]byte | ||
_, err = cryptoRand.Read(seed[:]) | ||
if err != nil { | ||
return | ||
} | ||
return sch.EncapsulateDeterministically(pk, seed[:]) | ||
} | ||
|
||
func (sch *scheme) EncapsulateDeterministically( | ||
pk kem.PublicKey, seed []byte, | ||
) ([]byte, []byte, error) { | ||
if len(seed) != EncapsulationSeedSize { | ||
return nil, nil, kem.ErrSeedSize | ||
} | ||
pub, ok := pk.(*PublicKey) | ||
if !ok { | ||
return nil, nil, kem.ErrTypeMismatch | ||
} | ||
var ( | ||
ct [CiphertextSize]byte | ||
ss [SharedKeySize]byte | ||
) | ||
pub.EncapsulateTo(ct[:], ss[:], seed) | ||
return ct[:], ss[:], nil | ||
} | ||
|
||
func (*scheme) UnmarshalBinaryPublicKey(buf []byte) (kem.PublicKey, error) { | ||
var pk PublicKey | ||
if len(buf) != PublicKeySize { | ||
return nil, kem.ErrPubKeySize | ||
} | ||
|
||
if err := pk.Unpack(buf); err != nil { | ||
return nil, err | ||
} | ||
return &pk, nil | ||
} | ||
|
||
func (*scheme) UnmarshalBinaryPrivateKey(buf []byte) (kem.PrivateKey, error) { | ||
var sk PrivateKey | ||
if len(buf) != PrivateKeySize { | ||
return nil, kem.ErrPrivKeySize | ||
} | ||
|
||
sk.Unpack(buf) | ||
return &sk, nil | ||
} | ||
|
||
func (sk *PrivateKey) MarshalBinary() ([]byte, error) { | ||
var ret [PrivateKeySize]byte | ||
sk.Pack(ret[:]) | ||
return ret[:], nil | ||
} | ||
|
||
func (sk *PrivateKey) Equal(other kem.PrivateKey) bool { | ||
oth, ok := other.(*PrivateKey) | ||
if !ok { | ||
return false | ||
} | ||
return sk.m.Equal(&oth.m) && | ||
subtle.ConstantTimeCompare(oth.x[:], sk.x[:]) == 1 | ||
} | ||
|
||
func (sk *PrivateKey) Public() kem.PublicKey { | ||
var pk PublicKey | ||
pk.m = *(sk.m.Public().(*mlkem768.PublicKey)) | ||
pk.x = sk.xpk | ||
return &pk | ||
} | ||
|
||
func (pk *PublicKey) Equal(other kem.PublicKey) bool { | ||
oth, ok := other.(*PublicKey) | ||
if !ok { | ||
return false | ||
} | ||
return pk.m.Equal(&oth.m) && bytes.Equal(pk.x[:], oth.x[:]) | ||
} | ||
|
||
func (pk *PublicKey) MarshalBinary() ([]byte, error) { | ||
var ret [PublicKeySize]byte | ||
pk.Pack(ret[:]) | ||
return ret[:], nil | ||
} | ||
|
||
func (*scheme) DeriveKeyPair(seed []byte) (kem.PublicKey, kem.PrivateKey) { | ||
sk, pk := DeriveKeyPair(seed) | ||
return pk, sk | ||
} | ||
|
||
func (sch *scheme) GenerateKeyPair() (kem.PublicKey, kem.PrivateKey, error) { | ||
sk, pk, err := GenerateKeyPair(nil) | ||
return pk, sk, err | ||
} | ||
|
||
func (*scheme) Decapsulate(sk kem.PrivateKey, ct []byte) ([]byte, error) { | ||
if len(ct) != CiphertextSize { | ||
return nil, kem.ErrCiphertextSize | ||
} | ||
|
||
var ss [SharedKeySize]byte | ||
|
||
priv, ok := sk.(*PrivateKey) | ||
if !ok { | ||
return nil, kem.ErrTypeMismatch | ||
} | ||
|
||
priv.DecapsulateTo(ss[:], ct[:]) | ||
|
||
return ss[:], nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.