package com.google.crypto.tink.aead;
import com.google.crypto.tink.Aead;
import com.google.crypto.tink.KeyTypeManager;
import com.google.crypto.tink.Mac;
import com.google.crypto.tink.Registry;
import com.google.crypto.tink.mac.HmacKeyManager;
import com.google.crypto.tink.proto.AesCtrHmacAeadKey;
import com.google.crypto.tink.proto.AesCtrHmacAeadKeyFormat;
import com.google.crypto.tink.proto.AesCtrKey;
import com.google.crypto.tink.proto.HmacKey;
import com.google.crypto.tink.proto.KeyData.KeyMaterialType;
import com.google.crypto.tink.subtle.EncryptThenAuthenticate;
import com.google.crypto.tink.subtle.IndCpaCipher;
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 class AesCtrHmacAeadKeyManager extends KeyTypeManager<AesCtrHmacAeadKey> {
AesCtrHmacAeadKeyManager() {
super(
AesCtrHmacAeadKey.class,
new PrimitiveFactory<Aead, AesCtrHmacAeadKey>(Aead.class) {
@Override
public Aead getPrimitive(AesCtrHmacAeadKey key) throws GeneralSecurityException {
return new EncryptThenAuthenticate(
new AesCtrKeyManager().getPrimitive(key.getAesCtrKey(), IndCpaCipher.class),
new HmacKeyManager().getPrimitive(key.getHmacKey(), Mac.class),
key.getHmacKey().getParams().getTagSize());
}
});
}
@Override
public String getKeyType() {
return "type.googleapis.com/google.crypto.tink.AesCtrHmacAeadKey";
}
@Override
public int getVersion() {
return 0;
}
@Override
public KeyMaterialType keyMaterialType() {
return KeyMaterialType.SYMMETRIC;
}
@Override
public void validateKey(AesCtrHmacAeadKey key) throws GeneralSecurityException {
Validators.validateVersion(key.getVersion(), getVersion());
}
@Override
public AesCtrHmacAeadKey parseKey(ByteString byteString) throws InvalidProtocolBufferException {
return AesCtrHmacAeadKey.parseFrom(byteString, ExtensionRegistryLite.getEmptyRegistry());
}
@Override
public KeyFactory<AesCtrHmacAeadKeyFormat, AesCtrHmacAeadKey> keyFactory() {
return new KeyFactory<AesCtrHmacAeadKeyFormat, AesCtrHmacAeadKey>(
AesCtrHmacAeadKeyFormat.class) {
@Override
public void validateKeyFormat(AesCtrHmacAeadKeyFormat format)
throws GeneralSecurityException {
new AesCtrKeyManager().keyFactory().validateKeyFormat(format.getAesCtrKeyFormat());
new HmacKeyManager().keyFactory().validateKeyFormat(format.getHmacKeyFormat());
Validators.validateAesKeySize(format.getAesCtrKeyFormat().getKeySize());
}
@Override
public AesCtrHmacAeadKeyFormat parseKeyFormat(ByteString byteString)
throws InvalidProtocolBufferException {
return AesCtrHmacAeadKeyFormat.parseFrom(
byteString, ExtensionRegistryLite.getEmptyRegistry());
}
@Override
public AesCtrHmacAeadKey createKey(AesCtrHmacAeadKeyFormat format)
throws GeneralSecurityException {
AesCtrKey aesCtrKey =
new AesCtrKeyManager().keyFactory().createKey(format.getAesCtrKeyFormat());
HmacKey hmacKey = new HmacKeyManager().keyFactory().createKey(format.getHmacKeyFormat());
return AesCtrHmacAeadKey.newBuilder()
.setAesCtrKey(aesCtrKey)
.setHmacKey(hmacKey)
.setVersion(getVersion())
.build();
}
};
}
public static void register(boolean newKeyAllowed) throws GeneralSecurityException {
Registry.registerKeyManager(new AesCtrHmacAeadKeyManager(), newKeyAllowed);
}
}