|
| 1 | +// Copyright Joyent, Inc. and other Node contributors. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the |
| 5 | +// "Software"), to deal in the Software without restriction, including |
| 6 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 7 | +// distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +// persons to whom the Software is furnished to do so, subject to the |
| 9 | +// following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included |
| 12 | +// in all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 17 | +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 | +// USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +var common = require('../common'); |
| 26 | +var assert = require('assert'); |
| 27 | + |
| 28 | +try { |
| 29 | + var crypto = require('crypto'); |
| 30 | +} catch (e) { |
| 31 | + console.log('Not compiled with OPENSSL support.'); |
| 32 | + process.exit(); |
| 33 | +} |
| 34 | + |
| 35 | +crypto.DEFAULT_ENCODING = 'buffer'; |
| 36 | + |
| 37 | +// |
| 38 | +// Test authenticated encryption modes. |
| 39 | +// |
| 40 | +// !NEVER USE STATIC IVs IN REAL LIFE! |
| 41 | +// |
| 42 | + |
| 43 | +var TEST_CASES = [ |
| 44 | + { algo: 'aes-128-gcm', key: 'ipxp9a6i1Mb4USb4', iv: 'X6sIq117H0vR', |
| 45 | + plain: 'Hello World!', ct: '4BE13896F64DFA2C2D0F2C76', |
| 46 | + tag: '272B422F62EB545EAA15B5FF84092447', tampered: false }, |
| 47 | + { algo: 'aes-128-gcm', key: 'ipxp9a6i1Mb4USb4', iv: 'X6sIq117H0vR', |
| 48 | + plain: 'Hello World!', ct: '4BE13596F64DFA2C2D0FAC76', |
| 49 | + tag: '272B422F62EB545EAA15B5FF84092447', tampered: true }, |
| 50 | + { algo: 'aes-256-gcm', key: '3zTvzr3p67VC61jmV54rIYu1545x4TlY', |
| 51 | + iv: '60iP0h6vJoEa', plain: 'Hello node.js world!', |
| 52 | + ct: '58E62CFE7B1D274111A82267EBB93866E72B6C2A', |
| 53 | + tag: '9BB44F663BADABACAE9720881FB1EC7A', tampered: false }, |
| 54 | + { algo: 'aes-256-gcm', key: '3zTvzr3p67VC61jmV54rIYu1545x4TlY', |
| 55 | + iv: '60iP0h6vJoEa', plain: 'Hello node.js world!', |
| 56 | + ct: '58E62CFF7B1D274011A82267EBB93866E72B6C2B', |
| 57 | + tag: '9BB44F663BADABACAE9720881FB1EC7A', tampered: true }, |
| 58 | +]; |
| 59 | + |
| 60 | +var ciphers = crypto.getCiphers(); |
| 61 | + |
| 62 | +for (var i in TEST_CASES) { |
| 63 | + var test = TEST_CASES[i]; |
| 64 | + |
| 65 | + if (ciphers.indexOf(test.algo) == -1) { |
| 66 | + console.log('skipping unsupported ' + test.algo + ' test'); |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + (function() { |
| 71 | + var encrypt = crypto.createCipheriv(test.algo, test.key, test.iv); |
| 72 | + var hex = encrypt.update(test.plain, 'ascii', 'hex'); |
| 73 | + hex += encrypt.final('hex'); |
| 74 | + var auth_tag = encrypt.getAuthTag(); |
| 75 | + // only test basic encryption run if output is marked as tampered. |
| 76 | + if (!test.tampered) { |
| 77 | + assert.equal(hex.toUpperCase(), test.ct); |
| 78 | + assert.equal(auth_tag.toString('hex').toUpperCase(), test.tag); |
| 79 | + } |
| 80 | + })(); |
| 81 | + |
| 82 | + (function() { |
| 83 | + var decrypt = crypto.createDecipheriv(test.algo, test.key, test.iv); |
| 84 | + decrypt.setAuthTag(new Buffer(test.tag, 'hex')); |
| 85 | + var msg = decrypt.update(test.ct, 'hex', 'ascii'); |
| 86 | + if (!test.tampered) { |
| 87 | + msg += decrypt.final('ascii'); |
| 88 | + assert.equal(msg, test.plain); |
| 89 | + } else { |
| 90 | + // assert that final throws if input data could not be verified! |
| 91 | + assert.throws(function() { decrypt.final('ascii'); }); |
| 92 | + } |
| 93 | + })(); |
| 94 | + |
| 95 | + // after normal operation, test some incorrect ways of calling the API: |
| 96 | + // it's most certainly enough to run these tests with one algorithm only. |
| 97 | + |
| 98 | + if (i > 0) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + (function() { |
| 103 | + // non-authenticating mode: |
| 104 | + var encrypt = crypto.createCipheriv('aes-128-cbc', |
| 105 | + 'ipxp9a6i1Mb4USb4', '6fKjEjR3Vl30EUYC'); |
| 106 | + encrypt.update('blah', 'ascii'); |
| 107 | + encrypt.final(); |
| 108 | + assert.throws(function() { encrypt.getAuthTag(); }); |
| 109 | + })(); |
| 110 | + |
| 111 | + (function() { |
| 112 | + // trying to get tag before inputting all data: |
| 113 | + var encrypt = crypto.createCipheriv(test.algo, test.key, test.iv); |
| 114 | + encrypt.update('blah', 'ascii'); |
| 115 | + assert.throws(function() { encrypt.getAuthTag(); }); |
| 116 | + })(); |
| 117 | + |
| 118 | + (function() { |
| 119 | + // trying to set tag on encryption object: |
| 120 | + var encrypt = crypto.createCipheriv(test.algo, test.key, test.iv); |
| 121 | + assert.throws(function() { |
| 122 | + encrypt.setAuthTag(new Buffer(test.tag, 'hex')); }); |
| 123 | + })(); |
| 124 | + |
| 125 | + (function() { |
| 126 | + // trying to read tag from decryption object: |
| 127 | + var decrypt = crypto.createDecipheriv(test.algo, test.key, test.iv); |
| 128 | + assert.throws(function() { decrypt.getAuthTag(); }); |
| 129 | + })(); |
| 130 | +} |
0 commit comments