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

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

import java.security.Provider;
import com.google.crypto.tink.internal.ConscryptUtil;
import com.google.crypto.tink.util.Bytes;
import com.google.crypto.tink.mac.ChunkedMacVerification;
import java.security.GeneralSecurityException;
import com.google.crypto.tink.mac.ChunkedMacComputation;
import com.google.crypto.tink.mac.AesCmacKey;
import com.google.crypto.tink.config.internal.TinkFipsUtil;
import com.google.errorprone.annotations.Immutable;
import com.google.crypto.tink.mac.ChunkedMac;

@Immutable
public final class ChunkedAesCmacImpl implements ChunkedMac
{
    private static final TinkFipsUtil.AlgorithmFipsCompatibility FIPS;
    private final AesCmacKey key;
    
    public ChunkedAesCmacImpl(final AesCmacKey key) {
        this.key = key;
    }
    
    @Override
    public ChunkedMacComputation createComputation() throws GeneralSecurityException {
        return new ChunkedAesCmacComputation(this.key);
    }
    
    @Override
    public ChunkedMacVerification createVerification(final byte[] tag) throws GeneralSecurityException {
        if (tag.length < this.key.getOutputPrefix().size()) {
            throw new GeneralSecurityException("Tag too short");
        }
        if (!this.key.getOutputPrefix().equals(Bytes.copyFrom(tag, 0, this.key.getOutputPrefix().size()))) {
            throw new GeneralSecurityException("Wrong tag prefix");
        }
        return ChunkedMacVerificationFromComputation.create(new ChunkedAesCmacComputation(this.key), tag);
    }
    
    public static ChunkedMac create(final AesCmacKey key) throws GeneralSecurityException {
        if (!ChunkedAesCmacImpl.FIPS.isCompatible()) {
            throw new GeneralSecurityException("Cannot use AES-CMAC in FIPS-mode.");
        }
        final Provider conscrypt = ConscryptUtil.providerOrNull();
        if (conscrypt != null) {
            try {
                return ChunkedAesCmacConscrypt.create(key, conscrypt);
            }
            catch (final GeneralSecurityException ex) {}
        }
        return new ChunkedAesCmacImpl(key);
    }
    
    static {
        FIPS = TinkFipsUtil.AlgorithmFipsCompatibility.ALGORITHM_NOT_FIPS;
    }
}
