Skip to content
Permalink
Browse files
Merge pull request #115 from arturobernalg/feature/CRYPTO-155
[CRYPTO-155] - Minor improvement
  • Loading branch information
garydgregory committed Dec 11, 2020
2 parents 3c9c78d + ca96d52 commit f62fd27134471625b2a180189a489e4b7f6c617f
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 29 deletions.
@@ -77,12 +77,13 @@ public int update(final byte[] input, final int inputOffset, final int inputLen,
public int doFinal(final byte[] input, final int inputOffset, final int inputLen, final byte[] output, final int outputOffset)
throws ShortBufferException, IllegalBlockSizeException, BadPaddingException {
checkState();
final int outputLength = output.length;

int len = OpenSslNative.updateByteArray(context, input, inputOffset,
inputLen, output, outputOffset, output.length - outputOffset);
inputLen, output, outputOffset, outputLength - outputOffset);

len += OpenSslNative.doFinalByteArray(context, output, outputOffset + len,
output.length - outputOffset - len);
outputLength - outputOffset - len);

return len;
}
@@ -130,6 +130,7 @@ public int doFinal(final byte[] input, final int inputOffset, final int inputLen

processAAD();

final int outputLength = output.length;
int len;
if (this.cipherMode == OpenSsl.DECRYPT_MODE) {
// if GCM-DECRYPT, we have to handle the buffered input
@@ -153,7 +154,7 @@ public int doFinal(final byte[] input, final int inputOffset, final int inputLen

final int inputDataLen = inputLenFinal - getTagLen();
len = OpenSslNative.updateByteArray(context, inputFinal, inputOffsetFinal,
inputDataLen, output, outputOffset, output.length - outputOffset);
inputDataLen, output, outputOffset, outputLength - outputOffset);

// set tag to EVP_Cipher for integrity verification in doFinal
final ByteBuffer tag = ByteBuffer.allocate(getTagLen());
@@ -162,18 +163,18 @@ public int doFinal(final byte[] input, final int inputOffset, final int inputLen
evpCipherCtxCtrl(context, OpenSslEvpCtrlValues.AEAD_SET_TAG.getValue(), getTagLen(), tag);
} else {
len = OpenSslNative.updateByteArray(context, input, inputOffset,
inputLen, output, outputOffset, output.length - outputOffset);
inputLen, output, outputOffset, outputLength - outputOffset);
}

len += OpenSslNative.doFinalByteArray(context, output, outputOffset + len,
output.length - outputOffset - len);
outputLength - outputOffset - len);

// Keep the similar behavior as JCE, append the tag to end of output
if (this.cipherMode == OpenSsl.ENCRYPT_MODE) {
ByteBuffer tag;
tag = ByteBuffer.allocate(getTagLen());
evpCipherCtxCtrl(context, OpenSslEvpCtrlValues.AEAD_GET_TAG.getValue(), getTagLen(), tag);
tag.get(output, output.length - getTagLen(), getTagLen());
tag.get(output, outputLength - getTagLen(), getTagLen());
len += getTagLen();
}

