]> Cypherpunks.ru repositories - govpn.git/blob - src/cypherpunks.ru/govpn/verifier.go
Replace Argon2 with Balloon hashing
[govpn.git] / src / cypherpunks.ru / govpn / verifier.go
1 /*
2 GoVPN -- simple secure free software virtual private network daemon
3 Copyright (C) 2014-2016 Sergey Matveev <stargrave@stargrave.org>
4
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.
9
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.
14
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/>.
17 */
18
19 package govpn
20
21 import (
22         "bytes"
23         "encoding/base64"
24         "errors"
25         "fmt"
26         "io/ioutil"
27         "log"
28         "os"
29         "strings"
30
31         "cypherpunks.ru/balloon"
32         "github.com/agl/ed25519"
33         "github.com/dchest/blake2b"
34         "golang.org/x/crypto/ssh/terminal"
35 )
36
37 const (
38         DefaultS = 1 << 20 / 32
39         DefaultT = 1 << 4
40         DefaultP = 2
41 )
42
43 type Verifier struct {
44         S   int
45         T   int
46         P   int
47         Id  *PeerId
48         Pub *[ed25519.PublicKeySize]byte
49 }
50
51 // Generate new verifier for given peer, with specified password and
52 // hashing parameters.
53 func VerifierNew(s, t, p int, id *PeerId) *Verifier {
54         return &Verifier{S: s, T: t, P: p, Id: id}
55 }
56
57 // Apply the password: create Ed25519 keypair based on it, save public
58 // key in verifier.
59 func (v *Verifier) PasswordApply(password string) *[ed25519.PrivateKeySize]byte {
60         r := balloon.H(blake2b.New256, []byte(password), v.Id[:], v.S, v.T, v.P)
61         defer SliceZero(r)
62         src := bytes.NewBuffer(r)
63         pub, prv, err := ed25519.GenerateKey(src)
64         if err != nil {
65                 log.Fatalln("Unable to generate Ed25519 keypair", err)
66         }
67         v.Pub = pub
68         return prv
69 }
70
71 // Parse either short or long verifier form.
72 func VerifierFromString(input string) (*Verifier, error) {
73         ss := strings.Split(input, "$")
74         if len(ss) < 4 || ss[1] != "balloon" {
75                 return nil, errors.New("Invalid verifier structure")
76         }
77         var s, t, p int
78         n, err := fmt.Sscanf(ss[2], "s=%d,t=%d,p=%d", &s, &t, &p)
79         if n != 3 || err != nil {
80                 return nil, errors.New("Invalid verifier parameters")
81         }
82         salt, err := base64.RawStdEncoding.DecodeString(ss[3])
83         if err != nil {
84                 return nil, err
85         }
86         v := Verifier{S: s, T: t, P: p}
87         id := new([IDSize]byte)
88         copy(id[:], salt)
89         pid := PeerId(*id)
90         v.Id = &pid
91         if len(ss) == 5 {
92                 pub, err := base64.RawStdEncoding.DecodeString(ss[4])
93                 if err != nil {
94                         return nil, err
95                 }
96                 v.Pub = new([ed25519.PublicKeySize]byte)
97                 copy(v.Pub[:], pub)
98         }
99         return &v, nil
100 }
101
102 // Short verifier string form -- it is useful for the client.
103 // Does not include public key.
104 func (v *Verifier) ShortForm() string {
105         return fmt.Sprintf(
106                 "$balloon$s=%d,t=%d,p=%d$%s",
107                 v.S, v.T, v.P, base64.RawStdEncoding.EncodeToString(v.Id[:]),
108         )
109 }
110
111 // Long verifier string form -- it is useful for the server.
112 // Includes public key.
113 func (v *Verifier) LongForm() string {
114         return fmt.Sprintf(
115                 "%s$%s", v.ShortForm(),
116                 base64.RawStdEncoding.EncodeToString(v.Pub[:]),
117         )
118 }
119
120 // Read the key either from text file (if path is specified), or
121 // from the terminal.
122 func KeyRead(path string) (string, error) {
123         var p []byte
124         var err error
125         var pass string
126         if path == "" {
127                 os.Stderr.Write([]byte("Passphrase:"))
128                 p, err = terminal.ReadPassword(0)
129                 os.Stderr.Write([]byte("\n"))
130                 pass = string(p)
131         } else {
132                 p, err = ioutil.ReadFile(path)
133                 pass = strings.TrimRight(string(p), "\n")
134         }
135         if err != nil {
136                 return "", err
137         }
138         if len(pass) == 0 {
139                 return "", errors.New("Empty passphrase submitted")
140         }
141         return pass, err
142 }