@@ -29,14 +29,15 @@ class AesGcmJceKeyCipher extends JceKeyCipher {
29
29
private static final int NONCE_LENGTH = 12 ;
30
30
private static final int TAG_LENGTH = 128 ;
31
31
private static final String TRANSFORMATION = "AES/GCM/NoPadding" ;
32
+ private static final int SPEC_LENGTH = Integer .BYTES + Integer .BYTES + NONCE_LENGTH ;
32
33
33
34
AesGcmJceKeyCipher (SecretKey key ) {
34
35
super (key , key );
35
36
}
36
37
37
38
private static byte [] specToBytes (final GCMParameterSpec spec ) {
38
39
final byte [] nonce = spec .getIV ();
39
- final byte [] result = new byte [Integer . BYTES + Integer . BYTES + nonce . length ];
40
+ final byte [] result = new byte [SPEC_LENGTH ];
40
41
final ByteBuffer buffer = ByteBuffer .wrap (result );
41
42
buffer .putInt (spec .getTLen ());
42
43
buffer .putInt (nonce .length );
@@ -45,16 +46,19 @@ private static byte[] specToBytes(final GCMParameterSpec spec) {
45
46
}
46
47
47
48
private static GCMParameterSpec bytesToSpec (final byte [] data , final int offset ) throws InvalidKeyException {
48
- final ByteBuffer buffer = ByteBuffer .wrap (data , offset , data .length - offset );
49
+ if (data .length - offset != SPEC_LENGTH ) {
50
+ throw new InvalidKeyException ("Algorithm specification was an invalid data size" );
51
+ }
49
52
53
+ final ByteBuffer buffer = ByteBuffer .wrap (data , offset , SPEC_LENGTH );
50
54
final int tagLen = buffer .getInt ();
51
55
final int nonceLen = buffer .getInt ();
52
56
53
57
if (tagLen != TAG_LENGTH ) {
54
58
throw new InvalidKeyException (String .format ("Authentication tag length must be %s" , TAG_LENGTH ));
55
59
}
56
60
57
- if (nonceLen != NONCE_LENGTH || buffer . remaining () != NONCE_LENGTH ) {
61
+ if (nonceLen != NONCE_LENGTH ) {
58
62
throw new InvalidKeyException (String .format ("Initialization vector (IV) length must be %s" , NONCE_LENGTH ));
59
63
}
60
64
0 commit comments