private static byte[] Scp03_encrypt_key(GPKey kek, GPKey key) { try { // Pad with random int n = key.GetLength() % 16 + 1; byte[] plaintext = new byte[n * 16]; SecureRandom sr = new SecureRandom(); sr.NextBytes(plaintext); Array.Copy(key.GetValue(), 0, plaintext, 0, key.GetValue().Length); // encrypt byte[] cgram = GPCrypto.DoEncrypt_AES_CBC(kek.GetValue(), plaintext); //Cipher c = Cipher.getInstance(AES_CBC_CIPHER); //c.init(Cipher.ENCRYPT_MODE, kek.GetEncoded(), null_bytes_16); //byte[] cgram = c.doFinal(plaintext); return(cgram); } catch (Exception e) { throw new Exception("Could not calculate key check value: ", e); } }
public GPKeySet GetSessionKeys(int scp, byte[] kdd, params byte[][] args) { GPKeySet cardKeys = staticKeys; if (diversifier != Diversification.NONE) { cardKeys = Diversify(staticKeys, kdd, diversifier, scp); } System.Diagnostics.Debug.WriteLine(String.Format("card manager keys are diversified from kmc: {0}", Formatting.ByteArrayToHexString(master.GetValue()))); System.Diagnostics.Debug.WriteLine(String.Format("diversified card keys: {0}", cardKeys.ToString())); GPKeySet sessionKeys; if (scp == 1) { if (args.Length != 2) { throw new Exception("SCP01 requires host challenge and card challenge"); } sessionKeys = DeriveSessionKeysSCP01(cardKeys, args[0], args[1]); } else if (scp == 2) { if (args.Length != 1) { throw new Exception("SCP02 requires sequence"); } sessionKeys = DeriveSessionKeysSCP02(cardKeys, args[0], false); } else if (scp == 3) { if (args.Length != 2) { throw new Exception("SCP03 requires host challenge and card challenge"); } sessionKeys = DeriveSessionKeysSCP03(cardKeys, args[0], args[1]); } else { throw new Exception("Dont know how to handle: " + scp); } System.Diagnostics.Debug.WriteLine(String.Format("session keys: {0}", sessionKeys.ToString())); return(sessionKeys); }
// SCP03 related private static byte[] Scp03_mac(GPKey key, byte[] msg, int lengthbits) { return(Scp03_mac(key.GetValue(), msg, lengthbits)); }
// GP 2.2.1 Amendment D v 1.1.1 public static byte[] Scp03_kdf(GPKey key, byte constant, byte[] context, int blocklen_bits) { return(Scp03_kdf(key.GetValue(), constant, context, blocklen_bits)); }