package org.bouncycastle.pqc.jcajce.provider.rainbow; import java.security.InvalidKeyException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.SecureRandom; import java.security.SignatureException; import java.security.spec.AlgorithmParameterSpec; import org.bouncycastle.crypto.CipherParameters; import org.bouncycastle.crypto.Digest; import org.bouncycastle.crypto.digests.SHA224Digest; import org.bouncycastle.crypto.digests.SHA256Digest; import org.bouncycastle.crypto.digests.SHA384Digest; import org.bouncycastle.crypto.digests.SHA512Digest; import org.bouncycastle.crypto.params.AsymmetricKeyParameter; import org.bouncycastle.crypto.params.ParametersWithRandom; import org.bouncycastle.pqc.crypto.rainbow.RainbowSigner; /* loaded from: classes6.dex */ public class SignatureSpi extends java.security.SignatureSpi { private Digest digest; private SecureRandom random; private RainbowSigner signer; @Override // java.security.SignatureSpi protected boolean engineVerify(byte[] bArr) throws SignatureException { byte[] bArr2 = new byte[this.digest.getDigestSize()]; this.digest.doFinal(bArr2, 0); return this.signer.verifySignature(bArr2, bArr); } @Override // java.security.SignatureSpi protected void engineUpdate(byte[] bArr, int i, int i2) throws SignatureException { this.digest.update(bArr, i, i2); } @Override // java.security.SignatureSpi protected void engineUpdate(byte b) throws SignatureException { this.digest.update(b); } @Override // java.security.SignatureSpi protected byte[] engineSign() throws SignatureException { byte[] bArr = new byte[this.digest.getDigestSize()]; this.digest.doFinal(bArr, 0); try { return this.signer.generateSignature(bArr); } catch (Exception e) { throw new SignatureException(e.toString()); } } @Override // java.security.SignatureSpi protected void engineSetParameter(AlgorithmParameterSpec algorithmParameterSpec) { throw new UnsupportedOperationException("engineSetParameter unsupported"); } @Override // java.security.SignatureSpi protected void engineSetParameter(String str, Object obj) { throw new UnsupportedOperationException("engineSetParameter unsupported"); } @Override // java.security.SignatureSpi protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException { AsymmetricKeyParameter generatePublicKeyParameter = RainbowKeysToParams.generatePublicKeyParameter(publicKey); this.digest.reset(); this.signer.init(false, generatePublicKeyParameter); } @Override // java.security.SignatureSpi protected void engineInitSign(PrivateKey privateKey, SecureRandom secureRandom) throws InvalidKeyException { this.random = secureRandom; engineInitSign(privateKey); } @Override // java.security.SignatureSpi protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException { CipherParameters generatePrivateKeyParameter = RainbowKeysToParams.generatePrivateKeyParameter(privateKey); SecureRandom secureRandom = this.random; if (secureRandom != null) { generatePrivateKeyParameter = new ParametersWithRandom(generatePrivateKeyParameter, secureRandom); } this.digest.reset(); this.signer.init(true, generatePrivateKeyParameter); } /* loaded from: classes6.dex */ public static class withSha224 extends SignatureSpi { public withSha224() { super(new SHA224Digest(), new RainbowSigner()); } } /* loaded from: classes6.dex */ public static class withSha256 extends SignatureSpi { public withSha256() { super(new SHA256Digest(), new RainbowSigner()); } } /* loaded from: classes6.dex */ public static class withSha384 extends SignatureSpi { public withSha384() { super(new SHA384Digest(), new RainbowSigner()); } } /* loaded from: classes6.dex */ public static class withSha512 extends SignatureSpi { public withSha512() { super(new SHA512Digest(), new RainbowSigner()); } } @Override // java.security.SignatureSpi protected Object engineGetParameter(String str) { throw new UnsupportedOperationException("engineSetParameter unsupported"); } protected SignatureSpi(Digest digest, RainbowSigner rainbowSigner) { this.digest = digest; this.signer = rainbowSigner; } }