Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support to GCM #77

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;

import org.apache.commons.crypto.Crypto;
Expand Down Expand Up @@ -174,6 +175,22 @@ protected CryptoInputStream(ReadableByteChannel in, CryptoCipher cipher,
this(new ChannelInput(in), cipher, bufferSize, key, params);
}

/**
* Constructs a {@link CryptoInputStream}.
*
* @param in the ReadableByteChannel instance.
* @param cipher the cipher instance.
* @param bufferSize the bufferSize.
* @param key crypto key for the cipher.
* @param params the algorithm parameters.
* @throws IOException if an I/O error occurs.
*/
protected CryptoInputStream(ReadableByteChannel in, CryptoCipher cipher,
int bufferSize, Key key, AlgorithmParameterSpec params,int dateSize)
throws IOException {
this(new ChannelInput(in,dateSize), cipher, bufferSize, key, params);
}

/**
* Constructs a {@link CryptoInputStream}.
*
Expand All @@ -192,15 +209,16 @@ protected CryptoInputStream(Input input, CryptoCipher cipher, int bufferSize,

this.key = key;
this.params = params;
if (!(params instanceof IvParameterSpec)) {
// other AlgorithmParameterSpec such as GCMParameterSpec is not
// supported now.
if (params instanceof IvParameterSpec){
outBuffer = ByteBuffer.allocateDirect(this.bufferSize
+ cipher.getBlockSize());
}else if (params instanceof GCMParameterSpec){
outBuffer = ByteBuffer.allocateDirect(input.available());
}else {
throw new IOException("Illegal parameters");
}

inBuffer = ByteBuffer.allocateDirect(this.bufferSize);
outBuffer = ByteBuffer.allocateDirect(this.bufferSize
+ cipher.getBlockSize());
outBuffer.limit(0);

initCipher();
Expand Down Expand Up @@ -412,7 +430,7 @@ public int read(ByteBuffer dst) throws IOException {
// Decrypt more data
// we loop for new data
int nd = 0;
while (nd == 0) {
while (nd == 0 && !finalDone) {
nd = decryptMore();
}

Expand Down Expand Up @@ -511,6 +529,8 @@ protected int decryptMore() throws IOException {
}

int n = input.read(inBuffer);

//if it is used of gmac ,it must dofinal though decryptFinal
if (n < 0) {
// The stream is end, finalize the cipher stream
decryptFinal();
Expand All @@ -523,10 +543,20 @@ protected int decryptMore() throws IOException {

// End of the stream
return -1;
} else if (n == 0) {
} else if (n == 0 && !(params instanceof GCMParameterSpec)) {
// No data is read, but the stream is not end yet
return 0;
} else {
} else if (n < bufferSize && (params instanceof GCMParameterSpec)){
decryptFinal();
// Satisfy the read with the remaining
int remaining = outBuffer.remaining();
if (remaining > 0) {
return remaining;
}

// End of the stream
return -1;
}else {
decrypt();
return outBuffer.remaining();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;

import org.apache.commons.crypto.cipher.CryptoCipher;
Expand Down Expand Up @@ -177,7 +178,7 @@ protected CryptoOutputStream(Output output, CryptoCipher cipher,
this.key = key;
this.params = params;

if (!(params instanceof IvParameterSpec)) {
if (!(params instanceof IvParameterSpec) && !(params instanceof GCMParameterSpec)) {
// other AlgorithmParameterSpec such as GCMParameterSpec is not
// supported now.
throw new IOException("Illegal parameters");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class ChannelInput implements Input {

private ByteBuffer buf;
private final ReadableByteChannel channel;
private int channelSize = 0;

/**
* Constructs the
Expand All @@ -42,6 +43,18 @@ public ChannelInput(ReadableByteChannel channel) {
this.channel = channel;
}

/**
* Constructs the
* {@link org.apache.commons.crypto.stream.input.ChannelInput}.
*
* @param channel the ReadableByteChannel object.
* @param channelSize using it to adjust gcm
*/
public ChannelInput(ReadableByteChannel channel,int channelSize) {
this.channel = channel;
this.channelSize = channelSize;
}