@@ -108,9 +108,10 @@ public void init(final int mode, final Key key, final AlgorithmParameterSpec par
if ((algMode == AlgorithmMode.AES_CBC || algMode == AlgorithmMode.AES_CTR) && iv.length != IV_LENGTH) {
throw new InvalidAlgorithmParameterException("Wrong IV length: must be 16 bytes long");
}
final int keyEncodedLength = key.getEncoded().length;

if (algMode == AlgorithmMode.AES_CBC) {
switch (key.getEncoded().length) {
switch (keyEncodedLength) {
case 16:
algo = OpenSslNativeJna.EVP_aes_128_cbc();
break;
@@ -121,11 +122,11 @@ public void init(final int mode, final Key key, final AlgorithmParameterSpec par
algo = OpenSslNativeJna.EVP_aes_256_cbc();
break;
default:
throw new InvalidKeyException("keysize unsupported (" + key.getEncoded().length + ")");
throw new InvalidKeyException("keysize unsupported (" + keyEncodedLength + ")");
}

} else {
switch (key.getEncoded().length) {
switch (keyEncodedLength) {
case 16:
algo = OpenSslNativeJna.EVP_aes_128_ctr();
break;
@@ -136,7 +137,7 @@ public void init(final int mode, final Key key, final AlgorithmParameterSpec par
algo = OpenSslNativeJna.EVP_aes_256_ctr();
break;
default:
throw new InvalidKeyException("keysize unsupported (" + key.getEncoded().length + ")");
throw new InvalidKeyException("keysize unsupported (" + keyEncodedLength + ")");
}
}

@@ -50,7 +50,7 @@ class OpenSslJnaCryptoRandom extends Random implements CryptoRandom {

private static final long serialVersionUID = -7128193502768749585L;
private final boolean rdrandEnabled;
private transient PointerByReference rdrandEngine;
private final transient PointerByReference rdrandEngine;

/**
* Constructs a {@link OpenSslJnaCryptoRandom}.
@@ -108,11 +108,12 @@ public void nextBytes(final byte[] bytes) {
throw new IllegalStateException("rdrand should be used but default is detected");
}

final ByteBuffer buf = ByteBuffer.allocateDirect(bytes.length);
final int retVal = OpenSslNativeJna.RAND_bytes(buf, bytes.length);
final int byteLength = bytes.length;
final ByteBuffer buf = ByteBuffer.allocateDirect(byteLength);
final int retVal = OpenSslNativeJna.RAND_bytes(buf, byteLength);
throwOnError(retVal);
buf.rewind();
buf.get(bytes,0, bytes.length);
buf.get(bytes,0, byteLength);
}
}

@@ -222,7 +222,8 @@ public void write(final int b) throws IOException {
public void write(final byte[] array, int off, int len) throws IOException {
checkStream();
Objects.requireNonNull(array, "array");
if (off < 0 || len < 0 || off > array.length || len > array.length - off) {
final int arrayLength = array.length;
if (off < 0 || len < 0 || off > arrayLength || len > arrayLength - off) {
throw new IndexOutOfBoundsException();
}

@@ -639,10 +639,11 @@ protected void decryptBuffer(final ByteBuffer out) throws IOException {
* @param IV the IV for input stream position
*/
static void calculateIV(final byte[] initIV, long counter, final byte[] IV) {
int i = IV.length; // IV length

Utils.checkArgument(initIV.length == CryptoCipherFactory.AES_BLOCK_SIZE);
Utils.checkArgument(IV.length == CryptoCipherFactory.AES_BLOCK_SIZE);
Utils.checkArgument(i == CryptoCipherFactory.AES_BLOCK_SIZE);

int i = IV.length; // IV length
int j = 0; // counter bytes index
int sum = 0;
while (i-- > 0) {
@@ -73,12 +73,13 @@ private static abstract class NegativeCacheSentinel {
public static <T> T newInstance(final Class<T> klass, final Object... args) {
try {
Constructor<T> ctor;
final int argsLength = args.length;

if (args.length == 0) {
if (argsLength == 0) {
ctor = klass.getDeclaredConstructor();
} else {
final Class<?>[] argClses = new Class[args.length];
for (int i = 0; i < args.length; i++) {
final Class<?>[] argClses = new Class[argsLength];
for (int i = 0; i < argsLength; i++) {
argClses[i] = args[i].getClass();
}
ctor = klass.getDeclaredConstructor(argClses);
@@ -116,11 +117,7 @@ private static Class<?> getClassByNameOrNull(final String name) {
Map<String, WeakReference<Class<?>>> map;

synchronized (CACHE_CLASSES) {
map = CACHE_CLASSES.get(CLASSLOADER);
if (map == null) {
map = Collections.synchronizedMap(new WeakHashMap<String, WeakReference<Class<?>>>());
CACHE_CLASSES.put(CLASSLOADER, map);
}
map = CACHE_CLASSES.computeIfAbsent(CLASSLOADER, k -> Collections.synchronizedMap(new WeakHashMap<String, WeakReference<Class<?>>>()));
}

Class<?> clazz = null;
@@ -58,7 +58,7 @@ public class TestData {
"ae2d8a571e03ac9c9eb76fac45af8e51",
"9cfc4e967edb808d679f777bc6702c7d" };

private static String[] CBCPKCS5PaddingTests = {
private static final String[] CBCPKCS5PaddingTests = {
/*
* key_len,key,iv,plainText,cipherText
*/
@@ -97,7 +97,7 @@ public class TestData {
"0397f4f6820b1f9386f14403be5ac16e50213bd473b4874b9bcbf5f318ee686b1d",
"e232cd6ef50047801ee681ec30f61d53cfd6b0bca02fd03c1b234baa10ea82ac9dab8b960926433a19ce6dea08677e34" };

private static String[] cipherCTRTests = {
private static final String[] cipherCTRTests = {
/*
* key_len,key,iv,plainText,cipherText
*/
@@ -336,9 +336,9 @@ private CryptoCipher getCipher(final String cipherClass) throws IOException {

class PositionedInputForTest implements Input {

byte[] data;
final byte[] data;
long pos;
long count;
final long count;

public PositionedInputForTest(final byte[] data) {
this.data = data;

0 comments on commit f62fd27

Please sign in to comment.