use of com.google.crypto.tink.Mac in project tink by google.
the class AesCmacTest method testTagTruncation.
@Test
public void testTagTruncation() throws Exception {
for (MacTestVector t : CMAC_TEST_VECTORS) {
Mac mac = new AesCmac(t.key, t.tag.length);
for (int j = 1; j < t.tag.length; j++) {
byte[] modifiedTag = Arrays.copyOf(t.tag, t.tag.length - j);
try {
mac.verifyMac(modifiedTag, t.message);
fail("Invalid MAC, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
}
}
// Test with random keys.
for (MacTestVector t : CMAC_TEST_VECTORS) {
Mac mac = new AesCmac(Random.randBytes(t.key.length), t.tag.length);
for (int j = 1; j < t.tag.length; j++) {
byte[] modifiedTag = Arrays.copyOf(t.tag, t.tag.length - j);
try {
mac.verifyMac(modifiedTag, t.message);
fail("Invalid MAC, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
}
}
}
use of com.google.crypto.tink.Mac in project tink by google.
the class AesCmacTest method testBitFlipTag.
@Test
public void testBitFlipTag() throws Exception {
for (MacTestVector t : CMAC_TEST_VECTORS) {
Mac mac = new AesCmac(t.key, t.tag.length);
for (int b = 0; b < t.tag.length; b++) {
for (int bit = 0; bit < 8; bit++) {
byte[] modifiedTag = Arrays.copyOf(t.tag, t.tag.length);
modifiedTag[b] = (byte) (modifiedTag[b] ^ (1 << bit));
try {
mac.verifyMac(modifiedTag, t.message);
fail("Invalid MAC, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
}
}
}
// Test with random keys.
for (MacTestVector t : CMAC_TEST_VECTORS) {
Mac mac = new AesCmac(Random.randBytes(t.key.length), t.tag.length);
for (int b = 0; b < t.tag.length; b++) {
for (int bit = 0; bit < 8; bit++) {
byte[] modifiedTag = Arrays.copyOf(t.tag, t.tag.length);
modifiedTag[b] = (byte) (modifiedTag[b] ^ (1 << bit));
try {
mac.verifyMac(modifiedTag, t.message);
fail("Invalid MAC, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
}
}
}
}
use of com.google.crypto.tink.Mac in project tink by google.
the class AesCmacTest method testMacTestVectors.
@Test
public void testMacTestVectors() throws Exception {
for (MacTestVector t : CMAC_TEST_VECTORS) {
Mac mac = new AesCmac(t.key, t.tag.length);
assertArrayEquals(t.tag, mac.computeMac(t.message));
try {
mac.verifyMac(t.tag, t.message);
} catch (GeneralSecurityException e) {
fail("Valid MAC, should not throw exception");
}
}
}
use of com.google.crypto.tink.Mac in project tink by google.
the class MacJceTest method testBitFlipTag.
@Test
public void testBitFlipTag() throws Exception {
for (MacTestVector t : HMAC_TEST_VECTORS) {
Mac mac = new MacJce(t.algName, new SecretKeySpec(t.key, "HMAC"), t.tag.length);
for (int b = 0; b < t.tag.length; b++) {
for (int bit = 0; bit < 8; bit++) {
byte[] modifiedTag = Arrays.copyOf(t.tag, t.tag.length);
modifiedTag[b] = (byte) (modifiedTag[b] ^ (1 << bit));
try {
mac.verifyMac(modifiedTag, t.message);
fail("Invalid MAC, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
}
}
}
// Test with random keys.
for (MacTestVector t : HMAC_TEST_VECTORS) {
Mac mac = new MacJce(t.algName, new SecretKeySpec(Random.randBytes(t.key.length), "HMAC"), t.tag.length);
for (int b = 0; b < t.tag.length; b++) {
for (int bit = 0; bit < 8; bit++) {
byte[] modifiedTag = Arrays.copyOf(t.tag, t.tag.length);
modifiedTag[b] = (byte) (modifiedTag[b] ^ (1 << bit));
try {
mac.verifyMac(modifiedTag, t.message);
fail("Invalid MAC, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
}
}
}
}
use of com.google.crypto.tink.Mac in project tink by google.
the class MacJceTest method testTagTruncation.
@Test
public void testTagTruncation() throws Exception {
for (MacTestVector t : HMAC_TEST_VECTORS) {
Mac mac = new MacJce(t.algName, new SecretKeySpec(t.key, "HMAC"), t.tag.length);
for (int j = 1; j < t.tag.length; j++) {
byte[] modifiedTag = Arrays.copyOf(t.tag, t.tag.length - j);
try {
mac.verifyMac(modifiedTag, t.message);
fail("Invalid MAC, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
}
}
// Test with random keys.
for (MacTestVector t : HMAC_TEST_VECTORS) {
Mac mac = new MacJce(t.algName, new SecretKeySpec(Random.randBytes(t.key.length), "HMAC"), t.tag.length);
for (int j = 1; j < t.tag.length; j++) {
byte[] modifiedTag = Arrays.copyOf(t.tag, t.tag.length - j);
try {
mac.verifyMac(modifiedTag, t.message);
fail("Invalid MAC, should have thrown exception");
} catch (GeneralSecurityException expected) {
// Expected
}
}
}
}
Aggregations