From 42707716cdd362b2538038eba195935ef5dcef4d Mon Sep 17 00:00:00 2001 From: Murtuza Chhil Date: Fri, 20 Nov 2026 16:39:32 +0530 Subject: [PATCH] test(paymentcryptography): the TR-34 codec answers the standard's own vectors (#9079) --- .../paymentcryptography/crypto/Tr34.java | 9 +- .../PaymentCryptographyTr34VectorTest.java | 127 ++++++++++++++++++ .../crypto/tr34-vectors/README.md | 35 +++++ .../tr34-2019-annex-b-aes-enveloped-data.der | Bin 0 -> 610 bytes .../tr34-2019-annex-b-aes-key-block.der | Bin 0 -> 133 bytes 5 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 src/test/java/io/floci/aws/services/paymentcryptography/crypto/PaymentCryptographyTr34VectorTest.java create mode 100644 src/test/resources/io/floci/aws/services/paymentcryptography/crypto/tr34-vectors/README.md create mode 100644 src/test/resources/io/floci/aws/services/paymentcryptography/crypto/tr34-vectors/tr34-2019-annex-b-aes-enveloped-data.der create mode 100644 src/test/resources/io/floci/aws/services/paymentcryptography/crypto/tr34-vectors/tr34-2019-annex-b-aes-key-block.der diff --git a/src/main/java/io/floci/aws/services/paymentcryptography/crypto/Tr34.java b/src/main/java/io/floci/aws/services/paymentcryptography/crypto/Tr34.java index 0fc4c60957..db87651256 100644 --- a/src/main/java/io/floci/aws/services/paymentcryptography/crypto/Tr34.java +++ b/src/main/java/io/floci/aws/services/paymentcryptography/crypto/Tr34.java @@ -680,8 +680,13 @@ public final class Tr34 { return new Recovered(HEX_UPPER.formatHex(key), header); } - /** The SHA-256 digest of the given bytes. */ - private static byte[] digest(byte[] data) { + /** + * The SHA-256 digest of the given bytes. + * + *

