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:
curry772
2026-10-01 15:08:09 +09:00
parent 1649a05501
commit 5bd6500bd7
4 changed files with 295 additions and 3 deletions
@@ -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);
}
}