-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathhkdf.js
More file actions
178 lines (159 loc) · 4.93 KB
/
hkdf.js
File metadata and controls
178 lines (159 loc) · 4.93 KB
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
(() => {
// Represents the secret message that could be sent
const message = {
salt: null,
iv: null,
ciphertext: null,
};
let alicesSecretKey;
let bobsSecretKey;
/*
Fetch the contents of the "message" textbox, and encode it
in a form we can use for the encrypt operation.
*/
function getMessageEncoding() {
let message = document.querySelector("#hkdf-message").value;
let enc = new TextEncoder();
return enc.encode(message);
}
/*
Given some key material and some random salt,
derive an AES-GCM key using HKDF.
*/
function getKey(keyMaterial, salt) {
return window.crypto.subtle.deriveKey(
{
name: "HKDF",
salt,
info: new Uint8Array("Encryption example"),
hash: "SHA-256",
},
keyMaterial,
{ name: "AES-GCM", length: 256 },
true,
["encrypt", "decrypt"]
);
}
/*
Encrypt the message using the secret key.
Update the "ciphertextValue" box with a representation of part of
the ciphertext.
*/
async function encrypt(secret) {
const ciphertextValue = document.querySelector(".hkdf .ciphertext-value");
ciphertextValue.textContent = "";
const decryptedValue = document.querySelector(".hkdf .decrypted-value");
decryptedValue.textContent = "";
message.salt = window.crypto.getRandomValues(new Uint8Array(16));
let key = await getKey(secret, message.salt);
message.iv = window.crypto.getRandomValues(new Uint8Array(12));
let encoded = getMessageEncoding();
message.ciphertext = await window.crypto.subtle.encrypt(
{
name: "AES-GCM",
iv: message.iv,
},
key,
encoded
);
let buffer = new Uint8Array(message.ciphertext, 0, 5);
ciphertextValue.classList.add("fade-in");
ciphertextValue.addEventListener("animationend", () => {
ciphertextValue.classList.remove("fade-in");
});
ciphertextValue.textContent = `${buffer}...[${message.ciphertext.byteLength} bytes total]`;
}
/*
Decrypt the message using the secret key.
If the ciphertext was decrypted successfully,
update the "decryptedValue" box with the decrypted value.
If there was an error decrypting,
update the "decryptedValue" box with an error message.
*/
async function decrypt(secret) {
const decryptedValue = document.querySelector(".hkdf .decrypted-value");
decryptedValue.textContent = "";
decryptedValue.classList.remove("error");
let key = await getKey(secret, message.salt);
try {
let decrypted = await window.crypto.subtle.decrypt(
{
name: "AES-GCM",
iv: message.iv,
},
key,
message.ciphertext
);
let dec = new TextDecoder();
decryptedValue.classList.add("fade-in");
decryptedValue.addEventListener("animationend", () => {
decryptedValue.classList.remove("fade-in");
});
decryptedValue.textContent = dec.decode(decrypted);
} catch (e) {
decryptedValue.classList.add("error");
decryptedValue.textContent = "*** Decryption error ***";
}
}
/*
Derive a shared secret, given:
- our ECDH private key
- their ECDH public key
*/
async function deriveSharedSecret(privateKey, publicKey) {
const secret = await window.crypto.subtle.deriveBits(
{ name: "ECDH", public: publicKey },
privateKey,
384
);
return window.crypto.subtle.importKey(
"raw",
secret,
{ name: "HKDF" },
false,
["deriveKey"]
);
}
async function agreeSharedSecretKey() {
// Generate 2 ECDH key pairs: one for Alice and one for Bob
// In more normal usage, they would generate their key pairs
// separately and exchange public keys securely
let alicesKeyPair = await window.crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "P-384",
},
false,
["deriveBits"]
);
let bobsKeyPair = await window.crypto.subtle.generateKey(
{
name: "ECDH",
namedCurve: "P-384",
},
false,
["deriveBits"]
);
// Alice then generates a secret key using her private key and Bob's public key.
alicesSecretKey = await deriveSharedSecret(
alicesKeyPair.privateKey,
bobsKeyPair.publicKey
);
// Bob generates the same secret key using his private key and Alice's public key.
bobsSecretKey = await deriveSharedSecret(
bobsKeyPair.privateKey,
alicesKeyPair.publicKey
);
// Alice can then use her copy of the secret key to encrypt a message to Bob.
let encryptButton = document.querySelector(".hkdf .encrypt-button");
encryptButton.addEventListener("click", () => {
encrypt(alicesSecretKey);
});
// Bob can use his copy to decrypt the message.
let decryptButton = document.querySelector(".hkdf .decrypt-button");
decryptButton.addEventListener("click", () => {
decrypt(bobsSecretKey);
});
}
agreeSharedSecretKey();
})();