Understanding and generating Ansi X9.24 TR31 Keyblocks
Creates ASC X9 TR-31 (now ANSI X9.143) key blocks taking inputs of the Key Block Protection Key (KBPK) and the clear key.
This is the Java implementation of work done here https://github.com/peterfillmore/pyTR31. It was used as a starting point and the spec was eventually implemented. Python files are also available in the python folder in the repo and work with python 2.7.
The original python implementation is dated and incomplete (understandably, as it’s 8+ years old).
This code implements TR31 keyblock types
There is a validation implementation, when you get an encrypted keyblock and a KBPK and need to validate the TR31 keyblock received. It will generate all keys for the KBPK supplied, extract the clear key from the TR31 keyblock, generate the MAC from the encrypted block and compare it to the one received.
The Main.java has tests for the various keyblock and key length combinations and is the best place to start and step through the code to understand its inner working.
This keyblocks generated have been tested with the EFTLABS BP-tools simulator by generating the keyblock using the code and pasting the output of (header+encryptedkey+mac) into the simulator to see if it can parse it and show you the clear key that you had encrypted. EFTLAB BP-TOOLS was also used to generate keyblocks and used for validity testing in the code. The samples provided in the ANSI X9 TR 31-2018 have been tested and outputs matched.
The code does padding of the pan, it uses 0x0 to pad (spec says use random values). Byte 0x0 was used to make it deterministic for every run, it can be easily changed to populate the padding array with a secure random generation. Also, the EFTLAB tool uses random bytes, so it may appear the data is not consistent when compared to the code’s output as it will vary for each time you generate the keyblock for the same keys. As long as the clear key is fine, and the MAC can be validated, you are good.
EFTLABS tool used to be freely available, but now its licence has changed, and you need to fill out a form requesting it. I haven’t received any response after filling out the form to download an updated version of their tool.
Thales keyblock is work in progress. Currently, Thales DES keyblock is supported. Thales AES keyblock is not working.
Useful documents to refer to