/** * Derives a SecretKey of the specified type from the MasterSecret. * @param type The type of key to derive * @return The derived key */ public SecretKey deriveKey(KeyType type){ try { return KeyGenUtils.deriveSecretKey(masterKey, getClass(), type.name()+" key", 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 testDeriveSecretKeyNullInput1() throws InvalidKeyException { SecretKey kdfKey = null; KeyGenUtils.deriveSecretKey(kdfKey, KeyGenUtils.class, kdfInput, KeyType.ChaCha128); }
@Test public void testDeriveSecretKey() throws InvalidKeyException{ SecretKey kdfKey = KeyGenUtils.getSecretKey(KeyType.HMACSHA512, trueLengthSecretKeys[6]); SecretKey buf1 = KeyGenUtils.deriveSecretKey(kdfKey, KeyGenUtils.class, kdfInput, KeyType.HMACSHA512); SecretKey buf2 = KeyGenUtils.deriveSecretKey(kdfKey, KeyGenUtils.class, kdfInput, KeyType.HMACSHA512); assertNotNull(buf1); assertTrue(buf1.equals(buf2)); }
@Test (expected = NullPointerException.class) public void testDeriveSecretKeyNullInput2() throws InvalidKeyException{ SecretKey kdfKey = KeyGenUtils.getSecretKey(KeyType.HMACSHA512, trueLengthSecretKeys[6]); KeyGenUtils.deriveSecretKey(kdfKey, null, kdfInput, KeyType.ChaCha128); }
@Test (expected = NullPointerException.class) public void testDeriveSecretKeyNullInput3() throws InvalidKeyException{ SecretKey kdfKey = KeyGenUtils.getSecretKey(KeyType.HMACSHA512, trueLengthSecretKeys[6]); KeyGenUtils.deriveSecretKey(kdfKey, KeyGenUtils.class, null, KeyType.ChaCha128); }
@Test (expected = NullPointerException.class) public void testDeriveSecretKeyNullInput4() throws InvalidKeyException{ SecretKey kdfKey = KeyGenUtils.getSecretKey(KeyType.HMACSHA512, trueLengthSecretKeys[6]); KeyGenUtils.deriveSecretKey(kdfKey, KeyGenUtils.class, kdfInput, null); }
@Test public void testDeriveSecretKeyLength() throws InvalidKeyException{ for(KeyType type: keyTypes){ SecretKey kdfKey = KeyGenUtils.getSecretKey(KeyType.HMACSHA512, trueLengthSecretKeys[6]); SecretKey buf1 = KeyGenUtils.deriveSecretKey(kdfKey, KeyGenUtils.class, kdfInput, type); assertEquals(buf1.getEncoded().length, type.keySize >> 3); } }
KeyParameter cipherKey = new KeyParameter(KeyGenUtils.deriveSecretKey(unencryptedBaseKey, getClass(), kdfInput.underlyingKey.input, type.encryptKey).getEncoded());