-
Notifications
You must be signed in to change notification settings - Fork 823
Expand file tree
/
Copy pathcrypto.test.js
More file actions
229 lines (203 loc) · 7.54 KB
/
Copy pathcrypto.test.js
File metadata and controls
229 lines (203 loc) · 7.54 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
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
218
219
220
221
222
223
224
225
226
227
228
229
const { describe, it } = require('node:test');
const assert = require('node:assert/strict');
const crypto = require('crypto');
const {
ALGORITHM,
KEY_BYTES,
IV_BYTES,
TAG_BYTES,
generateKey,
encrypt,
decrypt,
pack,
unpack,
} = require('../src/gep/crypto');
describe('constants', () => {
it('exposes AES-256-GCM algorithm', () => {
assert.equal(ALGORITHM, 'aes-256-gcm');
});
it('defines 32-byte keys, 12-byte IVs, 16-byte tags', () => {
assert.equal(KEY_BYTES, 32);
assert.equal(IV_BYTES, 12);
assert.equal(TAG_BYTES, 16);
});
});
describe('generateKey', () => {
it('returns a 32-byte Buffer', () => {
const k = generateKey();
assert.ok(Buffer.isBuffer(k));
assert.equal(k.length, KEY_BYTES);
});
it('returns different keys on successive calls', () => {
const a = generateKey();
const b = generateKey();
assert.notEqual(a.toString('hex'), b.toString('hex'));
});
});
describe('encrypt / decrypt round-trip', () => {
it('round-trips an ASCII string', () => {
const key = generateKey();
const plaintext = 'hello evolver';
const parts = encrypt(plaintext, key);
const recovered = decrypt(parts.ciphertext, key, parts.iv, parts.authTag);
assert.equal(recovered.toString('utf8'), plaintext);
});
it('round-trips a multi-byte UTF-8 string', () => {
const key = generateKey();
const plaintext = 'evolver 演进 🧬 multi-byte';
const parts = encrypt(plaintext, key);
const recovered = decrypt(parts.ciphertext, key, parts.iv, parts.authTag);
assert.equal(recovered.toString('utf8'), plaintext);
});
it('round-trips a binary Buffer', () => {
const key = generateKey();
const plaintext = crypto.randomBytes(1024);
const parts = encrypt(plaintext, key);
const recovered = decrypt(parts.ciphertext, key, parts.iv, parts.authTag);
assert.equal(Buffer.compare(recovered, plaintext), 0);
});
it('round-trips an empty buffer', () => {
const key = generateKey();
const parts = encrypt(Buffer.alloc(0), key);
const recovered = decrypt(parts.ciphertext, key, parts.iv, parts.authTag);
assert.equal(recovered.length, 0);
});
it('produces a fresh IV per call (non-deterministic ciphertext)', () => {
const key = generateKey();
const a = encrypt('same input', key);
const b = encrypt('same input', key);
assert.notEqual(a.iv.toString('hex'), b.iv.toString('hex'));
assert.notEqual(a.ciphertext.toString('hex'), b.ciphertext.toString('hex'));
});
it('returns components with expected lengths', () => {
const key = generateKey();
const { iv, authTag, ciphertext } = encrypt('abc', key);
assert.equal(iv.length, IV_BYTES);
assert.equal(authTag.length, TAG_BYTES);
assert.equal(ciphertext.length, Buffer.byteLength('abc', 'utf8'));
});
});
describe('encrypt key validation', () => {
it('rejects missing key', () => {
assert.throws(() => encrypt('x', null), /key must be exactly 32 bytes/);
});
it('rejects too-short key', () => {
assert.throws(() => encrypt('x', Buffer.alloc(16)), /key must be exactly 32 bytes/);
});
it('rejects too-long key', () => {
assert.throws(() => encrypt('x', Buffer.alloc(64)), /key must be exactly 32 bytes/);
});
});
describe('decrypt key validation', () => {
it('rejects missing key', () => {
const key = generateKey();
const parts = encrypt('x', key);
assert.throws(
() => decrypt(parts.ciphertext, null, parts.iv, parts.authTag),
/key must be exactly 32 bytes/
);
});
it('rejects wrong-size key', () => {
const key = generateKey();
const parts = encrypt('x', key);
assert.throws(
() => decrypt(parts.ciphertext, Buffer.alloc(16), parts.iv, parts.authTag),
/key must be exactly 32 bytes/
);
});
});
describe('decrypt authentication', () => {
it('fails when ciphertext is tampered', () => {
const key = generateKey();
const parts = encrypt('trusted payload', key);
const tampered = Buffer.from(parts.ciphertext);
tampered[0] ^= 0xff;
assert.throws(() => decrypt(tampered, key, parts.iv, parts.authTag));
});
it('fails when auth tag is tampered', () => {
const key = generateKey();
const parts = encrypt('trusted payload', key);
const tag = Buffer.from(parts.authTag);
tag[0] ^= 0xff;
assert.throws(() => decrypt(parts.ciphertext, key, parts.iv, tag));
});
it('fails when decrypted with a different key', () => {
const keyA = generateKey();
const keyB = generateKey();
const parts = encrypt('secret', keyA);
assert.throws(() => decrypt(parts.ciphertext, keyB, parts.iv, parts.authTag));
});
it('fails when IV is wrong', () => {
const key = generateKey();
const parts = encrypt('secret', key);
const badIv = crypto.randomBytes(IV_BYTES);
assert.throws(() => decrypt(parts.ciphertext, key, badIv, parts.authTag));
});
// #285: with authTagLength pinned to TAG_BYTES on createDecipheriv, a
// truncated authentication tag must be rejected outright rather than
// accepted as a weaker authenticator. Without the explicit length, Node's
// GCM decipher would accept short tags.
it('rejects a truncated auth tag (authTagLength enforcement, #285)', () => {
const key = generateKey();
const parts = encrypt('trusted payload', key);
const truncated = parts.authTag.subarray(0, 8); // 8 of 16 bytes
assert.throws(
() => decrypt(parts.ciphertext, key, parts.iv, truncated),
/authentication tag length/i,
'an 8-byte tag must be rejected, not accepted as a weaker authenticator'
);
});
});
describe('pack / unpack', () => {
it('pack produces iv || authTag || ciphertext layout', () => {
const key = generateKey();
const parts = encrypt('payload', key);
const packed = pack(parts);
assert.ok(Buffer.isBuffer(packed));
assert.equal(packed.length, IV_BYTES + TAG_BYTES + parts.ciphertext.length);
assert.equal(
packed.subarray(0, IV_BYTES).toString('hex'),
parts.iv.toString('hex')
);
assert.equal(
packed.subarray(IV_BYTES, IV_BYTES + TAG_BYTES).toString('hex'),
parts.authTag.toString('hex')
);
});
it('unpack reverses pack', () => {
const key = generateKey();
const parts = encrypt('payload', key);
const roundTripped = unpack(pack(parts));
assert.equal(roundTripped.iv.toString('hex'), parts.iv.toString('hex'));
assert.equal(roundTripped.authTag.toString('hex'), parts.authTag.toString('hex'));
assert.equal(
roundTripped.ciphertext.toString('hex'),
parts.ciphertext.toString('hex')
);
});
it('pack + unpack + decrypt composes with encrypt', () => {
const key = generateKey();
const plaintext = 'end-to-end transport test';
const packed = pack(encrypt(plaintext, key));
const parts = unpack(packed);
const recovered = decrypt(parts.ciphertext, key, parts.iv, parts.authTag);
assert.equal(recovered.toString('utf8'), plaintext);
});
it('unpack rejects non-Buffer input', () => {
assert.throws(() => unpack('not a buffer'), /packed buffer too short/);
assert.throws(() => unpack(null), /packed buffer too short/);
});
it('unpack rejects buffer smaller than iv + tag + 1', () => {
assert.throws(
() => unpack(Buffer.alloc(IV_BYTES + TAG_BYTES)),
/packed buffer too short/
);
});
it('unpack accepts minimal-size buffer (1 ciphertext byte)', () => {
const buf = Buffer.alloc(IV_BYTES + TAG_BYTES + 1);
const parts = unpack(buf);
assert.equal(parts.iv.length, IV_BYTES);
assert.equal(parts.authTag.length, TAG_BYTES);
assert.equal(parts.ciphertext.length, 1);
});
});