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

package com.google.crypto.tink.subtle;

import java.util.Arrays;
import java.security.GeneralSecurityException;
import java.nio.ByteBuffer;
import java.security.InvalidKeyException;
import com.google.crypto.tink.aead.internal.InsecureNonceXChaCha20;

class XChaCha20 implements IndCpaCipher
{
    static final int NONCE_LENGTH_IN_BYTES = 24;
    private final InsecureNonceXChaCha20 cipher;
    
    XChaCha20(final byte[] key, final int initialCounter) throws InvalidKeyException {
        this.cipher = new InsecureNonceXChaCha20(key, initialCounter);
    }
    
    @Override
    public byte[] encrypt(final byte[] plaintext) throws GeneralSecurityException {
        final ByteBuffer output = ByteBuffer.allocate(24 + plaintext.length);
        final byte[] nonce = Random.randBytes(24);
        output.put(nonce);
        this.cipher.encrypt();
        return output.array();
    }
    
    @Override
    public byte[] decrypt(final byte[] ciphertext) throws GeneralSecurityException {
        if (ciphertext.length < 24) {
            throw new GeneralSecurityException("ciphertext too short");
        }
        final byte[] nonce = Arrays.copyOf(ciphertext, 24);
        final ByteBuffer rawCiphertext = ByteBuffer.wrap(ciphertext, 24, ciphertext.length - 24);
        return this.cipher.decrypt();
    }
}
