code polish/cleanup

This commit is contained in:
nathan 2016-02-11 03:03:19 +01:00
parent 5aad770f0d
commit 5b8cbadb84
21 changed files with 295 additions and 1097 deletions

View File

@ -54,7 +54,6 @@
<orderEntry type="library" name="kryo3" level="application" />
<orderEntry type="library" name="jansi (1.11b)" level="application" />
<orderEntry type="library" name="netty-all (4.1.0)" level="application" />
<orderEntry type="library" name="JCTools (1.4 alpha MTAQ)" level="application" />
<orderEntry type="library" name="controlsfx" level="application" />
<orderEntry type="library" name="junit-4.12" level="application" />
<orderEntry type="library" name="bouncyCastle bcpg-jdk15on (1.53)" level="application" />

View File

@ -15,7 +15,7 @@
*/
package dorkbox.util.bytes;
import dorkbox.util.objectPool.PoolableObject;
import dorkbox.objectPool.PoolableObject;
public
class ByteBuffer2Poolable extends PoolableObject<ByteBuffer2> {

View File

@ -34,38 +34,34 @@
*/
package dorkbox.util.bytes;
public class OptimizeUtilsByteArray {
public
class OptimizeUtilsByteArray {
private static final OptimizeUtilsByteArray instance = new OptimizeUtilsByteArray();
public static OptimizeUtilsByteArray get() {
return instance;
}
private OptimizeUtilsByteArray() {
/**
* FROM KRYO
* <p>
* Returns the number of bytes that would be written with {@link #writeInt(byte[], int, boolean)}.
*
* @param optimizePositive
* true if you want to optimize the number of bytes needed to write the length value
*/
public static
int intLength(int value, boolean optimizePositive) {
return ByteBuffer2.intLength(value, optimizePositive);
}
// int
/**
* FROM KRYO
*
* Returns the number of bytes that would be written with {@link #writeInt(int, boolean)}.
*
* @param optimizePositive true if you want to optimize the number of bytes needed to write the length value
*/
public final int intLength (int value, boolean optimizePositive) {
return ByteBuffer2.intLength(value, optimizePositive);
}
/**
* FROM KRYO
*
* <p>
* look at buffer, and see if we can read the length of the int off of it. (from the reader index)
*
* @return 0 if we could not read anything, >0 for the number of bytes for the int on the buffer
*/
public boolean canReadInt(byte[] buffer) {
@SuppressWarnings("SimplifiableIfStatement")
public static
boolean canReadInt(byte[] buffer) {
int length = buffer.length;
if (length >= 5) {
@ -93,18 +89,17 @@ public class OptimizeUtilsByteArray {
if ((buffer[p++] & 0x80) == 0) {
return true;
}
if (p == length) {
return false;
}
return true;
return p != length;
}
/**
* FROM KRYO
*
* <p>
* Reads an int from the buffer that was optimized.
*/
public int readInt (byte[] buffer, boolean optimizePositive) {
@SuppressWarnings("UnusedAssignment")
public static
int readInt(byte[] buffer, boolean optimizePositive) {
int position = 0;
int b = buffer[position++];
int result = b & 0x7F;
@ -129,56 +124,60 @@ public class OptimizeUtilsByteArray {
/**
* FROM KRYO
*
* <p>
* Writes the specified int to the buffer using 1 to 5 bytes, depending on the size of the number.
*
* @param optimizePositive true if you want to optimize the number of bytes needed to write the length value
* @param optimizePositive
* true if you want to optimize the number of bytes needed to write the length value
*
* @return the number of bytes written.
*/
public int writeInt (byte[] buffer, int value, boolean optimizePositive) {
@SuppressWarnings({"UnusedAssignment", "NumericCastThatLosesPrecision", "Duplicates"})
public static
int writeInt(byte[] buffer, int value, boolean optimizePositive) {
int position = 0;
if (!optimizePositive) {
value = value << 1 ^ value >> 31;
}
if (value >>> 7 == 0) {
buffer[position++] = (byte)value;
buffer[position++] = (byte) value;
return 1;
}
if (value >>> 14 == 0) {
buffer[position++] = (byte)(value & 0x7F | 0x80);
buffer[position++] = (byte)(value >>> 7);
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7);
return 2;
}
if (value >>> 21 == 0) {
buffer[position++] = (byte)(value & 0x7F | 0x80);
buffer[position++] = (byte)(value >>> 7 | 0x80);
buffer[position++] = (byte)(value >>> 14);
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14);
return 3;
}
if (value >>> 28 == 0) {
buffer[position++] = (byte)(value & 0x7F | 0x80);
buffer[position++] = (byte)(value >>> 7 | 0x80);
buffer[position++] = (byte)(value >>> 14 | 0x80);
buffer[position++] = (byte)(value >>> 21);
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14 | 0x80);
buffer[position++] = (byte) (value >>> 21);
return 4;
}
buffer[position++] = (byte)(value & 0x7F | 0x80);
buffer[position++] = (byte)(value >>> 7 | 0x80);
buffer[position++] = (byte)(value >>> 14 | 0x80);
buffer[position++] = (byte)(value >>> 21 | 0x80);
buffer[position++] = (byte)(value >>> 28);
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14 | 0x80);
buffer[position++] = (byte) (value >>> 21 | 0x80);
buffer[position++] = (byte) (value >>> 28);
return 5;
}
// long
/**
* Returns the number of bytes that would be written with {@link #writeLong(long, boolean)}.
* Returns the number of bytes that would be written with {@link #writeLong(byte[], long, boolean)}.
*
* @param optimizePositive true if you want to optimize the number of bytes needed to write the length value
* @param optimizePositive
* true if you want to optimize the number of bytes needed to write the length value
*/
public final int longLength (long value, boolean optimizePositive) {
@SuppressWarnings("Duplicates")
public static
int longLength(long value, boolean optimizePositive) {
if (!optimizePositive) {
value = value << 1 ^ value >> 63;
}
@ -209,14 +208,20 @@ public class OptimizeUtilsByteArray {
return 9;
}
// long
/**
* FROM KRYO
*
* <p>
* Reads a 1-9 byte long.
*
* @param optimizePositive true if you want to optimize the number of bytes needed to write the length value
* @param optimizePositive
* true if you want to optimize the number of bytes needed to write the length value
*/
public long readLong (byte[] buffer, boolean optimizePositive) {
@SuppressWarnings({"IntegerMultiplicationImplicitCastToLong", "UnusedAssignment"})
public static
long readLong(byte[] buffer, boolean optimizePositive) {
int position = 0;
int b = buffer[position++];
long result = b & 0x7F;
@ -231,19 +236,19 @@ public class OptimizeUtilsByteArray {
result |= (b & 0x7F) << 21;
if ((b & 0x80) != 0) {
b = buffer[position++];
result |= (long)(b & 0x7F) << 28;
result |= (long) (b & 0x7F) << 28;
if ((b & 0x80) != 0) {
b = buffer[position++];
result |= (long)(b & 0x7F) << 35;
result |= (long) (b & 0x7F) << 35;
if ((b & 0x80) != 0) {
b = buffer[position++];
result |= (long)(b & 0x7F) << 42;
result |= (long) (b & 0x7F) << 42;
if ((b & 0x80) != 0) {
b = buffer[position++];
result |= (long)(b & 0x7F) << 49;
result |= (long) (b & 0x7F) << 49;
if ((b & 0x80) != 0) {
b = buffer[position++];
result |= (long)b << 56;
result |= (long) b << 56;
}
}
}
@ -258,7 +263,98 @@ public class OptimizeUtilsByteArray {
return result;
}
public boolean canReadLong (byte[] buffer) {
/**
* FROM KRYO
* <p>
* Writes a 1-9 byte long.
*
* @param optimizePositive
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9
* bytes).
*
* @return the number of bytes written.
*/
@SuppressWarnings({"Duplicates", "UnusedAssignment", "NumericCastThatLosesPrecision"})
public static
int writeLong(byte[] buffer, long value, boolean optimizePositive) {
if (!optimizePositive) {
value = value << 1 ^ value >> 63;
}
int position = 0;
if (value >>> 7 == 0) {
buffer[position++] = (byte) value;
return 1;
}
if (value >>> 14 == 0) {
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7);
return 2;
}
if (value >>> 21 == 0) {
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14);
return 3;
}
if (value >>> 28 == 0) {
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14 | 0x80);
buffer[position++] = (byte) (value >>> 21);
return 4;
}
if (value >>> 35 == 0) {
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14 | 0x80);
buffer[position++] = (byte) (value >>> 21 | 0x80);
buffer[position++] = (byte) (value >>> 28);
return 5;
}
if (value >>> 42 == 0) {
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14 | 0x80);
buffer[position++] = (byte) (value >>> 21 | 0x80);
buffer[position++] = (byte) (value >>> 28 | 0x80);
buffer[position++] = (byte) (value >>> 35);
return 6;
}
if (value >>> 49 == 0) {
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14 | 0x80);
buffer[position++] = (byte) (value >>> 21 | 0x80);
buffer[position++] = (byte) (value >>> 28 | 0x80);
buffer[position++] = (byte) (value >>> 35 | 0x80);
buffer[position++] = (byte) (value >>> 42);
return 7;
}
if (value >>> 56 == 0) {
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14 | 0x80);
buffer[position++] = (byte) (value >>> 21 | 0x80);
buffer[position++] = (byte) (value >>> 28 | 0x80);
buffer[position++] = (byte) (value >>> 35 | 0x80);
buffer[position++] = (byte) (value >>> 42 | 0x80);
buffer[position++] = (byte) (value >>> 49);
return 8;
}
buffer[position++] = (byte) (value & 0x7F | 0x80);
buffer[position++] = (byte) (value >>> 7 | 0x80);
buffer[position++] = (byte) (value >>> 14 | 0x80);
buffer[position++] = (byte) (value >>> 21 | 0x80);
buffer[position++] = (byte) (value >>> 28 | 0x80);
buffer[position++] = (byte) (value >>> 35 | 0x80);
buffer[position++] = (byte) (value >>> 42 | 0x80);
buffer[position++] = (byte) (value >>> 49 | 0x80);
buffer[position++] = (byte) (value >>> 56);
return 9;
}
public static
boolean canReadLong(byte[] buffer) {
int limit = buffer.length;
if (limit >= 9) {
@ -307,12 +403,17 @@ public class OptimizeUtilsByteArray {
if (p == limit) {
return false;
}
//noinspection SimplifiableIfStatement
if ((buffer[p++] & 0x80) == 0) {
return true;
}
if (p == limit) {
return false;
}
return true;
return p != limit;
}
private
OptimizeUtilsByteArray() {
}
}

View File

@ -36,25 +36,21 @@ package dorkbox.util.bytes;
import io.netty.buffer.ByteBuf;
public class OptimizeUtilsByteBuf {
private static final OptimizeUtilsByteBuf instance = new OptimizeUtilsByteBuf();
public static OptimizeUtilsByteBuf get() {
return instance;
}
public
class OptimizeUtilsByteBuf {
// int
/**
* FROM KRYO
*
* Returns the number of bytes that would be written with {@link #writeInt(int, boolean)}.
* <p>
* Returns the number of bytes that would be written with {@link #writeInt(ByteBuf, int, boolean)}.
*
* @param optimizePositive
* true if you want to optimize the number of bytes needed to write the length value
* true if you want to optimize the number of bytes needed to write the length value
*/
public final int intLength(int value, boolean optimizePositive) {
public static
int intLength(int value, boolean optimizePositive) {
if (!optimizePositive) {
value = value << 1 ^ value >> 31;
}
@ -75,12 +71,13 @@ public class OptimizeUtilsByteBuf {
/**
* FROM KRYO
*
* <p>
* look at buffer, and see if we can read the length of the int off of it. (from the reader index)
*
* @return 0 if we could not read anything, >0 for the number of bytes for the int on the buffer
*/
public final int canReadInt(ByteBuf buffer) {
public static
int canReadInt(ByteBuf buffer) {
int startIndex = buffer.readerIndex();
try {
int remaining = buffer.readableBytes();
@ -98,10 +95,11 @@ public class OptimizeUtilsByteBuf {
/**
* FROM KRYO
*
* <p>
* Reads an int from the buffer that was optimized.
*/
public final int readInt(ByteBuf buffer, boolean optimizePositive) {
public static
int readInt(ByteBuf buffer, boolean optimizePositive) {
int b = buffer.readByte();
int result = b & 0x7F;
if ((b & 0x80) != 0) {
@ -125,14 +123,16 @@ public class OptimizeUtilsByteBuf {
/**
* FROM KRYO
*
* <p>
* Writes the specified int to the buffer using 1 to 5 bytes, depending on the size of the number.
*
* @param optimizePositive
* true if you want to optimize the number of bytes needed to write the length value
* true if you want to optimize the number of bytes needed to write the length value
*
* @return the number of bytes written.
*/
public final int writeInt(ByteBuf buffer, int value, boolean optimizePositive) {
public static
int writeInt(ByteBuf buffer, int value, boolean optimizePositive) {
if (!optimizePositive) {
value = value << 1 ^ value >> 31;
}
@ -172,9 +172,10 @@ public class OptimizeUtilsByteBuf {
* Returns the number of bytes that would be written with {@link #writeLong(long, boolean)}.
*
* @param optimizePositive
* true if you want to optimize the number of bytes needed to write the length value
* true if you want to optimize the number of bytes needed to write the length value
*/
public final int longLength(long value, boolean optimizePositive) {
public static
int longLength(long value, boolean optimizePositive) {
if (!optimizePositive) {
value = value << 1 ^ value >> 63;
}
@ -207,13 +208,14 @@ public class OptimizeUtilsByteBuf {
/**
* FROM KRYO
*
* <p>
* Reads a 1-9 byte long.
*
* @param optimizePositive
* true if you want to optimize the number of bytes needed to write the length value
* true if you want to optimize the number of bytes needed to write the length value
*/
public final long readLong(ByteBuf buffer, boolean optimizePositive) {
public static
long readLong(ByteBuf buffer, boolean optimizePositive) {
int b = buffer.readByte();
long result = b & 0x7F;
if ((b & 0x80) != 0) {
@ -256,15 +258,17 @@ public class OptimizeUtilsByteBuf {
/**
* FROM KRYO
*
* <p>
* Writes a 1-9 byte long.
*
* @param optimizePositive
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be
* inefficient (9 bytes).
* If true, small positive numbers will be more efficient (1 byte) and small negative numbers will be inefficient (9
* bytes).
*
* @return the number of bytes written.
*/
public final int writeLong(ByteBuf buffer, long value, boolean optimizePositive) {
public static
int writeLong(ByteBuf buffer, long value, boolean optimizePositive) {
if (!optimizePositive) {
value = value << 1 ^ value >> 63;
}
@ -342,12 +346,13 @@ public class OptimizeUtilsByteBuf {
/**
* FROM KRYO
*
* <p>
* look at buffer, and see if we can read the length of the long off of it (from the reader index).
*
* @return 0 if we could not read anything, >0 for the number of bytes for the long on the buffer
*/
public final int canReadLong(ByteBuf buffer) {
public static
int canReadLong(ByteBuf buffer) {
int position = buffer.readerIndex();
try {
int remaining = buffer.readableBytes();

View File

@ -56,7 +56,6 @@ import java.util.jar.JarFile;
* <p/>
* To determine if we have hardware accelerated AES java -XX:+PrintFlagsFinal -version | grep UseAES
*/
@SuppressWarnings("unused")
public final
class Crypto {

View File

@ -29,7 +29,7 @@ import java.io.OutputStream;
/**
* AES crypto functions
*/
@SuppressWarnings({"unused", "Duplicates"})
@SuppressWarnings({"Duplicates"})
public final
class CryptoAES {
private static final int ivSize = 16;
@ -37,6 +37,8 @@ class CryptoAES {
/**
* AES encrypts data with a specified key.
*
* @param aesIV
* must be a nonce (unique value) !!
* @param logger
* may be null, if no log output is necessary
*
@ -57,12 +59,14 @@ class CryptoAES {
/**
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
* <p/>
* <p>
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
* data.
* <p/>
* <p>
* AES encrypts data with a specified key.
*
* @param aesIV
* must be a nonce (unique value) !!
* @param logger
* may be null, if no log output is necessary
*
@ -86,18 +90,15 @@ class CryptoAES {
/**
* AES encrypts data with a specified key.
*
* @param aesIV
* must be a nonce (unique value) !!
* @param logger
* may be null, if no log output is necessary
*
* @return true if successful
*/
public static
boolean encryptStreamWithIV(GCMBlockCipher aesEngine,
byte[] aesKey,
byte[] aesIV,
InputStream in,
OutputStream out,
Logger logger) {
boolean encryptStreamWithIV(GCMBlockCipher aesEngine, byte[] aesKey, byte[] aesIV, InputStream in, OutputStream out, Logger logger) {
try {
out.write(aesIV);
@ -113,12 +114,14 @@ class CryptoAES {
/**
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
* <p/>
* <p>
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
* data.
* <p/>
* <p>
* AES encrypts data with a specified key.
*
* @param aesIV
* must be a nonce (unique value) !!
* @param logger
* may be null, if no log output is necessary
*
@ -148,6 +151,8 @@ class CryptoAES {
/**
* AES encrypts data with a specified key.
*
* @param aesIV
* must be a nonce (unique value) !!
* @param logger
* may be null, if no log output is necessary
*
@ -158,30 +163,7 @@ class CryptoAES {
int length = data.length;
CipherParameters aesIVAndKey = new ParametersWithIV(new KeyParameter(aesKey), aesIV);
aesEngine.init(true, aesIVAndKey);
int minSize = aesEngine.getOutputSize(length);
byte[] outBuf = new byte[minSize];
int actualLength = aesEngine.processBytes(data, 0, length, outBuf, 0);
try {
actualLength += aesEngine.doFinal(outBuf, actualLength);
} catch (Exception e) {
if (logger != null) {
logger.error("Unable to perform AES cipher.", e);
}
return new byte[0];
}
if (outBuf.length == actualLength) {
return outBuf;
}
else {
byte[] result = new byte[actualLength];
System.arraycopy(outBuf, 0, result, 0, result.length);
return result;
}
return encrypt(aesEngine, aesIVAndKey, data, length, logger);
}
/**
@ -193,42 +175,45 @@ class CryptoAES {
* @return length of encrypted data, -1 if there was an error.
*/
public static
int encrypt(dorkbox.util.crypto.bouncycastle.GCMBlockCipher_ByteBuf aesEngine,
CipherParameters aesIVAndKey,
io.netty.buffer.ByteBuf inBuffer,
io.netty.buffer.ByteBuf outBuffer,
int length,
Logger logger) {
byte[] encrypt(GCMBlockCipher aesEngine, CipherParameters aesIVAndKey, byte[] data, int length, Logger logger) {
aesEngine.reset();
aesEngine.init(true, aesIVAndKey);
length = aesEngine.processBytes(inBuffer, outBuffer, length);
int minSize = aesEngine.getOutputSize(length);
byte[] outArray = new byte[minSize];
int actualLength = aesEngine.processBytes(data, 0, length, outArray, 0);
try {
length += aesEngine.doFinal(outBuffer);
actualLength += aesEngine.doFinal(outArray, actualLength);
} catch (Exception e) {
if (logger != null) {
logger.debug("Unable to perform AES cipher.", e);
logger.error("Unable to perform AES cipher.", e);
}
return -1;
return new byte[0];
}
// specify where the encrypted data is at
outBuffer.readerIndex(0);
outBuffer.writerIndex(length);
return length;
if (outArray.length == actualLength) {
return outArray;
}
else {
byte[] result = new byte[actualLength];
System.arraycopy(outArray, 0, result, 0, result.length);
return result;
}
}
/**
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
* <p/>
* <p>
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
* data.
* <p/>
* <p>
* AES encrypts data with a specified key.
*
* @param aesIV
* must be a nonce (unique value) !!
* @param logger
* may be null, if no log output is necessary
*
@ -268,12 +253,14 @@ class CryptoAES {
/**
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
* <p/>
* <p>
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
* data.
* <p/>
* <p>
* AES encrypt from one stream to another.
*
* @param aesIV
* must be a nonce (unique value) !!
* @param logger
* may be null, if no log output is necessary
*
@ -316,6 +303,8 @@ class CryptoAES {
*
* @param logger
* may be null, if no log output is necessary
* @param aesIV
* must be a nonce (unique value) !!
*
* @return true if successful
*/
@ -352,7 +341,7 @@ class CryptoAES {
}
/**
* AES decrypt (if the aes IV is included in the data)
* AES decrypt (if the aes IV is included in the data). IV must be a nonce (unique value) !!
*
* @param logger
* may be null, if no log output is necessary
@ -372,11 +361,11 @@ class CryptoAES {
/**
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
* <p/>
* <p>
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
* data.
* <p/>
* AES decrypt (if the aes IV is included in the data)
* <p>
* AES decrypt (if the aes IV is included in the data). IV must be a nonce (unique value)
*
* @param logger
* may be null, if no log output is necessary
@ -396,7 +385,7 @@ class CryptoAES {
}
/**
* AES decrypt (if the aes IV is included in the data)
* AES decrypt (if the aes IV is included in the data. IV must be a nonce (unique value)
*
* @param logger
* may be null, if no log output is necessary
@ -420,11 +409,11 @@ class CryptoAES {
/**
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
* <p/>
* <p>
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
* data.
* <p/>
* AES decrypt (if the aes IV is included in the data)
* <p>
* AES decrypt (if the aes IV is included in the data). IV must be a nonce (unique value)
*
* @param logger
* may be null, if no log output is necessary
@ -450,6 +439,8 @@ class CryptoAES {
/**
* AES decrypt (if we already know the aes IV -- and it's NOT included in the data)
*
* @param aesIV
* must be a nonce (unique value) !!
* @param logger
* may be null, if no log output is necessary
*
@ -485,52 +476,16 @@ class CryptoAES {
}
}
/**
* AES decrypt (if we already know the aes IV -- and it's NOT included in the data)
*
* @param logger
* may be null, if no log output is necessary
*
* @return length of decrypted data, -1 if there was an error.
*/
public static
int decrypt(dorkbox.util.crypto.bouncycastle.GCMBlockCipher_ByteBuf aesEngine,
ParametersWithIV aesIVAndKey,
io.netty.buffer.ByteBuf bufferWithData,
io.netty.buffer.ByteBuf bufferTempData,
int length,
Logger logger) {
aesEngine.reset();
aesEngine.init(false, aesIVAndKey);
// ignore the start position
// we also do NOT want to have the same start position for the altBuffer, since it could then grow larger than the buffer capacity.
length = aesEngine.processBytes(bufferWithData, bufferTempData, length);
try {
length += aesEngine.doFinal(bufferTempData);
} catch (Exception e) {
if (logger != null) {
logger.debug("Unable to perform AES cipher.", e);
}
return -1;
}
bufferTempData.readerIndex(0);
bufferTempData.writerIndex(length);
return length;
}
/**
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
* <p/>
* <p>
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
* data.
* <p/>
* <p>
* AES decrypt (if we already know the aes IV -- and it's NOT included in the data)
*
* @param aesIV
* must be a nonce (unique value) !!
* @param logger
* may be null, if no log output is necessary
*
@ -572,6 +527,8 @@ class CryptoAES {
/**
* AES decrypt from one stream to another.
*
* @param aesIV
* must be a nonce (unique value) !!
* @param logger
* may be null, if no log output is necessary
*
@ -610,12 +567,14 @@ class CryptoAES {
/**
* <b>CONVENIENCE METHOD ONLY - DO NOT USE UNLESS YOU HAVE TO</b>
* <p/>
* <p>
* Use GCM instead, as it's an authenticated cipher (and "regular" AES is not). This prevents tampering with the blocks of encrypted
* data.
* <p/>
* <p>
* AES decrypt from one stream to another.
*
* @param aesIV
* must be a nonce (unique value) !!
* @param logger
* may be null, if no log output is necessary
*

View File

@ -33,7 +33,6 @@ import java.security.SecureRandom;
/**
* This is here just for keeping track of how this is done. This should NOT be used, and instead use ECC crypto.
*/
@SuppressWarnings("unused")
@Deprecated
public final
class CryptoRSA {

View File

@ -21,7 +21,6 @@ import java.util.Arrays;
/**
* An implementation of the <a href="http://www.tarsnap.com/scrypt/scrypt.pdf"/>scrypt</a> key derivation function.
*/
@SuppressWarnings("unused")
public final
class CryptoSCrypt {
/**

View File

@ -15,48 +15,10 @@
*/
package dorkbox.util.crypto;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.PublicKey;
import java.security.Security;
import java.security.SignatureException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.security.spec.DSAParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateCrtKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.Date;
import java.util.Enumeration;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Encoding;
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.ASN1Set;
import org.bouncycastle.asn1.ASN1TaggedObject;
import org.bouncycastle.asn1.BERSet;
import org.bouncycastle.asn1.DERBMPString;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.DERSet;
import dorkbox.util.Base64Fast;
import dorkbox.util.crypto.signers.BcECDSAContentSignerBuilder;
import dorkbox.util.crypto.signers.BcECDSAContentVerifierProviderBuilder;
import org.bouncycastle.asn1.*;
import org.bouncycastle.asn1.cms.CMSObjectIdentifiers;
import org.bouncycastle.asn1.cms.ContentInfo;
import org.bouncycastle.asn1.cms.IssuerAndSerialNumber;
@ -118,9 +80,33 @@ import org.bouncycastle.operator.bc.BcRSAContentVerifierProviderBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import dorkbox.util.Base64Fast;
import dorkbox.util.crypto.signers.BcECDSAContentSignerBuilder;
import dorkbox.util.crypto.signers.BcECDSAContentVerifierProviderBuilder;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.PublicKey;
import java.security.Security;
import java.security.SignatureException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.security.spec.DSAParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateCrtKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.Date;
import java.util.Enumeration;
public class CryptoX509 {
@ -139,7 +125,6 @@ public class CryptoX509 {
/**
* @return true if saving the x509 certificate to a PEM format file was successful
*/
@SuppressWarnings("unused")
public static boolean convertToPemFile(X509Certificate x509cert, String fileName) {
boolean failed = false;
Writer output = null;
@ -1269,7 +1254,6 @@ public class CryptoX509 {
*
* Code is present but commented out, as it was a PITA to figure it out, as documentation is lacking....
*/
@SuppressWarnings("unused")
public static void loadKeystore(String keystoreLocation, String alias, char[] passwd, char[] keypasswd) {
// FileInputStream fileIn = new FileInputStream(keystoreLocation);
// KeyStore keyStore = KeyStore.getInstance("JKS");

View File

@ -1,455 +0,0 @@
/*
* Copyright 2010 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (c) 2000 - 2015 The Legion of the Bouncy Castle Inc.
* (http://www.bouncycastle.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package dorkbox.util.crypto.bouncycastle;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.modes.AEADBlockCipher;
import org.bouncycastle.crypto.params.AEADParameters;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Pack;
/**
* Implements the Galois/Counter mode (GCM) detailed in
* NIST Special Publication 800-38D.
*/
public class GCMBlockCipher_ByteBuf
implements AEADBlockCipher
{
private static final int BLOCK_SIZE = 16;
private static final byte[] ZEROES = new byte[BLOCK_SIZE];
// not final due to a compiler bug
private BlockCipher cipher;
private Tables8kGCMMultiplier_ByteBuf multiplier;
// These fields are set by init and not modified by processing
private boolean forEncryption;
private int macSize;
private byte[] nonce;
private byte[] A;
private byte[] H;
private ByteBuf initS;
private byte[] J0;
// These fields are modified during processing
private byte[] bufBlock;
private byte[] macBlock;
private ByteBuf S;
private byte[] counter;
private int bufOff;
private long totalLength;
public GCMBlockCipher_ByteBuf(BlockCipher c)
{
if (c.getBlockSize() != BLOCK_SIZE)
{
throw new IllegalArgumentException(
"cipher required with a block size of " + BLOCK_SIZE + ".");
}
this.cipher = c;
this.multiplier = new Tables8kGCMMultiplier_ByteBuf();
}
@Override
public BlockCipher getUnderlyingCipher()
{
return this.cipher;
}
@Override
public String getAlgorithmName()
{
return this.cipher.getAlgorithmName() + "/GCM";
}
@Override
public void init(boolean forEncryption, CipherParameters params)
throws IllegalArgumentException
{
this.forEncryption = forEncryption;
this.macBlock = null;
KeyParameter keyParam;
if (params instanceof AEADParameters)
{
AEADParameters param = (AEADParameters)params;
this.nonce = param.getNonce();
this.A = param.getAssociatedText();
int macSizeBits = param.getMacSize();
if (macSizeBits < 96 || macSizeBits > 128 || macSizeBits % 8 != 0)
{
throw new IllegalArgumentException("Invalid value for MAC size: " + macSizeBits);
}
this.macSize = macSizeBits / 8;
keyParam = param.getKey();
}
else if (params instanceof ParametersWithIV)
{
ParametersWithIV param = (ParametersWithIV)params;
this.nonce = param.getIV();
this.A = null;
this.macSize = 16;
keyParam = (KeyParameter)param.getParameters();
}
else
{
throw new IllegalArgumentException("invalid parameters passed to GCM");
}
int bufLength = forEncryption ? BLOCK_SIZE : BLOCK_SIZE + this.macSize;
this.bufBlock = new byte[bufLength];
if (this.nonce == null || this.nonce.length < 1)
{
throw new IllegalArgumentException("IV must be at least 1 byte");
}
if (this.A == null)
{
// Avoid lots of null checks
this.A = new byte[0];
}
// Cipher always used in forward mode
// if keyParam is null we're reusing the last key.
if (keyParam != null)
{
this.cipher.init(true, keyParam);
}
// TODO This should be configurable by init parameters
// (but must be 16 if nonce length not 12) (BLOCK_SIZE?)
// this.tagLength = 16;
this.H = new byte[BLOCK_SIZE];
this.cipher.processBlock(ZEROES, 0, this.H, 0);
this.multiplier.init(this.H);
this.initS = Unpooled.wrappedBuffer(gHASH(this.A));
if (this.nonce.length == 12)
{
this.J0 = new byte[16];
System.arraycopy(this.nonce, 0, this.J0, 0, this.nonce.length);
this.J0[15] = 0x01;
}
else
{
this.J0 = gHASH(this.nonce);
byte[] X = new byte[16];
packLength((long)this.nonce.length * 8, X, 8);
xor(this.J0, X);
this.multiplier.multiplyH(this.J0);
}
this.S = this.initS.copy();
this.counter = Arrays.clone(this.J0);
this.bufOff = 0;
this.totalLength = 0;
}
@Override
public byte[] getMac() {
return Arrays.clone(this.macBlock);
}
@Override
public int getOutputSize(int len) {
if (this.forEncryption) {
return len + this.bufOff + this.macSize;
}
return len + this.bufOff - this.macSize;
}
@Override
public int getUpdateOutputSize(int len) {
return (len + this.bufOff) / BLOCK_SIZE * BLOCK_SIZE;
}
// MODIFIED
@Override
public int processByte(byte in, byte[] out, int outOff)
throws DataLengthException
{
// DO NOTHING
return 0;
}
// MODIFIED
public int processBytes(ByteBuf in, ByteBuf out, int len)
throws DataLengthException
{
out.clear();
int didRead = 0;
int resultLen = 0;
while (didRead < len) {
int buffOffRead = this.bufOff + len - didRead;
if (buffOffRead >= this.bufBlock.length) {
int amtToRead = this.bufBlock.length - this.bufOff;
didRead += amtToRead;
in.readBytes(this.bufBlock, this.bufOff, amtToRead);
gCTRBlock(this.bufBlock, BLOCK_SIZE, out);
if (!this.forEncryption) {
System.arraycopy(this.bufBlock, BLOCK_SIZE, this.bufBlock, 0, this.macSize);
}
resultLen += BLOCK_SIZE;
this.bufOff = this.bufBlock.length - BLOCK_SIZE;
} else {
int read = len - didRead;
in.readBytes(this.bufBlock, this.bufOff, read);
this.bufOff += read;
break;
}
}
return resultLen;
}
public int doFinal(ByteBuf out) throws IllegalStateException, InvalidCipherTextException {
int extra = this.bufOff;
if (!this.forEncryption) {
if (extra < this.macSize) {
throw new InvalidCipherTextException("data too short");
}
extra -= this.macSize;
}
if (extra > 0) {
byte[] tmp = new byte[BLOCK_SIZE];
System.arraycopy(this.bufBlock, 0, tmp, 0, extra);
gCTRBlock(tmp, extra, out);
}
// Final gHASH
byte[] X = new byte[16];
packLength((long) this.A.length * 8, X, 0);
packLength(this.totalLength * 8, X, 8);
xor(this.S, X);
this.multiplier.multiplyH(this.S);
// TODO Fix this if tagLength becomes configurable
// T = MSBt(GCTRk(J0,S))
byte[] tag = new byte[BLOCK_SIZE];
this.cipher.processBlock(this.J0, 0, tag, 0);
xor(tag, this.S);
int resultLen = extra;
// We place into macBlock our calculated value for T
this.macBlock = new byte[this.macSize];
System.arraycopy(tag, 0, this.macBlock, 0, this.macSize);
if (this.forEncryption) {
// Append T to the message
out.writeBytes(this.macBlock, 0, this.macSize);
resultLen += this.macSize;
} else {
// Retrieve the T value from the message and compare to calculated
// one
byte[] msgMac = new byte[this.macSize];
System.arraycopy(this.bufBlock, extra, msgMac, 0, this.macSize);
if (!Arrays.constantTimeAreEqual(this.macBlock, msgMac)) {
throw new InvalidCipherTextException("mac check in GCM failed");
}
}
reset(false);
return resultLen;
}
@Override
public void reset() {
reset(true);
}
private void reset(boolean clearMac) {
this.S = this.initS != null ? this.initS.copy() : Unpooled.buffer();
this.counter = Arrays.clone(this.J0);
this.bufOff = 0;
this.totalLength = 0;
if (this.bufBlock != null) {
Arrays.fill(this.bufBlock, (byte) 0);
}
if (clearMac) {
this.macBlock = null;
}
this.cipher.reset();
}
// MODIFIED
private void gCTRBlock(byte[] buf, int bufCount, ByteBuf out) {
// inc(counter);
for (int i = 15; i >= 12; --i) {
byte b = (byte) (this.counter[i] + 1 & 0xff);
this.counter[i] = b;
if (b != 0) {
break;
}
}
byte[] tmp = new byte[BLOCK_SIZE];
this.cipher.processBlock(this.counter, 0, tmp, 0);
byte[] hashBytes;
if (this.forEncryption) {
System.arraycopy(ZEROES, bufCount, tmp, bufCount, BLOCK_SIZE - bufCount);
hashBytes = tmp;
} else {
hashBytes = buf;
}
for (int i = bufCount - 1; i >= 0; --i) {
tmp[i] ^= buf[i];
}
out.writeBytes(tmp, 0, bufCount);
// gHASHBlock(hashBytes);
xor(this.S, hashBytes);
this.multiplier.multiplyH(this.S);
this.totalLength += bufCount;
}
private byte[] gHASH(byte[] b)
{
byte[] Y = new byte[16];
for (int pos = 0; pos < b.length; pos += 16)
{
byte[] X = new byte[16];
int num = Math.min(b.length - pos, 16);
System.arraycopy(b, pos, X, 0, num);
xor(Y, X);
this.multiplier.multiplyH(Y);
}
return Y;
}
// private void gHASHBlock(byte[] block)
// {
// xor(S, block);
// multiplier.multiplyH(S);
// }
// private static void inc(byte[] block)
// {
// for (int i = 15; i >= 12; --i)
// {
// byte b = (byte)((block[i] + 1) & 0xff);
// block[i] = b;
//
// if (b != 0)
// {
// break;
// }
// }
// }
private static void xor(ByteBuf block, byte[] val) {
for (int i = 15; i >= 0; --i) {
byte b = (byte) (block.getByte(i) ^ val[i]);
block.setByte(i, b);
}
}
private static void xor(byte[] block, ByteBuf val) {
for (int i = 15; i >= 0; --i) {
block[i] ^= val.getByte(i);
}
}
private static void xor(byte[] block, byte[] val) {
for (int i = 15; i >= 0; --i) {
block[i] ^= val[i];
}
}
private static void packLength(long count, byte[] bs, int off) {
Pack.intToBigEndian((int)(count >>> 32), bs, off);
Pack.intToBigEndian((int)count, bs, off + 4);
}
// MODIFIED
@Override
public int processBytes(byte[] in, int inOff, int len, byte[] out, int outOff) throws DataLengthException {
// DO NOTHING
return 0;
}
// MODIFIED
@Override
public int doFinal(byte[] out, int outOff) throws IllegalStateException, InvalidCipherTextException {
// DO NOTHING
return 0;
}
@Override
public void processAADByte(byte arg0) {
}
@Override
public void processAADBytes(byte[] arg0, int arg1, int arg2) {
}
}

View File

@ -1,191 +0,0 @@
/*
* Copyright 2010 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (c) 2000 - 2015 The Legion of the Bouncy Castle Inc.
* (http://www.bouncycastle.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package dorkbox.util.crypto.bouncycastle;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Pack;
public abstract class GCMUtil_ByteBuf
{
static byte[] oneAsBytes()
{
byte[] tmp = new byte[16];
tmp[0] = (byte)0x80;
return tmp;
}
static int[] oneAsInts()
{
int[] tmp = new int[4];
tmp[0] = 0x80000000;
return tmp;
}
static int[] asInts(byte[] bs)
{
int[] us = new int[4];
us[0] = Pack.bigEndianToInt(bs, 0);
us[1] = Pack.bigEndianToInt(bs, 4);
us[2] = Pack.bigEndianToInt(bs, 8);
us[3] = Pack.bigEndianToInt(bs, 12);
return us;
}
static void multiply(byte[] block, byte[] val)
{
byte[] tmp = Arrays.clone(block);
byte[] c = new byte[16];
for (int i = 0; i < 16; ++i)
{
byte bits = val[i];
for (int j = 7; j >= 0; --j)
{
if ((bits & 1 << j) != 0)
{
xor(c, tmp);
}
boolean lsb = (tmp[15] & 1) != 0;
shiftRight(tmp);
if (lsb)
{
// R = new byte[]{ 0xe1, ... };
// GCMUtil.xor(v, R);
tmp[0] ^= (byte)0xe1;
}
}
}
System.arraycopy(c, 0, block, 0, 16);
}
// P is the value with only bit i=1 set
static void multiplyP(int[] x)
{
boolean lsb = (x[3] & 1) != 0;
shiftRight(x);
if (lsb)
{
// R = new int[]{ 0xe1000000, 0, 0, 0 };
// xor(v, R);
x[0] ^= 0xe1000000;
}
}
static void multiplyP8(int[] x)
{
// for (int i = 8; i != 0; --i)
// {
// multiplyP(x);
// }
int lsw = x[3];
shiftRightN(x, 8);
for (int i = 7; i >= 0; --i)
{
if ((lsw & 1 << i) != 0)
{
x[0] ^= 0xe1000000 >>> 7 - i;
}
}
}
static void shiftRight(byte[] block)
{
int i = 0;
int bit = 0;
for (;;)
{
int b = block[i] & 0xff;
block[i] = (byte) (b >>> 1 | bit);
if (++i == 16)
{
break;
}
bit = (b & 1) << 7;
}
}
static void shiftRight(int[] block)
{
int i = 0;
int bit = 0;
for (;;)
{
int b = block[i];
block[i] = b >>> 1 | bit;
if (++i == 4)
{
break;
}
bit = b << 31;
}
}
static void shiftRightN(int[] block, int n)
{
int i = 0;
int bits = 0;
for (;;)
{
int b = block[i];
block[i] = b >>> n | bits;
if (++i == 4)
{
break;
}
bits = b << 32 - n;
}
}
static void xor(byte[] block, byte[] val)
{
for (int i = 15; i >= 0; --i)
{
block[i] ^= val[i];
}
}
static void xor(int[] block, int[] val)
{
for (int i = 3; i >= 0; --i)
{
block[i] ^= val[i];
}
}
}

View File

@ -1,174 +0,0 @@
/*
* Copyright 2010 dorkbox, llc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (c) 2000 - 2015 The Legion of the Bouncy Castle Inc.
* (http://www.bouncycastle.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package dorkbox.util.crypto.bouncycastle;
import io.netty.buffer.ByteBuf;
import org.bouncycastle.util.Pack;
public class Tables8kGCMMultiplier_ByteBuf
{
private final int[][][] M = new int[32][16][];
public void init(byte[] H)
{
this.M[0][0] = new int[4];
this.M[1][0] = new int[4];
this.M[1][8] = GCMUtil_ByteBuf.asInts(H);
for (int j = 4; j >= 1; j >>= 1)
{
int[] tmp = new int[4];
System.arraycopy(this.M[1][j + j], 0, tmp, 0, 4);
GCMUtil_ByteBuf.multiplyP(tmp);
this.M[1][j] = tmp;
}
{
int[] tmp = new int[4];
System.arraycopy(this.M[1][1], 0, tmp, 0, 4);
GCMUtil_ByteBuf.multiplyP(tmp);
this.M[0][8] = tmp;
}
for (int j = 4; j >= 1; j >>= 1)
{
int[] tmp = new int[4];
System.arraycopy(this.M[0][j + j], 0, tmp, 0, 4);
GCMUtil_ByteBuf.multiplyP(tmp);
this.M[0][j] = tmp;
}
int i = 0;
for (;;)
{
for (int j = 2; j < 16; j += j)
{
for (int k = 1; k < j; ++k)
{
int[] tmp = new int[4];
System.arraycopy(this.M[i][j], 0, tmp, 0, 4);
GCMUtil_ByteBuf.xor(tmp, this.M[i][k]);
this.M[i][j + k] = tmp;
}
}
if (++i == 32)
{
return;
}
if (i > 1)
{
this.M[i][0] = new int[4];
for(int j = 8; j > 0; j >>= 1)
{
int[] tmp = new int[4];
System.arraycopy(this.M[i - 2][j], 0, tmp, 0, 4);
GCMUtil_ByteBuf.multiplyP8(tmp);
this.M[i][j] = tmp;
}
}
}
}
public void multiplyH(byte[] x)
{
// assert x.Length == 16;
int[] z = new int[4];
for (int i = 15; i >= 0; --i)
{
// GCMUtil.xor(z, M[i + i][x[i] & 0x0f]);
int[] m = this.M[i + i][x[i] & 0x0f];
z[0] ^= m[0];
z[1] ^= m[1];
z[2] ^= m[2];
z[3] ^= m[3];
// GCMUtil.xor(z, M[i + i + 1][(x[i] & 0xf0) >>> 4]);
m = this.M[i + i + 1][(x[i] & 0xf0) >>> 4];
z[0] ^= m[0];
z[1] ^= m[1];
z[2] ^= m[2];
z[3] ^= m[3];
}
Pack.intToBigEndian(z, x, 0);
}
public void multiplyH(ByteBuf x)
{
// assert x.Length == 16;
int[] z = new int[4];
for (int i = 15; i >= 0; --i)
{
// GCMUtil.xor(z, M[i + i][x[i] & 0x0f]);
int[] m = this.M[i + i][x.getByte(i) & 0x0f];
z[0] ^= m[0];
z[1] ^= m[1];
z[2] ^= m[2];
z[3] ^= m[3];
// GCMUtil.xor(z, M[i + i + 1][(x[i] & 0xf0) >>> 4]);
m = this.M[i + i + 1][(x.getByte(i) & 0xf0) >>> 4];
z[0] ^= m[0];
z[1] ^= m[1];
z[2] ^= m[2];
z[3] ^= m[3];
}
intToBigEndian(z, x, 0);
}
public static void intToBigEndian(int[] ns, ByteBuf bs, int off) {
for (int i = 0; i < ns.length; ++i) {
intToBigEndian(ns[i], bs, off);
off += 4;
}
}
public static void intToBigEndian(int n, ByteBuf bs, int off) {
bs.setByte(off, (byte) (n >>> 24));
bs.setByte(++off, (byte) (n >>> 16));
bs.setByte(++off, (byte) (n >>> 8));
bs.setByte(++off, (byte) n);
}
}

View File

@ -34,7 +34,6 @@ import java.io.IOException;
public
class BcECDSAContentVerifierProviderBuilder extends BcContentVerifierProviderBuilder {
@SuppressWarnings("unused")
public
BcECDSAContentVerifierProviderBuilder(DigestAlgorithmIdentifierFinder digestAlgorithmFinder) {
}

View File

@ -21,7 +21,6 @@ import java.awt.*;
import java.io.*;
import java.util.Properties;
@SuppressWarnings("unused")
public
class PropertiesProvider {

View File

@ -33,7 +33,6 @@ import java.util.concurrent.locks.ReentrantLock;
* <p/>
* Be wary of opening the database file in different JVM instances. Even with file-locks, you can corrupt the data.
*/
@SuppressWarnings("unused")
class DiskStorage implements Storage {
private final DelayTimer timer;
private final ByteArrayWrapper defaultKey;

View File

@ -23,13 +23,10 @@ import dorkbox.util.bytes.ByteArrayWrapper;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.lang.ref.WeakReference;
import java.nio.channels.FileLock;
@SuppressWarnings("unused")
public
class Metadata {
// The length of a key in the index.

View File

@ -23,7 +23,6 @@ import java.io.IOException;
/**
*
*/
@SuppressWarnings("unused")
public
interface Storage {
/**
@ -87,7 +86,6 @@ interface Storage {
* @param key The key used to check if data already exists.
* @param data This is the default value, and if there is no value with the key in the DB this default value will be saved.
*/
@SuppressWarnings("unchecked")
<T> T getAndPut(ByteArrayWrapper key, T data) throws IOException;
/**

View File

@ -25,7 +25,6 @@ import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@ -46,7 +45,6 @@ import java.util.concurrent.locks.ReentrantLock;
// Also, file locks on linux are ADVISORY. if an app doesn't care about locks, then it can do stuff -- even if locked by another app
@SuppressWarnings("unused")
class StorageBase {
private final Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());

View File

@ -45,7 +45,6 @@ import static org.junit.Assert.*;
public class UByteTest {
@SuppressWarnings("deprecation")
@Test
public void testValueOfByte() {
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
@ -53,7 +52,6 @@ public class UByteTest {
}
}
@SuppressWarnings("deprecation")
@Test
public void testValueOfByteCaching() {
for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
@ -63,7 +61,6 @@ public class UByteTest {
}
}
@SuppressWarnings("deprecation")
@Test
public void testValueOfShort() {
for (int i = UByte.MIN_VALUE; i <= UByte.MAX_VALUE; i++) {
@ -71,7 +68,6 @@ public class UByteTest {
}
}
@SuppressWarnings("deprecation")
@Test
public void testValueOfShortInvalid() {
try {
@ -86,7 +82,6 @@ public class UByteTest {
catch (NumberFormatException e) {}
}
@SuppressWarnings("deprecation")
@Test
public void testValueOfInt() {
for (int i = UByte.MIN_VALUE; i <= UByte.MAX_VALUE; i++) {
@ -94,7 +89,6 @@ public class UByteTest {
}
}
@SuppressWarnings("deprecation")
@Test
public void testValueOfIntInvalid() {
try {
@ -109,7 +103,6 @@ public class UByteTest {
catch (NumberFormatException e) {}
}
@SuppressWarnings("deprecation")
@Test
public void testValueOfLong() {
for (int i = UByte.MIN_VALUE; i <= UByte.MAX_VALUE; i++) {
@ -117,7 +110,6 @@ public class UByteTest {
}
}
@SuppressWarnings("deprecation")
@Test
public void testValueOfLongInvalid() {
try {
@ -132,7 +124,6 @@ public class UByteTest {
catch (NumberFormatException e) {}
}
@SuppressWarnings("deprecation")
@Test
public void testSerializeDeserialize() throws ClassNotFoundException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();

View File

@ -48,7 +48,6 @@ class UIntegerTest {
private static final int CACHE_SIZE = 256;
private static final int NEAR_MISS_OFFSET = 4;
@SuppressWarnings("deprecation")
@Test
public
void testValueOfLong() {
@ -57,7 +56,6 @@ class UIntegerTest {
}
}
@SuppressWarnings("deprecation")
@Test
public
void testValueOfLongCachingShift() {
@ -68,7 +66,6 @@ class UIntegerTest {
}
}
@SuppressWarnings("deprecation")
@Test
public
void testValueOfLongCachingNear() {
@ -79,7 +76,6 @@ class UIntegerTest {
}
}
@SuppressWarnings("deprecation")
@Test
public
void testValueOfLongNoCachingShift() {
@ -90,7 +86,6 @@ class UIntegerTest {
}
}
@SuppressWarnings("deprecation")
@Test
public
void testValueOfLongNoCachingNear() {
@ -101,7 +96,6 @@ class UIntegerTest {
}
}
@SuppressWarnings("deprecation")
@Test
public
void testValueOfLongInvalid() {
@ -117,7 +111,6 @@ class UIntegerTest {
}
}
@SuppressWarnings("deprecation")
@Test
public
void testSerializeDeserialize() throws ClassNotFoundException, IOException {

View File

@ -169,7 +169,6 @@ public class UNumberTest {
testCastable(ULong.MAX_VALUE, ulong(ULong.MAX_VALUE));
}
@SuppressWarnings("deprecation")
@Test
public void testObjectMethods() {
assertEquals(ubyte((byte) 0), ubyte((byte) 0));
@ -226,7 +225,7 @@ public class UNumberTest {
// Test utility methods
@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
@SuppressWarnings({ "rawtypes", "unchecked"})
private void testComparable(List<String> strings, UNumber... numbers) {
List<UNumber> list = new ArrayList<UNumber>(asList(numbers));
Collections.sort((List) list);