The digest is not private, so the vector test can check the samples it reads + * against the digests the standard published with them. + */ + static byte[] digest(byte[] data) { try { return MessageDigest.getInstance("SHA-256").digest(data); } catch (GeneralSecurityException e) { diff --git a/src/test/java/io/floci/aws/services/paymentcryptography/crypto/PaymentCryptographyTr34VectorTest.java b/src/test/java/io/floci/aws/services/paymentcryptography/crypto/PaymentCryptographyTr34VectorTest.java new file mode 100644 index 0000000000..d2f5c63e3c --- /dev/null +++ b/src/test/java/io/floci/aws/services/paymentcryptography/crypto/PaymentCryptographyTr34VectorTest.java @@ -0,0 +1,127 @@ +package io.floci.aws.services.paymentcryptography.crypto; + +import org.bouncycastle.asn1.ASN1OctetString; +import org.bouncycastle.asn1.ASN1Sequence; +import org.bouncycastle.asn1.ASN1TaggedObject; +import org.junit.jupiter.api.Test; + +import java.io.InputStream; +import java.util.HexFormat; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * The TR-34 key block codec, against the vectors the standard publishes. + * + *

The vectors are the AES samples of ASC X9 TR-34-2019 Annex B, in + * {@code tr34-vectors/} beside this test's resources. They are the standard's own + * byte values, not ones this branch produced, so a codec that drifts from the + * published encoding fails here even when it still round-trips its own blocks. + * + *

What is under test is the key block reader. The first sample is a key block in + * the clear and the second is one inside an envelope, so the reader answers two key + * blocks the standard published rather than one. + */ +public class PaymentCryptographyTr34VectorTest { + + private static final HexFormat HEX = HexFormat.of(); + + /** The key the standard's AES samples carry, in clear. */ + private static final String STANDARD_KEY = "0123456789ABCDEFFEDCBA9876543210"; + + /** The digests the standard's samples have, so a changed vector is named. */ + private static final Map DIGESTS = Map.of( + "tr34-2019-annex-b-aes-key-block.der", + "eceeedaa129dc583df2ac6a0d68d9b89247ea9e18b005eff4cdb617a5f0e7fc2", + "tr34-2019-annex-b-aes-enveloped-data.der", + "fb3e8b94bf22c4b456804d7145832315e4366937296c773f5cbc26dd39adbf71"); + + @Test + void readsTheStandardAesKeyBlock() throws Exception { + // B.2.2.2.4: the clear AES key block, with its header in the clear. + Tr34.Recovered recovered = Tr34.readKeyBlock(vector("tr34-2019-annex-b-aes-key-block.der")); + assertEquals(STANDARD_KEY, recovered.keyHex); + assertEquals("D0256K0AB00E0000", recovered.header); + } + + @Test + void opensTheStandardAesEnvelopedDataUnderThePublishedEphemeralKey() throws Exception { + // B.2.2.3.2: the AES key block encrypted under the published ephemeral key. + // + // The sample is opened here rather than by the codec's reader, because the + // sample is a standard CMS EnvelopedData and the reader takes the shape the + // reference writes. What the reader is given is the key block the sample + // carries, which is the codec under test. + byte[] envelope = vector("tr34-2019-annex-b-aes-enveloped-data.der"); + byte[] iv = envelopeIv(envelope); + byte[] ciphertext = envelopeCiphertext(envelope); + + // The sample serializes the IV over eight bytes; the sixteen-byte IV that + // produced the ciphertext is the zero-extended one, which the first CBC + // block also gives back. + assertArrayEquals(HEX.parseHex("0123456789ABCDEF"), iv); + assertEquals(144, ciphertext.length); + + byte[] plain = Ciphers.aesCbcDecrypt(ciphertext, HEX.parseHex(STANDARD_KEY), + HEX.parseHex("0123456789ABCDEF0000000000000000")); + // PKCS#7 padding of eleven bytes, so the key block is the first 133 bytes. + int padding = plain[plain.length - 1]; + assertEquals(11, padding); + assertEquals(133, plain.length - padding); + + Tr34.Recovered recovered = Tr34.readKeyBlock(Bytes.pkcs7Unpad(plain, 16)); + assertEquals(STANDARD_KEY, recovered.keyHex); + // The decrypted block carries the header the standard uses for this sample, + // not the header of the separately published clear key block. + assertEquals("A0256K0TB00E0000", recovered.header); + } + + /** The IV of the sample's content encryption, as the standard wrote it. */ + private static byte[] envelopeIv(byte[] envelope) { + return ASN1OctetString.getInstance(contentEncryption(envelope).getObjectAt(1)).getOctets(); + } + + /** The encrypted key block of the sample, under its {@code [0]} tag. */ + private static byte[] envelopeCiphertext(byte[] envelope) { + return ASN1OctetString.getInstance(ASN1TaggedObject.getInstance( + contentEncryption(envelope).getObjectAt(2)), false).getOctets(); + } + + /** + * The content-encryption sequence of the sample's EnvelopedData. + * + *

The sample is a standard CMS EnvelopedData: a sequence of the version, the + * recipients and the encrypted content information, and that last one names the + * content encryption and carries the IV and the ciphertext beside the algorithm's + * name. + * + *

This is not the shape the codec's reader takes. The reference writes the + * envelope as three bare objects, the version, the recipients and the encrypted + * content information, and the reader reads it that way, so it would refuse this + * sample. + */ + private static ASN1Sequence contentEncryption(byte[] envelope) { + ASN1Sequence encryptedContentInfo = ASN1Sequence.getInstance( + ASN1Sequence.getInstance(envelope).getObjectAt(2)); + return ASN1Sequence.getInstance(encryptedContentInfo.getObjectAt(1)); + } + + /** + * A vector, by the file name beside this test's resources. + * + *

The digest is checked as the vector is read, so one that was altered or + * mangled in the tree is named as such rather than failing as a parse error. + */ + private static byte[] vector(String name) throws Exception { + byte[] bytes; + try (InputStream in = PaymentCryptographyTr34VectorTest.class + .getResourceAsStream("tr34-vectors/" + name)) { + bytes = in.readAllBytes(); + } + assertEquals(DIGESTS.get(name), HEX.formatHex(Tr34.digest(bytes)), + "the vector " + name + " is not the one the standard published"); + return bytes; + } +} diff --git a/src/test/resources/io/floci/aws/services/paymentcryptography/crypto/tr34-vectors/README.md b/src/test/resources/io/floci/aws/services/paymentcryptography/crypto/tr34-vectors/README.md new file mode 100644 index 0000000000..290cc2d557 --- /dev/null +++ b/src/test/resources/io/floci/aws/services/paymentcryptography/crypto/tr34-vectors/README.md @@ -0,0 +1,35 @@ +# TR-34 test vectors + +Test vectors for the `Tr34` codec, from the informative samples in the ASC X9 +TR-34-2019 standard, Annex B. + +- `tr34-2019-annex-b-aes-key-block.der` + - Annex B.2.2.2.4, sample AES KeyBlock. + - SHA-256: `eceeedaa129dc583df2ac6a0d68d9b89247ea9e18b005eff4cdb617a5f0e7fc2` + - Clear key `0123456789ABCDEFFEDCBA9876543210`, header `D0256K0AB00E0000`. + +- `tr34-2019-annex-b-aes-enveloped-data.der` + - Annex B.2.2.3.2, sample AES EnvelopedData. + - SHA-256: `fb3e8b94bf22c4b456804d7145832315e4366937296c773f5cbc26dd39adbf71` + - Ephemeral AES key `0123456789ABCDEFFEDCBA9876543210`; 144 bytes of + AES-128-CBC ciphertext of a 133-byte key block. That key block carries the + same clear key under the header `A0256K0TB00E0000`, which is not the header + of the separately published clear key block. + - The sample serializes the AES-CBC IV as the eight bytes + `0123456789ABCDEF`; the 16-byte IV that produced the ciphertext is the + zero-extended form `0123456789ABCDEF0000000000000000`, which is + recoverable from the first CBC block. + - The sample is a standard CMS EnvelopedData, a sequence of the version, the + recipients and the encrypted content information. The reference writes the + envelope of a TR-34 block as three bare objects instead, which is the shape + `Tr34.recoverAndVerify` reads, so the sample is opened by the test rather + than by the codec. + +The vectors are the standard's published byte values, kept as-is. They are +compared against independent results recorded by the paysec project +(https://github.com/5n00py/paysec, `crates/paysec-tr34/tests/tr34_2019_aes_vectors.rs`), +which extracted the same samples. + +The test checks the SHA-256 of each vector as it reads it, so a sample that was +altered or mangled in the tree is named as such rather than failing as a parse +error. diff --git a/src/test/resources/io/floci/aws/services/paymentcryptography/crypto/tr34-vectors/tr34-2019-annex-b-aes-enveloped-data.der b/src/test/resources/io/floci/aws/services/paymentcryptography/crypto/tr34-vectors/tr34-2019-annex-b-aes-enveloped-data.der new file mode 100644 index 0000000000000000000000000000000000000000..315419f4315070af04ec6b4c37d0b950f04bd677 GIT binary patch literal 610 zcmXqLVv1v8WH4-EoM+I)I19)$@G@{TBtgQ=!a_*G3eJuS-a#%*tR@T$4D1H3Y@Awc9&O)w85!9PEDU(rI1}1D z7*m;9m>5|Z3?vY;90uG-av+5;eVi-|EKQ6IF#^$_uIxFm@yXzn(3zq>086m*NMS0_oX>skAWYgTz# zo#*6Ki|DH49X~8vwSWI@<>x;a_R+Uxf6< z+!l~{|GMDk2G+|ZfnXmFYdE<=6#szH|@W?{BB;>%PDoA zOJ#R2Rj=4Ssn_hE@-qR)?h^Cqf6ec#a{b+T zW_duBMX4OqtNqT3&O4W}8GH#o^xW*<*(>w?l9In^`a1MTU0EKwZTj;d*ZvxD0c$5N P@2%Q%?ybnERc`?Rkvj3% literal 0 HcmV?d00001 diff --git a/src/test/resources/io/floci/aws/services/paymentcryptography/crypto/tr34-vectors/tr34-2019-annex-b-aes-key-block.der b/src/test/resources/io/floci/aws/services/paymentcryptography/crypto/tr34-vectors/tr34-2019-annex-b-aes-key-block.der new file mode 100644 index 0000000000000000000000000000000000000000..08b20d482f4445c0718c5c59a8ee79bbb5fdca1b GIT binary patch literal 133 zcmXqLY+_<$H1INTG~_nmWMd9xVH0Kw4K@@t5C(C$gn2@Oj7=1R6LSl4Qi~0x4J1Lr z%)&xQ!V1og3f?XrOspmh3=C{60*uP8>7A?3zW;Y;*Nn0dBLM?>HcqWJkGAi;jO>ht WLM#F<21cf4-Ug0N1_rJMKmY(doE{wj literal 0 HcmV?d00001