-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
207 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Package dilithium_eddsa implements hybrid Dilithium + EdDSA encryption, suitable for OpenPGP, experimental. | ||
package dilithium_eddsa | ||
|
||
import ( | ||
"crypto/subtle" | ||
"errors" | ||
"io" | ||
|
||
"github.com/ProtonMail/go-crypto/openpgp/internal/ecc" | ||
dilithium "github.com/kudelskisecurity/crystals-go/crystals-dilithium" | ||
"golang.org/x/crypto/sha3" | ||
) | ||
|
||
type PublicKey struct { | ||
AlgId uint8 | ||
Curve ecc.EdDSACurve | ||
Dilithium *dilithium.Dilithium | ||
PublicPoint, PublicDilithium []byte | ||
} | ||
|
||
type PrivateKey struct { | ||
PublicKey | ||
SecretEC []byte | ||
SecretDilithium []byte | ||
} | ||
|
||
func GenerateKey(rand io.Reader, algId uint8, c ecc.EdDSACurve, d *dilithium.Dilithium) (priv *PrivateKey, err error) { | ||
priv = new(PrivateKey) | ||
|
||
priv.PublicKey.AlgId = algId | ||
priv.PublicKey.Curve = c | ||
priv.PublicKey.Dilithium = d | ||
|
||
priv.PublicKey.PublicPoint, priv.SecretEC, err = c.GenerateEdDSA(rand) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dilithiumSeed := make([]byte, dilithium.SEEDBYTES) | ||
_, err = rand.Read(dilithiumSeed) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
priv.PublicKey.PublicDilithium, priv.SecretDilithium = priv.PublicKey.Dilithium.KeyGen(dilithiumSeed) | ||
return | ||
} | ||
|
||
func Sign(priv *PrivateKey, message []byte) (dSig, ecSig []byte, err error) { | ||
ecSig, err = priv.PublicKey.Curve.Sign(priv.PublicKey.PublicPoint, priv.SecretEC, message) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
dSig = priv.PublicKey.Dilithium.Sign(priv.SecretDilithium, message) | ||
if dSig == nil { | ||
return nil, nil, errors.New("dilithium_eddsa: unable to sign with dilithium") | ||
} | ||
|
||
return | ||
} | ||
|
||
func Verify(pub *PublicKey, message, dSig, ecSig []byte) bool { | ||
return pub.Curve.Verify(pub.PublicPoint, message, ecSig) && pub.Dilithium.Verify(pub.PublicDilithium, message, dSig) | ||
} | ||
|
||
func Validate(priv *PrivateKey) (err error) { | ||
var tr [dilithium.SEEDBYTES]byte | ||
|
||
if err = priv.PublicKey.Curve.Validate(priv.PublicKey.PublicPoint, priv.SecretEC); err != nil { | ||
return err | ||
} | ||
|
||
state := sha3.NewShake256() | ||
|
||
state.Write(priv.PublicKey.PublicDilithium) | ||
state.Read(tr[:]) | ||
kSk := priv.PublicKey.Dilithium.UnpackSK(priv.SecretDilithium) | ||
if subtle.ConstantTimeCompare(kSk.Tr[:], tr[:]) == 0 { | ||
return errors.New("dilithium_eddsa: invalid public key") | ||
} | ||
|
||
return | ||
} |
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,87 @@ | ||
// Package dilithium_eddsa_test tests the implementation of hybrid Dilithium + EdDSA encryption, suitable for OpenPGP, experimental. | ||
package dilithium_eddsa_test | ||
|
||
import ( | ||
"crypto/rand" | ||
"io" | ||
"testing" | ||
|
||
"github.com/ProtonMail/go-crypto/openpgp/dilithium_eddsa" | ||
"github.com/ProtonMail/go-crypto/openpgp/packet" | ||
) | ||
|
||
func TestSignVerify(t *testing.T) { | ||
asymmAlgos := map[string] packet.PublicKeyAlgorithm { | ||
"Dilithium2_Ed25519": packet.PubKeyAlgoDilithium2Ed25519, | ||
"Dilithium5_Ed448": packet.PubKeyAlgoDilithium5Ed448, | ||
} | ||
|
||
for asymmName, asymmAlgo := range asymmAlgos { | ||
t.Run(asymmName, func(t *testing.T) { | ||
key := testGenerateKeyAlgo(t, asymmAlgo) | ||
testSignVerifyAlgo(t, key) | ||
testvalidateAlgo(t, asymmAlgo) | ||
}) | ||
} | ||
} | ||
|
||
func testvalidateAlgo(t *testing.T, algId packet.PublicKeyAlgorithm) { | ||
key := testGenerateKeyAlgo(t, algId) | ||
if err := dilithium_eddsa.Validate(key); err != nil { | ||
t.Fatalf("valid key marked as invalid: %s", err) | ||
} | ||
|
||
key.PublicDilithium[5] ^= 1 | ||
if err := dilithium_eddsa.Validate(key); err == nil { | ||
t.Fatalf("failed to detect invalid key") | ||
} | ||
|
||
// Generate fresh key | ||
key = testGenerateKeyAlgo(t, algId) | ||
if err := dilithium_eddsa.Validate(key); err != nil { | ||
t.Fatalf("valid key marked as invalid: %s", err) | ||
} | ||
|
||
key.PublicPoint[5] ^= 1 | ||
if err := dilithium_eddsa.Validate(key); err == nil { | ||
t.Fatalf("failed to detect invalid key") | ||
} | ||
} | ||
|
||
func testGenerateKeyAlgo(t *testing.T, algId packet.PublicKeyAlgorithm) *dilithium_eddsa.PrivateKey { | ||
curveObj, err := packet.GetEdDSACurveFromAlgID(algId) | ||
if err != nil { | ||
t.Errorf("error getting curve: %s", err) | ||
} | ||
|
||
kyberObj, err := packet.GetDilithiumFromAlgID(algId) | ||
if err != nil { | ||
t.Errorf("error getting dilithium: %s", err) | ||
} | ||
|
||
priv, err := dilithium_eddsa.GenerateKey(rand.Reader, uint8(algId), curveObj, kyberObj) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
return priv | ||
} | ||
|
||
|
||
func testSignVerifyAlgo(t *testing.T, priv *dilithium_eddsa.PrivateKey) { | ||
digest := make([]byte, 32) | ||
_, err := io.ReadFull(rand.Reader, digest[:]) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
dSig, ecSig, err := dilithium_eddsa.Sign(priv, digest) | ||
if err != nil { | ||
t.Errorf("error encrypting: %s", err) | ||
} | ||
|
||
result := dilithium_eddsa.Verify(&priv.PublicKey, digest, dSig, ecSig) | ||
if !result { | ||
t.Error("unable to verify message") | ||
} | ||
} |
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