From 394bc8f2265017b60800c990e7dec6114a15892f Mon Sep 17 00:00:00 2001 From: Dmitry Isaenko Date: Sat, 21 Jan 2023 14:17:50 +0300 Subject: [PATCH] XTS AES - remove deprecated BouncyCastle methods usage --- .../libKonogonka/xtsaes/XTSAESCipher.java | 6 +-- .../java/libKonogonka/xtsaes/XTSCore.java | 12 +++--- .../java/libKonogonka/xtsaes/XTSTweak.java | 40 +++++++++---------- 3 files changed, 25 insertions(+), 33 deletions(-) diff --git a/src/main/java/libKonogonka/xtsaes/XTSAESCipher.java b/src/main/java/libKonogonka/xtsaes/XTSAESCipher.java index be62679..13aee66 100644 --- a/src/main/java/libKonogonka/xtsaes/XTSAESCipher.java +++ b/src/main/java/libKonogonka/xtsaes/XTSAESCipher.java @@ -34,10 +34,8 @@ import java.util.function.LongFunction; * XTS-AES cipher with arbitrary (non 128-bit aligned) data unit lengths. * * @author Ahseya - */ - -/** -* Updated for special usage by Dmitry Isaenko. + * + * Class updated for NCAs usage. * */ @NotThreadSafe public class XTSAESCipher { diff --git a/src/main/java/libKonogonka/xtsaes/XTSCore.java b/src/main/java/libKonogonka/xtsaes/XTSCore.java index cc409e7..29342c9 100644 --- a/src/main/java/libKonogonka/xtsaes/XTSCore.java +++ b/src/main/java/libKonogonka/xtsaes/XTSCore.java @@ -26,7 +26,7 @@ package libKonogonka.xtsaes; import net.jcip.annotations.NotThreadSafe; import org.bouncycastle.crypto.BlockCipher; import org.bouncycastle.crypto.DataLengthException; -import org.bouncycastle.crypto.engines.AESFastEngine; +import org.bouncycastle.crypto.engines.AESEngine; import org.bouncycastle.crypto.params.KeyParameter; import java.util.Arrays; @@ -36,10 +36,8 @@ import java.util.Objects; * XTS core functions. * * @author Ahseya - */ - -/** - * Updated for special usage by Dmitry Isaenko. + * + * Class updated for NCAs usage. * */ @NotThreadSafe class XTSCore { @@ -56,7 +54,7 @@ class XTSCore { } XTSCore(XTSTweak tweak) { - this(new AESFastEngine(), tweak); + this(new AESEngine(), tweak); } XTSCore(boolean isDefault) { @@ -64,7 +62,7 @@ class XTSCore { } XTSCore init(boolean forEncryption, KeyParameter key) throws IllegalArgumentException { - byte[] k = ((KeyParameter) key).getKey(); + byte[] k = key.getKey(); if (k.length != 32 && k.length != 64) { throw new IllegalArgumentException("bad key length: " + k.length); } diff --git a/src/main/java/libKonogonka/xtsaes/XTSTweak.java b/src/main/java/libKonogonka/xtsaes/XTSTweak.java index cd2c2af..1ff0ee7 100644 --- a/src/main/java/libKonogonka/xtsaes/XTSTweak.java +++ b/src/main/java/libKonogonka/xtsaes/XTSTweak.java @@ -26,7 +26,7 @@ package libKonogonka.xtsaes; import net.jcip.annotations.NotThreadSafe; import org.bouncycastle.crypto.BlockCipher; import org.bouncycastle.crypto.DataLengthException; -import org.bouncycastle.crypto.engines.AESFastEngine; +import org.bouncycastle.crypto.engines.AESEngine; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.util.Pack; @@ -38,29 +38,11 @@ import java.util.function.LongFunction; * XTS tweak with pluggable tweak function. * * @author Ahseya - */ - -/** - * Updated for special usage by Dmitry Isaenko. + * + * Class updated for NCAs usage. * */ @NotThreadSafe class XTSTweak { - static byte[] nintTweakFunction(long tweakValue) { - byte[] bs = new byte[BLOCK_SIZE]; - byte[] twk = Pack.longToBigEndian(tweakValue); - int j = BLOCK_SIZE - twk.length; - for (byte b: twk){ - bs[j++] = b; - } - return bs; - } - - static byte[] defaultTweakFunction(long tweakValue) { - byte[] bs = Pack.longToLittleEndian(tweakValue); - bs = Arrays.copyOfRange(bs, 0, BLOCK_SIZE); - return bs; - } - private static final long FDBK = 0x87; private static final long MSB = 0x8000000000000000L; private static final int BLOCK_SIZE = 16; @@ -84,7 +66,7 @@ class XTSTweak { } XTSTweak(LongFunction tweakFunction) { - this(new AESFastEngine(), tweakFunction); + this(new AESEngine(), tweakFunction); } XTSTweak(boolean isDefault) { @@ -92,6 +74,20 @@ class XTSTweak { ? XTSTweak::defaultTweakFunction : XTSTweak::nintTweakFunction); } + static byte[] defaultTweakFunction(long tweakValue) { + byte[] bs = Pack.longToLittleEndian(tweakValue); + bs = Arrays.copyOfRange(bs, 0, BLOCK_SIZE); + return bs; + } + static byte[] nintTweakFunction(long tweakValue) { + byte[] bs = new byte[BLOCK_SIZE]; + byte[] twk = Pack.longToBigEndian(tweakValue); + int j = BLOCK_SIZE - twk.length; + for (byte b: twk){ + bs[j++] = b; + } + return bs; + } XTSTweak init(KeyParameter key) throws IllegalArgumentException { cipher.init(true, key);