Search in sources :

Example 26 with Mac

use of com.google.crypto.tink.Mac in project tink by google.

the class MacExample method main.

public static void main(String[] args) throws Exception {
    if (args.length != 4) {
        System.err.printf("Expected 4 parameters, got %d\n", args.length);
        System.err.println("Usage: java MacExample compute/verify key-file input-file mac-file");
        System.exit(1);
    }
    String mode = args[0];
    if (!mode.equals("compute") && !mode.equals("verify")) {
        System.err.println("Incorrect mode. Please select compute or verify.");
        System.exit(1);
    }
    File keyFile = new File(args[1]);
    byte[] msg = Files.readAllBytes(Paths.get(args[2]));
    File macFile = new File(args[3]);
    // Register all MAC key types with the Tink runtime.
    MacConfig.register();
    // Read the keyset into a KeysetHandle.
    KeysetHandle handle = null;
    try {
        handle = CleartextKeysetHandle.read(JsonKeysetReader.withFile(keyFile));
    } catch (GeneralSecurityException | IOException ex) {
        System.err.println("Cannot read keyset, got error: " + ex);
        System.exit(1);
    }
    // Get the primitive.
    Mac macPrimitive = null;
    try {
        macPrimitive = handle.getPrimitive(Mac.class);
    } catch (GeneralSecurityException ex) {
        System.err.println("Cannot create primitive, got error: " + ex);
        System.exit(1);
    }
    if (mode.equals("compute")) {
        byte[] mac = macPrimitive.computeMac(msg);
        try (FileOutputStream stream = new FileOutputStream(macFile)) {
            stream.write(Hex.encode(mac).getBytes(UTF_8));
        }
        System.exit(0);
    }
    List<String> lines = Files.readAllLines(macFile.toPath());
    if (lines.size() != 1) {
        System.err.printf("The MAC file should contain only one line, got %d", lines.size());
        System.exit(1);
    }
    byte[] mac = Hex.decode(lines.get(0).trim());
    try {
        macPrimitive.verifyMac(mac, msg);
    } catch (GeneralSecurityException ex) {
        System.err.println("MAC verification failed.");
        System.exit(1);
    }
    System.exit(0);
}
Also used : CleartextKeysetHandle(com.google.crypto.tink.CleartextKeysetHandle) KeysetHandle(com.google.crypto.tink.KeysetHandle) GeneralSecurityException(java.security.GeneralSecurityException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) Mac(com.google.crypto.tink.Mac)

Example 27 with Mac

use of com.google.crypto.tink.Mac in project tink by google.

the class PrfHmacJceTest method testBitFlipMessage.

@Test
public void testBitFlipMessage() throws Exception {
    Assume.assumeTrue(!TinkFips.useOnlyFips() || TinkFipsUtil.fipsModuleAvailable());
    for (MacTestVector t : HMAC_TEST_VECTORS) {
        Mac mac = new PrfMac(new PrfHmacJce(t.algName, new SecretKeySpec(t.key, "HMAC")), t.tag.length);
        for (int b = 0; b < t.message.length; b++) {
            for (int bit = 0; bit < 8; bit++) {
                byte[] modifiedMessage = Arrays.copyOf(t.message, t.message.length);
                modifiedMessage[b] = (byte) (modifiedMessage[b] ^ (1 << bit));
                assertThrows(GeneralSecurityException.class, () -> mac.verifyMac(t.tag, modifiedMessage));
            }
        }
    }
    // Test with random keys.
    for (MacTestVector t : HMAC_TEST_VECTORS) {
        Mac mac = new PrfMac(new PrfHmacJce(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);
            assertThrows(GeneralSecurityException.class, () -> mac.verifyMac(modifiedTag, t.message));
        }
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Mac(com.google.crypto.tink.Mac) Test(org.junit.Test)

Example 28 with Mac

use of com.google.crypto.tink.Mac in project tink by google.

the class PrfHmacJceTest method testBitFlipTag.

@Test
public void testBitFlipTag() throws Exception {
    Assume.assumeTrue(!TinkFips.useOnlyFips() || TinkFipsUtil.fipsModuleAvailable());
    for (MacTestVector t : HMAC_TEST_VECTORS) {
        Mac mac = new PrfMac(new PrfHmacJce(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));
                assertThrows(GeneralSecurityException.class, () -> mac.verifyMac(modifiedTag, t.message));
            }
        }
    }
    // Test with random keys.
    for (MacTestVector t : HMAC_TEST_VECTORS) {
        Mac mac = new PrfMac(new PrfHmacJce(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));
                assertThrows(GeneralSecurityException.class, () -> mac.verifyMac(modifiedTag, t.message));
            }
        }
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Mac(com.google.crypto.tink.Mac) Test(org.junit.Test)

Example 29 with Mac

use of com.google.crypto.tink.Mac in project tink by google.

the class PrfHmacJceTest method testTagTruncation.

@Test
public void testTagTruncation() throws Exception {
    Assume.assumeTrue(!TinkFips.useOnlyFips() || TinkFipsUtil.fipsModuleAvailable());
    for (MacTestVector t : HMAC_TEST_VECTORS) {
        Mac mac = new PrfMac(new PrfHmacJce(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);
            assertThrows(GeneralSecurityException.class, () -> mac.verifyMac(modifiedTag, t.message));
        }
    }
    // Test with random keys.
    for (MacTestVector t : HMAC_TEST_VECTORS) {
        Mac mac = new PrfMac(new PrfHmacJce(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);
            assertThrows(GeneralSecurityException.class, () -> mac.verifyMac(modifiedTag, t.message));
        }
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Mac(com.google.crypto.tink.Mac) Test(org.junit.Test)

Example 30 with Mac

use of com.google.crypto.tink.Mac in project tink by google.

the class AesCmacKeyManagerTest method getPrimitive_works.

@Test
public void getPrimitive_works() throws Exception {
    AesCmacKeyManager manager = new AesCmacKeyManager();
    AesCmacKey validKey = manager.keyFactory().createKey(makeAesCmacKeyFormat(32, 16));
    Mac managerMac = manager.getPrimitive(validKey, Mac.class);
    Mac directMac = new PrfMac(new PrfAesCmac(validKey.getKeyValue().toByteArray()), validKey.getParams().getTagSize());
    byte[] message = Random.randBytes(50);
    managerMac.verifyMac(directMac.computeMac(message), message);
}
Also used : PrfAesCmac(com.google.crypto.tink.subtle.PrfAesCmac) PrfMac(com.google.crypto.tink.subtle.PrfMac) AesCmacKey(com.google.crypto.tink.proto.AesCmacKey) Mac(com.google.crypto.tink.Mac) PrfMac(com.google.crypto.tink.subtle.PrfMac) 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