-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mkem scheme for pigeonhole mixnet protocols
- Loading branch information
Showing
4 changed files
with
179 additions
and
0 deletions.
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,128 @@ | ||
// SPDX-FileCopyrightText: Copyright (C) 2024 David Stainton | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
// Package mkem provides multiparty KEM construction. | ||
package mkem | ||
|
||
import ( | ||
"crypto/cipher" | ||
"crypto/rand" | ||
"errors" | ||
|
||
"github.com/katzenpost/chacha20poly1305" | ||
"github.com/katzenpost/hpqc/hash" | ||
"github.com/katzenpost/hpqc/nike" | ||
) | ||
|
||
type MKEMCiphertext struct { | ||
EphemeralPublicKey *PublicKey | ||
DEKCiphertexts [][]byte | ||
SecretCiphertext []byte | ||
} | ||
|
||
// Scheme is an MKEM scheme. | ||
type Scheme struct { | ||
nike nike.Scheme | ||
} | ||
|
||
// FromNIKE creates a new KEM adapter Scheme | ||
// using the given NIKE Scheme. | ||
func FromNIKE(nike nike.Scheme) *Scheme { | ||
if nike == nil { | ||
panic("NIKE is nil") | ||
} | ||
return &Scheme{ | ||
nike: nike, | ||
} | ||
} | ||
|
||
func (s *Scheme) GenerateKeyPair() (*PublicKey, *PrivateKey, error) { | ||
pubkey, privkey, err := s.nike.GenerateKeyPair() | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
return &PublicKey{ | ||
publicKey: pubkey, | ||
}, &PrivateKey{ | ||
privateKey: privkey, | ||
}, nil | ||
} | ||
|
||
func (s *Scheme) createCipher(key []byte) cipher.AEAD { | ||
aead, err := chacha20poly1305.New(key) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return aead | ||
} | ||
|
||
func (s *Scheme) encrypt(key []byte, plaintext []byte) []byte { | ||
aead := s.createCipher(key) | ||
nonce := make([]byte, aead.NonceSize()) | ||
_, err := rand.Reader.Read(nonce) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return aead.Seal(nonce, nonce, plaintext, nil) | ||
} | ||
|
||
func (s *Scheme) decrypt(key []byte, ciphertext []byte) ([]byte, error) { | ||
aead := s.createCipher(key) | ||
nonce := ciphertext[:aead.NonceSize()] | ||
ciphertext = ciphertext[aead.NonceSize():] | ||
return aead.Open(nil, nonce, ciphertext, nil) | ||
} | ||
|
||
func (s *Scheme) Encapsulate(keys []*PublicKey, sharedSecret []byte) *MKEMCiphertext { | ||
ephPub, ephPriv, err := s.nike.GenerateKeyPair() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
secrets := make([][hash.HashSize]byte, len(keys)) | ||
for i := 0; i < len(keys); i++ { | ||
secrets[i] = hash.Sum256(s.nike.DeriveSecret(ephPriv, keys[i].publicKey)) | ||
} | ||
|
||
msgKey := make([]byte, 32) | ||
_, err = rand.Reader.Read(msgKey) | ||
if err != nil { | ||
panic(err) | ||
} | ||
secretCiphertext := s.encrypt(msgKey, sharedSecret) | ||
|
||
outCiphertexts := make([][]byte, len(secrets)) | ||
for i := 0; i < len(secrets); i++ { | ||
outCiphertexts[i] = s.encrypt(secrets[i][:], msgKey) | ||
} | ||
|
||
return &MKEMCiphertext{ | ||
EphemeralPublicKey: &PublicKey{ | ||
publicKey: ephPub, | ||
}, | ||
DEKCiphertexts: outCiphertexts, | ||
SecretCiphertext: secretCiphertext, | ||
} | ||
} | ||
|
||
func (s *Scheme) Decapsulate(privkey *PrivateKey, c *MKEMCiphertext) ([]byte, error) { | ||
ephSecret := hash.Sum256(s.nike.DeriveSecret(privkey.privateKey, c.EphemeralPublicKey.publicKey)) | ||
for i := 0; i < len(c.DEKCiphertexts); i++ { | ||
msgKey, err := s.decrypt(ephSecret[:], c.DEKCiphertexts[i]) | ||
if err != nil { | ||
continue | ||
} | ||
return s.decrypt(msgKey, c.SecretCiphertext) | ||
} | ||
return nil, errors.New("failed to trial decrypt") | ||
} | ||
|
||
// PrivateKey is an MKEM private key. | ||
type PrivateKey struct { | ||
privateKey nike.PrivateKey | ||
} | ||
|
||
// PublicKey is an MKEM public key. | ||
type PublicKey struct { | ||
publicKey nike.PublicKey | ||
} |
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,41 @@ | ||
// SPDX-FileCopyrightText: Copyright (C) 2024 David Stainton | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
|
||
// Package mkem provides multiparty KEM construction. | ||
package mkem | ||
|
||
import ( | ||
"crypto/rand" | ||
"testing" | ||
|
||
"github.com/katzenpost/hpqc/nike/schemes" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMKEMCorrectness(t *testing.T) { | ||
nikeName := "x25519" | ||
nike := schemes.ByName(nikeName) | ||
s := FromNIKE(nike) | ||
|
||
replica1pub, replica1priv, err := s.GenerateKeyPair() | ||
require.NoError(t, err) | ||
|
||
replica2pub, replica2priv, err := s.GenerateKeyPair() | ||
require.NoError(t, err) | ||
|
||
secret := make([]byte, 32) | ||
_, err = rand.Reader.Read(secret) | ||
require.NoError(t, err) | ||
|
||
ciphertext := s.Encapsulate([]*PublicKey{replica1pub, replica2pub}, secret) | ||
|
||
secret1, err := s.Decapsulate(replica1priv, ciphertext) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, secret, secret1) | ||
|
||
secret2, err := s.Decapsulate(replica2priv, ciphertext) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, secret, secret2) | ||
} |