Creates Ansi X9.24 TR31 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 availble in the python folder in the repo and work with python 2.7.
The original python implementation is dated and incomplete (understandably as its 8 years old).
This code implements TR31 keblock types
- A :VARIANT BINDING
- B :TDEA KEY DERIVATION BINDING
- Double length 128 bits KBPK
- Triple length 192 bits KBPK
- C :TDEA KEY VARIANT BINDING
- D : AES KEY DERIVATION
- 128 bits KBPK
- 192 bits KBPK
- 256 bits KBPK
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 enrypted 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 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 teh codes 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 vlidated you are good.
EFTLABS tool used to be freely available but now its license 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.
- You need to receive a known TR31 keyblock from an external entity.
- You need to send a known TR31 keyblock to an external entity.
- Implement functionality in your HSM emulator to generate TR31 keyblock. (either you don't have access to a HSM that can do this for you or your HSM doesn't have the licenses required from the vendor for this functionality.
- Useful for local internal testing. (Never use this in production.)
Currently optional header blocks are not supported. Thale keyblock is work in progress. Currently Thales DES keyblock is supported. Thales AES keyblock is not working.
Useful documents to refer to
- Recommendation for Block Cipher Modes of Operation: The CMAC Mode for Authentication
- NIST SP 800-108 Recommendation for Key Derivation Using Pseudorandom Functions (Revised)
- ASC X9 TR 31-2018 (purchase required from ANSI store).