338 lines
14 KiB
Java
338 lines
14 KiB
Java
package org.bouncycastle.jcajce.provider.asymmetric.rsa;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.security.AlgorithmParameters;
|
|
import java.security.InvalidAlgorithmParameterException;
|
|
import java.security.InvalidKeyException;
|
|
import java.security.PrivateKey;
|
|
import java.security.PublicKey;
|
|
import java.security.SecureRandom;
|
|
import java.security.SignatureException;
|
|
import java.security.SignatureSpi;
|
|
import java.security.interfaces.RSAPrivateKey;
|
|
import java.security.interfaces.RSAPublicKey;
|
|
import java.security.spec.AlgorithmParameterSpec;
|
|
import java.security.spec.MGF1ParameterSpec;
|
|
import java.security.spec.PSSParameterSpec;
|
|
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
|
|
import org.bouncycastle.crypto.AsymmetricBlockCipher;
|
|
import org.bouncycastle.crypto.CryptoException;
|
|
import org.bouncycastle.crypto.Digest;
|
|
import org.bouncycastle.crypto.engines.RSABlindedEngine;
|
|
import org.bouncycastle.crypto.params.ParametersWithRandom;
|
|
import org.bouncycastle.crypto.signers.PSSSigner;
|
|
import org.bouncycastle.jcajce.provider.util.DigestFactory;
|
|
import org.bouncycastle.jcajce.util.BCJcaJceHelper;
|
|
import org.bouncycastle.jcajce.util.JcaJceHelper;
|
|
import org.bouncycastle.pqc.jcajce.spec.McElieceCCA2KeyGenParameterSpec;
|
|
import org.bouncycastle.pqc.jcajce.spec.SPHINCS256KeyGenParameterSpec;
|
|
|
|
/* loaded from: classes6.dex */
|
|
public class PSSSignatureSpi extends SignatureSpi {
|
|
private Digest contentDigest;
|
|
private AlgorithmParameters engineParams;
|
|
private final JcaJceHelper helper;
|
|
private boolean isRaw;
|
|
private Digest mgfDigest;
|
|
private PSSParameterSpec originalSpec;
|
|
private PSSParameterSpec paramSpec;
|
|
private PSSSigner pss;
|
|
private int saltLength;
|
|
private AsymmetricBlockCipher signer;
|
|
private byte trailer;
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected boolean engineVerify(byte[] bArr) throws SignatureException {
|
|
return this.pss.verifySignature(bArr);
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected void engineUpdate(byte[] bArr, int i, int i2) throws SignatureException {
|
|
this.pss.update(bArr, i, i2);
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected void engineUpdate(byte b) throws SignatureException {
|
|
this.pss.update(b);
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected byte[] engineSign() throws SignatureException {
|
|
try {
|
|
return this.pss.generateSignature();
|
|
} catch (CryptoException e) {
|
|
throw new SignatureException(e.getMessage());
|
|
}
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected void engineSetParameter(AlgorithmParameterSpec algorithmParameterSpec) throws InvalidAlgorithmParameterException {
|
|
if (!(algorithmParameterSpec instanceof PSSParameterSpec)) {
|
|
throw new InvalidAlgorithmParameterException("Only PSSParameterSpec supported");
|
|
}
|
|
PSSParameterSpec pSSParameterSpec = (PSSParameterSpec) algorithmParameterSpec;
|
|
PSSParameterSpec pSSParameterSpec2 = this.originalSpec;
|
|
if (pSSParameterSpec2 != null && !DigestFactory.isSameDigest(pSSParameterSpec2.getDigestAlgorithm(), pSSParameterSpec.getDigestAlgorithm())) {
|
|
StringBuilder sb = new StringBuilder("parameter must be using ");
|
|
sb.append(this.originalSpec.getDigestAlgorithm());
|
|
throw new InvalidAlgorithmParameterException(sb.toString());
|
|
}
|
|
if (!pSSParameterSpec.getMGFAlgorithm().equalsIgnoreCase("MGF1") && !pSSParameterSpec.getMGFAlgorithm().equals(PKCSObjectIdentifiers.id_mgf1.getId())) {
|
|
throw new InvalidAlgorithmParameterException("unknown mask generation function specified");
|
|
}
|
|
if (!(pSSParameterSpec.getMGFParameters() instanceof MGF1ParameterSpec)) {
|
|
throw new InvalidAlgorithmParameterException("unknown MGF parameters");
|
|
}
|
|
MGF1ParameterSpec mGF1ParameterSpec = (MGF1ParameterSpec) pSSParameterSpec.getMGFParameters();
|
|
if (!DigestFactory.isSameDigest(mGF1ParameterSpec.getDigestAlgorithm(), pSSParameterSpec.getDigestAlgorithm())) {
|
|
throw new InvalidAlgorithmParameterException("digest algorithm for MGF should be the same as for PSS parameters.");
|
|
}
|
|
Digest digest = DigestFactory.getDigest(mGF1ParameterSpec.getDigestAlgorithm());
|
|
if (digest == null) {
|
|
StringBuilder sb2 = new StringBuilder("no match on MGF digest algorithm: ");
|
|
sb2.append(mGF1ParameterSpec.getDigestAlgorithm());
|
|
throw new InvalidAlgorithmParameterException(sb2.toString());
|
|
}
|
|
this.engineParams = null;
|
|
this.paramSpec = pSSParameterSpec;
|
|
this.mgfDigest = digest;
|
|
this.saltLength = pSSParameterSpec.getSaltLength();
|
|
this.trailer = getTrailer(this.paramSpec.getTrailerField());
|
|
setupContentDigest();
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected void engineSetParameter(String str, Object obj) {
|
|
throw new UnsupportedOperationException("engineSetParameter unsupported");
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: package-private */
|
|
/* loaded from: classes6.dex */
|
|
public class NullPssDigest implements Digest {
|
|
private Digest baseDigest;
|
|
final PSSSignatureSpi this$0;
|
|
private ByteArrayOutputStream bOut = new ByteArrayOutputStream();
|
|
private boolean oddTime = true;
|
|
|
|
public int getByteLength() {
|
|
return 0;
|
|
}
|
|
|
|
@Override // org.bouncycastle.crypto.Digest
|
|
public void update(byte[] bArr, int i, int i2) {
|
|
this.bOut.write(bArr, i, i2);
|
|
}
|
|
|
|
@Override // org.bouncycastle.crypto.Digest
|
|
public void update(byte b) {
|
|
this.bOut.write(b);
|
|
}
|
|
|
|
@Override // org.bouncycastle.crypto.Digest
|
|
public void reset() {
|
|
this.bOut.reset();
|
|
this.baseDigest.reset();
|
|
}
|
|
|
|
@Override // org.bouncycastle.crypto.Digest
|
|
public int getDigestSize() {
|
|
return this.baseDigest.getDigestSize();
|
|
}
|
|
|
|
@Override // org.bouncycastle.crypto.Digest
|
|
public String getAlgorithmName() {
|
|
return "NULL";
|
|
}
|
|
|
|
@Override // org.bouncycastle.crypto.Digest
|
|
public int doFinal(byte[] bArr, int i) {
|
|
byte[] byteArray = this.bOut.toByteArray();
|
|
if (this.oddTime) {
|
|
System.arraycopy(byteArray, 0, bArr, i, byteArray.length);
|
|
} else {
|
|
this.baseDigest.update(byteArray, 0, byteArray.length);
|
|
this.baseDigest.doFinal(bArr, i);
|
|
}
|
|
reset();
|
|
this.oddTime = !this.oddTime;
|
|
return byteArray.length;
|
|
}
|
|
|
|
public NullPssDigest(PSSSignatureSpi pSSSignatureSpi, Digest digest) {
|
|
this.this$0 = pSSSignatureSpi;
|
|
this.baseDigest = digest;
|
|
}
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException {
|
|
if (!(publicKey instanceof RSAPublicKey)) {
|
|
throw new InvalidKeyException("Supplied key is not a RSAPublicKey instance");
|
|
}
|
|
PSSSigner pSSSigner = new PSSSigner(this.signer, this.contentDigest, this.mgfDigest, this.saltLength, this.trailer);
|
|
this.pss = pSSSigner;
|
|
pSSSigner.init(false, RSAUtil.generatePublicKeyParameter((RSAPublicKey) publicKey));
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected void engineInitSign(PrivateKey privateKey, SecureRandom secureRandom) throws InvalidKeyException {
|
|
if (!(privateKey instanceof RSAPrivateKey)) {
|
|
throw new InvalidKeyException("Supplied key is not a RSAPrivateKey instance");
|
|
}
|
|
PSSSigner pSSSigner = new PSSSigner(this.signer, this.contentDigest, this.mgfDigest, this.saltLength, this.trailer);
|
|
this.pss = pSSSigner;
|
|
pSSSigner.init(true, new ParametersWithRandom(RSAUtil.generatePrivateKeyParameter((RSAPrivateKey) privateKey), secureRandom));
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
|
|
if (!(privateKey instanceof RSAPrivateKey)) {
|
|
throw new InvalidKeyException("Supplied key is not a RSAPrivateKey instance");
|
|
}
|
|
PSSSigner pSSSigner = new PSSSigner(this.signer, this.contentDigest, this.mgfDigest, this.saltLength, this.trailer);
|
|
this.pss = pSSSigner;
|
|
pSSSigner.init(true, RSAUtil.generatePrivateKeyParameter((RSAPrivateKey) privateKey));
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected AlgorithmParameters engineGetParameters() {
|
|
if (this.engineParams == null && this.paramSpec != null) {
|
|
try {
|
|
AlgorithmParameters createAlgorithmParameters = this.helper.createAlgorithmParameters("PSS");
|
|
this.engineParams = createAlgorithmParameters;
|
|
createAlgorithmParameters.init(this.paramSpec);
|
|
} catch (Exception e) {
|
|
throw new RuntimeException(e.toString());
|
|
}
|
|
}
|
|
return this.engineParams;
|
|
}
|
|
|
|
@Override // java.security.SignatureSpi
|
|
protected Object engineGetParameter(String str) {
|
|
throw new UnsupportedOperationException("engineGetParameter unsupported");
|
|
}
|
|
|
|
private void setupContentDigest() {
|
|
this.contentDigest = this.isRaw ? new NullPssDigest(this, this.mgfDigest) : this.mgfDigest;
|
|
}
|
|
|
|
private byte getTrailer(int i) {
|
|
if (i == 1) {
|
|
return PSSSigner.TRAILER_IMPLICIT;
|
|
}
|
|
throw new IllegalArgumentException("unknown trailer field");
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class PSSwithRSA extends PSSSignatureSpi {
|
|
public PSSwithRSA() {
|
|
super(new RSABlindedEngine(), null);
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class SHA1withRSA extends PSSSignatureSpi {
|
|
public SHA1withRSA() {
|
|
super(new RSABlindedEngine(), PSSParameterSpec.DEFAULT);
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class SHA224withRSA extends PSSSignatureSpi {
|
|
public SHA224withRSA() {
|
|
super(new RSABlindedEngine(), new PSSParameterSpec(McElieceCCA2KeyGenParameterSpec.SHA224, "MGF1", new MGF1ParameterSpec(McElieceCCA2KeyGenParameterSpec.SHA224), 28, 1));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class SHA256withRSA extends PSSSignatureSpi {
|
|
public SHA256withRSA() {
|
|
super(new RSABlindedEngine(), new PSSParameterSpec(McElieceCCA2KeyGenParameterSpec.SHA256, "MGF1", new MGF1ParameterSpec(McElieceCCA2KeyGenParameterSpec.SHA256), 32, 1));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class SHA384withRSA extends PSSSignatureSpi {
|
|
public SHA384withRSA() {
|
|
super(new RSABlindedEngine(), new PSSParameterSpec(McElieceCCA2KeyGenParameterSpec.SHA384, "MGF1", new MGF1ParameterSpec(McElieceCCA2KeyGenParameterSpec.SHA384), 48, 1));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class SHA3_224withRSA extends PSSSignatureSpi {
|
|
public SHA3_224withRSA() {
|
|
super(new RSABlindedEngine(), new PSSParameterSpec("SHA3-224", "MGF1", new MGF1ParameterSpec("SHA3-224"), 28, 1));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class SHA3_256withRSA extends PSSSignatureSpi {
|
|
public SHA3_256withRSA() {
|
|
super(new RSABlindedEngine(), new PSSParameterSpec(SPHINCS256KeyGenParameterSpec.SHA3_256, "MGF1", new MGF1ParameterSpec(SPHINCS256KeyGenParameterSpec.SHA3_256), 32, 1));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class SHA3_384withRSA extends PSSSignatureSpi {
|
|
public SHA3_384withRSA() {
|
|
super(new RSABlindedEngine(), new PSSParameterSpec("SHA3-384", "MGF1", new MGF1ParameterSpec("SHA3-384"), 48, 1));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class SHA3_512withRSA extends PSSSignatureSpi {
|
|
public SHA3_512withRSA() {
|
|
super(new RSABlindedEngine(), new PSSParameterSpec("SHA3-512", "MGF1", new MGF1ParameterSpec("SHA3-512"), 64, 1));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class SHA512_224withRSA extends PSSSignatureSpi {
|
|
public SHA512_224withRSA() {
|
|
super(new RSABlindedEngine(), new PSSParameterSpec("SHA-512(224)", "MGF1", new MGF1ParameterSpec("SHA-512(224)"), 28, 1));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class SHA512_256withRSA extends PSSSignatureSpi {
|
|
public SHA512_256withRSA() {
|
|
super(new RSABlindedEngine(), new PSSParameterSpec("SHA-512(256)", "MGF1", new MGF1ParameterSpec("SHA-512(256)"), 32, 1));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class SHA512withRSA extends PSSSignatureSpi {
|
|
public SHA512withRSA() {
|
|
super(new RSABlindedEngine(), new PSSParameterSpec(McElieceCCA2KeyGenParameterSpec.SHA512, "MGF1", new MGF1ParameterSpec(McElieceCCA2KeyGenParameterSpec.SHA512), 64, 1));
|
|
}
|
|
}
|
|
|
|
/* loaded from: classes6.dex */
|
|
public static class nonePSS extends PSSSignatureSpi {
|
|
public nonePSS() {
|
|
super(new RSABlindedEngine(), null, true);
|
|
}
|
|
}
|
|
|
|
protected PSSSignatureSpi(AsymmetricBlockCipher asymmetricBlockCipher, PSSParameterSpec pSSParameterSpec, boolean z) {
|
|
this.helper = new BCJcaJceHelper();
|
|
this.signer = asymmetricBlockCipher;
|
|
this.originalSpec = pSSParameterSpec;
|
|
if (pSSParameterSpec == null) {
|
|
this.paramSpec = PSSParameterSpec.DEFAULT;
|
|
} else {
|
|
this.paramSpec = pSSParameterSpec;
|
|
}
|
|
this.mgfDigest = DigestFactory.getDigest(this.paramSpec.getDigestAlgorithm());
|
|
this.saltLength = this.paramSpec.getSaltLength();
|
|
this.trailer = getTrailer(this.paramSpec.getTrailerField());
|
|
this.isRaw = z;
|
|
setupContentDigest();
|
|
}
|
|
|
|
protected PSSSignatureSpi(AsymmetricBlockCipher asymmetricBlockCipher, PSSParameterSpec pSSParameterSpec) {
|
|
this(asymmetricBlockCipher, pSSParameterSpec, false);
|
|
}
|
|
}
|