@@ -144,6 +144,8 @@ public AESGCMCipher(OpenJCEPlusProvider provider) {
144144 throw provider .providerException ("Failed to initialize cipher context" , e );
145145 }
146146 buffer = new byte [AES_BLOCK_SIZE * 2 ];
147+
148+ this .provider .registerCleanable (this , cleanOCKResources (Key ));
147149 }
148150
149151
@@ -333,7 +335,7 @@ protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[]
333335 }
334336
335337 int ret = GCMCipher .doGCMFinal_Encrypt (ockContext , Key , IV , tagLenInBytes , input ,
336- inputOffset , inputLen , output , outputOffset , authData );
338+ inputOffset , inputLen , output , outputOffset , authData , provider );
337339 authData = null ; // Before returning from doFinal(), restore AAD to uninitialized state
338340
339341 if (generateIV ) {
@@ -356,7 +358,7 @@ protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[]
356358 }
357359
358360 int ret = GCMCipher .doGCMFinal_Decrypt (ockContext , Key , IV , tagLenInBytes , input ,
359- inputOffset , inputLen , output , outputOffset , authData );
361+ inputOffset , inputLen , output , outputOffset , authData , provider );
360362 authData = null ; // Before returning from doFinal(), restore AAD to uninitialized state
361363 return ret ;
362364 }
@@ -989,7 +991,7 @@ protected int doUpdate(byte[] input, int inputOffset, int inputLen, byte[] outpu
989991 if (!encrypting ) {
990992 // OCKDebug.Msg(debPrefix, methodName, "Calling do_GCM_InitForUpdateDecrypt");
991993 outLen = GCMCipher .do_GCM_InitForUpdateDecrypt (ockContext , Key , IV ,
992- tagLenInBytes , buffer , 0 , len , output , outputOffset , authData );
994+ tagLenInBytes , buffer , 0 , len , output , outputOffset , authData , provider );
993995 // OCKDebug.Msg(debPrefix, methodName, "returning ret from
994996 // InitForUpdateDecrypt=" + outLen);
995997 } else {
@@ -1007,7 +1009,7 @@ protected int doUpdate(byte[] input, int inputOffset, int inputLen, byte[] outpu
10071009 }
10081010 // OCKDebug.Msg(debPrefix, methodName, "Calling do_GCM_InitForUpdateEncrypt");
10091011 outLen = GCMCipher .do_GCM_InitForUpdateEncrypt (ockContext , Key , IV ,
1010- tagLenInBytes , buffer , 0 , len , output , outputOffset , authData );
1012+ tagLenInBytes , buffer , 0 , len , output , outputOffset , authData , provider );
10111013 // OCKDebug.Msg(debPrefix, methodName, "returning ret from
10121014 // InitForUpdateEncrypt=" + outLen);
10131015 }
@@ -1043,7 +1045,7 @@ protected int doUpdate(byte[] input, int inputOffset, int inputLen, byte[] outpu
10431045 // GCMCipher.do_GCM_UpdateDecrypt");
10441046
10451047 outLen = GCMCipher .do_GCM_UpdForUpdateDecrypt (ockContext , Key , IV ,
1046- tagLenInBytes , buffer , 0 , len , output , outputOffset , authData );
1048+ tagLenInBytes , buffer , 0 , len , output , outputOffset , authData , provider );
10471049
10481050 // OCKDebug.Msg(debPrefix, methodName, "returning ret from
10491051 // GCMCipher.do_GCM_UpdForUpdateDecrypt=" + outLen);
@@ -1055,7 +1057,7 @@ protected int doUpdate(byte[] input, int inputOffset, int inputLen, byte[] outpu
10551057 // OCKDebug.Msg(debPrefix, methodName, "FirstUpdate generateIV");
10561058
10571059 outLen = GCMCipher .do_GCM_UpdForUpdateEncrypt (ockContext , Key , IV ,
1058- tagLenInBytes , buffer , 0 , len , output , outputOffset , authData );
1060+ tagLenInBytes , buffer , 0 , len , output , outputOffset , authData , provider );
10591061 // OCKDebug.Msg(debPrefix, methodName, "returning ret from
10601062 // GCMCipher.do_GCM_UpdForUpdateEncrypt=" + outLen);
10611063 // outLen = cipher.encrypt(buffer, 0, len, output, outputOffset);
@@ -1089,13 +1091,13 @@ protected int doUpdate(byte[] input, int inputOffset, int inputLen, byte[] outpu
10891091 if (!encrypting ) {
10901092 outLen = GCMCipher .do_GCM_UpdForUpdateDecrypt (ockContext , Key , IV ,
10911093 tagLenInBytes , buffer , 0 , buffered , output , outputOffset ,
1092- authData );
1094+ authData , provider );
10931095 // outLen = cipher.decrypt(buffer, 0, buffered, output, outputOffset);
10941096 } // decrypting
10951097 else {
10961098 outLen = GCMCipher .do_GCM_UpdForUpdateEncrypt (ockContext , Key , IV ,
10971099 tagLenInBytes , buffer , 0 , buffered , output , outputOffset ,
1098- authData );
1100+ authData , provider );
10991101 // outLen = cipher.encrypt(buffer, 0, buffered, output, outputOffset);
11001102 // encrypt mode. Zero out internal (input) buffer
11011103 Arrays .fill (buffer , (byte ) 0x00 );
@@ -1110,15 +1112,15 @@ protected int doUpdate(byte[] input, int inputOffset, int inputLen, byte[] outpu
11101112
11111113 outLen += GCMCipher .do_GCM_UpdForUpdateDecrypt (ockContext , Key , IV ,
11121114 tagLenInBytes , input , inputOffset , inputConsumed , output ,
1113- outputOffset , authData );
1115+ outputOffset , authData , provider );
11141116 // outLen += cipher.decrypt(input, inputOffset, inputConsumed,
11151117 // output, outputOffset);
11161118 } else {
11171119
11181120
11191121 outLen += GCMCipher .do_GCM_UpdForUpdateEncrypt (ockContext , Key , IV ,
11201122 tagLenInBytes , input , inputOffset , inputConsumed , output ,
1121- outputOffset , authData );
1123+ outputOffset , authData , provider );
11221124
11231125 }
11241126 inputOffset += inputConsumed ;
@@ -1272,13 +1274,13 @@ private int finalNoPadding(byte[] in, int inOfs, byte[] out, int outOfs, int len
12721274
12731275 if (!encrypting ) {
12741276 outLen = GCMCipher .do_GCM_FinalForUpdateDecrypt (ockContext , Key , IV , tagLenInBytes , in ,
1275- inOfs , len , out , outOfs , authData );
1277+ inOfs , len , out , outOfs , authData , provider );
12761278 // OCKDebug.Msg(debPrefix, methodName, "outLen from
12771279 // GCMCipher.do_GCM_FinalForUpdateDecrypt=" + outLen);
12781280
12791281 } else {
12801282 outLen = GCMCipher .do_GCM_FinalForUpdateEncrypt (ockContext , Key , IV , tagLenInBytes , in ,
1281- inOfs , len , out , outOfs , authData );
1283+ inOfs , len , out , outOfs , authData , provider );
12821284 // OCKDebug.Msg(debPrefix, methodName, "outLen from
12831285 // GCMCipher.do_GCM_FinalForUpdateEncrypt=" + outLen);
12841286
@@ -1413,22 +1415,6 @@ private void endDoFinal() {
14131415 diffBlocksize = blockSize ;
14141416 }
14151417
1416- @ Override
1417- protected synchronized void finalize () throws Throwable {
1418- //final String methodName = "finalize";
1419- // OCKDebug.Msg (debPrefix, methodName, "finalize called");
1420- try {
1421-
1422- //JS00684 - Leave cleanup of internal variables to GCMCipher that caches them
1423- if (Key != null ) {
1424- Arrays .fill (Key , (byte ) 0x00 );
1425- Key = null ;
1426- }
1427- } finally {
1428- super .finalize ();
1429- }
1430- }
1431-
14321418 private void checkReinit () {
14331419 if (requireReinit ) {
14341420 throw new IllegalStateException (
@@ -1467,4 +1453,20 @@ private void resetVars(boolean afterFailure) {
14671453 this .buffered = 0 ;
14681454 Arrays .fill (buffer , (byte ) 0x0 );
14691455 }
1456+
1457+ private Runnable cleanOCKResources (byte [] Key ) {
1458+ return () -> {
1459+ try {
1460+ //JS00684 - Leave cleanup of internal variables to GCMCipher that caches them
1461+ if (Key != null ) {
1462+ Arrays .fill (Key , (byte ) 0x00 );
1463+ }
1464+ } catch (Exception e ) {
1465+ if (OpenJCEPlusProvider .getDebug () != null ) {
1466+ OpenJCEPlusProvider .getDebug ().println ("An error occurred while cleaning : " + e .getMessage ());
1467+ e .printStackTrace ();
1468+ }
1469+ }
1470+ };
1471+ }
14701472}
0 commit comments