diff --git "a/doc/(KS X 3246)128 \353\271\204\355\212\270 \353\270\224\353\241\235 \354\225\224\355\230\270 LEA.docx" "b/doc/(KS X 3246)128 \353\271\204\355\212\270 \353\270\224\353\241\235 \354\225\224\355\230\270 LEA.docx" new file mode 100644 index 0000000..7a00a75 Binary files /dev/null and "b/doc/(KS X 3246)128 \353\271\204\355\212\270 \353\270\224\353\241\235 \354\225\224\355\230\270 LEA.docx" differ diff --git a/src/test/java/kr/re/nsr/crypto/BlockCipherModeTest.java b/src/test/java/kr/re/nsr/crypto/BlockCipherModeTest.java index 869af69..823a802 100644 --- a/src/test/java/kr/re/nsr/crypto/BlockCipherModeTest.java +++ b/src/test/java/kr/re/nsr/crypto/BlockCipherModeTest.java @@ -1,10 +1,20 @@ package kr.re.nsr.crypto; +import com.github.jinahya.kisa.lea.LeaConstants; +import com.github.jinahya.kisa.lea.LeaTestUtils; +import kr.re.nsr.crypto.util.Hex; import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Assumptions; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import java.awt.*; +import java.security.SecureRandom; import java.util.Objects; +import static org.assertj.core.api.Assertions.assertThat; + @Slf4j abstract class BlockCipherModeTest { @@ -28,5 +38,35 @@ void getAlgorithmName__() { final var algorithmName = modeInstance().getAlgorithmName(); } + @MethodSource({"kr.re.nsr.crypto.TestVectors#testVectorStream"}) + @ParameterizedTest + void testVector__(final byte[] key, final byte[] plain, final byte[] encrypted) throws Exception { + log.debug(" key: {}", Hex.toHexString(key)); + log.debug(" plain: {}", Hex.toHexString(plain)); + log.debug("encrypted: {}", Hex.toHexString(encrypted)); +// final var random = SecureRandom.getInstanceStrong(); + final T instance = modeInstance(); +// final byte[] iv = new byte[LeaConstants.BLOCK_BYTES]; + try { + instance.init(BlockCipher.Mode.ENCRYPT, key); + } catch (final Exception e) { + Assumptions.assumeTrue(false); +// instance.init(BlockCipher.Mode.ENCRYPT, key, iv); + } + final byte[] encrypted_ = instance.doFinal(plain); + assertThat(encrypted_) + .isNotNull() + .isEqualTo(encrypted); + try { + instance.init(BlockCipher.Mode.DECRYPT, key); + } catch (final Exception e) { + Assumptions.assumeTrue(false); +// instance.init(BlockCipher.Mode.DECRYPT, key, iv); + } + assertThat(instance.doFinal(encrypted_)) + .isNotNull() + .isEqualTo(plain); + } + protected final Class modeClass; } \ No newline at end of file diff --git a/src/test/java/kr/re/nsr/crypto/TestVectors.java b/src/test/java/kr/re/nsr/crypto/TestVectors.java new file mode 100644 index 0000000..5b9f1c4 --- /dev/null +++ b/src/test/java/kr/re/nsr/crypto/TestVectors.java @@ -0,0 +1,56 @@ +package kr.re.nsr.crypto; + +import org.junit.jupiter.params.provider.Arguments; + +import java.io.ByteArrayOutputStream; +import java.io.ObjectInput; +import java.util.Arrays; +import java.util.stream.Stream; + +public final class TestVectors { + + private static byte[] array(final String string) { + final ByteArrayOutputStream stream = new ByteArrayOutputStream(); + Arrays.stream(string.split("\\s")) + .map(String::strip) + .mapToInt(v -> Integer.parseInt(v, 16)) + .forEach(stream::write); + return stream.toByteArray(); + } + + public static Stream testVectorStream() { + return Stream.of( + Arguments.of( + array("0f 1e 2d 3c 4b 5a 69 78 " + + "87 96 a5 b4 c3 d2 e1 f0"), + array("10 11 12 13 14 15 16 17 " + + "18 19 1a 1b 1c 1d 1e 1f"), + array("9f c8 4e 35 28 c6 c6 18 " + + "55 32 c7 a7 04 64 8b fd") + ), + Arguments.of( + array("0f 1e 2d 3c 4b 5a 69 78 " + + "87 96 a5 b4 c3 d2 e1 f0 " + + "f0 e1 d2 c3 b4 a5 96 87"), + array("20 21 22 23 24 25 26 27 " + + "28 29 2a 2b 2c 2d 2e 2f"), + array("6f b9 5e 32 5a ad 1b 87 " + + "8c dc f5 35 76 74 c6 f2") + ), + Arguments.of( + array("0f 1e 2d 3c 4b 5a 69 78 " + + "87 96 a5 b4 c3 d2 e1 f0 " + + "f0 e1 d2 c3 b4 a5 96 87 " + + "78 69 5a 4b 3c 2d 1e 0f"), + array("30 31 32 33 34 35 36 37 " + + "38 39 3a 3b 3c 3d 3e 3f"), + array("d6 51 af f6 47 b1 89 c1 " + + "3a 89 00 ca 27 f9 e1 97") + ) + ); + } + + private TestVectors() { + throw new AssertionError("instantiation is not allowed"); + } +}