From d21a726c681de3f5cc47d2fb9b57f2239022aad8 Mon Sep 17 00:00:00 2001 From: Jon Chambers Date: Sun, 3 Mar 2024 11:49:23 -0500 Subject: [PATCH] Add HMAC length tests --- .../noise/crypto/AbstractBlake2HmacTest.java | 27 +++++++++++++++++++ .../noise/crypto/HmacBlake2b512MacTest.java | 11 ++++++++ .../noise/crypto/HmacBlake2s256MacTest.java | 11 ++++++++ 3 files changed, 49 insertions(+) create mode 100644 src/test/java/com/eatthepath/noise/crypto/AbstractBlake2HmacTest.java create mode 100644 src/test/java/com/eatthepath/noise/crypto/HmacBlake2b512MacTest.java create mode 100644 src/test/java/com/eatthepath/noise/crypto/HmacBlake2s256MacTest.java diff --git a/src/test/java/com/eatthepath/noise/crypto/AbstractBlake2HmacTest.java b/src/test/java/com/eatthepath/noise/crypto/AbstractBlake2HmacTest.java new file mode 100644 index 0000000..ead0646 --- /dev/null +++ b/src/test/java/com/eatthepath/noise/crypto/AbstractBlake2HmacTest.java @@ -0,0 +1,27 @@ +package com.eatthepath.noise.crypto; + +import org.junit.jupiter.api.Test; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; + +import java.security.InvalidKeyException; +import java.security.Key; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +abstract class AbstractBlake2HmacTest { + + protected abstract Mac getHmac(); + + @Test + void getMacLength() throws InvalidKeyException { + final Key key = new SecretKeySpec(new byte[32], "RAW"); + + final Mac hmac = getHmac(); + hmac.init(key); + hmac.update(new byte[32]); + + assertEquals(hmac.getMacLength(), hmac.doFinal().length); + } +} diff --git a/src/test/java/com/eatthepath/noise/crypto/HmacBlake2b512MacTest.java b/src/test/java/com/eatthepath/noise/crypto/HmacBlake2b512MacTest.java new file mode 100644 index 0000000..5fbd128 --- /dev/null +++ b/src/test/java/com/eatthepath/noise/crypto/HmacBlake2b512MacTest.java @@ -0,0 +1,11 @@ +package com.eatthepath.noise.crypto; + +import javax.crypto.Mac; + +class HmacBlake2b512MacTest extends AbstractBlake2HmacTest { + + @Override + protected Mac getHmac() { + return new HmacBlake2b512Mac(); + } +} diff --git a/src/test/java/com/eatthepath/noise/crypto/HmacBlake2s256MacTest.java b/src/test/java/com/eatthepath/noise/crypto/HmacBlake2s256MacTest.java new file mode 100644 index 0000000..9160b17 --- /dev/null +++ b/src/test/java/com/eatthepath/noise/crypto/HmacBlake2s256MacTest.java @@ -0,0 +1,11 @@ +package com.eatthepath.noise.crypto; + +import javax.crypto.Mac; + +class HmacBlake2s256MacTest extends AbstractBlake2HmacTest { + + @Override + protected Mac getHmac() { + return new HmacBlake2s256Mac(); + } +}