src: don't error at startup when cwd doesn't exist
[platform/upstream/nodejs.git] / test / parallel / test-crypto-authenticated.js
1 var common = require('../common');
2 var assert = require('assert');
3
4 if (!common.hasCrypto) {
5   console.log('1..0 # Skipped: missing crypto');
6   process.exit();
7 }
8 var crypto = require('crypto');
9
10 crypto.DEFAULT_ENCODING = 'buffer';
11
12 //
13 // Test authenticated encryption modes.
14 //
15 // !NEVER USE STATIC IVs IN REAL LIFE!
16 //
17
18 var TEST_CASES = [
19   { algo: 'aes-128-gcm', key: '6970787039613669314d623455536234',
20     iv: '583673497131313748307652', plain: 'Hello World!',
21     ct: '4BE13896F64DFA2C2D0F2C76',
22     tag: '272B422F62EB545EAA15B5FF84092447', tampered: false },
23   { algo: 'aes-128-gcm', key: '6970787039613669314d623455536234',
24     iv: '583673497131313748307652', plain: 'Hello World!',
25     ct: '4BE13896F64DFA2C2D0F2C76', aad: '000000FF',
26     tag: 'BA2479F66275665A88CB7B15F43EB005', tampered: false },
27   { algo: 'aes-128-gcm', key: '6970787039613669314d623455536234',
28     iv: '583673497131313748307652', plain: 'Hello World!',
29     ct: '4BE13596F64DFA2C2D0FAC76',
30     tag: '272B422F62EB545EAA15B5FF84092447', tampered: true },
31   { algo: 'aes-256-gcm', key: '337a54767a7233703637564336316a6d56353472495975313534357834546c59',
32     iv: '36306950306836764a6f4561', plain: 'Hello node.js world!',
33     ct: '58E62CFE7B1D274111A82267EBB93866E72B6C2A',
34     tag: '9BB44F663BADABACAE9720881FB1EC7A', tampered: false },
35   { algo: 'aes-256-gcm', key: '337a54767a7233703637564336316a6d56353472495975313534357834546c59',
36     iv: '36306950306836764a6f4561', plain: 'Hello node.js world!',
37     ct: '58E62CFF7B1D274011A82267EBB93866E72B6C2B',
38     tag: '9BB44F663BADABACAE9720881FB1EC7A', tampered: true },
39   { algo: 'aes-192-gcm', key: '1ed2233fa2223ef5d7df08546049406c7305220bca40d4c9',
40     iv: '0e1791e9db3bd21a9122c416', plain: 'Hello node.js world!',
41     password: 'very bad password', aad: '63616c76696e',
42     ct: 'DDA53A4059AA17B88756984995F7BBA3C636CC44',
43     tag: 'D2A35E5C611E5E3D2258360241C5B045', tampered: false }
44 ];
45
46 var ciphers = crypto.getCiphers();
47
48 for (var i in TEST_CASES) {
49   var test = TEST_CASES[i];
50
51   if (ciphers.indexOf(test.algo) == -1) {
52     console.log('skipping unsupported ' + test.algo + ' test');
53     continue;
54   }
55
56   (function() {
57     var encrypt = crypto.createCipheriv(test.algo,
58       new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
59     if (test.aad)
60       encrypt.setAAD(new Buffer(test.aad, 'hex'));
61     var hex = encrypt.update(test.plain, 'ascii', 'hex');
62     hex += encrypt.final('hex');
63     var auth_tag = encrypt.getAuthTag();
64     // only test basic encryption run if output is marked as tampered.
65     if (!test.tampered) {
66       assert.equal(hex.toUpperCase(), test.ct);
67       assert.equal(auth_tag.toString('hex').toUpperCase(), test.tag);
68     }
69   })();
70
71   (function() {
72     var decrypt = crypto.createDecipheriv(test.algo,
73       new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
74     decrypt.setAuthTag(new Buffer(test.tag, 'hex'));
75     if (test.aad)
76       decrypt.setAAD(new Buffer(test.aad, 'hex'));
77     var msg = decrypt.update(test.ct, 'hex', 'ascii');
78     if (!test.tampered) {
79       msg += decrypt.final('ascii');
80       assert.equal(msg, test.plain);
81     } else {
82       // assert that final throws if input data could not be verified!
83       assert.throws(function() { decrypt.final('ascii'); }, / auth/);
84     }
85   })();
86
87   (function() {
88     if (!test.password) return;
89     var encrypt = crypto.createCipher(test.algo, test.password);
90     if (test.aad)
91       encrypt.setAAD(new Buffer(test.aad, 'hex'));
92     var hex = encrypt.update(test.plain, 'ascii', 'hex');
93     hex += encrypt.final('hex');
94     var auth_tag = encrypt.getAuthTag();
95     // only test basic encryption run if output is marked as tampered.
96     if (!test.tampered) {
97       assert.equal(hex.toUpperCase(), test.ct);
98       assert.equal(auth_tag.toString('hex').toUpperCase(), test.tag);
99     }
100   })();
101
102   (function() {
103     if (!test.password) return;
104     var decrypt = crypto.createDecipher(test.algo, test.password);
105     decrypt.setAuthTag(new Buffer(test.tag, 'hex'));
106     if (test.aad)
107       decrypt.setAAD(new Buffer(test.aad, 'hex'));
108     var msg = decrypt.update(test.ct, 'hex', 'ascii');
109     if (!test.tampered) {
110       msg += decrypt.final('ascii');
111       assert.equal(msg, test.plain);
112     } else {
113       // assert that final throws if input data could not be verified!
114       assert.throws(function() { decrypt.final('ascii'); }, / auth/);
115     }
116   })();
117
118   // after normal operation, test some incorrect ways of calling the API:
119   // it's most certainly enough to run these tests with one algorithm only.
120
121   if (i > 0) {
122     continue;
123   }
124
125   (function() {
126     // non-authenticating mode:
127     var encrypt = crypto.createCipheriv('aes-128-cbc',
128       'ipxp9a6i1Mb4USb4', '6fKjEjR3Vl30EUYC');
129     encrypt.update('blah', 'ascii');
130     encrypt.final();
131     assert.throws(function() { encrypt.getAuthTag(); }, / state/);
132     assert.throws(function() {
133       encrypt.setAAD(new Buffer('123', 'ascii')); }, / state/);
134   })();
135
136   (function() {
137     // trying to get tag before inputting all data:
138     var encrypt = crypto.createCipheriv(test.algo,
139       new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
140     encrypt.update('blah', 'ascii');
141     assert.throws(function() { encrypt.getAuthTag(); }, / state/);
142   })();
143
144   (function() {
145     // trying to set tag on encryption object:
146     var encrypt = crypto.createCipheriv(test.algo,
147       new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
148     assert.throws(function() {
149       encrypt.setAuthTag(new Buffer(test.tag, 'hex')); }, / state/);
150   })();
151
152   (function() {
153     // trying to read tag from decryption object:
154     var decrypt = crypto.createDecipheriv(test.algo,
155       new Buffer(test.key, 'hex'), new Buffer(test.iv, 'hex'));
156     assert.throws(function() { decrypt.getAuthTag(); }, / state/);
157   })();
158 }