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

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

import java.security.GeneralSecurityException;
import com.google.crypto.tink.aead.internal.InsecureNonceAesGcmJce;
import java.security.InvalidAlgorithmParameterException;
import com.google.errorprone.annotations.Immutable;

@Immutable
final class AesGcmHpkeAead implements HpkeAead
{
    private final int keyLength;
    
    AesGcmHpkeAead(final int keyLength) throws InvalidAlgorithmParameterException {
        if (keyLength != 16 && keyLength != 32) {
            throw new InvalidAlgorithmParameterException("Unsupported key length: " + keyLength);
        }
        this.keyLength = keyLength;
    }
    
    @Override
    public byte[] seal(final byte[] key, final byte[] nonce, final byte[] plaintext, final int ciphertextOffset, final byte[] associatedData) throws GeneralSecurityException {
        if (key.length != this.keyLength) {
            throw new InvalidAlgorithmParameterException("Unexpected key length: " + key.length);
        }
        final InsecureNonceAesGcmJce aead = new InsecureNonceAesGcmJce(key);
        return aead.encrypt(nonce, plaintext, ciphertextOffset, associatedData);
    }
    
    @Override
    public byte[] open(final byte[] key, final byte[] nonce, final byte[] ciphertext, final int ciphertextOffset, final byte[] associatedData) throws GeneralSecurityException {
        if (key.length != this.keyLength) {
            throw new InvalidAlgorithmParameterException("Unexpected key length: " + key.length);
        }
        final InsecureNonceAesGcmJce aead = new InsecureNonceAesGcmJce(key);
        return aead.decrypt(nonce, ciphertext, ciphertextOffset, associatedData);
    }
    
    @Override
    public byte[] getAeadId() throws GeneralSecurityException {
        switch (this.keyLength) {
            case 16: {
                return HpkeUtil.AES_128_GCM_AEAD_ID;
            }
            case 32: {
                return HpkeUtil.AES_256_GCM_AEAD_ID;
            }
            default: {
                throw new GeneralSecurityException("Could not determine HPKE AEAD ID");
            }
        }
    }
    
    @Override
    public int getKeyLength() {
        return this.keyLength;
    }
    
    @Override
    public int getNonceLength() {
        return 12;
    }
}
