| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 788541b commit def681a
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3054,6 +3054,11 @@ void CipherBase::Init(const char* cipher_type, | |||
| 3054 | 3054 | int key_buf_len) { | |
| 3055 | 3055 | HandleScope scope(env()->isolate()); | |
| 3056 | 3056 | ||
| 3057 | + #ifdef NODE_FIPS_MODE | ||
| 3058 | + return env()->ThrowError( | ||
| 3059 | + "crypto.createCipher() is not supported in FIPS mode."); | ||
| 3060 | + #endif // NODE_FIPS_MODE | ||
| 3061 | + | ||
| 3057 | 3062 | CHECK_EQ(cipher_, nullptr); | |
| 3058 | 3063 | cipher_ = EVP_get_cipherbyname(cipher_type); | |
| 3059 | 3064 | if (cipher_ == nullptr) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -93,32 +93,44 @@ for (var i in TEST_CASES) { | |||
| 93 | 93 | ||
| 94 | 94 | (function() { | |
| 95 | 95 | if (!test.password) return; | |
| 96 | - var encrypt = crypto.createCipher(test.algo, test.password); | ||
| 97 | - if (test.aad) | ||
| 98 | - encrypt.setAAD(new Buffer(test.aad, 'hex')); | ||
| 99 | - var hex = encrypt.update(test.plain, 'ascii', 'hex'); | ||
| 100 | - hex += encrypt.final('hex'); | ||
| 101 | - var auth_tag = encrypt.getAuthTag(); | ||
| 102 | - // only test basic encryption run if output is marked as tampered. | ||
| 103 | - if (!test.tampered) { | ||
| 104 | - assert.equal(hex.toUpperCase(), test.ct); | ||
| 105 | - assert.equal(auth_tag.toString('hex').toUpperCase(), test.tag); | ||
| 96 | + if (common.hasFipsCrypto) { | ||
| 97 | + assert.throws(function() | ||
| 98 | + { crypto.createCipher(test.algo, test.password); }, | ||
| 99 | + /not supported in FIPS mode/); | ||
| 100 | + } else { | ||
| 101 | + var encrypt = crypto.createCipher(test.algo, test.password); | ||
| 102 | + if (test.aad) | ||
| 103 | + encrypt.setAAD(new Buffer(test.aad, 'hex')); | ||
| 104 | + var hex = encrypt.update(test.plain, 'ascii', 'hex'); | ||
| 105 | + hex += encrypt.final('hex'); | ||
| 106 | + var auth_tag = encrypt.getAuthTag(); | ||
| 107 | + // only test basic encryption run if output is marked as tampered. | ||
| 108 | + if (!test.tampered) { | ||
| 109 | + assert.equal(hex.toUpperCase(), test.ct); | ||
| 110 | + assert.equal(auth_tag.toString('hex').toUpperCase(), test.tag); | ||
| 111 | + } | ||
| 106 | 112 | } | |
| 107 | 113 | })(); | |
| 108 | 114 | ||
| 109 | 115 | (function() { | |
| 110 | 116 | if (!test.password) return; | |
| 111 | - var decrypt = crypto.createDecipher(test.algo, test.password); | ||
| 112 | - decrypt.setAuthTag(new Buffer(test.tag, 'hex')); | ||
| 113 | - if (test.aad) | ||
| 114 | - decrypt.setAAD(new Buffer(test.aad, 'hex')); | ||
| 115 | - var msg = decrypt.update(test.ct, 'hex', 'ascii'); | ||
| 116 | - if (!test.tampered) { | ||
| 117 | - msg += decrypt.final('ascii'); | ||
| 118 | - assert.equal(msg, test.plain); | ||
| 117 | + if (common.hasFipsCrypto) { | ||
| 118 | + assert.throws(function() | ||
| 119 | + { crypto.createDecipher(test.algo, test.password); }, | ||
| 120 | + /not supported in FIPS mode/); | ||
| 119 | 121 | } else { | |
| 120 | - // assert that final throws if input data could not be verified! | ||
| 121 | - assert.throws(function() { decrypt.final('ascii'); }, / auth/); | ||
| 122 | + var decrypt = crypto.createDecipher(test.algo, test.password); | ||
| 123 | + decrypt.setAuthTag(new Buffer(test.tag, 'hex')); | ||
| 124 | + if (test.aad) | ||
| 125 | + decrypt.setAAD(new Buffer(test.aad, 'hex')); | ||
| 126 | + var msg = decrypt.update(test.ct, 'hex', 'ascii'); | ||
| 127 | + if (!test.tampered) { | ||
| 128 | + msg += decrypt.final('ascii'); | ||
| 129 | + assert.equal(msg, test.plain); | ||
| 130 | + } else { | ||
| 131 | + // assert that final throws if input data could not be verified! | ||
| 132 | + assert.throws(function() { decrypt.final('ascii'); }, / auth/); | ||
| 133 | + } | ||
| 122 | 134 | } | |
| 123 | 135 | })(); | |
| 124 | 136 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -496,12 +496,13 @@ function testCipher4(key, iv) { | |||
| 496 | 496 | assert.equal(txt, plaintext, 'encryption and decryption with key and iv'); | |
| 497 | 497 | } | |
| 498 | 498 | ||
| 499 | + if (!common.hasFipsCrypto) { | ||
| 500 | + testCipher1('MySecretKey123'); | ||
| 501 | + testCipher1(new Buffer('MySecretKey123')); | ||
| 499 | 502 | ||
| 500 | - testCipher1('MySecretKey123'); | ||
| 501 | - testCipher1(new Buffer('MySecretKey123')); | ||
| 502 | - | ||
| 503 | - testCipher2('0123456789abcdef'); | ||
| 504 | - testCipher2(new Buffer('0123456789abcdef')); | ||
| 503 | + testCipher2('0123456789abcdef'); | ||
| 504 | + testCipher2(new Buffer('0123456789abcdef')); | ||
| 505 | + } | ||
| 505 | 506 | ||
| 506 | 507 | testCipher3('0123456789abcd0123456789', '12345678'); | |
| 507 | 508 | testCipher3('0123456789abcd0123456789', new Buffer('12345678')); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,6 +6,10 @@ if (!common.hasCrypto) { | |||
| 6 | 6 | console.log('1..0 # Skipped: missing crypto'); | |
| 7 | 7 | return; | |
| 8 | 8 | } | |
| 9 | + if (common.hasFipsCrypto) { | ||
| 10 | + console.log('1..0 # Skipped: not supported in FIPS mode'); | ||
| 11 | + return; | ||
| 12 | + } | ||
| 9 | 13 | var crypto = require('crypto'); | |
| 10 | 14 | ||
| 11 | 15 | function testCipher1(key) { | |
@@ -62,71 +66,12 @@ function testCipher2(key) { | |||
| 62 | 66 | assert.equal(txt, plaintext, 'encryption and decryption with Base64'); | |
| 63 | 67 | } | |
| 64 | 68 | ||
| 65 | - | ||
| 66 | - function testCipher3(key, iv) { | ||
| 67 | - // Test encyrption and decryption with explicit key and iv | ||
| 68 | - var plaintext = | ||
| 69 | - '32|RmVZZkFUVmpRRkp0TmJaUm56ZU9qcnJkaXNNWVNpTTU*|iXmckfRWZBGWWELw' + | ||
| 70 | - 'eCBsThSsfUHLeRe0KCsK8ooHgxie0zOINpXxfZi/oNG7uq9JWFVCk70gfzQH8ZUJ' + | ||
| 71 | - 'jAfaFg**'; | ||
| 72 | - var cipher = crypto.createCipheriv('des-ede3-cbc', key, iv); | ||
| 73 | - var ciph = cipher.update(plaintext, 'utf8', 'hex'); | ||
| 74 | - ciph += cipher.final('hex'); | ||
| 75 | - | ||
| 76 | - var decipher = crypto.createDecipheriv('des-ede3-cbc', key, iv); | ||
| 77 | - var txt = decipher.update(ciph, 'hex', 'utf8'); | ||
| 78 | - txt += decipher.final('utf8'); | ||
| 79 | - | ||
| 80 | - assert.equal(txt, plaintext, 'encryption and decryption with key and iv'); | ||
| 81 | - | ||
| 82 | - // streaming cipher interface | ||
| 83 | - // NB: In real life, it's not guaranteed that you can get all of it | ||
| 84 | - // in a single read() like this. But in this case, we know it's | ||
| 85 | - // quite small, so there's no harm. | ||
| 86 | - var cStream = crypto.createCipheriv('des-ede3-cbc', key, iv); | ||
| 87 | - cStream.end(plaintext); | ||
| 88 | - ciph = cStream.read(); | ||
| 89 | - | ||
| 90 | - var dStream = crypto.createDecipheriv('des-ede3-cbc', key, iv); | ||
| 91 | - dStream.end(ciph); | ||
| 92 | - txt = dStream.read().toString('utf8'); | ||
| 93 | - | ||
| 94 | - assert.equal(txt, plaintext, 'streaming cipher iv'); | ||
| 95 | - } | ||
| 96 | - | ||
| 97 | - | ||
| 98 | - function testCipher4(key, iv) { | ||
| 99 | - // Test encyrption and decryption with explicit key and iv | ||
| 100 | - var plaintext = | ||
| 101 | - '32|RmVZZkFUVmpRRkp0TmJaUm56ZU9qcnJkaXNNWVNpTTU*|iXmckfRWZBGWWELw' + | ||
| 102 | - 'eCBsThSsfUHLeRe0KCsK8ooHgxie0zOINpXxfZi/oNG7uq9JWFVCk70gfzQH8ZUJ' + | ||
| 103 | - 'jAfaFg**'; | ||
| 104 | - var cipher = crypto.createCipheriv('des-ede3-cbc', key, iv); | ||
| 105 | - var ciph = cipher.update(plaintext, 'utf8', 'buffer'); | ||
| 106 | - ciph = Buffer.concat([ciph, cipher.final('buffer')]); | ||
| 107 | - | ||
| 108 | - var decipher = crypto.createDecipheriv('des-ede3-cbc', key, iv); | ||
| 109 | - var txt = decipher.update(ciph, 'buffer', 'utf8'); | ||
| 110 | - txt += decipher.final('utf8'); | ||
| 111 | - | ||
| 112 | - assert.equal(txt, plaintext, 'encryption and decryption with key and iv'); | ||
| 113 | - } | ||
| 114 | - | ||
| 115 | - | ||
| 116 | 69 | testCipher1('MySecretKey123'); | |
| 117 | 70 | testCipher1(new Buffer('MySecretKey123')); | |
| 118 | 71 | ||
| 119 | 72 | testCipher2('0123456789abcdef'); | |
| 120 | 73 | testCipher2(new Buffer('0123456789abcdef')); | |
| 121 | 74 | ||
| 122 | - testCipher3('0123456789abcd0123456789', '12345678'); | ||
| 123 | - testCipher3('0123456789abcd0123456789', new Buffer('12345678')); | ||
| 124 | - testCipher3(new Buffer('0123456789abcd0123456789'), '12345678'); | ||
| 125 | - testCipher3(new Buffer('0123456789abcd0123456789'), new Buffer('12345678')); | ||
| 126 | - | ||
| 127 | - testCipher4(new Buffer('0123456789abcd0123456789'), new Buffer('12345678')); | ||
| 128 | - | ||
| 129 | - | ||
| 130 | 75 | // Base64 padding regression test, see #4837. | |
| 131 | 76 | (function() { | |
| 132 | 77 | var c = crypto.createCipher('aes-256-cbc', 'secret'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,65 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + var common = require('../common'); | ||
| 3 | + var assert = require('assert'); | ||
| 4 | + | ||
| 5 | + if (!common.hasCrypto) { | ||
| 6 | + console.log('1..0 # Skipped: missing crypto'); | ||
| 7 | + return; | ||
| 8 | + } | ||
| 9 | + var crypto = require('crypto'); | ||
| 10 | + | ||
| 11 | + function testCipher1(key, iv) { | ||
| 12 | + // Test encyrption and decryption with explicit key and iv | ||
| 13 | + var plaintext = | ||
| 14 | + '32|RmVZZkFUVmpRRkp0TmJaUm56ZU9qcnJkaXNNWVNpTTU*|iXmckfRWZBGWWELw' + | ||
| 15 | + 'eCBsThSsfUHLeRe0KCsK8ooHgxie0zOINpXxfZi/oNG7uq9JWFVCk70gfzQH8ZUJ' + | ||
| 16 | + 'jAfaFg**'; | ||
| 17 | + var cipher = crypto.createCipheriv('des-ede3-cbc', key, iv); | ||
| 18 | + var ciph = cipher.update(plaintext, 'utf8', 'hex'); | ||
| 19 | + ciph += cipher.final('hex'); | ||
| 20 | + | ||
| 21 | + var decipher = crypto.createDecipheriv('des-ede3-cbc', key, iv); | ||
| 22 | + var txt = decipher.update(ciph, 'hex', 'utf8'); | ||
| 23 | + txt += decipher.final('utf8'); | ||
| 24 | + | ||
| 25 | + assert.equal(txt, plaintext, 'encryption and decryption with key and iv'); | ||
| 26 | + | ||
| 27 | + // streaming cipher interface | ||
| 28 | + // NB: In real life, it's not guaranteed that you can get all of it | ||
| 29 | + // in a single read() like this. But in this case, we know it's | ||
| 30 | + // quite small, so there's no harm. | ||
| 31 | + var cStream = crypto.createCipheriv('des-ede3-cbc', key, iv); | ||
| 32 | + cStream.end(plaintext); | ||
| 33 | + ciph = cStream.read(); | ||
| 34 | + | ||
| 35 | + var dStream = crypto.createDecipheriv('des-ede3-cbc', key, iv); | ||
| 36 | + dStream.end(ciph); | ||
| 37 | + txt = dStream.read().toString('utf8'); | ||
| 38 | + | ||
| 39 | + assert.equal(txt, plaintext, 'streaming cipher iv'); | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + | ||
| 43 | + function testCipher2(key, iv) { | ||
| 44 | + // Test encyrption and decryption with explicit key and iv | ||
| 45 | + var plaintext = | ||
| 46 | + '32|RmVZZkFUVmpRRkp0TmJaUm56ZU9qcnJkaXNNWVNpTTU*|iXmckfRWZBGWWELw' + | ||
| 47 | + 'eCBsThSsfUHLeRe0KCsK8ooHgxie0zOINpXxfZi/oNG7uq9JWFVCk70gfzQH8ZUJ' + | ||
| 48 | + 'jAfaFg**'; | ||
| 49 | + var cipher = crypto.createCipheriv('des-ede3-cbc', key, iv); | ||
| 50 | + var ciph = cipher.update(plaintext, 'utf8', 'buffer'); | ||
| 51 | + ciph = Buffer.concat([ciph, cipher.final('buffer')]); | ||
| 52 | + | ||
| 53 | + var decipher = crypto.createDecipheriv('des-ede3-cbc', key, iv); | ||
| 54 | + var txt = decipher.update(ciph, 'buffer', 'utf8'); | ||
| 55 | + txt += decipher.final('utf8'); | ||
| 56 | + | ||
| 57 | + assert.equal(txt, plaintext, 'encryption and decryption with key and iv'); | ||
| 58 | + } | ||
| 59 | + | ||
| 60 | + testCipher1('0123456789abcd0123456789', '12345678'); | ||
| 61 | + testCipher1('0123456789abcd0123456789', new Buffer('12345678')); | ||
| 62 | + testCipher1(new Buffer('0123456789abcd0123456789'), '12345678'); | ||
| 63 | + testCipher1(new Buffer('0123456789abcd0123456789'), new Buffer('12345678')); | ||
| 64 | + | ||
| 65 | + testCipher2(new Buffer('0123456789abcd0123456789'), new Buffer('12345678')); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -58,7 +58,7 @@ assert.equal(secret1, secret3); | |||
| 58 | 58 | ||
| 59 | 59 | // Run this one twice to make sure that the dh3 clears its error properly | |
| 60 | 60 | (function() { | |
| 61 | - var c = crypto.createDecipher('aes-128-ecb', ''); | ||
| 61 | + var c = crypto.createDecipheriv('aes-128-ecb', crypto.randomBytes(16), ''); | ||
| 62 | 62 | assert.throws(function() { c.final('utf8'); }, /wrong final block length/); | |
| 63 | 63 | })(); | |
| 64 | 64 | ||
@@ -67,7 +67,7 @@ assert.throws(function() { | |||
| 67 | 67 | }, /key is too small/i); | |
| 68 | 68 | ||
| 69 | 69 | (function() { | |
| 70 | - var c = crypto.createDecipher('aes-128-ecb', ''); | ||
| 70 | + var c = crypto.createDecipheriv('aes-128-ecb', crypto.randomBytes(16), ''); | ||
| 71 | 71 | assert.throws(function() { c.final('utf8'); }, /wrong final block length/); | |
| 72 | 72 | })(); | |
| 73 | 73 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -93,11 +93,11 @@ assertSorted(crypto.getCurves()); | |||
| 93 | 93 | // throw, not assert in C++ land. | |
| 94 | 94 | assert.throws(function() { | |
| 95 | 95 | crypto.createCipher('aes192', 'test').update('0', 'hex'); | |
| 96 | - }, /Bad input string/); | ||
| 96 | + }, common.hasFipsCrypto ? /not supported in FIPS mode/ : /Bad input string/); | ||
| 97 | 97 | ||
| 98 | 98 | assert.throws(function() { | |
| 99 | 99 | crypto.createDecipher('aes192', 'test').update('0', 'hex'); | |
| 100 | - }, /Bad input string/); | ||
| 100 | + }, common.hasFipsCrypto ? /not supported in FIPS mode/ : /Bad input string/); | ||
| 101 | 101 | ||
| 102 | 102 | assert.throws(function() { | |
| 103 | 103 | crypto.createHash('sha1').update('0', 'hex'); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments