AesCmacPrfKeyManager extends KeyTypeManager<AesCmacPrfKey> { @Override public KeyFactory<AesCmacPrfKeyFormat, AesCmacPrfKey> keyFactory() { return new KeyFactory<AesCmacPrfKeyFormat, AesCmacPrfKey>(AesCmacPrfKeyFormat.class) { @Override public void validateKeyFormat(AesCmacPrfKeyFormat format) throws GeneralSecurityException { validateSize(format.getKeySize()); } @Override public AesCmacPrfKeyFormat parseKeyFormat(ByteString byteString) throws InvalidProtocolBufferException { return AesCmacPrfKeyFormat.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry()); } @Override public AesCmacPrfKey createKey(AesCmacPrfKeyFormat format) { return AesCmacPrfKey.newBuilder() .setVersion(VERSION) .setKeyValue(ByteString.copyFrom(Random.randBytes(format.getKeySize()))) .build(); } }; } AesCmacPrfKeyManager(); @Override String getKeyType(); @Override int getVersion(); @Override KeyMaterialType keyMaterialType(); @Override void validateKey(AesCmacPrfKey key); @Override AesCmacPrfKey parseKey(ByteString byteString); @Override KeyFactory<AesCmacPrfKeyFormat, AesCmacPrfKey> keyFactory(); static void register(boolean newKeyAllowed); static final KeyTemplate aes256CmacTemplate();  }
@Test public void validateKeyFormat_empty() throws Exception { try { new AesCmacPrfKeyManager() .keyFactory() .validateKeyFormat(AesCmacPrfKeyFormat.getDefaultInstance()); fail(); } catch (GeneralSecurityException e) { } }
@Test public void validateKeyFormat_valid() throws Exception { AesCmacPrfKeyManager manager = new AesCmacPrfKeyManager(); manager.keyFactory().validateKeyFormat(makeAesCmacPrfKeyFormat(32)); }
@Test public void validateKeyFormat_notValid_throws() throws Exception { AesCmacPrfKeyManager manager = new AesCmacPrfKeyManager(); try { manager.keyFactory().validateKeyFormat(makeAesCmacPrfKeyFormat(31)); fail(); } catch (GeneralSecurityException e) { } try { manager.keyFactory().validateKeyFormat(makeAesCmacPrfKeyFormat(16)); fail(); } catch (GeneralSecurityException e) { } }
@Test public void createKey_checkValues() throws Exception { AesCmacPrfKeyFormat keyFormat = makeAesCmacPrfKeyFormat(32); AesCmacPrfKey key = new AesCmacPrfKeyManager().keyFactory().createKey(keyFormat); assertThat(key.getKeyValue()).hasSize(keyFormat.getKeySize()); }
@Test public void createKey_multipleTimes() throws Exception { AesCmacPrfKeyManager manager = new AesCmacPrfKeyManager(); AesCmacPrfKeyFormat keyFormat = makeAesCmacPrfKeyFormat(32); assertThat(manager.keyFactory().createKey(keyFormat).getKeyValue()) .isNotEqualTo(manager.keyFactory().createKey(keyFormat).getKeyValue()); }
@Test public void getPrimitive_works() throws Exception { AesCmacPrfKeyManager manager = new AesCmacPrfKeyManager(); AesCmacPrfKey validKey = manager.keyFactory().createKey(makeAesCmacPrfKeyFormat(32)); PrfSet managerPrf = manager.getPrimitive(validKey, PrfSet.class); Prf directPrf = new PrfAesCmac(validKey.getKeyValue().toByteArray()); byte[] message = Random.randBytes(50); assertThat(managerPrf.computePrimary(message, 16)).isEqualTo(directPrf.compute(message, 16)); }