/** * Creates an instance of MessageAuthCode that will use the specified algorithm and * key which is converted from a byte[] to a SecretKey. Must not be used on algorithms that * require an IV, as a specified key but a random IV is probably not useful for an HMAC. * @param type The MAC algorithm to use * @param cryptoKey The key to use * @throws InvalidKeyException */ public MessageAuthCode(MACType type, byte[] cryptoKey) throws InvalidKeyException { this(type, KeyGenUtils.getSecretKey(type.keyType, cryptoKey)); }
/** * Creates an instance of CryptByteBuffer that will be able to encrypt and decrypt * sets of bytes using the specified algorithm type with the given key. If the * algorithm requires an iv, it will generate a random one. * @param type The symmetric algorithm, mode, and key and block size to use * @param key The key that will be used for encryption * @throws InvalidAlgorithmParameterException * @throws InvalidKeyException */ public CryptByteBuffer(CryptByteBufferType type, byte[] key) throws GeneralSecurityException{ this(type, KeyGenUtils.getSecretKey(type.keyType, key)); }
/** * Creates an instance of MessageAuthCode that will use the specified algorithm with * the specified key and iv. The specified algorithm must require an iv. * @param key They key to be used as a byte[] * @param iv The iv to be used * @throws InvalidKeyException * @throws InvalidAlgorithmParameterException */ public MessageAuthCode(MACType type, byte[] key, IvParameterSpec iv) throws InvalidKeyException, InvalidAlgorithmParameterException{ this(type, KeyGenUtils.getSecretKey(type.keyType, key), iv); }
/** * Creates an instance of CryptByteBuffer that will be able to encrypt and decrypt * sets of bytes using the specified algorithm type with the given key and iv. * The iv will be extracted from the passed in byte[] starting at the offset * using the length provided by type.ivSize * @param type The symmetric algorithm, mode, and key and block size to use * @param key The key that will be used for encryption * @param iv The byte[] containing the iv * @param offset Where in the byte[] the iv starts * @throws InvalidKeyException * @throws InvalidAlgorithmParameterException */ public CryptByteBuffer(CryptByteBufferType type, byte[] key, byte[] iv, int offset) throws InvalidKeyException, InvalidAlgorithmParameterException{ this(type, KeyGenUtils.getSecretKey(type.keyType, key), iv, offset); }
public MasterSecret(byte[] secret) { if(secret.length != 64) throw new IllegalArgumentException(); masterKey = KeyGenUtils.getSecretKey(KeyType.HMACSHA512, secret); }
/** * Converts the specified key into a SecretKey for the specified algorithm * @param key The ByteBuffer of the key * @param type Type of key * @return The key as a SecretKey */ public static SecretKey getSecretKey(KeyType type, ByteBuffer key){ return getSecretKey(type, Fields.copyToArray(key)); }
/** * Derives a SecretKey of the specified type from the given key using the provided class name * and kdfString * @param kdfKey The key to derive from * @param c Class name to use in derivation * @param kdfString String to use in derivation * @param type The type of key to derive * @return The derived key as a SecretKey * @throws InvalidKeyException */ public static SecretKey deriveSecretKey(SecretKey kdfKey, Class<?> c, String kdfString, KeyType type) throws InvalidKeyException{ return getSecretKey(type, deriveBytesTruncated(kdfKey, c, kdfString, type.keySize >> 3)); }
@Test (expected = NullPointerException.class) public void testGetSecretKeyNullInput1() { byte[] nullArray = null; KeyGenUtils.getSecretKey(keyTypes[1], nullArray); }
@Test (expected = NullPointerException.class) public void testGetSecretKeyNullInput2() { KeyGenUtils.getSecretKey(null, trueSecretKeys[0]); }
@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 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 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 (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); } }
@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); } }
@Test public void testGetSecretKey() { for(int i = 0; i < keyTypes.length; i++){ KeyType type = keyTypes[i]; SecretKey newKey = KeyGenUtils.getSecretKey(type, trueLengthSecretKeys[i]); assertArrayEquals("KeyType: "+type.name(), trueLengthSecretKeys[i], newKey.getEncoded()); } }
@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 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); } }
@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()); }