aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 16b1c1812b06c388fbe5db3e68cc064fff81b1e7 (plain)
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
141
142
package main

import (
	"bufio"
	"crypto/rand"
	"encoding/base64"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"os"
	"strings"

	"golang.org/x/crypto/ed25519"
)

const (
	PKAlg      = "Ed"
	KDFAlg     = "BK"
	commentHdr = "untrusted comment: "
	verifyWith = "verify with "
	pubKey     = "%s public key"
	secKey     = "%s secret key"
	sigFrom    = "signature from %s"
	verFailed  = "signature verfication failed"
	verOK      = "Signature Verfied"
)

/*
	signify -C [-q] -p pubkey -x sigfile [file ...]
	signify -G [-n] [-c comment] -p pubkey -s seckey
	signify -S [-e] [-x sigfile] -s seckey -m message
	signify -V [-eq] [-x sigfile] -p pubkey -m message
*/

var (
	checksum = flag.Bool("C", false, "Verify a signed checksum list")
	generate = flag.Bool("G", false, "Generate a new key pair")
	sign     = flag.Bool("S", false, "Sign the specfied message")
	vefify   = flag.Bool("V", false, "Verify the message")
	comment  = flag.String("c", "", "Comment")
	embed    = flag.Bool("e", false, "Embed the message")
	msg      = flag.String("m", "", "Message file")
	nopass   = flag.Bool("n", false, "No key passphrase")
	pub      = flag.String("p", "", "Public key file")
	quiet    = flag.Bool("q", false, "Quiet mode")
	sec      = flag.String("s", "", "Secret key file")
	sig      = flag.String("x", "", "Signature file")
)

func main() {
	flag.Parse()

	var rounds = 42
	if *nopass {
		rounds = 0
	}
	_ = rounds

	/*
		if err := Generate(*pub, *sec, *comment, rounds); err != nil {
			log.Fatal(err)
		}
	*/

	log.Println(parseFile("test.sig"))
	s, _ := base64.StdEncoding.DecodeString("RWRCSwAAAACzJBN2gC5//jVvDiV76rs4m2aKXkljqDpbOC0bBf7abZhV/Zygr6b0KIbSI56JQutwzsQeouxnnHuVTZp3IW4M9qdpe5Nh8Jrr5g7r0rHLPxEPmcv/dNru6ZjqI7CcGsY=")
	fmt.Printf("%v\n", s)
}

const KeyNumLen = 8

type EncKey struct {
	PKAlg     [2]byte
	KDFAlg    [2]byte
	KDFRounds uint32 // network byte order
	Salt      [16]byte
	Checksum  [8]byte
	KeyNum    [KeyNumLen]byte
	SecKey    [ed25519.PrivateKeySize]byte
}

type PubKey struct {
	PKAlg  [2]byte
	KeyNum [KeyNumLen]byte
	PubKey [ed25519.PublicKeySize]byte
}

type Sig struct {
	PKAlg  [2]byte
	KeyNum [KeyNumLen]byte
	Sig    [ed25519.SignatureSize]byte
}

type File struct {
	Comment string
	Key     string
	Body    []byte
}

func Generate(pubFile, secFile, comment string, rounds int) error {
	pub, sec, err := ed25519.GenerateKey(rand.Reader)
	if err != nil {
		return err
	}
	b64 := base64.NewEncoder(base64.StdEncoding, os.Stdout)
	fmt.Println("pub", len(pub), pub)
	b64.Write(pub)
	fmt.Println("sec", len(sec), sec)
	b64.Write(sec)
	return nil
}

func Sign()   {}
func Verify() {}

func parseFile(fname string) (File, error) {
	fd, err := os.Open(fname)
	if err != nil {
		return File{}, err
	}
	defer fd.Close()
	buf := bufio.NewReader(fd)
	comment, err := buf.ReadString('\n')
	if err != nil {
		return File{}, err
	}
	comment = strings.TrimRight(comment, "\r\n")
	log.Println(comment)

	b64, err := buf.ReadString('\n')
	if err != nil {
		return File{}, err
	}
	b64 = strings.TrimRight(b64, "\r\n")
	body, err := ioutil.ReadAll(buf)
	return File{
		Comment: comment,
		Key:     b64,
		Body:    body,
	}, nil
}