package org.bouncycastle.jcajce.provider.asymmetric.gost; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.math.BigInteger; import org.bouncycastle.asn1.ASN1ObjectIdentifier; import org.bouncycastle.asn1.ASN1Sequence; import org.bouncycastle.asn1.DEROctetString; import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers; import org.bouncycastle.asn1.cryptopro.GOST3410PublicKeyAlgParameters; import org.bouncycastle.asn1.x509.AlgorithmIdentifier; import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo; import org.bouncycastle.crypto.params.GOST3410PublicKeyParameters; import org.bouncycastle.jcajce.provider.asymmetric.util.KeyUtil; import org.bouncycastle.jce.interfaces.GOST3410Params; import org.bouncycastle.jce.interfaces.GOST3410PublicKey; import org.bouncycastle.jce.spec.GOST3410ParameterSpec; import org.bouncycastle.jce.spec.GOST3410PublicKeyParameterSetSpec; import org.bouncycastle.jce.spec.GOST3410PublicKeySpec; import org.bouncycastle.util.Strings; /* loaded from: classes6.dex */ public class BCGOST3410PublicKey implements GOST3410PublicKey { static final long serialVersionUID = -6251023343619275990L; private transient GOST3410Params gost3410Spec; private BigInteger y; public String toString() { StringBuffer stringBuffer = new StringBuffer("GOST3410 Public Key"); String lineSeparator = Strings.lineSeparator(); stringBuffer.append(lineSeparator); stringBuffer.append(" y: ").append(getY().toString(16)).append(lineSeparator); return stringBuffer.toString(); } public int hashCode() { return this.y.hashCode() ^ this.gost3410Spec.hashCode(); } @Override // org.bouncycastle.jce.interfaces.GOST3410PublicKey public BigInteger getY() { return this.y; } @Override // org.bouncycastle.jce.interfaces.GOST3410Key public GOST3410Params getParameters() { return this.gost3410Spec; } @Override // java.security.Key public String getFormat() { return "X.509"; } @Override // java.security.Key public byte[] getEncoded() { SubjectPublicKeyInfo subjectPublicKeyInfo; SubjectPublicKeyInfo subjectPublicKeyInfo2; byte[] byteArray = getY().toByteArray(); int length = byteArray[0] == 0 ? byteArray.length - 1 : byteArray.length; byte[] bArr = new byte[length]; for (int i = 0; i != length; i++) { bArr[i] = byteArray[(byteArray.length - 1) - i]; } try { GOST3410Params gOST3410Params = this.gost3410Spec; if (!(gOST3410Params instanceof GOST3410ParameterSpec)) { subjectPublicKeyInfo = new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94), new DEROctetString(bArr)); } else { if (gOST3410Params.getEncryptionParamSetOID() != null) { subjectPublicKeyInfo2 = new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94, new GOST3410PublicKeyAlgParameters(new ASN1ObjectIdentifier(this.gost3410Spec.getPublicKeyParamSetOID()), new ASN1ObjectIdentifier(this.gost3410Spec.getDigestParamSetOID()), new ASN1ObjectIdentifier(this.gost3410Spec.getEncryptionParamSetOID()))), new DEROctetString(bArr)); return KeyUtil.getEncodedSubjectPublicKeyInfo(subjectPublicKeyInfo2); } subjectPublicKeyInfo = new SubjectPublicKeyInfo(new AlgorithmIdentifier(CryptoProObjectIdentifiers.gostR3410_94, new GOST3410PublicKeyAlgParameters(new ASN1ObjectIdentifier(this.gost3410Spec.getPublicKeyParamSetOID()), new ASN1ObjectIdentifier(this.gost3410Spec.getDigestParamSetOID()))), new DEROctetString(bArr)); } subjectPublicKeyInfo2 = subjectPublicKeyInfo; return KeyUtil.getEncodedSubjectPublicKeyInfo(subjectPublicKeyInfo2); } catch (IOException unused) { return null; } } @Override // java.security.Key public String getAlgorithm() { return "GOST3410"; } public boolean equals(Object obj) { if (!(obj instanceof BCGOST3410PublicKey)) { return false; } BCGOST3410PublicKey bCGOST3410PublicKey = (BCGOST3410PublicKey) obj; return this.y.equals(bCGOST3410PublicKey.y) && this.gost3410Spec.equals(bCGOST3410PublicKey.gost3410Spec); } private void writeObject(ObjectOutputStream objectOutputStream) throws IOException { Object a; objectOutputStream.defaultWriteObject(); if (this.gost3410Spec.getPublicKeyParamSetOID() != null) { a = this.gost3410Spec.getPublicKeyParamSetOID(); } else { objectOutputStream.writeObject(null); objectOutputStream.writeObject(this.gost3410Spec.getPublicKeyParameters().getP()); objectOutputStream.writeObject(this.gost3410Spec.getPublicKeyParameters().getQ()); a = this.gost3410Spec.getPublicKeyParameters().getA(); } objectOutputStream.writeObject(a); objectOutputStream.writeObject(this.gost3410Spec.getDigestParamSetOID()); objectOutputStream.writeObject(this.gost3410Spec.getEncryptionParamSetOID()); } private void readObject(ObjectInputStream objectInputStream) throws IOException, ClassNotFoundException { objectInputStream.defaultReadObject(); String str = (String) objectInputStream.readObject(); if (str != null) { this.gost3410Spec = new GOST3410ParameterSpec(str, (String) objectInputStream.readObject(), (String) objectInputStream.readObject()); return; } this.gost3410Spec = new GOST3410ParameterSpec(new GOST3410PublicKeyParameterSetSpec((BigInteger) objectInputStream.readObject(), (BigInteger) objectInputStream.readObject(), (BigInteger) objectInputStream.readObject())); objectInputStream.readObject(); objectInputStream.readObject(); } /* JADX INFO: Access modifiers changed from: package-private */ public BCGOST3410PublicKey(GOST3410PublicKeySpec gOST3410PublicKeySpec) { this.y = gOST3410PublicKeySpec.getY(); this.gost3410Spec = new GOST3410ParameterSpec(new GOST3410PublicKeyParameterSetSpec(gOST3410PublicKeySpec.getP(), gOST3410PublicKeySpec.getQ(), gOST3410PublicKeySpec.getA())); } /* JADX INFO: Access modifiers changed from: package-private */ public BCGOST3410PublicKey(GOST3410PublicKey gOST3410PublicKey) { this.y = gOST3410PublicKey.getY(); this.gost3410Spec = gOST3410PublicKey.getParameters(); } /* JADX INFO: Access modifiers changed from: package-private */ public BCGOST3410PublicKey(GOST3410PublicKeyParameters gOST3410PublicKeyParameters, GOST3410ParameterSpec gOST3410ParameterSpec) { this.y = gOST3410PublicKeyParameters.getY(); this.gost3410Spec = gOST3410ParameterSpec; } /* JADX INFO: Access modifiers changed from: package-private */ public BCGOST3410PublicKey(SubjectPublicKeyInfo subjectPublicKeyInfo) { GOST3410PublicKeyAlgParameters gOST3410PublicKeyAlgParameters = new GOST3410PublicKeyAlgParameters((ASN1Sequence) subjectPublicKeyInfo.getAlgorithmId().getParameters()); try { byte[] octets = ((DEROctetString) subjectPublicKeyInfo.parsePublicKey()).getOctets(); byte[] bArr = new byte[octets.length]; for (int i = 0; i != octets.length; i++) { bArr[i] = octets[(octets.length - 1) - i]; } this.y = new BigInteger(1, bArr); this.gost3410Spec = GOST3410ParameterSpec.fromPublicKeyAlg(gOST3410PublicKeyAlgParameters); } catch (IOException unused) { throw new IllegalArgumentException("invalid info structure in GOST3410 public key"); } } BCGOST3410PublicKey(BigInteger bigInteger, GOST3410ParameterSpec gOST3410ParameterSpec) { this.y = bigInteger; this.gost3410Spec = gOST3410ParameterSpec; } }