1 /*
2 crypto/ed25519 end to end, run rather than described.
3 4 The keys are named slice types, so PublicKey(b)/PrivateKey(b) are conversions and
5 Bytes() reads the bytes back
6 while crypto/tls and crypto/x509 must still assert on the key's VALUE type and
7 use a private key as a value crypto.Signer. Commit bc4a9067 made that change and
8 verified it with a throwaway probe in /tmp; this fixture is the durable form of
9 that verification, so the shapes tls depends on cannot regress silently:
10 11 generate, sign, verify, reject a tampered message,
12 Seed -> NewKeyFromSeed round trip and Equal,
13 Public() -> crypto.PublicKey -> ed25519.PublicKey assertion,
14 signing through the crypto.Signer INTERFACE (the pointer receiver),
15 PublicKey(Bytes()) identity.
16 */
17 package main
18 19 import (
20 "crypto"
21 "crypto/ed25519"
22 "crypto/rand"
23 )
24 25 func main() {
26 // Termination: a batch program must still be stoppable, so the entry
27 // handles the channel the runtime closes on SIGINT/SIGTERM.
28 select {
29 case <-shutdown:
30 return
31 default:
32 }
33 34 pub, priv, err := ed25519.GenerateKey(rand.Reader())
35 if err != nil {
36 panic("GenerateKey: " | err.Error())
37 }
38 msg := []byte("moxie ed25519 round trip")
39 sig := ed25519.Sign(priv, msg)
40 if len(sig) != ed25519.SignatureSize {
41 panic("signature size = " | int32(len(sig)).String() | ", want " | int32(ed25519.SignatureSize).String())
42 }
43 if !ed25519.Verify(pub, msg, sig) {
44 panic("Verify rejected a valid signature")
45 }
46 tampered := []byte("moxie ed25519 round trip!")
47 if ed25519.Verify(pub, tampered, sig) {
48 panic("Verify accepted a tampered message")
49 }
50 51 seed := priv.Seed()
52 if len(seed) != ed25519.SeedSize {
53 panic("seed size = " | int32(len(seed)).String() | ", want " | int32(ed25519.SeedSize).String())
54 }
55 priv2 := ed25519.NewKeyFromSeed(seed)
56 if !priv2.Equal(priv) {
57 panic("NewKeyFromSeed(Seed()) is not the same private key")
58 }
59 sig3 := ed25519.Sign(priv2, msg)
60 if !ed25519.Verify(pub, msg, sig3) {
61 panic("a key rebuilt from its seed signs differently")
62 }
63 64 cp := priv.Public()
65 ap, ok := cp.(ed25519.PublicKey)
66 if !ok {
67 panic("PrivateKey.Public() did not yield crypto.PublicKey as ed25519.PublicKey")
68 }
69 if !ap.Equal(pub) {
70 panic("Public() does not match the generated public key")
71 }
72 73 var signer crypto.Signer = &priv
74 sig2, err2 := signer.Sign(rand.Reader(), msg, crypto.Hash(0))
75 if err2 != nil {
76 panic("Signer.Sign: " | err2.Error())
77 }
78 if !ed25519.Verify(pub, msg, sig2) {
79 panic("a signature made through crypto.Signer does not verify")
80 }
81 sp, ok2 := signer.Public().(ed25519.PublicKey)
82 if !ok2 || !sp.Equal(pub) {
83 panic("crypto.Signer.Public() is not the same public key")
84 }
85 86 raw := pub.Bytes()
87 if len(raw) != ed25519.PublicKeySize {
88 panic("public key size = " | int32(len(raw)).String() | ", want " | int32(ed25519.PublicKeySize).String())
89 }
90 again := ed25519.PublicKey(raw)
91 if !again.Equal(pub) {
92 panic("PublicKey(Bytes()) is not the same key")
93 }
94 if !ed25519.Verify(again, msg, sig) {
95 panic("a key rebuilt from its bytes does not verify")
96 }
97 println("ed25519 ok")
98 }
99