Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
onacit committed Dec 16, 2023
1 parent 60d6759 commit 3196952
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 180 deletions.
31 changes: 18 additions & 13 deletions src/main/java/com/github/jinahya/kisa/lea/LeaConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,37 @@
import java.util.Arrays;
import java.util.List;

public final class LEAConstants {
/**
* Constants related to the LEA cipher.
*
* @author Jin Kwon <onacit_at_gmail.com>
*/
public final class LeaConstants {

public static final String ALGORITHM = "AES";
static final String ALGORITHM = "AES";

public static final String MODE_ECB = "ECB";
static final String MODE_ECB = "ECB";

public static final String MODE_CBC = "CBC";
static final String MODE_CBC = "CBC";

public static final String MODE_CTR = "CTR";
static final String MODE_CTR = "CTR";

public static final String MODE_CFB = "CFB";
static final String MODE_CFB = "CFB";

public static final String MODE_OFB = "OFB";
static final String MODE_OFB = "OFB";

public static final String MODE_CCM = "CCM";
static final String MODE_CCM = "CCM";

public static final String MODE_GCM = "GCM";
static final String MODE_GCM = "GCM";

public static final String PADDING_PKCS5_PADDING = "PKCS5Padding";
static final String PADDING_PKCS5_PADDING = "PKCS5Padding";

public static final String PADDING_NO_PADDING = "NoPadding";
static final String PADDING_NO_PADDING = "NoPadding";

/**
* The block size, in bits, of LEA. The value is {@value}.
*/
static final int BLOCK_SIZE = 128;
public static final int BLOCK_SIZE = 128;

/**
* The block size, in bytes, of LEA. The value is {@value}.
Expand All @@ -40,7 +45,7 @@ public final class LEAConstants {
*/
static final List<Integer> KEY_SIZES = Arrays.asList(128, 192, 256);

private LEAConstants() {
private LeaConstants() {
throw new AssertionError("instantiation is not allowed");
}
}
156 changes: 14 additions & 142 deletions src/main/java/com/github/jinahya/kisa/lea/ModeAEFactory.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package com.github.jinahya.kisa.lea;

import kr.re.nsr.crypto.BlockCipher;
import kr.re.nsr.crypto.BlockCipherMode;
import kr.re.nsr.crypto.BlockCipherModeBlock;
import kr.re.nsr.crypto.BlockCipherModeStream;
import kr.re.nsr.crypto.padding.PKCS5Padding;
import kr.re.nsr.crypto.BlockCipherModeAE;
import kr.re.nsr.crypto.symm.LEA;

import java.lang.reflect.Constructor;
import java.security.NoSuchAlgorithmException;

class ModeFactory {
class ModeAEFactory {

private static <T extends BlockCipherMode> T newInstance(final Class<T> type, final String name)
private static <T extends BlockCipherModeAE> T newInstance(final Class<T> type, final String name)
throws NoSuchAlgorithmException {
if (type == null) {
throw new NullPointerException("type is null");
Expand Down Expand Up @@ -42,152 +39,27 @@ private static <T extends BlockCipherMode> T newInstance(final Class<T> type, fi
throw new NoSuchAlgorithmException("unknown name: " + name);
}

private static <T extends BlockCipherMode> T init(final T cipher, final BlockCipher.Mode mode, final byte[] key,
final byte[] iv) {
// private static <T extends BlockCipherModeAE> T newInstance(final String name) throws NoSuchAlgorithmException {
// return newInstance(BlockCipherModeAE.class, name);
// }

private static <T extends BlockCipherModeAE> T init(final T cipher, final BlockCipher.Mode mode, final byte[] key,
final int tLen, final byte[] src, final byte[] aad) {
if (key == null) {
throw new NullPointerException("key is null");
}
if (key.length != 16 && key.length != 24 && key.length != 32) {
throw new IllegalArgumentException("invalid key");
}
if (iv != null && iv.length != LeaConstants.BLOCK_BYTES) {
throw new IllegalArgumentException("invalid iv");
}
if (iv == null) {
cipher.init(mode, key);
} else {
cipher.init(mode, key, iv);
if (src != null && src.length != LeaConstants.BLOCK_BYTES) {
throw new IllegalArgumentException("invalid src");
}
cipher.init(mode, key, src, tLen);
cipher.updateAAD(aad);
return cipher;
}

public static final class Block
extends ModeFactory {

private static BlockCipherModeBlock newInstance(final String name) throws NoSuchAlgorithmException {
return ModeFactory.newInstance(BlockCipherModeBlock.class, name);
}

private static BlockCipherModeBlock newInstance(final String name, final BlockCipher.Mode mode, final byte[] key,
final byte[] iv)
throws NoSuchAlgorithmException {
return init(newInstance(name), mode, key, iv);
}

private static BlockCipherMode ECB(final BlockCipher.Mode mode, final byte[] key, final boolean padding) {
final BlockCipherMode cipher;
final String name = "ECB";
try {
cipher = newInstance(name, mode, key, null);
} catch (final NoSuchAlgorithmException nsae) {
throw new RuntimeException(nsae);
}
if (padding) {
cipher.setPadding(new PKCS5Padding(LeaConstants.BLOCK_BYTES));
}
return cipher;
}

public static BlockCipherMode ECB_PKCS5Padding(final BlockCipher.Mode mode, final byte[] key) {
return ECB(mode, key, true);
}

public static BlockCipherMode ECB_NoPadding(final BlockCipher.Mode mode, final byte[] key) {
return ECB(mode, key, true);
}

private static BlockCipherMode CBC(final BlockCipher.Mode mode, final byte[] key, final byte[] iv,
final boolean padding) {
final BlockCipherMode cipher;
final String name = "CBC";
try {
cipher = newInstance(name, mode, key, iv);
} catch (final NoSuchAlgorithmException nsae) {
throw new RuntimeException(nsae);
}
if (padding) {
cipher.setPadding(new PKCS5Padding(LeaConstants.BLOCK_BYTES));
}
return cipher;
}

public static BlockCipherMode CBC_PKCK5Padding(final BlockCipher.Mode mode, final byte[] key, final byte[] iv) {
return CBC(mode, key, iv, true);
}

public static BlockCipherMode CBC_NoPadding(final BlockCipher.Mode mode, final byte[] key, final byte[] iv) {
return CBC(mode, key, iv, false);
}

private Block() {
throw new AssertionError("instantiation is not allowed");
}
}

public static final class Stream
extends ModeFactory {

private static BlockCipherModeStream newInstance(final String name) throws NoSuchAlgorithmException {
return ModeFactory.newInstance(BlockCipherModeStream.class, name);
}

private static BlockCipherModeStream newInstance(final String name, final BlockCipher.Mode mode, final byte[] key,
final byte[] iv)
throws NoSuchAlgorithmException {
return init(newInstance(name), mode, key, iv);
}

private static BlockCipherMode CTR(final BlockCipher.Mode mode, final byte[] key, final byte[] iv) {
final BlockCipherMode cipher;
final String name = "CTR";
try {
cipher = newInstance(name, mode, key, iv);
} catch (final NoSuchAlgorithmException nsae) {
throw new RuntimeException(nsae);
}
return cipher;
}

public static BlockCipherMode CTR_NoPadding(final BlockCipher.Mode mode, final byte[] key, final byte[] iv) {
return CTR(mode, key, iv);
}

private static BlockCipherMode CFB(final BlockCipher.Mode mode, final byte[] key, final byte[] iv) {
final BlockCipherMode cipher;
final String name = "CFB";
try {
cipher = newInstance(name, mode, key, iv);
} catch (final NoSuchAlgorithmException nsae) {
throw new RuntimeException(nsae);
}
return cipher;
}

public static BlockCipherMode CFB_NoPadding(final BlockCipher.Mode mode, final byte[] key, final byte[] iv) {
return CFB(mode, key, iv);
}

private static BlockCipherMode OFB(final BlockCipher.Mode mode, final byte[] key, final byte[] iv) {
final BlockCipherMode cipher;
final String name = "OFB";
try {
cipher = newInstance(name, mode, key, iv);
} catch (final NoSuchAlgorithmException nsae) {
throw new RuntimeException(nsae);
}
return cipher;
}

public static BlockCipherMode OFB_NoPadding(final BlockCipher.Mode mode, final byte[] key, final byte[] iv) {
return OFB(mode, key, iv);
}

private Stream() {
throw new AssertionError("instantiation is not allowed");
}
}

private ModeFactory() {
private ModeAEFactory() {
throw new AssertionError("instantiation is not allowed");
}
}
12 changes: 6 additions & 6 deletions src/main/java/com/github/jinahya/kisa/lea/ModeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.lang.reflect.Constructor;
import java.security.NoSuchAlgorithmException;

class LeaFactory {
class ModeFactory {

private static <T extends BlockCipherMode> T newInstance(final Class<T> type, final String name)
throws NoSuchAlgorithmException {
Expand Down Expand Up @@ -62,10 +62,10 @@ private static <T extends BlockCipherMode> T init(final T cipher, final BlockCip
}

public static final class Block
extends LeaFactory {
extends ModeFactory {

private static BlockCipherModeBlock newInstance(final String name) throws NoSuchAlgorithmException {
return LeaFactory.newInstance(BlockCipherModeBlock.class, name);
return ModeFactory.newInstance(BlockCipherModeBlock.class, name);
}

private static BlockCipherModeBlock newInstance(final String name, final BlockCipher.Mode mode, final byte[] key,
Expand Down Expand Up @@ -125,10 +125,10 @@ private Block() {
}

public static final class Stream
extends LeaFactory {
extends ModeFactory {

private static BlockCipherModeStream newInstance(final String name) throws NoSuchAlgorithmException {
return LeaFactory.newInstance(BlockCipherModeStream.class, name);
return ModeFactory.newInstance(BlockCipherModeStream.class, name);
}

private static BlockCipherModeStream newInstance(final String name, final BlockCipher.Mode mode, final byte[] key,
Expand Down Expand Up @@ -187,7 +187,7 @@ private Stream() {
}
}

private LeaFactory() {
private ModeFactory() {
throw new AssertionError("instantiation is not allowed");
}
}
28 changes: 22 additions & 6 deletions src/test/java/com/github/jinahya/kisa/lea/LeaConstantsTest.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
package com.github.jinahya.kisa.lea;

import kr.re.nsr.crypto.engine.LeaEngine;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;

import static org.assertj.core.api.Assertions.assertThat;

class LEAConstantsTest {
class LeaConstantsTest {

@Test
void BLOCK_SIZE__() {
assertThat(LEAConstants.BLOCK_SIZE) // NOSONAR
assertThat(LeaConstants.BLOCK_SIZE) // NOSONAR
.satisfies(v -> {
assertThat(v % Byte.SIZE).isZero();
});
}

@DisplayName("BLOCK_BYTES == LeaEngine.BLOCKSIZE")
@Test
void BLOCK_BYTES_EqualToLeaEngineBLOCKSIZE_() throws ReflectiveOperationException {
final Field field = LeaEngine.class.getDeclaredField("BLOCKSIZE");
field.setAccessible(true);
final int actual = LeaConstants.BLOCK_BYTES;
final int expected = (int) field.get(null);
assertThat(actual)
.isEqualTo(expected);
}

@DisplayName("BLOCK_BYTES == BLOCK_SIZE / 8")
@Test
void BLOCK_BYTES__() {
assertThat(LEAConstants.BLOCK_BYTES) // NOSONAR
.isEqualTo(LEAConstants.BLOCK_SIZE / Byte.SIZE);
void BLOCK_BYTES_BLOCK_SIZEDividedBy8_() {
assertThat(LeaConstants.BLOCK_BYTES) // NOSONAR
.isEqualTo(LeaConstants.BLOCK_SIZE / Byte.SIZE);
}

@Test
void KEY_SIZES__() {
assertThat(LEAConstants.KEY_SIZES)
assertThat(LeaConstants.KEY_SIZES)
.isNotEmpty()
.doesNotContainNull()
.allSatisfy(v -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public final class LeaTestUtils {

public static byte[] iv(final Random random) {
Objects.requireNonNull(random, "random is null");
final var iv = new byte[LEAConstants.BLOCK_BYTES];
final var iv = new byte[LeaConstants.BLOCK_BYTES];
random.nextBytes(iv);
return iv;
}
Expand Down
Loading

0 comments on commit 3196952

Please sign in to comment.