Skip to content

Commit 78a71aa

Browse files
shigekiMylesBorins
authored andcommitted
crypto: fix error of createCipher in wrap mode
EVP_CIPHER_CTX_FLAG_WRAP_ALLOW flag needs to be set in using wrap mode ciphers. In `crypto.createCipher()`, AES key wrap mode does not use a default IV defined in RFC3394 but a generated IV with `EVP_BytesToKey()` to be consistent API behaviors with other ciphers. The built-in AES wrap mode in OpenSSL is not supported in FIPS mode as http://openssl.6102.n7.nabble.com/AES-Key-Wrap-in-FIPS-Mode-td50238.html so its tests in FIPS mode are skipped. Fixes: #15009 PR-URL: #15037 Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 41bf40e commit 78a71aa

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

src/node_crypto.cc

+9-1
Original file line numberDiff line numberDiff line change
@@ -3349,6 +3349,9 @@ void CipherBase::Init(const char* cipher_type,
33493349
cipher_type);
33503350
}
33513351

3352+
if (mode == EVP_CIPH_WRAP_MODE)
3353+
EVP_CIPHER_CTX_set_flags(&ctx_, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
3354+
33523355
if (!EVP_CIPHER_CTX_set_key_length(&ctx_, key_len)) {
33533356
EVP_CIPHER_CTX_cleanup(&ctx_);
33543357
return env()->ThrowError("Invalid key length");
@@ -3396,13 +3399,18 @@ void CipherBase::InitIv(const char* cipher_type,
33963399
}
33973400

33983401
const int expected_iv_len = EVP_CIPHER_iv_length(cipher);
3399-
const bool is_gcm_mode = (EVP_CIPH_GCM_MODE == EVP_CIPHER_mode(cipher));
3402+
const int mode = EVP_CIPHER_mode(cipher);
3403+
const bool is_gcm_mode = (EVP_CIPH_GCM_MODE == mode);
34003404

34013405
if (is_gcm_mode == false && iv_len != expected_iv_len) {
34023406
return env()->ThrowError("Invalid IV length");
34033407
}
34043408

34053409
EVP_CIPHER_CTX_init(&ctx_);
3410+
3411+
if (mode == EVP_CIPH_WRAP_MODE)
3412+
EVP_CIPHER_CTX_set_flags(&ctx_, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
3413+
34063414
const bool encrypt = (kind_ == kCipher);
34073415
EVP_CipherInit_ex(&ctx_, cipher, nullptr, nullptr, nullptr, encrypt);
34083416

test/parallel/test-crypto-binary-default.js

+21
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,33 @@ function testCipher4(key, iv) {
530530
'encryption and decryption with key and iv');
531531
}
532532

533+
534+
function testCipher5(key, iv) {
535+
// Test encryption and decryption with explicit key with aes128-wrap
536+
const plaintext =
537+
'32|RmVZZkFUVmpRRkp0TmJaUm56ZU9qcnJkaXNNWVNpTTU*|iXmckfRWZBGWWELw' +
538+
'eCBsThSsfUHLeRe0KCsK8ooHgxie0zOINpXxfZi/oNG7uq9JWFVCk70gfzQH8ZUJ' +
539+
'jAfaFg**';
540+
const cipher = crypto.createCipher('id-aes128-wrap', key);
541+
let ciph = cipher.update(plaintext, 'utf8', 'buffer');
542+
ciph = Buffer.concat([ciph, cipher.final('buffer')]);
543+
544+
const decipher = crypto.createDecipher('id-aes128-wrap', key);
545+
let txt = decipher.update(ciph, 'buffer', 'utf8');
546+
txt += decipher.final('utf8');
547+
548+
assert.strictEqual(txt, plaintext,
549+
'encryption and decryption with key');
550+
}
551+
533552
if (!common.hasFipsCrypto) {
534553
testCipher1('MySecretKey123');
535554
testCipher1(Buffer.from('MySecretKey123'));
536555

537556
testCipher2('0123456789abcdef');
538557
testCipher2(Buffer.from('0123456789abcdef'));
558+
559+
testCipher5(Buffer.from('0123456789abcd0123456789'));
539560
}
540561

541562
testCipher3('0123456789abcd0123456789', '12345678');

test/parallel/test-crypto-cipheriv-decipheriv.js

+24
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,36 @@ function testCipher2(key, iv) {
5555
assert.strictEqual(txt, plaintext, 'encryption/decryption with key and iv');
5656
}
5757

58+
59+
function testCipher3(key, iv) {
60+
// Test encryption and decryption with explicit key and iv.
61+
// AES Key Wrap test vector comes from RFC3394
62+
const plaintext = Buffer.from('00112233445566778899AABBCCDDEEFF', 'hex');
63+
64+
const cipher = crypto.createCipheriv('id-aes128-wrap', key, iv);
65+
let ciph = cipher.update(plaintext, 'utf8', 'buffer');
66+
ciph = Buffer.concat([ciph, cipher.final('buffer')]);
67+
const ciph2 = Buffer.from('1FA68B0A8112B447AEF34BD8FB5A7B829D3E862371D2CFE5',
68+
'hex');
69+
assert(ciph.equals(ciph2));
70+
const decipher = crypto.createDecipheriv('id-aes128-wrap', key, iv);
71+
let deciph = decipher.update(ciph, 'buffer');
72+
deciph = Buffer.concat([deciph, decipher.final()]);
73+
74+
assert(deciph.equals(plaintext), 'encryption/decryption with key and iv');
75+
}
76+
5877
testCipher1('0123456789abcd0123456789', '12345678');
5978
testCipher1('0123456789abcd0123456789', Buffer.from('12345678'));
6079
testCipher1(Buffer.from('0123456789abcd0123456789'), '12345678');
6180
testCipher1(Buffer.from('0123456789abcd0123456789'), Buffer.from('12345678'));
6281
testCipher2(Buffer.from('0123456789abcd0123456789'), Buffer.from('12345678'));
6382

83+
if (!common.hasFipsCrypto) {
84+
testCipher3(Buffer.from('000102030405060708090A0B0C0D0E0F', 'hex'),
85+
Buffer.from('A6A6A6A6A6A6A6A6', 'hex'));
86+
}
87+
6488
// Zero-sized IV should be accepted in ECB mode.
6589
crypto.createCipheriv('aes-128-ecb', Buffer.alloc(16), Buffer.alloc(0));
6690

0 commit comments

Comments
 (0)