use of com.github.zhenwei.provider.jce.spec.IESParameterSpec in project LinLong-Java by zhenwei1108.
the class IESCipher method engineInit.
public void engineInit(int opmode, Key key, AlgorithmParameterSpec engineSpec, SecureRandom random) throws InvalidAlgorithmParameterException, InvalidKeyException {
// Use default parameters (including cipher key size) if none are specified
if (engineSpec == null) {
byte[] nonce = null;
if (ivLength != 0 && opmode == Cipher.ENCRYPT_MODE) {
nonce = new byte[ivLength];
random.nextBytes(nonce);
}
this.engineSpec = IESUtil.guessParameterSpec(engine.getCipher(), nonce);
} else if (engineSpec instanceof IESParameterSpec) {
this.engineSpec = (IESParameterSpec) engineSpec;
} else {
throw new InvalidAlgorithmParameterException("must be passed IES parameters");
}
byte[] nonce = this.engineSpec.getNonce();
if (ivLength != 0 && (nonce == null || nonce.length != ivLength)) {
throw new InvalidAlgorithmParameterException("NONCE in IES Parameters needs to be " + ivLength + " bytes long");
}
// Parse the recipient's key
if (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE) {
if (key instanceof DHPublicKey) {
this.key = DHUtil.generatePublicKeyParameter((PublicKey) key);
} else if (key instanceof IESKey) {
IESKey ieKey = (IESKey) key;
this.key = DHUtil.generatePublicKeyParameter(ieKey.getPublic());
this.otherKeyParameter = DHUtil.generatePrivateKeyParameter(ieKey.getPrivate());
} else {
throw new InvalidKeyException("must be passed recipient's public DH key for encryption");
}
} else if (opmode == Cipher.DECRYPT_MODE || opmode == Cipher.UNWRAP_MODE) {
if (key instanceof DHPrivateKey) {
this.key = DHUtil.generatePrivateKeyParameter((PrivateKey) key);
} else if (key instanceof IESKey) {
IESKey ieKey = (IESKey) key;
this.otherKeyParameter = DHUtil.generatePublicKeyParameter(ieKey.getPublic());
this.key = DHUtil.generatePrivateKeyParameter(ieKey.getPrivate());
} else {
throw new InvalidKeyException("must be passed recipient's private DH key for decryption");
}
} else {
throw new InvalidKeyException("must be passed EC key");
}
this.random = random;
this.state = opmode;
buffer.reset();
}
use of com.github.zhenwei.provider.jce.spec.IESParameterSpec in project LinLong-Java by zhenwei1108.
the class IESCipher method engineInit.
public void engineInit(int opmode, Key key, AlgorithmParameterSpec engineSpec, SecureRandom random) throws InvalidAlgorithmParameterException, InvalidKeyException {
otherKeyParameter = null;
// Use default parameters (including cipher key size) if none are specified
if (engineSpec == null) {
byte[] nonce = null;
if (ivLength != 0 && opmode == Cipher.ENCRYPT_MODE) {
nonce = new byte[ivLength];
random.nextBytes(nonce);
}
this.engineSpec = IESUtil.guessParameterSpec(engine.getCipher(), nonce);
} else if (engineSpec instanceof IESParameterSpec) {
this.engineSpec = (IESParameterSpec) engineSpec;
} else {
throw new InvalidAlgorithmParameterException("must be passed IES parameters");
}
byte[] nonce = this.engineSpec.getNonce();
if (ivLength != 0 && (nonce == null || nonce.length != ivLength)) {
throw new InvalidAlgorithmParameterException("NONCE in IES Parameters needs to be " + ivLength + " bytes long");
}
// Parse the recipient's key
if (opmode == Cipher.ENCRYPT_MODE || opmode == Cipher.WRAP_MODE) {
if (key instanceof PublicKey) {
this.key = ECUtils.generatePublicKeyParameter((PublicKey) key);
} else if (key instanceof IESKey) {
IESKey ieKey = (IESKey) key;
this.key = ECUtils.generatePublicKeyParameter(ieKey.getPublic());
this.otherKeyParameter = ECUtil.generatePrivateKeyParameter(ieKey.getPrivate());
} else {
throw new InvalidKeyException("must be passed recipient's public EC key for encryption");
}
} else if (opmode == Cipher.DECRYPT_MODE || opmode == Cipher.UNWRAP_MODE) {
if (key instanceof PrivateKey) {
this.key = ECUtil.generatePrivateKeyParameter((PrivateKey) key);
} else if (key instanceof IESKey) {
IESKey ieKey = (IESKey) key;
this.otherKeyParameter = ECUtils.generatePublicKeyParameter(ieKey.getPublic());
this.key = ECUtil.generatePrivateKeyParameter(ieKey.getPrivate());
} else {
throw new InvalidKeyException("must be passed recipient's private EC key for decryption");
}
} else {
throw new InvalidKeyException("must be passed EC key");
}
this.random = random;
this.state = opmode;
buffer.reset();
}
use of com.github.zhenwei.provider.jce.spec.IESParameterSpec in project LinLong-Java by zhenwei1108.
the class AlgorithmParametersSpi method engineInit.
protected void engineInit(byte[] params) throws IOException {
try {
ASN1Sequence s = (ASN1Sequence) ASN1Primitive.fromByteArray(params);
if (s.size() > 5) {
throw new IOException("sequence too big");
}
byte[] derivationV = null;
byte[] encodingV = null;
BigInteger macKeySize = null;
BigInteger keySize = null;
byte[] nonce = null;
boolean pointCompression = false;
for (Enumeration en = s.getObjects(); en.hasMoreElements(); ) {
Object o = en.nextElement();
if (o instanceof ASN1TaggedObject) {
ASN1TaggedObject t = ASN1TaggedObject.getInstance(o);
if (t.getTagNo() == 0) {
derivationV = ASN1OctetString.getInstance(t, false).getOctets();
} else if (t.getTagNo() == 1) {
encodingV = ASN1OctetString.getInstance(t, false).getOctets();
}
} else if (o instanceof ASN1Integer) {
macKeySize = ASN1Integer.getInstance(o).getValue();
} else if (o instanceof ASN1Sequence) {
ASN1Sequence seq = ASN1Sequence.getInstance(o);
keySize = ASN1Integer.getInstance(seq.getObjectAt(0)).getValue();
nonce = ASN1OctetString.getInstance(seq.getObjectAt(1)).getOctets();
} else if (o instanceof ASN1Boolean) {
pointCompression = ASN1Boolean.getInstance(o).isTrue();
}
}
if (keySize != null) {
this.currentSpec = new IESParameterSpec(derivationV, encodingV, macKeySize.intValue(), keySize.intValue(), nonce, pointCompression);
} else {
this.currentSpec = new IESParameterSpec(derivationV, encodingV, macKeySize.intValue(), -1, null, pointCompression);
}
} catch (ClassCastException e) {
throw new IOException("Not a valid IES Parameter encoding.");
} catch (ArrayIndexOutOfBoundsException e) {
throw new IOException("Not a valid IES Parameter encoding.");
}
}
Aggregations