Hello,
I am currently implementing SCP03 for a TSM functionality and using BouncyCastle's aescmac implementation for CMAC.
When deriving the card challenge, keys etc., Amendment D says the fields in derivation data can be re-ordered as long as the order, coding and length of each field is unambiguously defined.
Can someone help me understand how do we establish an order so that it matches with what the chip/card generates?
Here's the sample code which I use to generate card challenge. Am I correct in my implementation? Am I missing something? Any help is greatly appreciated.
private byte[] deriveCardChallenge(byte[] key, int sequenceCounter) {
try {
String label = "0000000000000000000000";
String constant = "02";
String separationIndicator = "00";
String LInteger = "0040";
String counter = "01";
byte[] sequenceBytes = getSequenceBytes(sequenceCounter);
String context = Utils.byteArrayToHex(sequenceBytes) + Utils.byteArrayToHex(AID);
String derivationText = counter + label + constant + separationIndicator + context + LInteger;
byte[] derivationData = Utils.hexToByteArray(derivationText);
Mac mac = Mac.getInstance("aescmac", PROVIDER);
SecretKey macKey = new SecretKeySpec(key, "AES");
mac.init(macKey);
mac.update(derivationData);
byte[] macFull = mac.doFinal();
byte[] cardChallenge = new byte[8];
System.arraycopy(macFull, 0, cardChallenge, 0, 8);
return cardChallenge;
} catch(Exception e){
e.printStackTrace();
}
return null;
}
Thanks!
Sam