@@ -66,6 +66,9 @@ class CryptoUtil {
6666
6767 private static final byte FORMAT_MARKER = 0x01 ;
6868
69+ private static final int GCM_TAG_LENGTH = 16 ;
70+ private static final int MIN_DATA_LENGTH = 1 ;
71+
6972 private final String OLD_KEY_ALIAS ;
7073 private final String OLD_KEY_IV_ALIAS ;
7174 private final String KEY_ALIAS ;
@@ -467,19 +470,30 @@ public byte[] decrypt(byte[] encryptedInput) throws CryptoException, Incompatibl
467470 */
468471 @ VisibleForTesting
469472 boolean isNewFormat (byte [] encryptedInput ) {
473+
474+ // Boundary check
475+ if (encryptedInput == null || encryptedInput .length < 2 ) {
476+ return false ;
477+ }
478+
470479 if (encryptedInput [0 ] != FORMAT_MARKER ) {
471480 return false ;
472481 }
473482
474483 // Check IV length is valid for AES-GCM (12 or 16 bytes)
484+ // AES is a 128 block size cipher ,which is 16 bytes
485+ // AES in GCM mode the recommended IV length is 12 bytes.
486+ // This 12-byte IV is then combined with a 4-byte internal counter to form the full 16-byte
487+ // input block for the underlying AES block cipher in counter mode (CTR), which GCM utilizes.
488+ // Thus checking for a 12 or 16 byte length
475489 int ivLength = encryptedInput [1 ] & 0xFF ;
476490 if (ivLength != 12 && ivLength != 16 ) {
477491 return false ;
478492 }
479493
480494 // Verify minimum total length
481495 // Need: marker(1) + length(1) + IV(12-16) + GCM tag(16) + data(1+)
482- int minLength = 2 + ivLength + 16 + 1 ;
496+ int minLength = 2 + ivLength + GCM_TAG_LENGTH + MIN_DATA_LENGTH ;
483497 return encryptedInput .length >= minLength ;
484498 }
485499
0 commit comments