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

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

import com.google.crypto.tink.subtle.EngineFactory;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.IvParameterSpec;
import com.google.crypto.tink.internal.Util;
import java.security.spec.AlgorithmParameterSpec;
import java.security.GeneralSecurityException;
import javax.crypto.spec.SecretKeySpec;
import com.google.crypto.tink.subtle.Validators;
import javax.crypto.SecretKey;
import javax.crypto.Cipher;

public final class AesGcmJceUtil
{
    public static final int IV_SIZE_IN_BYTES = 12;
    public static final int TAG_SIZE_IN_BYTES = 16;
    private static final ThreadLocal<Cipher> localCipher;
    
    public static Cipher getThreadLocalCipher() {
        return AesGcmJceUtil.localCipher.get();
    }
    
    public static SecretKey getSecretKey(final byte[] key) throws GeneralSecurityException {
        Validators.validateAesKeySize(key.length);
        return new SecretKeySpec(key, "AES");
    }
    
    public static AlgorithmParameterSpec getParams(final byte[] iv) {
        return getParams(iv, 0, iv.length);
    }
    
    public static AlgorithmParameterSpec getParams(final byte[] buf, final int offset, final int len) {
        final Integer apiLevel = Util.getAndroidApiLevel();
        if (apiLevel != null && apiLevel <= 19) {
            return new IvParameterSpec(buf, offset, len);
        }
        return new GCMParameterSpec(128, buf, offset, len);
    }
    
    private AesGcmJceUtil() {
    }
    
    static {
        localCipher = new ThreadLocal<Cipher>() {
            @Override
            protected Cipher initialValue() {
                try {
                    return EngineFactory.CIPHER.getInstance("AES/GCM/NoPadding");
                }
                catch (final GeneralSecurityException ex) {
                    throw new IllegalStateException(ex);
                }
            }
        };
    }
}
