AesCmacKeyManager extends KeyTypeManager<AesCmacKey> { @Override public KeyFactory<AesCmacKeyFormat, AesCmacKey> keyFactory() { return new KeyFactory<AesCmacKeyFormat, AesCmacKey>(AesCmacKeyFormat.class) { @Override public void validateKeyFormat(AesCmacKeyFormat format) throws GeneralSecurityException { validateParams(format.getParams()); validateSize(format.getKeySize()); } @Override public AesCmacKeyFormat parseKeyFormat(ByteString byteString) throws InvalidProtocolBufferException { return AesCmacKeyFormat.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry()); } @Override public AesCmacKey createKey(AesCmacKeyFormat format) throws GeneralSecurityException { return AesCmacKey.newBuilder() .setVersion(VERSION) .setKeyValue(ByteString.copyFrom(Random.randBytes(format.getKeySize()))) .setParams(format.getParams()) .build(); } }; } AesCmacKeyManager(); @Override String getKeyType(); @Override int getVersion(); @Override KeyMaterialType keyMaterialType(); @Override void validateKey(AesCmacKey key); @Override AesCmacKey parseKey(ByteString byteString); @Override KeyFactory<AesCmacKeyFormat, AesCmacKey> keyFactory(); static void register(boolean newKeyAllowed); static final KeyTemplate aes256CmacTemplate(); static final KeyTemplate rawAes256CmacTemplate();  }
@Test public void validateKeyFormat_empty() throws Exception { try { new AesCmacKeyManager().keyFactory().validateKeyFormat(AesCmacKeyFormat.getDefaultInstance()); fail(); } catch (GeneralSecurityException e) { } }
@Test public void validateKeyFormat_valid() throws Exception { AesCmacKeyManager manager = new AesCmacKeyManager(); manager.keyFactory().validateKeyFormat(makeAesCmacKeyFormat(32, 10)); manager.keyFactory().validateKeyFormat(makeAesCmacKeyFormat(32, 11)); manager.keyFactory().validateKeyFormat(makeAesCmacKeyFormat(32, 12)); manager.keyFactory().validateKeyFormat(makeAesCmacKeyFormat(32, 13)); manager.keyFactory().validateKeyFormat(makeAesCmacKeyFormat(32, 14)); manager.keyFactory().validateKeyFormat(makeAesCmacKeyFormat(32, 15)); manager.keyFactory().validateKeyFormat(makeAesCmacKeyFormat(32, 16)); }
@Test public void validateKeyFormat_notValid_throws() throws Exception { AesCmacKeyManager manager = new AesCmacKeyManager(); try { manager.keyFactory().validateKeyFormat(makeAesCmacKeyFormat(32, 9)); fail(); } catch (GeneralSecurityException e) { } try { manager.keyFactory().validateKeyFormat(makeAesCmacKeyFormat(32, 17)); fail(); } catch (GeneralSecurityException e) { } try { manager.keyFactory().validateKeyFormat(makeAesCmacKeyFormat(32, 32)); fail(); } catch (GeneralSecurityException e) { } try { manager.keyFactory().validateKeyFormat(makeAesCmacKeyFormat(16, 16)); fail(); } catch (GeneralSecurityException e) { } try { manager.keyFactory().validateKeyFormat(makeAesCmacKeyFormat(64, 16)); fail(); } catch (GeneralSecurityException e) { } }
@Test public void createKey_checkValues() throws Exception { AesCmacKeyFormat keyFormat = makeAesCmacKeyFormat(32, 16); AesCmacKey key = new AesCmacKeyManager().keyFactory().createKey(keyFormat); assertThat(key.getKeyValue()).hasSize(keyFormat.getKeySize()); assertThat(key.getParams().getTagSize()).isEqualTo(keyFormat.getParams().getTagSize()); }
@Test public void createKey_multipleTimes() throws Exception { AesCmacKeyManager manager = new AesCmacKeyManager(); AesCmacKeyFormat keyFormat = makeAesCmacKeyFormat(32, 16); assertThat(manager.keyFactory().createKey(keyFormat).getKeyValue()) .isNotEqualTo(manager.keyFactory().createKey(keyFormat).getKeyValue()); }
@Test public void getPrimitive_works() throws Exception { AesCmacKeyManager manager = new AesCmacKeyManager(); AesCmacKey validKey = manager.keyFactory().createKey(makeAesCmacKeyFormat(32, 16)); Mac managerMac = manager.getPrimitive(validKey, Mac.class); Mac directMac = new PrfMac( new PrfAesCmac(validKey.getKeyValue().toByteArray()), validKey.getParams().getTagSize()); byte[] message = Random.randBytes(50); managerMac.verifyMac(directMac.computeMac(message), message); }