~moody/libdp9ik

libdp9ik/auth.go -rw-r--r-- 3.0 KiB
4efa26b9 — Jacob Moody Progress made towards authpak_new and authpak_finish 9 months ago
                                                                                
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package libdp9ik

import (
	"crypto/rand"
	"crypto/sha256"
	"errors"
	"math/big"

	"golang.org/x/crypto/hkdf"
)

type AuthInfo struct {
	Cuid       string /* caller id */
	Suid       string /* server id */
	Capability string /* capability (only valid on server side) */
	Secret     []byte /* secret */
}

type AuthKey struct {
	Des     [Deskeylen]byte  /* DES key from password */
	Aes     [Aeskeylen]byte  /* Aes key from password */
	Pakkey  [Pakkeylen]byte  /* shared key from AuthPak exchange */
	Pakhash [Pakhashlen]byte /* secret hash from AES key and user name */
}

func (ak *AuthKey) Passtokey(pw string) {
	//TODO: p9sk1 DES
	copy(ak.Aes[:], passtoaeskey(pw))
}

func (ak *AuthKey) AuthpakHash(u []byte) error {
	info := []byte("Plan 9 AuthPAK hash")

	H := big.NewInt(0)
	PX := big.NewInt(0)
	PY := big.NewInt(0)
	PZ := big.NewInt(0)
	PT := big.NewInt(0)

	h := make([]byte, 2*Pakslen)

	salt := sha256.Sum256(u)
	hr := hkdf.New(sha256.New, ak.Aes[:], salt[:], info)
	_, err := hr.Read(h)
	if err != nil {
		return err
	}

	c := GPAKCurve
	H = H.SetBytes(h[:Pakslen])
	Spake2ee_h2P(c.P, c.A, c.D, H, PX, PY, PZ, PT)

	n := 0
	move := func(b *big.Int) {
		b.FillBytes(ak.Pakhash[n : n+Pakslen])
		n += Pakslen
	}
	move(PX)
	move(PY)
	move(PZ)
	move(PT)

	H = H.SetBytes(h[Pakslen:])
	Spake2ee_h2P(c.P, c.A, c.D, H, PX, PY, PZ, PT)
	move(PX)
	move(PY)
	move(PZ)
	move(PT)

	return nil
}

type PAKPriv struct {
	Isclient bool
	X        [Pakxlen]byte
	Y        [Pakylen]byte
}

func authpak_new(ak *AuthKey, y []byte, isclient int) (*PAKPriv, error) {
	p := &PAKPriv{}
	p.Isclient = (isclient != 0)

	Y := big.NewInt(0)

	PX := big.NewInt(0)
	PY := big.NewInt(0)
	PZ := big.NewInt(0)
	PT := big.NewInt(0)

	n := 0
	if p.Isclient == false {
		n += Pakplen
	}
	move := func(b *big.Int) {
		b.FillBytes(ak.Pakhash[n : n+Pakslen])
		n += Pakslen
	}
	move(PX)
	move(PY)
	move(PZ)
	move(PT)

	c := GPAKCurve
	X, err := rand.Int(rand.Reader, c.P)
	if err != nil {
		return nil, err
	}

	Spake2ee_1(c.P, c.A, c.D, X, c.X, c.Y, PX, PY, PZ, PT, Y)
	X.FillBytes(p.X[:])
	Y.FillBytes(p.Y[:])

	copy(y, p.Y[:])

	return p, nil
}

func (p *PAKPriv) authpak_finish(ak *AuthKey, y []byte) error {
	info := []byte("Plan 9 AuthPAK Key")
	z := make([]byte, Pakslen)

	X := big.NewInt(0)
	Y := big.NewInt(0)
	Z := big.NewInt(0)
	ok := big.NewInt(0)

	PX := big.NewInt(0)
	PY := big.NewInt(0)
	PZ := big.NewInt(0)
	PT := big.NewInt(0)

	n := 0
	if p.Isclient {
		n += Pakplen
	}
	move := func(b *big.Int) {
		b.FillBytes(ak.Pakhash[n : n+Pakslen])
		n += Pakslen
	}
	move(PX)
	move(PY)
	move(PZ)
	move(PT)

	X.SetBytes(p.X[:])
	Y.SetBytes(y[:Pakylen])

	c := GPAKCurve
	Spake2ee_2(c.P, c.A, c.D, PX, PY, PZ, PT, X, Y, ok, Z)

	if ok.Cmp(big.NewInt(0)) == 0 {
		return errors.New("spake2ee_2 failed")
	}

	Z.FillBytes(z)

	sha := sha256.New()
	if p.Isclient {
		sha.Write(p.Y[:])
		sha.Write(y[:Pakylen])
	} else {
		sha.Write(y[:Pakylen])
		sha.Write(p.Y[:])
	}
	salt := sha.Sum([]byte{})
	hr := hkdf.New(sha256.New, z[:], salt[:], info)
	_, err := hr.Read(ak.Pakkey[:])
	return err
}