2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2017 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
33 "cypherpunks.ru/balloon"
34 "github.com/agl/ed25519"
35 "golang.org/x/crypto/blake2b"
36 "golang.org/x/crypto/ssh/terminal"
40 // DefaultS is default Balloon space cost
41 DefaultS = 1 << 20 / 32
42 // DefaultT is default Balloon time cost
44 // DefaultP is default Balloon number of jobs
48 // Verifier is used to authenticate a peer
49 type Verifier struct {
54 Pub *[ed25519.PublicKeySize]byte
57 // VerifierNew generates new verifier for given peer,
58 // with specified password and hashing parameters.
59 func VerifierNew(s, t, p int, id *PeerID) *Verifier {
60 return &Verifier{S: s, T: t, P: p, ID: id}
63 func blake2bKeyless() hash.Hash {
64 h, err := blake2b.New256(nil)
71 // PasswordApply applies the password: create Ed25519 keypair based on it,
72 // saves public key in verifier.
73 func (v *Verifier) PasswordApply(password string) *[ed25519.PrivateKeySize]byte {
74 r := balloon.H(blake2bKeyless, []byte(password), v.ID[:], v.S, v.T, v.P)
76 src := bytes.NewBuffer(r)
77 pub, prv, err := ed25519.GenerateKey(src)
79 log.Fatalln("Unable to generate Ed25519 keypair", err)
85 // VerifierFromString parses either short or long verifier form.
86 func VerifierFromString(input string) (*Verifier, error) {
87 ss := strings.Split(input, "$")
88 if len(ss) < 4 || ss[1] != "balloon" {
89 return nil, errors.New("Invalid verifier structure")
92 n, err := fmt.Sscanf(ss[2], "s=%d,t=%d,p=%d", &s, &t, &p)
93 if n != 3 || err != nil {
94 return nil, errors.New("Invalid verifier parameters")
96 salt, err := base64.RawStdEncoding.DecodeString(ss[3])
100 v := Verifier{S: s, T: t, P: p}
101 id := new([IDSize]byte)
106 pub, err := base64.RawStdEncoding.DecodeString(ss[4])
110 v.Pub = new([ed25519.PublicKeySize]byte)
116 // ShortForm outputs the short verifier string form -- it is useful
117 // for the client. It does not include public key.
118 func (v *Verifier) ShortForm() string {
120 "$balloon$s=%d,t=%d,p=%d$%s",
121 v.S, v.T, v.P, base64.RawStdEncoding.EncodeToString(v.ID[:]),
125 // LongForm outputs long verifier string form -- it is useful for the server.
126 // It includes public key.
127 func (v *Verifier) LongForm() string {
129 "%s$%s", v.ShortForm(),
130 base64.RawStdEncoding.EncodeToString(v.Pub[:]),
134 // KeyRead reads the key either from text file (if path is specified), or
135 // from the terminal.
136 func KeyRead(path string) (string, error) {
141 os.Stderr.Write([]byte("Passphrase:"))
142 p, err = terminal.ReadPassword(int(uintptr(syscall.Stdin)))
143 os.Stderr.Write([]byte("\n"))
146 p, err = ioutil.ReadFile(path)
147 pass = strings.TrimRight(string(p), "\n")
153 return "", errors.New("Empty passphrase submitted")