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

package com.google.crypto.tink.prf;

import com.google.protobuf.MessageLite;
import com.google.protobuf.Parser;
import com.google.crypto.tink.internal.LegacyKeyManagerImpl;
import com.google.crypto.tink.proto.KeyData;
import com.google.crypto.tink.subtle.PrfHmacJce;
import com.google.crypto.tink.internal.TinkBugException;
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.MutableParametersRegistry;
import com.google.crypto.tink.internal.MutablePrimitiveRegistry;
import com.google.crypto.tink.prf.internal.HmacPrfProtoSerialization;
import java.util.Collections;
import java.util.HashMap;
import com.google.crypto.tink.Parameters;
import java.util.Map;
import com.google.crypto.tink.internal.Util;
import com.google.crypto.tink.SecretKeyAccess;
import java.io.InputStream;
import com.google.crypto.tink.AccessesPartialKey;
import com.google.crypto.tink.util.SecretBytes;
import java.security.GeneralSecurityException;
import javax.annotation.Nullable;
import com.google.crypto.tink.config.internal.TinkFipsUtil;
import com.google.crypto.tink.internal.MutableKeyDerivationRegistry;
import com.google.crypto.tink.internal.KeyCreator;
import com.google.crypto.tink.KeyManager;
import com.google.crypto.tink.internal.PrimitiveConstructor;

public final class HmacPrfKeyManager
{
    private static final PrimitiveConstructor<HmacPrfKey, Prf> PRF_PRIMITIVE_CONSTRUCTOR;
    private static final KeyManager<Prf> legacyKeyManager;
    private static final KeyCreator<HmacPrfParameters> KEY_CREATOR;
    private static final MutableKeyDerivationRegistry.InsecureKeyCreator<HmacPrfParameters> KEY_DERIVER;
    private static final TinkFipsUtil.AlgorithmFipsCompatibility FIPS;
    
    @AccessesPartialKey
    private static HmacPrfKey newKey(final HmacPrfParameters parameters, @Nullable final Integer idRequirement) throws GeneralSecurityException {
        if (idRequirement != null) {
            throw new GeneralSecurityException("Id Requirement is not supported for HMAC PRF keys");
        }
        return HmacPrfKey.builder().setParameters(parameters).setKeyBytes(SecretBytes.randomBytes(parameters.getKeySizeBytes())).build();
    }
    
    static String getKeyType() {
        return "type.googleapis.com/google.crypto.tink.HmacPrfKey";
    }
    
    @AccessesPartialKey
    static HmacPrfKey createHmacKeyFromRandomness(final HmacPrfParameters parameters, final InputStream stream, @Nullable final Integer idRequirement, final SecretKeyAccess access) throws GeneralSecurityException {
        return HmacPrfKey.builder().setParameters(parameters).setKeyBytes(Util.readIntoSecretBytes(stream, parameters.getKeySizeBytes(), access)).build();
    }
    
    private static Map<String, Parameters> namedParameters() throws GeneralSecurityException {
        final Map<String, Parameters> result = new HashMap<String, Parameters>();
        result.put("HMAC_SHA256_PRF", PredefinedPrfParameters.HMAC_SHA256_PRF);
        result.put("HMAC_SHA512_PRF", PredefinedPrfParameters.HMAC_SHA512_PRF);
        return Collections.unmodifiableMap((Map<? extends String, ? extends Parameters>)result);
    }
    
    public static void register(final boolean newKeyAllowed) throws GeneralSecurityException {
        if (!HmacPrfKeyManager.FIPS.isCompatible()) {
            throw new GeneralSecurityException("Can not use HMAC in FIPS-mode, as BoringCrypto module is not available.");
        }
        HmacPrfProtoSerialization.register();
        MutablePrimitiveRegistry.globalInstance().registerPrimitiveConstructor(HmacPrfKeyManager.PRF_PRIMITIVE_CONSTRUCTOR);
        MutableParametersRegistry.globalInstance().putAll(namedParameters());
        MutableKeyCreationRegistry.globalInstance().add(HmacPrfKeyManager.KEY_CREATOR, HmacPrfParameters.class);
        MutableKeyDerivationRegistry.globalInstance().add(HmacPrfKeyManager.KEY_DERIVER, HmacPrfParameters.class);
        KeyManagerRegistry.globalInstance().registerKeyManagerWithFipsCompatibility(HmacPrfKeyManager.legacyKeyManager, HmacPrfKeyManager.FIPS, newKeyAllowed);
    }
    
    public static final KeyTemplate hmacSha256Template() {
        return TinkBugException.exceptionIsBug(() -> KeyTemplate.createFrom(HmacPrfParameters.builder().setKeySizeBytes(32).setHashType(HmacPrfParameters.HashType.SHA256).build()));
    }
    
    public static final KeyTemplate hmacSha512Template() {
        return TinkBugException.exceptionIsBug(() -> KeyTemplate.createFrom(HmacPrfParameters.builder().setKeySizeBytes(64).setHashType(HmacPrfParameters.HashType.SHA512).build()));
    }
    
    private HmacPrfKeyManager() {
    }
    
    static {
        PRF_PRIMITIVE_CONSTRUCTOR = PrimitiveConstructor.create(PrfHmacJce::create, HmacPrfKey.class, Prf.class);
        legacyKeyManager = LegacyKeyManagerImpl.create(getKeyType(), Prf.class, KeyData.KeyMaterialType.SYMMETRIC, com.google.crypto.tink.proto.HmacPrfKey.parser());
        KEY_CREATOR = HmacPrfKeyManager::newKey;
        KEY_DERIVER = HmacPrfKeyManager::createHmacKeyFromRandomness;
        FIPS = TinkFipsUtil.AlgorithmFipsCompatibility.ALGORITHM_REQUIRES_BORINGCRYPTO;
    }
}
