mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(paymentcryptography): the modes and the keyed hash the reference names (#8984)
This commit is contained in:
1 parent
66dbdd2935
commit
09125016fe
12 files changed
+1272
-148
No files matched your search
+173
@@ -0,0 +1,173 @@
|
||||
package io.floci.aws.services.paymentcryptography;
|
||||
|
||||
import io.floci.aws.services.paymentcryptography.crypto.BlockMode;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* The block cipher modes the reference names, and the bytes each of them produces.
|
||||
*
|
||||
* <p>The values are the ones the standard's own examples give: the key, the vector and the
|
||||
* plain text are the ones NIST SP 800-38A uses for its AES-128 examples, so a mode that
|
||||
* agrees with them agrees with every other implementation of the standard.
|
||||
*/
|
||||
class PaymentCryptographyBlockModeTest {
|
||||
|
||||
/** The key the standard's AES-128 examples use. */
|
||||
private static final String KEY = "2b7e151628aed2a6abf7158809cf4f3c";
|
||||
|
||||
/** The vector the standard's AES-128 examples use. */
|
||||
private static final String VECTOR = "000102030405060708090a0b0c0d0e0f";
|
||||
|
||||
/** The plain text the standard's AES-128 examples use. */
|
||||
private static final String PLAIN_TEXT = "6bc1bee22e409f96e93d7e117393172a"
|
||||
+ "ae2d8a571e03ac9c9eb76fac45af8e51"
|
||||
+ "30c81c46a35ce411e5fbc1191a0a52ef"
|
||||
+ "f69f2445df4f9b17ad2b417be66c3710";
|
||||
|
||||
/** The cipher text the standard's example for the electronic code book gives. */
|
||||
private static final String ECB = "3ad77bb40d7a3660a89ecaf32466ef97"
|
||||
+ "f5d3d58503b9699de785895a96fdbaaf"
|
||||
+ "43b1cd7f598ece23881b00e3ed0306887b"
|
||||
+ "0c785e27e8ad3f8223207104725dd4";
|
||||
|
||||
/** The cipher text the standard's example for the chaining mode gives. */
|
||||
private static final String CBC = "7649abac8119b246cee98e9b12e9197d"
|
||||
+ "5086cb9b507219ee95db113a917678b2"
|
||||
+ "73bed6b8e3c1743b7116e69e222295163"
|
||||
+ "ff1caa1681fac09120eca307586e1a7";
|
||||
|
||||
/** The cipher text of the feedback mode over the cipher's own block. */
|
||||
private static final String CFB128 = "3b3fd92eb72dad20333449f8e83cfb4a"
|
||||
+ "c8a64537a0b3a93fcde3cdad9f1ce58b"
|
||||
+ "26751f67a3cbb140b1808cf187a4f4df"
|
||||
+ "c04b05357c5d1c0eeac4c66f9ff7f2e6";
|
||||
|
||||
/** The cipher text of the output feedback mode. */
|
||||
private static final String OFB = "3b3fd92eb72dad20333449f8e83cfb4a"
|
||||
+ "7789508d16918f03f53c52dac54ed825"
|
||||
+ "9740051e9c5fecf64344f7a82260edcc"
|
||||
+ "304c6528f659c77866a510d9c1d6ae5e";
|
||||
|
||||
/** The cipher text of the feedback mode over eight bits. */
|
||||
private static final String CFB8 = "3b79424c9c0dd436bace9e0ed4586a4f"
|
||||
+ "32b9ded50ae3ba69d472e88267fb5052"
|
||||
+ "70cbad1e257691f7c47c5038297edda32"
|
||||
+ "ff26d0ed19174096161ecc14086dd62";
|
||||
|
||||
/** The cipher text of the feedback mode over sixty-four bits. */
|
||||
private static final String CFB64 = "3b3fd92eb72dad20764bc8b40ee0de40"
|
||||
+ "f857ab76f3e7bc33332265ff0594b12e"
|
||||
+ "6c8bf2f3fc1ba87b2f124a56f7fe88d23"
|
||||
+ "41f1d0535f0d56e58287bbec2952b2a";
|
||||
|
||||
/** The cipher text of the feedback mode over a single bit. */
|
||||
private static final String CFB1 = "68b3a264f838f5f8c3101070d1ab4c2e"
|
||||
+ "22e7f950383a0b71ade4fad0095cb188"
|
||||
+ "a57972c3c1882615f7511411fbebf1193"
|
||||
+ "997069704fc1d1f27028434c99e60f4";
|
||||
|
||||
@Test
|
||||
void everyModeTheReferenceNamesIsMade() {
|
||||
for (String name : new String[] {"ECB", "CBC", "CFB", "CFB1", "CFB8", "CFB64", "CFB128",
|
||||
"OFB"}) {
|
||||
assertEquals(name, BlockMode.from(name).name());
|
||||
}
|
||||
assertEquals(null, BlockMode.from("ROT13"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theElectronicCodeBookModeIsTheOneTheStandardGives() {
|
||||
assertEquals(ECB, cipherText(BlockMode.ECB, null));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theChainingModeIsTheOneTheStandardGives() {
|
||||
assertEquals(CBC, cipherText(BlockMode.CBC, VECTOR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theFeedbackModeOverTheBlockIsTheOneTheStandardGives() {
|
||||
assertEquals(CFB128, cipherText(BlockMode.CFB128, VECTOR));
|
||||
// The feedback mode with no number is the one over the cipher's own block.
|
||||
assertEquals(CFB128, cipherText(BlockMode.CFB, VECTOR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theOutputFeedbackModeIsTheOneTheStandardGives() {
|
||||
assertEquals(OFB, cipherText(BlockMode.OFB, VECTOR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theFeedbackModeOverEightBitsIsTheOneAnotherImplementationComputes() {
|
||||
assertEquals(CFB8, cipherText(BlockMode.CFB8, VECTOR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theFeedbackModeOverSixtyFourBitsIsTheOneAnotherImplementationComputes() {
|
||||
assertEquals(CFB64, cipherText(BlockMode.CFB64, VECTOR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theFeedbackModeOverOneBitIsTheOneTheStandardGives() {
|
||||
assertEquals(CFB1, cipherText(BlockMode.CFB1, VECTOR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyModeDecryptsWhatItEncrypted() {
|
||||
for (BlockMode mode : BlockMode.values()) {
|
||||
byte[] key = PaymentCryptographyCrypto.fromHex(KEY);
|
||||
byte[] vector = PaymentCryptographyCrypto.fromHex(VECTOR);
|
||||
byte[] plain = PaymentCryptographyCrypto.fromHex(PLAIN_TEXT);
|
||||
byte[] cipherText = mode.takesVector()
|
||||
? PaymentCryptographyCrypto.encrypt(key, plain, vector, mode)
|
||||
: PaymentCryptographyCrypto.encrypt(key, plain, null, mode);
|
||||
byte[] back = mode.takesVector()
|
||||
? PaymentCryptographyCrypto.decrypt(key, cipherText, vector, mode)
|
||||
: PaymentCryptographyCrypto.decrypt(key, cipherText, null, mode);
|
||||
assertArrayEquals(plain, back, mode.name());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void theModeThatFeedsABlockBackIsTheOneTheBlockHolds() {
|
||||
// The feedback mode over the block enciphers the first block under the vector, so
|
||||
// the first cipher text block is the vector enciphered.
|
||||
byte[] key = PaymentCryptographyCrypto.fromHex(KEY);
|
||||
byte[] vector = PaymentCryptographyCrypto.fromHex(VECTOR);
|
||||
byte[] cipherText = PaymentCryptographyCrypto.encrypt(key,
|
||||
PaymentCryptographyCrypto.fromHex(PLAIN_TEXT), vector, BlockMode.CFB128);
|
||||
assertEquals("3b3fd92eb72dad20333449f8e83cfb4a",
|
||||
PaymentCryptographyCrypto.toHex(cipherText).substring(0, 32));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aModeThatTakesNoVectorIsEncryptedWithoutOne() {
|
||||
byte[] key = PaymentCryptographyCrypto.fromHex(KEY);
|
||||
byte[] plain = PaymentCryptographyCrypto.fromHex(PLAIN_TEXT);
|
||||
assertArrayEquals(plain,
|
||||
PaymentCryptographyCrypto.decrypt(key,
|
||||
PaymentCryptographyCrypto.encrypt(key, plain, null, BlockMode.ECB),
|
||||
null, BlockMode.ECB));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aVectorOfTheWrongLengthIsRefused() {
|
||||
byte[] key = PaymentCryptographyCrypto.fromHex(KEY);
|
||||
byte[] plain = PaymentCryptographyCrypto.fromHex(PLAIN_TEXT);
|
||||
assertThrows(IllegalArgumentException.class, () -> PaymentCryptographyCrypto.encrypt(key,
|
||||
plain, PaymentCryptographyCrypto.fromHex("00112233"), BlockMode.CBC));
|
||||
}
|
||||
|
||||
/** The cipher text of the plain text under the key, in the mode. */
|
||||
private static String cipherText(BlockMode mode, String vector) {
|
||||
byte[] key = PaymentCryptographyCrypto.fromHex(KEY);
|
||||
byte[] plain = PaymentCryptographyCrypto.fromHex(PLAIN_TEXT);
|
||||
byte[] iv = vector == null ? null : PaymentCryptographyCrypto.fromHex(vector);
|
||||
return PaymentCryptographyCrypto.toHex(
|
||||
PaymentCryptographyCrypto.encrypt(key, plain, iv, mode));
|
||||
}
|
||||
}
|
||||
+145
-35
@@ -230,7 +230,9 @@ class PaymentCryptographyCryptoHandlerTest {
|
||||
"GenerationAttributes", iso9797())), "us-east-1");
|
||||
String value = (String) mac.get("Mac");
|
||||
|
||||
assertEquals(8, hexBytes(value).length);
|
||||
// The reference counts the length of a mac in hexadecimal characters, so a length of
|
||||
// eight is four bytes.
|
||||
assertEquals(4, hexBytes(value).length);
|
||||
|
||||
Map<String, Object> verified = handler.verifyMac(request(Map.of(
|
||||
"KeyIdentifier", key.getArn(), "MessageData", message, "Mac", value, "MacLength", 8,
|
||||
@@ -240,56 +242,160 @@ class PaymentCryptographyCryptoHandlerTest {
|
||||
|
||||
@Test
|
||||
void aMacAlgorithmThisServiceDoesNotMakeIsRefused() {
|
||||
// The reference's page for the operation says a HMAC, a CMAC, a DUKPT or an EMV MAC
|
||||
// can be asked for, and its model names the HMAC family, the EMV MAC and the DUKPT
|
||||
// CMAC beside the ones this service makes. This service makes the ISO 9797
|
||||
// algorithms 1 and 3 and the CMAC, and a request that asks for another is refused
|
||||
// with the error that names the ones it makes.
|
||||
// The reference's model names the AS 2805 MAC beside the ones this service makes,
|
||||
// and the EMV MAC and the DUKPT CMAC as the variants of the attributes. A request
|
||||
// that asks for one of them is refused with the error that names the ones it makes.
|
||||
PaymentCryptographyKey key = createTdesKey("Generate", "Verify");
|
||||
String message = hex("a message".getBytes());
|
||||
for (String algorithm : new String[] {"HMAC", "HMAC_SHA256", "AS2805_4_1"}) {
|
||||
ObjectNode attributes = mapper.createObjectNode();
|
||||
attributes.put("Algorithm", algorithm);
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> handler.generateMac(request(Map.of(
|
||||
"KeyIdentifier", key.getArn(), "MessageData", message,
|
||||
"GenerationAttributes", attributes)), "us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertTrue(refused.getMessage().contains("CMAC"), refused.getMessage());
|
||||
}
|
||||
ObjectNode attributes = mapper.createObjectNode();
|
||||
attributes.put("Algorithm", "AS2805_4_1");
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> handler.generateMac(request(Map.of(
|
||||
"KeyIdentifier", key.getArn(), "MessageData", message,
|
||||
"GenerationAttributes", attributes)), "us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertTrue(refused.getMessage().contains("CMAC"), refused.getMessage());
|
||||
for (String variant : new String[] {"EmvMac", "DukptCmac"}) {
|
||||
ObjectNode attributes = mapper.createObjectNode();
|
||||
attributes.put(variant, mapper.createObjectNode());
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
ObjectNode variantAttributes = mapper.createObjectNode();
|
||||
variantAttributes.put(variant, mapper.createObjectNode());
|
||||
AwsException refusedVariant = assertThrows(AwsException.class,
|
||||
() -> handler.generateMac(request(Map.of(
|
||||
"KeyIdentifier", key.getArn(), "MessageData", message,
|
||||
"GenerationAttributes", attributes)), "us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertTrue(refused.getMessage().contains(variant), refused.getMessage());
|
||||
"GenerationAttributes", variantAttributes)), "us-east-1"));
|
||||
assertEquals("ValidationException", refusedVariant.getErrorCode());
|
||||
assertTrue(refusedVariant.getMessage().contains(variant), refusedVariant.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aHashedMacIsComputedAndVerified() {
|
||||
// The reference's guide: a key whose algorithm is a keyed hash carries the hash, and
|
||||
// the request names the mac by the name of the family, so the key sets the hash.
|
||||
PaymentCryptographyKey key = createKeyOf("HMAC_SHA256", "TR31_M7_HMAC_KEY",
|
||||
"Generate", "Verify");
|
||||
String message = hex("a message".getBytes());
|
||||
ObjectNode attributes = mapper.createObjectNode();
|
||||
attributes.put("Algorithm", "HMAC");
|
||||
|
||||
Map<String, Object> mac = handler.generateMac(request(Map.of(
|
||||
"KeyIdentifier", key.getArn(), "MessageData", message,
|
||||
"GenerationAttributes", attributes)), "us-east-1");
|
||||
String value = (String) mac.get("Mac");
|
||||
|
||||
// The length the request asks for is ignored, and the mac is the whole hash.
|
||||
assertEquals(32, hexBytes(value).length);
|
||||
|
||||
Map<String, Object> verified = handler.verifyMac(request(Map.of(
|
||||
"KeyIdentifier", key.getArn(), "MessageData", message, "Mac", value,
|
||||
"VerificationAttributes", attributes)), "us-east-1");
|
||||
assertEquals(key.getArn(), verified.get("KeyArn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aHashedMacUnderAKeyThatCarriesAnotherHashIsRefused() {
|
||||
// The hash a key carries is fixed at its creation, so a request that asks for
|
||||
// another one is refused rather than answered with the hash the key holds.
|
||||
PaymentCryptographyKey key = createKeyOf("HMAC_SHA256", "TR31_M7_HMAC_KEY", "Generate");
|
||||
ObjectNode attributes = mapper.createObjectNode();
|
||||
attributes.put("Algorithm", "HMAC_SHA512");
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> handler.generateMac(request(Map.of(
|
||||
"KeyIdentifier", key.getArn(), "MessageData", hex("a message".getBytes()),
|
||||
"GenerationAttributes", attributes)), "us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertTrue(refused.getMessage().contains("SHA-256"), refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aHashedMacUnderAKeyThatCarriesNoHashIsRefused() {
|
||||
// A key of the hash usage that is not itself a keyed hash key carries no hash, so
|
||||
// the request has to name the one it wants.
|
||||
PaymentCryptographyKey key = createKeyOf("TDES_2KEY", "TR31_M7_HMAC_KEY", "Generate");
|
||||
ObjectNode attributes = mapper.createObjectNode();
|
||||
attributes.put("Algorithm", "HMAC");
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> handler.generateMac(request(Map.of(
|
||||
"KeyIdentifier", key.getArn(), "MessageData", hex("a message".getBytes()),
|
||||
"GenerationAttributes", attributes)), "us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertTrue(refused.getMessage().contains("HMAC_SHA256"), refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aHashedMacKeyOfAnotherUsageIsRefused() {
|
||||
// The hash usage is what binds the key to the mac, so a keyed hash key of another
|
||||
// usage is not a key the service makes.
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> createKeyOf("HMAC_SHA256", "TR31_M1_ISO_9797_1_MAC_KEY", "Generate"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertTrue(refused.getMessage().contains("TR31_M1_ISO_9797_1_MAC_KEY"), refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void anEncryptionModeThisServiceDoesNotMakeIsRefused() {
|
||||
// The reference's model names eight block cipher modes for the symmetric
|
||||
// encryption, and this service makes the CBC one, so a request that names another is
|
||||
// refused rather than answered with a cipher of another kind.
|
||||
// The reference's model names eight block cipher modes for the symmetric encryption,
|
||||
// and this service makes them all, so a request that names another is refused rather
|
||||
// than answered with a cipher of another kind.
|
||||
PaymentCryptographyKey key = createKey("Encrypt", "Decrypt");
|
||||
for (String mode : new String[] {"ECB", "CFB", "CFB8", "OFB"}) {
|
||||
ObjectNode symmetric = mapper.createObjectNode();
|
||||
symmetric.put("Mode", "ROT13");
|
||||
ObjectNode attributes = mapper.createObjectNode();
|
||||
attributes.set("Symmetric", symmetric);
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> handler.encryptData(request(Map.of(
|
||||
"KeyIdentifier", key.getArn(),
|
||||
"PlainText", hex("hello, world!!!!".getBytes()),
|
||||
"EncryptionAttributes", attributes)), "us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertTrue(refused.getMessage().contains("CFB128"), refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void everyModeTheReferenceNamesEncryptsAndDecrypts() {
|
||||
PaymentCryptographyKey key = createKey("Encrypt", "Decrypt");
|
||||
String plain = hex("hello, world!!!!".getBytes());
|
||||
for (String mode : new String[] {"ECB", "CBC", "CFB", "CFB1", "CFB8", "CFB64", "CFB128",
|
||||
"OFB"}) {
|
||||
ObjectNode symmetric = mapper.createObjectNode();
|
||||
symmetric.put("Mode", mode);
|
||||
ObjectNode attributes = mapper.createObjectNode();
|
||||
attributes.set("Symmetric", symmetric);
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> handler.encryptData(request(Map.of(
|
||||
"KeyIdentifier", key.getArn(),
|
||||
"PlainText", hex("hello, world!!!!".getBytes()),
|
||||
"EncryptionAttributes", attributes)), "us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertTrue(refused.getMessage().contains("CBC"), refused.getMessage());
|
||||
|
||||
Map<String, Object> encrypted = handler.encryptData(request(Map.of(
|
||||
"KeyIdentifier", key.getArn(), "PlainText", plain,
|
||||
"EncryptionAttributes", attributes)), "us-east-1");
|
||||
String cipherText = (String) encrypted.get("CipherText");
|
||||
assertEquals(plain.length(), cipherText.length(), mode);
|
||||
|
||||
Map<String, Object> decrypted = handler.decryptData(request(Map.of(
|
||||
"KeyIdentifier", key.getArn(), "CipherText", cipherText,
|
||||
"DecryptionAttributes", attributes)), "us-east-1");
|
||||
assertEquals(plain, decrypted.get("PlainText"), mode);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aModeTheKeyCipherCannotTakeIsRefused() {
|
||||
// The feedback of the feedback modes is as wide as the cipher's own block at the
|
||||
// widest, and a Triple DES block is eight bytes, so the mode whose feedback is a
|
||||
// hundred and twenty-eight bits is not one a Triple DES key takes.
|
||||
PaymentCryptographyKey key = createTdesDataKey("TDES_2KEY", "Encrypt", "Decrypt");
|
||||
ObjectNode symmetric = mapper.createObjectNode();
|
||||
symmetric.put("Mode", "CFB128");
|
||||
ObjectNode attributes = mapper.createObjectNode();
|
||||
attributes.set("Symmetric", symmetric);
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> handler.encryptData(request(Map.of(
|
||||
"KeyIdentifier", key.getArn(), "PlainText", "0011223344556677",
|
||||
"EncryptionAttributes", attributes)), "us-east-1"));
|
||||
assertEquals("ValidationException", refused.getErrorCode());
|
||||
assertTrue(refused.getMessage().contains("CFB128"), refused.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMacUnderAThreeKeyTdesKeyIsComputedAndVerified() {
|
||||
// The ISO 9797 MACs are defined over Triple DES, and the cipher takes the three keys
|
||||
@@ -302,7 +408,9 @@ class PaymentCryptographyCryptoHandlerTest {
|
||||
"GenerationAttributes", iso9797())), "us-east-1");
|
||||
String value = (String) mac.get("Mac");
|
||||
|
||||
assertEquals(8, hexBytes(value).length);
|
||||
// The reference counts the length of a mac in hexadecimal characters, so a length of
|
||||
// eight is four bytes.
|
||||
assertEquals(4, hexBytes(value).length);
|
||||
|
||||
Map<String, Object> verified = handler.verifyMac(request(Map.of(
|
||||
"KeyIdentifier", key.getArn(), "MessageData", message, "Mac", value, "MacLength", 8,
|
||||
@@ -525,7 +633,9 @@ class PaymentCryptographyCryptoHandlerTest {
|
||||
"GenerationAttributes", dukpt("DukptIso9797Algorithm1"))), "us-east-1");
|
||||
String value = (String) mac.get("Mac");
|
||||
|
||||
assertEquals(8, hexBytes(value).length);
|
||||
// The reference counts the length of a mac in hexadecimal characters, so a length of
|
||||
// eight is four bytes.
|
||||
assertEquals(4, hexBytes(value).length);
|
||||
|
||||
Map<String, Object> verified = handler.verifyMac(request(Map.of(
|
||||
"KeyIdentifier", key.getArn(), "MessageData", message, "Mac", value, "MacLength", 8,
|
||||
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package io.floci.aws.services.paymentcryptography;
|
||||
|
||||
import io.floci.aws.services.paymentcryptography.crypto.Ciphers;
|
||||
import org.bouncycastle.crypto.Digest;
|
||||
import org.bouncycastle.crypto.digests.SHA224Digest;
|
||||
import org.bouncycastle.crypto.digests.SHA256Digest;
|
||||
import org.bouncycastle.crypto.digests.SHA384Digest;
|
||||
import org.bouncycastle.crypto.digests.SHA512Digest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* The keyed hash of a message, as RFC 4231 gives it.
|
||||
*
|
||||
* <p>The reference makes the keyed hash of the four hashes it names, and the values here are
|
||||
* the ones the standard's own test cases give, so a hash that agrees with them agrees with
|
||||
* every other implementation of the standard.
|
||||
*/
|
||||
class PaymentCryptographyHmacTest {
|
||||
|
||||
/** The key the standard's first test case uses: twenty bytes of the same byte. */
|
||||
private static final String KEY = "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b";
|
||||
|
||||
/** The message the standard's first test case uses. */
|
||||
private static final String MESSAGE = "4869205468657265";
|
||||
|
||||
@Test
|
||||
void theHashOfSha224IsTheOneTheStandardGives() {
|
||||
assertEquals("896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22",
|
||||
mac(new SHA224Digest()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theHashOfSha256IsTheOneTheStandardGives() {
|
||||
assertEquals("b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c2e32cff7",
|
||||
mac(new SHA256Digest()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theHashOfSha384IsTheOneTheStandardGives() {
|
||||
assertEquals("afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c7cebc59c"
|
||||
+ "faea9ea9076ede7f4af152e8b2fa9cb6",
|
||||
mac(new SHA384Digest()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theHashOfSha512IsTheOneTheStandardGives() {
|
||||
assertEquals("87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b30545e17cde"
|
||||
+ "daa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f1702e696c203a126854",
|
||||
mac(new SHA512Digest()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void theHashOfTheMessageIsAsLongAsTheHash() {
|
||||
assertEquals(32, Ciphers.hmac(PaymentCryptographyCrypto.fromHex(KEY),
|
||||
PaymentCryptographyCrypto.fromHex(MESSAGE), new SHA256Digest()).length);
|
||||
assertEquals(64, Ciphers.hmac(PaymentCryptographyCrypto.fromHex(KEY),
|
||||
PaymentCryptographyCrypto.fromHex(MESSAGE), new SHA512Digest()).length);
|
||||
}
|
||||
|
||||
@Test
|
||||
void theCheckValueOfAKeyedHashKeyIsTheHashOfNothing() {
|
||||
// The reference's terminology: "For HMAC keys, the KCV is computed using the hash
|
||||
// selected at key creation on a zero-length message, taking the leftmost 3 bytes."
|
||||
PaymentCryptographyKeyAlgorithm algorithm =
|
||||
PaymentCryptographyKeyAlgorithm.HMAC_SHA256;
|
||||
String checkValue = PaymentCryptographyCrypto.checkValue(
|
||||
PaymentCryptographyCrypto.fromHex(KEY), algorithm);
|
||||
String expected = PaymentCryptographyCrypto.toHex(
|
||||
Arrays.copyOf(Ciphers.hmac(PaymentCryptographyCrypto.fromHex(KEY),
|
||||
new byte[0], new SHA256Digest()), 3)).toUpperCase(Locale.ROOT);
|
||||
assertEquals(expected, checkValue);
|
||||
}
|
||||
|
||||
/** The keyed hash of the message under the key, in the hash. */
|
||||
private static String mac(Digest digest) {
|
||||
return PaymentCryptographyCrypto.toHex(Ciphers.hmac(
|
||||
PaymentCryptographyCrypto.fromHex(KEY),
|
||||
PaymentCryptographyCrypto.fromHex(MESSAGE), digest));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user