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);
}
@@ -396,4 +396,99 @@ class AESCryptoModuleExtensionTest {
assertArrayEquals(enc1, enc2, "편의 메서드와 오프셋 메서드의 결과가 동일해야 한다");
}
// =========================================================================
// 8. FF1 / FF3-1 — AAD를 tweak으로 사용
// =========================================================================
private static final byte[] FPE_PLAIN = "1234567890123456".getBytes(StandardCharsets.UTF_8);
private static final byte[] TWEAK_A = "ACCT".getBytes(StandardCharsets.UTF_8);
private static final byte[] TWEAK_B = "RRN".getBytes(StandardCharsets.UTF_8);
private static final byte[] TWEAK7_A = "TWEAK_A".getBytes(StandardCharsets.UTF_8);
private static AESCryptoModuleExtension fpe(String mode, byte[] iv) throws Exception {
AESCryptoModuleExtension ext = new AESCryptoModuleExtension();
ext.init("AES", mode, "NoPadding", iv, KEY_128, KEY_128);
return ext;
}
@Test
@DisplayName("8-1. FF1 — AAD(tweak) 라운드트립, 길이 보존")
void testFf1_aadTweak_roundTrip() throws Exception {
AESCryptoModuleExtension ext = fpe("FF1", IV_16);
byte[] enc = ext.encrypt(FPE_PLAIN, TWEAK_A);
assertEquals(FPE_PLAIN.length, enc.length, "FPE 암호문 길이는 평문과 같아야 한다");
assertArrayEquals(FPE_PLAIN, ext.decrypt(enc, TWEAK_A));
}
@Test
@DisplayName("8-2. FF1 — tweak이 다르면 암호문이 다르다")
void testFf1_differentTweak_differentCiphertext() throws Exception {
AESCryptoModuleExtension ext = fpe("FF1", IV_16);
assertFalse(Arrays.equals(ext.encrypt(FPE_PLAIN, TWEAK_A), ext.encrypt(FPE_PLAIN, TWEAK_B)));
}
@Test
@DisplayName("8-3. FF1 — 같은 tweak이면 결정적(같은 암호문)")
void testFf1_sameTweak_deterministic() throws Exception {
assertArrayEquals(fpe("FF1", IV_16).encrypt(FPE_PLAIN, TWEAK_A), fpe("FF1", null).encrypt(FPE_PLAIN, TWEAK_A),
"AAD로 tweak을 주면 설정 IV와 무관하게 같은 결과여야 한다");
}
@Test
@DisplayName("8-4. FF1 — AAD null이면 IV를 tweak으로 사용")
void testFf1_nullAad_usesIvAsTweak() throws Exception {
AESCryptoModuleExtension ext = fpe("FF1", IV_16);
assertArrayEquals(ext.encrypt(FPE_PLAIN, IV_16), ext.encrypt(FPE_PLAIN, (byte[]) null));
assertArrayEquals(ext.encrypt(FPE_PLAIN, IV_16), ext.encrypt(FPE_PLAIN));
}
@Test
@DisplayName("8-5. FF1 — 다른 tweak으로 복호화하면 원문이 나오지 않음(인증 없음)")
void testFf1_wrongTweak_wrongPlain() throws Exception {
AESCryptoModuleExtension ext = fpe("FF1", IV_16);
byte[] enc = ext.encrypt(FPE_PLAIN, TWEAK_A);
assertFalse(Arrays.equals(FPE_PLAIN, ext.decrypt(enc, TWEAK_B)));
}
@Test
@DisplayName("8-6. FF1 — IV 없이 init 후 AAD(tweak)로 동작, AAD도 없으면 IllegalStateException")
void testFf1_noIv() throws Exception {
AESCryptoModuleExtension ext = fpe("FF1", null);
assertArrayEquals(FPE_PLAIN, ext.decrypt(ext.encrypt(FPE_PLAIN, TWEAK_A), TWEAK_A));
assertThrows(IllegalStateException.class, () -> ext.encrypt(FPE_PLAIN));
assertThrows(IllegalStateException.class, () -> ext.decrypt(FPE_PLAIN));
}
@Test
@DisplayName("8-7. FF3-1 — 7바이트 AAD(tweak) 라운드트립")
void testFf31_aadTweak_roundTrip() throws Exception {
AESCryptoModuleExtension ext = fpe("FF3-1", null);
byte[] enc = ext.encrypt(FPE_PLAIN, TWEAK7_A);
assertEquals(FPE_PLAIN.length, enc.length);
assertArrayEquals(FPE_PLAIN, ext.decrypt(enc, TWEAK7_A));
}
@Test
@DisplayName("8-8. FF3-1 — 7바이트가 아닌 AAD(tweak)는 예외")
void testFf31_invalidTweakLength() throws Exception {
AESCryptoModuleExtension ext = fpe("FF3-1", null);
assertThrows(Exception.class, () -> ext.encrypt(FPE_PLAIN, TWEAK_A));
}
@Test
@DisplayName("8-9. CBC — AAD는 tweak으로 쓰이지 않고 무시된다")
void testCbc_aadIgnored() throws Exception {
AESCryptoModuleExtension ext = new AESCryptoModuleExtension();
ext.init("AES", "CBC", "PKCS5Padding", IV_16, KEY_128, KEY_128);
assertArrayEquals(ext.encrypt(FPE_PLAIN), ext.encrypt(FPE_PLAIN, TWEAK_A));
}
}
@@ -151,4 +151,99 @@ class ARIACryptoModuleExtensionTest {
assertArrayEquals(PLAIN, ext1.decrypt(enc1, 0, enc1.length));
assertArrayEquals(PLAIN, ext2.decrypt(enc2, 0, enc2.length));
}
// =========================================================================
// 5. FF1 / FF3-1 — AAD를 tweak으로 사용
// =========================================================================
private static final byte[] FPE_PLAIN = "1234567890123456".getBytes(StandardCharsets.UTF_8);
private static final byte[] TWEAK_A = "ACCT".getBytes(StandardCharsets.UTF_8);
private static final byte[] TWEAK_B = "RRN".getBytes(StandardCharsets.UTF_8);
private static final byte[] TWEAK7_A = "TWEAK_A".getBytes(StandardCharsets.UTF_8);
private static ARIACryptoModuleExtension fpe(String mode, byte[] iv) throws Exception {
ARIACryptoModuleExtension ext = new ARIACryptoModuleExtension();
ext.init("ARIA", mode, "NoPadding", iv, KEY_128, KEY_128);
return ext;
}
@Test
@DisplayName("5-1. FF1 — AAD(tweak) 라운드트립, 길이 보존")
void testFf1_aadTweak_roundTrip() throws Exception {
ARIACryptoModuleExtension ext = fpe("FF1", IV_16);
byte[] enc = ext.encrypt(FPE_PLAIN, TWEAK_A);
assertEquals(FPE_PLAIN.length, enc.length, "FPE 암호문 길이는 평문과 같아야 한다");
assertArrayEquals(FPE_PLAIN, ext.decrypt(enc, TWEAK_A));
}
@Test
@DisplayName("5-2. FF1 — tweak이 다르면 암호문이 다르다")
void testFf1_differentTweak_differentCiphertext() throws Exception {
ARIACryptoModuleExtension ext = fpe("FF1", IV_16);
assertFalse(Arrays.equals(ext.encrypt(FPE_PLAIN, TWEAK_A), ext.encrypt(FPE_PLAIN, TWEAK_B)));
}
@Test
@DisplayName("5-3. FF1 — 같은 tweak이면 결정적(같은 암호문)")
void testFf1_sameTweak_deterministic() throws Exception {
assertArrayEquals(fpe("FF1", IV_16).encrypt(FPE_PLAIN, TWEAK_A), fpe("FF1", null).encrypt(FPE_PLAIN, TWEAK_A),
"AAD로 tweak을 주면 설정 IV와 무관하게 같은 결과여야 한다");
}
@Test
@DisplayName("5-4. FF1 — AAD null이면 IV를 tweak으로 사용")
void testFf1_nullAad_usesIvAsTweak() throws Exception {
ARIACryptoModuleExtension ext = fpe("FF1", IV_16);
assertArrayEquals(ext.encrypt(FPE_PLAIN, IV_16), ext.encrypt(FPE_PLAIN, (byte[]) null));
assertArrayEquals(ext.encrypt(FPE_PLAIN, IV_16), ext.encrypt(FPE_PLAIN));
}
@Test
@DisplayName("5-5. FF1 — 다른 tweak으로 복호화하면 원문이 나오지 않음(인증 없음)")
void testFf1_wrongTweak_wrongPlain() throws Exception {
ARIACryptoModuleExtension ext = fpe("FF1", IV_16);
byte[] enc = ext.encrypt(FPE_PLAIN, TWEAK_A);
assertFalse(Arrays.equals(FPE_PLAIN, ext.decrypt(enc, TWEAK_B)));
}
@Test
@DisplayName("5-6. FF1 — IV 없이 init 후 AAD(tweak)로 동작, AAD도 없으면 IllegalStateException")
void testFf1_noIv() throws Exception {
ARIACryptoModuleExtension ext = fpe("FF1", null);
assertArrayEquals(FPE_PLAIN, ext.decrypt(ext.encrypt(FPE_PLAIN, TWEAK_A), TWEAK_A));
assertThrows(IllegalStateException.class, () -> ext.encrypt(FPE_PLAIN));
assertThrows(IllegalStateException.class, () -> ext.decrypt(FPE_PLAIN));
}
@Test
@DisplayName("5-7. FF3-1 — 7바이트 AAD(tweak) 라운드트립")
void testFf31_aadTweak_roundTrip() throws Exception {
ARIACryptoModuleExtension ext = fpe("FF3-1", null);
byte[] enc = ext.encrypt(FPE_PLAIN, TWEAK7_A);
assertEquals(FPE_PLAIN.length, enc.length);
assertArrayEquals(FPE_PLAIN, ext.decrypt(enc, TWEAK7_A));
}
@Test
@DisplayName("5-8. FF3-1 — 7바이트가 아닌 AAD(tweak)는 예외")
void testFf31_invalidTweakLength() throws Exception {
ARIACryptoModuleExtension ext = fpe("FF3-1", null);
assertThrows(Exception.class, () -> ext.encrypt(FPE_PLAIN, TWEAK_A));
}
@Test
@DisplayName("5-9. CBC — AAD는 tweak으로 쓰이지 않고 무시된다")
void testCbc_aadIgnored() throws Exception {
ARIACryptoModuleExtension ext = new ARIACryptoModuleExtension();
ext.init("ARIA", "CBC", "PKCS5Padding", IV_16, KEY_128, KEY_128);
assertArrayEquals(ext.encrypt(FPE_PLAIN), ext.encrypt(FPE_PLAIN, TWEAK_A));
}
}