use of com.github.zhenwei.provider.jcajce.spec.DHUParameterSpec in project LinLong-Java by zhenwei1108.
the class KeyAgreementSpi method engineInit.
protected void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
if (!(key instanceof DHPrivateKey)) {
throw new InvalidKeyException("DHKeyAgreement requires DHPrivateKey for initialisation");
}
DHPrivateKey privKey = (DHPrivateKey) key;
if (params != null) {
if (// p, g override.
params instanceof DHParameterSpec) {
DHParameterSpec p = (DHParameterSpec) params;
this.p = p.getP();
this.g = p.getG();
this.dheParameters = null;
this.ukmParameters = null;
} else if (params instanceof DHUParameterSpec) {
if (unifiedAgreement == null) {
throw new InvalidAlgorithmParameterException("agreement algorithm not DHU based");
}
this.p = privKey.getParams().getP();
this.g = privKey.getParams().getG();
this.dheParameters = (DHUParameterSpec) params;
this.ukmParameters = ((DHUParameterSpec) params).getUserKeyingMaterial();
if (dheParameters.getEphemeralPublicKey() != null) {
unifiedAgreement.init(new DHUPrivateParameters(generatePrivateKeyParameter(privKey), generatePrivateKeyParameter(dheParameters.getEphemeralPrivateKey()), generatePublicKeyParameter(dheParameters.getEphemeralPublicKey())));
} else {
unifiedAgreement.init(new DHUPrivateParameters(generatePrivateKeyParameter(privKey), generatePrivateKeyParameter(dheParameters.getEphemeralPrivateKey())));
}
} else if (params instanceof MQVParameterSpec) {
if (mqvAgreement == null) {
throw new InvalidAlgorithmParameterException("agreement algorithm not MQV based");
}
this.p = privKey.getParams().getP();
this.g = privKey.getParams().getG();
this.mqvParameters = (MQVParameterSpec) params;
this.ukmParameters = ((MQVParameterSpec) params).getUserKeyingMaterial();
if (mqvParameters.getEphemeralPublicKey() != null) {
mqvAgreement.init(new DHMQVPrivateParameters(generatePrivateKeyParameter(privKey), generatePrivateKeyParameter(mqvParameters.getEphemeralPrivateKey()), generatePublicKeyParameter(mqvParameters.getEphemeralPublicKey())));
} else {
mqvAgreement.init(new DHMQVPrivateParameters(generatePrivateKeyParameter(privKey), generatePrivateKeyParameter(mqvParameters.getEphemeralPrivateKey())));
}
} else if (params instanceof UserKeyingMaterialSpec) {
if (kdf == null) {
throw new InvalidAlgorithmParameterException("no KDF specified for UserKeyingMaterialSpec");
}
this.p = privKey.getParams().getP();
this.g = privKey.getParams().getG();
this.dheParameters = null;
this.ukmParameters = ((UserKeyingMaterialSpec) params).getUserKeyingMaterial();
} else {
throw new InvalidAlgorithmParameterException("DHKeyAgreement only accepts DHParameterSpec");
}
} else {
this.p = privKey.getParams().getP();
this.g = privKey.getParams().getG();
}
this.x = privKey.getX();
this.result = bigIntToBytes(x);
}
use of com.github.zhenwei.provider.jcajce.spec.DHUParameterSpec in project LinLong-Java by zhenwei1108.
the class KeyAgreementSpi method engineInit.
protected void engineInit(Key key, AlgorithmParameterSpec params, SecureRandom secureRandom) throws InvalidKeyException, InvalidAlgorithmParameterException {
AsymmetricKeyParameter priv = getLwXDHKeyPrivate(key);
if (priv instanceof X25519PrivateKeyParameters) {
agreement = getAgreement("X25519");
} else if (priv instanceof X448PrivateKeyParameters) {
agreement = getAgreement("X448");
} else {
throw new IllegalStateException("unsupported private key type");
}
ukmParameters = null;
if (params instanceof DHUParameterSpec) {
if (kaAlgorithm.indexOf('U') < 0) {
throw new InvalidAlgorithmParameterException("agreement algorithm not DHU based");
}
dhuSpec = (DHUParameterSpec) params;
ukmParameters = dhuSpec.getUserKeyingMaterial();
agreement.init(new XDHUPrivateParameters(priv, ((BCXDHPrivateKey) dhuSpec.getEphemeralPrivateKey()).engineGetKeyParameters(), ((BCXDHPublicKey) dhuSpec.getEphemeralPublicKey()).engineGetKeyParameters()));
} else {
agreement.init(priv);
if (params instanceof UserKeyingMaterialSpec) {
if (kdf == null) {
throw new InvalidAlgorithmParameterException("no KDF specified for UserKeyingMaterialSpec");
}
this.ukmParameters = ((UserKeyingMaterialSpec) params).getUserKeyingMaterial();
} else {
throw new InvalidAlgorithmParameterException("unknown ParameterSpec");
}
}
if (kdf != null && ukmParameters == null) {
ukmParameters = new byte[0];
}
}
use of com.github.zhenwei.provider.jcajce.spec.DHUParameterSpec in project LinLong-Java by zhenwei1108.
the class KeyAgreementSpi method initFromKey.
private void initFromKey(Key key, AlgorithmParameterSpec parameterSpec) throws InvalidKeyException, InvalidAlgorithmParameterException {
if (agreement instanceof ECMQVBasicAgreement) {
mqvParameters = null;
if (!(key instanceof MQVPrivateKey) && !(parameterSpec instanceof MQVParameterSpec)) {
throw new InvalidAlgorithmParameterException(kaAlgorithm + " key agreement requires " + getSimpleName(MQVParameterSpec.class) + " for initialisation");
}
ECPrivateKeyParameters staticPrivKey;
ECPrivateKeyParameters ephemPrivKey;
ECPublicKeyParameters ephemPubKey;
if (key instanceof MQVPrivateKey) {
MQVPrivateKey mqvPrivKey = (MQVPrivateKey) key;
staticPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(mqvPrivKey.getStaticPrivateKey());
ephemPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(mqvPrivKey.getEphemeralPrivateKey());
ephemPubKey = null;
if (mqvPrivKey.getEphemeralPublicKey() != null) {
ephemPubKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(mqvPrivKey.getEphemeralPublicKey());
}
} else {
MQVParameterSpec mqvParameterSpec = (MQVParameterSpec) parameterSpec;
staticPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter((PrivateKey) key);
ephemPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(mqvParameterSpec.getEphemeralPrivateKey());
ephemPubKey = null;
if (mqvParameterSpec.getEphemeralPublicKey() != null) {
ephemPubKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(mqvParameterSpec.getEphemeralPublicKey());
}
mqvParameters = mqvParameterSpec;
ukmParameters = mqvParameterSpec.getUserKeyingMaterial();
}
MQVPrivateParameters localParams = new MQVPrivateParameters(staticPrivKey, ephemPrivKey, ephemPubKey);
this.parameters = staticPrivKey.getParameters();
// TODO Validate that all the keys are using the same parameters?
((ECMQVBasicAgreement) agreement).init(localParams);
} else if (parameterSpec instanceof DHUParameterSpec) {
if (!(agreement instanceof ECDHCUnifiedAgreement)) {
throw new InvalidAlgorithmParameterException(kaAlgorithm + " key agreement cannot be used with " + getSimpleName(DHUParameterSpec.class));
}
DHUParameterSpec dheParameterSpec = (DHUParameterSpec) parameterSpec;
ECPrivateKeyParameters staticPrivKey;
ECPrivateKeyParameters ephemPrivKey;
ECPublicKeyParameters ephemPubKey;
staticPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter((PrivateKey) key);
ephemPrivKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter(dheParameterSpec.getEphemeralPrivateKey());
ephemPubKey = null;
if (dheParameterSpec.getEphemeralPublicKey() != null) {
ephemPubKey = (ECPublicKeyParameters) ECUtils.generatePublicKeyParameter(dheParameterSpec.getEphemeralPublicKey());
}
dheParameters = dheParameterSpec;
ukmParameters = dheParameterSpec.getUserKeyingMaterial();
ECDHUPrivateParameters localParams = new ECDHUPrivateParameters(staticPrivKey, ephemPrivKey, ephemPubKey);
this.parameters = staticPrivKey.getParameters();
((ECDHCUnifiedAgreement) agreement).init(localParams);
} else {
if (!(key instanceof PrivateKey)) {
throw new InvalidKeyException(kaAlgorithm + " key agreement requires " + getSimpleName(ECPrivateKey.class) + " for initialisation");
}
if (kdf == null && parameterSpec instanceof UserKeyingMaterialSpec) {
throw new InvalidAlgorithmParameterException("no KDF specified for UserKeyingMaterialSpec");
}
ECPrivateKeyParameters privKey = (ECPrivateKeyParameters) ECUtil.generatePrivateKeyParameter((PrivateKey) key);
this.parameters = privKey.getParameters();
ukmParameters = (parameterSpec instanceof UserKeyingMaterialSpec) ? ((UserKeyingMaterialSpec) parameterSpec).getUserKeyingMaterial() : null;
((BasicAgreement) agreement).init(privKey);
}
}
Aggregations