-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathderive_key.ts
More file actions
164 lines (139 loc) · 4.26 KB
/
derive_key.ts
File metadata and controls
164 lines (139 loc) · 4.26 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
import { test } from '../util';
import { expect } from 'chai';
import { subtle, getRandomValues } from 'react-native-quick-crypto';
import { CryptoKey } from 'react-native-quick-crypto';
import type { CryptoKeyPair } from 'react-native-quick-crypto';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const subtleAny = subtle as any;
const SUITE = 'subtle.deriveKey';
// Test 1: PBKDF2 deriveKey
test(SUITE, 'PBKDF2 deriveKey to AES-GCM', async () => {
const password = new TextEncoder().encode('my-password');
const salt = getRandomValues(new Uint8Array(16));
const baseKey = await subtle.importKey(
'raw',
password,
{ name: 'PBKDF2' },
false,
['deriveKey'],
);
const derivedKey = await subtleAny.deriveKey(
{
name: 'PBKDF2',
salt,
iterations: 100000,
hash: 'SHA-256',
},
baseKey as CryptoKey,
{ name: 'AES-GCM', length: 256 },
true,
['encrypt', 'decrypt'],
);
// Verify key can encrypt/decrypt
const plaintext = new Uint8Array([1, 2, 3, 4]);
const iv = getRandomValues(new Uint8Array(12));
const ciphertext = await subtle.encrypt(
{ name: 'AES-GCM', iv },
derivedKey as CryptoKey,
plaintext,
);
const decrypted = await subtle.decrypt(
{ name: 'AES-GCM', iv },
derivedKey as CryptoKey,
ciphertext,
);
expect(Buffer.from(decrypted).toString('hex')).to.equal(
Buffer.from(plaintext).toString('hex'),
);
});
// Test 2: X25519 deriveKey
test(SUITE, 'X25519 deriveKey to AES-GCM', async () => {
const aliceKeyPair = await subtle.generateKey({ name: 'X25519' }, false, [
'deriveKey',
'deriveBits',
]);
const bobKeyPair = await subtle.generateKey({ name: 'X25519' }, false, [
'deriveKey',
'deriveBits',
]);
const aliceDerivedKey = await subtleAny.deriveKey(
{
name: 'X25519',
public: (bobKeyPair as CryptoKeyPair).publicKey,
},
(aliceKeyPair as CryptoKeyPair).privateKey,
{ name: 'AES-GCM', length: 256 },
true,
['encrypt', 'decrypt'],
);
const bobDerivedKey = await subtleAny.deriveKey(
{
name: 'X25519',
public: (aliceKeyPair as CryptoKeyPair).publicKey,
},
(bobKeyPair as CryptoKeyPair).privateKey,
{ name: 'AES-GCM', length: 256 },
true,
['encrypt', 'decrypt'],
);
// Both should derive the same key
const aliceRaw = await subtle.exportKey('raw', aliceDerivedKey as CryptoKey);
const bobRaw = await subtle.exportKey('raw', bobDerivedKey as CryptoKey);
expect(Buffer.from(aliceRaw as ArrayBuffer).toString('hex')).to.equal(
Buffer.from(bobRaw as ArrayBuffer).toString('hex'),
);
});
// Test 3: ECDH deriveKey
test(SUITE, 'ECDH P-256 deriveKey to AES-GCM', async () => {
const aliceKeyPair = await subtle.generateKey(
{ name: 'ECDH', namedCurve: 'P-256' },
false,
['deriveKey', 'deriveBits'],
);
const bobKeyPair = await subtle.generateKey(
{ name: 'ECDH', namedCurve: 'P-256' },
false,
['deriveKey', 'deriveBits'],
);
const aliceDerivedKey = await subtleAny.deriveKey(
{
name: 'ECDH',
public: (aliceKeyPair as CryptoKeyPair).publicKey,
},
(bobKeyPair as CryptoKeyPair).privateKey,
{ name: 'AES-GCM', length: 256 },
true,
['encrypt', 'decrypt'],
);
const bobDerivedKey = await subtleAny.deriveKey(
{
name: 'ECDH',
public: (bobKeyPair as CryptoKeyPair).publicKey,
},
(aliceKeyPair as CryptoKeyPair).privateKey,
{ name: 'AES-GCM', length: 256 },
true,
['encrypt', 'decrypt'],
);
const aliceRaw = await subtle.exportKey('raw', aliceDerivedKey as CryptoKey);
const bobRaw = await subtle.exportKey('raw', bobDerivedKey as CryptoKey);
expect(Buffer.from(aliceRaw as ArrayBuffer).toString('hex')).to.equal(
Buffer.from(bobRaw as ArrayBuffer).toString('hex'),
);
// Verify key works for encrypt/decrypt
const plaintext = new Uint8Array([1, 2, 3, 4]);
const iv = getRandomValues(new Uint8Array(12));
const ciphertext = await subtle.encrypt(
{ name: 'AES-GCM', iv },
aliceDerivedKey as CryptoKey,
plaintext,
);
const decrypted = await subtle.decrypt(
{ name: 'AES-GCM', iv },
bobDerivedKey as CryptoKey,
ciphertext,
);
expect(Buffer.from(decrypted).toString('hex')).to.equal(
Buffer.from(plaintext).toString('hex'),
);
});