-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathrfc8291Ikm.js
80 lines (75 loc) · 1.62 KB
/
rfc8291Ikm.js
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
// Key derivation as per RFC 8291 (for sending encrypted push notifications)
export default async (uaPublic: Uint8Array, salt: Uint8Array): Promise<[ArrayBuffer, ArrayBuffer]> => {
const [[asPrivateKey, asPublic], uaPublicKey] = await Promise.all([
crypto.subtle.generateKey(
{
name: 'ECDH',
namedCurve: 'P-256'
},
false,
['deriveKey']
).then(async (asKeyPair) => {
const asPublic = await crypto.subtle.exportKey(
'raw',
asKeyPair.publicKey
)
return [asKeyPair.privateKey, asPublic]
}),
crypto.subtle.importKey(
'raw',
uaPublic,
{ name: 'ECDH', namedCurve: 'P-256' },
false,
[]
)
])
const ecdhSecret = await crypto.subtle.deriveKey(
{
name: 'ECDH',
public: uaPublicKey
},
asPrivateKey,
{
name: 'HKDF',
hash: 'SHA-256'
},
false,
['deriveBits']
)
// The `WebPush: info\x00` string
const infoString = new Uint8Array([
0x57,
0x65,
0x62,
0x50,
0x75,
0x73,
0x68,
0x3a,
0x20,
0x69,
0x6e,
0x66,
0x6f,
0x00
])
const info = new Uint8Array(infoString.byteLength + uaPublic.byteLength + asPublic.byteLength)
info.set(infoString, 0)
info.set(uaPublic, infoString.byteLength)
info.set(
new Uint8Array(asPublic),
infoString.byteLength + uaPublic.byteLength
)
const IKM = await crypto.subtle.deriveBits(
{
name: 'HKDF',
hash: 'SHA-256',
salt,
info
},
ecdhSecret,
32 << 3
)
// Role in RFC8188: `asPublic` is used as key ID, IKM as IKM.
return [asPublic, IKM]
}