XTS AES - remove deprecated BouncyCastle methods usage

master
Dmitry Isaenko 2023-01-21 14:17:50 +03:00
parent 6e4e4dd3e7
commit 394bc8f226
3 changed files with 25 additions and 33 deletions

View File

@ -34,10 +34,8 @@ import java.util.function.LongFunction;
* XTS-AES cipher with arbitrary (non 128-bit aligned) data unit lengths. * XTS-AES cipher with arbitrary (non 128-bit aligned) data unit lengths.
* *
* @author Ahseya * @author Ahseya
*/ *
* Class updated for NCAs usage.
/**
* Updated for special usage by Dmitry Isaenko.
* */ * */
@NotThreadSafe @NotThreadSafe
public class XTSAESCipher { public class XTSAESCipher {

View File

@ -26,7 +26,7 @@ package libKonogonka.xtsaes;
import net.jcip.annotations.NotThreadSafe; import net.jcip.annotations.NotThreadSafe;
import org.bouncycastle.crypto.BlockCipher; import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.DataLengthException; 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.crypto.params.KeyParameter;
import java.util.Arrays; import java.util.Arrays;
@ -36,10 +36,8 @@ import java.util.Objects;
* XTS core functions. * XTS core functions.
* *
* @author Ahseya * @author Ahseya
*/ *
* Class updated for NCAs usage.
/**
* Updated for special usage by Dmitry Isaenko.
* */ * */
@NotThreadSafe @NotThreadSafe
class XTSCore { class XTSCore {
@ -56,7 +54,7 @@ class XTSCore {
} }
XTSCore(XTSTweak tweak) { XTSCore(XTSTweak tweak) {
this(new AESFastEngine(), tweak); this(new AESEngine(), tweak);
} }
XTSCore(boolean isDefault) { XTSCore(boolean isDefault) {
@ -64,7 +62,7 @@ class XTSCore {
} }
XTSCore init(boolean forEncryption, KeyParameter key) throws IllegalArgumentException { XTSCore init(boolean forEncryption, KeyParameter key) throws IllegalArgumentException {
byte[] k = ((KeyParameter) key).getKey(); byte[] k = key.getKey();
if (k.length != 32 && k.length != 64) { if (k.length != 32 && k.length != 64) {
throw new IllegalArgumentException("bad key length: " + k.length); throw new IllegalArgumentException("bad key length: " + k.length);
} }

View File

@ -26,7 +26,7 @@ package libKonogonka.xtsaes;
import net.jcip.annotations.NotThreadSafe; import net.jcip.annotations.NotThreadSafe;
import org.bouncycastle.crypto.BlockCipher; import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.DataLengthException; 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.crypto.params.KeyParameter;
import org.bouncycastle.util.Pack; import org.bouncycastle.util.Pack;
@ -38,29 +38,11 @@ import java.util.function.LongFunction;
* XTS tweak with pluggable tweak function. * XTS tweak with pluggable tweak function.
* *
* @author Ahseya * @author Ahseya
*/ *
* Class updated for NCAs usage.
/**
* Updated for special usage by Dmitry Isaenko.
* */ * */
@NotThreadSafe @NotThreadSafe
class XTSTweak { 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 FDBK = 0x87;
private static final long MSB = 0x8000000000000000L; private static final long MSB = 0x8000000000000000L;
private static final int BLOCK_SIZE = 16; private static final int BLOCK_SIZE = 16;
@ -84,7 +66,7 @@ class XTSTweak {
} }
XTSTweak(LongFunction<byte[]> tweakFunction) { XTSTweak(LongFunction<byte[]> tweakFunction) {
this(new AESFastEngine(), tweakFunction); this(new AESEngine(), tweakFunction);
} }
XTSTweak(boolean isDefault) { XTSTweak(boolean isDefault) {
@ -92,6 +74,20 @@ class XTSTweak {
? XTSTweak::defaultTweakFunction ? XTSTweak::defaultTweakFunction
: XTSTweak::nintTweakFunction); : 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 { XTSTweak init(KeyParameter key) throws IllegalArgumentException {
cipher.init(true, key); cipher.init(true, key);