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

package org.bouncycastle.crypto.signers;

import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.BigIntegers;
import java.security.SecureRandom;
import org.bouncycastle.crypto.Digest;
import java.math.BigInteger;
import org.bouncycastle.crypto.macs.HMac;

public class HMacDSAKCalculator implements DSAKCalculator
{
    private final HMac hMac;
    private final byte[] K;
    private final byte[] V;
    private BigInteger n;
    
    public HMacDSAKCalculator(final Digest digest) {
        this.hMac = new HMac(digest);
        final int macSize = this.hMac.getMacSize();
        this.V = new byte[macSize];
        this.K = new byte[macSize];
    }
    
    @Override
    public boolean isDeterministic() {
        return true;
    }
    
    @Override
    public void init(final BigInteger bigInteger, final SecureRandom secureRandom) {
        throw new IllegalStateException("Operation not supported");
    }
    
    @Override
    public void init(final BigInteger val, final BigInteger bigInteger, final byte[] array) {
        this.n = val;
        BigInteger bigInteger2 = this.bitsToInt(array);
        if (bigInteger2.compareTo(val) >= 0) {
            bigInteger2 = bigInteger2.subtract(val);
        }
        final int unsignedByteLength = BigIntegers.getUnsignedByteLength(val);
        final byte[] unsignedByteArray = BigIntegers.asUnsignedByteArray(unsignedByteLength, bigInteger);
        final byte[] unsignedByteArray2 = BigIntegers.asUnsignedByteArray(unsignedByteLength, bigInteger2);
        Arrays.fill(this.K, (byte)0);
        Arrays.fill(this.V, (byte)1);
        this.hMac.init(new KeyParameter(this.K));
        this.hMac.update(this.V, 0, this.V.length);
        this.hMac.update((byte)0);
        this.hMac.update(unsignedByteArray, 0, unsignedByteArray.length);
        this.hMac.update(unsignedByteArray2, 0, unsignedByteArray2.length);
        this.initAdditionalInput0(this.hMac);
        this.hMac.doFinal(this.K, 0);
        this.hMac.init(new KeyParameter(this.K));
        this.hMac.update(this.V, 0, this.V.length);
        this.hMac.doFinal(this.V, 0);
        this.hMac.update(this.V, 0, this.V.length);
        this.hMac.update((byte)1);
        this.hMac.update(unsignedByteArray, 0, unsignedByteArray.length);
        this.hMac.update(unsignedByteArray2, 0, unsignedByteArray2.length);
        this.initAdditionalInput1(this.hMac);
        this.hMac.doFinal(this.K, 0);
        this.hMac.init(new KeyParameter(this.K));
        this.hMac.update(this.V, 0, this.V.length);
        this.hMac.doFinal(this.V, 0);
    }
    
    @Override
    public BigInteger nextK() {
        final byte[] array = new byte[BigIntegers.getUnsignedByteLength(this.n)];
        BigInteger bitsToInt;
        while (true) {
            int min;
            for (int i = 0; i < array.length; i += min) {
                this.hMac.update(this.V, 0, this.V.length);
                this.hMac.doFinal(this.V, 0);
                min = Math.min(array.length - i, this.V.length);
                System.arraycopy(this.V, 0, array, i, min);
            }
            bitsToInt = this.bitsToInt(array);
            if (bitsToInt.signum() > 0 && bitsToInt.compareTo(this.n) < 0) {
                break;
            }
            this.hMac.update(this.V, 0, this.V.length);
            this.hMac.update((byte)0);
            this.hMac.doFinal(this.K, 0);
            this.hMac.init(new KeyParameter(this.K));
            this.hMac.update(this.V, 0, this.V.length);
            this.hMac.doFinal(this.V, 0);
        }
        return bitsToInt;
    }
    
    protected void initAdditionalInput0(final HMac hMac) {
    }
    
    protected void initAdditionalInput1(final HMac hMac) {
    }
    
    private BigInteger bitsToInt(final byte[] array) {
        final int n = array.length * 8;
        final int bitLength = this.n.bitLength();
        BigInteger bigInteger = BigIntegers.fromUnsignedByteArray(array);
        if (n > bitLength) {
            bigInteger = bigInteger.shiftRight(n - bitLength);
        }
        return bigInteger;
    }
}
