/** * Derives a IvParameterSpec of the specified type from the MasterSecret. * @param type The type of iv to derive * @return The derived iv */ public IvParameterSpec deriveIv(KeyType type){ try { return KeyGenUtils.deriveIvParameterSpec(masterKey, getClass(), type.name()+" iv", type); } catch (InvalidKeyException e) { throw new IllegalStateException(e); // Definitely a bug. } }
private void setupKeys() { ParametersWithIV tempPram = null; try{ KeyParameter cipherKey = new KeyParameter(KeyGenUtils.deriveSecretKey(unencryptedBaseKey, EncryptedRandomAccessBuffer.class, kdfInput.underlyingKey.input, type.encryptKey).getEncoded()); tempPram = new ParametersWithIV(cipherKey, KeyGenUtils.deriveIvParameterSpec(unencryptedBaseKey, EncryptedRandomAccessBuffer.class, kdfInput.underlyingIV.input, type.encryptKey).getIV()); } catch(InvalidKeyException e) { throw new IllegalStateException(e); // Must be a bug. } this.cipherParams = tempPram; }
@Test (expected = InvalidKeyException.class) public void testDeriveIvParameterSpecNullInput1() throws InvalidKeyException { SecretKey kdfKey = null; KeyGenUtils.deriveIvParameterSpec(kdfKey, KeyGenUtils.class, kdfInput, KeyType.ChaCha128); }
@Test (expected = NullPointerException.class) public void testDeriveIvParameterSpecNullInput3() throws InvalidKeyException{ SecretKey kdfKey = KeyGenUtils.getSecretKey(KeyType.HMACSHA512, trueLengthSecretKeys[6]); KeyGenUtils.deriveIvParameterSpec(kdfKey, KeyGenUtils.class, null, KeyType.ChaCha128); }
@Test (expected = NullPointerException.class) public void testDeriveIvParameterSpecNullInput2() throws InvalidKeyException{ SecretKey kdfKey = KeyGenUtils.getSecretKey(KeyType.HMACSHA512, trueLengthSecretKeys[6]); KeyGenUtils.deriveIvParameterSpec(kdfKey, null, kdfInput, KeyType.ChaCha128); }
@Test (expected = NullPointerException.class) public void testDeriveIvParameterSpecNullInput4() throws InvalidKeyException{ SecretKey kdfKey = KeyGenUtils.getSecretKey(KeyType.HMACSHA512, trueLengthSecretKeys[6]); KeyGenUtils.deriveIvParameterSpec(kdfKey, KeyGenUtils.class, kdfInput, null); } }
type.encryptKey).getEncoded()); tempPram = new ParametersWithIV(cipherKey, KeyGenUtils.deriveIvParameterSpec(unencryptedBaseKey, getClass(), kdfInput.underlyingIV.input, type.encryptKey).getIV()); } catch(InvalidKeyException e) {
@Test public void testDeriveIvParameterSpec() throws InvalidKeyException{ SecretKey kdfKey = KeyGenUtils.getSecretKey(KeyType.HMACSHA512, trueLengthSecretKeys[6]); IvParameterSpec buf1 = KeyGenUtils.deriveIvParameterSpec(kdfKey, KeyGenUtils.class, kdfInput, KeyType.ChaCha128); IvParameterSpec buf2 = KeyGenUtils.deriveIvParameterSpec(kdfKey, KeyGenUtils.class, kdfInput, KeyType.ChaCha128); assertNotNull(buf1); assertArrayEquals(buf1.getIV(), buf2.getIV()); }
@Test public void testDeriveIvParameterSpecLength() throws InvalidKeyException{ for(KeyType type: keyTypes){ SecretKey kdfKey = KeyGenUtils.getSecretKey(KeyType.HMACSHA512, trueLengthSecretKeys[6]); IvParameterSpec buf1 = KeyGenUtils.deriveIvParameterSpec(kdfKey, KeyGenUtils.class, kdfInput, type); assertEquals(buf1.getIV().length, type.ivSize >> 3); } }