-
Notifications
You must be signed in to change notification settings - Fork 19
/
example.go
105 lines (80 loc) · 2.2 KB
/
example.go
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
package main
// Simple test program to test the SRP library
// Author: Sudhi Herle
// April 2014
import (
"crypto/subtle"
"fmt"
"github.com/opencoff/go-srp"
)
func main() {
bits := 1024
pass := []byte("password string that's too long")
i := []byte("foouser")
s, err := srp.New(bits)
if err != nil {
panic(err)
}
v, err := s.Verifier(i, pass)
if err != nil {
panic(err)
}
ih, vh := v.Encode()
// Store ih, vh in durable storage
fmt.Printf("Verifier Store:\n %s => %s\n", ih, vh)
c, err := s.NewClient(i, pass)
if err != nil {
panic(err)
}
// client credentials (public key and identity) to send to server
creds := c.Credentials()
fmt.Printf("Client Begin; <I, A> --> server:\n %s\n", creds)
// Begin the server by parsing the client public key and identity.
ih, A, err := srp.ServerBegin(creds)
if err != nil {
panic(err)
}
// Now, pretend to lookup the user db using "I" as the key and
// fetch salt, verifier etc.
s, v, err = srp.MakeSRPVerifier(vh)
if err != nil {
panic(err)
}
fmt.Printf("Server Begin; <v, A>:\n %s\n %x\n", vh, A.Bytes())
srv, err := s.NewServer(v, A)
if err != nil {
panic(err)
}
// Generate the credentials to send to client
creds = srv.Credentials()
// Send the server public key and salt to server
fmt.Printf("Server Begin; <s, B> --> client:\n %s\n", creds)
// client processes the server creds and generates
// a mutual authenticator; the authenticator is sent
// to the server as proof that the client derived its keys.
cauth, err := c.Generate(creds)
if err != nil {
panic(err)
}
fmt.Printf("Client Authenticator: M --> Server\n %s\n", cauth)
// Receive the proof of authentication from client
proof, ok := srv.ClientOk(cauth)
if !ok {
panic("client auth failed")
}
// Send proof to the client
fmt.Printf("Server Authenticator: M' --> Server\n %s\n", proof)
// Verify the server's proof
if !c.ServerOk(proof) {
panic("server auth failed")
}
// Now, we have successfully authenticated the client to the
// server and vice versa.
kc := c.RawKey()
ks := srv.RawKey()
if 1 != subtle.ConstantTimeCompare(kc, ks) {
panic("Keys are different!")
}
fmt.Printf("Client Key: %x\nServer Key: %x\n", kc, ks)
}
// EOF