/**
* Overrides the
* {@link org.apache.commons.crypto.stream.input.Input#read(ByteBuffer)}.
Expand Down Expand Up @@ -99,13 +112,17 @@ public long skip(long n) throws IOException {
* single read or skip of this many bytes will not block, but may read or
* skip fewer bytes.
*
* @author Li,Gongyi
* @return an estimate of the number of bytes that can be read (or skipped
* over) from this input stream without blocking or {@code 0} when
* it reaches the end of the input stream.
* @throws IOException if an I/O error occurs.
*/
@Override
public int available() throws IOException {
if (channelSize != 0){
return channelSize;
}
return 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,5 @@ int read(long position, byte[] buffer, int offset, int length)
* @throws IOException if an I/O error occurs.
*/
void close() throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.commons.crypto.stream.input;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -157,4 +158,5 @@ public void seek(long position) throws IOException {
public void close() throws IOException {
in.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.nio.ByteBuffer;
import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Properties;
import java.util.Random;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package org.apache.commons.crypto.jna;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.spec.AlgorithmParameterSpec;

import org.apache.commons.crypto.cipher.AbstractCipherTest;
import org.apache.commons.crypto.stream.AbstractCipherStreamTest;
Expand All @@ -30,7 +32,7 @@ public abstract class AbstractCipherJnaStreamTest extends AbstractCipherStreamTe
private static final String CIPHER_OPENSSL_JNA = OpenSslJna.getCipherClass().getName();

@Before
public void init() {
public void init() throws IOException{
Assume.assumeTrue(OpenSslJna.isEnabled());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
*/
package org.apache.commons.crypto.jna;

import javax.crypto.spec.IvParameterSpec;
import java.io.IOException;
import java.security.spec.AlgorithmParameterSpec;

public class CbcNoPaddingCipherJnaStreamTest extends AbstractCipherJnaStreamTest {

@Override
public void setUp() throws IOException {
transformation = "AES/CBC/NoPadding";
super.algorithmParameterSpec = new IvParameterSpec(iv);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
*/
package org.apache.commons.crypto.jna;

import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import java.io.IOException;
import java.security.spec.AlgorithmParameterSpec;

public class CbcPkcs5PaddingCipherJnaStreamTest extends AbstractCipherJnaStreamTest {

@Override
public void setUp() throws IOException {
transformation = "AES/CBC/PKCS5Padding";
algorithmParameterSpec = new IvParameterSpec(iv);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
*/
package org.apache.commons.crypto.jna;

import javax.crypto.spec.IvParameterSpec;
import java.io.IOException;
import java.security.spec.AlgorithmParameterSpec;

public class CtrCryptoJnaStreamTest extends AbstractCipherJnaStreamTest {

@Override
public void setUp() throws IOException {
transformation = "AES/CTR/NoPadding";
algorithmParameterSpec = new IvParameterSpec(iv);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
*/
package org.apache.commons.crypto.jna;

import javax.crypto.spec.IvParameterSpec;
import java.io.IOException;
import java.security.spec.AlgorithmParameterSpec;

public class CtrNoPaddingCipherJnaStreamTest extends AbstractCipherJnaStreamTest {

@Override
public void setUp() throws IOException {
transformation = "AES/CTR/NoPadding";
algorithmParameterSpec = new IvParameterSpec(iv);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Arrays;
import java.util.Properties;
import java.util.Random;
import javax.crypto.spec.IvParameterSpec;
Expand All @@ -52,6 +54,7 @@ public abstract class AbstractCipherStreamTest {
protected int count = 10000;
protected static int defaultBufferSize = 8192;
protected static int smallBufferSize = 1024;
protected AlgorithmParameterSpec algorithmParameterSpec;

protected String transformation;

Expand All @@ -72,7 +75,6 @@ public void before() throws IOException {
public void testSkip() throws Exception {
doSkipTest(AbstractCipherTest.JCE_CIPHER_CLASSNAME, false);
doSkipTest(AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME, false);

doSkipTest(AbstractCipherTest.JCE_CIPHER_CLASSNAME, true);
doSkipTest(AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME, true);
}
Expand All @@ -82,7 +84,6 @@ public void testSkip() throws Exception {
public void testByteBufferRead() throws Exception {
doByteBufferRead(AbstractCipherTest.JCE_CIPHER_CLASSNAME, false);
doByteBufferRead(AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME, false);

doByteBufferRead(AbstractCipherTest.JCE_CIPHER_CLASSNAME, true);
doByteBufferRead(AbstractCipherTest.OPENSSL_CIPHER_CLASSNAME, true);
}
Expand Down Expand Up @@ -263,7 +264,7 @@ private void prepareData() throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (OutputStream out = new CryptoOutputStream(baos, cipher,
defaultBufferSize, new SecretKeySpec(key, "AES"),
new IvParameterSpec(iv))) {
algorithmParameterSpec)) {
out.write(data);
out.flush();
}
Expand All @@ -276,10 +277,10 @@ protected CryptoInputStream getCryptoInputStream(ByteArrayInputStream bais,
if (withChannel) {
return new CryptoInputStream(Channels.newChannel(bais), cipher,
bufferSize, new SecretKeySpec(key, "AES"),
new IvParameterSpec(iv));
algorithmParameterSpec);
}
return new CryptoInputStream(bais, cipher, bufferSize,
new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
new SecretKeySpec(key, "AES"), algorithmParameterSpec);
}

protected CryptoOutputStream getCryptoOutputStream(
Expand All @@ -288,10 +289,10 @@ protected CryptoOutputStream getCryptoOutputStream(
if (withChannel) {
return new CryptoOutputStream(Channels.newChannel(baos), cipher,
bufferSize, new SecretKeySpec(key, "AES"),
new IvParameterSpec(iv));
algorithmParameterSpec);
}
return new CryptoOutputStream(baos, cipher, bufferSize,
new SecretKeySpec(key, "AES"), new IvParameterSpec(iv));
new SecretKeySpec(key, "AES"), algorithmParameterSpec);
}

private int readAll(InputStream in, byte[] b, int offset, int len)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
*/
package org.apache.commons.crypto.stream;

import javax.crypto.spec.IvParameterSpec;
import java.io.IOException;
import java.security.spec.AlgorithmParameterSpec;

public class CbcNoPaddingCipherStreamTest extends AbstractCipherStreamTest {

@Override
public void setUp() throws IOException {
transformation = "AES/CBC/NoPadding";
algorithmParameterSpec = new IvParameterSpec(super.iv);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
*/
package org.apache.commons.crypto.stream;

import javax.crypto.spec.IvParameterSpec;
import javax.swing.plaf.SeparatorUI;
import java.io.IOException;

public class CbcPkcs5PaddingCipherStreamTest extends AbstractCipherStreamTest {

@Override
public void setUp() throws IOException {
transformation = "AES/CBC/PKCS5Padding";
algorithmParameterSpec = new IvParameterSpec(super.iv);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@

import org.apache.commons.crypto.cipher.CryptoCipher;

import javax.crypto.spec.IvParameterSpec;

public class CtrCryptoStreamTest extends AbstractCipherStreamTest {

@Override
public void setUp() throws IOException {
transformation = "AES/CTR/NoPadding";
algorithmParameterSpec = new IvParameterSpec(super.iv);
}

@Override
Expand Down
Loading