Search in sources :

Example 1 with Mac

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
            }
        }
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) Mac(com.google.crypto.tink.Mac) Test(org.junit.Test)

Example 2 with Mac

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
                }
            }
        }
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) Mac(com.google.crypto.tink.Mac) Test(org.junit.Test)

Example 3 with Mac

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");
        }
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) Mac(com.google.crypto.tink.Mac) Test(org.junit.Test)

Example 4 with Mac

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
                }
            }
        }
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) Mac(com.google.crypto.tink.Mac) Test(org.junit.Test)

Example 5 with Mac

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
            }
        }
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) Mac(com.google.crypto.tink.Mac) Test(org.junit.Test)

Aggregations

Mac (com.google.crypto.tink.Mac)34 Test (org.junit.Test)29 GeneralSecurityException (java.security.GeneralSecurityException)19 SecretKeySpec (javax.crypto.spec.SecretKeySpec)15 KeysetHandle (com.google.crypto.tink.KeysetHandle)7 Key (com.google.crypto.tink.proto.Keyset.Key)6 PrfMac (com.google.crypto.tink.subtle.PrfMac)4 CleartextKeysetHandle (com.google.crypto.tink.CleartextKeysetHandle)3 HmacKey (com.google.crypto.tink.proto.HmacKey)3 PrfHmacJce (com.google.crypto.tink.subtle.PrfHmacJce)3 IOException (java.io.IOException)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 Aead (com.google.crypto.tink.Aead)1 KeyManager (com.google.crypto.tink.KeyManager)1 Prf (com.google.crypto.tink.prf.Prf)1 AesCmacKey (com.google.crypto.tink.proto.AesCmacKey)1 KeyTypeEntry (com.google.crypto.tink.proto.KeyTypeEntry)1 RegistryConfig (com.google.crypto.tink.proto.RegistryConfig)1 ComputeMacResponse (com.google.crypto.tink.proto.testing.ComputeMacResponse)1 VerifyMacResponse (com.google.crypto.tink.proto.testing.VerifyMacResponse)1