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

package org.bouncycastle.crypto.fpe;

import org.bouncycastle.crypto.params.FPEParameters;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.util.Properties;
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.engines.AESEngine;

public class FPEFF3_1Engine extends FPEEngine
{
    public FPEFF3_1Engine() {
        this(AESEngine.newInstance());
    }
    
    public FPEFF3_1Engine(final BlockCipher blockCipher) {
        super(blockCipher);
        if (blockCipher.getBlockSize() != 16) {
            throw new IllegalArgumentException("base cipher needs to be 128 bits");
        }
        if (Properties.isOverrideSet("org.bouncycastle.fpe.disable")) {
            throw new UnsupportedOperationException("FPE disabled");
        }
    }
    
    @Override
    public void init(final boolean forEncryption, final CipherParameters cipherParameters) {
        this.forEncryption = forEncryption;
        this.fpeParameters = (FPEParameters)cipherParameters;
        this.baseCipher.init(!this.fpeParameters.isUsingInverseFunction(), this.fpeParameters.getKey().reverse());
        if (this.fpeParameters.getTweak().length != 7) {
            throw new IllegalArgumentException("tweak should be 56 bits");
        }
    }
    
    @Override
    public String getAlgorithmName() {
        return "FF3-1";
    }
    
    @Override
    protected int encryptBlock(final byte[] array, final int n, final int n2, final byte[] array2, final int n3) {
        byte[] array3;
        if (this.fpeParameters.getRadix() > 256) {
            array3 = FPEEngine.toByteArray(SP80038G.encryptFF3_1w(this.baseCipher, this.fpeParameters.getRadixConverter(), this.fpeParameters.getTweak(), FPEEngine.toShortArray(array), n, n2 / 2));
        }
        else {
            array3 = SP80038G.encryptFF3_1(this.baseCipher, this.fpeParameters.getRadixConverter(), this.fpeParameters.getTweak(), array, n, n2);
        }
        System.arraycopy(array3, 0, array2, n3, n2);
        return n2;
    }
    
    @Override
    protected int decryptBlock(final byte[] array, final int n, final int n2, final byte[] array2, final int n3) {
        byte[] array3;
        if (this.fpeParameters.getRadix() > 256) {
            array3 = FPEEngine.toByteArray(SP80038G.decryptFF3_1w(this.baseCipher, this.fpeParameters.getRadixConverter(), this.fpeParameters.getTweak(), FPEEngine.toShortArray(array), n, n2 / 2));
        }
        else {
            array3 = SP80038G.decryptFF3_1(this.baseCipher, this.fpeParameters.getRadixConverter(), this.fpeParameters.getTweak(), array, n, n2);
        }
        System.arraycopy(array3, 0, array2, n3, n2);
        return n2;
    }
}
