-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
236 lines (217 loc) · 5.77 KB
/
main.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
//go:generate cp /usr/local/go/misc/wasm/wasm_exec.js .
// compile with
// GOOS=js GOARCH=wasm go build -o main.wasm
//
// index.html
//
// <html>
// <head>
// <meta charset="utf-8">
// <script src="wasm_exec.js"></script>
// <script>
// const go = new Go();
// WebAssembly.instantiateStreaming(fetch("main.wasm"), go.importObject).then((result) => {
// go.run(result.instance);
// });
// </script>
// </head>
// <body></body>
// </html>
// to run
//
// bob = pakeInit("pass1","0");
// jane = pakeInit("pass1","1");
// jane = pakeUpdate(jane,pakePublic(bob));
// bob = pakeUpdate(bob,pakePublic(jane));
// jane = pakeUpdate(jane,pakePublic(bob));
// console.log(pakeSessionKey(bob))
// console.log(pakeSessionKey(jane))
import (
"crypto/aes"
"crypto/cipher"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"syscall/js"
"time"
"github.com/schollz/pake/v2"
"golang.org/x/crypto/pbkdf2"
)
// ENCRYPTION
type Encryption struct {
key []byte
passphrase []byte
salt []byte
}
// New generates a new Encryption, using the supplied passphrase and
// an optional supplied salt.
// Passing nil passphrase will not use decryption.
func NewEncryption(passphrase []byte, salt []byte) (e Encryption, err error) {
if passphrase == nil {
e = Encryption{nil, nil, nil}
return
}
e.passphrase = passphrase
if salt == nil {
e.salt = make([]byte, 8)
// http://www.ietf.org/rfc/rfc2898.txt
// Salt.
rand.Read(e.salt)
} else {
e.salt = salt
}
e.key = pbkdf2.Key([]byte(passphrase), e.salt, 100, 32, sha256.New)
return
}
func (e Encryption) Salt() []byte {
return e.salt
}
// Encrypt will generate an Encryption, prefixed with the IV
func (e Encryption) Encrypt(plaintext []byte) (encrypted []byte, err error) {
if e.passphrase == nil {
encrypted = plaintext
return
}
// generate a random iv each time
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
// Section 8.2
ivBytes := make([]byte, 12)
rand.Read(ivBytes)
b, err := aes.NewCipher(e.key)
if err != nil {
return
}
aesgcm, err := cipher.NewGCM(b)
if err != nil {
return
}
encrypted = aesgcm.Seal(nil, ivBytes, plaintext, nil)
encrypted = append(ivBytes, encrypted...)
return
}
// Decrypt an Encryption
func (e Encryption) Decrypt(encrypted []byte) (plaintext []byte, err error) {
if e.passphrase == nil {
plaintext = encrypted
return
}
b, err := aes.NewCipher(e.key)
if err != nil {
return
}
aesgcm, err := cipher.NewGCM(b)
if err != nil {
return
}
plaintext, err = aesgcm.Open(nil, encrypted[:12], encrypted[12:], nil)
return
}
// encrypt(message,password,salt)
func encrypt(this js.Value, inputs []js.Value) interface{} {
if len(inputs) != 3 {
return js.Global().Get("Error").New("not enough inputs")
}
e, err := NewEncryption([]byte(inputs[1].String()), []byte(inputs[2].String()))
if err != nil {
return js.Global().Get("Error").New(err.Error())
}
enc, err := e.Encrypt([]byte(inputs[0].String()))
if err != nil {
return js.Global().Get("Error").New(err.Error())
}
return hex.EncodeToString(enc)
}
// decrypt(message,password,salt)
func decrypt(this js.Value, inputs []js.Value) interface{} {
e, err := NewEncryption([]byte(inputs[1].String()), []byte(inputs[2].String()))
if err != nil {
return js.Global().Get("Error").New(err.Error())
}
decBytes, err := hex.DecodeString(inputs[0].String())
if err != nil {
return js.Global().Get("Error").New(err.Error())
}
dec, err := e.Decrypt(decBytes)
if err != nil {
return js.Global().Get("Error").New(err.Error())
}
return string(dec)
}
// initPake(weakPassphrase, role)
// returns: pakeBytes
func pakeInit(this js.Value, inputs []js.Value) interface{} {
// initialize sender P ("0" indicates sender)
if len(inputs) != 2 {
return js.Global().Get("Error").New("need weakPassphrase, role")
}
role := 0
if inputs[1].String() == "1" {
role = 1
}
P, err := pake.Init([]byte(inputs[0].String()), role, elliptic.P521(), 1*time.Millisecond)
if err != nil {
return js.Global().Get("Error").New(err.Error())
}
bJSON, _ := json.Marshal(P)
return string(bJSON)
}
// pakeUpdate(pakeBytes,otherPublicPakeBytes)
func pakeUpdate(this js.Value, inputs []js.Value) interface{} {
if len(inputs) != 2 {
return js.Global().Get("Error").New("need two input")
}
var P, Q *pake.Pake
err := json.Unmarshal([]byte(inputs[0].String()), &P)
P.SetCurve(elliptic.P521())
if err != nil {
return js.Global().Get("Error").New(err.Error())
}
err = json.Unmarshal([]byte(inputs[1].String()), &Q)
Q.SetCurve(elliptic.P521())
if err != nil {
return js.Global().Get("Error").New(err.Error())
}
P.Update(Q.Bytes())
bJSON, _ := json.Marshal(P)
return string(bJSON)
}
// pakePublic(pakeBytes)
func pakePublic(this js.Value, inputs []js.Value) interface{} {
var P *pake.Pake
err := json.Unmarshal([]byte(inputs[0].String()), &P)
P.SetCurve(elliptic.P521())
if err != nil {
return js.Global().Get("Error").New(err.Error())
}
return string(P.Public().Bytes())
}
// pakeSessionKey(pakeBytes)
func pakeSessionKey(this js.Value, inputs []js.Value) interface{} {
var P *pake.Pake
err := json.Unmarshal([]byte(inputs[0].String()), &P)
P.SetCurve(elliptic.P521())
if err != nil {
return js.Global().Get("Error").New(err.Error())
}
key, err := P.SessionKey()
if err != nil {
return js.Global().Get("Error").New(err.Error())
}
return hex.EncodeToString(key)
}
func main() {
c := make(chan bool)
fmt.Println("starting")
js.Global().Set("encrypt", js.FuncOf(encrypt))
js.Global().Set("decrypt", js.FuncOf(decrypt))
js.Global().Set("pakeInit", js.FuncOf(pakeInit))
js.Global().Set("pakePublic", js.FuncOf(pakePublic))
js.Global().Set("pakeUpdate", js.FuncOf(pakeUpdate))
js.Global().Set("pakeSessionKey", js.FuncOf(pakeSessionKey))
fmt.Println("Initiated")
<-c
}