@@ -50,6 +50,54 @@ public byte[] encryptWithStaticIvByteArray(byte[] key, byte[] plaintext) throws
5050 return cipher .doFinal ();
5151 }
5252
53+ // BAD: AES-GCM with static IV from a multidimensional byte array
54+ public byte [] encryptWithOneOfStaticIvs01 (byte [] key , byte [] plaintext ) throws Exception {
55+ byte [][] staticIvs = new byte [][] {
56+ { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 , 1 , 2 , 3 , 4 , 5 },
57+ { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 , 1 , 2 , 3 , 4 , 42 }
58+ };
59+
60+ GCMParameterSpec ivSpec = new GCMParameterSpec (128 , staticIvs [1 ]);
61+ SecretKeySpec keySpec = new SecretKeySpec (key , "AES" );
62+
63+ Cipher cipher = Cipher .getInstance ("AES/GCM/PKCS5PADDING" );
64+ cipher .init (Cipher .ENCRYPT_MODE , keySpec , ivSpec ); // $staticInitializationVector
65+ cipher .update (plaintext );
66+ return cipher .doFinal ();
67+ }
68+
69+ // BAD: AES-GCM with static IV from a multidimensional byte array
70+ public byte [] encryptWithOneOfStaticIvs02 (byte [] key , byte [] plaintext ) throws Exception {
71+ byte [][] staticIvs = new byte [][] {
72+ new byte [] { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 , 1 , 2 , 3 , 4 , 5 },
73+ new byte [] { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 0 , 1 , 2 , 3 , 4 , 42 }
74+ };
75+
76+ GCMParameterSpec ivSpec = new GCMParameterSpec (128 , staticIvs [1 ]);
77+ SecretKeySpec keySpec = new SecretKeySpec (key , "AES" );
78+
79+ Cipher cipher = Cipher .getInstance ("AES/GCM/PKCS5PADDING" );
80+ cipher .init (Cipher .ENCRYPT_MODE , keySpec , ivSpec ); // $staticInitializationVector
81+ cipher .update (plaintext );
82+ return cipher .doFinal ();
83+ }
84+
85+ // BAD: AES-GCM with static IV from a multidimensional byte array
86+ public byte [] encryptWithOneOfStaticZeroIvs (byte [] key , byte [] plaintext ) throws Exception {
87+ byte [][] ivs = new byte [][] {
88+ new byte [8 ],
89+ new byte [16 ]
90+ };
91+
92+ GCMParameterSpec ivSpec = new GCMParameterSpec (128 , ivs [1 ]);
93+ SecretKeySpec keySpec = new SecretKeySpec (key , "AES" );
94+
95+ Cipher cipher = Cipher .getInstance ("AES/GCM/PKCS5PADDING" );
96+ cipher .init (Cipher .ENCRYPT_MODE , keySpec , ivSpec ); // $staticInitializationVector
97+ cipher .update (plaintext );
98+ return cipher .doFinal ();
99+ }
100+
53101 // GOOD: AES-GCM with a random IV
54102 public byte [] encryptWithRandomIv (byte [] key , byte [] plaintext ) throws Exception {
55103 byte [] iv = new byte [16 ];
0 commit comments