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 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 0000000000..315419f431
Binary files /dev/null and b/src/test/resources/io/floci/aws/services/paymentcryptography/crypto/tr34-vectors/tr34-2019-annex-b-aes-enveloped-data.der differ
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 0000000000..08b20d482f
Binary files /dev/null and b/src/test/resources/io/floci/aws/services/paymentcryptography/crypto/tr34-vectors/tr34-2019-annex-b-aes-key-block.der differ