-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathencap_decap.ts
More file actions
162 lines (129 loc) · 4.74 KB
/
encap_decap.ts
File metadata and controls
162 lines (129 loc) · 4.74 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
import { expect } from 'chai';
import { test } from '../util';
import crypto from 'react-native-quick-crypto';
import type { WebCryptoKeyPair } from 'react-native-quick-crypto';
import {
MLKEM_VARIANTS,
MLKEM_CIPHERTEXT_SIZES,
SHARED_SECRET_SIZE,
} from './mlkem_constants';
const { subtle } = crypto;
const SUITE = 'subtle.encapsulate/decapsulate';
// --- encapsulateBits / decapsulateBits ---
for (const variant of MLKEM_VARIANTS) {
test(SUITE, `${variant} encapsulateBits/decapsulateBits`, async () => {
const keyPair = await subtle.generateKey({ name: variant }, true, [
'encapsulateBits',
'decapsulateBits',
]);
const { publicKey, privateKey } = keyPair as WebCryptoKeyPair;
const { sharedKey, ciphertext } = await subtle.encapsulateBits(
{ name: variant },
publicKey,
);
expect(ciphertext.byteLength).to.equal(MLKEM_CIPHERTEXT_SIZES[variant]);
expect(sharedKey.byteLength).to.equal(SHARED_SECRET_SIZE);
const decapsulated = await subtle.decapsulateBits(
{ name: variant },
privateKey,
ciphertext,
);
expect(decapsulated.byteLength).to.equal(SHARED_SECRET_SIZE);
const encapsulatedBytes = new Uint8Array(sharedKey);
const decapsulatedBytes = new Uint8Array(decapsulated);
expect(encapsulatedBytes).to.deep.equal(decapsulatedBytes);
});
// --- encapsulateKey / decapsulateKey with AES-GCM ---
test(SUITE, `${variant} encapsulateKey/decapsulateKey AES-GCM`, async () => {
const keyPair = await subtle.generateKey({ name: variant }, true, [
'encapsulateKey',
'decapsulateKey',
]);
const { publicKey, privateKey } = keyPair as WebCryptoKeyPair;
const { key: aesKey, ciphertext } = await subtle.encapsulateKey(
{ name: variant },
publicKey,
{ name: 'AES-GCM', length: 256 },
true,
['encrypt', 'decrypt'],
);
expect(ciphertext.byteLength).to.equal(MLKEM_CIPHERTEXT_SIZES[variant]);
expect(aesKey.algorithm.name).to.equal('AES-GCM');
expect(aesKey.extractable).to.equal(true);
expect(aesKey.usages).to.include('encrypt');
const decapsulatedKey = await subtle.decapsulateKey(
{ name: variant },
privateKey,
ciphertext,
{ name: 'AES-GCM', length: 256 },
true,
['encrypt', 'decrypt'],
);
expect(decapsulatedKey.algorithm.name).to.equal('AES-GCM');
const rawEncapsulated = await subtle.exportKey('raw', aesKey);
const rawDecapsulated = await subtle.exportKey('raw', decapsulatedKey);
expect(new Uint8Array(rawEncapsulated as ArrayBuffer)).to.deep.equal(
new Uint8Array(rawDecapsulated as ArrayBuffer),
);
});
// --- Import then encapsulate/decapsulate roundtrip ---
test(SUITE, `${variant} import then encap/decap`, async () => {
const keyPair = await subtle.generateKey({ name: variant }, true, [
'encapsulateBits',
'decapsulateBits',
]);
const { publicKey, privateKey } = keyPair as WebCryptoKeyPair;
const spki = (await subtle.exportKey('spki', publicKey)) as ArrayBuffer;
const pkcs8 = (await subtle.exportKey('pkcs8', privateKey)) as ArrayBuffer;
const importedPub = await subtle.importKey(
'spki',
spki,
{ name: variant },
true,
['encapsulateBits'],
);
const importedPriv = await subtle.importKey(
'pkcs8',
pkcs8,
{ name: variant },
true,
['decapsulateBits'],
);
const { sharedKey, ciphertext } = await subtle.encapsulateBits(
{ name: variant },
importedPub,
);
const decapsulated = await subtle.decapsulateBits(
{ name: variant },
importedPriv,
ciphertext,
);
expect(new Uint8Array(sharedKey)).to.deep.equal(
new Uint8Array(decapsulated),
);
});
}
// --- Top-level crypto.encapsulate/decapsulate ---
for (const variant of MLKEM_VARIANTS) {
test(SUITE, `${variant} crypto.encapsulate/decapsulate`, async () => {
const keyPair = await subtle.generateKey({ name: variant }, true, [
'encapsulateBits',
'decapsulateBits',
]);
const { publicKey, privateKey } = keyPair as WebCryptoKeyPair;
const result = crypto.encapsulate(publicKey);
expect(result).to.have.property('sharedKey');
expect(result).to.have.property('ciphertext');
const { sharedKey, ciphertext } = result!;
expect(ciphertext.byteLength).to.equal(MLKEM_CIPHERTEXT_SIZES[variant]);
expect(sharedKey.byteLength).to.equal(SHARED_SECRET_SIZE);
const decapsulated = crypto.decapsulate(privateKey, ciphertext);
expect(decapsulated).to.be.an.instanceOf(ArrayBuffer);
expect((decapsulated as ArrayBuffer).byteLength).to.equal(
SHARED_SECRET_SIZE,
);
expect(new Uint8Array(sharedKey)).to.deep.equal(
new Uint8Array(decapsulated as ArrayBuffer),
);
});
}