2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2016 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/>.
32 "cypherpunks.ru/balloon"
33 "github.com/agl/ed25519"
34 "golang.org/x/crypto/blake2b"
35 "golang.org/x/crypto/ssh/terminal"
39 DefaultS = 1 << 20 / 32
44 type Verifier struct {
49 Pub *[ed25519.PublicKeySize]byte
52 // Generate new verifier for given peer, with specified password and
53 // hashing parameters.
54 func VerifierNew(s, t, p int, id *PeerId) *Verifier {
55 return &Verifier{S: s, T: t, P: p, Id: id}
58 func blake2bKeyless() hash.Hash {
59 h, err := blake2b.New256(nil)
66 // Apply the password: create Ed25519 keypair based on it, save public
68 func (v *Verifier) PasswordApply(password string) *[ed25519.PrivateKeySize]byte {
69 r := balloon.H(blake2bKeyless, []byte(password), v.Id[:], v.S, v.T, v.P)
71 src := bytes.NewBuffer(r)
72 pub, prv, err := ed25519.GenerateKey(src)
74 log.Fatalln("Unable to generate Ed25519 keypair", err)
80 // Parse either short or long verifier form.
81 func VerifierFromString(input string) (*Verifier, error) {
82 ss := strings.Split(input, "$")
83 if len(ss) < 4 || ss[1] != "balloon" {
84 return nil, errors.New("Invalid verifier structure")
87 n, err := fmt.Sscanf(ss[2], "s=%d,t=%d,p=%d", &s, &t, &p)
88 if n != 3 || err != nil {
89 return nil, errors.New("Invalid verifier parameters")
91 salt, err := base64.RawStdEncoding.DecodeString(ss[3])
95 v := Verifier{S: s, T: t, P: p}
96 id := new([IDSize]byte)
101 pub, err := base64.RawStdEncoding.DecodeString(ss[4])
105 v.Pub = new([ed25519.PublicKeySize]byte)
111 // Short verifier string form -- it is useful for the client.
112 // Does not include public key.
113 func (v *Verifier) ShortForm() string {
115 "$balloon$s=%d,t=%d,p=%d$%s",
116 v.S, v.T, v.P, base64.RawStdEncoding.EncodeToString(v.Id[:]),
120 // Long verifier string form -- it is useful for the server.
121 // Includes public key.
122 func (v *Verifier) LongForm() string {
124 "%s$%s", v.ShortForm(),
125 base64.RawStdEncoding.EncodeToString(v.Pub[:]),
129 // Read the key either from text file (if path is specified), or
130 // from the terminal.
131 func KeyRead(path string) (string, error) {
136 os.Stderr.Write([]byte("Passphrase:"))
137 p, err = terminal.ReadPassword(0)
138 os.Stderr.Write([]byte("\n"))
141 p, err = ioutil.ReadFile(path)
142 pass = strings.TrimRight(string(p), "\n")
148 return "", errors.New("Empty passphrase submitted")