-
Notifications
You must be signed in to change notification settings - Fork 14
/
pake_test.go
103 lines (92 loc) · 1.92 KB
/
pake_test.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
package pake
import (
"bytes"
"fmt"
"testing"
)
func ExampleUsage() {
// both parties should have a weak key
weakKey := []byte{1, 2, 3}
// initialize A
A, err := InitCurve(weakKey, 0, "siec")
if err != nil {
panic(err)
}
// initialize B
B, err := InitCurve(weakKey, 1, "siec")
if err != nil {
panic(err)
}
// send A's stuff to B
err = B.Update(A.Bytes())
if err != nil {
panic(err)
}
// send B's stuff to A
err = A.Update(B.Bytes())
if err != nil {
panic(err)
}
// both P and Q now have session key
kA, _ := A.SessionKey()
kB, _ := A.SessionKey()
fmt.Println(bytes.Equal(kA, kB))
// Output: true
}
func TestBadCurve(t *testing.T) {
_, err := InitCurve([]byte{1, 2, 3}, 0, "bad")
if err == nil {
t.Errorf("curve should not exist!")
}
}
func TestSessionKeyString(t *testing.T) {
for _, curve := range AvailableCurves() {
fmt.Printf("testing curve '%s'\n", curve)
A, err := InitCurve([]byte{1, 2, 3}, 0, curve)
if err != nil {
t.Errorf("%s", err)
}
// initialize B
B, err := InitCurve([]byte{1, 2, 3}, 1, curve)
if err != nil {
t.Errorf("%s", err)
}
// send A's stuff to B
B.Update(A.Bytes())
// send B's stuff to A
A.Update(B.Bytes())
s1A, err := A.SessionKey()
if err != nil {
t.Errorf("%s", err)
}
s1B, err := B.SessionKey()
if err != nil {
t.Errorf("%s", err)
}
fmt.Printf("A) K=%x\n", s1A)
fmt.Printf("B) K=%x\n", s1B)
if !bytes.Equal(s1A, s1B) {
t.Errorf("keys not equal")
}
// test using incorrect password
// initialize A
A, _ = InitCurve([]byte{1, 2, 3}, 0, curve)
// initialize B
B, _ = InitCurve([]byte{1, 2, 4}, 1, curve)
// send A's stuff to B
B.Update(A.Bytes())
// send B's stuff to A
A.Update(B.Bytes())
s1A, err = A.SessionKey()
if err != nil {
t.Errorf("%s", err)
}
s1B, err = B.SessionKey()
if err != nil {
t.Errorf("%s", err)
}
if bytes.Equal(s1A, s1B) {
t.Errorf("keys should not be equal")
}
}
}