// 
// Decompiled by Procyon v0.6.0
// 

package com.google.crypto.tink.aead;

import com.google.protobuf.MessageLite;
import com.google.protobuf.Parser;
import com.google.crypto.tink.internal.LegacyKeyManagerImpl;
import com.google.crypto.tink.proto.KmsAeadKey;
import com.google.crypto.tink.proto.KeyData;
import com.google.crypto.tink.Parameters;
import com.google.crypto.tink.KeyTemplate;
import com.google.crypto.tink.internal.KeyManagerRegistry;
import com.google.crypto.tink.internal.MutableKeyCreationRegistry;
import com.google.crypto.tink.internal.MutablePrimitiveRegistry;
import com.google.crypto.tink.config.internal.TinkFipsUtil;
import com.google.crypto.tink.AccessesPartialKey;
import javax.annotation.Nullable;
import java.security.GeneralSecurityException;
import com.google.crypto.tink.aead.internal.LegacyFullAead;
import com.google.crypto.tink.KmsClients;
import com.google.crypto.tink.internal.KeyCreator;
import com.google.crypto.tink.KeyManager;
import com.google.crypto.tink.Aead;
import com.google.crypto.tink.internal.PrimitiveConstructor;

public final class KmsAeadKeyManager
{
    private static final PrimitiveConstructor<LegacyKmsAeadKey, Aead> LEGACY_KMS_AEAD_PRIMITIVE_CONSTRUCTOR;
    private static final KeyManager<Aead> legacyKeyManager;
    private static final KeyCreator<LegacyKmsAeadParameters> KEY_CREATOR;
    
    private static Aead create(final LegacyKmsAeadKey key) throws GeneralSecurityException {
        final Aead rawAead = KmsClients.get(key.getParameters().keyUri()).getAead(key.getParameters().keyUri());
        return LegacyFullAead.create(rawAead, key.getOutputPrefix());
    }
    
    @AccessesPartialKey
    private static LegacyKmsAeadKey newKey(final LegacyKmsAeadParameters parameters, @Nullable final Integer idRequirement) throws GeneralSecurityException {
        return LegacyKmsAeadKey.create(parameters, idRequirement);
    }
    
    static String getKeyType() {
        return "type.googleapis.com/google.crypto.tink.KmsAeadKey";
    }
    
    public static void register(final boolean newKeyAllowed) throws GeneralSecurityException {
        if (!TinkFipsUtil.AlgorithmFipsCompatibility.ALGORITHM_NOT_FIPS.isCompatible()) {
            throw new GeneralSecurityException("Registering KMS AEAD is not supported in FIPS mode");
        }
        LegacyKmsAeadProtoSerialization.register();
        MutablePrimitiveRegistry.globalInstance().registerPrimitiveConstructor(KmsAeadKeyManager.LEGACY_KMS_AEAD_PRIMITIVE_CONSTRUCTOR);
        MutableKeyCreationRegistry.globalInstance().add(KmsAeadKeyManager.KEY_CREATOR, LegacyKmsAeadParameters.class);
        KeyManagerRegistry.globalInstance().registerKeyManager(KmsAeadKeyManager.legacyKeyManager, newKeyAllowed);
    }
    
    public static KeyTemplate createKeyTemplate(final String keyUri) {
        try {
            return KeyTemplate.createFrom(LegacyKmsAeadParameters.create(keyUri));
        }
        catch (final GeneralSecurityException e) {
            throw new IllegalArgumentException(e);
        }
    }
    
    private KmsAeadKeyManager() {
    }
    
    static {
        LEGACY_KMS_AEAD_PRIMITIVE_CONSTRUCTOR = PrimitiveConstructor.create(KmsAeadKeyManager::create, LegacyKmsAeadKey.class, Aead.class);
        legacyKeyManager = LegacyKeyManagerImpl.create(getKeyType(), Aead.class, KeyData.KeyMaterialType.REMOTE, KmsAeadKey.parser());
        KEY_CREATOR = KmsAeadKeyManager::newKey;
    }
}
