-
Notifications
You must be signed in to change notification settings - Fork 2
/
keys.go
217 lines (186 loc) · 5.14 KB
/
keys.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
/*
* Copyright (c) 2019 Zenichi Amano
*
* This file is part of http-ece, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
package httpece
import (
"crypto/aes"
"crypto/cipher"
"crypto/ecdh"
"crypto/rand"
"errors"
"fmt"
"golang.org/x/crypto/hkdf"
)
type key []byte
type nonce []byte
func deriveKeyAndNonce(opt *options) (key, nonce, error) {
var keyInfo, nonceInfo, secret, context []byte
var err error
switch opt.encoding {
case AESGCM:
// old
secret, context, err = extractSecretAndContext(opt)
if err != nil {
return nil, nil, err
}
keyInfo = buildInfo(aesgcmInfo, context)
nonceInfo = buildInfo(nonceBaseInfo, context)
break
case AES128GCM:
// latest
secret, err = extractSecret(opt)
if err != nil {
return nil, nil, err
}
keyInfo = buildInfo(aes128gcmInfo, nil)
nonceInfo = buildInfo(nonceBaseInfo, nil)
break
default:
return nil, nil, fmt.Errorf("must include a Salt parameter for %s", opt.encoding.String())
}
debug.dumpBinary("info aesgcm", keyInfo)
debug.dumpBinary("info nonce", nonceInfo)
debug.dumpBinary("hkdf secret", secret)
debug.dumpBinary("hkdf salt", opt.salt)
prk := hkdf.Extract(hashAlgorithm, secret, opt.salt)
debug.dumpBinary("hkdf prk", prk)
debug.dumpBinary("hkdf info", keyInfo)
key := make([]byte, keyLen)
_, err = hkdf.Expand(hashAlgorithm, prk, keyInfo).Read(key)
if err != nil {
return nil, nil, err
}
debug.dumpBinary("key", key)
debug.dumpBinary("hkdf prk", prk)
debug.dumpBinary("hkdf info", nonceInfo)
nonce := make([]byte, nonceLen)
if _, err = hkdf.Expand(hashAlgorithm, prk, nonceInfo).Read(nonce); err != nil {
return nil, nil, err
}
debug.dumpBinary("base nonce", nonce)
return key, nonce, err
}
func createCipher(key key) (cipher.AEAD, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
return cipher.NewGCM(block)
}
func extractSecretAndContext(opt *options) (secret []byte, context []byte, err error) {
optKeyLen := len(opt.key)
if optKeyLen > 0 {
secret = opt.key
if optKeyLen != keyLen {
return nil, nil, fmt.Errorf("an explicit Key must be %d bytes", keyLen)
}
context = nil
} else if opt.private != nil {
if secret, err = getSecret(opt); err != nil {
return nil, nil, err
}
context = newContext(opt)
} else if opt.keyID != nil {
secret = opt.keyMap(opt.keyID)
context = nil
}
if secret == nil {
return nil, nil, errors.New("unable to determine key")
}
debug.dumpBinary("secret", secret)
debug.dumpBinary("context", context)
if opt.authSecret == nil {
return secret, context, nil
}
debug.dumpBinary("hkdf secret", secret)
debug.dumpBinary("hkdf salt", opt.authSecret)
debug.dumpBinary("hkdf info", authInfo)
authSecret := make([]byte, secretLen)
_, err = hkdf.New(hashAlgorithm, secret, opt.authSecret, authInfo).Read(authSecret)
if err != nil {
return nil, nil, err
}
debug.dumpBinary("authsecret", authSecret)
return authSecret, context, nil
}
func extractSecret(opt *options) ([]byte, error) {
optKeyLen := len(opt.key)
if optKeyLen > 0 {
if optKeyLen != keyLen {
return nil, fmt.Errorf("an explicit Key must be %d bytes", keyLen)
}
return opt.key, nil
}
if opt.authSecret == nil {
return nil, errors.New("no authentication secret for webpush")
}
var secret []byte
var err error
if secret, err = getSecret(opt); err != nil {
return nil, err
}
sp, rp := getKeys(opt)
authInfo := append(append(webPushInfo, rp...), sp...)
debug.dumpBinary("hkdf ikm", secret)
debug.dumpBinary("hkdf salt", opt.authSecret)
debug.dumpBinary("hkdf info", authInfo)
newSecret := make([]byte, secretLen)
_, err = hkdf.New(hashAlgorithm, secret, opt.authSecret, authInfo).Read(newSecret)
if err != nil {
return nil, err
}
return newSecret, nil
}
func buildInfo(base []byte, context []byte) []byte {
baseLen := len(base)
contextLen := len(context)
result := make([]byte, 0, baseLen+contextLen)
result = append(result, base...)
if contextLen > 0 {
result = append(result, context...)
}
return result
}
func newContext(opt *options) []byte {
if opt.encoding == AESGCM {
// The context format is:
// KeyLabel || 0x00 ||
// length(receiverPublicKey) || receiverPublicKey ||
// length(senderPublicKey) || senderPublicKey
// The lengths are 16-bit, Big Endian, unsigned integers so take 2 bytes each.
keyLabelLen := len(opt.keyLabel)
sp, rp := getKeys(opt)
rplen := len(rp)
rplenbuf := uint16ToBytes(rplen)
splen := len(sp)
splenbuf := uint16ToBytes(splen)
ctx := make([]byte, keyLabelLen+1+2+rplen+2+splen)
copy(ctx, opt.keyLabel)
ctx[keyLabelLen] = 0x00
copy(ctx[keyLabelLen+1:], rplenbuf)
copy(ctx[keyLabelLen+3:], rp)
copy(ctx[keyLabelLen+3+rplen:], splenbuf)
copy(ctx[keyLabelLen+3+rplen+2:], sp)
return ctx
}
return nil
}
func randomKey(curve ecdh.Curve) (*ecdh.PrivateKey, error) {
return curve.GenerateKey(rand.Reader)
}
func getKeys(opt *options) (sp, rp []byte) {
if opt.mode == decrypt {
return opt.dh, opt.public
}
return opt.public, opt.dh
}
func getSecret(opt *options) (secret []byte, err error) {
var dh *ecdh.PublicKey
if dh, err = opt.curve.NewPublicKey(opt.dh); err != nil {
return nil, err
}
return opt.privateKey.ECDH(dh)
}