| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7115079 commit f2cafff
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3359,6 +3359,9 @@ void CipherBase::Init(const char* cipher_type, | |||
| 3359 | 3359 | cipher_type); | |
| 3360 | 3360 | } | |
| 3361 | 3361 | ||
| 3362 | + if (mode == EVP_CIPH_WRAP_MODE) | ||
| 3363 | + EVP_CIPHER_CTX_set_flags(&ctx_, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); | ||
| 3364 | + | ||
| 3362 | 3365 | if (!EVP_CIPHER_CTX_set_key_length(&ctx_, key_len)) { | |
| 3363 | 3366 | EVP_CIPHER_CTX_cleanup(&ctx_); | |
| 3364 | 3367 | return env()->ThrowError("Invalid key length"); | |
@@ -3406,13 +3409,18 @@ void CipherBase::InitIv(const char* cipher_type, | |||
| 3406 | 3409 | } | |
| 3407 | 3410 | ||
| 3408 | 3411 | const int expected_iv_len = EVP_CIPHER_iv_length(cipher_); | |
| 3409 | - const bool is_gcm_mode = (EVP_CIPH_GCM_MODE == EVP_CIPHER_mode(cipher_)); | ||
| 3412 | + const int mode = EVP_CIPHER_mode(cipher_); | ||
| 3413 | + const bool is_gcm_mode = (EVP_CIPH_GCM_MODE == mode); | ||
| 3410 | 3414 | ||
| 3411 | 3415 | if (is_gcm_mode == false && iv_len != expected_iv_len) { | |
| 3412 | 3416 | return env()->ThrowError("Invalid IV length"); | |
| 3413 | 3417 | } | |
| 3414 | 3418 | ||
| 3415 | 3419 | EVP_CIPHER_CTX_init(&ctx_); | |
| 3420 | + | ||
| 3421 | + if (mode == EVP_CIPH_WRAP_MODE) | ||
| 3422 | + EVP_CIPHER_CTX_set_flags(&ctx_, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); | ||
| 3423 | + | ||
| 3416 | 3424 | const bool encrypt = (kind_ == kCipher); | |
| 3417 | 3425 | EVP_CipherInit_ex(&ctx_, cipher_, nullptr, nullptr, nullptr, encrypt); | |
| 3418 | 3426 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -509,12 +509,33 @@ function testCipher4(key, iv) { | |||
| 509 | 509 | 'encryption and decryption with key and iv'); | |
| 510 | 510 | } | |
| 511 | 511 | ||
| 512 | + | ||
| 513 | + function testCipher5(key, iv) { | ||
| 514 | + // Test encryption and decryption with explicit key with aes128-wrap | ||
| 515 | + const plaintext = | ||
| 516 | + '32|RmVZZkFUVmpRRkp0TmJaUm56ZU9qcnJkaXNNWVNpTTU*|iXmckfRWZBGWWELw' + | ||
| 517 | + 'eCBsThSsfUHLeRe0KCsK8ooHgxie0zOINpXxfZi/oNG7uq9JWFVCk70gfzQH8ZUJ' + | ||
| 518 | + 'jAfaFg**'; | ||
| 519 | + const cipher = crypto.createCipher('id-aes128-wrap', key); | ||
| 520 | + let ciph = cipher.update(plaintext, 'utf8', 'buffer'); | ||
| 521 | + ciph = Buffer.concat([ciph, cipher.final('buffer')]); | ||
| 522 | + | ||
| 523 | + const decipher = crypto.createDecipher('id-aes128-wrap', key); | ||
| 524 | + let txt = decipher.update(ciph, 'buffer', 'utf8'); | ||
| 525 | + txt += decipher.final('utf8'); | ||
| 526 | + | ||
| 527 | + assert.strictEqual(txt, plaintext, | ||
| 528 | + 'encryption and decryption with key'); | ||
| 529 | + } | ||
| 530 | + | ||
| 512 | 531 | if (!common.hasFipsCrypto) { | |
| 513 | 532 | testCipher1('MySecretKey123'); | |
| 514 | 533 | testCipher1(Buffer.from('MySecretKey123')); | |
| 515 | 534 | ||
| 516 | 535 | testCipher2('0123456789abcdef'); | |
| 517 | 536 | testCipher2(Buffer.from('0123456789abcdef')); | |
| 537 | + | ||
| 538 | + testCipher5(Buffer.from('0123456789abcd0123456789')); | ||
| 518 | 539 | } | |
| 519 | 540 | ||
| 520 | 541 | testCipher3('0123456789abcd0123456789', '12345678'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,12 +55,36 @@ function testCipher2(key, iv) { | |||
| 55 | 55 | assert.strictEqual(txt, plaintext, 'encryption/decryption with key and iv'); | |
| 56 | 56 | } | |
| 57 | 57 | ||
| 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 | + | ||
| 58 | 77 | testCipher1('0123456789abcd0123456789', '12345678'); | |
| 59 | 78 | testCipher1('0123456789abcd0123456789', Buffer.from('12345678')); | |
| 60 | 79 | testCipher1(Buffer.from('0123456789abcd0123456789'), '12345678'); | |
| 61 | 80 | testCipher1(Buffer.from('0123456789abcd0123456789'), Buffer.from('12345678')); | |
| 62 | 81 | testCipher2(Buffer.from('0123456789abcd0123456789'), Buffer.from('12345678')); | |
| 63 | 82 | ||
| 83 | + if (!common.hasFipsCrypto) { | ||
| 84 | + testCipher3(Buffer.from('000102030405060708090A0B0C0D0E0F', 'hex'), | ||
| 85 | + Buffer.from('A6A6A6A6A6A6A6A6', 'hex')); | ||
| 86 | + } | ||
| 87 | + | ||
| 64 | 88 | // Zero-sized IV should be accepted in ECB mode. | |
| 65 | 89 | crypto.createCipheriv('aes-128-ecb', Buffer.alloc(16), Buffer.alloc(0)); | |
| 66 | 90 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments