@@ -916,7 +916,7 @@ private void authenticateReset() {
916916 */
917917 public short generalAuthenticate (byte [] buffer , short offset , short length ) {
918918
919- final byte CONST_TAG_TEMPLATE = (byte )0x7C ;
919+ final byte CONST_TAG_TEMPLATE = (byte )0x7C ;
920920 final byte CONST_TAG_WITNESS = (byte )0x80 ;
921921 final byte CONST_TAG_CHALLENGE = (byte )0x81 ;
922922 final byte CONST_TAG_RESPONSE = (byte )0x82 ;
@@ -1049,16 +1049,20 @@ public short generalAuthenticate(byte[] buffer, short offset, short length) {
10491049 ISOException .throwIt (ISO7816 .SW_WRONG_DATA );
10501050 }
10511051
1052+ // Encrypt the CHALLENGE data
1053+ length = cspPIV .encrypt (key , scratch , tlvReader .getDataOffset (), length , buffer , (short )0 );
1054+
1055+ cspPIV .zeroise (scratch , (short )0 , LENGTH_SCRATCH );
1056+
10521057 // Write out the response TLV, passing through the block length as an indicative maximum
10531058 tlvWriter .init (scratch , (short )0 , length , CONST_TAG_TEMPLATE );
10541059
10551060 // Create the RESPONSE tag
10561061 tlvWriter .writeTag (CONST_TAG_RESPONSE );
10571062 tlvWriter .writeLength (length );
10581063
1059- // Encrypt the CHALLENGE data and write it to the output buffer
1060- offset = tlvWriter .getOffset ();
1061- offset += cspPIV .encrypt (key , scratch , tlvReader .getDataOffset (), length , scratch , offset );
1064+ // Write the response cryptogram
1065+ offset = Util .arrayCopyNonAtomic (buffer , (short )0 , scratch , tlvWriter .getOffset (), length );
10621066 tlvWriter .setOffset (offset ); // Update the TLV offset value
10631067
10641068 // Finalise the TLV object and get the entire data object length
@@ -1286,16 +1290,18 @@ else if (witnessOffset != 0 && !witnessEmpty && challengeOffset != 0 && !challen
12861290 ISOException .throwIt (ISO7816 .SW_WRONG_LENGTH );
12871291 }
12881292
1293+ // Encrypt the CHALLENGE data
1294+ length = cspPIV .encrypt (key , scratch , tlvReader .getDataOffset (), length , buffer , (short )0 );
1295+
12891296 // Write out the response TLV, passing through the block length as an indicative maximum
12901297 tlvWriter .init (scratch , (short )0 , length , CONST_TAG_TEMPLATE );
12911298
12921299 // Create the RESPONSE tag
12931300 tlvWriter .writeTag (CONST_TAG_RESPONSE );
12941301 tlvWriter .writeLength (length );
12951302
1296- // Encrypt the RESPONSE data and write it to the output buffer
1297- offset = tlvWriter .getOffset ();
1298- offset += cspPIV .encrypt (key , scratch , tlvReader .getDataOffset (), key .getBlockLength (), scratch , offset );
1303+ // Write the response cryptogram
1304+ offset = Util .arrayCopyNonAtomic (buffer , (short )0 , scratch , tlvWriter .getOffset (), length );
12991305 tlvWriter .setOffset (offset ); // Update the TLV offset value
13001306
13011307 // Finalise the TLV object and get the entire data object length
0 commit comments