Tools

    TR-31 key block decoder and builder

    Decode a TR-31 / X9.143 key block header; unwrap or build one with a test KBPK.

    • Runs in your browser
    • No sign-up

    Input

    The whole key block, header first.

    MAC verified, key unwrapped.

    Result

    MAC verified
    3F419E1CB7079442AA37474C2EFBF8B8
    Key length
    128 bits (16 bytes)
    KCV
    57C409
    Header
    FieldValueMeaning
    Versionchars 1BTDES key derivation binding
    Key block lengthchars 2–5008080 characters
    Key usagechars 6–7P0PIN encryption key
    Algorithmchars 8TTriple DES
    Mode of usechars 9EEncrypt / wrap only
    Key version numberchars 10–1100Not used
    Exportabilitychars 12EExportable under a trusted key (X9.24 key block)
    Optional blockschars 13–14000 optional blocks
    Reserved / key contextchars 15–1600Not used
    Steps
    KBPK KCVF7BAA8
    Key block encryption key (KBEK): TDES-CMAC of the KBPK over the derivation input698832F8778A7CFCBC79559DAB07B88A
    Key block MAC key (KBAK)DD6CEEC1782D8453671BF8358AF9DB47
    Clear key data (length ‖ key ‖ padding)00803F419E1CB7079442AA37474C2EFBF8B81C2965473CE2
    Padding1C2965473CE2
    Encrypted key data94B420079CC80BA3461F86FE26EFC4A3B8E4FA4C5F534117
    MAC in the block6EED7B727B8A248E
    MAC computed6EED7B727B8A248E

    Exchanging keys under TR-31 with a processor or HSM? IDNTEQ coordinates key hand-offs. Card-program implementation in Brazil

    About this tool

    Runs in your browser. This page loads no analytics or third-party scripts and does not send what you type.

    Paste a key block to read each header field and optional block with its meaning. Add the test KBPK to verify the MAC and get the clear key with its KCV, or switch to Build to wrap a key.

    Versions

    Versions B (TDES) and D (AES) derive an encryption key and a MAC key from the KBPK with CMAC. The MAC covers the header and the clear key data, and is also the IV that encrypts the key data. Versions A and C derive the two keys by XOR with 45 and 4D bytes and use a 4-byte CBC-MAC; A is deprecated. Version E (ISO 20038) headers are read but not unwrapped.

    Worked example

    TR-31:2018 example A.7.2.2: KBPK DD7515F2BFC17F85CE48F3CA25CB21F6 and the block B0080P0TE00E0000… unwrap to the PIN key 3F419E1CB7079442AA37474C2EFBF8B8, KCV 57C409. The header reads: version B, 80 characters, usage P0 (PIN encryption), algorithm T (TDES), mode E (encrypt only), no key version, exportable under a trusted key, no optional blocks.

    Building a block

    The clear key data is a 2-byte key length in bits, the key and padding to a whole number of cipher blocks. Padding is random unless entered; enter the padding of a published block to rebuild it exactly. A PB block is added when the optional blocks do not end on a cipher block boundary.

    Spec references

    • ASC X9 TR-31:2018 (key block versions A, B, C and D)
    • ANSI X9.143:2021 (header fields and optional blocks)
    • NIST SP 800-38B (CMAC)
    • ANSI X9.24-1:2017 Annex A (key check values)

    Verified against

    Specifications
    • ASC X9 TR-31:2018 (key block versions A, B, C, D; Annex A.7 examples)
    • ANSI X9.143:2021 (header fields, optional blocks)
    • NIST SP 800-38B (CMAC)
    Published examples in the tests
    Other checks in the tests
    • Each published block is rebuilt byte for byte from its fields, key and padding
    • CMAC key derivation against a CMAC built on OpenSSL; wrap and unwrap round trips for every version and KBPK length
    • A wrong MAC or KBPK gives no key; malformed headers and lengths are errors
    Inputs
    A key block (versions A–D to unwrap, any version to read the header) and a KBPK in hex; to build, the header fields, optional blocks, key and optional padding.
    Not covered
    Version E (ISO 20038) and TR-34 blocks are not unwrapped. Optional blocks other than KC, KP, HM and BI are shown as text, not checked.
    Last reviewed