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

package com.google.crypto.tink.keyderivation.internal;

import com.google.crypto.tink.internal.MutablePrimitiveRegistry;
import com.google.crypto.tink.internal.KeyManagerRegistry;
import com.google.crypto.tink.InsecureSecretKeyAccess;
import com.google.crypto.tink.internal.ProtoKeySerialization;
import com.google.crypto.tink.internal.MutableSerializationRegistry;
import com.google.crypto.tink.internal.ProtoParametersSerialization;
import com.google.crypto.tink.proto.KeyTemplate;
import com.google.crypto.tink.proto.PrfBasedDeriverKeyFormat;
import com.google.crypto.tink.proto.OutputPrefixType;
import com.google.crypto.tink.proto.KeyData;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.crypto.tink.proto.PrfBasedDeriverKey;
import com.google.protobuf.ExtensionRegistryLite;
import com.google.protobuf.MessageLite;
import com.google.protobuf.ByteString;
import com.google.crypto.tink.AccessesPartialKey;
import com.google.crypto.tink.Key;
import java.security.GeneralSecurityException;
import com.google.crypto.tink.prf.PrfKey;
import com.google.crypto.tink.Parameters;
import com.google.crypto.tink.internal.MutableKeyCreationRegistry;
import javax.annotation.Nullable;
import com.google.crypto.tink.keyderivation.PrfBasedKeyDerivationParameters;
import com.google.crypto.tink.internal.KeyCreator;
import com.google.crypto.tink.keyderivation.PrfBasedKeyDerivationKey;
import com.google.crypto.tink.internal.PrimitiveConstructor;
import com.google.crypto.tink.KeyManager;

public final class PrfBasedDeriverKeyManager implements KeyManager<Void>
{
    private static final PrimitiveConstructor<PrfBasedKeyDerivationKey, KeyDeriver> PRIMITIVE_CONSTRUCTOR;
    private static final KeyCreator<PrfBasedKeyDerivationParameters> KEY_CREATOR;
    private static final String TYPE_URL = "type.googleapis.com/google.crypto.tink.PrfBasedDeriverKey";
    
    @AccessesPartialKey
    private static final PrfBasedKeyDerivationKey createNewKey(final PrfBasedKeyDerivationParameters parameters, @Nullable final Integer idRequirement) throws GeneralSecurityException {
        final Key prfKey = MutableKeyCreationRegistry.globalInstance().createKey(parameters.getPrfParameters(), null);
        if (!(prfKey instanceof PrfKey)) {
            throw new GeneralSecurityException("Failed to create PrfKey from parameters" + parameters.getPrfParameters() + ", instead got " + prfKey.getClass());
        }
        return PrfBasedKeyDerivationKey.create(parameters, (PrfKey)prfKey, idRequirement);
    }
    
    PrfBasedDeriverKeyManager() {
    }
    
    @Override
    public Void getPrimitive(final ByteString serializedKey) throws GeneralSecurityException {
        throw new GeneralSecurityException("Cannot use the KeyManager to get a primitive for KeyDerivation");
    }
    
    @Override
    public final Void getPrimitive(final MessageLite key) throws GeneralSecurityException {
        throw new GeneralSecurityException("Cannot use the KeyManager to get a primitive for KeyDerivation");
    }
    
    @Override
    public final MessageLite newKey(final ByteString serializedKeyFormat) throws GeneralSecurityException {
        final KeyData keyData = this.newKeyData(serializedKeyFormat);
        try {
            return PrfBasedDeriverKey.parseFrom(keyData.getValue(), ExtensionRegistryLite.getEmptyRegistry());
        }
        catch (final InvalidProtocolBufferException e) {
            throw new GeneralSecurityException("Unexpectedly failed to parse key");
        }
    }
    
    @Override
    public final MessageLite newKey(final MessageLite keyFormat) throws GeneralSecurityException {
        return this.newKey(keyFormat.toByteString());
    }
    
    @Override
    public final boolean doesSupport(final String typeUrl) {
        return typeUrl.equals(this.getKeyType());
    }
    
    @Override
    public final String getKeyType() {
        return "type.googleapis.com/google.crypto.tink.PrfBasedDeriverKey";
    }
    
    @Override
    public int getVersion() {
        return 0;
    }
    
    private static OutputPrefixType getOutputPrefixTypeFromSerializedKeyFormat(final ByteString serializedKeyFormat) throws GeneralSecurityException {
        try {
            final PrfBasedDeriverKeyFormat format = PrfBasedDeriverKeyFormat.parseFrom(serializedKeyFormat, ExtensionRegistryLite.getEmptyRegistry());
            return format.getParams().getDerivedKeyTemplate().getOutputPrefixType();
        }
        catch (final InvalidProtocolBufferException e) {
            throw new GeneralSecurityException("Unexpectedly failed to parse key format", e);
        }
    }
    
    @Override
    public final KeyData newKeyData(final ByteString serializedKeyFormat) throws GeneralSecurityException {
        final OutputPrefixType outputPrefixType = getOutputPrefixTypeFromSerializedKeyFormat(serializedKeyFormat);
        final ProtoParametersSerialization parametersSerialization = ProtoParametersSerialization.checkedCreate(KeyTemplate.newBuilder().setTypeUrl("type.googleapis.com/google.crypto.tink.PrfBasedDeriverKey").setValue(serializedKeyFormat).setOutputPrefixType(outputPrefixType).build());
        final Parameters parameters = MutableSerializationRegistry.globalInstance().parseParameters(parametersSerialization);
        Integer idRequirement = null;
        if (!outputPrefixType.equals(OutputPrefixType.RAW)) {
            idRequirement = 123;
        }
        final Key key = MutableKeyCreationRegistry.globalInstance().createKey(parameters, idRequirement);
        final ProtoKeySerialization keySerialization = MutableSerializationRegistry.globalInstance().serializeKey(key, ProtoKeySerialization.class, InsecureSecretKeyAccess.get());
        return KeyData.newBuilder().setTypeUrl(keySerialization.getTypeUrl()).setValue(keySerialization.getValue()).setKeyMaterialType(keySerialization.getKeyMaterialType()).build();
    }
    
    @Override
    public final Class<Void> getPrimitiveClass() {
        return Void.class;
    }
    
    public static void register(final boolean newKeyAllowed) throws GeneralSecurityException {
        KeyManagerRegistry.globalInstance().registerKeyManager((KeyManager<Object>)new PrfBasedDeriverKeyManager(), newKeyAllowed);
        MutableKeyCreationRegistry.globalInstance().add(PrfBasedDeriverKeyManager.KEY_CREATOR, PrfBasedKeyDerivationParameters.class);
        MutablePrimitiveRegistry.globalInstance().registerPrimitiveConstructor(PrfBasedDeriverKeyManager.PRIMITIVE_CONSTRUCTOR);
        PrfBasedKeyDerivationKeyProtoSerialization.register();
    }
    
    static {
        PRIMITIVE_CONSTRUCTOR = PrimitiveConstructor.create(PrfBasedKeyDeriver::create, PrfBasedKeyDerivationKey.class, KeyDeriver.class);
        KEY_CREATOR = PrfBasedDeriverKeyManager::createNewKey;
    }
}
