package com.google.crypto.tink.prf;
import com.google.crypto.tink.KeyTemplate;
import com.google.crypto.tink.KeyTypeManager;
import com.google.crypto.tink.Registry;
import com.google.crypto.tink.proto.AesCmacPrfKey;
import com.google.crypto.tink.proto.AesCmacPrfKeyFormat;
import com.google.crypto.tink.proto.KeyData.KeyMaterialType;
import com.google.crypto.tink.subtle.PrfAesCmac;
import com.google.crypto.tink.subtle.Random;
import com.google.crypto.tink.subtle.Validators;
import com.google.protobuf.ByteString;
import com.google.protobuf.ExtensionRegistryLite;
import com.google.protobuf.InvalidProtocolBufferException;
import java.security.GeneralSecurityException;
public final class AesCmacPrfKeyManager extends KeyTypeManager<AesCmacPrfKey> {
AesCmacPrfKeyManager() {
super(
AesCmacPrfKey.class,
new PrimitiveFactory<Prf, AesCmacPrfKey>(Prf.class) {
@Override
public Prf getPrimitive(AesCmacPrfKey key) throws GeneralSecurityException {
return new PrfAesCmac(key.getKeyValue().toByteArray());
}
});
}
private static final int VERSION = 0;
private static final int KEY_SIZE_IN_BYTES = 32;
@Override
public String getKeyType() {
return "type.googleapis.com/google.crypto.tink.AesCmacPrfKey";
}
@Override
public int getVersion() {
return VERSION;
}
@Override
public KeyMaterialType keyMaterialType() {
return KeyMaterialType.SYMMETRIC;
}
@Override
public void validateKey(AesCmacPrfKey key) throws GeneralSecurityException {
Validators.validateVersion(key.getVersion(), getVersion());
validateSize(key.getKeyValue().size());
}
@Override
public AesCmacPrfKey parseKey(ByteString byteString) throws InvalidProtocolBufferException {
return AesCmacPrfKey.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry());
}
private static void validateSize(int size) throws GeneralSecurityException {
if (size != KEY_SIZE_IN_BYTES) {
throw new GeneralSecurityException("AesCmacPrfKey size wrong, must be 32 bytes");
}
}
@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();
}
};
}
public static void register(boolean newKeyAllowed) throws GeneralSecurityException {
Registry.registerKeyManager(new AesCmacPrfKeyManager(), newKeyAllowed);
}
public static final KeyTemplate aes256CmacTemplate() {
AesCmacPrfKeyFormat format = AesCmacPrfKeyFormat.newBuilder().setKeySize(32).build();
return KeyTemplate.create(
new AesCmacPrfKeyManager().getKeyType(),
format.toByteArray(),
KeyTemplate.OutputPrefixType.RAW);
}
}