/** * Creates the RijndaelECB enum value. iv is null. * @param bitmask * @param keyType The type of key the alg requires */ private CryptByteBufferType(int bitmask, KeyType keyType){ this.bitmask = bitmask; this.keyType = keyType; this.cipherName = keyType.alg; this.blockSize = keyType.keySize; this.ivSize = null; algName = name(); isStreamCipher = false; }
/** * Gets the IV being used. Only works with algorithms that support IVs. * @return Returns the iv as a IvParameterSpec */ public IvParameterSpec getIV(){ if(!type.hasIV()){ throw new UnsupportedTypeException(type); } return iv; } }
/** * Creates the RijndaelPCFB enum value. * @param bitmask * @param ivSize Size of the iv * @param keyType The type of key the alg requires */ private CryptByteBufferType(int bitmask, int ivSize, KeyType keyType){ this.bitmask = bitmask; this.keyType = keyType; this.cipherName = keyType.alg; this.blockSize = keyType.keySize; this.ivSize = ivSize; algName = name(); isStreamCipher = true; }
/** * Changes the current iv to the provided iv and initializes the cipher instances with * the new iv. Only works with algorithms that support IVs, not RijndaelPCFB. * @param iv The new iv to use as IvParameterSpec * @throws InvalidAlgorithmParameterException */ public void setIV(IvParameterSpec iv) throws InvalidAlgorithmParameterException{ if(!type.hasIV()){ throw new UnsupportedTypeException(type); } this.iv = iv; try { encryptCipher.init(Cipher.ENCRYPT_MODE, this.key, this.iv); decryptCipher.init(Cipher.DECRYPT_MODE, this.key, this.iv); } catch (InvalidKeyException e) { throw new IllegalArgumentException(e); } }
/** * Creates the RijndaelECB128 enum value. iv is null. * and sets the non-standard blocksize. * @param bitmask * @param keyType The type of key the alg requires * @param blockSize The blocksize the alg uses */ private CryptByteBufferType(int bitmask, KeyType keyType, int blockSize){ this.bitmask = bitmask; this.ivSize = null; this.keyType = keyType; this.cipherName = keyType.alg; this.blockSize = blockSize; algName = name(); isStreamCipher = false; }
/** * Generates a new IV to be used and initializes the cipher instances with * the new iv. Only works with algorithms that support IVs, not RijndaelPCFB. * @return The generated IV */ public IvParameterSpec genIV(){ if(!type.hasIV()){ throw new UnsupportedTypeException(type); } this.iv = KeyGenUtils.genIV(type.ivSize); try { encryptCipher.init(Cipher.ENCRYPT_MODE, this.key, this.iv); decryptCipher.init(Cipher.DECRYPT_MODE, this.key, this.iv); } catch (InvalidKeyException e) { throw new IllegalArgumentException(e); // Definitely a bug ... } catch (InvalidAlgorithmParameterException e) { throw new IllegalArgumentException(e); // Definitely a bug ... } return iv; }
@Test public void testEncryptByteArrayIntIntNullInput() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] nullArray = null; try{ crypt.encryptCopy(nullArray, 0, plainText[i].length()); fail("CryptByteBufferType: "+type.name()+": Expected IllegalArgumentException or " + "NullPointerException"); }catch(IllegalArgumentException e) { }catch(NullPointerException e){} } }
@Test public void testDecryptByteArrayIntIntNullInput() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] nullArray = null; try{ crypt.decryptCopy(nullArray, 0, plainText[i].length()); fail("CryptByteBufferType: "+type.name()+": Expected IllegalArgumentException or " + "NullPointerException"); }catch(NullPointerException e) { } catch(IllegalArgumentException e){ } } }
@Test public void testEncryptByteArrayIntIntLengthOutOfBounds() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } try{ crypt.encryptCopy(Hex.decode(plainText[i]), 0, plainText[i].length()+3); fail("CryptByteBufferType: "+type.name()+": Expected IllegalArgumentException or " + "ArrayIndexOutOfBoundsException"); }catch(IllegalArgumentException e) { } catch(IndexOutOfBoundsException e){} } }
@Test public void testEncryptByteArrayIntIntOffsetOutOfBounds() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } try{ crypt.encryptCopy(Hex.decode(plainText[i]), -3, plainText[i].length()-3); fail("CryptByteBufferType: "+type.name()+": Expected IllegalArgumentException or " + "ArrayIndexOutOfBoundsException"); }catch(IllegalArgumentException e) { }catch(IndexOutOfBoundsException e){ } } }
@Test public void testDecryptByteArrayIntIntLengthOutOfBounds() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } try{ crypt.decryptCopy(Hex.decode(plainText[i]), 0, plainText[i].length()+3); fail("CryptByteBufferType: "+type.name()+": Expected IllegalArgumentException or " + "ArrayIndexOutOfBoundsException"); }catch(IllegalArgumentException e) { } catch (IndexOutOfBoundsException e){ } } }
@Test public void testDecryptByteArrayIntIntOffsetOutOfBounds() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } try{ crypt.decryptCopy(Hex.decode(plainText[i]), -3, plainText[i].length()-3); fail("CryptByteBufferType: "+type.name()+": Expected IllegalArgumentException or " + "ArrayIndexOutOfBoundsException"); }catch(IllegalArgumentException e) { } catch (IndexOutOfBoundsException e){ } } }
ByteBuffer decipheredtext2 = crypt.decryptCopy(ciphertext2); ByteBuffer decipheredtext3 = crypt.decryptCopy(ciphertext3); assertTrue("CryptByteBufferType: "+type.name(), plain.equals(decipheredtext1)); assertTrue("CryptByteBufferType: "+type.name(), plain.equals(decipheredtext2)); assertTrue("CryptByteBufferType: "+type.name(), plain.equals(decipheredtext3));
@Test public void testSuccessfulRoundTripInPlaceOffset() throws GeneralSecurityException { int header = 5; int footer = 5; for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] originalPlaintext = Hex.decode(plainText[i]); byte[] buffer = new byte[header+originalPlaintext.length+footer]; byte[] copyBuffer = buffer.clone(); System.arraycopy(originalPlaintext, 0, buffer, header, originalPlaintext.length); crypt.encrypt(buffer, footer, originalPlaintext.length); assertTrue(!Arrays.equals(buffer, copyBuffer)); crypt.decrypt(buffer, footer, originalPlaintext.length); assertArrayEquals("CryptByteBufferType: "+type.name(), originalPlaintext, Arrays.copyOfRange(buffer, footer, footer+originalPlaintext.length)); } }
@Test public void testEncryptByteArrayNullInput() throws GeneralSecurityException{ for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] nullArray = null; try{ crypt.encryptCopy(nullArray); fail("CryptByteBufferType: "+type.name()+": Expected NullPointerException"); }catch(NullPointerException e){} } }
@Test public void testDecryptByteArrayNullInput() throws GeneralSecurityException{ for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] nullArray = null; try{ crypt.decryptCopy(nullArray); fail("CryptByteBufferType: "+type.name()+": Expected NullPointerException"); }catch(NullPointerException e){} } }
@Test public void testSuccessfulRoundTripOutOfPlaceOffset() throws GeneralSecurityException { int inHeader = 5; int inFooter = 5; int outHeader = 33; int outFooter = 33; for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] originalPlaintext = Hex.decode(plainText[i]); byte[] buffer = new byte[inHeader+originalPlaintext.length+inFooter]; System.arraycopy(originalPlaintext, 0, buffer, inHeader, originalPlaintext.length); byte[] copyBuffer = buffer.clone(); byte[] outBuffer = new byte[outHeader + originalPlaintext.length + outFooter]; crypt.encrypt(buffer, inFooter, originalPlaintext.length, outBuffer, outHeader); assertTrue(Arrays.equals(buffer, copyBuffer)); copyBuffer = outBuffer.clone(); crypt.decrypt(outBuffer, outHeader, originalPlaintext.length, buffer, inFooter); assertTrue(Arrays.equals(copyBuffer, outBuffer)); assertArrayEquals("CryptByteBufferType: "+type.name(), originalPlaintext, Arrays.copyOfRange(buffer, inFooter, inFooter+originalPlaintext.length)); } }
assertArrayEquals("CryptByteBufferType: "+type.name(), plain, decipheredtext); decipheredtext = crypt.decryptCopy(ciphertext2); assertArrayEquals("CryptByteBufferType2: "+type.name(), plain, decipheredtext); decipheredtext = crypt.decryptCopy(ciphertext3); assertArrayEquals("CryptByteBufferType3: "+type.name(), plain, decipheredtext);
@Test public void testSuccessfulRoundTripInPlace() throws GeneralSecurityException { for(int i = 0; i < cipherTypes.length; i++){ CryptByteBufferType type = cipherTypes[i]; CryptByteBuffer crypt; if(ivs[i] == null){ crypt = new CryptByteBuffer(type, keys[i]); } else { crypt = new CryptByteBuffer(type, keys[i], ivs[i]); } byte[] buffer = Hex.decode(plainText[i]); byte[] plaintextCopy = buffer.clone(); crypt.encrypt(buffer, 0, buffer.length); assertTrue(!Arrays.equals(buffer, plaintextCopy)); crypt.decrypt(buffer, 0, buffer.length); assertArrayEquals("CryptByteBufferType: "+type.name(), plaintextCopy, buffer); } }