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

package com.google.crypto.tink.subtle;

import com.google.crypto.tink.prf.internal.PrfAesCmacConscrypt;
import java.security.GeneralSecurityException;
import com.google.crypto.tink.util.SecretBytes;
import com.google.crypto.tink.InsecureSecretKeyAccess;
import com.google.crypto.tink.prf.AesCmacPrfParameters;
import com.google.crypto.tink.prf.AesCmacPrfKey;
import com.google.crypto.tink.AccessesPartialKey;
import com.google.errorprone.annotations.Immutable;
import com.google.crypto.tink.prf.Prf;

@Immutable
@AccessesPartialKey
public final class PrfAesCmac implements Prf
{
    private final Prf prf;
    
    @AccessesPartialKey
    private static AesCmacPrfKey createAesCmacPrfKey(final byte[] key) throws GeneralSecurityException {
        return AesCmacPrfKey.create(AesCmacPrfParameters.create(key.length), SecretBytes.copyFrom(key, InsecureSecretKeyAccess.get()));
    }
    
    private PrfAesCmac(final AesCmacPrfKey key) throws GeneralSecurityException {
        this.prf = create(key);
    }
    
    public PrfAesCmac(final byte[] key) throws GeneralSecurityException {
        this(createAesCmacPrfKey(key));
    }
    
    public static Prf create(final AesCmacPrfKey key) throws GeneralSecurityException {
        final Prf prf = com.google.crypto.tink.prf.internal.PrfAesCmac.create(key);
        try {
            final Prf conscryptPrf = PrfAesCmacConscrypt.create(key);
            return new PrfImplementation(prf, conscryptPrf);
        }
        catch (final GeneralSecurityException e) {
            return prf;
        }
    }
    
    @Override
    public byte[] compute(final byte[] data, final int outputLength) throws GeneralSecurityException {
        return this.prf.compute(data, outputLength);
    }
    
    @Immutable
    private static class PrfImplementation implements Prf
    {
        final Prf small;
        final Prf large;
        private static final int SMALL_DATA_SIZE = 64;
        
        @Override
        public byte[] compute(final byte[] data, final int outputLength) throws GeneralSecurityException {
            if (data.length <= 64) {
                return this.small.compute(data, outputLength);
            }
            return this.large.compute(data, outputLength);
        }
        
        private PrfImplementation(final Prf small, final Prf large) {
            this.small = small;
            this.large = large;
        }
    }
}
