HkdfPrfKeyManager extends KeyTypeManager<HkdfPrfKey> { @Override public void validateKey(HkdfPrfKey key) throws GeneralSecurityException { Validators.validateVersion(key.getVersion(), getVersion()); validateKeySize(key.getKeyValue().size()); validateParams(key.getParams()); } HkdfPrfKeyManager(); @Override String getKeyType(); @Override int getVersion(); @Override KeyMaterialType keyMaterialType(); @Override void validateKey(HkdfPrfKey key); @Override HkdfPrfKey parseKey(ByteString byteString); @Override KeyFactory<HkdfPrfKeyFormat, HkdfPrfKey> keyFactory(); static void register(boolean newKeyAllowed); static String staticKeyType(); static final KeyTemplate hkdfSha256Template();  }
@Test public void validateKey_empty_throws() throws Exception { HkdfPrfKey key = HkdfPrfKey.getDefaultInstance(); try { manager.validateKey(key); fail(); } catch (GeneralSecurityException e) { } }
@Test public void validateKey_valid32ByteKey_works() throws Exception { manager.validateKey( HkdfPrfKey.newBuilder() .setVersion(0) .setKeyValue(ByteString.copyFrom(Random.randBytes(32))) .setParams(HkdfPrfParams.newBuilder().setHash(HashType.SHA256)) .build()); }
@Test public void validateKey_invalid31ByteKey_throws() throws Exception { HkdfPrfKey key = HkdfPrfKey.newBuilder() .setVersion(0) .setKeyValue(ByteString.copyFrom(Random.randBytes(31))) .setParams(HkdfPrfParams.newBuilder().setHash(HashType.SHA256)) .build(); try { manager.validateKey(key); fail(); } catch (GeneralSecurityException e) { } }
@Test public void validateKey_validSha512Key_works() throws Exception { manager.validateKey( HkdfPrfKey.newBuilder() .setVersion(0) .setKeyValue(ByteString.copyFrom(Random.randBytes(32))) .setParams(HkdfPrfParams.newBuilder().setHash(HashType.SHA512)) .build()); }
@Test public void validateKey_valid33ByteKey_works() throws Exception { manager.validateKey( HkdfPrfKey.newBuilder() .setVersion(0) .setKeyValue(ByteString.copyFrom(Random.randBytes(33))) .setParams(HkdfPrfParams.newBuilder().setHash(HashType.SHA256)) .build()); }
@Test public void validateKey_validKeyWithSalt_works() throws Exception { manager.validateKey( HkdfPrfKey.newBuilder() .setVersion(0) .setKeyValue(ByteString.copyFrom(Random.randBytes(32))) .setParams( HkdfPrfParams.newBuilder() .setSalt(ByteString.copyFrom(Random.randBytes(5))) .setHash(HashType.SHA256)) .build()); }
@Test public void validateKey_invalidSha1Key_throws() throws Exception { HkdfPrfKey key = HkdfPrfKey.newBuilder() .setVersion(0) .setKeyValue(ByteString.copyFrom(Random.randBytes(32))) .setParams(HkdfPrfParams.newBuilder().setHash(HashType.SHA1)) .build(); try { manager.validateKey(key); fail(); } catch (GeneralSecurityException e) { } }
@Test public void validateKey_invalidKeyVersion_throws() throws Exception { HkdfPrfKey key = HkdfPrfKey.newBuilder() .setVersion(1) .setKeyValue(ByteString.copyFrom(Random.randBytes(32))) .setParams(HkdfPrfParams.newBuilder().setHash(HashType.SHA256)) .build(); try { manager.validateKey(key); fail(); } catch (GeneralSecurityException e) { } }