FF1/FF3-1 모드에서 AAD 를 tweak 으로 사용

- AES/ARIA : FF 모드에서 aad 가 있으면 호출 시점 tweak 으로 사용, 없으면 IV 사용
- FF 모드는 IV 없이 init 가능 (IV·AAD 모두 없으면 IllegalStateException)
- ARIA 에 AAD 오버로드 구현 추가
- 단위테스트 AES/ARIA 각 9건 추가
This commit is contained in:
curry772
2026-10-01 15:08:23 +09:00
parent 5bd6500bd7
commit d38c7dac05
5 changed files with 305 additions and 16 deletions
@@ -19,6 +19,7 @@ public class AESCryptoModuleExtension implements CryptoModuleExtension {
private static final int GCM_IV_LENGTH = 12;
private static final int GCM_TAG_LENGTH = 128;
private static final String DEFAULT_PROVIDER = "BC";
private static final int FPE_RADIX = 256;
private Cipher encryptEngine;
private Cipher decryptEngine;
@@ -63,7 +64,8 @@ public class AESCryptoModuleExtension implements CryptoModuleExtension {
Security.addProvider(new BouncyCastleProvider());
}
if (!mode.equalsIgnoreCase("GCM")) {
// FF 모드에서 IV(tweak)가 없으면 호출 시 AAD로 tweak을 받으므로 엔진을 미리 만들지 않는다
if (!mode.equalsIgnoreCase("GCM") && !(isFpe() && iv == null)) {
String m = mode != null ? mode : "";
String p = pad != null ? pad : "";
String transformation = String.format("AES/%s/%s", m, p);
@@ -71,8 +73,8 @@ public class AESCryptoModuleExtension implements CryptoModuleExtension {
Key encKeySpec = new SecretKeySpec(encKey, "AES");
Key decKeySpec = new SecretKeySpec(decKey, "AES");
AlgorithmParameterSpec param = iv != null ? new IvParameterSpec(iv) : null;
if (m.toLowerCase().contains("ff")) {
param = new FPEParameterSpec(256, iv);
if (isFpe()) {
param = new FPEParameterSpec(FPE_RADIX, iv);
}
String prov = this.provider != null ? this.provider : DEFAULT_PROVIDER;
@@ -83,13 +85,33 @@ public class AESCryptoModuleExtension implements CryptoModuleExtension {
}
}
/** FF1 / FF3-1 (형식보존암호화) 모드 여부 */
private boolean isFpe() {
return this.mode != null && this.mode.toLowerCase().contains("ff");
}
private Cipher engine(Cipher c) {
if (c == null) {
throw new IllegalStateException("FF 모드에 IV(tweak)가 설정되지 않았습니다. AAD로 tweak을 전달하세요.");
}
return c;
}
/** 호출 시점 tweak으로 FPE Cipher를 새로 생성한다. */
private Cipher fpeCipher(int opmode, byte[] key, byte[] tweak) throws Exception {
String prov = this.provider != null ? this.provider : DEFAULT_PROVIDER;
Cipher cipher = Cipher.getInstance(String.format("AES/%s/%s", this.mode, this.pad != null ? this.pad : ""), prov);
cipher.init(opmode, new SecretKeySpec(key, "AES"), new FPEParameterSpec(FPE_RADIX, tweak), (SecureRandom) null);
return cipher;
}
// ------------------------------------------------------------------
// encrypt
// ------------------------------------------------------------------
@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);
return engine(this.encryptEngine).doFinal(src, sindex, slength, dst, dindex);
}
@Override
@@ -100,7 +122,8 @@ public class AESCryptoModuleExtension implements CryptoModuleExtension {
/**
* AAD를 명시한 암호화.
* GCM 모드에서 aad != null 이면 해당 값을 사용하고, null 이면 iv를 AAD로 사용하는 기본 동작을 따른다.
* 비-GCM 모드는 AAD를 무시하고 사전 초기화된 엔진으로 처리한다.
* FF1/FF3-1 모드에서 aad != null 이면 해당 값을 tweak으로 사용하고, null 이면 iv를 tweak으로 사용한다.
* 그 외 모드는 AAD를 무시하고 사전 초기화된 엔진으로 처리한다.
*/
@Override
public byte[] encrypt(byte[] src, int sindex, int slength, byte[] aad) throws Exception {
@@ -124,7 +147,11 @@ public class AESCryptoModuleExtension implements CryptoModuleExtension {
return output;
}
int outLen = this.encryptEngine.getOutputSize(slength);
if (aad != null && isFpe()) {
return fpeCipher(Cipher.ENCRYPT_MODE, this.encKey, aad).doFinal(src, sindex, slength);
}
int outLen = engine(this.encryptEngine).getOutputSize(slength);
byte[] dst = new byte[outLen];
int actual = encrypt(src, sindex, slength, dst, 0);
return ByteBuffer.allocate(actual).put(dst, 0, actual).array();
@@ -136,7 +163,7 @@ public class AESCryptoModuleExtension implements CryptoModuleExtension {
@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);
return engine(this.decryptEngine).doFinal(src, sindex, slength, dst, dindex);
}
@Override
@@ -147,7 +174,8 @@ public class AESCryptoModuleExtension implements CryptoModuleExtension {
/**
* AAD를 명시한 복호화.
* GCM 모드에서 aad != null 이면 해당 값을 사용하고, null 이면 iv를 AAD로 사용하는 기본 동작을 따른다.
* 비-GCM 모드는 AAD를 무시하고 사전 초기화된 엔진으로 처리한다.
* FF1/FF3-1 모드에서 aad != null 이면 해당 값을 tweak으로 사용하고, null 이면 iv를 tweak으로 사용한다.
* 그 외 모드는 AAD를 무시하고 사전 초기화된 엔진으로 처리한다.
*/
@Override
public byte[] decrypt(byte[] src, int sindex, int slength, byte[] aad) throws Exception {
@@ -168,6 +196,9 @@ public class AESCryptoModuleExtension implements CryptoModuleExtension {
return cipher.doFinal(cipherTextWithTag);
}
return this.decryptEngine.doFinal(src, sindex, slength);
if (aad != null && isFpe()) {
return fpeCipher(Cipher.DECRYPT_MODE, this.decKey, aad).doFinal(src, sindex, slength);
}
return engine(this.decryptEngine).doFinal(src, sindex, slength);
}
}
@@ -15,8 +15,14 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class ARIACryptoModuleExtension implements CryptoModuleExtension {
private static final int FPE_RADIX = 256;
private Cipher encryptEngine;
private Cipher decryptEngine;
private String mode;
private String pad;
private byte[] encKey;
private byte[] decKey;
@Override
public void init(String alg, String mode, String pad, byte[] encKey, byte[] decKey) throws Exception {
@@ -30,11 +36,21 @@ public class ARIACryptoModuleExtension implements CryptoModuleExtension {
}
String m = mode != null ? mode : "";
String p = pad != null ? pad : "";
this.mode = m;
this.pad = p;
this.encKey = encKey;
this.decKey = decKey;
Key encKeySpec = new SecretKeySpec(encKey, "ARIA");
Key decKeySpec = new SecretKeySpec(decKey, "ARIA");
AlgorithmParameterSpec param = iv != null ? new IvParameterSpec(iv) : null;
if (m.toLowerCase().contains("ff")) {
param = new FPEParameterSpec(256, iv);
if (isFpe()) {
param = new FPEParameterSpec(FPE_RADIX, iv);
}
// FF 모드에서 IV(tweak)가 없으면 호출 시 AAD로 tweak을 받으므로 엔진을 미리 만들지 않는다
if (isFpe() && iv == null) {
this.encryptEngine = null;
this.decryptEngine = null;
return;
}
this.encryptEngine = Cipher.getInstance(String.format("ARIA/%s/%s", m, p));
this.encryptEngine.init(Cipher.ENCRYPT_MODE, encKeySpec, param, (SecureRandom) null);
@@ -42,13 +58,33 @@ public class ARIACryptoModuleExtension implements CryptoModuleExtension {
this.decryptEngine.init(Cipher.DECRYPT_MODE, decKeySpec, param, (SecureRandom) null);
}
/** FF1 / FF3-1 (형식보존암호화) 모드 여부 */
private boolean isFpe() {
return this.mode != null && this.mode.toLowerCase().contains("ff");
}
private Cipher engine(Cipher c) {
if (c == null) {
throw new IllegalStateException("FF 모드에 IV(tweak)가 설정되지 않았습니다. AAD로 tweak을 전달하세요.");
}
return c;
}
/** 호출 시점 tweak으로 FPE Cipher를 새로 생성한다. */
private Cipher fpeCipher(int opmode, byte[] key, byte[] tweak) throws Exception {
Cipher cipher = Cipher.getInstance(String.format("ARIA/%s/%s", this.mode, this.pad));
cipher.init(opmode, new SecretKeySpec(key, "ARIA"), new FPEParameterSpec(FPE_RADIX, tweak), (SecureRandom) null);
return cipher;
}
@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);
return engine(this.encryptEngine).doFinal(src, sindex, slength, dst, dindex);
}
@Override
public byte[] encrypt(byte[] src, int sindex, int slength) throws Exception {
engine(this.encryptEngine);
int size = (slength + 17) / this.encryptEngine.getBlockSize() * this.encryptEngine.getBlockSize();
size += (slength + 17) % this.encryptEngine.getBlockSize() == 0 ? 0 : this.encryptEngine.getBlockSize();
byte[] dst = new byte[this.encryptEngine.getOutputSize(size)];
@@ -56,13 +92,39 @@ public class ARIACryptoModuleExtension implements CryptoModuleExtension {
return ByteBuffer.allocate(size).put(dst, 0, size).array();
}
/**
* AAD를 명시한 암호화.
* FF1/FF3-1 모드에서 aad != null 이면 해당 값을 tweak으로 사용하고, null 이면 iv를 tweak으로 사용한다.
* 그 외 모드는 AAD를 무시한다.
*/
@Override
public byte[] encrypt(byte[] src, int sindex, int slength, byte[] aad) throws Exception {
if (aad != null && isFpe()) {
return fpeCipher(Cipher.ENCRYPT_MODE, this.encKey, aad).doFinal(src, sindex, slength);
}
return this.encrypt(src, sindex, slength);
}
@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);
return engine(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);
return engine(this.decryptEngine).doFinal(src, sindex, slength);
}
/**
* AAD를 명시한 복호화.
* FF1/FF3-1 모드에서 aad != null 이면 해당 값을 tweak으로 사용하고, null 이면 iv를 tweak으로 사용한다.
* 그 외 모드는 AAD를 무시한다.
*/
@Override
public byte[] decrypt(byte[] src, int sindex, int slength, byte[] aad) throws Exception {
if (aad != null && isFpe()) {
return fpeCipher(Cipher.DECRYPT_MODE, this.decKey, aad).doFinal(src, sindex, slength);
}
return this.decrypt(src, sindex, slength);
}
}
@@ -20,7 +20,10 @@ public interface CryptoModuleExtension {
byte[] encrypt(byte[] src, int sindex, int slength) throws Exception;
/** AAD를 명시한 암호화. null이면 구현체 기본 동작(IV를 AAD로 사용하거나 AAD 없음)을 따른다. */
/**
* AAD를 명시한 암호화. null이면 구현체 기본 동작(IV를 AAD로 사용하거나 AAD 없음)을 따른다.
* FF1/FF3-1(형식보존암호화) 모드에서는 이 값을 tweak으로 사용한다.
*/
default byte[] encrypt(byte[] src, int sindex, int slength, byte[] aad) throws Exception {
return encrypt(src, sindex, slength);
}
@@ -37,7 +40,10 @@ public interface CryptoModuleExtension {
byte[] decrypt(byte[] src, int sindex, int slength) throws Exception;
/** AAD를 명시한 복호화. null이면 구현체 기본 동작을 따른다. */
/**
* AAD를 명시한 복호화. null이면 구현체 기본 동작을 따른다.
* FF1/FF3-1(형식보존암호화) 모드에서는 이 값을 tweak으로 사용한다.
*/
default byte[] decrypt(byte[] src, int sindex, int slength, byte[] aad) throws Exception {
return decrypt(src, sindex, slength);
}