| 1 |
// Package mldsa_eddsa_test tests the implementation of hybrid ML-DSA + EdDSA encryption, suitable for OpenPGP, experimental. |
| 2 |
package mldsa_eddsa_test |
| 3 |
|
| 4 |
import ( |
| 5 |
"crypto/rand" |
| 6 |
"io" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/ProtonMail/go-crypto/openpgp/mldsa_eddsa" |
| 10 |
"github.com/ProtonMail/go-crypto/openpgp/packet" |
| 11 |
) |
| 12 |
|
| 13 |
func TestSignVerify(t *testing.T) { |
| 14 |
asymmAlgos := map[string]packet.PublicKeyAlgorithm{ |
| 15 |
"ML-DSA3_Ed25519": packet.PubKeyAlgoMldsa65Ed25519, |
| 16 |
"ML-DSA5_Ed448": packet.PubKeyAlgoMldsa87Ed448, |
| 17 |
} |
| 18 |
|
| 19 |
for asymmName, asymmAlgo := range asymmAlgos { |
| 20 |
t.Run(asymmName, func(t *testing.T) { |
| 21 |
key := testGenerateKeyAlgo(t, asymmAlgo) |
| 22 |
testSignVerifyAlgo(t, key) |
| 23 |
testvalidateAlgo(t, asymmAlgo) |
| 24 |
}) |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
func testvalidateAlgo(t *testing.T, algId packet.PublicKeyAlgorithm) { |
| 29 |
key := testGenerateKeyAlgo(t, algId) |
| 30 |
if err := mldsa_eddsa.Validate(key); err != nil { |
| 31 |
t.Fatalf("valid key marked as invalid: %s", err) |
| 32 |
} |
| 33 |
|
| 34 |
bin, err := key.PublicMldsa.MarshalBinary() |
| 35 |
if err != nil { |
| 36 |
t.Fatal(err) |
| 37 |
} |
| 38 |
bin[5] ^= 1 |
| 39 |
key.PublicMldsa, err = key.Mldsa.UnmarshalBinaryPublicKey(bin) //PublicKeyFromBytes(bin) |
| 40 |
if err != nil { |
| 41 |
t.Fatal(err) |
| 42 |
} |
| 43 |
|
| 44 |
if err := mldsa_eddsa.Validate(key); err == nil { |
| 45 |
t.Fatalf("failed to detect invalid key") |
| 46 |
} |
| 47 |
|
| 48 |
// Generate fresh key |
| 49 |
key = testGenerateKeyAlgo(t, algId) |
| 50 |
if err := mldsa_eddsa.Validate(key); err != nil { |
| 51 |
t.Fatalf("valid key marked as invalid: %s", err) |
| 52 |
} |
| 53 |
|
| 54 |
key.PublicPoint[5] ^= 1 |
| 55 |
if err := mldsa_eddsa.Validate(key); err == nil { |
| 56 |
t.Fatalf("failed to detect invalid key") |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
func testGenerateKeyAlgo(t *testing.T, algId packet.PublicKeyAlgorithm) *mldsa_eddsa.PrivateKey { |
| 61 |
curveObj, err := packet.GetEdDSACurveFromAlgID(algId) |
| 62 |
if err != nil { |
| 63 |
t.Errorf("error getting curve: %s", err) |
| 64 |
} |
| 65 |
|
| 66 |
kyberObj, err := packet.GetMldsaFromAlgID(algId) |
| 67 |
if err != nil { |
| 68 |
t.Errorf("error getting ML-DSA: %s", err) |
| 69 |
} |
| 70 |
|
| 71 |
priv, err := mldsa_eddsa.GenerateKey(rand.Reader, uint8(algId), curveObj, kyberObj) |
| 72 |
if err != nil { |
| 73 |
t.Fatal(err) |
| 74 |
} |
| 75 |
|
| 76 |
return priv |
| 77 |
} |
| 78 |
|
| 79 |
func testSignVerifyAlgo(t *testing.T, priv *mldsa_eddsa.PrivateKey) { |
| 80 |
digest := make([]byte, 32) |
| 81 |
_, err := io.ReadFull(rand.Reader, digest[:]) |
| 82 |
if err != nil { |
| 83 |
t.Fatal(err) |
| 84 |
} |
| 85 |
|
| 86 |
dSig, ecSig, err := mldsa_eddsa.Sign(priv, digest) |
| 87 |
if err != nil { |
| 88 |
t.Errorf("error encrypting: %s", err) |
| 89 |
} |
| 90 |
|
| 91 |
result := mldsa_eddsa.Verify(&priv.PublicKey, digest, dSig, ecSig) |
| 92 |
if !result { |
| 93 |
t.Error("unable to verify message") |
| 94 |
} |
| 95 |
} |