| 1 |
package ed25519 |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/ed25519" |
| 5 |
"crypto/rand" |
| 6 |
"io" |
| 7 |
"testing" |
| 8 |
) |
| 9 |
|
| 10 |
const messageDigestSize = 32 |
| 11 |
|
| 12 |
func TestGenerate(t *testing.T) { |
| 13 |
priv, err := GenerateKey(rand.Reader) |
| 14 |
if err != nil { |
| 15 |
t.Fatal(err) |
| 16 |
} |
| 17 |
if len(priv.Key) != ed25519.SeedSize+ed25519.PublicKeySize && len(priv.Point) != ed25519.PublicKeySize { |
| 18 |
t.Error("generated wrong key sizes") |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
func TestSignVerify(t *testing.T) { |
| 23 |
digest := make([]byte, messageDigestSize) |
| 24 |
_, err := io.ReadFull(rand.Reader, digest[:]) |
| 25 |
if err != nil { |
| 26 |
t.Fatal(err) |
| 27 |
} |
| 28 |
|
| 29 |
priv, err := GenerateKey(rand.Reader) |
| 30 |
if err != nil { |
| 31 |
t.Fatal(err) |
| 32 |
} |
| 33 |
|
| 34 |
signature, err := Sign(priv, digest) |
| 35 |
if err != nil { |
| 36 |
t.Errorf("error signing: %s", err) |
| 37 |
} |
| 38 |
|
| 39 |
result := Verify(&priv.PublicKey, digest, signature) |
| 40 |
|
| 41 |
if !result { |
| 42 |
t.Error("unable to verify message") |
| 43 |
} |
| 44 |
|
| 45 |
digest[0] += 1 |
| 46 |
result = Verify(&priv.PublicKey, digest, signature) |
| 47 |
|
| 48 |
if result { |
| 49 |
t.Error("signature should be invalid") |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func TestValidation(t *testing.T) { |
| 54 |
priv, err := GenerateKey(rand.Reader) |
| 55 |
if err != nil { |
| 56 |
t.Fatal(err) |
| 57 |
} |
| 58 |
if err := Validate(priv); err != nil { |
| 59 |
t.Fatalf("valid key marked as invalid: %s", err) |
| 60 |
} |
| 61 |
|
| 62 |
priv.Key[0] += 1 |
| 63 |
if err := Validate(priv); err == nil { |
| 64 |
t.Fatal("failed to detect invalid key") |
| 65 |
} |
| 66 |
} |