SEED 암호화 모듈 추가
- SEEDCryptoModuleExtension : BouncyCastle SEED(128bit 키), CBC/ECB 지원, GCM 미지원 - 키 16바이트 검증, 프로바이더 BC 고정 - CryptoModuleManager : AES/ARIA/SEED 분기, 미지원 알고리즘은 예외 - 단위테스트 : RFC 4269 표준 벡터 포함 SEED 10건, Manager 2건 추가
This commit is contained in:
@@ -216,9 +216,17 @@ public class CryptoModuleManager implements Lifecycle {
|
||||
|
||||
private CryptoModuleExtension buildExtension(CryptoModuleConfigVO vo, byte[] iv,
|
||||
byte[] encKey, byte[] decKey) throws Exception {
|
||||
CryptoModuleExtension ext = "ARIA".equalsIgnoreCase(vo.getAlgType())
|
||||
? new ARIACryptoModuleExtension()
|
||||
: new AESCryptoModuleExtension();
|
||||
String alg = vo.getAlgType();
|
||||
CryptoModuleExtension ext;
|
||||
if (alg == null || alg.trim().isEmpty() || "AES".equalsIgnoreCase(alg.trim())) {
|
||||
ext = new AESCryptoModuleExtension();
|
||||
} else if ("ARIA".equalsIgnoreCase(alg.trim())) {
|
||||
ext = new ARIACryptoModuleExtension();
|
||||
} else if ("SEED".equalsIgnoreCase(alg.trim())) {
|
||||
ext = new SEEDCryptoModuleExtension();
|
||||
} else {
|
||||
throw new IllegalArgumentException("지원하지 않는 암호 알고리즘: " + alg + " (" + vo.getCryptoName() + ")");
|
||||
}
|
||||
ext.init(vo.getAlgType(), vo.getCipherMode(), vo.getPadding(), iv, encKey, decKey);
|
||||
return ext;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.eactive.eai.common.security;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.security.Key;
|
||||
import java.security.SecureRandom;
|
||||
import java.security.Security;
|
||||
import java.security.spec.AlgorithmParameterSpec;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.IvParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
|
||||
/**
|
||||
* KISA SEED(128bit 블록/128bit 키) 암복호화 구현체. BouncyCastle 프로바이더를 사용한다.
|
||||
* GCM 모드는 지원하지 않는다.
|
||||
*/
|
||||
public class SEEDCryptoModuleExtension implements CryptoModuleExtension {
|
||||
|
||||
private static final int KEY_LENGTH = 16;
|
||||
private static final String DEFAULT_PROVIDER = "BC";
|
||||
|
||||
private Cipher encryptEngine;
|
||||
private Cipher decryptEngine;
|
||||
|
||||
@Override
|
||||
public void init(String alg, String mode, String pad, byte[] encKey, byte[] decKey) throws Exception {
|
||||
init(alg, mode, pad, null, encKey, decKey, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(String alg, String mode, String pad, byte[] iv, byte[] encKey, byte[] decKey) throws Exception {
|
||||
init(alg, mode, pad, iv, encKey, decKey, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(String alg, String mode, String pad, byte[] encKey, byte[] decKey, String provider) throws Exception {
|
||||
init(alg, mode, pad, null, encKey, decKey, provider);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(String alg, String mode, String pad, byte[] iv, byte[] encKey, byte[] decKey, String provider) throws Exception {
|
||||
String m = mode != null ? mode : "";
|
||||
String p = pad != null ? pad : "";
|
||||
if ("GCM".equalsIgnoreCase(m)) {
|
||||
throw new IllegalArgumentException("SEED 암호화모듈은 GCM 모드를 지원하지 않습니다.");
|
||||
}
|
||||
checkKeyLength("encKey", encKey);
|
||||
checkKeyLength("decKey", decKey);
|
||||
|
||||
if (Security.getProvider("BC") == null) {
|
||||
Security.addProvider(new BouncyCastleProvider());
|
||||
}
|
||||
|
||||
String transformation = String.format("SEED/%s/%s", m, p);
|
||||
String prov = (provider != null && !provider.isEmpty()) ? provider : DEFAULT_PROVIDER;
|
||||
Key encKeySpec = new SecretKeySpec(encKey, "SEED");
|
||||
Key decKeySpec = new SecretKeySpec(decKey, "SEED");
|
||||
AlgorithmParameterSpec param = iv != null ? new IvParameterSpec(iv) : null;
|
||||
|
||||
this.encryptEngine = Cipher.getInstance(transformation, prov);
|
||||
this.decryptEngine = Cipher.getInstance(transformation, prov);
|
||||
this.encryptEngine.init(Cipher.ENCRYPT_MODE, encKeySpec, param, (SecureRandom) null);
|
||||
this.decryptEngine.init(Cipher.DECRYPT_MODE, decKeySpec, param, (SecureRandom) null);
|
||||
}
|
||||
|
||||
private static void checkKeyLength(String name, byte[] key) {
|
||||
if (key == null || key.length != KEY_LENGTH) {
|
||||
throw new IllegalArgumentException(String.format("SEED %s 는 %d바이트여야 합니다. (입력=%s)",
|
||||
name, KEY_LENGTH, key == null ? "null" : key.length + "바이트"));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int encrypt(byte[] src, int sindex, int slength, byte[] dst, int dindex) throws Exception {
|
||||
return this.encryptEngine.doFinal(src, sindex, slength, dst, dindex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] encrypt(byte[] src, int sindex, int slength) throws Exception {
|
||||
byte[] dst = new byte[this.encryptEngine.getOutputSize(slength)];
|
||||
int actual = encrypt(src, sindex, slength, dst, 0);
|
||||
return ByteBuffer.allocate(actual).put(dst, 0, actual).array();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int decrypt(byte[] src, int sindex, int slength, byte[] dst, int dindex) throws Exception {
|
||||
return this.decryptEngine.doFinal(src, sindex, slength, dst, dindex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] decrypt(byte[] src, int sindex, int slength) throws Exception {
|
||||
return this.decryptEngine.doFinal(src, sindex, slength);
|
||||
}
|
||||
}
|
||||
@@ -135,6 +135,34 @@ class CryptoModuleManagerTest {
|
||||
assertArrayEquals(plain, decrypted);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("2-2a. STATIC SEED/CBC — createExtension 반환 및 암복호화 동작")
|
||||
void testCreateExtension_staticSeedCbc_roundTrip() throws Exception {
|
||||
CryptoModuleConfig config = buildStaticConfig("SEED_CBC", "SEED", "CBC", "PKCS5Padding");
|
||||
when(mockLoader.findAll()).thenReturn(Arrays.asList(config));
|
||||
reload();
|
||||
|
||||
byte[] plain = "SEED 테스트".getBytes(StandardCharsets.UTF_8);
|
||||
CryptoModuleExtension ext = manager.createExtension("SEED_CBC");
|
||||
assertTrue(ext instanceof SEEDCryptoModuleExtension, "algType=SEED 는 SEED 구현체여야 한다");
|
||||
byte[] encrypted = ext.encrypt(plain, 0, plain.length);
|
||||
|
||||
CryptoModuleExtension extDec = manager.createExtension("SEED_CBC");
|
||||
assertArrayEquals(plain, extDec.decrypt(encrypted, 0, encrypted.length));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("2-2b. 지원하지 않는 algType — IllegalArgumentException")
|
||||
void testCreateExtension_unsupportedAlg_throwsException() throws Exception {
|
||||
CryptoModuleConfig config = buildStaticConfig("DES_CBC", "DES", "CBC", "PKCS5Padding");
|
||||
when(mockLoader.findAll()).thenReturn(Arrays.asList(config));
|
||||
reload();
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> manager.createExtension("DES_CBC"),
|
||||
"목록에 없는 알고리즘은 AES로 대체하지 않고 예외여야 한다");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("2-3. 미등록 cryptoName — IllegalArgumentException")
|
||||
void testCreateExtension_unknownName_throwsException() {
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
package com.eactive.eai.common.security;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
/**
|
||||
* SEEDCryptoModuleExtension 단위 테스트
|
||||
* BouncyCastle SEED 암복호화 로직을 검증한다.
|
||||
*/
|
||||
@TestMethodOrder(MethodOrderer.DisplayName.class)
|
||||
class SEEDCryptoModuleExtensionTest {
|
||||
|
||||
private static final byte[] KEY_128 = "0123456789abcdef".getBytes(StandardCharsets.UTF_8);
|
||||
private static final byte[] IV_16 = "abcdef0123456789".getBytes(StandardCharsets.UTF_8);
|
||||
private static final byte[] PLAIN = "SEED 암호화 테스트 평문입니다.".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
private static byte[] hex(String s) {
|
||||
return DatatypeConverter.parseHexBinary(s);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 1. 표준 테스트 벡터 (RFC 4269)
|
||||
// =========================================================================
|
||||
|
||||
@Test
|
||||
@DisplayName("1-1. RFC 4269 벡터 1 — 키 0x00*16, 평문 00..0F")
|
||||
void testRfc4269_vector1() throws Exception {
|
||||
byte[] key = hex("00000000000000000000000000000000");
|
||||
SEEDCryptoModuleExtension ext = new SEEDCryptoModuleExtension();
|
||||
ext.init("SEED", "ECB", "NoPadding", key, key);
|
||||
|
||||
byte[] encrypted = ext.encrypt(hex("000102030405060708090A0B0C0D0E0F"));
|
||||
|
||||
assertArrayEquals(hex("5EBAC6E0054E166819AFF1CC6D346CDB"), encrypted);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("1-2. RFC 4269 벡터 2 — 키 00..0F, 평문 0x00*16")
|
||||
void testRfc4269_vector2() throws Exception {
|
||||
byte[] key = hex("000102030405060708090A0B0C0D0E0F");
|
||||
SEEDCryptoModuleExtension ext = new SEEDCryptoModuleExtension();
|
||||
ext.init("SEED", "ECB", "NoPadding", key, key);
|
||||
|
||||
byte[] encrypted = ext.encrypt(hex("00000000000000000000000000000000"));
|
||||
|
||||
assertArrayEquals(hex("C11F22F20140505084483597E4370F43"), encrypted);
|
||||
assertArrayEquals(hex("00000000000000000000000000000000"), ext.decrypt(encrypted));
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 2. CBC 모드
|
||||
// =========================================================================
|
||||
|
||||
@Test
|
||||
@DisplayName("2-1. SEED/CBC/PKCS5Padding — 암복호화 라운드트립")
|
||||
void testCbc_roundTrip() throws Exception {
|
||||
SEEDCryptoModuleExtension ext = new SEEDCryptoModuleExtension();
|
||||
ext.init("SEED", "CBC", "PKCS5Padding", IV_16, KEY_128, KEY_128);
|
||||
|
||||
byte[] encrypted = ext.encrypt(PLAIN, 0, PLAIN.length);
|
||||
byte[] decrypted = ext.decrypt(encrypted, 0, encrypted.length);
|
||||
|
||||
assertFalse(Arrays.equals(PLAIN, encrypted), "암호문은 평문과 달라야 한다");
|
||||
assertArrayEquals(PLAIN, decrypted, "복호화 결과가 원문과 일치해야 한다");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("2-2. SEED/CBC — 암호화 결과는 블록 크기(16바이트)의 배수")
|
||||
void testCbc_ciphertextIsMultipleOfBlockSize() throws Exception {
|
||||
SEEDCryptoModuleExtension ext = new SEEDCryptoModuleExtension();
|
||||
ext.init("SEED", "CBC", "PKCS5Padding", IV_16, KEY_128, KEY_128);
|
||||
|
||||
byte[] encrypted = ext.encrypt(PLAIN, 0, PLAIN.length);
|
||||
|
||||
assertEquals(0, encrypted.length % 16, "SEED CBC 암호문 길이는 16 바이트 배수여야 한다");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("2-3. SEED/CBC — 다른 IV로 암호화 시 다른 암호문 생성")
|
||||
void testCbc_differentIv_differentCiphertext() throws Exception {
|
||||
byte[] iv2 = "9876543210fedcba".getBytes(StandardCharsets.UTF_8);
|
||||
|
||||
SEEDCryptoModuleExtension ext1 = new SEEDCryptoModuleExtension();
|
||||
ext1.init("SEED", "CBC", "PKCS5Padding", IV_16, KEY_128, KEY_128);
|
||||
SEEDCryptoModuleExtension ext2 = new SEEDCryptoModuleExtension();
|
||||
ext2.init("SEED", "CBC", "PKCS5Padding", iv2, KEY_128, KEY_128);
|
||||
|
||||
assertFalse(Arrays.equals(ext1.encrypt(PLAIN), ext2.encrypt(PLAIN)), "IV가 다르면 암호문도 달라야 한다");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("2-4. SEED/CBC — 잘못된 키로 복호화 시 예외 발생")
|
||||
void testCbc_wrongKeyThrowsException() throws Exception {
|
||||
SEEDCryptoModuleExtension encExt = new SEEDCryptoModuleExtension();
|
||||
encExt.init("SEED", "CBC", "PKCS5Padding", IV_16, KEY_128, KEY_128);
|
||||
byte[] encrypted = encExt.encrypt(PLAIN, 0, PLAIN.length);
|
||||
|
||||
byte[] wrongKey = "wrongkey12345678".getBytes(StandardCharsets.UTF_8);
|
||||
SEEDCryptoModuleExtension decExt = new SEEDCryptoModuleExtension();
|
||||
decExt.init("SEED", "CBC", "PKCS5Padding", IV_16, wrongKey, wrongKey);
|
||||
|
||||
assertThrows(Exception.class, () -> decExt.decrypt(encrypted, 0, encrypted.length),
|
||||
"잘못된 키로 복호화 시 예외가 발생해야 한다");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("2-5. encrypt(src, sindex, slength, dst, dindex) — 버퍼 직접 지정 라운드트립")
|
||||
void testEncryptDecryptWithBuffer() throws Exception {
|
||||
SEEDCryptoModuleExtension ext = new SEEDCryptoModuleExtension();
|
||||
ext.init("SEED", "CBC", "PKCS5Padding", IV_16, KEY_128, KEY_128);
|
||||
|
||||
byte[] dst = new byte[256];
|
||||
int encLen = ext.encrypt(PLAIN, 0, PLAIN.length, dst, 0);
|
||||
byte[] decDst = new byte[256];
|
||||
int decLen = ext.decrypt(dst, 0, encLen, decDst, 0);
|
||||
|
||||
assertArrayEquals(PLAIN, Arrays.copyOf(decDst, decLen));
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// 3. 초기화 검증
|
||||
// =========================================================================
|
||||
|
||||
@Test
|
||||
@DisplayName("3-1. 16바이트가 아닌 키 — IllegalArgumentException")
|
||||
void testInvalidKeyLength() {
|
||||
byte[] key32 = "0123456789abcdef0123456789abcdef".getBytes(StandardCharsets.UTF_8);
|
||||
SEEDCryptoModuleExtension ext = new SEEDCryptoModuleExtension();
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> ext.init("SEED", "CBC", "PKCS5Padding", IV_16, key32, key32));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("3-2. IV 없이 CBC 초기화 — 예외 발생")
|
||||
void testInitWithoutIv() {
|
||||
SEEDCryptoModuleExtension ext = new SEEDCryptoModuleExtension();
|
||||
|
||||
assertThrows(Exception.class,
|
||||
() -> ext.init("SEED", "CBC", "PKCS5Padding", KEY_128, KEY_128));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("3-3. GCM 모드 — IllegalArgumentException (미지원)")
|
||||
void testGcmNotSupported() {
|
||||
SEEDCryptoModuleExtension ext = new SEEDCryptoModuleExtension();
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> ext.init("SEED", "GCM", "NoPadding", IV_16, KEY_128, KEY_128));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user