@@ -786,3 +786,34 @@ for (const test of TEST_CASES) {
786
786
assert . strictEqual ( plaintext . toString ( 'hex' ) , testCase . plain ) ;
787
787
}
788
788
}
789
+
790
+ // https://github.com/nodejs/node/issues/45874
791
+ {
792
+ const rfcTestCases = TEST_CASES . filter ( ( { algo, tampered } ) => {
793
+ return algo === 'chacha20-poly1305' && tampered === false ;
794
+ } ) ;
795
+ assert . strictEqual ( rfcTestCases . length , 1 ) ;
796
+
797
+ const [ testCase ] = rfcTestCases ;
798
+ const key = Buffer . from ( testCase . key , 'hex' ) ;
799
+ const iv = Buffer . from ( testCase . iv , 'hex' ) ;
800
+ const aad = Buffer . from ( testCase . aad , 'hex' ) ;
801
+ const opt = { authTagLength : 16 } ;
802
+
803
+ const cipher = crypto . createCipheriv ( 'chacha20-poly1305' , key , iv , opt ) ;
804
+ const ciphertext = Buffer . concat ( [
805
+ cipher . setAAD ( aad ) . update ( testCase . plain , 'hex' ) ,
806
+ cipher . final ( ) ,
807
+ ] ) ;
808
+ const authTag = cipher . getAuthTag ( ) ;
809
+
810
+ assert . strictEqual ( ciphertext . toString ( 'hex' ) , testCase . ct ) ;
811
+ assert . strictEqual ( authTag . toString ( 'hex' ) , testCase . tag ) ;
812
+
813
+ const decipher = crypto . createDecipheriv ( 'chacha20-poly1305' , key , iv , opt ) ;
814
+ decipher . setAAD ( aad ) . update ( ciphertext ) ;
815
+
816
+ assert . throws ( ( ) => {
817
+ decipher . final ( ) ;
818
+ } , / U n s u p p o r t e d s t a t e o r u n a b l e t o a u t h e n t i c a t e d a t a / ) ;
819
+ }
0 commit comments