package com.oracle.security.ucrypto;
import java.nio.ByteBuffer;
import java.util.Set;
import java.util.Arrays;
import java.util.Locale;
import java.util.concurrent.ConcurrentSkipListSet;
import java.lang.ref.*;
import java.security.AlgorithmParameters;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidParameterSpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.CipherSpi;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.ShortBufferException;
import javax.crypto.spec.IvParameterSpec;
public class NativeCipherWithJavaPadding extends CipherSpi {
private static interface Padding {
int getPadLen(int dataLen);
byte[] getPaddingBytes(int dataLen);
byte[] bufferBytes(byte[] intermediateData);
int getBufferedLength();
int unpad(byte[] paddedData, byte[] out, int outOfs)
throws BadPaddingException, IllegalBlockSizeException,
ShortBufferException;
void clear();
}
private static class PKCS5Padding implements Padding {
private final int blockSize;
private ByteBuffer trailingBytes = null;
PKCS5Padding(int blockSize)
throws NoSuchPaddingException {
if (blockSize == 0) {
throw new NoSuchPaddingException
("PKCS#5 padding not supported with stream ciphers");
}
this.blockSize = blockSize;
}
public int getPadLen(int dataLen) {
return (blockSize - (dataLen & (blockSize - 1)));
}
public byte[] getPaddingBytes(int dataLen) {
byte padValue = (byte) getPadLen(dataLen);
byte[] paddingBytes = new byte[padValue];
Arrays.fill(paddingBytes, padValue);
return paddingBytes;
}
public byte[] bufferBytes(byte[] dataFromUpdate) {
if (dataFromUpdate == null || dataFromUpdate.length == 0) {
return null;
}
byte[] result = null;
if (trailingBytes == null) {
trailingBytes = ByteBuffer.wrap(new byte[blockSize]);
}
int tbSize = trailingBytes.position();
if (dataFromUpdate.length > trailingBytes.remaining()) {
int totalLen = dataFromUpdate.length + tbSize;
int newTBSize = totalLen % blockSize;
if (newTBSize == 0) {
newTBSize = blockSize;
}
if (tbSize == 0) {
result = Arrays.copyOf(dataFromUpdate, totalLen - newTBSize);
} else {
result = Arrays.copyOf(trailingBytes.array(),
totalLen - newTBSize);
if (result.length != tbSize) {
System.arraycopy(dataFromUpdate, 0, result, tbSize,
result.length - tbSize);
}
}
trailingBytes.clear();
trailingBytes.put(dataFromUpdate,
dataFromUpdate.length - newTBSize, newTBSize);
} else {
trailingBytes.put(dataFromUpdate);
}
return result;
}
public int getBufferedLength() {
if (trailingBytes != null) {
return trailingBytes.position();
}
return 0;
}
public int unpad(byte[] lastData, byte[] out, int outOfs)
throws BadPaddingException, IllegalBlockSizeException,
ShortBufferException {
int tbSize = (trailingBytes == null? 0:trailingBytes.position());
int dataLen = tbSize + lastData.length;
if (dataLen <= 0) {
return 0;
} else if (dataLen % blockSize != 0) {
UcryptoProvider.debug("PKCS5Padding: unpad, buffered " + tbSize +
" bytes, last block " + lastData.length + " bytes");
throw new IllegalBlockSizeException
("Input length must be multiples of " + blockSize);
}
if (lastData.length == 0) {
if (tbSize != 0) {
lastData = Arrays.copyOf(trailingBytes.array(), tbSize);
trailingBytes.clear();
tbSize = 0;
} else {
throw new BadPaddingException("No pad bytes found!");
}
}
byte padValue = lastData[lastData.length - 1];
if (padValue < 1 || padValue > blockSize) {
UcryptoProvider.debug("PKCS5Padding: unpad, lastData: " + Arrays.toString(lastData));
UcryptoProvider.debug("PKCS5Padding: unpad, padValue=" + padValue);
throw new BadPaddingException("Invalid pad value: " + padValue);
}
int padStartIndex = lastData.length - padValue;
for (int i = padStartIndex; i < lastData.length; i++) {
if (lastData[i] != padValue) {
UcryptoProvider.debug("PKCS5Padding: unpad, lastData: " + Arrays.toString(lastData));
UcryptoProvider.debug("PKCS5Padding: unpad, padValue=" + padValue);
throw new BadPaddingException("Invalid padding bytes!");
}
}
int actualOutLen = dataLen - padValue;
if (out.length - outOfs < actualOutLen) {
throw new ShortBufferException("Output buffer too small, need " + actualOutLen +
", got " + (out.length - outOfs));
}
try {
if (tbSize != 0) {
trailingBytes.rewind();
if (tbSize < actualOutLen) {
trailingBytes.get(out, outOfs, tbSize);
outOfs += tbSize;
} else {
trailingBytes.get(out, outOfs, actualOutLen);
return actualOutLen;
}
}
if (lastData.length > padValue) {
System.arraycopy(lastData, 0, out, outOfs,
lastData.length - padValue);
}
return actualOutLen;
} finally {
clear();
}
}
public void clear() {
if (trailingBytes != null) trailingBytes.clear();
}
}
public static final class AesEcbPKCS5 extends NativeCipherWithJavaPadding {
public AesEcbPKCS5() throws NoSuchAlgorithmException, NoSuchPaddingException {
super(new NativeCipher.AesEcbNoPadding(), "PKCS5Padding");
}
}
public static final class AesCbcPKCS5 extends NativeCipherWithJavaPadding {
public AesCbcPKCS5() throws NoSuchAlgorithmException, NoSuchPaddingException {
super(new NativeCipher.AesCbcNoPadding(), "PKCS5Padding");
}
}
public static final class AesCfb128PKCS5 extends NativeCipherWithJavaPadding {
public AesCfb128PKCS5() throws NoSuchAlgorithmException, NoSuchPaddingException {
super(new NativeCipher.AesCfb128NoPadding(), "PKCS5Padding");
}
}
private final NativeCipher nc;
private final Padding padding;
private final int blockSize;
private int lastBlockLen = 0;
NativeCipherWithJavaPadding(NativeCipher nc, String paddingScheme)
throws NoSuchAlgorithmException, NoSuchPaddingException {
this.nc = nc;
this.blockSize = nc.engineGetBlockSize();
if (paddingScheme.toUpperCase(Locale.ROOT).equals("PKCS5PADDING")) {
padding = new PKCS5Padding(blockSize);
} else {
throw new NoSuchAlgorithmException("Unsupported padding scheme: " + paddingScheme);
}
}
void reset() {
padding.clear();
lastBlockLen = 0;
}
@Override
protected synchronized void engineSetMode(String mode) throws NoSuchAlgorithmException {
nc.engineSetMode(mode);
}
@Override
protected void engineSetPadding(String padding)
throws NoSuchPaddingException {
throw new NoSuchPaddingException("Unsupported padding " + padding);
}
@Override
protected int engineGetBlockSize() {
return blockSize;
}
@Override
protected synchronized int engineGetOutputSize(int inputLen) {
int result = nc.engineGetOutputSize(inputLen);
if (nc.encrypt) {
result += padding.getPadLen(result);
} else {
result += padding.getBufferedLength();
}
return result;
}
@Override
protected synchronized byte[] engineGetIV() {
return nc.engineGetIV();
}
@Override
protected synchronized AlgorithmParameters engineGetParameters() {
return nc.engineGetParameters();
}
@Override
protected int engineGetKeySize(Key key) throws InvalidKeyException {
return nc.engineGetKeySize(key);
}
@Override
protected synchronized void engineInit(int opmode, Key key, SecureRandom random)
throws InvalidKeyException {
reset();
nc.engineInit(opmode, key, random);
}
@Override
protected synchronized void engineInit(int opmode, Key key,
AlgorithmParameterSpec params, SecureRandom random)
throws InvalidKeyException, InvalidAlgorithmParameterException {
reset();
nc.engineInit(opmode, key, params, random);
}
@Override
protected synchronized void engineInit(int opmode, Key key, AlgorithmParameters params,
SecureRandom random)
throws InvalidKeyException, InvalidAlgorithmParameterException {
reset();
nc.engineInit(opmode, key, params, random);
}
@Override
protected synchronized byte[] engineUpdate(byte[] in, int inOfs, int inLen) {
if (nc.encrypt) {
lastBlockLen += inLen;
lastBlockLen &= (blockSize - 1);
return nc.engineUpdate(in, inOfs, inLen);
} else {
return padding.bufferBytes(nc.engineUpdate(in, inOfs, inLen));
}
}
@Override
protected synchronized int engineUpdate(byte[] in, int inOfs, int inLen, byte[] out,
int outOfs) throws ShortBufferException {
if (nc.encrypt) {
lastBlockLen += inLen;
lastBlockLen &= (blockSize - 1);
return nc.engineUpdate(in, inOfs, inLen, out, outOfs);
} else {
byte[] result = padding.bufferBytes(nc.engineUpdate(in, inOfs, inLen));
if (result != null) {
System.arraycopy(result, 0, out, outOfs, result.length);
return result.length;
} else return 0;
}
}
@Override
protected synchronized byte[] engineDoFinal(byte[] in, int inOfs, int inLen)
throws IllegalBlockSizeException, BadPaddingException {
int estimatedOutLen = engineGetOutputSize(inLen);
byte[] out = new byte[estimatedOutLen];
try {
int actualOut = this.engineDoFinal(in, inOfs, inLen, out, 0);
if (actualOut != out.length) {
out = Arrays.copyOf(out, actualOut);
}
} catch (ShortBufferException sbe) {
throw new UcryptoException("Internal Error", sbe);
} finally {
reset();
}
return out;
}
@Override
protected synchronized int engineDoFinal(byte[] in, int inOfs, int inLen, byte[] out,
int outOfs)
throws ShortBufferException, IllegalBlockSizeException,
BadPaddingException {
int estimatedOutLen = engineGetOutputSize(inLen);
if (out.length - outOfs < estimatedOutLen) {
throw new ShortBufferException("Actual: " + (out.length - outOfs) +
". Estimated Out Length: " + estimatedOutLen);
}
try {
if (nc.encrypt) {
int k = nc.engineUpdate(in, inOfs, inLen, out, outOfs);
lastBlockLen += inLen;
lastBlockLen &= (blockSize - 1);
byte[] padBytes = padding.getPaddingBytes(lastBlockLen);
k += nc.engineDoFinal(padBytes, 0, padBytes.length, out, (outOfs + k));
return k;
} else {
byte[] tempOut = nc.engineDoFinal(in, inOfs, inLen);
int len = padding.unpad(tempOut, out, outOfs);
return len;
}
} finally {
reset();
}
}
@Override
protected synchronized byte[] engineWrap(Key key) throws IllegalBlockSizeException,
InvalidKeyException {
byte[] result = null;
try {
byte[] encodedKey = key.getEncoded();
if ((encodedKey == null) || (encodedKey.length == 0)) {
throw new InvalidKeyException("Cannot get an encoding of " +
"the key to be wrapped");
}
result = engineDoFinal(encodedKey, 0, encodedKey.length);
} catch (BadPaddingException e) {
throw new UcryptoException("Internal Error", e);
}
return result;
}
@Override
protected synchronized Key engineUnwrap(byte[] wrappedKey, String wrappedKeyAlgorithm,
int wrappedKeyType)
throws InvalidKeyException, NoSuchAlgorithmException {
byte[] encodedKey;
try {
encodedKey = engineDoFinal(wrappedKey, 0,
wrappedKey.length);
} catch (Exception e) {
throw (InvalidKeyException)
(new InvalidKeyException()).initCause(e);
}
return NativeCipher.constructKey(wrappedKeyType, encodedKey,
wrappedKeyAlgorithm);
}
}