-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathmain.go
140 lines (121 loc) · 3.15 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"bytes"
"crypto/ed25519"
"crypto/sha256"
"encoding/base64"
"image/jpeg"
"log"
"os"
"path/filepath"
"strings"
"lukechampine.com/flagg"
"lukechampine.com/jsteg"
)
const magic = "slink"
func keypair(password string) (ed25519.PublicKey, ed25519.PrivateKey) {
h := sha256.Sum256([]byte(password))
pk, sk, _ := ed25519.GenerateKey(bytes.NewReader(h[:]))
return pk, sk
}
func main() {
log.SetFlags(0)
flagg.Root.Usage = flagg.SimpleUsage(flagg.Root, `Usage: slink [command] [args]
Commands:
claim img.jpg password Embed a public key
prove data password Sign arbitrary data
verify img.jpg data signature Verify a signature
Use claim to embed your public key in an image. Later, you can
use prove to demonstrate that you control the private key paired
with the public key. Use verify to verify that a signature was
generated from the same keypair embedded in the image.
`)
cmdClaim := flagg.New("claim", `Usage:
slink claim img.jpg password
Embed a public key (derived from password) in img.jpg
`)
cmdProve := flagg.New("prove", `Usage:
slink prove data password
Sign arbitrary data using the private key derived from password
`)
cmdVerify := flagg.New("verify", `Usage:
slink verify img.jpg data signature
Verify that data was signed by the same key embedded in img.jpg
`)
cmd := flagg.Parse(flagg.Tree{
Cmd: flagg.Root,
Sub: []flagg.Tree{
{Cmd: cmdClaim},
{Cmd: cmdProve},
{Cmd: cmdVerify},
},
})
switch cmd {
case cmdClaim:
if cmd.NArg() != 2 {
cmd.Usage()
return
}
infile, err := os.Open(cmd.Arg(0))
if err != nil {
log.Fatal(err)
}
defer infile.Close()
img, err := jpeg.Decode(infile)
if err != nil {
log.Fatal(err)
}
outPath := strings.TrimSuffix(cmd.Arg(0), filepath.Ext(cmd.Arg(0))) + ".claimed.jpg"
outfile, err := os.Create(outPath)
if err != nil {
log.Fatal(err)
}
defer outfile.Close()
data := make([]byte, len(magic)+ed25519.PublicKeySize)
copy(data, magic)
pk, _ := keypair(cmd.Arg(1))
copy(data[len(magic):], pk[:])
err = jsteg.Hide(outfile, img, data, nil)
if err != nil {
log.Fatal(err)
}
os.Stdout.WriteString("Wrote claimed jpeg to " + outPath + "\n")
case cmdProve:
if cmd.NArg() != 2 {
cmd.Usage()
return
}
_, sk := keypair(cmd.Arg(1))
sig := ed25519.Sign(sk, []byte(cmd.Arg(0)))
os.Stdout.WriteString(base64.StdEncoding.EncodeToString(sig) + "\n")
case cmdVerify:
if cmd.NArg() != 3 {
cmd.Usage()
return
}
infile, err := os.Open(cmd.Arg(0))
if err != nil {
log.Fatal(err)
}
defer infile.Close()
data, err := jsteg.Reveal(infile)
if err != nil {
log.Fatal(err)
}
if len(data) < len(magic)+ed25519.PublicKeySize || string(data[:len(magic)]) != magic {
log.Fatal("Image was not signed with slink")
}
pk := ed25519.PublicKey(data[len(magic):][:ed25519.PublicKeySize])
sig, err := base64.StdEncoding.DecodeString(cmd.Arg(2))
if err != nil {
log.Fatal("Invalid signature")
}
if ed25519.Verify(pk, []byte(cmd.Arg(1)), sig) {
log.Println("Verified OK")
} else {
log.Fatal("Bad signature")
}
default:
flagg.Root.Usage()
}
}