@@ -364,10 +364,14 @@ function gctr (
364364 * fully compliant AES-GCM encoding will require a compatibility strategy, as
365365 * existing ciphertexts produced by this implementation will otherwise become
366366 * undecryptable.
367+ *
368+ * This non-standard padding behavior is retained intentionally for backward
369+ * compatibility: existing ciphertexts in production were generated with this
370+ * encoding, and changing it would render previously encrypted data
371+ * undecryptable by newer versions of the library.
367372 */
368373export function AESGCM (
369374 plainText : number [ ] ,
370- additionalAuthenticatedData : number [ ] ,
371375 initializationVector : number [ ] ,
372376 key : number [ ]
373377) : { result : number [ ] , authenticationTag : number [ ] } {
@@ -380,7 +384,7 @@ export function AESGCM (
380384 }
381385
382386 let preCounterBlock
383- let plainTag
387+ let plainTag : number [ ] = [ ]
384388 const hashSubKey = AES ( createZeroBlock ( 16 ) , key )
385389 preCounterBlock = [ ...initializationVector ]
386390 if ( initializationVector . length === 12 ) {
@@ -400,14 +404,7 @@ export function AESGCM (
400404
401405 const cipherText = gctr ( plainText , incrementLeastSignificantThirtyTwoBits ( preCounterBlock ) , key )
402406
403- plainTag = additionalAuthenticatedData . slice ( )
404-
405- if ( additionalAuthenticatedData . length === 0 ) {
406- plainTag = plainTag . concat ( createZeroBlock ( 16 ) )
407- } else if ( additionalAuthenticatedData . length % 16 !== 0 ) {
408- plainTag = plainTag . concat ( createZeroBlock ( 16 - ( additionalAuthenticatedData . length % 16 ) ) )
409- }
410-
407+ plainTag = plainTag . concat ( createZeroBlock ( 16 ) )
411408 plainTag = plainTag . concat ( cipherText )
412409
413410 if ( cipherText . length === 0 ) {
@@ -417,7 +414,7 @@ export function AESGCM (
417414 }
418415
419416 plainTag = plainTag . concat ( createZeroBlock ( 4 ) )
420- . concat ( getBytes ( additionalAuthenticatedData . length * 8 ) )
417+ . concat ( getBytes ( 0 ) )
421418 . concat ( createZeroBlock ( 4 ) ) . concat ( getBytes ( cipherText . length * 8 ) )
422419
423420 return {
@@ -428,7 +425,6 @@ export function AESGCM (
428425
429426export function AESGCMDecrypt (
430427 cipherText : number [ ] ,
431- additionalAuthenticatedData : number [ ] ,
432428 initializationVector : number [ ] ,
433429 authenticationTag : number [ ] ,
434430 key : number [ ]
@@ -446,7 +442,7 @@ export function AESGCMDecrypt (
446442 }
447443
448444 let preCounterBlock
449- let compareTag
445+ let compareTag : number [ ] = [ ]
450446
451447 // Generate the hash subkey
452448 const hashSubKey = AES ( createZeroBlock ( 16 ) , key )
@@ -467,14 +463,7 @@ export function AESGCMDecrypt (
467463 // Decrypt to obtain the plain text
468464 const plainText = gctr ( cipherText , incrementLeastSignificantThirtyTwoBits ( preCounterBlock ) , key )
469465
470- compareTag = additionalAuthenticatedData . slice ( )
471-
472- if ( additionalAuthenticatedData . length === 0 ) {
473- compareTag = compareTag . concat ( createZeroBlock ( 16 ) )
474- } else if ( additionalAuthenticatedData . length % 16 !== 0 ) {
475- compareTag = compareTag . concat ( createZeroBlock ( 16 - ( additionalAuthenticatedData . length % 16 ) ) )
476- }
477-
466+ compareTag = compareTag . concat ( createZeroBlock ( 16 ) )
478467 compareTag = compareTag . concat ( cipherText )
479468
480469 if ( cipherText . length === 0 ) {
@@ -484,8 +473,9 @@ export function AESGCMDecrypt (
484473 }
485474
486475 compareTag = compareTag . concat ( createZeroBlock ( 4 ) )
487- . concat ( getBytes ( additionalAuthenticatedData . length * 8 ) )
488- . concat ( createZeroBlock ( 4 ) ) . concat ( getBytes ( cipherText . length * 8 ) )
476+ . concat ( getBytes ( 0 ) )
477+ . concat ( createZeroBlock ( 4 ) )
478+ . concat ( getBytes ( cipherText . length * 8 ) )
489479
490480 // Generate the authentication tag
491481 const calculatedTag = gctr ( ghash ( compareTag , hashSubKey ) , preCounterBlock , key )
0 commit comments