protected int engineUpdate( byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException { try { return cipher.processBytes(input, inputOffset, inputLen, output, outputOffset); } catch (DataLengthException e) { throw new ShortBufferException(e.getMessage()); } }
@Override protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException { int maxSize = engineGetOutputSize(inputLen); byte[] output = new byte[maxSize]; int finalSize; try { finalSize = doFinal(input, inputOffset, inputLen, output, 0); } catch (ShortBufferException e) { // This shouldn't be possible rethrow as RuntimeException throw new RuntimeException("Short buffer exception shouldn't be possible from here."); } if ( maxSize == finalSize ) { return output; } else { // TODO: Special doFinal to avoid this copy byte[] exact = new byte[finalSize]; System.arraycopy(output, 0, exact, 0, finalSize); return exact; } }
public int update(ByteBuffer input, ByteBuffer output) throws ShortBufferException { //checkState(); Preconditions.checkArgument(input.isDirect() && output.isDirect(), "Direct buffers are required."); try { int outputPosition = output.position(); if (mode == TRANSFORM_MODE) transform(input, output); else if (mode == ENCRYPT_MODE) encrypt(input, output); else decrypt(input, output); return output.position() - outputPosition; } catch (IOException e) { e.printStackTrace(); } return 0; }
/** * Finish a multiple-part encryption or decryption operation (depending on * how this cipher was initialized). * * @param input the input buffer * @param inOff the offset where the input starts * @param inLen the input length * @param output the buffer for the result * @param outOff the offset where the result is stored * @return the output length * @throws ShortBufferException if the output buffer is too small to hold the result. * @throws IllegalBlockSizeException if the plaintext or ciphertext size is too large. * @throws BadPaddingException if the ciphertext is invalid. */ public final int doFinal(byte[] input, int inOff, int inLen, byte[] output, int outOff) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException { if (output.length < getOutputSize(inLen)) { throw new ShortBufferException("Output buffer too short."); } byte[] out = doFinal(input, inOff, inLen); System.arraycopy(out, 0, output, outOff, out.length); return out.length; }
protected int engineUpdate( byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException { try { cipher.processBytes(input, inputOffset, inputLen, output, outputOffset); return inputLen; } catch (DataLengthException e) { throw new ShortBufferException(e.getMessage()); } }
public static void main(String[] args) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException { byte[] plainText = new byte[801]; // Initialization RandomFactory.getRandom().nextBytes(plainText); Cipher ci = new NullCipher(); // Encryption byte[] cipherText = new byte[ci.getOutputSize(plainText.length)]; int offset = ci.update(plainText, 0, plainText.length, cipherText, 0); ci.doFinal(cipherText, offset); // Decryption byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)]; int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText); // Comparison if (len != plainText.length || !TestUtilities.equalsBlock(plainText, cipherText, len) || !TestUtilities.equalsBlock(plainText, recoveredText, len)) { throw new RuntimeException( "Test failed because plainText not equal to cipherText and revoveredText"); } }
protected int engineGenerateSecret( byte[] sharedSecret, int offset) throws IllegalStateException, ShortBufferException { byte[] secret = engineGenerateSecret(); if (sharedSecret.length - offset < secret.length) { throw new ShortBufferException(kaAlgorithm + " key agreement: need " + secret.length + " bytes"); } System.arraycopy(secret, 0, sharedSecret, offset, secret.length); return secret.length; }
/** * Calls the second overloaded version of the same method. */ protected final byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException { int length = 0; byte[] out = new byte[engineGetOutputSize(inputLen)]; try { length = engineDoFinal(input, inputOffset, inputLen, out, 0); } catch (ShortBufferException sbe) { } if (length < out.length) { byte[] smaller = new byte[length]; System.arraycopy(out, 0, smaller, 0, length); } return out; }
/** * Calls encrypt or decrypt based on the state of the cipher. Creates a * single input array from the supplied input data. And returns number of * bytes stored in output. * * @param input * - the input buffer * @param inputOffset * - the offset in input where the input starts always zero * @param inputLen * - the input length * @param output * - the buffer for the result * @param outputOffset * - the offset in output where the result is stored * @return the number of bytes stored in output */ protected final int engineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException { // Create a single array of input data byte[] totalInput = new byte[inputLen ]; if (inputLen > 0) System.arraycopy(input, inputOffset, totalInput, 0, inputLen); if (stateMode == Cipher.ENCRYPT_MODE) try { return encrypt(input, inputOffset, inputLen, output, outputOffset); } catch (Exception e) { e.printStackTrace(); } else if (stateMode == Cipher.DECRYPT_MODE) return decrypt(input, inputOffset, inputLen, output, outputOffset); return 0; }
public void runAll() throws InvalidKeyException, NoSuchPaddingException, InvalidAlgorithmParameterException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException, NoSuchProviderException { for (String mode : MODES) { for (String padding : PADDINGS) { if (!isMultipleKeyLengthSupported()) { runTest(mode, padding, minKeySize); } else { int keySize = maxKeySize; while (keySize >= minKeySize) { out.println("With Key Strength: " + keySize); runTest(mode, padding, keySize); keySize -= KEYCUTTER; } } } } }
/** * Adds the given number of padding bytes to the data input. * The value of the padding bytes is determined * by the specific padding mechanism that implements this * interface. * * @param in the input buffer with the data to pad * @param off the offset in <code>in</code> where the padding bytes * are appended * @param len the number of padding bytes to add * * @exception ShortBufferException if <code>in</code> is too small to hold * the padding bytes */ public void padWithLen(byte[] in, int off, int len) throws ShortBufferException { if (in == null) return; if ((off + len) > in.length) { throw new ShortBufferException("Buffer too small to hold padding"); } byte paddingOctet = (byte) (len & 0xff); byte[] padding = new byte[len]; SunJCE.getRandom().nextBytes(padding); padding[len-1] = paddingOctet; System.arraycopy(padding, 0, in, off, len); return; }
/** * Adds the given number of padding bytes to the data input. * The value of the padding bytes is determined * by the specific padding mechanism that implements this * interface. * * @param in the input buffer with the data to pad * @param off the offset in <code>in</code> where the padding bytes * are appended * @param len the number of padding bytes to add * * @exception ShortBufferException if <code>in</code> is too small to hold * the padding bytes */ public void padWithLen(byte[] in, int off, int len) throws ShortBufferException { if (in == null) return; if ((off + len) > in.length) { throw new ShortBufferException("Buffer too small to hold padding"); } byte paddingOctet = (byte) (len & 0xff); for (int i = 0; i < len; i++) { in[i + off] = paddingOctet; } return; }
private byte[] hash(Cipher aes, byte[] h, byte[] nonce, byte[] plaintext, byte[] data) { final Polyval polyval = new Polyval(h); polyval.update(data); // hash data with padding polyval.update(plaintext); // hash plaintext with padding // hash data and plaintext lengths in bits with padding final byte[] block = new byte[AES_BLOCK_SIZE]; Bytes.putLong((long) data.length * 8, block, 0); Bytes.putLong((long) plaintext.length * 8, block, 8); polyval.updateBlock(block, 0); polyval.digest(block); for (int i = 0; i < nonce.length; i++) { block[i] ^= nonce[i]; } block[block.length - 1] &= (byte) ~0x80; // encrypt polyval hash to produce tag try { aes.update(block, 0, block.length, block, 0); } catch (ShortBufferException e) { throw new IllegalStateException(e); } return block; }
private byte[] subKey(int ctrStart, int ctrEnd, byte[] nonce) { final byte[] counter = new byte[AES_BLOCK_SIZE]; System.arraycopy(nonce, 0, counter, counter.length - nonce.length, nonce.length); final byte[] key = new byte[(ctrEnd - ctrStart + 1) * 8]; final byte[] block = new byte[AES_BLOCK_SIZE]; for (int i = ctrStart; i <= ctrEnd; i++) { Bytes.putInt(i, counter); try { aes.update(counter, 0, AES_BLOCK_SIZE, block, 0); } catch (ShortBufferException e) { throw new IllegalStateException(e); } System.arraycopy(block, 0, key, (i - ctrStart) * 8, 8); } return key; }
public byte[] encrypt(int version, byte[] data) { CryptVersion cryptVersion = cryptVersion(version); try { int cryptedLength = cryptVersion.encryptedLength.apply(data.length); byte[] result = new byte[cryptedLength + cryptVersion.saltLength + 1]; result[0] = toSignedByte(version); byte[] random = urandomBytes(cryptVersion.saltLength); IvParameterSpec iv_spec = new IvParameterSpec(random); System.arraycopy(random, 0, result, 1, cryptVersion.saltLength); Cipher cipher = cipher(cryptVersion.cipher); cipher.init(Cipher.ENCRYPT_MODE, cryptVersion.key, iv_spec); int len = cipher.doFinal(data, 0, data.length, result, cryptVersion.saltLength + 1); if (len < cryptedLength) LOG.info("len was " + len + " instead of " + cryptedLength); return result; } catch (ShortBufferException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException | InvalidKeyException e) { throw new RuntimeException("JCE exception caught while encrypting with version " + version, e); } }
protected int engineUpdate( byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws ShortBufferException { throw new RuntimeException("not supported for wrapping"); }
@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; } }
protected ByteBuf decipher(ChannelHandlerContext ctx, ByteBuf buffer) throws ShortBufferException { int i = buffer.readableBytes(); byte[] abyte = this.bufToBytes(buffer); ByteBuf bytebuf = ctx.alloc().heapBuffer(this.cipher.getOutputSize(i)); bytebuf.writerIndex(this.cipher.update(abyte, 0, i, bytebuf.array(), bytebuf.arrayOffset())); return bytebuf; }
/** * Continue a multiple-part encryption or decryption operation (depending on * how this cipher was initialized), processing another data part. * * @param input the input buffer * @param inOff the offset where the input starts * @param inLen the input length * @param output the output buffer * @param outOff the offset where the result is stored * @return the length of the output * @throws ShortBufferException if the output buffer is too small to hold the result. */ public final int update(byte[] input, int inOff, int inLen, byte[] output, int outOff) throws ShortBufferException { if (output.length < getOutputSize(inLen)) { throw new ShortBufferException("output"); } byte[] out = update(input, inOff, inLen); System.arraycopy(out, 0, output, outOff, out.length); return out.length; }
/** * Finish a multiple-part encryption or decryption operation (depending on * how this cipher was initialized). * * @param input the input buffer * @param inOff the offset where the input starts * @param inLen the input length * @param output the buffer for the result * @param outOff the offset where the result is stored * @return the output length * @throws ShortBufferException if the output buffer is too small to hold the result. * @throws BadPaddingException if the ciphertext is invalid. */ public final int doFinal(byte[] input, int inOff, int inLen, byte[] output, int outOff) throws ShortBufferException, BadPaddingException { if (output.length < getOutputSize(inLen)) { throw new ShortBufferException("Output buffer too short."); } byte[] out = doFinal(input, inOff, inLen); System.arraycopy(out, 0, output, outOff, out.length); return out.length; }
protected int engineDoFinal( byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset) throws IllegalBlockSizeException, BadPaddingException, ShortBufferException { return 0; }
public int engineDoFinal( byte[] input, int inputOffset, int inputLength, byte[] output, int outputOffset) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException { byte[] buf = engineDoFinal(input, inputOffset, inputLength); System.arraycopy(buf, 0, output, outputOffset, buf.length); return buf.length; }
protected void cipher(ByteBuf in, ByteBuf out) throws ShortBufferException { int i = in.readableBytes(); byte[] abyte = this.bufToBytes(in); int j = this.cipher.getOutputSize(i); if (this.outputBuffer.length < j) { this.outputBuffer = new byte[j]; } out.writeBytes((byte[])this.outputBuffer, 0, this.cipher.update(abyte, 0, i, this.outputBuffer)); }
@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(); } }
private int nonFlushingUpdate(byte[] in, int inOffset, int length, byte[] out, int outOffset) { try { return cipher.update(in, inOffset, length, out, outOffset); } catch (ShortBufferException e) { // Should never happen. throw new RuntimeException(e); } }
/** * Calls the second overloaded version of the same method. * * @return the result from encryption or decryption */ protected final byte[] engineUpdate(byte[] input, int inputOffset, int inputLen) { byte[] out = new byte[engineGetOutputSize(inputLen)]; try { engineUpdate(input, inputOffset, inputLen, out, 0); } catch (ShortBufferException sbe) { } return out; }
/** * Calls the second overloaded version of the same method, * to perform the required operation based on the state of the cipher. * * @return returns the result from encryption or decryption */ protected final byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException { byte[] out = new byte[engineGetOutputSize(inputLen)]; try { engineDoFinal(input, inputOffset, inputLen, out, 0); } catch (ShortBufferException sbe) { } return out; }
private int readFinal(byte[] buffer, int offset, int length) throws IOException { try { int flourish = cipher.doFinal(buffer, offset); done = true; return flourish; } catch (IllegalBlockSizeException | BadPaddingException | ShortBufferException e) { throw new IOException(e); } }
protected ByteBuf decipher(ChannelHandlerContext ctx, ByteBuf buffer) throws ShortBufferException { int i = buffer.readableBytes(); byte[] abyte = this.func_150502_a(buffer); ByteBuf bytebuf = ctx.alloc().heapBuffer(this.cipher.getOutputSize(i)); bytebuf.writerIndex(this.cipher.update(abyte, 0, i, bytebuf.array(), bytebuf.arrayOffset())); return bytebuf; }
private int doFinal(byte[] out, int outOfs, int outLen) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException { if (bufOfs > buffer.length) { throw new IllegalBlockSizeException( "Data must not be longer than " + (buffer.length - (encrypt ? padLen : 0)) + " bytes"); } if (outLen < outputSize) { throw new ShortBufferException(); } try { long keyValue = key.value(); int k = nativeAtomic(mech.value(), encrypt, keyValue, key.length(), buffer, bufOfs, out, outOfs, outLen); if (k < 0) { if ( k == -16 || k == -64) { // -16: CRYPTO_ENCRYPTED_DATA_INVALID // -64: CKR_ENCRYPTED_DATA_INVALID, see bug 17459266 UcryptoException ue = new UcryptoException(16); BadPaddingException bpe = new BadPaddingException("Invalid encryption data"); bpe.initCause(ue); throw bpe; } throw new UcryptoException(-k); } return k; } finally { bufOfs = 0; } }
protected void cipher(ByteBuf p_150504_1_, ByteBuf p_150504_2_) throws ShortBufferException { int i = p_150504_1_.readableBytes(); byte[] abyte = this.func_150502_a(p_150504_1_); int j = this.cipher.getOutputSize(i); if (this.field_150506_c.length < j) { this.field_150506_c = new byte[j]; } p_150504_2_.writeBytes((byte[])this.field_150506_c, 0, this.cipher.update(abyte, 0, i, this.field_150506_c)); }
public byte[] generateSecret() throws IllegalStateException { byte[] sharedSecret = new byte[KEY_SIZE]; try { generateSecret(sharedSecret, 0); } catch (ShortBufferException e) { e.printStackTrace(); } return sharedSecret; }
public int generateSecret(byte[] bytes, int i) throws IllegalStateException, ShortBufferException { if (bytes.length - i < KEY_SIZE) throw new ShortBufferException(); System.arraycopy(mSharedSecret, 0, bytes, i, KEY_SIZE); return KEY_SIZE; }
public int update(byte[] input, int offset, int length, byte[] output, int outputOffset) throws ShortBufferException { willUseKey(); if (outputOffset + length > output.length) throw new ShortBufferException( "output buffer is too small to hold the result."); return native_update(opmode, input, offset, length, output, outputOffset); }