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);
}
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));
}
}
}
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));
}
}
}
}
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));
}
}
}
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);
}
Aggregations