-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for the Tink FPE library. Refactored some of the code related the existing Tink features (tink-daead). Should be read together with the changes in dapla-dlp-pseudo-core. * Add tink FPE function + refactor daead * Test cases for FAIL and SKIP strategies * Refactor * Update dependencies --------- Co-authored-by: Miles Mason Winther <[email protected]>
- Loading branch information
Showing
21 changed files
with
409 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 0 additions & 19 deletions
19
src/main/java/no/ssb/dapla/dlp/pseudo/func/daead/DaeadFuncConfigService.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/no/ssb/dapla/dlp/pseudo/func/tink/daead/TinkDaeadFuncConfigService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package no.ssb.dapla.dlp.pseudo.func.tink.daead; | ||
|
||
import com.google.crypto.tink.DeterministicAead; | ||
import lombok.extern.slf4j.Slf4j; | ||
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncConfig; | ||
|
||
import static no.ssb.dapla.dlp.pseudo.func.tink.daead.TinkDaeadFuncConfig.Param.DAEAD; | ||
|
||
@Slf4j | ||
public class TinkDaeadFuncConfigService { | ||
|
||
public TinkDaeadFuncConfig resolve(PseudoFuncConfig cfg) { | ||
|
||
return TinkDaeadFuncConfig.builder() | ||
.daead(cfg.getRequired(DAEAD, DeterministicAead.class)) | ||
.build(); | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/no/ssb/dapla/dlp/pseudo/func/tink/fpe/TinkFpeFunc.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package no.ssb.dapla.dlp.pseudo.func.tink.fpe; | ||
|
||
import lombok.NonNull; | ||
import lombok.extern.slf4j.Slf4j; | ||
import no.ssb.crypto.tink.fpe.Fpe; | ||
import no.ssb.dapla.dlp.pseudo.func.*; | ||
|
||
import java.security.GeneralSecurityException; | ||
|
||
import static no.ssb.crypto.tink.fpe.util.ByteArrayUtil.b2s; | ||
import static no.ssb.crypto.tink.fpe.util.ByteArrayUtil.s2b; | ||
|
||
@Slf4j | ||
public class TinkFpeFunc extends AbstractPseudoFunc { | ||
private final TinkFpeFuncConfigService configService = new TinkFpeFuncConfigService(); | ||
private final TinkFpeFuncConfig config; | ||
|
||
public TinkFpeFunc(@NonNull PseudoFuncConfig genericConfig) { | ||
super(genericConfig.getFuncDecl()); | ||
this.config = configService.resolve(genericConfig); | ||
} | ||
|
||
private Fpe fpe() { | ||
return config.getFpe(); | ||
} | ||
|
||
@Override | ||
public PseudoFuncOutput apply(PseudoFuncInput input) { | ||
PseudoFuncOutput output = new PseudoFuncOutput(); | ||
input.getValues().forEach(in -> { | ||
String plain = String.valueOf(in); | ||
try { | ||
byte[] ciphertext = fpe().encrypt(s2b(plain), config.getFpeParams()); | ||
output.add(b2s(ciphertext)); | ||
} | ||
catch (GeneralSecurityException e) { | ||
throw new TinkFpePseudoFuncException("Tink FPE apply error. func=" + getFuncDecl() + ", contentType=" + input.getParamMetadata(), e); | ||
} | ||
}); | ||
|
||
return output; | ||
} | ||
|
||
@Override | ||
public PseudoFuncOutput restore(PseudoFuncInput input) { | ||
PseudoFuncOutput output = new PseudoFuncOutput(); | ||
input.getValues().forEach(in -> { | ||
byte[] ciphertext = s2b(String.valueOf(in)); | ||
try { | ||
byte[] plaintext = fpe().decrypt(ciphertext, config.getFpeParams()); | ||
output.add(b2s(plaintext)); | ||
} | ||
catch (GeneralSecurityException e) { | ||
throw new TinkFpePseudoFuncException("Tink FPE restore error. func=" + getFuncDecl() + ", contentType=" + input.getParamMetadata(), e); | ||
} | ||
}); | ||
|
||
return output; | ||
} | ||
|
||
public static class TinkFpePseudoFuncException extends PseudoFuncException { | ||
public TinkFpePseudoFuncException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/no/ssb/dapla/dlp/pseudo/func/tink/fpe/TinkFpeFuncConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package no.ssb.dapla.dlp.pseudo.func.tink.fpe; | ||
|
||
import lombok.Builder; | ||
import lombok.Value; | ||
import lombok.experimental.UtilityClass; | ||
import no.ssb.crypto.tink.fpe.Fpe; | ||
import no.ssb.crypto.tink.fpe.FpeParams; | ||
|
||
@Value | ||
@Builder | ||
public class TinkFpeFuncConfig { | ||
private final Fpe fpe; | ||
private final FpeParams fpeParams; | ||
|
||
@UtilityClass | ||
public static class Param { | ||
public static final String FPE = "fpe"; | ||
public static final String KEY_ID = "keyId"; | ||
public static final String UNKNOWN_CHARACTER_STRATEGY = "strategy"; | ||
public static final String REDACT_CHAR = "redactChar"; | ||
public static final String TWEAK = "tweak"; | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/no/ssb/dapla/dlp/pseudo/func/tink/fpe/TinkFpeFuncConfigService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package no.ssb.dapla.dlp.pseudo.func.tink.fpe; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import no.ssb.crypto.tink.fpe.Fpe; | ||
import no.ssb.crypto.tink.fpe.FpeParams; | ||
import no.ssb.crypto.tink.fpe.UnknownCharacterStrategy; | ||
import no.ssb.dapla.dlp.pseudo.func.PseudoFuncConfig; | ||
|
||
import static no.ssb.dapla.dlp.pseudo.func.tink.fpe.TinkFpeFuncConfig.Param.*; | ||
|
||
@Slf4j | ||
public class TinkFpeFuncConfigService { | ||
|
||
public TinkFpeFuncConfig resolve(PseudoFuncConfig cfg) { | ||
FpeParams fpeParams = FpeParams.DEFAULT; | ||
if (cfg.has(UNKNOWN_CHARACTER_STRATEGY)) { | ||
fpeParams.unknownCharacterStrategy(cfg.getRequired(UNKNOWN_CHARACTER_STRATEGY, UnknownCharacterStrategy.class)); | ||
} | ||
if (cfg.has(REDACT_CHAR)) { | ||
fpeParams.redactionChar(cfg.getRequired(REDACT_CHAR, Character.class)); | ||
} | ||
|
||
//UnknownCharacterStrategy strategy = cfg.get(UNKNOWN_CHARACTER_STRATEGY, UnknownCharacterStrategy.class).orElse(UnknownCharacterStrategy.FAIL); | ||
|
||
return TinkFpeFuncConfig.builder() | ||
.fpe(cfg.getRequired(FPE, Fpe.class)) | ||
.fpeParams(fpeParams) | ||
.build(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.