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

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

import java.util.Arrays;
import java.security.InvalidAlgorithmParameterException;
import javax.crypto.spec.SecretKeySpec;
import com.google.crypto.tink.InsecureSecretKeyAccess;
import javax.crypto.Mac;
import java.security.GeneralSecurityException;
import com.google.crypto.tink.internal.ConscryptUtil;
import com.google.crypto.tink.prf.AesCmacPrfKey;
import java.security.Provider;
import java.security.Key;
import com.google.crypto.tink.config.internal.TinkFipsUtil;
import com.google.crypto.tink.AccessesPartialKey;
import com.google.errorprone.annotations.Immutable;
import com.google.crypto.tink.prf.Prf;

@Immutable
@AccessesPartialKey
public final class PrfAesCmacConscrypt implements Prf
{
    private static final TinkFipsUtil.AlgorithmFipsCompatibility FIPS;
    private final Key key;
    private final Provider conscrypt;
    
    public static Prf create(final AesCmacPrfKey key) throws GeneralSecurityException {
        final Provider conscrypt = ConscryptUtil.providerOrNull();
        if (conscrypt == null) {
            throw new GeneralSecurityException("Conscrypt not available");
        }
        final Mac unused = Mac.getInstance("AESCMAC", conscrypt);
        return new PrfAesCmacConscrypt(key.getKeyBytes().toByteArray(InsecureSecretKeyAccess.get()), conscrypt);
    }
    
    private PrfAesCmacConscrypt(final byte[] keyBytes, final Provider conscrypt) throws GeneralSecurityException {
        if (!PrfAesCmacConscrypt.FIPS.isCompatible()) {
            throw new GeneralSecurityException("Cannot use AES-CMAC in FIPS-mode, as BoringCrypto module is not available");
        }
        this.key = new SecretKeySpec(keyBytes, "AES");
        this.conscrypt = conscrypt;
    }
    
    @Override
    public byte[] compute(final byte[] data, final int outputLength) throws GeneralSecurityException {
        if (outputLength > 16) {
            throw new InvalidAlgorithmParameterException("outputLength must not be larger than 16");
        }
        final Mac mac = Mac.getInstance("AESCMAC", this.conscrypt);
        mac.init(this.key);
        final byte[] result = mac.doFinal(data);
        if (outputLength == result.length) {
            return result;
        }
        return Arrays.copyOf(result, outputLength);
    }
    
    static {
        FIPS = TinkFipsUtil.AlgorithmFipsCompatibility.ALGORITHM_NOT_FIPS;
    }
}
