ATM Pin encryption using 3DES

Introduction

Most modern ATM’s use a Triple Des algorithm to encrypt the pin and send it to a host server for processing. Once the host system receives the pin, it does a translation of the pin from one encryption key to another, and sends it to a bank. In this post I will attempt to explain the process and how it is done in the real world.

Overview of the Triple Data Encryption Standard

What we all call Triple DES is EDE (encrypt, decrypt, encrypt). The way that it works is that you take three 56-bit keys, and encrypt with K1, decrypt with K2 and encrypt with K3. There are two-key and three-key versions. Think of the two-key version as merely one where K1=K3. Note that if K1=K2=K3, then Triple DES is really Single DES.
Triple DES was created back when DES was getting a bit weaker than people were comfortable with. As a result, they wanted an easy way to get more strength. In a system dependent on DES, making a composite function out of multiple DESes is likely to be easier than bolting in a new cipher and sidesteps the political issue of arguing that the new cipher is better than DES.
As it turns out, when you compose a cipher into a new one, you can’t use a double enciphering. There is a class of attacks called meet-in-the-middle attacks, in which you encrypt from one end, decrypt from the other, and start looking for collisions (things that give you the same answer). With sufficient memory, Double DES (or any other cipher) would only be twice as strong as the base cipher — or one bit more in strength.
There’s more to it. If the cipher forms a group, then encrypting twice with two keys is equivalent to encrypting once with some key. Now, it’s not trivial to know what that other key is, but it means that a brute-force attack would find that third key as it tried all possible single-keys. So if the cipher’s a group, then multiple-ciphering is merely a waste of time.
Applying this encryption in Java is trivial and it could be done since earlier versions:
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

public class TDesTests {

 @Test
 public void TDesEncript() throws HexStringException {
  byte[] result = new byte[8];
  byte[] key = new byte[] { 1, 1, 1, 1, 1, 1, 1, 1, 
                      2, 2, 2, 2, 2, 2, 2, 2, 
                      3, 3, 3, 3, 3, 3, 3, 3 };
  byte[] data = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
  byte[] expected = new byte[] { -50, -83, 55, 61, -72, 14, -85, -8 };
  try {
   KeySpec specChave = new DESedeKeySpec(key);
   SecretKey secretKey = SecretKeyFactory.getInstance("DESede")
     .generateSecret(specChave);
   Cipher cipher = Cipher.getInstance("DESede/ECB/NoPadding");
   cipher.init(Cipher.DECRYPT_MODE, secretKey);
   result = cipher.doFinal(data);
  } catch (Exception e) {
   e.printStackTrace();
  }
  Assert.assertArrayEquals(expected, result);
 }
}

ATM Internals and how they calculate the keys

When you have an ATM, you typically need to provide it with a set of encryption keys from your host, or HSM. These keys are clear text keys and it’s not encrypted in any way. Your host will link them to your terminal number, and when the ATM encrypts the pin; the host will know what keys are used so it can decrypt / translate them to the bank. The clear keys are never stored by the host, only the LMK encrypted keys.
Lets assume your host provides the following keys to you as the ‘ATM Encryption Key’ :
Clear component A: 67C4 A719 1ADA FD08 6432 CE0D D638 4AB
Key check value: 20D40B
Clear component B: 8A89 6D4C 4625 5E2A 1A75 2002 07A7 D35E
Key check value: 4EC801
Combined Check Value: 2B547D
Now typically you would enter the clear components into the ATM, as Encryption keys, and the ATM will combine them (Basically XOR Them) and derive the check value. If the check value match, then all is good.
What happens at your host end is the following:
Your host will also combine the keys and encrypt them under the LMK (Local Master Key). It will then use this key to encrypt all other keys that are sent to the ATM.
Now the ATM have a Terminal Master Key that it can use to decrypt all keys that are sent to it from the host.

ATM Configuration Request (Key Exchange)

Now when an ATM starts up, the first thing it does it send a configuration request to the host. This request is to get the Third key used in Triple DES.  The Host will generate a random Terminal Pin Key and encrypt it under the Terminal Master Key (TMK). Since the ATM has the Terminal Master Key, it can decrypt the encrypted TPK, and use all 3 Keys now for the Triple DES operations. (it actually uses 2)
The Host would generally execute the A0 Thales command to get this key. He would store the key in the key database to do the decryption / translation later.

Pin Encryption / Decryption

When a ATM gets ready to transmit a transaction it does the 3DES operation on the Pin only. the cypher text is now transmitted to the host. The host never knows the pin code, and only does a translation of the pin from the terminal keys to the bank keys.
The Host will have the following:
(ZPK) Zone Pin Key – from the Bank during Host to Bank Key exchange
(TPK) Terminal Pin Key – from Terminal using Terminal Configuration Request
(PAN) Account Number –  from Transaction transmitted.
The transaction can now be transmitted to the acquiring bank with the translated pin for processing.

Comentários

Postagens mais visitadas deste blog

How use the LZMA SDK (7Zip)

How config Maven to work with NTLM proxy

Como utilizar a LZMA SDK (7Zip)