116 lines
4.9 KiB
Java
116 lines
4.9 KiB
Java
|
package org.bouncycastle.pqc.jcajce.provider.mceliece;
|
||
|
|
||
|
import java.io.ByteArrayOutputStream;
|
||
|
import java.security.InvalidAlgorithmParameterException;
|
||
|
import java.security.InvalidKeyException;
|
||
|
import java.security.Key;
|
||
|
import java.security.NoSuchAlgorithmException;
|
||
|
import java.security.PrivateKey;
|
||
|
import java.security.PublicKey;
|
||
|
import java.security.SecureRandom;
|
||
|
import java.security.spec.AlgorithmParameterSpec;
|
||
|
import javax.crypto.BadPaddingException;
|
||
|
import javax.crypto.IllegalBlockSizeException;
|
||
|
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
|
||
|
import org.bouncycastle.asn1.x509.X509ObjectIdentifiers;
|
||
|
import org.bouncycastle.crypto.Digest;
|
||
|
import org.bouncycastle.crypto.digests.SHA1Digest;
|
||
|
import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
|
||
|
import org.bouncycastle.crypto.params.ParametersWithRandom;
|
||
|
import org.bouncycastle.pqc.crypto.mceliece.McElieceCCA2KeyParameters;
|
||
|
import org.bouncycastle.pqc.crypto.mceliece.McElieceFujisakiCipher;
|
||
|
import org.bouncycastle.pqc.jcajce.provider.util.AsymmetricHybridCipher;
|
||
|
|
||
|
/* loaded from: classes6.dex */
|
||
|
public class McElieceFujisakiCipherSpi extends AsymmetricHybridCipher implements PKCSObjectIdentifiers, X509ObjectIdentifiers {
|
||
|
private ByteArrayOutputStream buf = new ByteArrayOutputStream();
|
||
|
private McElieceFujisakiCipher cipher;
|
||
|
private Digest digest;
|
||
|
|
||
|
@Override // org.bouncycastle.pqc.jcajce.provider.util.AsymmetricHybridCipher
|
||
|
public int decryptOutputSize(int i) {
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.pqc.jcajce.provider.util.AsymmetricHybridCipher
|
||
|
public int encryptOutputSize(int i) {
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.pqc.jcajce.provider.util.AsymmetricHybridCipher, org.bouncycastle.pqc.jcajce.provider.util.CipherSpiExt
|
||
|
public byte[] update(byte[] bArr, int i, int i2) {
|
||
|
this.buf.write(bArr, i, i2);
|
||
|
return new byte[0];
|
||
|
}
|
||
|
|
||
|
public byte[] messageEncrypt(byte[] bArr) throws IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException {
|
||
|
try {
|
||
|
return this.cipher.messageEncrypt(bArr);
|
||
|
} catch (Exception unused) {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public byte[] messageDecrypt(byte[] bArr) throws IllegalBlockSizeException, BadPaddingException, NoSuchAlgorithmException {
|
||
|
try {
|
||
|
return this.cipher.messageDecrypt(bArr);
|
||
|
} catch (Exception unused) {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.pqc.jcajce.provider.util.AsymmetricHybridCipher
|
||
|
public void initCipherEncrypt(Key key, AlgorithmParameterSpec algorithmParameterSpec, SecureRandom secureRandom) throws InvalidKeyException, InvalidAlgorithmParameterException {
|
||
|
ParametersWithRandom parametersWithRandom = new ParametersWithRandom(McElieceCCA2KeysToParams.generatePublicKeyParameter((PublicKey) key), secureRandom);
|
||
|
this.digest.reset();
|
||
|
this.cipher.init(true, parametersWithRandom);
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.pqc.jcajce.provider.util.AsymmetricHybridCipher
|
||
|
public void initCipherDecrypt(Key key, AlgorithmParameterSpec algorithmParameterSpec) throws InvalidKeyException, InvalidAlgorithmParameterException {
|
||
|
AsymmetricKeyParameter generatePrivateKeyParameter = McElieceCCA2KeysToParams.generatePrivateKeyParameter((PrivateKey) key);
|
||
|
this.digest.reset();
|
||
|
this.cipher.init(false, generatePrivateKeyParameter);
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.pqc.jcajce.provider.util.CipherSpiExt
|
||
|
public String getName() {
|
||
|
return "McElieceFujisakiCipher";
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.pqc.jcajce.provider.util.CipherSpiExt
|
||
|
public int getKeySize(Key key) throws InvalidKeyException {
|
||
|
return this.cipher.getKeySize((McElieceCCA2KeyParameters) (key instanceof PublicKey ? McElieceCCA2KeysToParams.generatePublicKeyParameter((PublicKey) key) : McElieceCCA2KeysToParams.generatePrivateKeyParameter((PrivateKey) key)));
|
||
|
}
|
||
|
|
||
|
@Override // org.bouncycastle.pqc.jcajce.provider.util.AsymmetricHybridCipher, org.bouncycastle.pqc.jcajce.provider.util.CipherSpiExt
|
||
|
public byte[] doFinal(byte[] bArr, int i, int i2) throws BadPaddingException {
|
||
|
update(bArr, i, i2);
|
||
|
byte[] byteArray = this.buf.toByteArray();
|
||
|
this.buf.reset();
|
||
|
try {
|
||
|
if (this.opMode == 1) {
|
||
|
return this.cipher.messageEncrypt(byteArray);
|
||
|
}
|
||
|
if (this.opMode == 2) {
|
||
|
return this.cipher.messageDecrypt(byteArray);
|
||
|
}
|
||
|
return null;
|
||
|
} catch (Exception unused) {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* loaded from: classes6.dex */
|
||
|
public static class McElieceFujisaki extends McElieceFujisakiCipherSpi {
|
||
|
public McElieceFujisaki() {
|
||
|
super(new SHA1Digest(), new McElieceFujisakiCipher());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
protected McElieceFujisakiCipherSpi(Digest digest, McElieceFujisakiCipher mcElieceFujisakiCipher) {
|
||
|
this.digest = digest;
|
||
|
this.cipher = mcElieceFujisakiCipher;
|
||
|
}
|
||
|
}
|