package org.bouncycastle.crypto.signers;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class Ed25519Signer implements org.bouncycastle.crypto.Signer {
    private final org.bouncycastle.crypto.signers.Ed25519Signer.Buffer g = new org.bouncycastle.crypto.signers.Ed25519Signer.Buffer();
    private boolean h;
    private org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters i;
    private org.bouncycastle.crypto.params.Ed25519PublicKeyParameters j;

    private static final class Buffer extends java.io.ByteArrayOutputStream {
        private Buffer() {
        }

        synchronized byte[] a(org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters ed25519PrivateKeyParameters) {
            byte[] bArr;
            bArr = new byte[64];
            ed25519PrivateKeyParameters.h(0, null, ((java.io.ByteArrayOutputStream) this).buf, 0, ((java.io.ByteArrayOutputStream) this).count, bArr, 0);
            reset();
            return bArr;
        }

        synchronized boolean d(org.bouncycastle.crypto.params.Ed25519PublicKeyParameters ed25519PublicKeyParameters, byte[] bArr) {
            if (64 != bArr.length) {
                reset();
                return false;
            }
            boolean zJ = ed25519PublicKeyParameters.j(0, null, ((java.io.ByteArrayOutputStream) this).buf, 0, ((java.io.ByteArrayOutputStream) this).count, bArr, 0);
            reset();
            return zJ;
        }

        @Override // java.io.ByteArrayOutputStream
        public synchronized void reset() {
            org.bouncycastle.util.Arrays.H(((java.io.ByteArrayOutputStream) this).buf, 0, ((java.io.ByteArrayOutputStream) this).count, (byte) 0);
            ((java.io.ByteArrayOutputStream) this).count = 0;
        }
    }

    @Override // org.bouncycastle.crypto.Signer
    public boolean a(byte[] bArr) {
        org.bouncycastle.crypto.params.Ed25519PublicKeyParameters ed25519PublicKeyParameters;
        if (this.h || (ed25519PublicKeyParameters = this.j) == null) {
            throw new java.lang.IllegalStateException("Ed25519Signer not initialised for verification");
        }
        return this.g.d(ed25519PublicKeyParameters, bArr);
    }

    @Override // org.bouncycastle.crypto.Signer
    public byte[] b() {
        org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters ed25519PrivateKeyParameters;
        if (!this.h || (ed25519PrivateKeyParameters = this.i) == null) {
            throw new java.lang.IllegalStateException("Ed25519Signer not initialised for signature generation.");
        }
        return this.g.a(ed25519PrivateKeyParameters);
    }

    public void c() {
        this.g.reset();
    }

    @Override // org.bouncycastle.crypto.Signer
    public void init(boolean z, org.bouncycastle.crypto.CipherParameters cipherParameters) {
        this.h = z;
        if (cipherParameters instanceof org.bouncycastle.crypto.params.ParametersWithRandom) {
            cipherParameters = ((org.bouncycastle.crypto.params.ParametersWithRandom) cipherParameters).a();
        }
        org.bouncycastle.crypto.params.Ed25519PublicKeyParameters ed25519PublicKeyParameters = null;
        if (z) {
            this.i = (org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters) cipherParameters;
        } else {
            this.i = null;
            ed25519PublicKeyParameters = (org.bouncycastle.crypto.params.Ed25519PublicKeyParameters) cipherParameters;
        }
        this.j = ed25519PublicKeyParameters;
        org.bouncycastle.crypto.CryptoServicesRegistrar.a(org.bouncycastle.crypto.signers.Utils.a("Ed25519", 128, cipherParameters, z));
        c();
    }

    @Override // org.bouncycastle.crypto.Signer
    public void update(byte b) throws java.io.IOException {
        this.g.write(b);
    }

    @Override // org.bouncycastle.crypto.Signer
    public void update(byte[] bArr, int i, int i2) throws java.io.IOException {
        this.g.write(bArr, i, i2);
    }
}
