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/>.
30 "github.com/agl/ed25519"
31 "github.com/magical/argon2"
40 type Verifier struct {
45 Pub *[ed25519.PublicKeySize]byte
48 // Generate new verifier for given peer, with specified password and
49 // hashing parameters.
50 func VerifierNew(m, t, p int, id *PeerId) *Verifier {
51 return &Verifier{M: m, T: t, P: p, Id: id}
54 // Apply the password: create Ed25519 keypair based on it, save public
56 func (v *Verifier) PasswordApply(password string) *[ed25519.PrivateKeySize]byte {
57 r, err := argon2.Key([]byte(password), v.Id[:], v.T, v.P, int64(v.M), 32)
59 log.Fatalln("Unable to apply Argon2d", err)
62 src := bytes.NewBuffer(r)
63 pub, prv, err := ed25519.GenerateKey(src)
65 log.Fatalln("Unable to generate Ed25519 keypair", err)
71 // Parse either short or long verifier form.
72 func VerifierFromString(input string) (*Verifier, error) {
73 s := strings.Split(input, "$")
74 if !(len(s) != 4 || len(s) != 5) || s[1] != "argon2d" {
75 return nil, errors.New("Invalid verifier structure")
78 n, err := fmt.Sscanf(s[2], "m=%d,t=%d,p=%d", &m, &t, &p)
79 if n != 3 || err != nil {
80 return nil, errors.New("Invalid verifier parameters")
82 salt, err := base64.RawStdEncoding.DecodeString(s[3])
86 v := Verifier{M: m, T: t, P: p}
87 id := new([IDSize]byte)
92 pub, err := base64.RawStdEncoding.DecodeString(s[4])
96 v.Pub = new([ed25519.PublicKeySize]byte)
102 // Short verifier string form -- it is useful for the client.
103 // Does not include public key.
104 func (v *Verifier) ShortForm() string {
106 "$argon2d$m=%d,t=%d,p=%d$%s",
107 v.M, v.T, v.P, base64.RawStdEncoding.EncodeToString(v.Id[:]),
111 // Long verifier string form -- it is useful for the server.
112 // Includes public key.
113 func (v *Verifier) LongForm() string {
115 "%s$%s", v.ShortForm(),
116 base64.RawStdEncoding.EncodeToString(v.Pub[:]),
120 // Read string from the file, trimming newline.
121 func StringFromFile(path string) string {
122 s, err := ioutil.ReadFile(path)
124 log.Fatalln("Can not read string from", path, err)
126 return strings.TrimRight(string(s), "\n")