| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -546,7 +546,8 @@ added: v1.0.0 | |||
| 546 | 546 | --> | |
| 547 | 547 | ||
| 548 | 548 | * Returns: {Buffer} When using an authenticated encryption mode (`GCM`, `CCM`, | |
| 549 | - and `OCB` are currently supported), the `cipher.getAuthTag()` method returns a | ||
| 549 | + `OCB`, and `chacha20-poly1305` are currently supported), the | ||
| 550 | + `cipher.getAuthTag()` method returns a | ||
| 550 | 551 | [`Buffer`][] containing the _authentication tag_ that has been computed from | |
| 551 | 552 | the given data. | |
| 552 | 553 | ||
@@ -568,7 +569,8 @@ added: v1.0.0 | |||
| 568 | 569 | * `encoding` {string} The string encoding to use when `buffer` is a string. | |
| 569 | 570 | * Returns: {Cipher} for method chaining. | |
| 570 | 571 | ||
| 571 | - When using an authenticated encryption mode (`GCM`, `CCM`, and `OCB` are | ||
| 572 | + When using an authenticated encryption mode (`GCM`, `CCM`, `OCB`, and | ||
| 573 | + `chacha20-poly1305` are | ||
| 572 | 574 | currently supported), the `cipher.setAAD()` method sets the value used for the | |
| 573 | 575 | _additional authenticated data_ (AAD) input parameter. | |
| 574 | 576 | ||
@@ -865,7 +867,8 @@ changes: | |||
| 865 | 867 | * `encoding` {string} String encoding to use when `buffer` is a string. | |
| 866 | 868 | * Returns: {Decipher} for method chaining. | |
| 867 | 869 | ||
| 868 | - When using an authenticated encryption mode (`GCM`, `CCM`, and `OCB` are | ||
| 870 | + When using an authenticated encryption mode (`GCM`, `CCM`, `OCB`, and | ||
| 871 | + `chacha20-poly1305` are | ||
| 869 | 872 | currently supported), the `decipher.setAAD()` method sets the value used for the | |
| 870 | 873 | _additional authenticated data_ (AAD) input parameter. | |
| 871 | 874 | ||
@@ -899,7 +902,8 @@ changes: | |||
| 899 | 902 | * `encoding` {string} String encoding to use when `buffer` is a string. | |
| 900 | 903 | * Returns: {Decipher} for method chaining. | |
| 901 | 904 | ||
| 902 | - When using an authenticated encryption mode (`GCM`, `CCM`, and `OCB` are | ||
| 905 | + When using an authenticated encryption mode (`GCM`, `CCM`, `OCB`, and | ||
| 906 | + `chacha20-poly1305` are | ||
| 903 | 907 | currently supported), the `decipher.setAuthTag()` method is used to pass in the | |
| 904 | 908 | received _authentication tag_. If no tag is provided, or if the cipher text | |
| 905 | 909 | has been tampered with, [`decipher.final()`][] will throw, indicating that the | |
@@ -908,7 +912,8 @@ is invalid according to [NIST SP 800-38D][] or does not match the value of the | |||
| 908 | 912 | `authTagLength` option, `decipher.setAuthTag()` will throw an error. | |
| 909 | 913 | ||
| 910 | 914 | The `decipher.setAuthTag()` method must be called before [`decipher.update()`][] | |
| 911 | - for `CCM` mode or before [`decipher.final()`][] for `GCM` and `OCB` modes. | ||
| 915 | + for `CCM` mode or before [`decipher.final()`][] for `GCM` and `OCB` modes and | ||
| 916 | + `chacha20-poly1305`. | ||
| 912 | 917 | `decipher.setAuthTag()` can only be called once. | |
| 913 | 918 | ||
| 914 | 919 | When passing a string as the authentication tag, please consider | |
@@ -2945,7 +2950,8 @@ Creates and returns a `Cipher` object that uses the given `algorithm` and | |||
| 2945 | 2950 | `password`. | |
| 2946 | 2951 | ||
| 2947 | 2952 | The `options` argument controls stream behavior and is optional except when a | |
| 2948 | - cipher in CCM or OCB mode is used (e.g. `'aes-128-ccm'`). In that case, the | ||
| 2953 | + cipher in CCM or OCB mode (e.g. `'aes-128-ccm'`) or `chacha20-poly1305` is used. | ||
| 2954 | + In that case, the | ||
| 2949 | 2955 | `authTagLength` option is required and specifies the length of the | |
| 2950 | 2956 | authentication tag in bytes, see [CCM mode][]. In GCM mode, the `authTagLength` | |
| 2951 | 2957 | option is not required but can be used to set the length of the authentication | |
@@ -3016,7 +3022,8 @@ Creates and returns a `Cipher` object, with the given `algorithm`, `key` and | |||
| 3016 | 3022 | initialization vector (`iv`). | |
| 3017 | 3023 | ||
| 3018 | 3024 | The `options` argument controls stream behavior and is optional except when a | |
| 3019 | - cipher in CCM or OCB mode is used (e.g. `'aes-128-ccm'`). In that case, the | ||
| 3025 | + cipher in CCM or OCB mode (e.g. `'aes-128-ccm'`) or `chacha20-poly1305` is used. | ||
| 3026 | + In that case, the | ||
| 3020 | 3027 | `authTagLength` option is required and specifies the length of the | |
| 3021 | 3028 | authentication tag in bytes, see [CCM mode][]. In GCM mode, the `authTagLength` | |
| 3022 | 3029 | option is not required but can be used to set the length of the authentication | |
@@ -3064,7 +3071,8 @@ Creates and returns a `Decipher` object that uses the given `algorithm` and | |||
| 3064 | 3071 | `password` (key). | |
| 3065 | 3072 | ||
| 3066 | 3073 | The `options` argument controls stream behavior and is optional except when a | |
| 3067 | - cipher in CCM or OCB mode is used (e.g. `'aes-128-ccm'`). In that case, the | ||
| 3074 | + cipher in CCM or OCB mode (e.g. `'aes-128-ccm'`) or `chacha20-poly1305` is used. | ||
| 3075 | + In that case, the | ||
| 3068 | 3076 | `authTagLength` option is required and specifies the length of the | |
| 3069 | 3077 | authentication tag in bytes, see [CCM mode][]. | |
| 3070 | 3078 | ||
@@ -3117,7 +3125,8 @@ Creates and returns a `Decipher` object that uses the given `algorithm`, `key` | |||
| 3117 | 3125 | and initialization vector (`iv`). | |
| 3118 | 3126 | ||
| 3119 | 3127 | The `options` argument controls stream behavior and is optional except when a | |
| 3120 | - cipher in CCM or OCB mode is used (e.g. `'aes-128-ccm'`). In that case, the | ||
| 3128 | + cipher in CCM or OCB mode (e.g. `'aes-128-ccm'`) or `chacha20-poly1305` is used. | ||
| 3129 | + In that case, the | ||
| 3121 | 3130 | `authTagLength` option is required and specifies the length of the | |
| 3122 | 3131 | authentication tag in bytes, see [CCM mode][]. In GCM mode, the `authTagLength` | |
| 3123 | 3132 | option is not required but can be used to restrict accepted authentication tags | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -701,3 +701,47 @@ for (const test of TEST_CASES) { | |||
| 701 | 701 | }); | |
| 702 | 702 | } | |
| 703 | 703 | } | |
| 704 | + | ||
| 705 | + // ChaCha20-Poly1305 should respect the authTagLength option and should not | ||
| 706 | + // require the authentication tag before calls to update() during decryption. | ||
| 707 | + { | ||
| 708 | + const key = Buffer.alloc(32); | ||
| 709 | + const iv = Buffer.alloc(12); | ||
| 710 | + | ||
| 711 | + for (let authTagLength = 1; authTagLength <= 16; authTagLength++) { | ||
| 712 | + const cipher = | ||
| 713 | + crypto.createCipheriv('chacha20-poly1305', key, iv, { authTagLength }); | ||
| 714 | + const ciphertext = Buffer.concat([cipher.update('foo'), cipher.final()]); | ||
| 715 | + const authTag = cipher.getAuthTag(); | ||
| 716 | + assert.strictEqual(authTag.length, authTagLength); | ||
| 717 | + | ||
| 718 | + // The decipher operation should reject all authentication tags other than | ||
| 719 | + // that of the expected length. | ||
| 720 | + for (let other = 1; other <= 16; other++) { | ||
| 721 | + const decipher = crypto.createDecipheriv('chacha20-poly1305', key, iv, { | ||
| 722 | + authTagLength: other | ||
| 723 | + }); | ||
| 724 | + // ChaCha20 is a stream cipher so we do not need to call final() to obtain | ||
| 725 | + // the full plaintext. | ||
| 726 | + const plaintext = decipher.update(ciphertext); | ||
| 727 | + assert.strictEqual(plaintext.toString(), 'foo'); | ||
| 728 | + if (other === authTagLength) { | ||
| 729 | + // The authentication tag length is as expected and the tag itself is | ||
| 730 | + // correct, so this should work. | ||
| 731 | + decipher.setAuthTag(authTag); | ||
| 732 | + decipher.final(); | ||
| 733 | + } else { | ||
| 734 | + // The authentication tag that we are going to pass to setAuthTag is | ||
| 735 | + // either too short or too long. If other < authTagLength, the | ||
| 736 | + // authentication tag is still correct, but it should still be rejected | ||
| 737 | + // because its security assurance is lower than expected. | ||
| 738 | + assert.throws(() => { | ||
| 739 | + decipher.setAuthTag(authTag); | ||
| 740 | + }, { | ||
| 741 | + code: 'ERR_CRYPTO_INVALID_AUTH_TAG', | ||
| 742 | + message: `Invalid authentication tag length: ${authTagLength}` | ||
| 743 | + }); | ||
| 744 | + } | ||
| 745 | + } | ||
| 746 | + } | ||
| 747 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments