forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest-crypto-authenticated-stream.js
More file actions
146 lines (120 loc) · 4.19 KB
/
Copy pathtest-crypto-authenticated-stream.js
File metadata and controls
146 lines (120 loc) · 4.19 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
'use strict';
// Refs: https://github.com/nodejs/node/issues/31733
const common = require('../common');
if (!common.hasCrypto)
common.skip('missing crypto');
const assert = require('assert');
const crypto = require('crypto');
const fs = require('fs');
const stream = require('stream');
const tmpdir = require('../common/tmpdir');
class Sink extends stream.Writable {
constructor() {
super();
this.chunks = [];
}
_write(chunk, encoding, cb) {
this.chunks.push(chunk);
cb();
}
}
function direct(config) {
const { cipher, key, iv, aad, authTagLength, plaintextLength } = config;
const expected = Buffer.alloc(plaintextLength);
const c = crypto.createCipheriv(cipher, key, iv, { authTagLength });
c.setAAD(aad, { plaintextLength });
const ciphertext = Buffer.concat([c.update(expected), c.final()]);
const d = crypto.createDecipheriv(cipher, key, iv, { authTagLength });
d.setAAD(aad, { plaintextLength });
d.setAuthTag(c.getAuthTag());
const actual = Buffer.concat([d.update(ciphertext), d.final()]);
assert.deepStrictEqual(expected, actual);
}
function mstream(config) {
const { cipher, key, iv, aad, authTagLength, plaintextLength } = config;
const expected = Buffer.alloc(plaintextLength);
const c = crypto.createCipheriv(cipher, key, iv, { authTagLength });
c.setAAD(aad, { plaintextLength });
const plain = new stream.PassThrough();
const crypt = new Sink();
const chunks = crypt.chunks;
plain.pipe(c).pipe(crypt);
plain.end(expected);
crypt.on('close', common.mustCall(() => {
const d = crypto.createDecipheriv(cipher, key, iv, { authTagLength });
d.setAAD(aad, { plaintextLength });
d.setAuthTag(c.getAuthTag());
const crypt = new stream.PassThrough();
const plain = new Sink();
crypt.pipe(d).pipe(plain);
for (const chunk of chunks) crypt.write(chunk);
crypt.end();
plain.on('close', common.mustCall(() => {
const actual = Buffer.concat(plain.chunks);
assert.deepStrictEqual(expected, actual);
}));
}));
}
function fstream(config) {
const count = fstream.count++;
const filename = (name) => tmpdir.resolve(`${name}${count}`);
const { cipher, key, iv, aad, authTagLength, plaintextLength } = config;
const expected = Buffer.alloc(plaintextLength);
fs.writeFileSync(filename('a'), expected);
const c = crypto.createCipheriv(cipher, key, iv, { authTagLength });
c.setAAD(aad, { plaintextLength });
const plain = fs.createReadStream(filename('a'));
const crypt = fs.createWriteStream(filename('b'));
plain.pipe(c).pipe(crypt);
// Observation: 'close' comes before 'end' on |c|, which definitely feels
// wrong. Switching to `c.on('end', ...)` doesn't fix the test though.
crypt.on('close', common.mustCall(() => {
// Just to drive home the point that decryption does actually work:
// reading the file synchronously, then decrypting it, works.
{
const ciphertext = fs.readFileSync(filename('b'));
const d = crypto.createDecipheriv(cipher, key, iv, { authTagLength });
d.setAAD(aad, { plaintextLength });
d.setAuthTag(c.getAuthTag());
const actual = Buffer.concat([d.update(ciphertext), d.final()]);
assert.deepStrictEqual(expected, actual);
}
const d = crypto.createDecipheriv(cipher, key, iv, { authTagLength });
d.setAAD(aad, { plaintextLength });
d.setAuthTag(c.getAuthTag());
const crypt = fs.createReadStream(filename('b'));
const plain = fs.createWriteStream(filename('c'));
crypt.pipe(d).pipe(plain);
plain.on('close', common.mustCall(() => {
const actual = fs.readFileSync(filename('c'));
assert.deepStrictEqual(expected, actual);
}));
}));
}
fstream.count = 0;
function test(config) {
if (!crypto.getCiphers().includes(config.cipher)) {
common.printSkipMessage(`unsupported cipher: ${config.cipher}`);
return;
}
direct(config);
mstream(config);
fstream(config);
}
tmpdir.refresh();
test({
cipher: 'aes-128-ccm',
aad: Buffer.alloc(1),
iv: Buffer.alloc(8),
key: Buffer.alloc(16),
authTagLength: 16,
plaintextLength: 32768,
});
test({
cipher: 'aes-128-ccm',
aad: Buffer.alloc(1),
iv: Buffer.alloc(8),
key: Buffer.alloc(16),
authTagLength: 16,
plaintextLength: 32769,
});